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