Refactor editor sound selection popup using CScrollRegion

Decrease popup size by remove the unused empty space designated for "previewing" sounds.
This commit is contained in:
Robert Müller 2022-09-04 22:59:33 +02:00
parent d1fdb1f9eb
commit 833f690c59

View file

@ -1335,66 +1335,33 @@ static int g_SelectSoundCurrent = -100;
int CEditor::PopupSelectSound(CEditor *pEditor, CUIRect View, void *pContext) int CEditor::PopupSelectSound(CEditor *pEditor, CUIRect View, void *pContext)
{ {
CUIRect ButtonBar, SoundView; const float ButtonHeight = 12.0f;
View.VSplitLeft(80.0f, &ButtonBar, &View); const float ButtonMargin = 2.0f;
View.Margin(10.0f, &SoundView);
static float s_ScrollValue = 0; static CScrollRegion s_ScrollRegion;
float SoundsHeight = pEditor->m_Map.m_vpSounds.size() * 14; vec2 ScrollOffset(0.0f, 0.0f);
float ScrollDifference = SoundsHeight - ButtonBar.h; CScrollRegionParams ScrollParams;
ScrollParams.m_ScrollbarWidth = 10.0f;
ScrollParams.m_ScrollbarMargin = 3.0f;
ScrollParams.m_ScrollUnit = (ButtonHeight + ButtonMargin) * 5;
s_ScrollRegion.Begin(&View, &ScrollOffset, &ScrollParams);
View.y += ScrollOffset.y;
if(pEditor->m_Map.m_vpSounds.size() > 20) // Do we need a scrollbar?
{
CUIRect Scroll;
ButtonBar.VSplitRight(20.0f, &ButtonBar, &Scroll);
s_ScrollValue = pEditor->UI()->DoScrollbarV(&s_ScrollValue, &Scroll, s_ScrollValue);
if(pEditor->UI()->MouseInside(&Scroll) || pEditor->UI()->MouseInside(&ButtonBar))
{
int ScrollNum = (int)((SoundsHeight - ButtonBar.h) / 14.0f) + 1;
if(ScrollNum > 0)
{
if(pEditor->Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
s_ScrollValue = clamp(s_ScrollValue - 1.0f / ScrollNum, 0.0f, 1.0f);
if(pEditor->Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
s_ScrollValue = clamp(s_ScrollValue + 1.0f / ScrollNum, 0.0f, 1.0f);
}
}
}
float SoundStartAt = ScrollDifference * s_ScrollValue;
if(SoundStartAt < 0.0f)
SoundStartAt = 0.0f;
float SoundStopAt = SoundsHeight - ScrollDifference * (1 - s_ScrollValue);
float SoundCur = 0.0f;
for(int i = -1; i < (int)pEditor->m_Map.m_vpSounds.size(); i++) for(int i = -1; i < (int)pEditor->m_Map.m_vpSounds.size(); i++)
{ {
if(SoundCur > SoundStopAt)
break;
if(SoundCur < SoundStartAt)
{
SoundCur += 14.0f;
continue;
}
SoundCur += 14.0f;
CUIRect Button; CUIRect Button;
ButtonBar.HSplitTop(14.0f, &Button, &ButtonBar); View.HSplitTop(ButtonMargin, nullptr, &View);
View.HSplitTop(ButtonHeight, &Button, &View);
if(i == -1) if(s_ScrollRegion.AddRect(Button))
{ {
static int s_NoneButton = 0; static int s_NoneButton = 0;
if(pEditor->DoButton_MenuItem(&s_NoneButton, "None", i == g_SelectSoundCurrent, &Button)) if(pEditor->DoButton_MenuItem(i == -1 ? (void *)&s_NoneButton : &pEditor->m_Map.m_vpSounds[i], i == -1 ? "None" : pEditor->m_Map.m_vpSounds[i]->m_aName, i == g_SelectSoundCurrent, &Button))
g_SelectSoundSelected = -1;
}
else
{
if(pEditor->DoButton_MenuItem(&pEditor->m_Map.m_vpSounds[i], pEditor->m_Map.m_vpSounds[i]->m_aName, i == g_SelectSoundCurrent, &Button))
g_SelectSoundSelected = i; g_SelectSoundSelected = i;
} }
} }
s_ScrollRegion.End();
return 0; return 0;
} }
@ -1403,7 +1370,7 @@ void CEditor::PopupSelectSoundInvoke(int Current, float x, float y)
static int s_SelectSoundPopupId = 0; static int s_SelectSoundPopupId = 0;
g_SelectSoundSelected = -100; g_SelectSoundSelected = -100;
g_SelectSoundCurrent = Current; g_SelectSoundCurrent = Current;
UiInvokePopupMenu(&s_SelectSoundPopupId, 0, x, y, 400, 300, PopupSelectSound); UiInvokePopupMenu(&s_SelectSoundPopupId, 0, x, y, 150, 300, PopupSelectSound);
} }
int CEditor::PopupSelectSoundResult() int CEditor::PopupSelectSoundResult()