mirror of
https://github.com/openmoh/openmohaa.git
synced 2025-04-28 21:57:57 +03:00
Implement UIPulldownMenu (#85)
* Implement UIPulldownMenu * Remove unnecessary value-type member initialization
This commit is contained in:
parent
9814887b10
commit
2821488c93
3 changed files with 380 additions and 31 deletions
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
===========================================================================
|
||||
Copyright (C) 2015 the OpenMoHAA team
|
||||
Copyright (C) 2023 the OpenMoHAA team
|
||||
|
||||
This file is part of OpenMoHAA source code.
|
||||
|
||||
|
@ -20,16 +20,81 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
===========================================================================
|
||||
*/
|
||||
|
||||
#include "../qcommon/localization.h"
|
||||
#include "ui_local.h"
|
||||
|
||||
Event EV_Pulldown_ChildKilled
|
||||
(
|
||||
"_ev_pulldown_child_killed",
|
||||
EV_DEFAULT,
|
||||
NULL,
|
||||
NULL,
|
||||
"Signaled when the sub menu is killed for one reason or another"
|
||||
);
|
||||
|
||||
Event EV_Pulldown_HighlightBGColor
|
||||
(
|
||||
"highlight_bgcolor",
|
||||
EV_DEFAULT,
|
||||
"fff",
|
||||
"r g b",
|
||||
"Set the background color of the highlighted text when a pulldown is used"
|
||||
);
|
||||
|
||||
Event EV_Pulldown_HighlightFGColor
|
||||
(
|
||||
"highlight_fgcolor",
|
||||
EV_DEFAULT,
|
||||
"fff",
|
||||
"r g b",
|
||||
"Set the foreground color of the highlighted text when a pulldown is used"
|
||||
);
|
||||
|
||||
CLASS_DECLARATION( UIWidget, UIPulldownMenu, NULL )
|
||||
{
|
||||
{ &W_LeftMouseDown, &UIPulldownMenu::MousePressed },
|
||||
{ &W_LeftMouseUp, &UIPulldownMenu::MouseReleased },
|
||||
{ &W_LeftMouseDragged, &UIPulldownMenu::MouseDragged },
|
||||
{ &EV_Pulldown_ChildKilled, &UIPulldownMenu::ChildKilled },
|
||||
{ &EV_Pulldown_HighlightBGColor, &UIPulldownMenu::HighlightBGColor },
|
||||
{ &EV_Pulldown_HighlightFGColor, &UIPulldownMenu::HighlightFGColor },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static UIPopupMenu* MenuFromPoint(UIPoint2D& p)
|
||||
{
|
||||
UIWidget *res = uWinMan.FindResponder(p);
|
||||
if (res && res->isSubclassOf(UIPopupMenu))
|
||||
{
|
||||
return (UIPopupMenu*)res;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
UIPulldownMenu::UIPulldownMenu()
|
||||
{
|
||||
// FIXME: stub
|
||||
m_listener = NULL;
|
||||
m_submenu = -1;
|
||||
m_highlightBGColor.r = 0.02f;
|
||||
m_highlightBGColor.g = 0.07f;
|
||||
m_highlightBGColor.b = 0.005f;
|
||||
m_highlightBGColor.a = 1.0f;
|
||||
m_highlightFGColor = (UColor)UHudColor;
|
||||
|
||||
// uninitialized in original game
|
||||
m_submenuptr = NULL;
|
||||
}
|
||||
|
||||
UIPulldownMenu::~UIPulldownMenu()
|
||||
{
|
||||
for (int i = m_desc.NumObjects(); i > 0; i--)
|
||||
{
|
||||
uipull_describe* updb = m_desc.ObjectAt(i);
|
||||
// FIXME: possible OG bug, memory leak? updb->desc is not cleared
|
||||
m_desc.RemoveObjectAt(i);
|
||||
delete updb;
|
||||
}
|
||||
}
|
||||
|
||||
UIRect2D UIPulldownMenu::getAlignmentRect
|
||||
|
@ -38,8 +103,40 @@ UIRect2D UIPulldownMenu::getAlignmentRect
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
return UIRect2D();
|
||||
if (!parent)
|
||||
{
|
||||
parent = (m_parent == NULL) ? &uWinMan : m_parent;
|
||||
}
|
||||
|
||||
UIRect2D parentRect = parent->getClientFrame();
|
||||
if (m_bVirtual)
|
||||
{
|
||||
m_vVirtualScale[0] = (float)uid.vidWidth / SCREEN_WIDTH;
|
||||
m_vVirtualScale[1] = (float)uid.vidHeight / SCREEN_HEIGHT;
|
||||
}
|
||||
|
||||
int maxheight = m_font->getHeight(m_bVirtual);
|
||||
for (int i = 1; i <= m_desc.NumObjects(); i++)
|
||||
{
|
||||
uipull_describe *uipd = m_desc.ObjectAt(i);
|
||||
if (uipd && uipd->material)
|
||||
{
|
||||
uipd->material->ReregisterMaterial();
|
||||
|
||||
int h = uii.Rend_GetShaderHeight(uipd->material->GetMaterial());
|
||||
if (h > maxheight)
|
||||
{
|
||||
maxheight = h;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return UIRect2D(
|
||||
parentRect.pos.x,
|
||||
parentRect.pos.y,
|
||||
parentRect.size.width,
|
||||
(maxheight + 4) * m_vVirtualScale[1]
|
||||
);
|
||||
}
|
||||
|
||||
float UIPulldownMenu::getDescWidth
|
||||
|
@ -48,8 +145,14 @@ float UIPulldownMenu::getDescWidth
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
return 0.0f;
|
||||
UIReggedMaterial* mat = desc->material;
|
||||
if (mat)
|
||||
{
|
||||
mat->ReregisterMaterial();
|
||||
return uii.Rend_GetShaderWidth(mat->GetMaterial());
|
||||
}
|
||||
|
||||
return m_font->getWidth(desc->title, -1) + 8.0f;
|
||||
}
|
||||
|
||||
float UIPulldownMenu::getDescHeight
|
||||
|
@ -58,8 +161,14 @@ float UIPulldownMenu::getDescHeight
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
return 0.0f;
|
||||
UIReggedMaterial* mat = desc->material;
|
||||
if (mat)
|
||||
{
|
||||
mat->ReregisterMaterial();
|
||||
return uii.Rend_GetShaderHeight(mat->GetMaterial());
|
||||
}
|
||||
|
||||
return m_font->getHeight(m_bVirtual);
|
||||
}
|
||||
|
||||
uipull_describe *UIPulldownMenu::getPulldown
|
||||
|
@ -68,8 +177,14 @@ uipull_describe *UIPulldownMenu::getPulldown
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
return NULL;
|
||||
for (int i = 1; i <= m_desc.NumObjects(); i++)
|
||||
{
|
||||
uipull_describe *pd = m_desc.ObjectAt(i);
|
||||
if (pd->title == title)
|
||||
{
|
||||
return pd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::HighlightBGColor
|
||||
|
@ -78,7 +193,10 @@ void UIPulldownMenu::HighlightBGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
m_highlightBGColor.r = ev->GetFloat(1);
|
||||
m_highlightBGColor.g = ev->GetFloat(2);
|
||||
m_highlightBGColor.b = ev->GetFloat(3);
|
||||
m_highlightBGColor.a = 1.0f;
|
||||
}
|
||||
|
||||
void UIPulldownMenu::HighlightFGColor
|
||||
|
@ -87,7 +205,10 @@ void UIPulldownMenu::HighlightFGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
m_highlightFGColor.r = ev->GetFloat(1);
|
||||
m_highlightFGColor.g = ev->GetFloat(2);
|
||||
m_highlightFGColor.b = ev->GetFloat(3);
|
||||
m_highlightFGColor.a = 1.0f;
|
||||
}
|
||||
|
||||
|
||||
|
@ -99,7 +220,8 @@ void UIPulldownMenu::Create
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
m_listener = listener;
|
||||
InitFrame(parent, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height, 0);
|
||||
}
|
||||
|
||||
void UIPulldownMenu::CreateAligned
|
||||
|
@ -109,7 +231,9 @@ void UIPulldownMenu::CreateAligned
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
setFont("verdana-12");
|
||||
UIRect2D rect = getAlignmentRect(parent);
|
||||
Create(parent, listener, rect);
|
||||
}
|
||||
|
||||
void UIPulldownMenu::MousePressed
|
||||
|
@ -118,7 +242,8 @@ void UIPulldownMenu::MousePressed
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
uWinMan.setFirstResponder(this);
|
||||
MouseDragged(ev);
|
||||
}
|
||||
|
||||
void UIPulldownMenu::MouseDragged
|
||||
|
@ -127,7 +252,85 @@ void UIPulldownMenu::MouseDragged
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
int newSubMenu = -1;
|
||||
UIRect2D subRect;
|
||||
uipull_describe* subdesc = NULL;
|
||||
|
||||
UIPoint2D point = MouseEventToClientPoint(ev);
|
||||
if (getClientFrame().contains(point))
|
||||
{
|
||||
float atx = m_vVirtualScale[0] * 2;
|
||||
for (int i = 1; i <= m_desc.NumObjects(); i++)
|
||||
{
|
||||
uipull_describe *desc = m_desc.ObjectAt(i);
|
||||
float width = getDescWidth(desc) * m_vVirtualScale[0];
|
||||
if (atx <= point.x && atx + width > point.x)
|
||||
{
|
||||
newSubMenu = i;
|
||||
subdesc = desc;
|
||||
subRect.pos.x = atx;
|
||||
subRect.pos.y = 0.0f;
|
||||
subRect.size.width = width;
|
||||
subRect.size.height = m_frame.size.height;
|
||||
break;
|
||||
}
|
||||
atx += width;
|
||||
}
|
||||
}
|
||||
|
||||
if (newSubMenu == m_submenu || newSubMenu == -1)
|
||||
{
|
||||
if (m_submenu == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UIPoint2D cursorLocation(
|
||||
point.x + m_screenframe.pos.x,
|
||||
point.y + m_screenframe.pos.y
|
||||
);
|
||||
|
||||
UIPopupMenu* menu = MenuFromPoint(cursorLocation);
|
||||
if (!menu)
|
||||
{
|
||||
UIPopupMenu* submenu = m_submenuptr;
|
||||
while (submenu->m_submenu != -1)
|
||||
{
|
||||
submenu = submenu->m_submenuptr;
|
||||
}
|
||||
|
||||
submenu->MouseMoved(ev);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (menu->m_parentMenu)
|
||||
{
|
||||
menu = menu->m_parentMenu;
|
||||
}
|
||||
if (menu == m_submenuptr)
|
||||
{
|
||||
menu->MouseMoved(ev);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_submenu != -1)
|
||||
{
|
||||
m_submenuptr->Disconnect(this, W_Destroyed);
|
||||
if (m_submenuptr)
|
||||
{
|
||||
delete m_submenuptr;
|
||||
}
|
||||
}
|
||||
|
||||
m_submenu = newSubMenu;
|
||||
m_submenuptr = new UIPopupMenu();
|
||||
subRect.pos.x += m_screenframe.pos.x;
|
||||
subRect.pos.y += m_screenframe.pos.y;
|
||||
m_submenuptr->Create(&subdesc->desc, m_listener, subRect, UIP_WHERE_DOWN, m_bVirtual, -1.0f);
|
||||
m_submenuptr->Connect(this, W_Destroyed, EV_Pulldown_ChildKilled);
|
||||
}
|
||||
|
||||
void UIPulldownMenu::MouseReleased
|
||||
|
@ -136,7 +339,23 @@ void UIPulldownMenu::MouseReleased
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
if (uWinMan.getFirstResponder() == this)
|
||||
{
|
||||
uWinMan.setFirstResponder(NULL);
|
||||
UIPoint2D point(ev->GetFloat(1), ev->GetFloat(2));
|
||||
UIPopupMenu *menu = MenuFromPoint(point);
|
||||
if (menu)
|
||||
{
|
||||
menu->MouseReleased(ev);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_submenu != -1)
|
||||
{
|
||||
m_submenuptr->Disconnect(this, W_Destroyed);
|
||||
delete m_submenuptr;
|
||||
m_submenu = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::ChildKilled
|
||||
|
@ -145,7 +364,15 @@ void UIPulldownMenu::ChildKilled
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
if (m_submenu != -1)
|
||||
{
|
||||
m_submenu = -1;
|
||||
}
|
||||
|
||||
if (uWinMan.getFirstResponder() == this)
|
||||
{
|
||||
uWinMan.setFirstResponder(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::AddUIPopupDescribe
|
||||
|
@ -155,7 +382,19 @@ void UIPulldownMenu::AddUIPopupDescribe
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
if (!title)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uipull_describe *pd = getPulldown(title);
|
||||
if (!pd)
|
||||
{
|
||||
pd = new uipull_describe(title, NULL, NULL);
|
||||
m_desc.AddObject(pd);
|
||||
}
|
||||
|
||||
pd->desc.AddObject(d);
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setHighlightFGColor
|
||||
|
@ -164,7 +403,7 @@ void UIPulldownMenu::setHighlightFGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
m_highlightFGColor = c;
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setHighlightBGColor
|
||||
|
@ -173,7 +412,7 @@ void UIPulldownMenu::setHighlightBGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
m_highlightBGColor = c;
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setPopupHighlightFGColor
|
||||
|
@ -183,7 +422,11 @@ void UIPulldownMenu::setPopupHighlightFGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
uipull_describe* up = getPulldown(menu);
|
||||
if (up)
|
||||
{
|
||||
up->highlightFGColor = c;
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setPopupHighlightBGColor
|
||||
|
@ -193,7 +436,11 @@ void UIPulldownMenu::setPopupHighlightBGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
uipull_describe* up = getPulldown(menu);
|
||||
if (up)
|
||||
{
|
||||
up->highlightBGColor = c;
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setPopupFGColor
|
||||
|
@ -203,7 +450,11 @@ void UIPulldownMenu::setPopupFGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
uipull_describe* up = getPulldown(menu);
|
||||
if (up)
|
||||
{
|
||||
up->FGColor = c;
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setPopupBGColor
|
||||
|
@ -213,7 +464,11 @@ void UIPulldownMenu::setPopupBGColor
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
uipull_describe* up = getPulldown(menu);
|
||||
if (up)
|
||||
{
|
||||
up->BGColor = c;
|
||||
}
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setPulldownShader
|
||||
|
@ -223,7 +478,20 @@ void UIPulldownMenu::setPulldownShader
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
if (!title)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uipull_describe *pd = getPulldown(title);
|
||||
if (!pd)
|
||||
{
|
||||
pd = new uipull_describe(title, NULL, NULL);
|
||||
m_desc.AddObject(pd);
|
||||
}
|
||||
|
||||
pd->material = mat;
|
||||
Realign();
|
||||
}
|
||||
|
||||
void UIPulldownMenu::setSelectedPulldownShader
|
||||
|
@ -233,7 +501,20 @@ void UIPulldownMenu::setSelectedPulldownShader
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
if (!title)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uipull_describe *pd = getPulldown(title);
|
||||
if (!pd)
|
||||
{
|
||||
pd = new uipull_describe(title, NULL, NULL);
|
||||
m_desc.AddObject(pd);
|
||||
}
|
||||
|
||||
pd->selected_material = mat;
|
||||
Realign();
|
||||
}
|
||||
|
||||
void UIPulldownMenu::Realign
|
||||
|
@ -242,7 +523,8 @@ void UIPulldownMenu::Realign
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
UIRect2D rect = getAlignmentRect(NULL);
|
||||
setFrame(rect);
|
||||
}
|
||||
|
||||
void UIPulldownMenu::Draw
|
||||
|
@ -251,5 +533,70 @@ void UIPulldownMenu::Draw
|
|||
)
|
||||
|
||||
{
|
||||
// FIXME: stub
|
||||
float atx = 0.0f;
|
||||
for (int i = 1; i <= m_desc.NumObjects(); i++)
|
||||
{
|
||||
uipull_describe* desc = m_desc.ObjectAt(i);
|
||||
float width = getDescWidth(desc) * m_vVirtualScale[0];
|
||||
float height = getDescHeight(desc) * m_vVirtualScale[1];
|
||||
|
||||
// Set font color based on whether the current item is selected
|
||||
if (m_submenu == i)
|
||||
{
|
||||
// Selected item
|
||||
m_font->setColor(m_highlightFGColor);
|
||||
if (desc->selected_material)
|
||||
{
|
||||
desc->selected_material->ReregisterMaterial();
|
||||
uii.Rend_DrawPicStretched(
|
||||
atx,
|
||||
0.0f,
|
||||
width,
|
||||
height,
|
||||
0.0f,
|
||||
0.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
desc->selected_material->GetMaterial()
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawBox(atx, 0.0f, width, m_frame.size.height, m_highlightBGColor, 1.0f);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unselected item
|
||||
m_font->setColor(m_foreground_color);
|
||||
}
|
||||
|
||||
if (desc->material)
|
||||
{
|
||||
if (m_submenu != i)
|
||||
{
|
||||
desc->material->ReregisterMaterial();
|
||||
uii.Rend_DrawPicStretched(
|
||||
atx,
|
||||
0.0f,
|
||||
width,
|
||||
height,
|
||||
0.0f,
|
||||
0.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
desc->material->GetMaterial()
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *text = Sys_LV_CL_ConvertString(desc->title);
|
||||
float text_xpos = m_vVirtualScale[0] * 4.0f + atx;
|
||||
float text_ypos = m_vVirtualScale[1] * 2.0f;
|
||||
m_font->Print(text_xpos, text_ypos, text, -1, m_bVirtual);
|
||||
}
|
||||
|
||||
atx += width;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue