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 : /* 19 : GUI Object - Drop Down (list) 20 : 21 : --Overview-- 22 : 23 : Works just like a list-box, but it hides 24 : all the elements that aren't selected. They 25 : can be brought up by pressing the control. 26 : */ 27 : 28 : #ifndef INCLUDED_CDROPDOWN 29 : #define INCLUDED_CDROPDOWN 30 : 31 : #include "gui/CGUISetting.h" 32 : #include "gui/CGUISprite.h" 33 : #include "gui/ObjectBases/IGUIObject.h" 34 : #include "gui/ObjectTypes/CList.h" 35 : #include "gui/SettingTypes/CGUIColor.h" 36 : #include "maths/Rect.h" 37 : #include "maths/Vector2D.h" 38 : #include "ps/CStr.h" 39 : #include "ps/Input.h" 40 : 41 : #include <string> 42 : 43 : class CGUI; 44 : union SDL_Event; 45 : 46 : /** 47 : * Drop Down 48 : * 49 : * The control can be pressed, but we will not inherent 50 : * this behavior from IGUIButtonBehavior, because when 51 : * you press this control, the list with elements will 52 : * immediately appear, and not first after release 53 : * (which is the whole gist of the IGUIButtonBehavior). 54 : */ 55 : class CDropDown : public CList 56 : { 57 0 : GUI_OBJECT(CDropDown) 58 : 59 : public: 60 : CDropDown(CGUI& pGUI); 61 : 62 : /** 63 : * @see IGUIObject#HandleMessage() 64 : */ 65 : virtual void HandleMessage(SGUIMessage& Message); 66 : 67 : /** 68 : * Handle events manually to catch keyboard inputting. 69 : */ 70 : virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev); 71 : 72 : /** 73 : * Draws the Button 74 : */ 75 : virtual void Draw(CCanvas2D& canvas); 76 : 77 : virtual void CreateJSObject(); 78 : 79 : // This is one of the few classes we actually need to redefine this function 80 : // this is because the size of the control changes whether it is open 81 : // or closed. 82 : virtual bool IsMouseOver() const; 83 : 84 : virtual float GetBufferedZ() const; 85 : 86 : /** 87 : * Calculate the preferred text size of the currently selected item displayed in the header. 88 : */ 89 : virtual CSize2D GetPreferredHeaderTextSize() const; 90 : 91 : protected: 92 : /** 93 : * If the size changed, the texts have to be updated as 94 : * the word wrapping depends on the size. 95 : */ 96 : virtual void HandleSizeChanged(); 97 : 98 : /** 99 : * Sets up text, should be called every time changes has been 100 : * made that can change the visual. 101 : */ 102 : void SetupText(); 103 : 104 : // Sets up the cached GetListRect. Decided whether it should 105 : // have a scrollbar, and so on. 106 : virtual void SetupListRect(); 107 : 108 : // Specify a new List rectangle. 109 : virtual CRect GetListRect() const; 110 : 111 : /** 112 : * Placement of text. 113 : */ 114 : CVector2D m_TextPos; 115 : 116 : // Is the dropdown opened? 117 : bool m_Open; 118 : 119 : // I didn't cache this at first, but it's just as easy as caching 120 : // GetActualSize(), so I thought, what the heck it's used a lot. 121 : CRect m_CachedListRect; 122 : 123 : // Hide scrollbar when it's not needed 124 : bool m_HideScrollBar; 125 : 126 : // Not necessarily the element that is selected, this is just 127 : // which element should be highlighted. When opening the dropdown 128 : // it is set to "selected", but then when moving the mouse it will 129 : // change. 130 : int m_ElementHighlight; 131 : 132 : // Stores any text entered by the user for quick access to an element 133 : // (ie if you type "acro" it will take you to acropolis). 134 : std::string m_InputBuffer; 135 : 136 : // used to know if we want to restart anew or add to m_inputbuffer. 137 : double m_TimeOfLastInput; 138 : 139 : // Settings 140 : CGUISimpleSetting<float> m_ButtonWidth; 141 : CGUISimpleSetting<float> m_DropDownSize; 142 : CGUISimpleSetting<float> m_DropDownBuffer; 143 : CGUISimpleSetting<std::uint32_t> m_MinimumVisibleItems; 144 : CGUISimpleSetting<CStrW> m_SoundClosed; 145 : CGUISimpleSetting<CStrW> m_SoundEnter; 146 : CGUISimpleSetting<CStrW> m_SoundLeave; 147 : CGUISimpleSetting<CStrW> m_SoundOpened; 148 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteDisabled; 149 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteOverlayDisabled; 150 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteList; 151 : CGUISimpleSetting<CGUISpriteInstance> m_SpriteListOverlay; 152 : CGUISimpleSetting<CGUISpriteInstance> m_Sprite2; 153 : CGUISimpleSetting<CGUISpriteInstance> m_Sprite2Over; 154 : CGUISimpleSetting<CGUISpriteInstance> m_Sprite2Pressed; 155 : CGUISimpleSetting<CGUISpriteInstance> m_Sprite2Disabled; 156 : CGUISimpleSetting<CGUIColor> m_TextColorDisabled; 157 : }; 158 : 159 : #endif // INCLUDED_CDROPDOWN