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