Files
UnleashedRecomp-hedge-dev/UnleashedRecomp/ui/message_window.cpp
Skyth (Asilkan) c0897dd507 Options menu, achievements, and installer wizard. (#19)
* Implemented guest-to-host function pointers (WIP)

Co-Authored-By: Skyth (Asilkan) <19259897+blueskythlikesclouds@users.noreply.github.com>

* function: support more types for function pointers

* Initial options menu implementation.

* Improve options menu visuals.

* Draw fade on borders, center tabs better.

* Adjust line sizes, fix tab text centering.

* Adjust padding & text sizes.

* Fix bar dark gradient effect.

* api: ported BlueBlur headers and misc. research

* Fix config name padding not getting scaled at different resolutions.

* config: use string_view, added method to get value pointer

* config: use std::map for reverse enum template

* Draw config options manually instead of looping through them.

* config: implemented name and enum localisation

* config_detail: move implementation to cpp, relocate sources

* Implemented accessing options menu via pause and title screen

* config: replace MSAA with AntiAliasing enum

* options_menu: implemented info panel and text marquee (see TODOs)

* Draw selection triangles.

* Supersample fonts to 2K.

* Implement options menu navigation.

* Fix duplicate triangles when selecting options.

* Draw scroll bar.

* Adjust scroll bar padding.

* Further scroll bar padding adjustments.

* Draw outer container as an outline.

* Improve marquee text scrolling.

* CTitleMenu: fix options menu re-entering on A press whilst visible

* Make procedural grid pattern more accurate.

* Add enum & bool editing.

* Update English localisation

* Fix input state mapping.

* options_menu: hide menu on Y hold

* CHudPause: fix crash when opening options menu from village/lab

* Implement float slider.

* options_menu: round res scale description resolution

* options_menu: use config callbacks after setting items

* api: fix GameObject layout

* camera_patches: implemented camera X/Y invert

* options_menu: fix buffered A press selecting first option upon entry

* config_locale: update description for Battle Music

* config: added Allow Background Input option

* options_menu: move ATOC option below Anti-Aliasing

* options_menu: only draw header/footer fade in stages

* Handle real-time modifications of some video config values.

* Converge increments only when holding the left/right button.

* Add sound effects to options menu.

* Change some sounds used in options menu.

* Give the final decide sound to bool toggling.

* Add option select animation.

* options_menu: only play slider sound between min/max range

* Apply category select animation.

* config: rename Controls category to Input

* Implement intro transition animation for options menu.

* audio_patches: implemented music volume

* Implement FPS slider.

* Prevent ImGui from displaying OS cursor.

* Fade container brackets during intro transition.

* player_patches: added penalty to Unleash Cancel

* config_locale: update English localisation

* player_patches: ensure Unleash gauge penalty doesn't dip into negatives

* options_menu: fix being unable to press A at least once after opening the menu

* CTitleMenu: added open/close sounds to the options menu

* audio_patches: implemented Music and SE volume

* api: update research

* Implemented music volume attenuation for Windows 10+

* api: fix score offset

* Add an interval between consecutive playbacks of the slider sound effect in fastIncrement mode

* config: implemented enum descriptions

* options_menu: fit thumbnail rect to grid, remove menu hide input

* options_menu: fix description wrap width

* camera_patches: fix FOV at narrow aspect ratios

mobile gaming is back on the menu

* options_menu: implemented greyed out options and localisation

* options_menu: allow providing reasons for greyed out options

* audio_patches: check if Windows version is supported

* Update PowerRecomp submodule

* api: more research

* options_menu: forget selected item upon opening

* options_menu: restrict XButtonHoming to title and world map

* window: always hide mouse cursor

The options menu doesn't accept mouse input, so there's not really any point to showing the cursor at all.

* Animate category tab background  from the center.

* Fix clip rect in info panel not getting popped at all times.

* Expose texture loader in "video.h".

* config: use final names and descriptions, label options to be moved to exports

* options_menu: implemented Voice Language (and some misc. clean-up)

* Move Voice Language patch to resident_patches

* config: added Aspect Ratio option (to be implemented)

* options_menu: implemented Subtitles

* Remove triple buffering from options menu, turn it to an enum.

* window: hide mouse cursor on controller input for windowed mode

* window: show window dimensions on title bar when resizing window

* api: update research

* Accept functions directly in GuestToHostFunction & add memory range asserts.

* Add guest_stack_var, improve shared_ptr implementation.

* Handle float/double arguments properly in GuestToHostFunction.

* CHudPause_patches: allocate options strings on stack

* api: update research

* guest_stack_var: allow creation without constructing underlying type

* memory: make assertions lenient towards nullptr

* api: include guest_stack_var in SWA.inl

* audio_patches: don't worry about it

* Implemented achievement overlay (WIP)

* Implemented achievements menu (WIP)

* Clean-up, improved animation and layouts

* options_menu: fix naming convention

* achievements_overlay: implemented queue and hermite interpolation

* achievements_menu: implemented animations and improved navigation

* achievements_menu: improve animation accuracy

* achievements_menu: added timestamps

* achievement_data: added checksum and format verification

* achievement_menu: improved outro animation

* achievement_menu: added total unlocked achievements

* achievement_menu: update sprite animation

* Update resources submodule

* Add installer wizard.

* Skip drawing scanlines when height is 0.

* Tweak install screen to better match the original

* Added arrow circle to installer's header

* Move icon header generation to resources submodule

* Added missing animations and tweaked other ones for installer

* Improve detection for DLC only mode. Add template for message prompts.

* Add language picker.

* window: update icon resources

* Added file_to_c

* Fixes to conversion.

* Implemented message window

* achievement_menu: use selection cursor texture

* Update embedded resources

* Implemented message window

* Merge branch 'bin2c' into options-menu

* Update embedded resources

* Framework for max width for buttons.

* Update embedded resources

* Use textures for pause menu containers

* audio_patches: check if Windows major version is >=10

Just in case.

* installer_wizard: use integer outline for button text

* Added arrow circle spinning animation during installation screen

* achievement_menu: fix timestamp and scroll bar padding

* achievement_overlay: fix achievement name padding

* installer_wizard: fix arrow circle spinning animation misaligning

* Add Scale and Origin to ImGui shaders. Change text to be squashed.

* message_window: implemented mouse input

* installer_wizard: implemented message windows

* achievement_menu: start marquee before timestamp margin

* Fix message box flow.

* message_window: use pause container texture

* Add extra condition for starting the installer.

* message_window: only accept mouse click if option is selected

* Implemented safer way to check if the game is loaded

* Add queued update when using files pickers.

* installer_wizard: implement localisation

* installer_wizard: use enum for localisation

* message_window: fix visibility persisting after window closes

* Fix arrow circle animation and added pulse animation

* Come back check space.

* Implement ZSTD compression in file_to_c.

* Add fade-in/out to installation icons and sleep after hitting 100%

* Implement ImGui font atlas caching.

* Controller navigation.

* Implemented button guide

* CTitleStateMenu: fix start button opening old options menu

* Update resources submodule

* imgui_snapshot: check if game is loaded before accessing XDBF

* message_window: added button guide

* options_menu: increase button guide side margins

* video: disable imgui.ini creation

* Use IM_DELETE for deleting the existing font atlas.

* Remove redundant FlushViewport call.

* Fix ImGui callbacks leaking memory.

* Replace unique_ptr reference arguments with raw pointers.

* Specialize description for resolution scale by reference.

---------

Co-authored-by: Hyper <34012267+hyperbx@users.noreply.github.com>
Co-authored-by: PTKay <jp_moura99@outlook.com>
Co-authored-by: Dario <dariosamo@gmail.com>
2024-12-06 18:52:06 +03:00

352 lines
12 KiB
C++

#include "message_window.h"
#include "imgui_utils.h"
#include <api/SWA.h>
#include <gpu/video.h>
#include <locale/locale.h>
#include <ui/button_guide.h>
#include <app.h>
#include <exports.h>
#include <res/images/common/general_window.dds.h>
#include <decompressor.h>
#include <res/images/common/select_fade.dds.h>
#include <gpu/imgui_snapshot.h>
constexpr double OVERLAY_CONTAINER_COMMON_MOTION_START = 0;
constexpr double OVERLAY_CONTAINER_COMMON_MOTION_END = 11;
constexpr double OVERLAY_CONTAINER_INTRO_FADE_START = 5;
constexpr double OVERLAY_CONTAINER_INTRO_FADE_END = 9;
constexpr double OVERLAY_CONTAINER_OUTRO_FADE_START = 0;
constexpr double OVERLAY_CONTAINER_OUTRO_FADE_END = 4;
static bool g_isAwaitingResult = false;
static bool g_isClosing = false;
static bool g_isControlsVisible = false;
static int g_selectedRowIndex;
static int g_foregroundCount;
static bool g_upWasHeld;
static bool g_downWasHeld;
static double g_appearTime;
static double g_controlsAppearTime;
static ImFont* g_fntSeurat;
static std::unique_ptr<GuestTexture> g_upSelectionCursor;
static std::unique_ptr<GuestTexture> g_upWindow;
std::string g_text;
int g_result;
std::vector<std::string> g_buttons;
int g_defaultButtonIndex;
bool DrawContainer(float appearTime, ImVec2 centre, ImVec2 max, bool isForeground = true)
{
auto drawList = ImGui::GetForegroundDrawList();
ImVec2 _min = { centre.x - max.x, centre.y - max.y };
ImVec2 _max = { centre.x + max.x, centre.y + max.y };
// Expand/retract animation.
auto containerMotion = ComputeMotion(appearTime, OVERLAY_CONTAINER_COMMON_MOTION_START, OVERLAY_CONTAINER_COMMON_MOTION_END);
if (g_isClosing)
{
_min.x = Hermite(_min.x, centre.x, containerMotion);
_max.x = Hermite(_max.x, centre.x, containerMotion);
_min.y = Hermite(_min.y, centre.y, containerMotion);
_max.y = Hermite(_max.y, centre.y, containerMotion);
}
else
{
_min.x = Hermite(centre.x, _min.x, containerMotion);
_max.x = Hermite(centre.x, _max.x, containerMotion);
_min.y = Hermite(centre.y, _min.y, containerMotion);
_max.y = Hermite(centre.y, _max.y, containerMotion);
}
// Transparency fade animation.
auto colourMotion = g_isClosing
? ComputeMotion(appearTime, OVERLAY_CONTAINER_OUTRO_FADE_START, OVERLAY_CONTAINER_OUTRO_FADE_END)
: ComputeMotion(appearTime, OVERLAY_CONTAINER_INTRO_FADE_START, OVERLAY_CONTAINER_INTRO_FADE_END);
auto alpha = g_isClosing
? Lerp(1, 0, colourMotion)
: Lerp(0, 1, colourMotion);
if (!isForeground)
g_foregroundCount++;
if (isForeground)
drawList->AddRectFilled({ 0.0f, 0.0f }, ImGui::GetIO().DisplaySize, IM_COL32(0, 0, 0, 223 * (g_foregroundCount ? 1 : alpha)));
DrawPauseContainer(g_upWindow.get(), _min, _max, alpha);
drawList->PushClipRect(_min, _max);
return containerMotion >= 1.0f && !g_isClosing;
}
void DrawButton(int rowIndex, float yOffset, float width, float height, std::string& text)
{
auto drawList = ImGui::GetForegroundDrawList();
auto clipRectMin = drawList->GetClipRectMin();
auto clipRectMax = drawList->GetClipRectMax();
ImVec2 min = { clipRectMin.x + ((clipRectMax.x - clipRectMin.x) - width) / 2, clipRectMin.y + height * rowIndex + yOffset };
ImVec2 max = { min.x + width, min.y + height };
bool isSelected = rowIndex == g_selectedRowIndex;
if (isSelected)
{
static auto breatheStart = ImGui::GetTime();
auto alpha = Lerp(1.0f, 0.75f, (sin((ImGui::GetTime() - breatheStart) * (2.0f * M_PI / (55.0f / 60.0f))) + 1.0f) / 2.0f);
auto colour = IM_COL32(255, 255, 255, 255 * alpha);
auto width = Scale(11);
auto left = PIXELS_TO_UV_COORDS(64, 64, 0, 0, 11, 50);
auto centre = PIXELS_TO_UV_COORDS(64, 64, 11, 0, 8, 50);
auto right = PIXELS_TO_UV_COORDS(64, 64, 19, 0, 11, 50);
drawList->AddImage(g_upSelectionCursor.get(), min, { min.x + width, max.y }, GET_UV_COORDS(left), colour);
drawList->AddImage(g_upSelectionCursor.get(), { min.x + width, min.y }, { max.x - width, max.y }, GET_UV_COORDS(centre), colour);
drawList->AddImage(g_upSelectionCursor.get(), { max.x - width, min.y }, max, GET_UV_COORDS(right), colour);
}
auto fontSize = Scale(28);
auto textSize = g_fntSeurat->CalcTextSizeA(fontSize, FLT_MAX, 0, text.c_str());
DrawTextWithShadow
(
g_fntSeurat,
fontSize,
{ /* X */ min.x + ((max.x - min.x) - textSize.x) / 2, /* Y */ min.y + ((max.y - min.y) - textSize.y) / 2 },
isSelected ? IM_COL32(255, 128, 0, 255) : IM_COL32(255, 255, 255, 255),
text.c_str()
);
}
static void ResetSelection()
{
g_selectedRowIndex = g_defaultButtonIndex;
g_upWasHeld = false;
g_downWasHeld = false;
}
void MessageWindow::Init()
{
auto& io = ImGui::GetIO();
constexpr float FONT_SCALE = 2.0f;
g_fntSeurat = ImFontAtlasSnapshot::GetFont("FOT-SeuratPro-M.otf", 24.0f * FONT_SCALE);
g_upSelectionCursor = LoadTexture(decompressZstd(g_select_fade, g_select_fade_uncompressed_size).get(), g_select_fade_uncompressed_size);
g_upWindow = LoadTexture(decompressZstd(g_general_window, g_general_window_uncompressed_size).get(), g_general_window_uncompressed_size);
}
void MessageWindow::Draw()
{
if (!s_isVisible)
return;
auto pInputState = g_isGameLoaded ? SWA::CInputState::GetInstance() : nullptr;
auto drawList = ImGui::GetForegroundDrawList();
auto& res = ImGui::GetIO().DisplaySize;
ImVec2 centre = { res.x / 2, res.y / 2 };
auto fontSize = Scale(28);
auto textSize = MeasureCentredParagraph(g_fntSeurat, fontSize, 5, g_text.c_str());
auto textMarginX = Scale(37);
auto textMarginY = Scale(45);
if (DrawContainer(g_appearTime, centre, { textSize.x / 2 + textMarginX, textSize.y / 2 + textMarginY }, !g_isControlsVisible))
{
DrawCentredParagraph
(
g_fntSeurat,
fontSize,
{ centre.x, centre.y + Scale(3) },
5,
g_text.c_str(),
[=](const char* str, ImVec2 pos)
{
DrawTextWithShadow(g_fntSeurat, fontSize, pos, IM_COL32(255, 255, 255, 255), str);
}
);
drawList->PopClipRect();
bool isAccepted = pInputState
? pInputState->GetPadState().IsTapped(SWA::eKeyState_A)
: ImGui::IsMouseClicked(ImGuiMouseButton_Left);
if (g_buttons.size())
{
auto itemWidth = std::max(Scale(162), Scale(CalcWidestTextSize(g_fntSeurat, fontSize, g_buttons)));
auto itemHeight = Scale(57);
auto windowMarginX = Scale(23);
auto windowMarginY = Scale(30);
ImVec2 controlsMax = { /* X */ itemWidth / 2 + windowMarginX, /* Y */ itemHeight / 2 * g_buttons.size() + windowMarginY };
if (g_isControlsVisible && DrawContainer(g_controlsAppearTime, centre, controlsMax))
{
auto rowCount = 0;
for (auto& button : g_buttons)
DrawButton(rowCount++, windowMarginY, itemWidth, itemHeight, button);
if (pInputState)
{
bool upIsHeld = pInputState->GetPadState().IsDown(SWA::eKeyState_DpadUp) ||
pInputState->GetPadState().LeftStickVertical > 0.5f;
bool downIsHeld = pInputState->GetPadState().IsDown(SWA::eKeyState_DpadDown) ||
pInputState->GetPadState().LeftStickVertical < -0.5f;
bool scrollUp = !g_upWasHeld && upIsHeld;
bool scrollDown = !g_downWasHeld && downIsHeld;
if (scrollUp)
{
--g_selectedRowIndex;
if (g_selectedRowIndex < 0)
g_selectedRowIndex = rowCount - 1;
}
else if (scrollDown)
{
++g_selectedRowIndex;
if (g_selectedRowIndex >= rowCount)
g_selectedRowIndex = 0;
}
if (scrollUp || scrollDown)
Game_PlaySound("sys_actstg_pausecursor");
g_upWasHeld = upIsHeld;
g_downWasHeld = downIsHeld;
if (pInputState->GetPadState().IsTapped(SWA::eKeyState_B))
{
g_result = -1;
Game_PlaySound("sys_actstg_pausecansel");
MessageWindow::Close();
}
ButtonGuide::Open
(
{
Button(Localise("Common_Select"), EButtonIcon::A),
Button(Localise("Common_Back"),EButtonIcon::B),
}
);
}
else
{
auto clipRectMin = drawList->GetClipRectMin();
auto clipRectMax = drawList->GetClipRectMax();
g_selectedRowIndex = -1;
for (int i = 0; i < rowCount; i++)
{
ImVec2 itemMin = { clipRectMin.x + windowMarginX, clipRectMin.y + windowMarginY + itemHeight * i };
ImVec2 itemMax = { clipRectMax.x - windowMarginX, clipRectMin.y + windowMarginY + itemHeight * i + itemHeight };
if (ImGui::IsMouseHoveringRect(itemMin, itemMax, false))
g_selectedRowIndex = i;
}
ButtonGuide::Open({ Button(Localise("Common_Select"), EButtonIcon::LMB) });
}
if (g_selectedRowIndex != -1 && isAccepted)
{
g_result = g_selectedRowIndex;
Game_PlaySound("sys_actstg_pausedecide");
MessageWindow::Close();
}
drawList->PopClipRect();
}
else
{
if (!g_isControlsVisible && isAccepted)
{
g_controlsAppearTime = ImGui::GetTime();
g_isControlsVisible = true;
Game_PlaySound("sys_actstg_pausewinopen");
}
}
}
else
{
if (isAccepted)
{
g_result = 0;
MessageWindow::Close();
}
}
}
else if (g_isClosing)
{
s_isVisible = false;
}
}
bool MessageWindow::Open(std::string text, int* result, std::span<std::string> buttons, int defaultButtonIndex)
{
if (!g_isAwaitingResult && *result == -1)
{
s_isVisible = true;
g_isClosing = false;
g_isControlsVisible = false;
g_foregroundCount = 0;
g_appearTime = ImGui::GetTime();
g_controlsAppearTime = ImGui::GetTime();
g_text = text;
g_buttons = std::vector(buttons.begin(), buttons.end());
g_defaultButtonIndex = g_isGameLoaded ? defaultButtonIndex : -1;
ResetSelection();
ButtonGuide::Open({ Button(Localise("Common_Next"), g_isGameLoaded ? EButtonIcon::A : EButtonIcon::LMB) });
Game_PlaySound("sys_actstg_pausewinopen");
g_isAwaitingResult = true;
}
*result = g_result;
return !g_isAwaitingResult;
}
void MessageWindow::Close()
{
if (!g_isClosing)
{
g_appearTime = ImGui::GetTime();
g_controlsAppearTime = ImGui::GetTime();
g_isClosing = true;
g_isControlsVisible = false;
g_foregroundCount = 0;
g_isAwaitingResult = false;
ButtonGuide::Close();
}
Game_PlaySound("sys_actstg_pausewinclose");
}