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#include "ui.h"
4#include "ui_scrollregion.h"
5
6#include <base/dbg.h>
7#include <base/math.h>
8#include <base/str.h>
9
10#include <engine/graphics.h>
11
12#include <game/localization.h>
13
14void CUi::DoPopupMenu(const SPopupMenuId *pId, float X, float Y, float Width, float Height, void *pContext, FPopupMenuFunction pfnFunc, const SPopupMenuProperties &Props)
15{
16 constexpr float Margin = SPopupMenu::POPUP_BORDER + SPopupMenu::POPUP_MARGIN;
17 if(X + Width > Screen()->w - Margin)
18 X = std::max(a: X - Width, b: Margin);
19 if(Y + Height > Screen()->h - Margin)
20 Y = std::max(a: Y - Height, b: Margin);
21
22 m_vPopupMenus.emplace_back();
23 SPopupMenu *pNewMenu = &m_vPopupMenus.back();
24 pNewMenu->m_pId = pId;
25 pNewMenu->m_Props = Props;
26 pNewMenu->m_Rect.x = X;
27 pNewMenu->m_Rect.y = Y;
28 pNewMenu->m_Rect.w = Width;
29 pNewMenu->m_Rect.h = Height;
30 pNewMenu->m_pContext = pContext;
31 pNewMenu->m_pfnFunc = pfnFunc;
32}
33
34void CUi::RenderPopupMenus()
35{
36 for(size_t i = 0; i < m_vPopupMenus.size(); ++i)
37 {
38 const SPopupMenu &PopupMenu = m_vPopupMenus[i];
39 const SPopupMenuId *pId = PopupMenu.m_pId;
40 const bool Inside = MouseInside(pRect: &PopupMenu.m_Rect);
41 const bool Active = i == m_vPopupMenus.size() - 1;
42
43 if(Active)
44 {
45 // Prevent UI elements below the popup menu from being activated.
46 SetHotItem(pId);
47 }
48
49 if(CheckActiveItem(pId))
50 {
51 if(!MouseButton(Index: 0))
52 {
53 if(!Inside)
54 {
55 ClosePopupMenu(pId);
56 --i;
57 continue;
58 }
59 SetActiveItem(nullptr);
60 }
61 }
62 else if(HotItem() == pId)
63 {
64 if(MouseButton(Index: 0))
65 SetActiveItem(pId);
66 }
67
68 if(Inside)
69 {
70 // Prevent scroll regions directly behind popup menus from using the mouse scroll events.
71 SetHotScrollRegion(nullptr);
72 }
73
74 CUIRect PopupRect = PopupMenu.m_Rect;
75 PopupRect.Draw(Color: PopupMenu.m_Props.m_BorderColor, Corners: PopupMenu.m_Props.m_Corners, Rounding: 3.0f);
76 PopupRect.Margin(Cut: SPopupMenu::POPUP_BORDER, pOtherRect: &PopupRect);
77 PopupRect.Draw(Color: PopupMenu.m_Props.m_BackgroundColor, Corners: PopupMenu.m_Props.m_Corners, Rounding: 3.0f);
78 PopupRect.Margin(Cut: SPopupMenu::POPUP_MARGIN, pOtherRect: &PopupRect);
79
80 // The popup render function can open/close popups, which may resize the vector and thus
81 // invalidate the variable PopupMenu. We therefore store pId in a separate variable.
82 EPopupMenuFunctionResult Result = PopupMenu.m_pfnFunc(PopupMenu.m_pContext, PopupRect, Active);
83 if(Result != POPUP_KEEP_OPEN || (Active && ConsumeHotkey(Hotkey: HOTKEY_ESCAPE)))
84 ClosePopupMenu(pId, IncludeDescendants: Result == POPUP_CLOSE_CURRENT_AND_DESCENDANTS);
85 }
86}
87
88void CUi::ClosePopupMenu(const SPopupMenuId *pId, bool IncludeDescendants)
89{
90 auto PopupMenuToClose = std::find_if(first: m_vPopupMenus.begin(), last: m_vPopupMenus.end(), pred: [pId](const SPopupMenu &PopupMenu) { return PopupMenu.m_pId == pId; });
91 if(PopupMenuToClose != m_vPopupMenus.end())
92 {
93 if(IncludeDescendants)
94 m_vPopupMenus.erase(first: PopupMenuToClose, last: m_vPopupMenus.end());
95 else
96 m_vPopupMenus.erase(position: PopupMenuToClose);
97 SetActiveItem(nullptr);
98 }
99}
100
101void CUi::ClosePopupMenus()
102{
103 if(m_vPopupMenus.empty())
104 return;
105
106 m_vPopupMenus.clear();
107 SetActiveItem(nullptr);
108}
109
110bool CUi::IsPopupOpen() const
111{
112 return !m_vPopupMenus.empty();
113}
114
115bool CUi::IsPopupOpen(const SPopupMenuId *pId) const
116{
117 return std::any_of(first: m_vPopupMenus.begin(), last: m_vPopupMenus.end(), pred: [pId](const SPopupMenu PopupMenu) { return PopupMenu.m_pId == pId; });
118}
119
120bool CUi::IsPopupHovered() const
121{
122 return std::any_of(first: m_vPopupMenus.begin(), last: m_vPopupMenus.end(), pred: [this](const SPopupMenu PopupMenu) { return MouseHovered(pRect: &PopupMenu.m_Rect); });
123}
124
125void CUi::SMessagePopupContext::DefaultColor(ITextRender *pTextRender)
126{
127 m_TextColor = pTextRender->DefaultTextColor();
128}
129
130void CUi::SMessagePopupContext::ErrorColor()
131{
132 m_TextColor = ColorRGBA(1.0f, 0.0f, 0.0f, 1.0f);
133}
134
135CUi::EPopupMenuFunctionResult CUi::PopupMessage(void *pContext, CUIRect View, bool Active)
136{
137 SMessagePopupContext *pMessagePopup = static_cast<SMessagePopupContext *>(pContext);
138 CUi *pUI = pMessagePopup->m_pUI;
139
140 pUI->TextRender()->TextColor(Color: pMessagePopup->m_TextColor);
141 pUI->TextRender()->Text(x: View.x, y: View.y, Size: SMessagePopupContext::POPUP_FONT_SIZE, pText: pMessagePopup->m_aMessage, LineWidth: View.w);
142 pUI->TextRender()->TextColor(Color: pUI->TextRender()->DefaultTextColor());
143
144 return (Active && pUI->ConsumeHotkey(Hotkey: HOTKEY_ENTER)) ? CUi::POPUP_CLOSE_CURRENT : CUi::POPUP_KEEP_OPEN;
145}
146
147void CUi::ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext)
148{
149 const float TextWidth = std::min(a: std::ceil(x: TextRender()->TextWidth(Size: SMessagePopupContext::POPUP_FONT_SIZE, pText: pContext->m_aMessage, StrLength: -1, LineWidth: -1.0f) + 0.5f), b: SMessagePopupContext::POPUP_MAX_WIDTH);
150 float TextHeight = 0.0f;
151 STextSizeProperties TextSizeProps{};
152 TextSizeProps.m_pHeight = &TextHeight;
153 TextRender()->TextWidth(Size: SMessagePopupContext::POPUP_FONT_SIZE, pText: pContext->m_aMessage, StrLength: -1, LineWidth: TextWidth, Flags: 0, TextSizeProps);
154 pContext->m_pUI = this;
155 DoPopupMenu(pId: pContext, X, Y, Width: TextWidth + 10.0f, Height: TextHeight + 10.0f, pContext, pfnFunc: PopupMessage);
156}
157
158CUi::SConfirmPopupContext::SConfirmPopupContext()
159{
160 Reset();
161}
162
163void CUi::SConfirmPopupContext::Reset()
164{
165 m_Result = SConfirmPopupContext::UNSET;
166}
167
168void CUi::SConfirmPopupContext::YesNoButtons()
169{
170 str_copy(dst&: m_aPositiveButtonLabel, src: Localize(pStr: "Yes"));
171 str_copy(dst&: m_aNegativeButtonLabel, src: Localize(pStr: "No"));
172}
173
174void CUi::ShowPopupConfirm(float X, float Y, SConfirmPopupContext *pContext)
175{
176 const float TextWidth = std::min(a: std::ceil(x: TextRender()->TextWidth(Size: SConfirmPopupContext::POPUP_FONT_SIZE, pText: pContext->m_aMessage, StrLength: -1, LineWidth: -1.0f) + 0.5f), b: SConfirmPopupContext::POPUP_MAX_WIDTH);
177 float TextHeight = 0.0f;
178 STextSizeProperties TextSizeProps{};
179 TextSizeProps.m_pHeight = &TextHeight;
180 TextRender()->TextWidth(Size: SConfirmPopupContext::POPUP_FONT_SIZE, pText: pContext->m_aMessage, StrLength: -1, LineWidth: TextWidth, Flags: 0, TextSizeProps);
181 const float PopupHeight = TextHeight + SConfirmPopupContext::POPUP_BUTTON_HEIGHT + SConfirmPopupContext::POPUP_BUTTON_SPACING + 10.0f;
182 pContext->m_pUI = this;
183 pContext->m_Result = SConfirmPopupContext::UNSET;
184 DoPopupMenu(pId: pContext, X, Y, Width: TextWidth + 10.0f, Height: PopupHeight, pContext, pfnFunc: PopupConfirm);
185}
186
187CUi::EPopupMenuFunctionResult CUi::PopupConfirm(void *pContext, CUIRect View, bool Active)
188{
189 SConfirmPopupContext *pConfirmPopup = static_cast<SConfirmPopupContext *>(pContext);
190 CUi *pUI = pConfirmPopup->m_pUI;
191
192 CUIRect Label, ButtonBar, CancelButton, ConfirmButton;
193 View.HSplitBottom(Cut: SConfirmPopupContext::POPUP_BUTTON_HEIGHT, pTop: &Label, pBottom: &ButtonBar);
194 ButtonBar.VSplitMid(pLeft: &CancelButton, pRight: &ConfirmButton, Spacing: SConfirmPopupContext::POPUP_BUTTON_SPACING);
195
196 pUI->TextRender()->Text(x: Label.x, y: Label.y, Size: SConfirmPopupContext::POPUP_FONT_SIZE, pText: pConfirmPopup->m_aMessage, LineWidth: Label.w);
197
198 if(pUI->DoButton_PopupMenu(pButtonContainer: &pConfirmPopup->m_CancelButton, pText: pConfirmPopup->m_aNegativeButtonLabel, pRect: &CancelButton, Size: SConfirmPopupContext::POPUP_FONT_SIZE, Align: TEXTALIGN_MC))
199 {
200 pConfirmPopup->m_Result = SConfirmPopupContext::CANCELED;
201 return CUi::POPUP_CLOSE_CURRENT;
202 }
203
204 if(pUI->DoButton_PopupMenu(pButtonContainer: &pConfirmPopup->m_ConfirmButton, pText: pConfirmPopup->m_aPositiveButtonLabel, pRect: &ConfirmButton, Size: SConfirmPopupContext::POPUP_FONT_SIZE, Align: TEXTALIGN_MC) || (Active && pUI->ConsumeHotkey(Hotkey: HOTKEY_ENTER)))
205 {
206 pConfirmPopup->m_Result = SConfirmPopupContext::CONFIRMED;
207 return CUi::POPUP_CLOSE_CURRENT;
208 }
209
210 return CUi::POPUP_KEEP_OPEN;
211}
212
213CUi::SSelectionPopupContext::SSelectionPopupContext()
214{
215 Reset();
216}
217
218void CUi::SSelectionPopupContext::Reset()
219{
220 m_Props = SPopupMenuProperties();
221 m_aMessage[0] = '\0';
222 m_pSelection = nullptr;
223 m_SelectionIndex = -1;
224 m_vEntries.clear();
225 m_vButtonContainers.clear();
226 m_EntryHeight = 12.0f;
227 m_EntryPadding = 0.0f;
228 m_EntrySpacing = 5.0f;
229 m_FontSize = 10.0f;
230 m_Width = 300.0f + (SPopupMenu::POPUP_BORDER + SPopupMenu::POPUP_MARGIN) * 2;
231 m_AlignmentHeight = -1.0f;
232 m_TransparentButtons = false;
233}
234
235CUi::EPopupMenuFunctionResult CUi::PopupSelection(void *pContext, CUIRect View, bool Active)
236{
237 SSelectionPopupContext *pSelectionPopup = static_cast<SSelectionPopupContext *>(pContext);
238 CUi *pUI = pSelectionPopup->m_pUI;
239 CScrollRegion *pScrollRegion = pSelectionPopup->m_pScrollRegion;
240
241 CScrollRegionParams ScrollParams;
242 ScrollParams.m_ScrollbarThickness = 10.0f;
243 ScrollParams.m_ScrollbarMargin = SPopupMenu::POPUP_MARGIN;
244 ScrollParams.m_ScrollbarNoOuterMargin = true;
245 ScrollParams.m_ScrollUnit = 3 * (pSelectionPopup->m_EntryHeight + pSelectionPopup->m_EntrySpacing);
246 pScrollRegion->Begin(pClipRect: &View, pParams: &ScrollParams);
247
248 CUIRect Slot;
249 if(pSelectionPopup->m_aMessage[0] != '\0')
250 {
251 const STextBoundingBox TextBoundingBox = pUI->TextRender()->TextBoundingBox(Size: pSelectionPopup->m_FontSize, pText: pSelectionPopup->m_aMessage, StrLength: -1, LineWidth: pSelectionPopup->m_Width);
252 View.HSplitTop(Cut: TextBoundingBox.m_H, pTop: &Slot, pBottom: &View);
253 if(pScrollRegion->AddRect(Rect: Slot))
254 {
255 pUI->TextRender()->Text(x: Slot.x, y: Slot.y, Size: pSelectionPopup->m_FontSize, pText: pSelectionPopup->m_aMessage, LineWidth: Slot.w);
256 }
257 }
258
259 pSelectionPopup->m_vButtonContainers.resize(sz: pSelectionPopup->m_vEntries.size());
260
261 size_t Index = 0;
262 for(const auto &Entry : pSelectionPopup->m_vEntries)
263 {
264 if(pSelectionPopup->m_aMessage[0] != '\0' || Index != 0)
265 View.HSplitTop(Cut: pSelectionPopup->m_EntrySpacing, pTop: nullptr, pBottom: &View);
266 View.HSplitTop(Cut: pSelectionPopup->m_EntryHeight, pTop: &Slot, pBottom: &View);
267 if(pScrollRegion->AddRect(Rect: Slot))
268 {
269 if(pUI->DoButton_PopupMenu(pButtonContainer: &pSelectionPopup->m_vButtonContainers[Index], pText: Entry.c_str(), pRect: &Slot, Size: pSelectionPopup->m_FontSize, Align: TEXTALIGN_ML, Padding: pSelectionPopup->m_EntryPadding, TransparentInactive: pSelectionPopup->m_TransparentButtons))
270 {
271 pSelectionPopup->m_pSelection = &Entry;
272 pSelectionPopup->m_SelectionIndex = Index;
273 }
274 }
275 ++Index;
276 }
277
278 pScrollRegion->End();
279
280 return pSelectionPopup->m_pSelection == nullptr ? CUi::POPUP_KEEP_OPEN : CUi::POPUP_CLOSE_CURRENT;
281}
282
283void CUi::ShowPopupSelection(float X, float Y, SSelectionPopupContext *pContext)
284{
285 const STextBoundingBox TextBoundingBox = TextRender()->TextBoundingBox(Size: pContext->m_FontSize, pText: pContext->m_aMessage, StrLength: -1, LineWidth: pContext->m_Width);
286 const float PopupHeight = std::min(a: (pContext->m_aMessage[0] == '\0' ? -pContext->m_EntrySpacing : TextBoundingBox.m_H) + pContext->m_vEntries.size() * (pContext->m_EntryHeight + pContext->m_EntrySpacing) + (SPopupMenu::POPUP_BORDER + SPopupMenu::POPUP_MARGIN) * 2, b: Screen()->h * 0.4f);
287 pContext->m_pUI = this;
288 pContext->m_pSelection = nullptr;
289 pContext->m_SelectionIndex = -1;
290 pContext->m_Props.m_Corners = IGraphics::CORNER_ALL;
291 if(pContext->m_AlignmentHeight >= 0.0f)
292 {
293 constexpr float Margin = SPopupMenu::POPUP_BORDER + SPopupMenu::POPUP_MARGIN;
294 if(X + pContext->m_Width > Screen()->w - Margin)
295 {
296 X = std::max(a: X - pContext->m_Width, b: Margin);
297 }
298 if(Y + pContext->m_AlignmentHeight + PopupHeight > Screen()->h - Margin)
299 {
300 Y -= PopupHeight;
301 pContext->m_Props.m_Corners = IGraphics::CORNER_T;
302 }
303 else
304 {
305 Y += pContext->m_AlignmentHeight;
306 pContext->m_Props.m_Corners = IGraphics::CORNER_B;
307 }
308 }
309 DoPopupMenu(pId: pContext, X, Y, Width: pContext->m_Width, Height: PopupHeight, pContext, pfnFunc: PopupSelection, Props: pContext->m_Props);
310}
311
312int CUi::DoDropDown(CUIRect *pRect, int CurSelection, const char **pStrs, int Num, SDropDownState &State)
313{
314 if(!State.m_Init)
315 {
316 State.m_UiElement.Init(pUI: this, RequestedRectCount: -1);
317 State.m_Init = true;
318 }
319
320 const auto LabelFunc = [CurSelection, pStrs]() {
321 return CurSelection > -1 ? pStrs[CurSelection] : "";
322 };
323
324 SMenuButtonProperties Props;
325 Props.m_HintRequiresStringCheck = true;
326 Props.m_HintCanChangePositionOrSize = true;
327 Props.m_ShowDropDownIcon = true;
328 if(IsPopupOpen(pId: &State.m_SelectionPopupContext))
329 Props.m_Corners = IGraphics::CORNER_ALL & (~State.m_SelectionPopupContext.m_Props.m_Corners);
330 if(DoButton_Menu(UIElement&: State.m_UiElement, pId: &State.m_ButtonContainer, GetTextLambda: LabelFunc, pRect, Props))
331 {
332 State.m_SelectionPopupContext.Reset();
333 State.m_SelectionPopupContext.m_Props.m_BorderColor = ColorRGBA(0.7f, 0.7f, 0.7f, 0.9f);
334 State.m_SelectionPopupContext.m_Props.m_BackgroundColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.25f);
335 for(int i = 0; i < Num; ++i)
336 State.m_SelectionPopupContext.m_vEntries.emplace_back(args&: pStrs[i]);
337 State.m_SelectionPopupContext.m_EntryHeight = pRect->h;
338 State.m_SelectionPopupContext.m_EntryPadding = pRect->h >= 20.0f ? 2.0f : 1.0f;
339 State.m_SelectionPopupContext.m_FontSize = (State.m_SelectionPopupContext.m_EntryHeight - 2 * State.m_SelectionPopupContext.m_EntryPadding) * CUi::ms_FontmodHeight;
340 State.m_SelectionPopupContext.m_Width = pRect->w;
341 State.m_SelectionPopupContext.m_AlignmentHeight = pRect->h;
342 State.m_SelectionPopupContext.m_TransparentButtons = true;
343 ShowPopupSelection(X: pRect->x, Y: pRect->y, pContext: &State.m_SelectionPopupContext);
344 }
345
346 if(State.m_SelectionPopupContext.m_SelectionIndex >= 0)
347 {
348 const int NewSelection = State.m_SelectionPopupContext.m_SelectionIndex;
349 State.m_SelectionPopupContext.Reset();
350 return NewSelection;
351 }
352
353 return CurSelection;
354}
355
356CUi::EPopupMenuFunctionResult CUi::PopupColorPicker(void *pContext, CUIRect View, bool Active)
357{
358 SColorPickerPopupContext *pColorPicker = static_cast<SColorPickerPopupContext *>(pContext);
359 CUi *pUI = pColorPicker->m_pUI;
360 pColorPicker->m_State = EEditState::NONE;
361
362 CUIRect ColorsArea, HueArea, BottomArea, ModeButtonArea, HueRect, SatRect, ValueRect, HexRect, AlphaRect;
363
364 View.HSplitTop(Cut: 140.0f, pTop: &ColorsArea, pBottom: &BottomArea);
365 ColorsArea.VSplitRight(Cut: 20.0f, pLeft: &ColorsArea, pRight: &HueArea);
366
367 BottomArea.HSplitTop(Cut: 3.0f, pTop: nullptr, pBottom: &BottomArea);
368 HueArea.VSplitLeft(Cut: 3.0f, pLeft: nullptr, pRight: &HueArea);
369
370 BottomArea.HSplitTop(Cut: 20.0f, pTop: &HueRect, pBottom: &BottomArea);
371 BottomArea.HSplitTop(Cut: 3.0f, pTop: nullptr, pBottom: &BottomArea);
372
373 constexpr float ValuePadding = 5.0f;
374 const float HsvValueWidth = (HueRect.w - ValuePadding * 2) / 3.0f;
375 const float HexValueWidth = HsvValueWidth * 2 + ValuePadding;
376
377 HueRect.VSplitLeft(Cut: HsvValueWidth, pLeft: &HueRect, pRight: &SatRect);
378 SatRect.VSplitLeft(Cut: ValuePadding, pLeft: nullptr, pRight: &SatRect);
379 SatRect.VSplitLeft(Cut: HsvValueWidth, pLeft: &SatRect, pRight: &ValueRect);
380 ValueRect.VSplitLeft(Cut: ValuePadding, pLeft: nullptr, pRight: &ValueRect);
381
382 BottomArea.HSplitTop(Cut: 20.0f, pTop: &HexRect, pBottom: &BottomArea);
383 BottomArea.HSplitTop(Cut: 3.0f, pTop: nullptr, pBottom: &BottomArea);
384 HexRect.VSplitLeft(Cut: HexValueWidth, pLeft: &HexRect, pRight: &AlphaRect);
385 AlphaRect.VSplitLeft(Cut: ValuePadding, pLeft: nullptr, pRight: &AlphaRect);
386 BottomArea.HSplitTop(Cut: 20.0f, pTop: &ModeButtonArea, pBottom: &BottomArea);
387
388 const ColorRGBA BlackColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f);
389
390 HueArea.Draw(Color: BlackColor, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
391 HueArea.Margin(Cut: 1.0f, pOtherRect: &HueArea);
392
393 ColorsArea.Draw(Color: BlackColor, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
394 ColorsArea.Margin(Cut: 1.0f, pOtherRect: &ColorsArea);
395
396 ColorHSVA PickerColorHSV = pColorPicker->m_HsvaColor;
397 ColorRGBA PickerColorRGB = pColorPicker->m_RgbaColor;
398 ColorHSLA PickerColorHSL = pColorPicker->m_HslaColor;
399
400 // Color Area
401 ColorRGBA TL, TR, BL, BR;
402 TL = BL = color_cast<ColorRGBA>(hsv: ColorHSVA(PickerColorHSV.x, 0.0f, 1.0f));
403 TR = BR = color_cast<ColorRGBA>(hsv: ColorHSVA(PickerColorHSV.x, 1.0f, 1.0f));
404 ColorsArea.Draw4(ColorTopLeft: TL, ColorTopRight: TR, ColorBottomLeft: BL, ColorBottomRight: BR, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
405
406 TL = TR = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
407 BL = BR = ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f);
408 ColorsArea.Draw4(ColorTopLeft: TL, ColorTopRight: TR, ColorBottomLeft: BL, ColorBottomRight: BR, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
409
410 // Hue Area
411 static const float s_aaColorIndices[7][3] = {
412 {1.0f, 0.0f, 0.0f}, // red
413 {1.0f, 0.0f, 1.0f}, // magenta
414 {0.0f, 0.0f, 1.0f}, // blue
415 {0.0f, 1.0f, 1.0f}, // cyan
416 {0.0f, 1.0f, 0.0f}, // green
417 {1.0f, 1.0f, 0.0f}, // yellow
418 {1.0f, 0.0f, 0.0f}, // red
419 };
420
421 const float HuePickerOffset = HueArea.h / 6.0f;
422 CUIRect HuePartialArea = HueArea;
423 HuePartialArea.h = HuePickerOffset;
424
425 for(size_t j = 0; j < std::size(s_aaColorIndices) - 1; j++)
426 {
427 TL = ColorRGBA(s_aaColorIndices[j][0], s_aaColorIndices[j][1], s_aaColorIndices[j][2], 1.0f);
428 BL = ColorRGBA(s_aaColorIndices[j + 1][0], s_aaColorIndices[j + 1][1], s_aaColorIndices[j + 1][2], 1.0f);
429
430 HuePartialArea.y = HueArea.y + HuePickerOffset * j;
431 HuePartialArea.Draw4(ColorTopLeft: TL, ColorTopRight: TL, ColorBottomLeft: BL, ColorBottomRight: BL, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
432 }
433
434 const auto &&RenderAlphaSelector = [&](unsigned OldA) -> SEditResult<int64_t> {
435 if(pColorPicker->m_Alpha)
436 {
437 return pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[3], pRect: &AlphaRect, pLabel: "A:", Current: OldA, Min: 0, Max: 255);
438 }
439 else
440 {
441 char aBuf[8];
442 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "A: %d", OldA);
443 pUI->DoLabel(pRect: &AlphaRect, pText: aBuf, Size: 10.0f, Align: TEXTALIGN_MC);
444 AlphaRect.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.65f), Corners: IGraphics::CORNER_ALL, Rounding: 3.0f);
445 return {.m_State: EEditState::NONE, .m_Value: OldA};
446 }
447 };
448
449 // Editboxes Area
450 if(pColorPicker->m_ColorMode == SColorPickerPopupContext::MODE_HSVA)
451 {
452 const unsigned OldH = round_to_int(f: PickerColorHSV.h * 255.0f);
453 const unsigned OldS = round_to_int(f: PickerColorHSV.s * 255.0f);
454 const unsigned OldV = round_to_int(f: PickerColorHSV.v * 255.0f);
455 const unsigned OldA = round_to_int(f: PickerColorHSV.a * 255.0f);
456
457 const auto [StateH, H] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[0], pRect: &HueRect, pLabel: "H:", Current: OldH, Min: 0, Max: 255);
458 const auto [StateS, S] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[1], pRect: &SatRect, pLabel: "S:", Current: OldS, Min: 0, Max: 255);
459 const auto [StateV, V] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[2], pRect: &ValueRect, pLabel: "V:", Current: OldV, Min: 0, Max: 255);
460 const auto [StateA, A] = RenderAlphaSelector(OldA);
461
462 if(OldH != H || OldS != S || OldV != V || OldA != A)
463 {
464 PickerColorHSV = ColorHSVA(H / 255.0f, S / 255.0f, V / 255.0f, A / 255.0f);
465 PickerColorHSL = color_cast<ColorHSLA>(hsv: PickerColorHSV);
466 PickerColorRGB = color_cast<ColorRGBA>(hsl: PickerColorHSL);
467 }
468
469 for(auto State : {StateH, StateS, StateV, StateA})
470 {
471 if(State != EEditState::NONE)
472 {
473 pColorPicker->m_State = State;
474 break;
475 }
476 }
477 }
478 else if(pColorPicker->m_ColorMode == SColorPickerPopupContext::MODE_RGBA)
479 {
480 const unsigned OldR = round_to_int(f: PickerColorRGB.r * 255.0f);
481 const unsigned OldG = round_to_int(f: PickerColorRGB.g * 255.0f);
482 const unsigned OldB = round_to_int(f: PickerColorRGB.b * 255.0f);
483 const unsigned OldA = round_to_int(f: PickerColorRGB.a * 255.0f);
484
485 const auto [StateR, R] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[0], pRect: &HueRect, pLabel: "R:", Current: OldR, Min: 0, Max: 255);
486 const auto [StateG, G] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[1], pRect: &SatRect, pLabel: "G:", Current: OldG, Min: 0, Max: 255);
487 const auto [StateB, B] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[2], pRect: &ValueRect, pLabel: "B:", Current: OldB, Min: 0, Max: 255);
488 const auto [StateA, A] = RenderAlphaSelector(OldA);
489
490 if(OldR != R || OldG != G || OldB != B || OldA != A)
491 {
492 PickerColorRGB = ColorRGBA(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
493 PickerColorHSL = color_cast<ColorHSLA>(rgb: PickerColorRGB);
494 PickerColorHSV = color_cast<ColorHSVA>(hsl: PickerColorHSL);
495 }
496
497 for(auto State : {StateR, StateG, StateB, StateA})
498 {
499 if(State != EEditState::NONE)
500 {
501 pColorPicker->m_State = State;
502 break;
503 }
504 }
505 }
506 else if(pColorPicker->m_ColorMode == SColorPickerPopupContext::MODE_HSLA)
507 {
508 const unsigned OldH = round_to_int(f: PickerColorHSL.h * 255.0f);
509 const unsigned OldS = round_to_int(f: PickerColorHSL.s * 255.0f);
510 const unsigned OldL = round_to_int(f: PickerColorHSL.l * 255.0f);
511 const unsigned OldA = round_to_int(f: PickerColorHSL.a * 255.0f);
512
513 const auto [StateH, H] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[0], pRect: &HueRect, pLabel: "H:", Current: OldH, Min: 0, Max: 255);
514 const auto [StateS, S] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[1], pRect: &SatRect, pLabel: "S:", Current: OldS, Min: 0, Max: 255);
515 const auto [StateL, L] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[2], pRect: &ValueRect, pLabel: "L:", Current: OldL, Min: 0, Max: 255);
516 const auto [StateA, A] = RenderAlphaSelector(OldA);
517
518 if(OldH != H || OldS != S || OldL != L || OldA != A)
519 {
520 PickerColorHSL = ColorHSLA(H / 255.0f, S / 255.0f, L / 255.0f, A / 255.0f);
521 PickerColorHSV = color_cast<ColorHSVA>(hsl: PickerColorHSL);
522 PickerColorRGB = color_cast<ColorRGBA>(hsl: PickerColorHSL);
523 }
524
525 for(auto State : {StateH, StateS, StateL, StateA})
526 {
527 if(State != EEditState::NONE)
528 {
529 pColorPicker->m_State = State;
530 break;
531 }
532 }
533 }
534 else
535 {
536 dbg_assert_failed("Color picker mode invalid: %d", (int)pColorPicker->m_ColorMode);
537 }
538
539 SValueSelectorProperties Props;
540 Props.m_UseScroll = false;
541 Props.m_IsHex = true;
542 Props.m_HexPrefix = pColorPicker->m_Alpha ? 8 : 6;
543 const unsigned OldHex = PickerColorRGB.PackAlphaLast(Alpha: pColorPicker->m_Alpha);
544 auto [HexState, Hex] = pUI->DoValueSelectorWithState(pId: &pColorPicker->m_aValueSelectorIds[4], pRect: &HexRect, pLabel: "Hex:", Current: OldHex, Min: 0, Max: pColorPicker->m_Alpha ? 0xFFFFFFFFll : 0xFFFFFFll, Props);
545 if(OldHex != Hex)
546 {
547 const float OldAlpha = PickerColorRGB.a;
548 PickerColorRGB = ColorRGBA::UnpackAlphaLast<ColorRGBA>(Color: Hex, Alpha: pColorPicker->m_Alpha);
549 if(!pColorPicker->m_Alpha)
550 PickerColorRGB.a = OldAlpha;
551 PickerColorHSL = color_cast<ColorHSLA>(rgb: PickerColorRGB);
552 PickerColorHSV = color_cast<ColorHSVA>(hsl: PickerColorHSL);
553 }
554
555 if(HexState != EEditState::NONE)
556 pColorPicker->m_State = HexState;
557
558 // Logic
559 float PickerX, PickerY;
560 EEditState ColorPickerRes = pUI->DoPickerLogic(pId: &pColorPicker->m_ColorPickerId, pRect: &ColorsArea, pX: &PickerX, pY: &PickerY);
561 if(ColorPickerRes != EEditState::NONE)
562 {
563 PickerColorHSV.y = PickerX / ColorsArea.w;
564 PickerColorHSV.z = 1.0f - PickerY / ColorsArea.h;
565 PickerColorHSL = color_cast<ColorHSLA>(hsv: PickerColorHSV);
566 PickerColorRGB = color_cast<ColorRGBA>(hsl: PickerColorHSL);
567 pColorPicker->m_State = ColorPickerRes;
568 }
569
570 EEditState HuePickerRes = pUI->DoPickerLogic(pId: &pColorPicker->m_HuePickerId, pRect: &HueArea, pX: &PickerX, pY: &PickerY);
571 if(HuePickerRes != EEditState::NONE)
572 {
573 PickerColorHSV.x = 1.0f - PickerY / HueArea.h;
574 PickerColorHSL = color_cast<ColorHSLA>(hsv: PickerColorHSV);
575 PickerColorRGB = color_cast<ColorRGBA>(hsl: PickerColorHSL);
576 pColorPicker->m_State = HuePickerRes;
577 }
578
579 // Marker Color Area
580 const float MarkerX = ColorsArea.x + ColorsArea.w * PickerColorHSV.y;
581 const float MarkerY = ColorsArea.y + ColorsArea.h * (1.0f - PickerColorHSV.z);
582
583 const float MarkerOutlineInd = PickerColorHSV.z > 0.5f ? 0.0f : 1.0f;
584 const ColorRGBA MarkerOutline = ColorRGBA(MarkerOutlineInd, MarkerOutlineInd, MarkerOutlineInd, 1.0f);
585
586 pUI->Graphics()->TextureClear();
587 pUI->Graphics()->QuadsBegin();
588 pUI->Graphics()->SetColor(MarkerOutline);
589 pUI->Graphics()->DrawCircle(CenterX: MarkerX, CenterY: MarkerY, Radius: 4.5f, Segments: 32);
590 pUI->Graphics()->SetColor(PickerColorRGB);
591 pUI->Graphics()->DrawCircle(CenterX: MarkerX, CenterY: MarkerY, Radius: 3.5f, Segments: 32);
592 pUI->Graphics()->QuadsEnd();
593
594 // Marker Hue Area
595 CUIRect HueMarker;
596 HueArea.Margin(Cut: -2.5f, pOtherRect: &HueMarker);
597 HueMarker.h = 6.5f;
598 HueMarker.y = (HueArea.y + HueArea.h * (1.0f - PickerColorHSV.x)) - HueMarker.h / 2.0f;
599
600 const ColorRGBA HueMarkerColor = color_cast<ColorRGBA>(hsv: ColorHSVA(PickerColorHSV.x, 1.0f, 1.0f, 1.0f));
601 const float HueMarkerOutlineColor = PickerColorHSV.x > 0.75f ? 1.0f : 0.0f;
602 const ColorRGBA HueMarkerOutline = ColorRGBA(HueMarkerOutlineColor, HueMarkerOutlineColor, HueMarkerOutlineColor, 1.0f);
603
604 HueMarker.Draw(Color: HueMarkerOutline, Corners: IGraphics::CORNER_ALL, Rounding: 1.2f);
605 HueMarker.Margin(Cut: 1.2f, pOtherRect: &HueMarker);
606 HueMarker.Draw(Color: HueMarkerColor, Corners: IGraphics::CORNER_ALL, Rounding: 1.2f);
607
608 pColorPicker->m_HsvaColor = PickerColorHSV;
609 pColorPicker->m_RgbaColor = PickerColorRGB;
610 pColorPicker->m_HslaColor = PickerColorHSL;
611 if(pColorPicker->m_pHslaColor != nullptr)
612 *pColorPicker->m_pHslaColor = PickerColorHSL.Pack(Alpha: pColorPicker->m_Alpha);
613
614 static constexpr SColorPickerPopupContext::EColorPickerMode PICKER_MODES[] = {SColorPickerPopupContext::MODE_HSVA, SColorPickerPopupContext::MODE_RGBA, SColorPickerPopupContext::MODE_HSLA};
615 static constexpr const char *PICKER_MODE_LABELS[] = {"HSVA", "RGBA", "HSLA"};
616 static_assert(std::size(PICKER_MODES) == std::size(PICKER_MODE_LABELS));
617 for(SColorPickerPopupContext::EColorPickerMode Mode : PICKER_MODES)
618 {
619 CUIRect ModeButton;
620 ModeButtonArea.VSplitLeft(Cut: HsvValueWidth, pLeft: &ModeButton, pRight: &ModeButtonArea);
621 ModeButtonArea.VSplitLeft(Cut: ValuePadding, pLeft: nullptr, pRight: &ModeButtonArea);
622 if(pUI->DoButton_PopupMenu(pButtonContainer: &pColorPicker->m_aModeButtons[(int)Mode], pText: PICKER_MODE_LABELS[Mode], pRect: &ModeButton, Size: 10.0f, Align: TEXTALIGN_MC, Padding: 2.0f, TransparentInactive: false, Enabled: pColorPicker->m_ColorMode != Mode))
623 {
624 pColorPicker->m_ColorMode = Mode;
625 }
626 }
627
628 return CUi::POPUP_KEEP_OPEN;
629}
630
631void CUi::ShowPopupColorPicker(float X, float Y, SColorPickerPopupContext *pContext)
632{
633 pContext->m_pUI = this;
634 if(pContext->m_ColorMode == SColorPickerPopupContext::MODE_UNSET)
635 pContext->m_ColorMode = SColorPickerPopupContext::MODE_HSVA;
636 DoPopupMenu(pId: pContext, X, Y, Width: 160.0f + 10.0f, Height: 209.0f + 10.0f, pContext, pfnFunc: PopupColorPicker);
637}
638