]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiButton.h
Drop dependency on IrrCompileConfig
[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 <IGUIStaticText.h>\r
8 #include "irrlicht_changes/static_text.h"\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 #include "StyleSpec.h"\r
15 \r
16 using namespace irr;\r
17 \r
18 class ISimpleTextureSource;\r
19 \r
20 class GUIButton : public gui::IGUIButton\r
21 {\r
22 public:\r
23 \r
24         //! constructor\r
25         GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,\r
26                            s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,\r
27                            bool noclip=false);\r
28 \r
29         //! destructor\r
30         virtual ~GUIButton();\r
31 \r
32         //! called if an event happened.\r
33         virtual bool OnEvent(const SEvent& event) override;\r
34 \r
35         //! draws the element and its children\r
36         virtual void draw() override;\r
37 \r
38         //! sets another skin independent font. if this is set to zero, the button uses the font of the skin.\r
39         virtual void setOverrideFont(gui::IGUIFont* font=0) override;\r
40 \r
41         //! Gets the override font (if any)\r
42         virtual gui::IGUIFont* getOverrideFont() const override;\r
43 \r
44         //! Get the font which is used right now for drawing\r
45         virtual gui::IGUIFont* getActiveFont() const override;\r
46 \r
47         //! Sets another color for the button text.\r
48         virtual void setOverrideColor(video::SColor color) override;\r
49 \r
50         //! Gets the override color\r
51         virtual video::SColor getOverrideColor() const override;\r
52 \r
53         //! Gets the currently used text color\r
54         virtual video::SColor getActiveColor() const override;\r
55 \r
56         //! Sets if the button text should use the override color or the color in the gui skin.\r
57         virtual void enableOverrideColor(bool enable) override;\r
58 \r
59         //! Checks if an override color is enabled\r
60         virtual bool isOverrideColorEnabled(void) const override;\r
61 \r
62         // PATCH\r
63         //! Sets an image which should be displayed on the button when it is in the given state.\r
64         virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,\r
65                         video::ITexture* image=nullptr,\r
66                         const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;\r
67 \r
68         //! Sets an image which should be displayed on the button when it is in normal state.\r
69         virtual void setImage(video::ITexture* image=nullptr) override;\r
70 \r
71         //! Sets an image which should be displayed on the button when it is in normal state.\r
72         virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
73 \r
74         //! Sets an image which should be displayed on the button when it is in pressed state.\r
75         virtual void setPressedImage(video::ITexture* image=nullptr) override;\r
76 \r
77         //! Sets an image which should be displayed on the button when it is in pressed state.\r
78         virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
79 \r
80         //! Sets the text displayed by the button\r
81         virtual void setText(const wchar_t* text) override;\r
82         // END PATCH\r
83 \r
84         //! Sets the sprite bank used by the button\r
85         virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;\r
86 \r
87         //! Sets the animated sprite for a specific button state\r
88         /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite\r
89         \param state: State of the button to set the sprite for\r
90         \param index: The sprite number from the current sprite bank\r
91         \param color: The color of the sprite\r
92         */\r
93         virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,\r
94                                                    video::SColor color=video::SColor(255,255,255,255),\r
95                                                    bool loop=false, bool scale=false) override;\r
96 \r
97         //! Get the sprite-index for the given state or -1 when no sprite is set\r
98         virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;\r
99 \r
100         //! Get the sprite color for the given state. Color is only used when a sprite is set.\r
101         virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;\r
102 \r
103         //! Returns if the sprite in the given state does loop\r
104         virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;\r
105 \r
106         //! Returns if the sprite in the given state is scaled\r
107         virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const override;\r
108 \r
109         //! Sets if the button should behave like a push button. Which means it\r
110         //! can be in two states: Normal or Pressed. With a click on the button,\r
111         //! the user can change the state of the button.\r
112         virtual void setIsPushButton(bool isPushButton=true) override;\r
113 \r
114         //! Checks whether the button is a push button\r
115         virtual bool isPushButton() const override;\r
116 \r
117         //! Sets the pressed state of the button if this is a pushbutton\r
118         virtual void setPressed(bool pressed=true) override;\r
119 \r
120         //! Returns if the button is currently pressed\r
121         virtual bool isPressed() const override;\r
122 \r
123         // PATCH\r
124         //! Returns if this element (or one of its direct children) is hovered\r
125         bool isHovered() const;\r
126         // END PATCH\r
127 \r
128         //! Sets if the button should use the skin to draw its border\r
129         virtual void setDrawBorder(bool border=true) override;\r
130 \r
131         //! Checks if the button face and border are being drawn\r
132         virtual bool isDrawingBorder() const override;\r
133 \r
134         //! Sets if the alpha channel should be used for drawing images on the button (default is false)\r
135         virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;\r
136 \r
137         //! Checks if the alpha channel should be used for drawing images on the button\r
138         virtual bool isAlphaChannelUsed() const override;\r
139 \r
140         //! Sets if the button should scale the button images to fit\r
141         virtual void setScaleImage(bool scaleImage=true) override;\r
142 \r
143         //! Checks whether the button scales the used images\r
144         virtual bool isScalingImage() const override;\r
145 \r
146         //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event\r
147         virtual bool getClickShiftState() const override\r
148         {\r
149                 return ClickShiftState;\r
150         }\r
151 \r
152         //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event\r
153         virtual bool getClickControlState() const override\r
154         {\r
155                 return ClickControlState;\r
156         }\r
157 \r
158         void setColor(video::SColor color);\r
159         // PATCH\r
160         //! Set element properties from a StyleSpec corresponding to the button state\r
161         void setFromState();\r
162 \r
163         //! Set element properties from a StyleSpec\r
164         virtual void setFromStyle(const StyleSpec& style);\r
165 \r
166         //! Set the styles used for each state\r
167         void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);\r
168         // END PATCH\r
169 \r
170 \r
171         //! Do not drop returned handle\r
172         static GUIButton* addButton(gui::IGUIEnvironment *environment,\r
173                         const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,\r
174                         IGUIElement* parent, s32 id, const wchar_t* text,\r
175                         const wchar_t *tooltiptext=L"");\r
176 \r
177 protected:\r
178         void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);\r
179         gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;\r
180 \r
181         ISimpleTextureSource *getTextureSource() { return TSrc; }\r
182 \r
183         struct ButtonImage\r
184         {\r
185                 ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
186                 {\r
187                 }\r
188 \r
189                 ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
190                 {\r
191                         *this = other;\r
192                 }\r
193 \r
194                 ~ButtonImage()\r
195                 {\r
196                         if ( Texture )\r
197                                 Texture->drop();\r
198                 }\r
199 \r
200                 ButtonImage& operator=(const ButtonImage& other)\r
201                 {\r
202                         if ( this == &other )\r
203                                 return *this;\r
204 \r
205                         if (other.Texture)\r
206                                 other.Texture->grab();\r
207                         if ( Texture )\r
208                                 Texture->drop();\r
209                         Texture = other.Texture;\r
210                         SourceRect = other.SourceRect;\r
211                         return *this;\r
212                 }\r
213 \r
214                 bool operator==(const ButtonImage& other) const\r
215                 {\r
216                         return Texture == other.Texture && SourceRect == other.SourceRect;\r
217                 }\r
218 \r
219 \r
220                 video::ITexture* Texture;\r
221                 core::rect<s32> SourceRect;\r
222         };\r
223 \r
224         gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;\r
225 \r
226 private:\r
227 \r
228         struct ButtonSprite\r
229         {\r
230                 ButtonSprite() : Index(-1), Loop(false), Scale(false)\r
231                 {\r
232                 }\r
233 \r
234                 bool operator==(const ButtonSprite& other) const\r
235                 {\r
236                         return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;\r
237                 }\r
238 \r
239                 s32 Index;\r
240                 video::SColor Color;\r
241                 bool Loop;\r
242                 bool Scale;\r
243         };\r
244 \r
245         ButtonSprite ButtonSprites[gui::EGBS_COUNT];\r
246         gui::IGUISpriteBank* SpriteBank;\r
247 \r
248         ButtonImage ButtonImages[gui::EGBIS_COUNT];\r
249 \r
250         std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;\r
251 \r
252         gui::IGUIFont* OverrideFont;\r
253 \r
254         bool OverrideColorEnabled;\r
255         video::SColor OverrideColor;\r
256 \r
257         u32 ClickTime, HoverTime, FocusTime;\r
258 \r
259         bool ClickShiftState;\r
260         bool ClickControlState;\r
261 \r
262         bool IsPushButton;\r
263         bool Pressed;\r
264         bool UseAlphaChannel;\r
265         bool DrawBorder;\r
266         bool ScaleImage;\r
267 \r
268         video::SColor Colors[4];\r
269         // PATCH\r
270         bool WasHovered = false;\r
271         ISimpleTextureSource *TSrc;\r
272 \r
273         gui::IGUIStaticText *StaticText;\r
274 \r
275         core::rect<s32> BgMiddle;\r
276         core::rect<s32> Padding;\r
277         core::vector2d<s32> ContentOffset;\r
278         video::SColor BgColor;\r
279         // END PATCH\r
280 };\r