Skip to content

Commit

Permalink
Improve dropdown menu button icon and label alignment
Browse files Browse the repository at this point in the history
Reduce space for label by the space for the dropdown icon instead of rendering the text behind the icon.

This is more noticeable with smaller dropdown menu buttons, which will be used in the future.
  • Loading branch information
Robyt3 committed Sep 10, 2023
1 parent 62c0d34 commit 936a9d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/game/client/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,9 +866,14 @@ bool CUI::DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float

int CUI::DoButton_Menu(CUIElement &UIElement, const CButtonContainer *pID, const std::function<const char *()> &GetTextLambda, const CUIRect *pRect, const SMenuButtonProperties &Props)
{
CUIRect Text = *pRect;
CUIRect Text = *pRect, DropDownIcon;
Text.HMargin(pRect->h >= 20.0f ? 2.0f : 1.0f, &Text);
Text.HMargin((Text.h * Props.m_FontFactor) / 2.0f, &Text);
if(Props.m_ShowDropDownIcon)
{
Text.VSplitRight(5.0f, &Text, nullptr);
Text.VSplitRight(14.0f, &Text, &DropDownIcon);
}

if(!UIElement.AreRectsInit() || Props.m_HintRequiresStringCheck || Props.m_HintCanChangePositionOrSize || !UIElement.Rect(0)->m_UITextContainer.Valid())
{
Expand Down Expand Up @@ -946,6 +951,14 @@ int CUI::DoButton_Menu(CUIElement &UIElement, const CButtonContainer *pID, const
Index = 1;
Graphics()->TextureClear();
Graphics()->RenderQuadContainer(UIElement.Rect(Index)->m_UIRectQuadContainer, -1);
if(Props.m_ShowDropDownIcon)
{
TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
DoLabel(&DropDownIcon, FONT_ICON_CIRCLE_CHEVRON_DOWN, DropDownIcon.h * CUI::ms_FontmodHeight, TEXTALIGN_MC);
TextRender()->SetRenderFlags(0);
TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
}
ColorRGBA ColorText(TextRender()->DefaultTextColor());
ColorRGBA ColorTextOutline(TextRender()->DefaultTextOutlineColor());
if(UIElement.Rect(0)->m_UITextContainer.Valid())
Expand Down Expand Up @@ -1638,6 +1651,7 @@ int CUI::DoDropDown(CUIRect *pRect, int CurSelection, const char **pStrs, int Nu
SMenuButtonProperties Props;
Props.m_HintRequiresStringCheck = true;
Props.m_HintCanChangePositionOrSize = true;
Props.m_ShowDropDownIcon = true;
if(IsPopupOpen(&State.m_SelectionPopupContext))
Props.m_Corners = IGraphics::CORNER_ALL & (~State.m_SelectionPopupContext.m_Props.m_Corners);
if(DoButton_Menu(State.m_UiElement, &State.m_ButtonContainer, LabelFunc, pRect, Props))
Expand All @@ -1656,15 +1670,6 @@ int CUI::DoDropDown(CUIRect *pRect, int CurSelection, const char **pStrs, int Nu
ShowPopupSelection(pRect->x, pRect->y, &State.m_SelectionPopupContext);
}

CUIRect DropDownIcon;
pRect->HMargin(2.0f, &DropDownIcon);
DropDownIcon.VSplitRight(5.0f, &DropDownIcon, nullptr);
TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
DoLabel(&DropDownIcon, FONT_ICON_CIRCLE_CHEVRON_DOWN, DropDownIcon.h * CUI::ms_FontmodHeight, TEXTALIGN_MR);
TextRender()->SetRenderFlags(0);
TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);

if(State.m_SelectionPopupContext.m_SelectionIndex >= 0)
{
const int NewSelection = State.m_SelectionPopupContext.m_SelectionIndex;
Expand Down
1 change: 1 addition & 0 deletions src/game/client/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ struct SMenuButtonProperties
bool m_HintRequiresStringCheck = false;
bool m_HintCanChangePositionOrSize = false;
bool m_UseIconFont = false;
bool m_ShowDropDownIcon = false;
int m_Corners = IGraphics::CORNER_ALL;
float m_Rounding = 5.0f;
float m_FontFactor = 0.0f;
Expand Down

0 comments on commit 936a9d8

Please sign in to comment.