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 enum class EBackButtonOp
416 {
417 NONE,
418 CLICKED,
419 DRAGGING,
420 };
421 EBackButtonOp m_BackButtonOp = EBackButtonOp::NONE;
422 vec2 m_BackButtonDragOffset = vec2(0.0f, 0.0f);
423 vec2 m_BackButtonInitialMouse = vec2(0.0f, 0.0f);
424 CUIRect m_BackButtonRect = {.x: 0.0f, .y: 0.0f, .w: 0.0f, .h: 0.0f};
425 const char m_BackButtonId = 0;
426
427 std::function<void(const IInput::CEvent &Event)> m_DispatchInputFunction;
428 std::function<void()> m_OnBackButtonPressedFunction;
429
430 CUIRect m_Screen;
431
432 std::vector<CUIRect> m_vClips;
433 void UpdateClipping();
434
435 struct SPopupMenu
436 {
437 static constexpr float POPUP_BORDER = 1.0f;
438 static constexpr float POPUP_MARGIN = 4.0f;
439
440 const SPopupMenuId *m_pId;
441 SPopupMenuProperties m_Props;
442 CUIRect m_Rect;
443 void *m_pContext;
444 FPopupMenuFunction m_pfnFunc;
445 };
446 std::vector<SPopupMenu> m_vPopupMenus;
447 FPopupMenuClosedCallback m_pfnPopupMenuClosedCallback = nullptr;
448
449 static CUi::EPopupMenuFunctionResult PopupMessage(void *pContext, CUIRect View, bool Active);
450 static CUi::EPopupMenuFunctionResult PopupConfirm(void *pContext, CUIRect View, bool Active);
451 static CUi::EPopupMenuFunctionResult PopupSelection(void *pContext, CUIRect View, bool Active);
452 static CUi::EPopupMenuFunctionResult PopupColorPicker(void *pContext, CUIRect View, bool Active);
453
454 IClient *m_pClient;
455 IGraphics *m_pGraphics;
456 IInput *m_pInput;
457 ITextRender *m_pTextRender;
458
459 std::vector<CUIElement *> m_vpOwnUIElements; // ui elements maintained by CUi class
460 std::vector<CUIElement *> m_vpUIElements;
461
462public:
463 static const CLinearScrollbarScale ms_LinearScrollbarScale;
464 static const CLogarithmicScrollbarScale ms_LogarithmicScrollbarScale;
465 static const CDarkButtonColorFunction ms_DarkButtonColorFunction;
466 static const CLightButtonColorFunction ms_LightButtonColorFunction;
467 static const CScrollBarColorFunction ms_ScrollBarColorFunction;
468
469 static const float ms_FontmodHeight;
470
471 void Init(IKernel *pKernel);
472 IClient *Client() const { return m_pClient; }
473 IGraphics *Graphics() const { return m_pGraphics; }
474 IInput *Input() const { return m_pInput; }
475 ITextRender *TextRender() const { return m_pTextRender; }
476
477 CUi();
478 ~CUi();
479
480 enum EHotkey : unsigned
481 {
482 HOTKEY_ENTER = 1 << 0,
483 HOTKEY_ESCAPE = 1 << 1,
484 HOTKEY_UP = 1 << 2,
485 HOTKEY_DOWN = 1 << 3,
486 HOTKEY_LEFT = 1 << 4,
487 HOTKEY_RIGHT = 1 << 5,
488 HOTKEY_DELETE = 1 << 6,
489 HOTKEY_TAB = 1 << 7,
490 HOTKEY_SCROLL_UP = 1 << 8,
491 HOTKEY_SCROLL_DOWN = 1 << 9,
492 HOTKEY_PAGE_UP = 1 << 10,
493 HOTKEY_PAGE_DOWN = 1 << 11,
494 HOTKEY_HOME = 1 << 12,
495 HOTKEY_END = 1 << 13,
496 };
497
498 void ResetUIElement(CUIElement &UIElement) const;
499
500 CUIElement *GetNewUIElement(int RequestedRectCount);
501
502 void AddUIElement(CUIElement *pElement);
503 void OnElementsReset();
504 void OnWindowResize();
505 void OnCursorMove(float X, float Y);
506
507 void SetEnabled(bool Enabled) { m_Enabled = Enabled; }
508 bool Enabled() const { return m_Enabled; }
509 void Update();
510 void DebugRender(float X, float Y);
511
512 vec2 MousePos() const { return m_MousePos; }
513 float MouseX() const { return m_MousePos.x; }
514 float MouseY() const { return m_MousePos.y; }
515 vec2 MouseDelta() const { return m_MouseDelta; }
516 float MouseDeltaX() const { return m_MouseDelta.x; }
517 float MouseDeltaY() const { return m_MouseDelta.y; }
518 vec2 UpdatedMousePos() const { return m_UpdatedMousePos; }
519 vec2 UpdatedMouseDelta() const { return m_UpdatedMouseDelta; }
520 int MouseButton(int Index) const { return (m_MouseButtons >> Index) & 1; }
521 int MouseButtonClicked(int Index) const { return MouseButton(Index) && !((m_LastMouseButtons >> Index) & 1); }
522 bool CheckMouseLock()
523 {
524 if(m_MouseLock && ActiveItem() != m_pMouseLockId)
525 DisableMouseLock();
526 return m_MouseLock;
527 }
528 void EnableMouseLock(const void *pId)
529 {
530 m_MouseLock = true;
531 m_pMouseLockId = pId;
532 }
533 void DisableMouseLock() { m_MouseLock = false; }
534
535 void SetHotItem(const void *pId) { m_pBecomingHotItem = pId; }
536 void SetActiveItem(const void *pId)
537 {
538 m_ActiveItemValid = true;
539 m_pActiveItem = pId;
540 if(pId)
541 m_pLastActiveItem = pId;
542 }
543 bool CheckActiveItem(const void *pId)
544 {
545 if(m_pActiveItem == pId)
546 {
547 m_ActiveItemValid = true;
548 return true;
549 }
550 return false;
551 }
552 void SetHotScrollRegion(CScrollRegion *pId) { m_pBecomingHotScrollRegion = pId; }
553 const void *HotItem() const { return m_pHotItem; }
554 const void *NextHotItem() const { return m_pBecomingHotItem; }
555 const void *ActiveItem() const { return m_pActiveItem; }
556 const CScrollRegion *HotScrollRegion() const { return m_pHotScrollRegion; }
557
558 void StartCheck() { m_ActiveItemValid = false; }
559 void FinishCheck()
560 {
561 if(!m_ActiveItemValid && m_pActiveItem != nullptr)
562 {
563 SetActiveItem(nullptr);
564 m_pHotItem = nullptr;
565 m_pBecomingHotItem = nullptr;
566 }
567 }
568
569 bool MouseInside(const CUIRect *pRect) const;
570 bool MouseInsideClip() const { return !IsClipped() || MouseInside(pRect: ClipArea()); }
571 bool MouseHovered(const CUIRect *pRect) const { return MouseInside(pRect) && MouseInsideClip(); }
572 void ConvertMouseMove(float *pX, float *pY, IInput::ECursorType CursorType) const;
573 void UpdateTouchState(CTouchState &State) const;
574 void SetMouseSlow(bool MouseSlow) { m_MouseSlow = MouseSlow; }
575
576 bool ConsumeHotkey(EHotkey Hotkey);
577 void ClearHotkeys() { m_HotkeysPressed = 0; }
578 bool OnInput(const IInput::CEvent &Event);
579
580 constexpr float ButtonColorMulActive() const { return 0.5f; }
581 constexpr float ButtonColorMulHot() const { return 1.5f; }
582 constexpr float ButtonColorMulDefault() const { return 1.0f; }
583 float ButtonColorMul(const void *pId);
584
585 const CUIRect *Screen();
586 void MapScreen();
587 float PixelSize();
588
589 void ClipEnable(const CUIRect *pRect);
590 void ClipDisable();
591 const CUIRect *ClipArea() const;
592 bool IsClipped() const { return !m_vClips.empty(); }
593
594 int DoButtonLogic(const void *pId, int Checked, const CUIRect *pRect, unsigned Flags);
595 int DoDraggableButtonLogic(const void *pId, int Checked, const CUIRect *pRect, bool *pClicked, bool *pAbrupted);
596 bool DoDoubleClickLogic(const void *pId);
597 EEditState DoPickerLogic(const void *pId, const CUIRect *pRect, float *pX, float *pY);
598 void DoSmoothScrollLogic(float *pScrollOffset, float *pScrollOffsetChange, float ViewPortSize, float TotalSize, bool SmoothClamp = false, float ScrollSpeed = 10.0f) const;
599 static vec2 CalcAlignedCursorPos(const CUIRect *pRect, vec2 TextSize, int Align, const float *pBiggestCharHeight = nullptr);
600
601 CLabelResult DoLabel(const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps = {}) const;
602 CLabelResult DoLabel_AutoLineSize(const char *pText, float FontSize, int Align, CUIRect *pRect, float LineSize, const SLabelProperties &LabelProps = {}) const;
603
604 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;
605 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;
606
607 /**
608 * Creates an input field.
609 *
610 * @see DoClearableEditBox
611 *
612 * @param pLineInput This pointer will be stored and written to on next user input.
613 * So you can not pass in a pointer that goes out of scope such as a local variable.
614 * Pass in either a member variable of the current class or a static variable.
615 * For example ```static CLineInputBuffered<IO_MAX_PATH_LENGTH> s_MyInput;```
616 * @param pRect the UI rect it will attach to with a 2.0f margin
617 * @param FontSize Size of the font (`10.0f`, `12.0f` and `14.0f` are commonly used here)
618 * @param Corners Number of corners (default: `IGraphics::CORNER_ALL`)
619 * @param vColorSplits Sets color splits of the `CTextCursor` to allow multicolored text
620 *
621 * @return true if the value of the input field changed since the last call.
622 */
623 bool DoEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const std::vector<STextColorSplit> &vColorSplits = {});
624
625 /**
626 * Creates an input field with a clear [x] button attached to it.
627 *
628 * @see DoEditBox
629 *
630 * @param pLineInput This pointer will be stored and written to on next user input.
631 * So you can not pass in a pointer that goes out of scope such as a local variable.
632 * Pass in either a member variable of the current class or a static variable.
633 * For example ```static CLineInputBuffered<IO_MAX_PATH_LENGTH> s_MyInput;```
634 * @param pRect the UI rect it will attach to
635 * @param FontSize Size of the font (`10.0f`, `12.0f` and `14.0f` are commonly used here)
636 * @param Corners Number of corners (default: `IGraphics::CORNER_ALL`)
637 * @param vColorSplits Sets color splits of the `CTextCursor` to allow multicolored text
638 *
639 * @return true if the value of the input field changed since the last call.
640 */
641 bool DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const std::vector<STextColorSplit> &vColorSplits = {});
642
643 /**
644 * Creates an input field with a search icon and a clear [x] button attached to it.
645 * The input will have default text "Search" and the hotkey Ctrl+F can be used to activate the input.
646 *
647 * @see DoEditBox
648 *
649 * @param pLineInput This pointer will be stored and written to on next user input.
650 * So you can not pass in a pointer that goes out of scope such as a local variable.
651 * Pass in either a member variable of the current class or a static variable.
652 * For example ```static CLineInputBuffered<IO_MAX_PATH_LENGTH> s_MyInput;```
653 * @param pRect the UI rect it will attach to
654 * @param FontSize Size of the font (`10.0f`, `12.0f` and `14.0f` are commonly used here)
655 * @param HotkeyEnabled Whether the hotkey to enable this editbox is currently enabled.
656 *
657 * @return true if the value of the input field changed since the last call.
658 */
659 bool DoEditBox_Search(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, bool HotkeyEnabled);
660
661 int DoButton_Menu(CUIElement &UIElement, const CButtonContainer *pId, const std::function<const char *()> &GetTextLambda, const CUIRect *pRect, const SMenuButtonProperties &Props = {});
662 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);
663 // only used for popup menus
664 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);
665
666 // value selector
667 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 = {});
668 int64_t DoValueSelector(const void *pId, const CUIRect *pRect, const char *pLabel, int64_t Current, int64_t Min, int64_t Max, const SValueSelectorProperties &Props = {});
669
670 // scrollbars
671 enum
672 {
673 SCROLLBAR_OPTION_INFINITE = 1 << 0,
674 SCROLLBAR_OPTION_NOCLAMPVALUE = 1 << 1,
675 SCROLLBAR_OPTION_MULTILINE = 1 << 2,
676 SCROLLBAR_OPTION_DELAYUPDATE = 1 << 3,
677 };
678 float DoScrollbarV(const void *pId, const CUIRect *pRect, float Current);
679 float DoScrollbarH(const void *pId, const CUIRect *pRect, float Current, const ColorRGBA *pColorInner = nullptr);
680 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 = "");
681
682 // progress bar
683 void RenderProgressBar(CUIRect ProgressBar, float Progress);
684
685 // render time with hundredths or thousands aligned to the right of the UIRect
686 void RenderTime(CUIRect TimeRect, float FontSize, int Seconds, bool NotFinished, int Millis, bool TrueMilliseconds) const;
687
688 // progress spinner
689 void RenderProgressSpinner(vec2 Center, float OuterRadius, const SProgressSpinnerProperties &Props = {}) const;
690
691 // virtual back button
692 void DoBackButton();
693 void RenderBackButton();
694 void SetDispatchInputCallback(std::function<void(const IInput::CEvent &Event)> pfnCallback) { m_DispatchInputFunction = std::move(pfnCallback); }
695 // Fired the moment the back button transitions to active (mouse-down inside it).
696 void SetOnBackButtonPressedCallback(std::function<void()> pfnCallback) { m_OnBackButtonPressedFunction = std::move(pfnCallback); }
697
698 // popup menu
699 void DoPopupMenu(const SPopupMenuId *pId, float X, float Y, float Width, float Height, void *pContext, FPopupMenuFunction pfnFunc, const SPopupMenuProperties &Props = {});
700 void RenderPopupMenus();
701 void ClosePopupMenu(const SPopupMenuId *pId, bool IncludeDescendants = false);
702 void ClosePopupMenus();
703 bool IsPopupOpen() const;
704 bool IsPopupOpen(const SPopupMenuId *pId) const;
705 bool IsPopupHovered() const;
706 void SetPopupMenuClosedCallback(FPopupMenuClosedCallback pfnCallback);
707
708 struct SMessagePopupContext : public SPopupMenuId
709 {
710 static constexpr float POPUP_MAX_WIDTH = 200.0f;
711 static constexpr float POPUP_FONT_SIZE = 10.0f;
712
713 CUi *m_pUI; // set by CUi when popup is shown
714 char m_aMessage[1024];
715 ColorRGBA m_TextColor;
716
717 void DefaultColor(class ITextRender *pTextRender);
718 void ErrorColor();
719 };
720 void ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext);
721
722 struct SConfirmPopupContext : public SPopupMenuId
723 {
724 enum EConfirmationResult
725 {
726 UNSET = 0,
727 CONFIRMED,
728 CANCELED,
729 };
730 static constexpr float POPUP_MAX_WIDTH = 200.0f;
731 static constexpr float POPUP_FONT_SIZE = 10.0f;
732 static constexpr float POPUP_BUTTON_HEIGHT = 12.0f;
733 static constexpr float POPUP_BUTTON_SPACING = 5.0f;
734
735 CUi *m_pUI; // set by CUi when popup is shown
736 char m_aPositiveButtonLabel[128];
737 char m_aNegativeButtonLabel[128];
738 char m_aMessage[1024];
739 EConfirmationResult m_Result;
740
741 CButtonContainer m_CancelButton;
742 CButtonContainer m_ConfirmButton;
743
744 SConfirmPopupContext();
745 void Reset();
746 void YesNoButtons();
747 };
748 void ShowPopupConfirm(float X, float Y, SConfirmPopupContext *pContext);
749
750 struct SSelectionPopupContext : public SPopupMenuId
751 {
752 CUi *m_pUI; // set by CUi when popup is shown
753 CScrollRegion *m_pScrollRegion;
754 SPopupMenuProperties m_Props;
755 char m_aMessage[256];
756 std::vector<std::string> m_vEntries;
757 std::vector<CButtonContainer> m_vButtonContainers;
758 const std::string *m_pSelection;
759 int m_SelectionIndex;
760 float m_EntryHeight;
761 float m_EntryPadding;
762 float m_EntrySpacing;
763 float m_FontSize;
764 float m_Width;
765 float m_AlignmentHeight;
766 bool m_TransparentButtons;
767
768 SSelectionPopupContext();
769 void Reset();
770 };
771 void ShowPopupSelection(float X, float Y, SSelectionPopupContext *pContext);
772
773 struct SColorPickerPopupContext : public SPopupMenuId
774 {
775 enum EColorPickerMode
776 {
777 MODE_UNSET = -1,
778 MODE_HSVA,
779 MODE_RGBA,
780 MODE_HSLA,
781 };
782
783 CUi *m_pUI; // set by CUi when popup is shown
784 EColorPickerMode m_ColorMode = MODE_UNSET;
785 bool m_Alpha = false;
786 unsigned int *m_pHslaColor = nullptr; // may be nullptr
787 ColorHSVA m_HsvaColor;
788 ColorRGBA m_RgbaColor;
789 ColorHSLA m_HslaColor;
790 // UI element IDs
791 const char m_HuePickerId = 0;
792 const char m_ColorPickerId = 0;
793 const char m_aValueSelectorIds[5] = {0};
794 CButtonContainer m_aModeButtons[(int)MODE_HSLA + 1];
795 EEditState m_State = EEditState::NONE;
796 };
797 void ShowPopupColorPicker(float X, float Y, SColorPickerPopupContext *pContext);
798
799 // dropdown menu
800 struct SDropDownState
801 {
802 SSelectionPopupContext m_SelectionPopupContext;
803 CUIElement m_UiElement;
804 CButtonContainer m_ButtonContainer;
805 bool m_Init = false;
806 };
807 int DoDropDown(CUIRect *pRect, int CurSelection, const char **pStrs, int Num, SDropDownState &State);
808};
809
810#endif
811