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
5 #ifndef __C_GUI_BUTTON_H_INCLUDED__
\r
6 #define __C_GUI_BUTTON_H_INCLUDED__
\r
8 #include "IrrCompileConfig.h"
\r
9 #ifdef _IRR_COMPILE_WITH_GUI_
\r
11 #include "IGUIButton.h"
\r
12 #include "IGUISpriteBank.h"
\r
13 #include "ITexture.h"
\r
21 class CGUIButton : public IGUIButton
\r
26 CGUIButton(IGUIEnvironment* environment, IGUIElement* parent,
\r
27 s32 id, core::rect<s32> rectangle, bool noclip=false);
\r
30 virtual ~CGUIButton();
\r
32 //! called if an event happened.
\r
33 virtual bool OnEvent(const SEvent& event) _IRR_OVERRIDE_;
\r
35 //! draws the element and its children
\r
36 virtual void draw() _IRR_OVERRIDE_;
\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
41 //! Gets the override font (if any)
\r
42 virtual IGUIFont* getOverrideFont() const _IRR_OVERRIDE_;
\r
44 //! Get the font which is used right now for drawing
\r
45 virtual IGUIFont* getActiveFont() const _IRR_OVERRIDE_;
\r
47 //! Sets another color for the button text.
\r
48 virtual void setOverrideColor(video::SColor color) _IRR_OVERRIDE_;
\r
50 //! Gets the override color
\r
51 virtual video::SColor getOverrideColor(void) const _IRR_OVERRIDE_;
\r
53 //! Gets the currently used text color
\r
54 virtual video::SColor getActiveColor() const _IRR_OVERRIDE_;
\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
59 //! Checks if an override color is enabled
\r
60 virtual bool isOverrideColorEnabled(void) const _IRR_OVERRIDE_;
\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
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
68 setImage(EGBIS_IMAGE_UP, image);
\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
74 setImage(EGBIS_IMAGE_UP, image, pos);
\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
80 setImage(EGBIS_IMAGE_DOWN, image);
\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
86 setImage(EGBIS_IMAGE_DOWN, image, pos);
\r
89 //! Sets the sprite bank used by the button
\r
90 virtual void setSpriteBank(IGUISpriteBank* bank=0) _IRR_OVERRIDE_;
\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
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
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
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
108 //! Returns if the sprite in the given state does loop
\r
109 virtual bool getSpriteLoop(EGUI_BUTTON_STATE state) const _IRR_OVERRIDE_;
\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
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
119 //! Checks whether the button is a push button
\r
120 virtual bool isPushButton() const _IRR_OVERRIDE_;
\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
125 //! Returns if the button is currently pressed
\r
126 virtual bool isPressed() const _IRR_OVERRIDE_;
\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
131 //! Checks if the button face and border are being drawn
\r
132 virtual bool isDrawingBorder() const _IRR_OVERRIDE_;
\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
137 //! Checks if the alpha channel should be used for drawing images on the button
\r
138 virtual bool isAlphaChannelUsed() const _IRR_OVERRIDE_;
\r
140 //! Sets if the button should scale the button images to fit
\r
141 virtual void setScaleImage(bool scaleImage=true) _IRR_OVERRIDE_;
\r
143 //! Checks whether the button scales the used images
\r
144 virtual bool isScalingImage() const _IRR_OVERRIDE_;
\r
146 //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
\r
147 virtual bool getClickShiftState() const _IRR_OVERRIDE_
\r
149 return ClickShiftState;
\r
152 //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event
\r
153 virtual bool getClickControlState() const _IRR_OVERRIDE_
\r
155 return ClickControlState;
\r
158 //! Writes attributes of the element.
\r
159 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const _IRR_OVERRIDE_;
\r
161 //! Reads attributes of the element
\r
162 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) _IRR_OVERRIDE_;
\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
170 struct ButtonSprite
\r
172 ButtonSprite() : Index(-1), Loop(false), Scale(false)
\r
176 bool operator==(const ButtonSprite& other) const
\r
178 return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;
\r
182 video::SColor Color;
\r
187 ButtonSprite ButtonSprites[EGBS_COUNT];
\r
188 IGUISpriteBank* SpriteBank;
\r
192 ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))
\r
196 ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))
\r
207 ButtonImage& operator=(const ButtonImage& other)
\r
209 if ( this == &other )
\r
213 other.Texture->grab();
\r
216 Texture = other.Texture;
\r
217 SourceRect = other.SourceRect;
\r
221 bool operator==(const ButtonImage& other) const
\r
223 return Texture == other.Texture && SourceRect == other.SourceRect;
\r
227 video::ITexture* Texture;
\r
228 core::rect<s32> SourceRect;
\r
231 ButtonImage ButtonImages[EGBIS_COUNT];
\r
233 IGUIFont* OverrideFont;
\r
235 bool OverrideColorEnabled;
\r
236 video::SColor OverrideColor;
\r
238 u32 ClickTime, HoverTime, FocusTime;
\r
240 bool ClickShiftState;
\r
241 bool ClickControlState;
\r
245 bool UseAlphaChannel;
\r
250 } // end namespace gui
\r
251 } // end namespace irr
\r
253 #endif // _IRR_COMPILE_WITH_GUI_
\r
255 #endif // __C_GUI_BUTTON_H_INCLUDED__
\r