]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiButton.h
ee9bb6f2196412fcef5a09f51bee845d3488f2ec
[minetest.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 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)\r
73 #define OVERRIDE_19\r
74 #else\r
75 #define OVERRIDE_19 override\r
76 #endif\r
77 \r
78 class GUIButton : public gui::IGUIButton\r
79 {\r
80 public:\r
81 \r
82         //! constructor\r
83         GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,\r
84                            s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,\r
85                            bool noclip=false);\r
86 \r
87         //! destructor\r
88         virtual ~GUIButton();\r
89 \r
90         //! called if an event happened.\r
91         virtual bool OnEvent(const SEvent& event) override;\r
92 \r
93         //! draws the element and its children\r
94         virtual void draw() override;\r
95 \r
96         //! sets another skin independent font. if this is set to zero, the button uses the font of the skin.\r
97         virtual void setOverrideFont(gui::IGUIFont* font=0) override;\r
98 \r
99         //! Gets the override font (if any)\r
100         virtual gui::IGUIFont* getOverrideFont() const override;\r
101 \r
102         //! Get the font which is used right now for drawing\r
103         virtual gui::IGUIFont* getActiveFont() const override;\r
104 \r
105         //! Sets another color for the button text.\r
106         virtual void setOverrideColor(video::SColor color) OVERRIDE_19;\r
107 \r
108         //! Gets the override color\r
109         virtual video::SColor getOverrideColor(void) const OVERRIDE_19;\r
110 \r
111         #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8\r
112         //! Gets the currently used text color\r
113         virtual video::SColor getActiveColor() const override;\r
114         #endif\r
115 \r
116         //! Sets if the button text should use the override color or the color in the gui skin.\r
117         virtual void enableOverrideColor(bool enable) OVERRIDE_19;\r
118 \r
119         //! Checks if an override color is enabled\r
120         virtual bool isOverrideColorEnabled(void) const OVERRIDE_19;\r
121 \r
122         // PATCH\r
123         //! Sets an image which should be displayed on the button when it is in the given state.\r
124         virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,\r
125                         video::ITexture* image=nullptr,\r
126                         const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) OVERRIDE_19;\r
127 \r
128         //! Sets an image which should be displayed on the button when it is in normal state.\r
129         virtual void setImage(video::ITexture* image=nullptr) override;\r
130 \r
131         //! Sets an image which should be displayed on the button when it is in normal state.\r
132         virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
133 \r
134         //! Sets an image which should be displayed on the button when it is in pressed state.\r
135         virtual void setPressedImage(video::ITexture* image=nullptr) override;\r
136 \r
137         //! Sets an image which should be displayed on the button when it is in pressed state.\r
138         virtual void setPressedImage(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 sprite\r
149         \param state: State of the button to set the sprite for\r
150         \param index: The sprite number from the current sprite bank\r
151         \param color: The color of the sprite\r
152         */\r
153         virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,\r
154                                                    video::SColor color=video::SColor(255,255,255,255),\r
155                                                    bool loop=false, bool scale=false) OVERRIDE_19;\r
156 \r
157 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)\r
158         void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop) override {\r
159                 setSprite(state, index, color, loop, false);\r
160         }\r
161 #endif\r
162 \r
163         //! Get the sprite-index for the given state or -1 when no sprite is set\r
164         virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;\r
165 \r
166         //! Get the sprite color for the given state. Color is only used when a sprite is set.\r
167         virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;\r
168 \r
169         //! Returns if the sprite in the given state does loop\r
170         virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;\r
171 \r
172         //! Returns if the sprite in the given state is scaled\r
173         virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;\r
174 \r
175         //! Sets if the button should behave like a push button. Which means it\r
176         //! can be in two states: Normal or Pressed. With a click on the button,\r
177         //! the user can change the state of the button.\r
178         virtual void setIsPushButton(bool isPushButton=true) override;\r
179 \r
180         //! Checks whether the button is a push button\r
181         virtual bool isPushButton() const override;\r
182 \r
183         //! Sets the pressed state of the button if this is a pushbutton\r
184         virtual void setPressed(bool pressed=true) override;\r
185 \r
186         //! Returns if the button is currently pressed\r
187         virtual bool isPressed() const override;\r
188 \r
189         // PATCH\r
190         //! Returns if this element (or one of its direct children) is hovered\r
191         bool isHovered() const;\r
192         // END PATCH\r
193 \r
194         //! Sets if the button should use the skin to draw its border\r
195         virtual void setDrawBorder(bool border=true) override;\r
196 \r
197         //! Checks if the button face and border are being drawn\r
198         virtual bool isDrawingBorder() const override;\r
199 \r
200         //! Sets if the alpha channel should be used for drawing images on the button (default is false)\r
201         virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;\r
202 \r
203         //! Checks if the alpha channel should be used for drawing images on the button\r
204         virtual bool isAlphaChannelUsed() const override;\r
205 \r
206         //! Sets if the button should scale the button images to fit\r
207         virtual void setScaleImage(bool scaleImage=true) override;\r
208 \r
209         //! Checks whether the button scales the used images\r
210         virtual bool isScalingImage() const override;\r
211 \r
212         //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event\r
213         virtual bool getClickShiftState() const OVERRIDE_19\r
214         {\r
215                 return ClickShiftState;\r
216         }\r
217 \r
218         //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event\r
219         virtual bool getClickControlState() const OVERRIDE_19\r
220         {\r
221                 return ClickControlState;\r
222         }\r
223 \r
224         void setColor(video::SColor color);\r
225         // PATCH\r
226         //! Set element properties from a StyleSpec corresponding to the button state\r
227         void setFromState();\r
228 \r
229         //! Set element properties from a StyleSpec\r
230         virtual void setFromStyle(const StyleSpec& style);\r
231 \r
232         //! Set the styles used for each state\r
233         void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);\r
234         // END PATCH\r
235 \r
236 \r
237         //! Do not drop returned handle\r
238         static GUIButton* addButton(gui::IGUIEnvironment *environment,\r
239                         const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,\r
240                         IGUIElement* parent, s32 id, const wchar_t* text,\r
241                         const wchar_t *tooltiptext=L"");\r
242 \r
243 protected:\r
244         void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);\r
245         gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;\r
246 \r
247         ISimpleTextureSource *getTextureSource() { return TSrc; }\r
248 \r
249         struct ButtonImage\r
250         {\r
251                 ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
252                 {\r
253                 }\r
254 \r
255                 ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
256                 {\r
257                         *this = other;\r
258                 }\r
259 \r
260                 ~ButtonImage()\r
261                 {\r
262                         if ( Texture )\r
263                                 Texture->drop();\r
264                 }\r
265 \r
266                 ButtonImage& operator=(const ButtonImage& other)\r
267                 {\r
268                         if ( this == &other )\r
269                                 return *this;\r
270 \r
271                         if (other.Texture)\r
272                                 other.Texture->grab();\r
273                         if ( Texture )\r
274                                 Texture->drop();\r
275                         Texture = other.Texture;\r
276                         SourceRect = other.SourceRect;\r
277                         return *this;\r
278                 }\r
279 \r
280                 bool operator==(const ButtonImage& other) const\r
281                 {\r
282                         return Texture == other.Texture && SourceRect == other.SourceRect;\r
283                 }\r
284 \r
285 \r
286                 video::ITexture* Texture;\r
287                 core::rect<s32> SourceRect;\r
288         };\r
289 \r
290         gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;\r
291 \r
292 private:\r
293 \r
294         struct ButtonSprite\r
295         {\r
296                 ButtonSprite() : Index(-1), Loop(false), Scale(false)\r
297                 {\r
298                 }\r
299 \r
300                 bool operator==(const ButtonSprite& other) const\r
301                 {\r
302                         return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;\r
303                 }\r
304 \r
305                 s32 Index;\r
306                 video::SColor Color;\r
307                 bool Loop;\r
308                 bool Scale;\r
309         };\r
310 \r
311         ButtonSprite ButtonSprites[gui::EGBS_COUNT];\r
312         gui::IGUISpriteBank* SpriteBank;\r
313 \r
314         ButtonImage ButtonImages[gui::EGBIS_COUNT];\r
315 \r
316         std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;\r
317 \r
318         gui::IGUIFont* OverrideFont;\r
319 \r
320         bool OverrideColorEnabled;\r
321         video::SColor OverrideColor;\r
322 \r
323         u32 ClickTime, HoverTime, FocusTime;\r
324 \r
325         bool ClickShiftState;\r
326         bool ClickControlState;\r
327 \r
328         bool IsPushButton;\r
329         bool Pressed;\r
330         bool UseAlphaChannel;\r
331         bool DrawBorder;\r
332         bool ScaleImage;\r
333 \r
334         video::SColor Colors[4];\r
335         // PATCH\r
336         bool WasHovered = false;\r
337         ISimpleTextureSource *TSrc;\r
338 \r
339         gui::IGUIStaticText *StaticText;\r
340 \r
341         core::rect<s32> BgMiddle;\r
342         core::rect<s32> Padding;\r
343         core::vector2d<s32> ContentOffset;\r
344         video::SColor BgColor;\r
345         // END PATCH\r
346 };\r