]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CGUIButton.h
9bd861223f61187cf569198062e0c550afffe1fa
[irrlicht.git] / source / Irrlicht / CGUIButton.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 #ifndef __C_GUI_BUTTON_H_INCLUDED__\r
6 #define __C_GUI_BUTTON_H_INCLUDED__\r
7 \r
8 #include "IrrCompileConfig.h"\r
9 #ifdef _IRR_COMPILE_WITH_GUI_\r
10 \r
11 #include "IGUIButton.h"\r
12 #include "IGUISpriteBank.h"\r
13 #include "ITexture.h"\r
14 #include "SColor.h"\r
15 \r
16 namespace irr\r
17 {\r
18 namespace gui\r
19 {\r
20 \r
21         class CGUIButton : public IGUIButton\r
22         {\r
23         public:\r
24 \r
25                 //! constructor\r
26                 CGUIButton(IGUIEnvironment* environment, IGUIElement* parent,\r
27                         s32 id, core::rect<s32> rectangle, bool noclip=false);\r
28 \r
29                 //! destructor\r
30                 virtual ~CGUIButton();\r
31 \r
32                 //! called if an event happened.\r
33                 bool OnEvent(const SEvent& event) override;\r
34 \r
35                 //! draws the element and its children\r
36                 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                 void setOverrideFont(IGUIFont* font=0) override;\r
40 \r
41                 //! Gets the override font (if any)\r
42                 IGUIFont* getOverrideFont() const override;\r
43 \r
44                 //! Get the font which is used right now for drawing\r
45                 IGUIFont* getActiveFont() const override;\r
46 \r
47                 //! Sets another color for the button text.\r
48                 void setOverrideColor(video::SColor color)  override;\r
49 \r
50                 //! Gets the override color\r
51                 video::SColor getOverrideColor(void) const  override;\r
52 \r
53                 //! Gets the currently used text color\r
54                 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                 void enableOverrideColor(bool enable)  override;\r
58 \r
59                 //! Checks if an override color is enabled\r
60                 bool isOverrideColorEnabled(void) const  override;\r
61 \r
62                 //! Sets an image which should be displayed on the button when it is in the given state.\r
63                 void setImage(EGUI_BUTTON_IMAGE_STATE state, video::ITexture* image=0, const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0))  override;\r
64 \r
65                 //! Sets an image which should be displayed on the button when it is in normal state.\r
66                 void setImage(video::ITexture* image=0) override\r
67                 {\r
68                         setImage(EGBIS_IMAGE_UP, image);\r
69                 }\r
70 \r
71                 //! Sets an image which should be displayed on the button when it is in normal state.\r
72                 void setImage(video::ITexture* image, const core::rect<s32>& pos) override\r
73                 {\r
74                         setImage(EGBIS_IMAGE_UP, image, pos);\r
75                 }\r
76 \r
77                 //! Sets an image which should be displayed on the button when it is in pressed state.\r
78                 void setPressedImage(video::ITexture* image=0) override\r
79                 {\r
80                         setImage(EGBIS_IMAGE_DOWN, image);\r
81                 }\r
82 \r
83                 //! Sets an image which should be displayed on the button when it is in pressed state.\r
84                 void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override\r
85                 {\r
86                         setImage(EGBIS_IMAGE_DOWN, image, pos);\r
87                 }\r
88 \r
89                 //! Sets the sprite bank used by the button\r
90                 void setSpriteBank(IGUISpriteBank* bank=0) override;\r
91 \r
92                 //! Sets the animated sprite for a specific button state\r
93                 /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite\r
94                 \param state: State of the button to set the sprite for\r
95                 \param index: The sprite number from the current sprite bank\r
96                 \param color: The color of the sprite\r
97                 */\r
98                 virtual void setSprite(EGUI_BUTTON_STATE state, s32 index,\r
99                                 video::SColor color=video::SColor(255,255,255,255),\r
100                                 bool loop=false, bool scale=false) override;\r
101 \r
102                 //! Get the sprite-index for the given state or -1 when no sprite is set\r
103                 s32 getSpriteIndex(EGUI_BUTTON_STATE state) const override;\r
104 \r
105                 //! Get the sprite color for the given state. Color is only used when a sprite is set.\r
106                 video::SColor getSpriteColor(EGUI_BUTTON_STATE state) const override;\r
107 \r
108                 //! Returns if the sprite in the given state does loop\r
109                 bool getSpriteLoop(EGUI_BUTTON_STATE state) const override;\r
110 \r
111                 //! Returns if the sprite in the given state is scaled\r
112                 bool getSpriteScale(EGUI_BUTTON_STATE state) const override;\r
113 \r
114                 //! Sets if the button should behave like a push button. Which means it\r
115                 //! can be in two states: Normal or Pressed. With a click on the button,\r
116                 //! the user can change the state of the button.\r
117                 void setIsPushButton(bool isPushButton=true) override;\r
118 \r
119                 //! Checks whether the button is a push button\r
120                 bool isPushButton() const override;\r
121 \r
122                 //! Sets the pressed state of the button if this is a pushbutton\r
123                 void setPressed(bool pressed=true) override;\r
124 \r
125                 //! Returns if the button is currently pressed\r
126                 bool isPressed() const override;\r
127 \r
128                 //! Sets if the button should use the skin to draw its border\r
129                 void setDrawBorder(bool border=true) override;\r
130 \r
131                 //! Checks if the button face and border are being drawn\r
132                 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                 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                 bool isAlphaChannelUsed() const override;\r
139 \r
140                 //! Sets if the button should scale the button images to fit\r
141                 void setScaleImage(bool scaleImage=true) override;\r
142 \r
143                 //! Checks whether the button scales the used images\r
144                 bool isScalingImage() const override;\r
145 \r
146                 //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event\r
147                 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                 bool getClickControlState() const override\r
154                 {\r
155                         return ClickControlState;\r
156                 }\r
157 \r
158         protected:\r
159                 void drawSprite(EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);\r
160                 EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;\r
161 \r
162         private:\r
163 \r
164                 struct ButtonSprite\r
165                 {\r
166                         ButtonSprite() : Index(-1), Loop(false), Scale(false)\r
167                         {\r
168                         }\r
169 \r
170                         bool operator==(const ButtonSprite& other) const\r
171                         {\r
172                                 return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;\r
173                         }\r
174 \r
175                         s32 Index;\r
176                         video::SColor Color;\r
177                         bool Loop;\r
178                         bool Scale;\r
179                 };\r
180 \r
181                 ButtonSprite ButtonSprites[EGBS_COUNT];\r
182                 IGUISpriteBank* SpriteBank;\r
183 \r
184                 struct ButtonImage\r
185                 {\r
186                         ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
187                         {\r
188                         }\r
189 \r
190                         ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
191                         {\r
192                                 *this = other;\r
193                         }\r
194 \r
195                         ~ButtonImage()\r
196                         {\r
197                                 if ( Texture )\r
198                                         Texture->drop();\r
199                         }\r
200 \r
201                         ButtonImage& operator=(const ButtonImage& other)\r
202                         {\r
203                                 if ( this == &other )\r
204                                         return *this;\r
205 \r
206                                 if (other.Texture)\r
207                                         other.Texture->grab();\r
208                                 if ( Texture )\r
209                                         Texture->drop();\r
210                                 Texture = other.Texture;\r
211                                 SourceRect = other.SourceRect;\r
212                                 return *this;\r
213                         }\r
214 \r
215                         bool operator==(const ButtonImage& other) const\r
216                         {\r
217                                 return Texture == other.Texture && SourceRect == other.SourceRect;\r
218                         }\r
219 \r
220 \r
221                         video::ITexture* Texture;\r
222                         core::rect<s32> SourceRect;\r
223                 };\r
224 \r
225                 ButtonImage ButtonImages[EGBIS_COUNT];\r
226 \r
227                 IGUIFont* OverrideFont;\r
228 \r
229                 bool OverrideColorEnabled;\r
230                 video::SColor OverrideColor;\r
231 \r
232                 u32 ClickTime, HoverTime, FocusTime;\r
233 \r
234                 bool ClickShiftState;\r
235                 bool ClickControlState;\r
236 \r
237                 bool IsPushButton;\r
238                 bool Pressed;\r
239                 bool UseAlphaChannel;\r
240                 bool DrawBorder;\r
241                 bool ScaleImage;\r
242         };\r
243 \r
244 } // end namespace gui\r
245 } // end namespace irr\r
246 \r
247 #endif // _IRR_COMPILE_WITH_GUI_\r
248 \r
249 #endif // __C_GUI_BUTTON_H_INCLUDED__\r
250 \r