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