]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CGUIButton.h
Merging r6145 through r6171 from trunk to ogl-es branch
[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                 virtual bool OnEvent(const SEvent& event) _IRR_OVERRIDE_;\r
34 \r
35                 //! draws the element and its children\r
36                 virtual void draw() _IRR_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(IGUIFont* font=0) _IRR_OVERRIDE_;\r
40 \r
41                 //! Gets the override font (if any)\r
42                 virtual IGUIFont* getOverrideFont() const _IRR_OVERRIDE_;\r
43 \r
44                 //! Get the font which is used right now for drawing\r
45                 virtual IGUIFont* getActiveFont() const _IRR_OVERRIDE_;\r
46 \r
47                 //! Sets another color for the button text.\r
48                 virtual void setOverrideColor(video::SColor color)  _IRR_OVERRIDE_;\r
49 \r
50                 //! Gets the override color\r
51                 virtual video::SColor getOverrideColor(void) const  _IRR_OVERRIDE_;\r
52 \r
53                 //! Gets the currently used text color\r
54                 virtual video::SColor getActiveColor() const _IRR_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)  _IRR_OVERRIDE_;\r
58 \r
59                 //! Checks if an override color is enabled\r
60                 virtual bool isOverrideColorEnabled(void) const  _IRR_OVERRIDE_;\r
61 \r
62                 //! Sets an image which should be displayed on the button when it is in the given state.\r
63                 virtual void setImage(EGUI_BUTTON_IMAGE_STATE state, video::ITexture* image=0, const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0))  _IRR_OVERRIDE_;\r
64 \r
65                 //! Sets an image which should be displayed on the button when it is in normal state.\r
66                 virtual void setImage(video::ITexture* image=0) _IRR_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                 virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) _IRR_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                 virtual void setPressedImage(video::ITexture* image=0) _IRR_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                 virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) _IRR_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                 virtual void setSpriteBank(IGUISpriteBank* bank=0) _IRR_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) _IRR_OVERRIDE_;\r
101 \r
102                 //! Get the sprite-index for the given state or -1 when no sprite is set\r
103                 virtual s32 getSpriteIndex(EGUI_BUTTON_STATE state) const _IRR_OVERRIDE_;\r
104 \r
105                 //! Get the sprite color for the given state. Color is only used when a sprite is set.\r
106                 virtual video::SColor getSpriteColor(EGUI_BUTTON_STATE state) const _IRR_OVERRIDE_;\r
107 \r
108                 //! Returns if the sprite in the given state does loop\r
109                 virtual bool getSpriteLoop(EGUI_BUTTON_STATE state) const _IRR_OVERRIDE_;\r
110 \r
111                 //! Returns if the sprite in the given state is scaled\r
112                 virtual bool getSpriteScale(EGUI_BUTTON_STATE state) const _IRR_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                 virtual void setIsPushButton(bool isPushButton=true) _IRR_OVERRIDE_;\r
118 \r
119                 //! Checks whether the button is a push button\r
120                 virtual bool isPushButton() const _IRR_OVERRIDE_;\r
121 \r
122                 //! Sets the pressed state of the button if this is a pushbutton\r
123                 virtual void setPressed(bool pressed=true) _IRR_OVERRIDE_;\r
124 \r
125                 //! Returns if the button is currently pressed\r
126                 virtual bool isPressed() const _IRR_OVERRIDE_;\r
127 \r
128                 //! Sets if the button should use the skin to draw its border\r
129                 virtual void setDrawBorder(bool border=true) _IRR_OVERRIDE_;\r
130 \r
131                 //! Checks if the button face and border are being drawn\r
132                 virtual bool isDrawingBorder() const _IRR_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) _IRR_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 _IRR_OVERRIDE_;\r
139 \r
140                 //! Sets if the button should scale the button images to fit\r
141                 virtual void setScaleImage(bool scaleImage=true) _IRR_OVERRIDE_;\r
142 \r
143                 //! Checks whether the button scales the used images\r
144                 virtual bool isScalingImage() const _IRR_OVERRIDE_;\r
145 \r
146                 //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event\r
147                 virtual bool getClickShiftState() const _IRR_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 _IRR_OVERRIDE_\r
154                 {\r
155                         return ClickControlState;\r
156                 }\r
157 \r
158                 //! Writes attributes of the element.\r
159                 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const _IRR_OVERRIDE_;\r
160 \r
161                 //! Reads attributes of the element\r
162                 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) _IRR_OVERRIDE_;\r
163 \r
164         protected:\r
165                 void drawSprite(EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);\r
166                 EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;\r
167 \r
168         private:\r
169 \r
170                 struct ButtonSprite\r
171                 {\r
172                         ButtonSprite() : Index(-1), Loop(false), Scale(false)\r
173                         {\r
174                         }\r
175 \r
176                         bool operator==(const ButtonSprite& other) const\r
177                         {\r
178                                 return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;\r
179                         }\r
180 \r
181                         s32 Index;\r
182                         video::SColor Color;\r
183                         bool Loop;\r
184                         bool Scale;\r
185                 };\r
186 \r
187                 ButtonSprite ButtonSprites[EGBS_COUNT];\r
188                 IGUISpriteBank* SpriteBank;\r
189 \r
190                 struct ButtonImage\r
191                 {\r
192                         ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
193                         {\r
194                         }\r
195 \r
196                         ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
197                         {\r
198                                 *this = other;\r
199                         }\r
200 \r
201                         ~ButtonImage()\r
202                         {\r
203                                 if ( Texture )\r
204                                         Texture->drop();\r
205                         }\r
206 \r
207                         ButtonImage& operator=(const ButtonImage& other)\r
208                         {\r
209                                 if ( this == &other )\r
210                                         return *this;\r
211 \r
212                                 if (other.Texture)\r
213                                         other.Texture->grab();\r
214                                 if ( Texture )\r
215                                         Texture->drop();\r
216                                 Texture = other.Texture;\r
217                                 SourceRect = other.SourceRect;\r
218                                 return *this;\r
219                         }\r
220 \r
221                         bool operator==(const ButtonImage& other) const\r
222                         {\r
223                                 return Texture == other.Texture && SourceRect == other.SourceRect;\r
224                         }\r
225 \r
226 \r
227                         video::ITexture* Texture;\r
228                         core::rect<s32> SourceRect;\r
229                 };\r
230 \r
231                 ButtonImage ButtonImages[EGBIS_COUNT];\r
232 \r
233                 IGUIFont* OverrideFont;\r
234 \r
235                 bool OverrideColorEnabled;\r
236                 video::SColor OverrideColor;\r
237 \r
238                 u32 ClickTime, HoverTime, FocusTime;\r
239 \r
240                 bool ClickShiftState;\r
241                 bool ClickControlState;\r
242 \r
243                 bool IsPushButton;\r
244                 bool Pressed;\r
245                 bool UseAlphaChannel;\r
246                 bool DrawBorder;\r
247                 bool ScaleImage;\r
248         };\r
249 \r
250 } // end namespace gui\r
251 } // end namespace irr\r
252 \r
253 #endif // _IRR_COMPILE_WITH_GUI_\r
254 \r
255 #endif // __C_GUI_BUTTON_H_INCLUDED__\r
256 \r