LCOV - code coverage report
Current view: top level - source/gui/ObjectTypes - CList.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 267 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 18 0.0 %

          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 "CList.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/CGUIScrollBarVertical.h"
      24             : #include "gui/CGUIText.h"
      25             : #include "gui/IGUIScrollBar.h"
      26             : #include "gui/SGUIMessage.h"
      27             : #include "gui/SettingTypes/CGUIColor.h"
      28             : #include "gui/SettingTypes/CGUIList.h"
      29             : #include "gui/SettingTypes/CGUIString.h"
      30             : #include "lib/external_libraries/libsdl.h"
      31             : #include "lib/timer.h"
      32             : #include "maths/Size2D.h"
      33             : #include "maths/Vector2D.h"
      34             : #include "ps/XMB/XMBData.h"
      35             : 
      36             : #include <SDL_events.h>
      37             : #include <SDL_keycode.h>
      38             : #include <cstddef>
      39             : #include <memory>
      40             : #include <string>
      41             : #include <utility>
      42             : 
      43             : class CCanvas2D;
      44             : 
      45             : const CStr CList::EventNameSelectionChange = "SelectionChange";
      46             : const CStr CList::EventNameHoverChange = "HoverChange";
      47             : const CStr CList::EventNameMouseLeftClickItem = "MouseLeftClickItem";
      48             : const CStr CList::EventNameMouseLeftDoubleClickItem = "MouseLeftDoubleClickItem";
      49             : 
      50           0 : CList::CList(CGUI& pGUI)
      51             :     : IGUIObject(pGUI),
      52             :       IGUITextOwner(*static_cast<IGUIObject*>(this)),
      53             :       IGUIScrollBarOwner(*static_cast<IGUIObject*>(this)),
      54           0 :       m_Modified(false),
      55           0 :       m_PrevSelectedItem(-1),
      56           0 :       m_LastItemClickTime(0),
      57           0 :       m_BufferZone(this, "buffer_zone"),
      58           0 :       m_Font(this, "font"),
      59           0 :       m_ScrollBar(this, "scrollbar", false),
      60           0 :       m_ScrollBarStyle(this, "scrollbar_style"),
      61           0 :       m_ScrollBottom(this, "scroll_bottom", false),
      62           0 :       m_SoundDisabled(this, "sound_disabled"),
      63           0 :       m_SoundSelected(this, "sound_selected"),
      64           0 :       m_Sprite(this, "sprite"),
      65           0 :       m_SpriteOverlay(this, "sprite_overlay"),
      66             :       // Add sprite_disabled! TODO
      67           0 :       m_SpriteSelectArea(this, "sprite_selectarea"),
      68           0 :       m_SpriteSelectAreaOverlay(this, "sprite_selectarea_overlay"),
      69           0 :       m_TextColor(this, "textcolor"),
      70           0 :       m_TextColorSelected(this, "textcolor_selected"),
      71           0 :       m_Selected(this, "selected", -1), // Index selected. -1 is none.
      72           0 :       m_AutoScroll(this, "auto_scroll", false),
      73           0 :       m_Hovered(this, "hovered", -1),
      74             :       // Each list item has both a name (in 'list') and an associated data string (in 'list_data')
      75           0 :       m_List(this, "list"),
      76           0 :       m_ListData(this, "list_data")
      77             : {
      78             :     // Add scroll-bar
      79           0 :     auto bar = std::make_unique<CGUIScrollBarVertical>(pGUI);
      80           0 :     bar->SetRightAligned(true);
      81           0 :     AddScrollBar(std::move(bar));
      82           0 : }
      83             : 
      84           0 : void CList::SetupText()
      85             : {
      86           0 :     SetupText(false);
      87           0 : }
      88             : 
      89           0 : void CList::SetupText(bool append)
      90             : {
      91           0 :     m_Modified = true;
      92             : 
      93           0 :     if (!append)
      94             :         // Delete all generated texts.
      95             :         // TODO: try to be cleverer if we want to update items before the end.
      96           0 :         m_GeneratedTexts.clear();
      97             : 
      98           0 :     float width = GetListRect().GetWidth();
      99             : 
     100           0 :     bool bottom = false;
     101           0 :     if (m_ScrollBar && GetScrollBar(0).GetStyle())
     102             :     {
     103           0 :         if (m_ScrollBottom && GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f)
     104             :             bottom = true;
     105             : 
     106             :         // remove scrollbar if applicable
     107           0 :         width -= GetScrollBar(0).GetStyle()->m_Width;
     108             :     }
     109             : 
     110             :     // Generate texts
     111           0 :     float buffered_y = 0.f;
     112             : 
     113           0 :     if (append && !m_ItemsYPositions.empty())
     114           0 :         buffered_y = m_ItemsYPositions.back();
     115             : 
     116           0 :     m_ItemsYPositions.resize(m_List->m_Items.size() + 1);
     117             : 
     118           0 :     for (size_t i = append ? m_List->m_Items.size() - 1 : 0; i < m_List->m_Items.size(); ++i)
     119             :     {
     120           0 :         CGUIText* text;
     121             : 
     122           0 :         if (!m_List->m_Items[i].GetOriginalString().empty())
     123           0 :             text = &AddText(m_List->m_Items[i], m_Font, width, m_BufferZone);
     124             :         else
     125             :         {
     126             :             // Minimum height of a space character of the current font size
     127           0 :             CGUIString align_string;
     128           0 :             align_string.SetValue(L" ");
     129           0 :             text = &AddText(align_string, m_Font, width, m_BufferZone);
     130           0 :         }
     131             : 
     132           0 :         m_ItemsYPositions[i] = buffered_y;
     133           0 :         buffered_y += text->GetSize().Height;
     134             :     }
     135             : 
     136           0 :     m_ItemsYPositions[m_List->m_Items.size()] = buffered_y;
     137             : 
     138             :     // Setup scrollbar
     139           0 :     if (m_ScrollBar)
     140             :     {
     141           0 :         GetScrollBar(0).SetScrollRange(m_ItemsYPositions.back());
     142           0 :         GetScrollBar(0).SetScrollSpace(GetListRect().GetHeight());
     143             : 
     144           0 :         CRect rect = GetListRect();
     145           0 :         GetScrollBar(0).SetX(rect.right);
     146           0 :         GetScrollBar(0).SetY(rect.top);
     147           0 :         GetScrollBar(0).SetZ(GetBufferedZ());
     148           0 :         GetScrollBar(0).SetLength(rect.bottom - rect.top);
     149             : 
     150           0 :         if (bottom)
     151           0 :             GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
     152             :     }
     153           0 : }
     154             : 
     155           0 : void CList::ResetStates()
     156             : {
     157           0 :     IGUIObject::ResetStates();
     158           0 :     IGUIScrollBarOwner::ResetStates();
     159           0 : }
     160             : 
     161           0 : void CList::HandleSizeChanged()
     162             : {
     163           0 :     IGUIObject::HandleSizeChanged();
     164           0 :     IGUITextOwner::HandleSizeChanged();
     165           0 : }
     166             : 
     167           0 : void CList::HandleMessage(SGUIMessage& Message)
     168             : {
     169           0 :     IGUIObject::HandleMessage(Message);
     170           0 :     if (m_ScrollBar)
     171           0 :         IGUIScrollBarOwner::HandleMessage(Message);
     172             :     //IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
     173             : 
     174           0 :     m_Modified = false;
     175           0 :     switch (Message.type)
     176             :     {
     177           0 :     case GUIM_SETTINGS_UPDATED:
     178           0 :         if (Message.value == "list")
     179           0 :             SetupText();
     180             : 
     181             :         // If selected is changed, call "SelectionChange"
     182           0 :         if (Message.value == "selected")
     183             :         {
     184             :             // TODO: Check range
     185             : 
     186           0 :             if (m_AutoScroll)
     187           0 :                 UpdateAutoScroll();
     188             : 
     189           0 :             ScriptEvent(EventNameSelectionChange);
     190             :         }
     191             : 
     192           0 :         if (Message.value == "scrollbar")
     193           0 :             SetupText();
     194             : 
     195             :         // Update scrollbar
     196           0 :         if (Message.value == "scrollbar_style")
     197             :         {
     198           0 :             GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
     199           0 :             SetupText();
     200             :         }
     201             : 
     202             :         break;
     203             : 
     204           0 :     case GUIM_MOUSE_PRESS_LEFT:
     205           0 :     {
     206           0 :         if (!m_Enabled)
     207             :         {
     208           0 :             PlaySound(m_SoundDisabled);
     209           0 :             break;
     210             :         }
     211             : 
     212           0 :         int hovered = GetHoveredItem();
     213           0 :         if (hovered == -1)
     214             :             break;
     215           0 :         m_Selected.Set(hovered, true);
     216           0 :         UpdateAutoScroll();
     217           0 :         PlaySound(m_SoundSelected);
     218             : 
     219           0 :         if (timer_Time() - m_LastItemClickTime < SELECT_DBLCLICK_RATE && hovered == m_PrevSelectedItem)
     220           0 :             this->SendMouseEvent(GUIM_MOUSE_DBLCLICK_LEFT_ITEM, EventNameMouseLeftDoubleClickItem);
     221             :         else
     222           0 :             this->SendMouseEvent(GUIM_MOUSE_PRESS_LEFT_ITEM, EventNameMouseLeftClickItem);
     223             : 
     224           0 :         m_LastItemClickTime = timer_Time();
     225           0 :         m_PrevSelectedItem = hovered;
     226           0 :         break;
     227             :     }
     228             : 
     229           0 :     case GUIM_MOUSE_LEAVE:
     230           0 :     {
     231           0 :         if (m_Hovered == -1)
     232             :             break;
     233             : 
     234           0 :         m_Hovered.Set(-1, true);
     235           0 :         ScriptEvent(EventNameHoverChange);
     236           0 :         break;
     237             :     }
     238             : 
     239           0 :     case GUIM_MOUSE_OVER:
     240           0 :     {
     241           0 :         int hovered = GetHoveredItem();
     242           0 :         if (hovered == m_Hovered)
     243             :             break;
     244             : 
     245           0 :         m_Hovered.Set(hovered, true);
     246           0 :         ScriptEvent(EventNameHoverChange);
     247           0 :         break;
     248             :     }
     249             : 
     250           0 :     case GUIM_LOAD:
     251           0 :     {
     252           0 :         GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
     253           0 :         break;
     254             :     }
     255             : 
     256             :     default:
     257             :         break;
     258             :     }
     259             : 
     260           0 :     IGUITextOwner::HandleMessage(Message);
     261           0 : }
     262             : 
     263           0 : Input::Reaction CList::ManuallyHandleKeys(const SDL_Event& ev)
     264             : {
     265           0 :     Input::Reaction result{Input::Reaction::PASS};
     266             : 
     267           0 :     if (ev.type == SDL_KEYDOWN)
     268             :     {
     269           0 :         int szChar = ev.key.keysym.sym;
     270             : 
     271           0 :         switch (szChar)
     272             :         {
     273           0 :         case SDLK_HOME:
     274           0 :             SelectFirstElement();
     275           0 :             UpdateAutoScroll();
     276           0 :             result = Input::Reaction::HANDLED;
     277           0 :             break;
     278             : 
     279           0 :         case SDLK_END:
     280           0 :             SelectLastElement();
     281           0 :             UpdateAutoScroll();
     282           0 :             result = Input::Reaction::HANDLED;
     283           0 :             break;
     284             : 
     285           0 :         case SDLK_UP:
     286           0 :             SelectPrevElement();
     287           0 :             UpdateAutoScroll();
     288           0 :             result = Input::Reaction::HANDLED;
     289           0 :             break;
     290             : 
     291           0 :         case SDLK_DOWN:
     292           0 :             SelectNextElement();
     293           0 :             UpdateAutoScroll();
     294           0 :             result = Input::Reaction::HANDLED;
     295           0 :             break;
     296             : 
     297           0 :         case SDLK_PAGEUP:
     298           0 :             GetScrollBar(0).ScrollMinusPlenty();
     299           0 :             result = Input::Reaction::HANDLED;
     300           0 :             break;
     301             : 
     302           0 :         case SDLK_PAGEDOWN:
     303           0 :             GetScrollBar(0).ScrollPlusPlenty();
     304           0 :             result = Input::Reaction::HANDLED;
     305           0 :             break;
     306             : 
     307             :         default: // Do nothing
     308             :             result = Input::Reaction::PASS;
     309             :         }
     310             :     }
     311             : 
     312           0 :     return result;
     313             : }
     314             : 
     315           0 : void CList::Draw(CCanvas2D& canvas)
     316             : {
     317           0 :     DrawList(canvas, m_Selected, m_Sprite, m_SpriteOverlay, m_SpriteSelectArea, m_SpriteSelectAreaOverlay, m_TextColor);
     318           0 : }
     319             : 
     320           0 : void CList::DrawList(CCanvas2D& canvas, const int& selected, const CGUISpriteInstance& sprite, const CGUISpriteInstance& spriteOverlay,
     321             :                      const CGUISpriteInstance& spriteSelectArea, const CGUISpriteInstance& spriteSelectAreaOverlay, const CGUIColor& textColor)
     322             : {
     323           0 :     CRect rect = GetListRect();
     324             : 
     325           0 :     m_pGUI.DrawSprite(sprite, canvas, rect);
     326             : 
     327           0 :     float scroll = 0.f;
     328           0 :     if (m_ScrollBar)
     329           0 :         scroll = GetScrollBar(0).GetPos();
     330             : 
     331           0 :     bool drawSelected = false;
     332           0 :     CRect rectSel;
     333           0 :     if (selected >= 0 && selected+1 < (int)m_ItemsYPositions.size())
     334             :     {
     335             :         // Get rectangle of selection:
     336           0 :         rectSel = CRect(
     337           0 :             rect.left, rect.top + m_ItemsYPositions[selected] - scroll,
     338           0 :             rect.right, rect.top + m_ItemsYPositions[selected+1] - scroll);
     339             : 
     340           0 :         if (rectSel.top <= rect.bottom &&
     341           0 :             rectSel.bottom >= rect.top)
     342             :         {
     343           0 :             if (rectSel.bottom > rect.bottom)
     344           0 :                 rectSel.bottom = rect.bottom;
     345           0 :             if (rectSel.top < rect.top)
     346           0 :                 rectSel.top = rect.top;
     347           0 :             if (m_ScrollBar)
     348             :             {
     349             :                 // Remove any overlapping area of the scrollbar.
     350           0 :                 if (rectSel.right > GetScrollBar(0).GetOuterRect().left &&
     351           0 :                     rectSel.right <= GetScrollBar(0).GetOuterRect().right)
     352           0 :                     rectSel.right = GetScrollBar(0).GetOuterRect().left;
     353             : 
     354           0 :                 if (rectSel.left >= GetScrollBar(0).GetOuterRect().left &&
     355           0 :                     rectSel.left < GetScrollBar(0).GetOuterRect().right)
     356           0 :                     rectSel.left = GetScrollBar(0).GetOuterRect().right;
     357             :             }
     358             : 
     359           0 :             m_pGUI.DrawSprite(spriteSelectArea, canvas, rectSel);
     360           0 :             drawSelected = true;
     361             :         }
     362             :     }
     363             : 
     364           0 :     for (size_t i = 0; i < m_List->m_Items.size(); ++i)
     365             :     {
     366           0 :         if (m_ItemsYPositions[i+1] - scroll < 0 ||
     367           0 :             m_ItemsYPositions[i] - scroll > rect.GetHeight())
     368           0 :             continue;
     369             : 
     370             :         // Clipping area (we'll have to substract the scrollbar)
     371           0 :         CRect cliparea = GetListRect();
     372             : 
     373           0 :         if (m_ScrollBar)
     374             :         {
     375           0 :             if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
     376           0 :                 cliparea.right <= GetScrollBar(0).GetOuterRect().right)
     377           0 :                 cliparea.right = GetScrollBar(0).GetOuterRect().left;
     378             : 
     379           0 :             if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
     380           0 :                 cliparea.left < GetScrollBar(0).GetOuterRect().right)
     381           0 :                 cliparea.left = GetScrollBar(0).GetOuterRect().right;
     382             :         }
     383             : 
     384           0 :         const CGUIColor& finalTextColor = (drawSelected && static_cast<size_t>(selected) == i && *m_TextColorSelected) ? m_TextColorSelected : textColor;
     385             : 
     386           0 :         DrawText(canvas, i, finalTextColor, rect.TopLeft() - CVector2D(0.f, scroll - m_ItemsYPositions[i]), cliparea);
     387             :     }
     388             : 
     389             :     // Draw scrollbars on top of the content
     390           0 :     if (m_ScrollBar)
     391           0 :         IGUIScrollBarOwner::Draw(canvas);
     392             : 
     393             :     // Draw the overlays last
     394           0 :     m_pGUI.DrawSprite(spriteOverlay, canvas, rect);
     395           0 :     if (drawSelected)
     396           0 :         m_pGUI.DrawSprite(spriteSelectAreaOverlay, canvas, rectSel);
     397           0 : }
     398             : 
     399           0 : void CList::AddItem(const CGUIString& str, const CGUIString& data)
     400             : {
     401             :     // Do not send a settings-changed message
     402           0 :     m_List.GetMutable().m_Items.push_back(str);
     403           0 :     m_ListData.GetMutable().m_Items.push_back(data);
     404             : 
     405           0 :     SetupText(true);
     406           0 : }
     407             : 
     408           0 : void CList::AddItem(const CGUIString& strAndData)
     409             : {
     410           0 :     AddItem(strAndData, strAndData);
     411           0 : }
     412             : 
     413           0 : bool CList::HandleAdditionalChildren(const XMBData& xmb, const XMBElement& child)
     414             : {
     415           0 :     int elmt_item = xmb.GetElementID("item");
     416             : 
     417           0 :     if (child.GetNodeName() == elmt_item)
     418             :     {
     419           0 :         CGUIString vlist;
     420           0 :         vlist.SetValue(child.GetText().FromUTF8());
     421           0 :         AddItem(vlist, vlist);
     422           0 :         return true;
     423           0 :     }
     424             : 
     425             :     return false;
     426             : }
     427             : 
     428           0 : void CList::SelectNextElement()
     429             : {
     430           0 :     if (m_Selected != static_cast<int>(m_List->m_Items.size()) - 1)
     431             :     {
     432           0 :         m_Selected.Set(m_Selected + 1, true);
     433           0 :         PlaySound(m_SoundSelected);
     434             :     }
     435           0 : }
     436             : 
     437           0 : void CList::SelectPrevElement()
     438             : {
     439           0 :     if (m_Selected > 0)
     440             :     {
     441           0 :         m_Selected.Set(m_Selected - 1, true);
     442           0 :         PlaySound(m_SoundSelected);
     443             :     }
     444           0 : }
     445             : 
     446           0 : void CList::SelectFirstElement()
     447             : {
     448           0 :     if (m_Selected >= 0)
     449           0 :         m_Selected.Set(0, true);
     450           0 : }
     451             : 
     452           0 : void CList::SelectLastElement()
     453             : {
     454           0 :     const int index = static_cast<int>(m_List->m_Items.size()) - 1;
     455             : 
     456           0 :     if (m_Selected != index)
     457           0 :         m_Selected.Set(index, true);
     458           0 : }
     459             : 
     460           0 : void CList::UpdateAutoScroll()
     461             : {
     462             :     // No scrollbar, no scrolling (at least it's not made to work properly).
     463           0 :     if (!m_ScrollBar || m_Selected < 0 || static_cast<std::size_t>(m_Selected) >= m_ItemsYPositions.size())
     464           0 :         return;
     465             : 
     466           0 :     float scroll = GetScrollBar(0).GetPos();
     467             : 
     468             :     // Check upper boundary
     469           0 :     if (m_ItemsYPositions[m_Selected] < scroll)
     470             :     {
     471           0 :         GetScrollBar(0).SetPos(m_ItemsYPositions[m_Selected]);
     472           0 :         return; // this means, if it wants to align both up and down at the same time
     473             :                 //  this will have precedence.
     474             :     }
     475             : 
     476             :     // Check lower boundary
     477           0 :     CRect rect = GetListRect();
     478           0 :     if (m_ItemsYPositions[m_Selected+1]-rect.GetHeight() > scroll)
     479           0 :         GetScrollBar(0).SetPos(m_ItemsYPositions[m_Selected+1]-rect.GetHeight());
     480             : }
     481             : 
     482           0 : int CList::GetHoveredItem()
     483             : {
     484           0 :     const float scroll = m_ScrollBar ? GetScrollBar(0).GetPos() : 0.f;
     485             : 
     486           0 :     const CRect& rect = GetListRect();
     487           0 :     CVector2D mouse = m_pGUI.GetMousePos();
     488           0 :     mouse.Y += scroll;
     489             : 
     490             :     // Mouse is over scrollbar
     491           0 :     if (m_ScrollBar && GetScrollBar(0).IsVisible() &&
     492           0 :         mouse.X >= GetScrollBar(0).GetOuterRect().left &&
     493           0 :         mouse.X <= GetScrollBar(0).GetOuterRect().right)
     494           0 :         return -1;
     495             : 
     496           0 :     for (size_t i = 0; i < m_List->m_Items.size(); ++i)
     497           0 :         if (mouse.Y >= rect.top + m_ItemsYPositions[i] &&
     498           0 :             mouse.Y < rect.top + m_ItemsYPositions[i + 1])
     499           0 :             return i;
     500             : 
     501             :     return -1;
     502             : }

Generated by: LCOV version 1.16