Improve marquee text scrolling.

This commit is contained in:
Skyth 2024-11-18 18:09:31 +03:00
parent b7607f5611
commit 0d433ed6ff

View file

@ -64,18 +64,16 @@ static void SetShaderModifier(uint32_t shaderModifier)
callbackData->setShaderModifier.shaderModifier = shaderModifier; callbackData->setShaderModifier.shaderModifier = shaderModifier;
} }
// TODO: add delay argument to delay when to start the marquee. Unleashed menus have ~0.9 seconds of delay before the text beings to marquee. static void DrawTextWithMarquee(const ImFont* font, float fontSize, const ImVec2& pos, const ImVec2& min, const ImVec2& max, ImU32 color, const char* text, double time, double delay, double speed)
// TODO: reset the marquee position when a different item is selected. This might have to be handled once we use events for controller input.
static void DrawTextWithMarquee(const ImFont* font, float fontSize, const ImVec2& pos, const ImVec2& min, const ImVec2& max, ImU32 color, const char* text, float speed)
{ {
auto drawList = ImGui::GetForegroundDrawList(); auto drawList = ImGui::GetForegroundDrawList();
auto rectWidth = max.x - min.x; auto rectWidth = max.x - min.x;
auto textSize = g_seuratFont->CalcTextSizeA(fontSize, FLT_MAX, 0.0f, text); auto textSize = g_seuratFont->CalcTextSizeA(fontSize, FLT_MAX, 0.0f, text);
auto textX = pos.x - fmodf(speed * ImGui::GetTime(), textSize.x + rectWidth); auto textX = pos.x - fmodf(std::max(0.0, ImGui::GetTime() - (time + delay)) * speed, textSize.x + rectWidth);
drawList->PushClipRect(min, max, true); drawList->PushClipRect(min, max, true);
if (textX < pos.x) if (textX <= pos.x)
drawList->AddText(font, fontSize, { textX, pos.y }, color, text); drawList->AddText(font, fontSize, { textX, pos.y }, color, text);
if (textX + textSize.x < pos.x) if (textX + textSize.x < pos.x)
@ -253,6 +251,18 @@ static constexpr const char* CATEGORIES[] =
static int32_t g_categoryIndex; static int32_t g_categoryIndex;
static int32_t g_firstVisibleRowIndex; static int32_t g_firstVisibleRowIndex;
static int32_t g_selectedRowIndex; static int32_t g_selectedRowIndex;
static double g_rowSelectionTime;
static bool g_upWasHeld;
static bool g_downWasHeld;
static void ResetSelection()
{
g_firstVisibleRowIndex = 0;
g_selectedRowIndex = 0;
g_rowSelectionTime = ImGui::GetTime();
g_upWasHeld = false;
g_downWasHeld = false;
}
static void DrawCategories() static void DrawCategories()
{ {
@ -278,10 +288,7 @@ static void DrawCategories()
} }
if (moveLeft || moveRight) if (moveLeft || moveRight)
{ ResetSelection();
g_firstVisibleRowIndex = 0;
g_selectedRowIndex = 0;
}
auto drawList = ImGui::GetForegroundDrawList(); auto drawList = ImGui::GetForegroundDrawList();
auto clipRectMin = drawList->GetClipRectMin(); auto clipRectMin = drawList->GetClipRectMin();
@ -387,7 +394,7 @@ static void DrawConfigOption(int32_t rowIndex, float yOffset, const ConfigDef<T>
g_selectedItem = config; g_selectedItem = config;
drawList->AddRectFilledMultiColor(min, max, COLOR0, COLOR0, COLOR1, COLOR1); drawList->AddRectFilledMultiColor(min, max, COLOR0, COLOR0, COLOR1, COLOR1);
DrawTextWithMarquee(g_seuratFont, size, textPos, min, max, IM_COL32_WHITE, configName.c_str(), 250.0f); DrawTextWithMarquee(g_seuratFont, size, textPos, min, max, IM_COL32_WHITE, configName.c_str(), g_rowSelectionTime, 1.0, 250.0);
} }
else else
{ {
@ -453,9 +460,6 @@ static void DrawConfigOption(int32_t rowIndex, float yOffset, const ConfigDef<T>
ResetGradient(); ResetGradient();
} }
static bool g_upWasHeld;
static bool g_downWasHeld;
static void DrawConfigOptions() static void DrawConfigOptions()
{ {
g_selectedItem = nullptr; g_selectedItem = nullptr;
@ -518,19 +522,25 @@ static void DrawConfigOptions()
bool downIsHeld = inputState->GetPadState().IsDown(SWA::eKeyState_DpadDown) || bool downIsHeld = inputState->GetPadState().IsDown(SWA::eKeyState_DpadDown) ||
inputState->GetPadState().LeftStickVertical < -0.5f; inputState->GetPadState().LeftStickVertical < -0.5f;
if (!g_upWasHeld && upIsHeld) bool scrollUp = !g_upWasHeld && upIsHeld;
bool scrollDown = !g_downWasHeld && downIsHeld;
if (scrollUp)
{ {
--g_selectedRowIndex; --g_selectedRowIndex;
if (g_selectedRowIndex < 0) if (g_selectedRowIndex < 0)
g_selectedRowIndex = rowCount - 1; g_selectedRowIndex = rowCount - 1;
} }
else if (!g_downWasHeld && downIsHeld) else if (scrollDown)
{ {
++g_selectedRowIndex; ++g_selectedRowIndex;
if (g_selectedRowIndex >= rowCount) if (g_selectedRowIndex >= rowCount)
g_selectedRowIndex = 0; g_selectedRowIndex = 0;
} }
if (scrollUp || scrollDown)
g_rowSelectionTime = ImGui::GetTime();
g_upWasHeld = upIsHeld; g_upWasHeld = upIsHeld;
g_downWasHeld = downIsHeld; g_downWasHeld = downIsHeld;
@ -653,8 +663,8 @@ void OptionsMenu::Open(bool stage)
{ {
s_isVisible = true; s_isVisible = true;
s_isDimBackground = stage; s_isDimBackground = stage;
g_upWasHeld = false; g_categoryIndex = 0;
g_downWasHeld = false; ResetSelection();
*(bool*)g_memory.Translate(0x8328BB26) = false; *(bool*)g_memory.Translate(0x8328BB26) = false;