]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiButton.h
Revert "Make Lint Happy"
[dragonfireclient.git] / src / gui / guiButton.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #pragma once\r
6 \r
7 #include "IrrCompileConfig.h"\r
8 \r
9 #include <IGUIStaticText.h>\r
10 #include "irrlicht_changes/static_text.h"\r
11 #include "IGUIButton.h"\r
12 #include "IGUISpriteBank.h"\r
13 #include "ITexture.h"\r
14 #include "SColor.h"\r
15 #include "guiSkin.h"\r
16 #include "StyleSpec.h"\r
17 \r
18 using namespace irr;\r
19 \r
20 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)\r
21         namespace irr { namespace gui {\r
22 \r
23                 //! State of buttons used for drawing texture images.\r
24                 //! Note that only a single state is active at a time\r
25                 //! Also when no image is defined for a state it will use images from another state\r
26                 //! and if that state is not set from the replacement for that,etc.\r
27                 //! So in many cases setting EGBIS_IMAGE_UP and EGBIS_IMAGE_DOWN is sufficient.\r
28                 enum EGUI_BUTTON_IMAGE_STATE {\r
29                         //! When no other states have images they will all use this one.\r
30                                         EGBIS_IMAGE_UP,\r
31                         //! When not set EGBIS_IMAGE_UP is used.\r
32                                         EGBIS_IMAGE_UP_MOUSEOVER,\r
33                         //! When not set EGBIS_IMAGE_UP_MOUSEOVER is used.\r
34                                         EGBIS_IMAGE_UP_FOCUSED,\r
35                         //! When not set EGBIS_IMAGE_UP_FOCUSED is used.\r
36                                         EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER,\r
37                         //! When not set EGBIS_IMAGE_UP is used.\r
38                                         EGBIS_IMAGE_DOWN,\r
39                         //! When not set EGBIS_IMAGE_DOWN is used.\r
40                                         EGBIS_IMAGE_DOWN_MOUSEOVER,\r
41                         //! When not set EGBIS_IMAGE_DOWN_MOUSEOVER is used.\r
42                                         EGBIS_IMAGE_DOWN_FOCUSED,\r
43                         //! When not set EGBIS_IMAGE_DOWN_FOCUSED is used.\r
44                                         EGBIS_IMAGE_DOWN_FOCUSED_MOUSEOVER,\r
45                         //! When not set EGBIS_IMAGE_UP or EGBIS_IMAGE_DOWN are used (depending on button state).\r
46                                         EGBIS_IMAGE_DISABLED,\r
47                         //! not used, counts the number of enumerated items\r
48                                         EGBIS_COUNT\r
49                 };\r
50 \r
51                 //! Names for gui button image states\r
52                 const c8 *const GUIButtonImageStateNames[EGBIS_COUNT + 1] =\r
53                                 {\r
54                                                 "Image",    // not "ImageUp" as it otherwise breaks serialization of old files\r
55                                                 "ImageUpOver",\r
56                                                 "ImageUpFocused",\r
57                                                 "ImageUpFocusedOver",\r
58                                                 "PressedImage",    // not "ImageDown" as it otherwise breaks serialization of old files\r
59                                                 "ImageDownOver",\r
60                                                 "ImageDownFocused",\r
61                                                 "ImageDownFocusedOver",\r
62                                                 "ImageDisabled",\r
63                                                 0    // count\r
64                                 };\r
65 \r
66         }}\r
67 \r
68 #endif\r
69 \r
70 class ISimpleTextureSource;\r
71 \r
72 class GUIButton : public gui::IGUIButton\r
73 {\r
74 public:\r
75 \r
76         //! constructor\r
77         GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,\r
78                            s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,\r
79                            bool noclip=false);\r
80 \r
81         //! destructor\r
82         virtual ~GUIButton();\r
83 \r
84         //! called if an event happened.\r
85         virtual bool OnEvent(const SEvent& event) override;\r
86 \r
87         //! draws the element and its children\r
88         virtual void draw() override;\r
89 \r
90         //! sets another skin independent font. if this is set to zero, the button uses the font of the skin.\r
91         virtual void setOverrideFont(gui::IGUIFont* font=0) override;\r
92 \r
93         //! Gets the override font (if any)\r
94         virtual gui::IGUIFont* getOverrideFont() const override;\r
95 \r
96         //! Get the font which is used right now for drawing\r
97         virtual gui::IGUIFont* getActiveFont() const override;\r
98 \r
99         //! Sets another color for the button text.\r
100         virtual void setOverrideColor(video::SColor color);\r
101 \r
102         //! Gets the override color\r
103         virtual video::SColor getOverrideColor(void) const;\r
104 \r
105         //! Sets if the button text should use the override color or the color in the gui skin.\r
106         virtual void enableOverrideColor(bool enable);\r
107 \r
108         //! Checks if an override color is enabled\r
109         virtual bool isOverrideColorEnabled(void) const;\r
110 \r
111         // PATCH\r
112         //! Sets an image which should be displayed on the button when it is in the given state.\r
113         virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,\r
114                         video::ITexture* image=nullptr,\r
115                         const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0));\r
116 \r
117         //! Sets an image which should be displayed on the button when it is in normal state.\r
118         virtual void setImage(video::ITexture* image=nullptr) override;\r
119 \r
120         //! Sets an image which should be displayed on the button when it is in normal state.\r
121         virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
122 \r
123         //! Sets an image which should be displayed on the button when it is in pressed state.\r
124         virtual void setPressedImage(video::ITexture* image=nullptr) override;\r
125 \r
126         //! Sets an image which should be displayed on the button when it is in pressed state.\r
127         virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
128 \r
129         //! Sets the text displayed by the button\r
130         virtual void setText(const wchar_t* text) override;\r
131         // END PATCH\r
132 \r
133         //! Sets the sprite bank used by the button\r
134         virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;\r
135 \r
136         //! Sets the animated sprite for a specific button state\r
137         /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite\r
138         \param state: State of the button to set the sprite for\r
139         \param index: The sprite number from the current sprite bank\r
140         \param color: The color of the sprite\r
141         */\r
142         virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,\r
143                                                    video::SColor color=video::SColor(255,255,255,255),\r
144                                                    bool loop=false, bool scale=false);\r
145 \r
146 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)\r
147         void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop) override {\r
148                 setSprite(state, index, color, loop, false);\r
149         }\r
150 #endif\r
151 \r
152         //! Get the sprite-index for the given state or -1 when no sprite is set\r
153         virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const;\r
154 \r
155         //! Get the sprite color for the given state. Color is only used when a sprite is set.\r
156         virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const;\r
157 \r
158         //! Returns if the sprite in the given state does loop\r
159         virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const;\r
160 \r
161         //! Returns if the sprite in the given state is scaled\r
162         virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const;\r
163 \r
164         //! Sets if the button should behave like a push button. Which means it\r
165         //! can be in two states: Normal or Pressed. With a click on the button,\r
166         //! the user can change the state of the button.\r
167         virtual void setIsPushButton(bool isPushButton=true) override;\r
168 \r
169         //! Checks whether the button is a push button\r
170         virtual bool isPushButton() const override;\r
171 \r
172         //! Sets the pressed state of the button if this is a pushbutton\r
173         virtual void setPressed(bool pressed=true) override;\r
174 \r
175         //! Returns if the button is currently pressed\r
176         virtual bool isPressed() const override;\r
177 \r
178         // PATCH\r
179         //! Returns if this element (or one of its direct children) is hovered\r
180         bool isHovered() const;\r
181         // END PATCH\r
182 \r
183         //! Sets if the button should use the skin to draw its border\r
184         virtual void setDrawBorder(bool border=true) override;\r
185 \r
186         //! Checks if the button face and border are being drawn\r
187         virtual bool isDrawingBorder() const override;\r
188 \r
189         //! Sets if the alpha channel should be used for drawing images on the button (default is false)\r
190         virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;\r
191 \r
192         //! Checks if the alpha channel should be used for drawing images on the button\r
193         virtual bool isAlphaChannelUsed() const override;\r
194 \r
195         //! Sets if the button should scale the button images to fit\r
196         virtual void setScaleImage(bool scaleImage=true) override;\r
197 \r
198         //! Checks whether the button scales the used images\r
199         virtual bool isScalingImage() const override;\r
200 \r
201         //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event\r
202         virtual bool getClickShiftState() const\r
203         {\r
204                 return ClickShiftState;\r
205         }\r
206 \r
207         //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event\r
208         virtual bool getClickControlState() const\r
209         {\r
210                 return ClickControlState;\r
211         }\r
212 \r
213         //! Writes attributes of the element.\r
214         virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const override;\r
215 \r
216         //! Reads attributes of the element\r
217         virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) override;\r
218 \r
219 \r
220 \r
221         void setColor(video::SColor color);\r
222         // PATCH\r
223         //! Set element properties from a StyleSpec corresponding to the button state\r
224         void setFromState();\r
225 \r
226         //! Set element properties from a StyleSpec\r
227         virtual void setFromStyle(const StyleSpec& style);\r
228 \r
229         //! Set the styles used for each state\r
230         void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);\r
231         // END PATCH\r
232 \r
233 \r
234         //! Do not drop returned handle\r
235         static GUIButton* addButton(gui::IGUIEnvironment *environment,\r
236                         const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,\r
237                         IGUIElement* parent, s32 id, const wchar_t* text,\r
238                         const wchar_t *tooltiptext=L"");\r
239 \r
240 protected:\r
241         void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);\r
242         gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;\r
243 \r
244         ISimpleTextureSource *getTextureSource() { return TSrc; }\r
245 \r
246         struct ButtonImage\r
247         {\r
248                 ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
249                 {\r
250                 }\r
251 \r
252                 ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
253                 {\r
254                         *this = other;\r
255                 }\r
256 \r
257                 ~ButtonImage()\r
258                 {\r
259                         if ( Texture )\r
260                                 Texture->drop();\r
261                 }\r
262 \r
263                 ButtonImage& operator=(const ButtonImage& other)\r
264                 {\r
265                         if ( this == &other )\r
266                                 return *this;\r
267 \r
268                         if (other.Texture)\r
269                                 other.Texture->grab();\r
270                         if ( Texture )\r
271                                 Texture->drop();\r
272                         Texture = other.Texture;\r
273                         SourceRect = other.SourceRect;\r
274                         return *this;\r
275                 }\r
276 \r
277                 bool operator==(const ButtonImage& other) const\r
278                 {\r
279                         return Texture == other.Texture && SourceRect == other.SourceRect;\r
280                 }\r
281 \r
282 \r
283                 video::ITexture* Texture;\r
284                 core::rect<s32> SourceRect;\r
285         };\r
286 \r
287         gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;\r
288 \r
289 private:\r
290 \r
291         struct ButtonSprite\r
292         {\r
293                 ButtonSprite() : Index(-1), Loop(false), Scale(false)\r
294                 {\r
295                 }\r
296 \r
297                 bool operator==(const ButtonSprite& other) const\r
298                 {\r
299                         return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;\r
300                 }\r
301 \r
302                 s32 Index;\r
303                 video::SColor Color;\r
304                 bool Loop;\r
305                 bool Scale;\r
306         };\r
307 \r
308         ButtonSprite ButtonSprites[gui::EGBS_COUNT];\r
309         gui::IGUISpriteBank* SpriteBank;\r
310 \r
311         ButtonImage ButtonImages[gui::EGBIS_COUNT];\r
312 \r
313         std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;\r
314 \r
315         gui::IGUIFont* OverrideFont;\r
316 \r
317         bool OverrideColorEnabled;\r
318         video::SColor OverrideColor;\r
319 \r
320         u32 ClickTime, HoverTime, FocusTime;\r
321 \r
322         bool ClickShiftState;\r
323         bool ClickControlState;\r
324 \r
325         bool IsPushButton;\r
326         bool Pressed;\r
327         bool UseAlphaChannel;\r
328         bool DrawBorder;\r
329         bool ScaleImage;\r
330 \r
331         video::SColor Colors[4];\r
332         // PATCH\r
333         bool WasHovered = false;\r
334         ISimpleTextureSource *TSrc;\r
335 \r
336         gui::IGUIStaticText *StaticText;\r
337 \r
338         core::rect<s32> BgMiddle;\r
339         core::rect<s32> Padding;\r
340         core::vector2d<s32> ContentOffset;\r
341         // END PATCH\r
342 };\r