Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CList.h
Go to the documentation of this file.
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#ifndef INCLUDED_CLIST
19#define INCLUDED_CLIST
20
21#include "gui/CGUISetting.h"
22#include "gui/CGUISprite.h"
28#include "maths/Rect.h"
29#include "ps/CStr.h"
30
31#include <vector>
32
33class CCanvas2D;
34class CGUI;
35class CGUIString;
36union SDL_Event;
37
38/**
39 * Create a list of elements, where one can be selected
40 * by the user. The control will use a pre-processed
41 * text-object for each element, which will be managed
42 * by the IGUITextOwner structure.
43 *
44 * A scroll-bar will appear when needed. This will be
45 * achieved with the IGUIScrollBarOwner structure.
46 */
47class CList : public IGUIObject, public IGUIScrollBarOwner, public IGUITextOwner
48{
50public:
51 CList(CGUI& pGUI);
52
53 /**
54 * @see IGUIObject#ResetStates()
55 */
56 virtual void ResetStates();
57
58 /**
59 * @see IGUIObject#HandleSizeChanged()
60 */
61 virtual void HandleSizeChanged();
62
63 /**
64 * Adds an item last to the list.
65 */
66 virtual void AddItem(const CGUIString& str, const CGUIString& data);
67
68 /**
69 * Add an item where both parameters are identical.
70 */
71 void AddItem(const CGUIString& strAndData);
72
73protected:
74 /**
75 * Sets up text, should be called every time changes has been
76 * made that can change the visual.
77 * @param append - if true, we assume we only need to render the new element at the end of the list.
78 */
79 virtual void SetupText();
80 virtual void SetupText(bool append);
81
82 /**
83 * @see IGUIObject#HandleMessage()
84 */
85 virtual void HandleMessage(SGUIMessage& Message);
86
87 /**
88 * Handle events manually to catch keyboard inputting.
89 */
90 virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev);
91
92 /**
93 * Draws the List box
94 */
95 virtual void Draw(CCanvas2D& canvas);
96
97 virtual void CreateJSObject();
98
99 /**
100 * Easy select elements functions
101 */
102 virtual void SelectNextElement();
103 virtual void SelectPrevElement();
104 virtual void SelectFirstElement();
105 virtual void SelectLastElement();
106
107 /**
108 * Handle the <item> tag.
109 */
110 virtual bool HandleAdditionalChildren(const XMBData& xmb, const XMBElement& child);
111
112 // Called every time the auto-scrolling should be checked.
113 void UpdateAutoScroll();
114
115 // Extended drawing interface, this is so that classes built on the this one
116 // can use other sprite names.
117 virtual void DrawList(CCanvas2D& canvas, const int& selected, const CGUISpriteInstance& sprite, const CGUISpriteInstance& spriteOverlay,
118 const CGUISpriteInstance& spriteSelectArea, const CGUISpriteInstance& spriteSelectAreaOverlay, const CGUIColor& textColor);
119
120 // Get the area of the list. This is so that it can easily be changed, like in CDropDown
121 // where the area is not equal to GetActualSize().
122 virtual CRect GetListRect() const { return GetActualSize(); }
123
124 // Returns whether SetupText() has run since the last message was received
125 // (and thus whether list items have possibly changed).
126 virtual bool GetModified() const { return m_Modified; }
127
128 /**
129 * List of each element's relative y position. Will be
130 * one larger than m_Items, because it will end with the
131 * bottom of the last element. First element will always
132 * be zero, but still stored for easy handling.
133 */
134 std::vector<float> m_ItemsYPositions;
135
136 virtual int GetHoveredItem();
137
156
157private:
158 static const CStr EventNameSelectionChange;
159 static const CStr EventNameHoverChange;
162
163 // Whether the list's items have been modified since last handling a message.
165
166 // Used for doubleclick registration
168
169 // Last time a click on an item was issued
171};
172
173#endif // INCLUDED_CLIST
#define GUI_OBJECT(obj)
Definition: IGUIObject.h:53
Definition: Canvas2D.h:36
Definition: CGUISprite.h:137
String class, substitute for CStr, but that parses the tags and builds up a list of all text that wil...
Definition: CGUIString.h:47
The main object that represents a whole GUI page.
Definition: CGUI.h:80
Create a list of elements, where one can be selected by the user.
Definition: CList.h:48
CGUISimpleSetting< CGUISpriteInstance > m_SpriteSelectAreaOverlay
Definition: CList.h:148
CGUISimpleSetting< CStr > m_ScrollBarStyle
Definition: CList.h:141
virtual int GetHoveredItem()
Definition: CList.cpp:482
virtual void SelectLastElement()
Definition: CList.cpp:452
CGUISimpleSetting< bool > m_ScrollBottom
Definition: CList.h:142
CGUISimpleSetting< CGUIColor > m_TextColor
Definition: CList.h:149
CGUISimpleSetting< CGUISpriteInstance > m_Sprite
Definition: CList.h:145
virtual void SelectPrevElement()
Definition: CList.cpp:437
virtual void CreateJSObject()
Creates the JS object representing this page upon first use.
CGUISimpleSetting< bool > m_ScrollBar
Definition: CList.h:140
CGUISimpleSetting< std::int32_t > m_Selected
Definition: CList.h:151
CGUISimpleSetting< CStrW > m_SoundSelected
Definition: CList.h:144
virtual void DrawList(CCanvas2D &canvas, const int &selected, const CGUISpriteInstance &sprite, const CGUISpriteInstance &spriteOverlay, const CGUISpriteInstance &spriteSelectArea, const CGUISpriteInstance &spriteSelectAreaOverlay, const CGUIColor &textColor)
Definition: CList.cpp:320
static const CStr EventNameHoverChange
Definition: CList.h:159
CGUISimpleSetting< CGUISpriteInstance > m_SpriteOverlay
Definition: CList.h:146
CGUISimpleSetting< CGUIList > m_List
Definition: CList.h:154
CGUISimpleSetting< bool > m_AutoScroll
Definition: CList.h:152
virtual Input::Reaction ManuallyHandleKeys(const SDL_Event &ev)
Handle events manually to catch keyboard inputting.
Definition: CList.cpp:263
std::vector< float > m_ItemsYPositions
List of each element's relative y position.
Definition: CList.h:134
static const CStr EventNameMouseLeftDoubleClickItem
Definition: CList.h:161
CGUISimpleSetting< std::int32_t > m_Hovered
Definition: CList.h:153
void UpdateAutoScroll()
Definition: CList.cpp:460
int m_PrevSelectedItem
Definition: CList.h:167
CGUISimpleSetting< CStrW > m_SoundDisabled
Definition: CList.h:143
static const CStr EventNameMouseLeftClickItem
Definition: CList.h:160
CGUISimpleSetting< CGUISpriteInstance > m_SpriteSelectArea
Definition: CList.h:147
virtual void ResetStates()
Definition: CList.cpp:155
virtual void HandleMessage(SGUIMessage &Message)
Definition: CList.cpp:167
virtual void SelectFirstElement()
Definition: CList.cpp:446
CGUISimpleSetting< CGUIList > m_ListData
Definition: CList.h:155
virtual void SelectNextElement()
Easy select elements functions.
Definition: CList.cpp:428
static const CStr EventNameSelectionChange
Definition: CList.h:158
virtual bool GetModified() const
Definition: CList.h:126
virtual void HandleSizeChanged()
Definition: CList.cpp:161
bool m_Modified
Definition: CList.h:164
CGUISimpleSetting< float > m_BufferZone
Definition: CList.h:138
CGUISimpleSetting< CStrW > m_Font
Definition: CList.h:139
double m_LastItemClickTime
Definition: CList.h:170
virtual void Draw(CCanvas2D &canvas)
Draws the List box.
Definition: CList.cpp:315
CGUISimpleSetting< CGUIColor > m_TextColorSelected
Definition: CList.h:150
virtual CRect GetListRect() const
Definition: CList.h:122
virtual bool HandleAdditionalChildren(const XMBData &xmb, const XMBElement &child)
Handle the <item> tag.
Definition: CList.cpp:413
CList(CGUI &pGUI)
Definition: CList.cpp:50
virtual void SetupText()
Sets up text, should be called every time changes has been made that can change the visual.
Definition: CList.cpp:84
virtual void AddItem(const CGUIString &str, const CGUIString &data)
Adds an item last to the list.
Definition: CList.cpp:399
Rectangle class used for screen rectangles.
Definition: Rect.h:31
GUI object such as a button or an input-box.
Definition: IGUIObject.h:63
virtual const CRect & GetActualSize() const
Get the size of the object.
Definition: IGUIObject.cpp:252
Base-class this if you want an object to contain one, or several, scroll-bars.
Definition: IGUIScrollBarOwner.h:38
Framework for handling Output text.
Definition: IGUITextOwner.h:55
Definition: XMBData.h:95
Definition: XMBData.h:135
Reaction
Definition: Input.h:39
Same as the CColor class, but this one can also parse colors predefined in the GUI page (such as "yel...
Definition: CGUIColor.h:31
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: SGUIMessage.h:73