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