Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "CDropDown.h"
21 :
22 : #include "gui/CGUI.h"
23 : #include "gui/IGUIScrollBar.h"
24 : #include "gui/SGUIMessage.h"
25 : #include "gui/SettingTypes/CGUIColor.h"
26 : #include "gui/SettingTypes/CGUIList.h"
27 : #include "gui/SettingTypes/CGUIString.h"
28 : #include "lib/external_libraries/libsdl.h"
29 : #include "lib/timer.h"
30 : #include "maths/Size2D.h"
31 :
32 : #include <SDL_events.h>
33 : #include <SDL_keycode.h>
34 : #include <algorithm>
35 : #include <cmath>
36 : #include <cstdlib>
37 : #include <limits>
38 : #include <vector>
39 :
40 0 : CDropDown::CDropDown(CGUI& pGUI)
41 : : CList(pGUI),
42 0 : m_Open(),
43 0 : m_HideScrollBar(),
44 0 : m_ElementHighlight(-1),
45 0 : m_ButtonWidth(this, "button_width"),
46 0 : m_DropDownSize(this, "dropdown_size"),
47 0 : m_DropDownBuffer(this, "dropdown_buffer"),
48 0 : m_MinimumVisibleItems(this, "minimum_visible_items"),
49 0 : m_SoundClosed(this, "sound_closed"),
50 0 : m_SoundEnter(this, "sound_enter"),
51 0 : m_SoundLeave(this, "sound_leave"),
52 0 : m_SoundOpened(this, "sound_opened"),
53 : // Setting "sprite" is registered by CList and used as the background
54 0 : m_SpriteDisabled(this, "sprite_disabled"),
55 0 : m_SpriteOverlayDisabled(this, "sprite_overlay_disabled"),
56 0 : m_SpriteList(this, "sprite_list"), // Background of the drop down list
57 0 : m_SpriteListOverlay(this, "sprite_list_overlay"), // Overlay above the drop down list
58 0 : m_Sprite2(this, "sprite2"), // Button that sits to the right
59 0 : m_Sprite2Over(this, "sprite2_over"),
60 0 : m_Sprite2Pressed(this, "sprite2_pressed"),
61 0 : m_Sprite2Disabled(this, "sprite2_disabled"),
62 0 : m_TextColorDisabled(this, "textcolor_disabled")
63 : // Add these in CList! And implement TODO
64 : //m_TextColorOver("textcolor_over");
65 : //m_TextColorPressed("textcolor_pressed");
66 : {
67 0 : m_ScrollBar.Set(true, true);
68 0 : }
69 :
70 0 : void CDropDown::SetupText()
71 : {
72 0 : SetupListRect();
73 0 : CList::SetupText();
74 0 : }
75 :
76 0 : void CDropDown::HandleSizeChanged()
77 : {
78 0 : CList::HandleSizeChanged();
79 0 : SetupText();
80 0 : }
81 :
82 0 : void CDropDown::HandleMessage(SGUIMessage& Message)
83 : {
84 : // CList::HandleMessage(Message); placed after the switch!
85 :
86 0 : switch (Message.type)
87 : {
88 0 : case GUIM_SETTINGS_UPDATED:
89 0 : {
90 : // Update cached list rect
91 0 : if (Message.value == "size" ||
92 0 : Message.value == "absolute" ||
93 0 : Message.value == "dropdown_size" ||
94 0 : Message.value == "dropdown_buffer" ||
95 0 : Message.value == "minimum_visible_items" ||
96 0 : Message.value == "scrollbar_style" ||
97 0 : Message.value == "button_width")
98 : {
99 0 : SetupListRect();
100 : }
101 :
102 : break;
103 : }
104 :
105 0 : case GUIM_MOUSE_MOTION:
106 0 : {
107 0 : if (!m_Open)
108 : break;
109 :
110 0 : CVector2D mouse = m_pGUI.GetMousePos();
111 :
112 0 : if (!GetListRect().PointInside(mouse))
113 : break;
114 :
115 0 : const float scroll = m_ScrollBar ? GetScrollBar(0).GetPos() : 0.f;
116 :
117 0 : CRect rect = GetListRect();
118 0 : mouse.Y += scroll;
119 0 : int set = -1;
120 0 : for (int i = 0; i < static_cast<int>(m_List->m_Items.size()); ++i)
121 : {
122 0 : if (mouse.Y >= rect.top + m_ItemsYPositions[i] &&
123 0 : mouse.Y < rect.top + m_ItemsYPositions[i+1] &&
124 : // mouse is not over scroll-bar
125 0 : (m_HideScrollBar ||
126 0 : mouse.X < GetScrollBar(0).GetOuterRect().left ||
127 0 : mouse.X > GetScrollBar(0).GetOuterRect().right))
128 : {
129 : set = i;
130 : }
131 : }
132 :
133 0 : if (set != -1)
134 : {
135 0 : m_ElementHighlight = set;
136 : //UpdateAutoScroll();
137 : }
138 :
139 : break;
140 : }
141 :
142 0 : case GUIM_MOUSE_ENTER:
143 0 : {
144 0 : if (m_Enabled)
145 0 : PlaySound(m_SoundEnter);
146 : break;
147 : }
148 :
149 0 : case GUIM_MOUSE_LEAVE:
150 0 : {
151 0 : m_ElementHighlight = m_Selected;
152 :
153 0 : if (m_Enabled)
154 0 : PlaySound(m_SoundLeave);
155 : break;
156 : }
157 :
158 : // We can't inherent this routine from CList, because we need to include
159 : // a mouse click to open the dropdown, also the coordinates are changed.
160 0 : case GUIM_MOUSE_PRESS_LEFT:
161 0 : {
162 0 : if (!m_Enabled)
163 : {
164 0 : PlaySound(m_SoundDisabled);
165 0 : break;
166 : }
167 :
168 0 : if (!m_Open)
169 : {
170 0 : if (m_List->m_Items.empty())
171 : return;
172 :
173 0 : if (m_VisibleArea && !m_VisibleArea.PointInside(m_pGUI.GetMousePos()))
174 : return;
175 :
176 0 : m_Open = true;
177 0 : GetScrollBar(0).SetZ(GetBufferedZ());
178 0 : m_ElementHighlight = m_Selected;
179 :
180 : // Start at the position of the selected item, if possible.
181 0 : if (m_ItemsYPositions.empty() || m_ElementHighlight < 0 || static_cast<size_t>(m_ElementHighlight) >= m_ItemsYPositions.size())
182 0 : GetScrollBar(0).SetPos(0);
183 : else
184 0 : GetScrollBar(0).SetPos(m_ItemsYPositions[m_ElementHighlight] - 60);
185 :
186 0 : PlaySound(m_SoundOpened);
187 0 : return; // overshadow
188 : }
189 : else
190 : {
191 0 : const CVector2D& mouse = m_pGUI.GetMousePos();
192 :
193 : // If the regular area is pressed, then abort, and close.
194 0 : if (GetActualSize().PointInside(mouse))
195 : {
196 0 : m_Open = false;
197 0 : GetScrollBar(0).SetZ(GetBufferedZ());
198 0 : PlaySound(m_SoundClosed);
199 0 : return; // overshadow
200 : }
201 :
202 0 : if (m_HideScrollBar ||
203 0 : mouse.X < GetScrollBar(0).GetOuterRect().left ||
204 0 : mouse.X > GetScrollBar(0).GetOuterRect().right ||
205 0 : mouse.Y < GetListRect().top)
206 : {
207 0 : m_Open = false;
208 0 : GetScrollBar(0).SetZ(GetBufferedZ());
209 : }
210 : }
211 : break;
212 : }
213 :
214 0 : case GUIM_MOUSE_WHEEL_DOWN:
215 0 : {
216 : // Don't switch elements by scrolling when open, causes a confusing interaction between this and the scrollbar.
217 0 : if (m_Open || !m_Enabled)
218 : break;
219 :
220 0 : Message.Skip(false);
221 :
222 0 : m_ElementHighlight = m_Selected;
223 :
224 0 : if (m_ElementHighlight + 1 >= (int)m_ItemsYPositions.size() - 1)
225 : break;
226 :
227 0 : ++m_ElementHighlight;
228 0 : m_Selected.Set(m_ElementHighlight, true);
229 : break;
230 : }
231 :
232 0 : case GUIM_MOUSE_WHEEL_UP:
233 0 : {
234 : // Don't switch elements by scrolling when open, causes a confusing interaction between this and the scrollbar.
235 0 : if (m_Open || !m_Enabled)
236 : break;
237 :
238 0 : Message.Skip(false);
239 :
240 0 : m_ElementHighlight = m_Selected;
241 0 : if (m_ElementHighlight - 1 < 0)
242 : break;
243 :
244 0 : --m_ElementHighlight;
245 0 : m_Selected.Set(m_ElementHighlight, true);
246 : break;
247 : }
248 :
249 0 : case GUIM_LOST_FOCUS:
250 0 : {
251 0 : if (m_Open)
252 0 : PlaySound(m_SoundClosed);
253 :
254 0 : m_Open = false;
255 0 : break;
256 : }
257 :
258 0 : case GUIM_LOAD:
259 0 : SetupListRect();
260 0 : break;
261 :
262 : default:
263 : break;
264 : }
265 :
266 : // Important that this is after, so that overshadowed implementations aren't processed
267 0 : CList::HandleMessage(Message);
268 :
269 : // As HandleMessage functions return void, we need to manually verify
270 : // whether the child list's items were modified.
271 0 : if (CList::GetModified())
272 0 : SetupText();
273 : }
274 :
275 0 : Input::Reaction CDropDown::ManuallyHandleKeys(const SDL_Event& ev)
276 : {
277 0 : Input::Reaction result{Input::Reaction::PASS};
278 0 : bool update_highlight = false;
279 :
280 0 : if (ev.type == SDL_KEYDOWN)
281 : {
282 0 : int szChar = ev.key.keysym.sym;
283 :
284 0 : switch (szChar)
285 : {
286 0 : case '\r':
287 0 : m_Open = false;
288 0 : result = Input::Reaction::HANDLED;
289 0 : break;
290 :
291 0 : case SDLK_HOME:
292 0 : case SDLK_END:
293 0 : case SDLK_UP:
294 0 : case SDLK_DOWN:
295 0 : case SDLK_PAGEUP:
296 0 : case SDLK_PAGEDOWN:
297 0 : if (!m_Open)
298 : return Input::Reaction::PASS;
299 : // Set current selected item to highlighted, before
300 : // then really processing these in CList::ManuallyHandleKeys()
301 0 : m_Selected.Set(m_ElementHighlight, true);
302 0 : update_highlight = true;
303 0 : break;
304 :
305 0 : default:
306 : // If we have typed a character try to get the closest element to it.
307 : // TODO: not too nice and doesn't deal with dashes.
308 0 : if (m_Open && ((szChar >= SDLK_a && szChar <= SDLK_z) || szChar == SDLK_SPACE
309 0 : || (szChar >= SDLK_0 && szChar <= SDLK_9)
310 0 : || (szChar >= SDLK_KP_1 && szChar <= SDLK_KP_0)))
311 : {
312 : // arbitrary 1 second limit to add to string or start fresh.
313 : // maximal amount of characters is 100, which imo is far more than enough.
314 0 : if (timer_Time() - m_TimeOfLastInput > 1.0 || m_InputBuffer.length() >= 100)
315 0 : m_InputBuffer = szChar;
316 : else
317 0 : m_InputBuffer += szChar;
318 :
319 0 : m_TimeOfLastInput = timer_Time();
320 :
321 : // let's look for the closest element
322 : // basically it's alphabetic order and "as many letters as we can get".
323 0 : int closest = -1;
324 0 : int bestIndex = -1;
325 0 : int difference = 1250;
326 0 : for (int i = 0; i < static_cast<int>(m_List->m_Items.size()); ++i)
327 : {
328 : int indexOfDifference = 0;
329 : int diff = 0;
330 0 : for (size_t j = 0; j < m_InputBuffer.length(); ++j)
331 : {
332 0 : diff = std::abs(static_cast<int>(m_List->m_Items[i].GetRawString().LowerCase()[j]) - static_cast<int>(m_InputBuffer[j]));
333 0 : if (diff == 0)
334 0 : indexOfDifference = j+1;
335 : else
336 : break;
337 : }
338 0 : if (indexOfDifference > bestIndex || (indexOfDifference >= bestIndex && diff < difference))
339 : {
340 0 : bestIndex = indexOfDifference;
341 0 : closest = i;
342 0 : difference = diff;
343 : }
344 : }
345 : // let's select the closest element. There should basically always be one.
346 0 : if (closest != -1)
347 : {
348 0 : m_Selected.Set(closest, true);
349 0 : update_highlight = true;
350 0 : GetScrollBar(0).SetPos(m_ItemsYPositions[closest] - 60);
351 : }
352 : result = Input::Reaction::HANDLED;
353 : }
354 : break;
355 : }
356 : }
357 :
358 0 : if (CList::ManuallyHandleKeys(ev) == Input::Reaction::HANDLED)
359 0 : result = Input::Reaction::HANDLED;
360 :
361 0 : if (update_highlight)
362 0 : m_ElementHighlight = m_Selected;
363 :
364 : return result;
365 : }
366 :
367 0 : void CDropDown::SetupListRect()
368 : {
369 0 : const CSize2D windowSize = m_pGUI.GetWindowSize();
370 :
371 0 : if (m_ItemsYPositions.empty())
372 : {
373 0 : m_CachedListRect = CRect(GetActualSize().left, GetActualSize().bottom + m_DropDownBuffer,
374 0 : GetActualSize().right, GetActualSize().bottom + m_DropDownBuffer + m_DropDownSize);
375 0 : m_HideScrollBar = false;
376 : }
377 : // Too many items so use a scrollbar
378 0 : else if (m_ItemsYPositions.back() > m_DropDownSize)
379 : {
380 : // Place items below if at least some items can be placed below
381 0 : if (GetActualSize().bottom + m_DropDownBuffer + m_DropDownSize <= windowSize.Height)
382 0 : m_CachedListRect = CRect(GetActualSize().left, GetActualSize().bottom + m_DropDownBuffer,
383 0 : GetActualSize().right, GetActualSize().bottom + m_DropDownBuffer + m_DropDownSize);
384 0 : else if ((m_ItemsYPositions.size() > m_MinimumVisibleItems && windowSize.Height - GetActualSize().bottom - m_DropDownBuffer >= m_ItemsYPositions[m_MinimumVisibleItems]) ||
385 0 : GetActualSize().top < windowSize.Height - GetActualSize().bottom)
386 0 : m_CachedListRect = CRect(GetActualSize().left, GetActualSize().bottom + m_DropDownBuffer,
387 0 : GetActualSize().right, windowSize.Height);
388 : // Not enough space below, thus place items above
389 : else
390 0 : m_CachedListRect = CRect(GetActualSize().left, std::max(0.f, GetActualSize().top - m_DropDownBuffer - m_DropDownSize),
391 0 : GetActualSize().right, GetActualSize().top - m_DropDownBuffer);
392 :
393 0 : m_HideScrollBar = false;
394 : }
395 : else
396 : {
397 : // Enough space below, no scrollbar needed
398 0 : if (GetActualSize().bottom + m_DropDownBuffer + m_ItemsYPositions.back() <= windowSize.Height)
399 : {
400 0 : m_CachedListRect = CRect(GetActualSize().left, GetActualSize().bottom + m_DropDownBuffer,
401 0 : GetActualSize().right, GetActualSize().bottom + m_DropDownBuffer + m_ItemsYPositions.back());
402 0 : m_HideScrollBar = true;
403 : }
404 : // Enough space below for some items, but not all, so place items below and use a scrollbar
405 0 : else if ((m_ItemsYPositions.size() > m_MinimumVisibleItems && windowSize.Height - GetActualSize().bottom - m_DropDownBuffer >= m_ItemsYPositions[m_MinimumVisibleItems]) ||
406 0 : GetActualSize().top < windowSize.Height - GetActualSize().bottom)
407 : {
408 0 : m_CachedListRect = CRect(GetActualSize().left, GetActualSize().bottom + m_DropDownBuffer,
409 0 : GetActualSize().right, windowSize.Height);
410 0 : m_HideScrollBar = false;
411 : }
412 : // Not enough space below, thus place items above. Hide the scrollbar accordingly
413 : else
414 : {
415 0 : m_CachedListRect = CRect(GetActualSize().left, std::max(0.f, GetActualSize().top - m_DropDownBuffer - m_ItemsYPositions.back()),
416 0 : GetActualSize().right, GetActualSize().top - m_DropDownBuffer);
417 0 : m_HideScrollBar = GetActualSize().top > m_ItemsYPositions.back() + m_DropDownBuffer;
418 : }
419 : }
420 0 : }
421 :
422 0 : CRect CDropDown::GetListRect() const
423 : {
424 0 : return m_CachedListRect;
425 : }
426 :
427 0 : bool CDropDown::IsMouseOver() const
428 : {
429 0 : if (m_Open)
430 : {
431 0 : CRect rect(GetActualSize().left, std::min(GetActualSize().top, GetListRect().top),
432 0 : GetActualSize().right, std::max(GetActualSize().bottom, GetListRect().bottom));
433 0 : return rect.PointInside(m_pGUI.GetMousePos());
434 : }
435 : else
436 0 : return IGUIObject::IsMouseOver();
437 : }
438 :
439 0 : void CDropDown::Draw(CCanvas2D& canvas)
440 : {
441 0 : const CGUISpriteInstance& sprite = m_Enabled ? m_Sprite : m_SpriteDisabled;
442 0 : const CGUISpriteInstance& spriteOverlay = m_Enabled ? m_SpriteOverlay : m_SpriteOverlayDisabled;
443 :
444 0 : m_pGUI.DrawSprite(sprite, canvas, GetActualSize(), m_VisibleArea);
445 :
446 0 : if (m_ButtonWidth > 0.f)
447 : {
448 0 : CRect rect(GetActualSize().right - m_ButtonWidth, GetActualSize().top,
449 0 : GetActualSize().right, GetActualSize().bottom);
450 :
451 0 : if (!m_Enabled)
452 : {
453 0 : m_pGUI.DrawSprite(*m_Sprite2Disabled ? m_Sprite2Disabled : m_Sprite2, canvas, rect, m_VisibleArea);
454 : }
455 0 : else if (m_Open)
456 : {
457 0 : m_pGUI.DrawSprite(*m_Sprite2Pressed ? m_Sprite2Pressed : m_Sprite2, canvas, rect, m_VisibleArea);
458 : }
459 0 : else if (m_MouseHovering)
460 : {
461 0 : m_pGUI.DrawSprite(*m_Sprite2Over ? m_Sprite2Over : m_Sprite2, canvas, rect, m_VisibleArea);
462 : }
463 : else
464 0 : m_pGUI.DrawSprite(m_Sprite2, canvas, rect, m_VisibleArea);
465 : }
466 :
467 0 : if (m_Selected != -1) // TODO: Maybe check validity completely?
468 : {
469 0 : CRect cliparea = m_VisibleArea != CRect() ? m_VisibleArea : GetActualSize();
470 0 : if (cliparea.right > GetActualSize().right - m_ButtonWidth)
471 0 : cliparea.right = GetActualSize().right - m_ButtonWidth;
472 :
473 0 : CVector2D pos(GetActualSize().left, GetActualSize().top);
474 0 : DrawText(canvas, m_Selected, m_Enabled ? m_TextColorSelected : m_TextColorDisabled, pos, cliparea);
475 : }
476 :
477 0 : if (m_Open)
478 : {
479 : // Disable scrollbar during drawing without sending a setting-changed message
480 0 : const bool old = m_ScrollBar;
481 :
482 : // TODO: drawScrollbar as an argument of DrawList?
483 0 : if (m_HideScrollBar)
484 0 : m_ScrollBar.Set(false, false);
485 :
486 0 : DrawList(canvas, m_ElementHighlight, m_SpriteList, m_SpriteListOverlay, m_SpriteSelectArea, m_SpriteSelectAreaOverlay, m_TextColor);
487 :
488 0 : if (m_HideScrollBar)
489 0 : m_ScrollBar.Set(old, false);
490 : }
491 0 : m_pGUI.DrawSprite(spriteOverlay, canvas, GetActualSize(), m_VisibleArea);
492 0 : }
493 :
494 : // When a dropdown list is opened, it needs to be visible above all the other
495 : // controls on the page. The only way I can think of to do this is to increase
496 : // its z value when opened, so that it's probably on top.
497 0 : float CDropDown::GetBufferedZ() const
498 : {
499 0 : float bz = CList::GetBufferedZ();
500 0 : if (m_Open)
501 0 : return std::min(bz + 500.f, 1000.f); // TODO - don't use magic number for max z value
502 : else
503 : return bz;
504 : }
505 :
506 0 : CSize2D CDropDown::GetPreferredHeaderTextSize() const
507 : {
508 0 : if (m_Selected == -1)
509 0 : return CSize2D{0.0f, 0.0f};
510 :
511 0 : return CGUIText{m_pGUI, m_List->m_Items[m_Selected], m_Font, std::numeric_limits<float>::max(), m_BufferZone, m_TextAlign, this}.GetSize();
512 : }
|