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