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