Improve voting HUD

Reduce height and empty space and make margins consistent. Slightly increase width for better round corner drawing and to support longer vote descriptions.

Auto-scale description and reason labels and show ellipsis if they don't fit.

Show circular progress indicator for remaining vote time.
This commit is contained in:
Robert Müller 2024-01-29 22:21:15 +01:00
parent fa62d3c360
commit 1955905a19
2 changed files with 50 additions and 48 deletions

View file

@ -142,12 +142,7 @@ void CVoting::Vote(int v)
CVoting::CVoting() CVoting::CVoting()
{ {
ClearOptions(); ClearOptions();
OnReset();
m_Closetime = 0;
m_aDescription[0] = 0;
m_aReason[0] = 0;
m_Yes = m_No = m_Pass = m_Total = 0;
m_Voted = 0;
} }
void CVoting::AddOption(const char *pDescription) void CVoting::AddOption(const char *pDescription)
@ -225,7 +220,7 @@ void CVoting::ClearOptions()
void CVoting::OnReset() void CVoting::OnReset()
{ {
m_Closetime = 0; m_Opentime = m_Closetime = 0;
m_aDescription[0] = 0; m_aDescription[0] = 0;
m_aReason[0] = 0; m_aReason[0] = 0;
m_Yes = m_No = m_Pass = m_Total = 0; m_Yes = m_No = m_Pass = m_Total = 0;
@ -248,6 +243,7 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
{ {
str_copy(m_aDescription, pMsg->m_pDescription); str_copy(m_aDescription, pMsg->m_pDescription);
str_copy(m_aReason, pMsg->m_pReason); str_copy(m_aReason, pMsg->m_pReason);
m_Opentime = time();
m_Closetime = time() + time_freq() * pMsg->m_Timeout; m_Closetime = time() + time_freq() * pMsg->m_Timeout;
if(Client()->RconAuthed()) if(Client()->RconAuthed())
@ -325,77 +321,82 @@ void CVoting::Render()
return; return;
} }
Graphics()->DrawRect(-10, 60 - 2, 100 + 10 + 4 + 5, 46, ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), IGraphics::CORNER_ALL, 5.0f); CUIRect View = {0.0f, 60.0f, 120.0f, 38.0f};
View.Draw(ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), IGraphics::CORNER_R, 3.0f);
View.Margin(3.0f, &View);
TextRender()->TextColor(TextRender()->DefaultTextColor()); SLabelProperties Props;
Props.m_EllipsisAtEnd = true;
CTextCursor Cursor; char aBuf[256];
char aBuf[512];
str_format(aBuf, sizeof(aBuf), Localize("%ds left"), Seconds); str_format(aBuf, sizeof(aBuf), Localize("%ds left"), Seconds);
float tw = TextRender()->TextWidth(6, aBuf, -1, -1.0f);
TextRender()->SetCursor(&Cursor, 5.0f + 100.0f - tw, 60.0f, 6.0f, TEXTFLAG_RENDER);
TextRender()->TextEx(&Cursor, aBuf, -1);
TextRender()->SetCursor(&Cursor, 5.0f, 60.0f, 6.0f, TEXTFLAG_RENDER); CUIRect Row, LeftColumn, RightColumn, ProgressSpinner;
Cursor.m_LineWidth = 100.0f - tw; View.HSplitTop(6.0f, &Row, &View);
Cursor.m_MaxLines = 3; Row.VSplitRight(TextRender()->TextWidth(6.0f, aBuf), &LeftColumn, &RightColumn);
TextRender()->TextEx(&Cursor, VoteDescription(), -1); LeftColumn.VSplitRight(2.0f, &LeftColumn, nullptr);
LeftColumn.VSplitRight(6.0f, &LeftColumn, &ProgressSpinner);
LeftColumn.VSplitRight(2.0f, &LeftColumn, nullptr);
// reason SProgressSpinnerProperties ProgressProps;
ProgressProps.m_Progress = clamp((time() - m_Opentime) / (float)(m_Closetime - m_Opentime), 0.0f, 1.0f);
UI()->RenderProgressSpinner(ProgressSpinner.Center(), ProgressSpinner.h / 2.0f, ProgressProps);
UI()->DoLabel(&RightColumn, aBuf, 6.0f, TEXTALIGN_MR);
Props.m_MaxWidth = LeftColumn.w;
UI()->DoLabel(&LeftColumn, VoteDescription(), 6.0f, TEXTALIGN_ML, Props);
View.HSplitTop(3.0f, nullptr, &View);
View.HSplitTop(6.0f, &Row, &View);
str_format(aBuf, sizeof(aBuf), "%s %s", Localize("Reason:"), VoteReason()); str_format(aBuf, sizeof(aBuf), "%s %s", Localize("Reason:"), VoteReason());
TextRender()->SetCursor(&Cursor, 5.0f, 79.0f, 6.0f, TEXTFLAG_RENDER | TEXTFLAG_STOP_AT_END); Props.m_MaxWidth = Row.w;
Cursor.m_LineWidth = 100.0f; UI()->DoLabel(&Row, aBuf, 6.0f, TEXTALIGN_ML, Props);
TextRender()->TextEx(&Cursor, aBuf, -1);
CUIRect Base = {5, 88, 100, 4}; View.HSplitTop(3.0f, nullptr, &View);
RenderBars(Base); View.HSplitTop(4.0f, &Row, &View);
RenderBars(Row);
View.HSplitTop(3.0f, nullptr, &View);
View.HSplitTop(6.0f, &Row, &View);
Row.VSplitMid(&LeftColumn, &RightColumn, 4.0f);
char aKey[64]; char aKey[64];
m_pClient->m_Binds.GetKey("vote yes", aKey, sizeof(aKey)); m_pClient->m_Binds.GetKey("vote yes", aKey, sizeof(aKey));
str_format(aBuf, sizeof(aBuf), "%s - %s", aKey, Localize("Vote yes")); str_format(aBuf, sizeof(aBuf), "%s - %s", aKey, Localize("Vote yes"));
Base.y += Base.h; TextRender()->TextColor(TakenChoice() == 1 ? ColorRGBA(0.2f, 0.9f, 0.2f, 0.85f) : TextRender()->DefaultTextColor());
Base.h = 12.0f; UI()->DoLabel(&LeftColumn, aBuf, 6.0f, TEXTALIGN_ML);
if(TakenChoice() == 1)
TextRender()->TextColor(ColorRGBA(0.2f, 0.9f, 0.2f, 0.85f));
UI()->DoLabel(&Base, aBuf, 6.0f, TEXTALIGN_ML);
TextRender()->TextColor(TextRender()->DefaultTextColor());
m_pClient->m_Binds.GetKey("vote no", aKey, sizeof(aKey)); m_pClient->m_Binds.GetKey("vote no", aKey, sizeof(aKey));
str_format(aBuf, sizeof(aBuf), "%s - %s", Localize("Vote no"), aKey); str_format(aBuf, sizeof(aBuf), "%s - %s", Localize("Vote no"), aKey);
if(TakenChoice() == -1) TextRender()->TextColor(TakenChoice() == -1 ? ColorRGBA(0.95f, 0.25f, 0.25f, 0.85f) : TextRender()->DefaultTextColor());
TextRender()->TextColor(ColorRGBA(0.9f, 0.2f, 0.2f, 0.85f)); UI()->DoLabel(&RightColumn, aBuf, 6.0f, TEXTALIGN_MR);
UI()->DoLabel(&Base, aBuf, 6.0f, TEXTALIGN_MR);
TextRender()->TextColor(TextRender()->DefaultTextColor()); TextRender()->TextColor(TextRender()->DefaultTextColor());
} }
void CVoting::RenderBars(CUIRect Bars) const void CVoting::RenderBars(CUIRect Bars) const
{ {
Bars.Draw(ColorRGBA(0.8f, 0.8f, 0.8f, 0.5f), IGraphics::CORNER_ALL, Bars.h / 3); Bars.Draw(ColorRGBA(0.8f, 0.8f, 0.8f, 0.5f), IGraphics::CORNER_ALL, Bars.h / 2.0f);
CUIRect Splitter = Bars; CUIRect Splitter;
Splitter.x = Splitter.x + Splitter.w / 2; Bars.VMargin((Bars.w - 2.0f) / 2.0f, &Splitter);
Splitter.w = Splitter.h / 2.0f; Splitter.Draw(ColorRGBA(0.4f, 0.4f, 0.4f, 0.5f), IGraphics::CORNER_NONE, 0.0f);
Splitter.x -= Splitter.w / 2;
Splitter.Draw(ColorRGBA(0.4f, 0.4f, 0.4f, 0.5f), IGraphics::CORNER_ALL, Splitter.h / 4);
if(m_Total) if(m_Total)
{ {
if(m_Yes) if(m_Yes)
{ {
CUIRect YesArea = Bars; CUIRect YesArea;
YesArea.w *= m_Yes / (float)m_Total; Bars.VSplitLeft(Bars.w * m_Yes / m_Total, &YesArea, nullptr);
YesArea.Draw(ColorRGBA(0.2f, 0.9f, 0.2f, 0.85f), IGraphics::CORNER_ALL, Bars.h / 3); YesArea.Draw(ColorRGBA(0.2f, 0.9f, 0.2f, 0.85f), IGraphics::CORNER_ALL, YesArea.h / 2.0f);
} }
if(m_No) if(m_No)
{ {
CUIRect NoArea = Bars; CUIRect NoArea;
NoArea.w *= m_No / (float)m_Total; Bars.VSplitRight(Bars.w * m_No / m_Total, nullptr, &NoArea);
NoArea.x = (Bars.x + Bars.w) - NoArea.w; NoArea.Draw(ColorRGBA(0.9f, 0.2f, 0.2f, 0.85f), IGraphics::CORNER_ALL, NoArea.h / 2.0f);
NoArea.Draw(ColorRGBA(0.9f, 0.2f, 0.2f, 0.85f), IGraphics::CORNER_ALL, Bars.h / 3);
} }
} }
} }

View file

@ -18,6 +18,7 @@ class CVoting : public CComponent
static void ConCallvote(IConsole::IResult *pResult, void *pUserData); static void ConCallvote(IConsole::IResult *pResult, void *pUserData);
static void ConVote(IConsole::IResult *pResult, void *pUserData); static void ConVote(IConsole::IResult *pResult, void *pUserData);
int64_t m_Opentime;
int64_t m_Closetime; int64_t m_Closetime;
char m_aDescription[VOTE_DESC_LENGTH]; char m_aDescription[VOTE_DESC_LENGTH];
char m_aReason[VOTE_REASON_LENGTH]; char m_aReason[VOTE_REASON_LENGTH];