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
7 #include "IrrCompileConfig.h"
\r
9 #include "IGUIButton.h"
\r
10 #include "IGUISpriteBank.h"
\r
11 #include "ITexture.h"
\r
13 #include "guiSkin.h"
\r
15 using namespace irr;
\r
17 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)
\r
18 namespace irr { namespace gui {
\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
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
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
48 //! Names for gui button image states
\r
49 const c8 *const GUIButtonImageStateNames[EGBIS_COUNT + 1] =
\r
51 "Image", // not "ImageUp" as it otherwise breaks serialization of old files
\r
54 "ImageUpFocusedOver",
\r
55 "PressedImage", // not "ImageDown" as it otherwise breaks serialization of old files
\r
58 "ImageDownFocusedOver",
\r
67 class GUIButton : public gui::IGUIButton
\r
72 GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
\r
73 s32 id, core::rect<s32> rectangle, bool noclip=false);
\r
76 virtual ~GUIButton();
\r
78 //! called if an event happened.
\r
79 virtual bool OnEvent(const SEvent& event);
\r
81 //! draws the element and its children
\r
82 virtual void draw();
\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);
\r
87 //! Gets the override font (if any)
\r
88 virtual gui::IGUIFont* getOverrideFont() const;
\r
90 //! Get the font which is used right now for drawing
\r
91 virtual gui::IGUIFont* getActiveFont() const;
\r
93 //! Sets another color for the button text.
\r
94 virtual void setOverrideColor(video::SColor color);
\r
96 //! Gets the override color
\r
97 virtual video::SColor getOverrideColor(void) const;
\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
102 //! Checks if an override color is enabled
\r
103 virtual bool isOverrideColorEnabled(void) const;
\r
105 //! Sets an image which should be displayed on the button when it is in the given state.
\r
106 virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state, video::ITexture* image=0, const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0));
\r
108 //! Sets an image which should be displayed on the button when it is in normal state.
\r
109 virtual void setImage(video::ITexture* image=0)
\r
111 setImage(gui::EGBIS_IMAGE_UP, image);
\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)
\r
117 setImage(gui::EGBIS_IMAGE_UP, image, pos);
\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=0)
\r
123 setImage(gui::EGBIS_IMAGE_DOWN, image);
\r
126 //! Sets an image which should be displayed on the button when it is in pressed state.
\r
127 virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos)
\r
129 setImage(gui::EGBIS_IMAGE_DOWN, image, pos);
\r
132 //! Sets the sprite bank used by the button
\r
133 virtual void setSpriteBank(gui::IGUISpriteBank* bank=0);
\r
135 //! Sets the animated sprite for a specific button state
\r
136 /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite
\r
137 \param state: State of the button to set the sprite for
\r
138 \param index: The sprite number from the current sprite bank
\r
139 \param color: The color of the sprite
\r
141 virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
\r
142 video::SColor color=video::SColor(255,255,255,255),
\r
143 bool loop=false, bool scale=false);
\r
145 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)
\r
146 void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop) override {
\r
147 setSprite(state, index, color, loop, false);
\r
151 //! Get the sprite-index for the given state or -1 when no sprite is set
\r
152 virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const;
\r
154 //! Get the sprite color for the given state. Color is only used when a sprite is set.
\r
155 virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const;
\r
157 //! Returns if the sprite in the given state does loop
\r
158 virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const;
\r
160 //! Returns if the sprite in the given state is scaled
\r
161 virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const;
\r
163 //! Sets if the button should behave like a push button. Which means it
\r
164 //! can be in two states: Normal or Pressed. With a click on the button,
\r
165 //! the user can change the state of the button.
\r
166 virtual void setIsPushButton(bool isPushButton=true);
\r
168 //! Checks whether the button is a push button
\r
169 virtual bool isPushButton() const;
\r
171 //! Sets the pressed state of the button if this is a pushbutton
\r
172 virtual void setPressed(bool pressed=true);
\r
174 //! Returns if the button is currently pressed
\r
175 virtual bool isPressed() const;
\r
177 //! Sets if the button should use the skin to draw its border
\r
178 virtual void setDrawBorder(bool border=true);
\r
180 //! Checks if the button face and border are being drawn
\r
181 virtual bool isDrawingBorder() const;
\r
183 //! Sets if the alpha channel should be used for drawing images on the button (default is false)
\r
184 virtual void setUseAlphaChannel(bool useAlphaChannel=true);
\r
186 //! Checks if the alpha channel should be used for drawing images on the button
\r
187 virtual bool isAlphaChannelUsed() const;
\r
189 //! Sets if the button should scale the button images to fit
\r
190 virtual void setScaleImage(bool scaleImage=true);
\r
192 //! Checks whether the button scales the used images
\r
193 virtual bool isScalingImage() const;
\r
195 //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
\r
196 virtual bool getClickShiftState() const
\r
198 return ClickShiftState;
\r
201 //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event
\r
202 virtual bool getClickControlState() const
\r
204 return ClickControlState;
\r
207 //! Writes attributes of the element.
\r
208 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
\r
210 //! Reads attributes of the element
\r
211 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
\r
215 void setColor(video::SColor color);
\r
218 //! Do not drop returned handle
\r
219 static GUIButton* addButton(gui::IGUIEnvironment *environment, const core::rect<s32>& rectangle,
\r
220 IGUIElement* parent, s32 id, const wchar_t* text, const wchar_t *tooltiptext=L"");
\r
223 void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
\r
224 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
\r
228 struct ButtonSprite
\r
230 ButtonSprite() : Index(-1), Loop(false), Scale(false)
\r
234 bool operator==(const ButtonSprite& other) const
\r
236 return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;
\r
240 video::SColor Color;
\r
245 ButtonSprite ButtonSprites[gui::EGBS_COUNT];
\r
246 gui::IGUISpriteBank* SpriteBank;
\r
250 ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))
\r
254 ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))
\r
265 ButtonImage& operator=(const ButtonImage& other)
\r
267 if ( this == &other )
\r
271 other.Texture->grab();
\r
274 Texture = other.Texture;
\r
275 SourceRect = other.SourceRect;
\r
279 bool operator==(const ButtonImage& other) const
\r
281 return Texture == other.Texture && SourceRect == other.SourceRect;
\r
285 video::ITexture* Texture;
\r
286 core::rect<s32> SourceRect;
\r
289 ButtonImage ButtonImages[gui::EGBIS_COUNT];
\r
291 gui::IGUIFont* OverrideFont;
\r
293 bool OverrideColorEnabled;
\r
294 video::SColor OverrideColor;
\r
296 u32 ClickTime, HoverTime, FocusTime;
\r
298 bool ClickShiftState;
\r
299 bool ClickControlState;
\r
303 bool UseAlphaChannel;
\r
307 video::SColor Colors[4];
\r