1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_UI_H
4#define GAME_CLIENT_UI_H
5
6#include "lineinput.h"
7#include "ui_rect.h"
8
9#include <engine/input.h>
10#include <engine/textrender.h>
11
12#include <chrono>
13#include <string>
14#include <vector>
15
16class CScrollRegion;
17class IClient;
18class IGraphics;
19class IKernel;
20
21enum class EEditState
22{
23 NONE,
24 START,
25 EDITING,
26 END,
27 ONE_GO
28};
29
30template<typename T>
31struct SEditResult
32{
33 EEditState m_State;
34 T m_Value;
35};
36
37struct SUIAnimator
38{
39 bool m_Active;
40 bool m_ScaleLabel;
41 bool m_RepositionLabel;
42
43 std::chrono::nanoseconds m_Time;
44 float m_Value;
45
46 float m_XOffset;
47 float m_YOffset;
48 float m_WOffset;
49 float m_HOffset;
50};
51
52class IScrollbarScale
53{
54public:
55 virtual ~IScrollbarScale() = default;
56 virtual float ToRelative(int AbsoluteValue, int Min, int Max) const = 0;
57 virtual int ToAbsolute(float RelativeValue, int Min, int Max) const = 0;
58};
59class CLinearScrollbarScale : public IScrollbarScale
60{
61public:
62 float ToRelative(int AbsoluteValue, int Min, int Max) const override
63 {
64 return (AbsoluteValue - Min) / (float)(Max - Min);
65 }
66 int ToAbsolute(float RelativeValue, int Min, int Max) const override
67 {
68 return round_to_int(f: RelativeValue * (Max - Min) + Min + 0.1f);
69 }
70};
71class CLogarithmicScrollbarScale : public IScrollbarScale
72{
73private:
74 int m_MinAdjustment;
75
76public:
77 CLogarithmicScrollbarScale(int MinAdjustment)
78 {
79 m_MinAdjustment = maximum(a: MinAdjustment, b: 1); // must be at least 1 to support Min == 0 with logarithm
80 }
81 float ToRelative(int AbsoluteValue, int Min, int Max) const override
82 {
83 if(Min < m_MinAdjustment)
84 {
85 AbsoluteValue += m_MinAdjustment;
86 Min += m_MinAdjustment;
87 Max += m_MinAdjustment;
88 }
89 return (std::log(x: AbsoluteValue) - std::log(x: Min)) / (float)(std::log(x: Max) - std::log(x: Min));
90 }
91 int ToAbsolute(float RelativeValue, int Min, int Max) const override
92 {
93 int ResultAdjustment = 0;
94 if(Min < m_MinAdjustment)
95 {
96 Min += m_MinAdjustment;
97 Max += m_MinAdjustment;
98 ResultAdjustment = -m_MinAdjustment;
99 }
100 return round_to_int(f: std::exp(x: RelativeValue * (std::log(x: Max) - std::log(x: Min)) + std::log(x: Min))) + ResultAdjustment;
101 }
102};
103
104class IButtonColorFunction
105{
106public:
107 virtual ~IButtonColorFunction() = default;
108 virtual ColorRGBA GetColor(bool Active, bool Hovered) const = 0;
109};
110class CDarkButtonColorFunction : public IButtonColorFunction
111{
112public:
113 ColorRGBA GetColor(bool Active, bool Hovered) const override
114 {
115 if(Active)
116 return ColorRGBA(0.15f, 0.15f, 0.15f, 0.25f);
117 else if(Hovered)
118 return ColorRGBA(0.5f, 0.5f, 0.5f, 0.25f);
119 return ColorRGBA(0.0f, 0.0f, 0.0f, 0.25f);
120 }
121};
122class CLightButtonColorFunction : public IButtonColorFunction
123{
124public:
125 ColorRGBA GetColor(bool Active, bool Hovered) const override
126 {
127 if(Active)
128 return ColorRGBA(1.0f, 1.0f, 1.0f, 0.4f);
129 else if(Hovered)
130 return ColorRGBA(1.0f, 1.0f, 1.0f, 0.6f);
131 return ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f);
132 }
133};
134class CScrollBarColorFunction : public IButtonColorFunction
135{
136public:
137 ColorRGBA GetColor(bool Active, bool Hovered) const override
138 {
139 if(Active)
140 return ColorRGBA(0.9f, 0.9f, 0.9f, 1.0f);
141 else if(Hovered)
142 return ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
143 return ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f);
144 }
145};
146
147class CUi;
148
149class CUIElement
150{
151 friend class CUi;
152
153 CUi *m_pUI;
154
155 CUIElement(CUi *pUI, int RequestedRectCount) { Init(pUI, RequestedRectCount); }
156
157public:
158 struct SUIElementRect
159 {
160 CUIElement *m_pParent;
161
162 public:
163 int m_UIRectQuadContainer;
164 STextContainerIndex m_UITextContainer;
165
166 float m_X;
167 float m_Y;
168 float m_Width;
169 float m_Height;
170 float m_Rounding;
171 int m_Corners;
172
173 std::string m_Text;
174 int m_ReadCursorGlyphCount;
175
176 CTextCursor m_Cursor;
177
178 ColorRGBA m_TextColor;
179 ColorRGBA m_TextOutlineColor;
180
181 SUIElementRect();
182
183 ColorRGBA m_QuadColor;
184
185 void Reset();
186 void Draw(const CUIRect *pRect, ColorRGBA Color, int Corners, float Rounding);
187 };
188
189protected:
190 CUi *Ui() const { return m_pUI; }
191 std::vector<SUIElementRect> m_vUIRects;
192
193public:
194 CUIElement() = default;
195
196 void Init(CUi *pUI, int RequestedRectCount);
197
198 SUIElementRect *Rect(size_t Index)
199 {
200 return &m_vUIRects[Index];
201 }
202
203 bool AreRectsInit()
204 {
205 return !m_vUIRects.empty();
206 }
207
208 void InitRects(int RequestedRectCount);
209};
210
211struct SLabelProperties
212{
213 float m_MaxWidth = -1;
214 bool m_StopAtEnd = false;
215 bool m_EllipsisAtEnd = false;
216 bool m_EnableWidthCheck = true;
217 float m_MinimumFontSize = 5.0f;
218 std::vector<STextColorSplit> m_vColorSplits;
219
220 void SetColor(const ColorRGBA &Color);
221};
222
223class CLabelResult
224{
225public:
226 bool m_Truncated;
227};
228
229enum EButtonFlags : unsigned
230{
231 BUTTONFLAG_NONE = 0,
232 BUTTONFLAG_LEFT = 1 << 0,
233 BUTTONFLAG_RIGHT = 1 << 1,
234 BUTTONFLAG_MIDDLE = 1 << 2,
235
236 BUTTONFLAG_ALL = BUTTONFLAG_LEFT | BUTTONFLAG_RIGHT | BUTTONFLAG_MIDDLE,
237};
238
239struct SMenuButtonProperties
240{
241 int m_Checked = 0;
242 bool m_HintRequiresStringCheck = false;
243 bool m_HintCanChangePositionOrSize = false;
244 bool m_UseIconFont = false;
245 bool m_ShowDropDownIcon = false;
246 int m_Corners = IGraphics::CORNER_ALL;
247 float m_Rounding = 5.0f;
248 float m_FontFactor = 0.0f;
249 ColorRGBA m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f);
250 unsigned m_Flags = BUTTONFLAG_LEFT;
251};
252
253class CUIElementBase
254{
255private:
256 static CUi *ms_pUi;
257
258public:
259 static void Init(CUi *pUI) { ms_pUi = pUI; }
260
261 IClient *Client() const;
262 IGraphics *Graphics() const;
263 IInput *Input() const;
264 ITextRender *TextRender() const;
265 CUi *Ui() const { return ms_pUi; }
266};
267
268class CButtonContainer
269{
270};
271
272struct SValueSelectorProperties
273{
274 bool m_UseScroll = true;
275 int64_t m_Step = 1;
276 float m_Scale = 1.0f;
277 bool m_IsHex = false;
278 int m_HexPrefix = 6;
279 ColorRGBA m_Color = ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f);
280};
281
282struct SProgressSpinnerProperties
283{
284 float m_Progress = -1.0f; // between 0.0f and 1.0f, or negative for indeterminate progress
285 ColorRGBA m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
286 int m_Segments = 64;
287};
288
289/**
290 * Type safe UI ID for popup menus.
291 */
292struct SPopupMenuId
293{
294};
295
296struct SPopupMenuProperties
297{
298 int m_Corners = IGraphics::CORNER_ALL;
299 ColorRGBA m_BorderColor = ColorRGBA(0.5f, 0.5f, 0.5f, 0.75f);
300 ColorRGBA m_BackgroundColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.75f);
301};
302
303class CUi
304{
305public:
306 /**
307 * These enum values are returned by popup menu functions to specify the behavior.
308 */
309 enum EPopupMenuFunctionResult
310 {
311 /**
312 * The current popup menu will be kept open.
313 */
314 POPUP_KEEP_OPEN = 0,
315
316 /**
317 * The current popup menu will be closed.
318 */
319 POPUP_CLOSE_CURRENT = 1,
320
321 /**
322 * The current popup menu and all popup menus above it will be closed.
323 */
324 POPUP_CLOSE_CURRENT_AND_DESCENDANTS = 2,
325 };
326
327 /**
328 * Callback that draws a popup menu.
329 *
330 * @param pContext The context object of the popup menu.
331 * @param View The UI rect where the popup menu's contents should be drawn.
332 * @param Active Whether this popup is active (the top-most popup).
333 * Only the active popup should handle key and mouse events.
334 *
335 * @return Value from the @link EPopupMenuFunctionResult @endlink enum.
336 */
337 typedef EPopupMenuFunctionResult (*FPopupMenuFunction)(void *pContext, CUIRect View, bool Active);
338
339 /**
340 * Callback that is called when one or more popups are closed.
341 */
342 typedef std::function<void()> FPopupMenuClosedCallback;
343
344 /**
345 * Represents the aggregated state of current touch events to control a user interface.
346 */
347 class CTouchState
348 {
349 friend class CUi;
350
351 bool m_SecondaryPressedNext = false;
352 float m_SecondaryActivationTime = 0.0f;
353 vec2 m_SecondaryActivationDelta = vec2(0.0f, 0.0f);
354
355 public:
356 bool m_AnyPressed = false;
357 bool m_PrimaryPressed = false;
358 bool m_SecondaryPressed = false;
359 vec2 m_PrimaryPosition = vec2(-1.0f, -1.0f);
360 vec2 m_PrimaryDelta = vec2(0.0f, 0.0f);
361 vec2 m_ScrollAmount = vec2(0.0f, 0.0f);
362 };
363
364private:
365 bool m_Enabled;
366
367 const void *m_pHotItem = nullptr;
368 const void *m_pActiveItem = nullptr;
369 const void *m_pLastActiveItem = nullptr; // only used internally to track active CLineInput
370 const void *m_pBecomingHotItem = nullptr;
371 CScrollRegion *m_pHotScrollRegion = nullptr;
372 CScrollRegion *m_pBecomingHotScrollRegion = nullptr;
373 bool m_ActiveItemValid = false;
374
375 int m_ActiveButtonLogicButton = -1;
376 int m_ActiveDraggableButtonLogicButton = -1;
377 class CDoubleClickState
378 {
379 public:
380 const void *m_pLastClickedId = nullptr;
381 float m_LastClickTime = -1.0f;
382 vec2 m_LastClickPos = vec2(-1.0f, -1.0f);
383 };
384 CDoubleClickState m_DoubleClickState;
385 const void *m_pLastEditingItem = nullptr;
386 const void *m_pLastActiveScrollbar = nullptr;
387 int m_ScrollbarValue = 0;
388 float m_ActiveScrollbarOffset = 0.0f;
389 float m_ProgressSpinnerOffset = 0.0f;
390 class CValueSelectorState
391 {
392 public:
393 int m_Button = -1;
394 bool m_DidScroll = false;
395 float m_ScrollValue = 0.0f;
396 CLineInputNumber m_NumberInput;
397 const void *m_pLastTextId = nullptr;
398 };
399 CValueSelectorState m_ActiveValueSelectorState;
400
401 vec2 m_UpdatedMousePos = vec2(0.0f, 0.0f); // in window screen space
402 vec2 m_UpdatedMouseDelta = vec2(0.0f, 0.0f); // in window screen space
403 vec2 m_MousePos = vec2(0.0f, 0.0f); // in gui space
404 vec2 m_MouseDelta = vec2(0.0f, 0.0f); // in gui space
405 unsigned m_UpdatedMouseButtons = 0;
406 unsigned m_MouseButtons = 0;
407 unsigned m_LastMouseButtons = 0;
408 CTouchState m_TouchState;
409 bool m_MouseSlow = false;
410 bool m_MouseLock = false;
411 const void *m_pMouseLockId = nullptr;
412
413 unsigned m_HotkeysPressed = 0;
414
415 CUIRect m_Screen;
416
417 std::vector<CUIRect> m_vClips;
418 void UpdateClipping();
419
420 struct SPopupMenu
421 {
422 static constexpr float POPUP_BORDER = 1.0f;
423 static constexpr float POPUP_MARGIN = 4.0f;
424
425 const SPopupMenuId *m_pId;
426 SPopupMenuProperties m_Props;
427 CUIRect m_Rect;
428 void *m_pContext;
429 FPopupMenuFunction m_pfnFunc;
430 };
431 std::vector<SPopupMenu> m_vPopupMenus;
432 FPopupMenuClosedCallback m_pfnPopupMenuClosedCallback = nullptr;
433
434 static CUi::EPopupMenuFunctionResult PopupMessage(void *pContext, CUIRect View, bool Active);
435 static CUi::EPopupMenuFunctionResult PopupConfirm(void *pContext, CUIRect View, bool Active);
436 static CUi::EPopupMenuFunctionResult PopupSelection(void *pContext, CUIRect View, bool Active);
437 static CUi::EPopupMenuFunctionResult PopupColorPicker(void *pContext, CUIRect View, bool Active);
438
439 IClient *m_pClient;
440 IGraphics *m_pGraphics;
441 IInput *m_pInput;
442 ITextRender *m_pTextRender;
443
444 std::vector<CUIElement *> m_vpOwnUIElements; // ui elements maintained by CUi class
445 std::vector<CUIElement *> m_vpUIElements;
446
447public:
448 static const CLinearScrollbarScale ms_LinearScrollbarScale;
449 static const CLogarithmicScrollbarScale ms_LogarithmicScrollbarScale;
450 static const CDarkButtonColorFunction ms_DarkButtonColorFunction;
451 static const CLightButtonColorFunction ms_LightButtonColorFunction;
452 static const CScrollBarColorFunction ms_ScrollBarColorFunction;
453
454 static const float ms_FontmodHeight;
455
456 void Init(IKernel *pKernel);
457 IClient *Client() const { return m_pClient; }
458 IGraphics *Graphics() const { return m_pGraphics; }
459 IInput *Input() const { return m_pInput; }
460 ITextRender *TextRender() const { return m_pTextRender; }
461
462 CUi();
463 ~CUi();
464
465 enum EHotkey : unsigned
466 {
467 HOTKEY_ENTER = 1 << 0,
468 HOTKEY_ESCAPE = 1 << 1,
469 HOTKEY_UP = 1 << 2,
470 HOTKEY_DOWN = 1 << 3,
471 HOTKEY_LEFT = 1 << 4,
472 HOTKEY_RIGHT = 1 << 5,
473 HOTKEY_DELETE = 1 << 6,
474 HOTKEY_TAB = 1 << 7,
475 HOTKEY_SCROLL_UP = 1 << 8,
476 HOTKEY_SCROLL_DOWN = 1 << 9,
477 HOTKEY_PAGE_UP = 1 << 10,
478 HOTKEY_PAGE_DOWN = 1 << 11,
479 HOTKEY_HOME = 1 << 12,
480 HOTKEY_END = 1 << 13,
481 };
482
483 void ResetUIElement(CUIElement &UIElement) const;
484
485 CUIElement *GetNewUIElement(int RequestedRectCount);
486
487 void AddUIElement(CUIElement *pElement);
488 void OnElementsReset();
489 void OnWindowResize();
490 void OnCursorMove(float X, float Y);
491
492 void SetEnabled(bool Enabled) { m_Enabled = Enabled; }
493 bool Enabled() const { return m_Enabled; }
494 void Update();
495 void DebugRender(float X, float Y);
496
497 vec2 MousePos() const { return m_MousePos; }
498 float MouseX() const { return m_MousePos.x; }
499 float MouseY() const { return m_MousePos.y; }
500 vec2 MouseDelta() const { return m_MouseDelta; }
501 float MouseDeltaX() const { return m_MouseDelta.x; }
502 float MouseDeltaY() const { return m_MouseDelta.y; }
503 vec2 UpdatedMousePos() const { return m_UpdatedMousePos; }
504 vec2 UpdatedMouseDelta() const { return m_UpdatedMouseDelta; }
505 int MouseButton(int Index) const { return (m_MouseButtons >> Index) & 1; }
506 int MouseButtonClicked(int Index) const { return MouseButton(Index) && !((m_LastMouseButtons >> Index) & 1); }
507 bool CheckMouseLock()
508 {
509 if(m_MouseLock && ActiveItem() != m_pMouseLockId)
510 DisableMouseLock();
511 return m_MouseLock;
512 }
513 void EnableMouseLock(const void *pId)
514 {
515 m_MouseLock = true;
516 m_pMouseLockId = pId;
517 }
518 void DisableMouseLock() { m_MouseLock = false; }
519
520 void SetHotItem(const void *pId) { m_pBecomingHotItem = pId; }
521 void SetActiveItem(const void *pId)
522 {
523 m_ActiveItemValid = true;
524 m_pActiveItem = pId;
525 if(pId)
526 m_pLastActiveItem = pId;
527 }
528 bool CheckActiveItem(const void *pId)
529 {
530 if(m_pActiveItem == pId)
531 {
532 m_ActiveItemValid = true;
533 return true;
534 }
535 return false;
536 }
537 void SetHotScrollRegion(CScrollRegion *pId) { m_pBecomingHotScrollRegion = pId; }
538 const void *HotItem() const { return m_pHotItem; }
539 const void *NextHotItem() const { return m_pBecomingHotItem; }
540 const void *ActiveItem() const { return m_pActiveItem; }
541 const CScrollRegion *HotScrollRegion() const { return m_pHotScrollRegion; }
542
543 void StartCheck() { m_ActiveItemValid = false; }
544 void FinishCheck()
545 {
546 if(!m_ActiveItemValid && m_pActiveItem != nullptr)
547 {
548 SetActiveItem(nullptr);
549 m_pHotItem = nullptr;
550 m_pBecomingHotItem = nullptr;
551 }
552 }
553
554 bool MouseInside(const CUIRect *pRect) const;
555 bool MouseInsideClip() const { return !IsClipped() || MouseInside(pRect: ClipArea()); }
556 bool MouseHovered(const CUIRect *pRect) const { return MouseInside(pRect) && MouseInsideClip(); }
557 void ConvertMouseMove(float *pX, float *pY, IInput::ECursorType CursorType) const;
558 void UpdateTouchState(CTouchState &State) const;
559 void SetMouseSlow(bool MouseSlow) { m_MouseSlow = MouseSlow; }
560
561 bool ConsumeHotkey(EHotkey Hotkey);
562 void ClearHotkeys() { m_HotkeysPressed = 0; }
563 bool OnInput(const IInput::CEvent &Event);
564
565 constexpr float ButtonColorMulActive() const { return 0.5f; }
566 constexpr float ButtonColorMulHot() const { return 1.5f; }
567 constexpr float ButtonColorMulDefault() const { return 1.0f; }
568 float ButtonColorMul(const void *pId);
569
570 const CUIRect *Screen();
571 void MapScreen();
572 float PixelSize();
573
574 void ClipEnable(const CUIRect *pRect);
575 void ClipDisable();
576 const CUIRect *ClipArea() const;
577 bool IsClipped() const { return !m_vClips.empty(); }
578
579 int DoButtonLogic(const void *pId, int Checked, const CUIRect *pRect, unsigned Flags);
580 int DoDraggableButtonLogic(const void *pId, int Checked, const CUIRect *pRect, bool *pClicked, bool *pAbrupted);
581 bool DoDoubleClickLogic(const void *pId);
582 EEditState DoPickerLogic(const void *pId, const CUIRect *pRect, float *pX, float *pY);
583 void DoSmoothScrollLogic(float *pScrollOffset, float *pScrollOffsetChange, float ViewPortSize, float TotalSize, bool SmoothClamp = false, float ScrollSpeed = 10.0f) const;
584 static vec2 CalcAlignedCursorPos(const CUIRect *pRect, vec2 TextSize, int Align, const float *pBiggestCharHeight = nullptr);
585
586 CLabelResult DoLabel(const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps = {}) const;
587 CLabelResult DoLabel_AutoLineSize(const char *pText, float FontSize, int Align, CUIRect *pRect, float LineSize, const SLabelProperties &LabelProps = {}) const;
588
589 void DoLabel(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps = {}, int StrLen = -1, const CTextCursor *pReadCursor = nullptr) const;
590 void DoLabelStreamed(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps = {}, int StrLen = -1, const CTextCursor *pReadCursor = nullptr) const;
591
592 /**
593 * Creates an input field.
594 *
595 * @see DoClearableEditBox
596 *
597 * @param pLineInput This pointer will be stored and written to on next user input.
598 * So you can not pass in a pointer that goes out of scope such as a local variable.
599 * Pass in either a member variable of the current class or a static variable.
600 * For example ```static CLineInputBuffered<IO_MAX_PATH_LENGTH> s_MyInput;```
601 * @param pRect the UI rect it will attach to with a 2.0f margin
602 * @param FontSize Size of the font (`10.0f`, `12.0f` and `14.0f` are commonly used here)
603 * @param Corners Number of corners (default: `IGraphics::CORNER_ALL`)
604 * @param vColorSplits Sets color splits of the `CTextCursor` to allow multicolored text
605 *
606 * @return true if the value of the input field changed since the last call.
607 */
608 bool DoEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const std::vector<STextColorSplit> &vColorSplits = {});
609
610 /**
611 * Creates an input field with a clear [x] button attached to it.
612 *
613 * @see DoEditBox
614 *
615 * @param pLineInput This pointer will be stored and written to on next user input.
616 * So you can not pass in a pointer that goes out of scope such as a local variable.
617 * Pass in either a member variable of the current class or a static variable.
618 * For example ```static CLineInputBuffered<IO_MAX_PATH_LENGTH> s_MyInput;```
619 * @param pRect the UI rect it will attach to
620 * @param FontSize Size of the font (`10.0f`, `12.0f` and `14.0f` are commonly used here)
621 * @param Corners Number of corners (default: `IGraphics::CORNER_ALL`)
622 * @param vColorSplits Sets color splits of the `CTextCursor` to allow multicolored text
623 *
624 * @return true if the value of the input field changed since the last call.
625 */
626 bool DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const std::vector<STextColorSplit> &vColorSplits = {});
627
628 /**
629 * Creates an input field with a search icon and a clear [x] button attached to it.
630 * The input will have default text "Search" and the hotkey Ctrl+F can be used to activate the input.
631 *
632 * @see DoEditBox
633 *
634 * @param pLineInput This pointer will be stored and written to on next user input.
635 * So you can not pass in a pointer that goes out of scope such as a local variable.
636 * Pass in either a member variable of the current class or a static variable.
637 * For example ```static CLineInputBuffered<IO_MAX_PATH_LENGTH> s_MyInput;```
638 * @param pRect the UI rect it will attach to
639 * @param FontSize Size of the font (`10.0f`, `12.0f` and `14.0f` are commonly used here)
640 * @param HotkeyEnabled Whether the hotkey to enable this editbox is currently enabled.
641 *
642 * @return true if the value of the input field changed since the last call.
643 */
644 bool DoEditBox_Search(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, bool HotkeyEnabled);
645
646 int DoButton_Menu(CUIElement &UIElement, const CButtonContainer *pId, const std::function<const char *()> &GetTextLambda, const CUIRect *pRect, const SMenuButtonProperties &Props = {});
647 int DoButton_FontIcon(CButtonContainer *pButtonContainer, const char *pText, int Checked, const CUIRect *pRect, unsigned Flags, int Corners = IGraphics::CORNER_ALL, bool Enabled = true, std::optional<ColorRGBA> ButtonColor = std::nullopt);
648 // only used for popup menus
649 int DoButton_PopupMenu(CButtonContainer *pButtonContainer, const char *pText, const CUIRect *pRect, float Size, int Align, float Padding = 0.0f, bool TransparentInactive = false, bool Enabled = true, std::optional<ColorRGBA> ButtonColor = std::nullopt);
650
651 // value selector
652 SEditResult<int64_t> DoValueSelectorWithState(const void *pId, const CUIRect *pRect, const char *pLabel, int64_t Current, int64_t Min, int64_t Max, const SValueSelectorProperties &Props = {});
653 int64_t DoValueSelector(const void *pId, const CUIRect *pRect, const char *pLabel, int64_t Current, int64_t Min, int64_t Max, const SValueSelectorProperties &Props = {});
654
655 // scrollbars
656 enum
657 {
658 SCROLLBAR_OPTION_INFINITE = 1 << 0,
659 SCROLLBAR_OPTION_NOCLAMPVALUE = 1 << 1,
660 SCROLLBAR_OPTION_MULTILINE = 1 << 2,
661 SCROLLBAR_OPTION_DELAYUPDATE = 1 << 3,
662 };
663 float DoScrollbarV(const void *pId, const CUIRect *pRect, float Current);
664 float DoScrollbarH(const void *pId, const CUIRect *pRect, float Current, const ColorRGBA *pColorInner = nullptr);
665 bool DoScrollbarOption(const void *pId, int *pOption, const CUIRect *pRect, const char *pStr, int Min, int Max, const IScrollbarScale *pScale = &ms_LinearScrollbarScale, unsigned Flags = 0u, const char *pSuffix = "");
666
667 // progress bar
668 void RenderProgressBar(CUIRect ProgressBar, float Progress);
669
670 // render time with hundredths or thousands aligned to the right of the UIRect
671 void RenderTime(CUIRect TimeRect, float FontSize, int Seconds, bool NotFinished, int Millis, bool TrueMilliseconds) const;
672
673 // progress spinner
674 void RenderProgressSpinner(vec2 Center, float OuterRadius, const SProgressSpinnerProperties &Props = {}) const;
675
676 // popup menu
677 void DoPopupMenu(const SPopupMenuId *pId, float X, float Y, float Width, float Height, void *pContext, FPopupMenuFunction pfnFunc, const SPopupMenuProperties &Props = {});
678 void RenderPopupMenus();
679 void ClosePopupMenu(const SPopupMenuId *pId, bool IncludeDescendants = false);
680 void ClosePopupMenus();
681 bool IsPopupOpen() const;
682 bool IsPopupOpen(const SPopupMenuId *pId) const;
683 bool IsPopupHovered() const;
684 void SetPopupMenuClosedCallback(FPopupMenuClosedCallback pfnCallback);
685
686 struct SMessagePopupContext : public SPopupMenuId
687 {
688 static constexpr float POPUP_MAX_WIDTH = 200.0f;
689 static constexpr float POPUP_FONT_SIZE = 10.0f;
690
691 CUi *m_pUI; // set by CUi when popup is shown
692 char m_aMessage[1024];
693 ColorRGBA m_TextColor;
694
695 void DefaultColor(class ITextRender *pTextRender);
696 void ErrorColor();
697 };
698 void ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext);
699
700 struct SConfirmPopupContext : public SPopupMenuId
701 {
702 enum EConfirmationResult
703 {
704 UNSET = 0,
705 CONFIRMED,
706 CANCELED,
707 };
708 static constexpr float POPUP_MAX_WIDTH = 200.0f;
709 static constexpr float POPUP_FONT_SIZE = 10.0f;
710 static constexpr float POPUP_BUTTON_HEIGHT = 12.0f;
711 static constexpr float POPUP_BUTTON_SPACING = 5.0f;
712
713 CUi *m_pUI; // set by CUi when popup is shown
714 char m_aPositiveButtonLabel[128];
715 char m_aNegativeButtonLabel[128];
716 char m_aMessage[1024];
717 EConfirmationResult m_Result;
718
719 CButtonContainer m_CancelButton;
720 CButtonContainer m_ConfirmButton;
721
722 SConfirmPopupContext();
723 void Reset();
724 void YesNoButtons();
725 };
726 void ShowPopupConfirm(float X, float Y, SConfirmPopupContext *pContext);
727
728 struct SSelectionPopupContext : public SPopupMenuId
729 {
730 CUi *m_pUI; // set by CUi when popup is shown
731 CScrollRegion *m_pScrollRegion;
732 SPopupMenuProperties m_Props;
733 char m_aMessage[256];
734 std::vector<std::string> m_vEntries;
735 std::vector<CButtonContainer> m_vButtonContainers;
736 const std::string *m_pSelection;
737 int m_SelectionIndex;
738 float m_EntryHeight;
739 float m_EntryPadding;
740 float m_EntrySpacing;
741 float m_FontSize;
742 float m_Width;
743 float m_AlignmentHeight;
744 bool m_TransparentButtons;
745
746 SSelectionPopupContext();
747 void Reset();
748 };
749 void ShowPopupSelection(float X, float Y, SSelectionPopupContext *pContext);
750
751 struct SColorPickerPopupContext : public SPopupMenuId
752 {
753 enum EColorPickerMode
754 {
755 MODE_UNSET = -1,
756 MODE_HSVA,
757 MODE_RGBA,
758 MODE_HSLA,
759 };
760
761 CUi *m_pUI; // set by CUi when popup is shown
762 EColorPickerMode m_ColorMode = MODE_UNSET;
763 bool m_Alpha = false;
764 unsigned int *m_pHslaColor = nullptr; // may be nullptr
765 ColorHSVA m_HsvaColor;
766 ColorRGBA m_RgbaColor;
767 ColorHSLA m_HslaColor;
768 // UI element IDs
769 const char m_HuePickerId = 0;
770 const char m_ColorPickerId = 0;
771 const char m_aValueSelectorIds[5] = {0};
772 CButtonContainer m_aModeButtons[(int)MODE_HSLA + 1];
773 EEditState m_State = EEditState::NONE;
774 };
775 void ShowPopupColorPicker(float X, float Y, SColorPickerPopupContext *pContext);
776
777 // dropdown menu
778 struct SDropDownState
779 {
780 SSelectionPopupContext m_SelectionPopupContext;
781 CUIElement m_UiElement;
782 CButtonContainer m_ButtonContainer;
783 bool m_Init = false;
784 };
785 int DoDropDown(CUIRect *pRect, int CurSelection, const char **pStrs, int Num, SDropDownState &State);
786};
787
788#endif
789