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