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 : #ifndef INCLUDED_CLIST 19 : #define INCLUDED_CLIST 20 : 21 : #include "gui/CGUISetting.h" 22 : #include "gui/CGUISprite.h" 23 : #include "gui/ObjectBases/IGUIObject.h" 24 : #include "gui/ObjectBases/IGUIScrollBarOwner.h" 25 : #include "gui/ObjectBases/IGUITextOwner.h" 26 : #include "gui/SettingTypes/CGUIColor.h" 27 : #include "gui/SettingTypes/CGUIList.h" 28 : #include "maths/Rect.h" 29 : #include "ps/CStr.h" 30 : 31 : #include <vector> 32 : 33 : class CCanvas2D; 34 : class CGUI; 35 : class CGUIString; 36 : union 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 : */ 47 : class CList : public IGUIObject, public IGUIScrollBarOwner, public IGUITextOwner 48 : { 49 0 : GUI_OBJECT(CList) 50 : public: 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 : 73 : protected: 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 0 : 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 0 : 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 : 138 : CGUISimpleSetting<float> m_BufferZone; 139 : CGUISimpleSetting<CStrW> m_Font; 140 : CGUISimpleSetting<bool> m_ScrollBar; 141 : CGUISimpleSetting<CStr> m_ScrollBarStyle; 142 : CGUISimpleSetting<bool> m_ScrollBottom; 143 : CGUISimpleSetting<CStrW> m_SoundDisabled; 144 : CGUISimpleSetting<CStrW> m_SoundSelected; 145 : CGUISimpleSetting<CGUISpriteInstance> m_Sprite; 146 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteOverlay; 147 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteSelectArea; 148 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteSelectAreaOverlay; 149 : CGUISimpleSetting<CGUIColor> m_TextColor; 150 : CGUISimpleSetting<CGUIColor> m_TextColorSelected; 151 : CGUISimpleSetting<std::int32_t> m_Selected; 152 : CGUISimpleSetting<bool> m_AutoScroll; 153 : CGUISimpleSetting<std::int32_t> m_Hovered; 154 : CGUISimpleSetting<CGUIList> m_List; 155 : CGUISimpleSetting<CGUIList> m_ListData; 156 : 157 : private: 158 : static const CStr EventNameSelectionChange; 159 : static const CStr EventNameHoverChange; 160 : static const CStr EventNameMouseLeftClickItem; 161 : static const CStr EventNameMouseLeftDoubleClickItem; 162 : 163 : // Whether the list's items have been modified since last handling a message. 164 : bool m_Modified; 165 : 166 : // Used for doubleclick registration 167 : int m_PrevSelectedItem; 168 : 169 : // Last time a click on an item was issued 170 : double m_LastItemClickTime; 171 : }; 172 : 173 : #endif // INCLUDED_CLIST