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 <IGUIStaticText.h>
\r
8 #include "irrlicht_changes/static_text.h"
\r
9 #include "IGUIButton.h"
\r
10 #include "IGUISpriteBank.h"
\r
11 #include "ITexture.h"
\r
13 #include "guiSkin.h"
\r
14 #include "StyleSpec.h"
\r
16 using namespace irr;
\r
18 class ISimpleTextureSource;
\r
20 class GUIButton : public gui::IGUIButton
\r
25 GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
\r
26 s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
\r
30 virtual ~GUIButton();
\r
32 //! called if an event happened.
\r
33 virtual bool OnEvent(const SEvent& event) override;
\r
35 //! draws the element and its children
\r
36 virtual void draw() 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(gui::IGUIFont* font=0) override;
\r
41 //! Gets the override font (if any)
\r
42 virtual gui::IGUIFont* getOverrideFont() const override;
\r
44 //! Get the font which is used right now for drawing
\r
45 virtual gui::IGUIFont* getActiveFont() const override;
\r
47 //! Sets another color for the button text.
\r
48 virtual void setOverrideColor(video::SColor color) override;
\r
50 //! Gets the override color
\r
51 virtual video::SColor getOverrideColor() const override;
\r
53 //! Gets the currently used text color
\r
54 virtual video::SColor getActiveColor() const 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) override;
\r
59 //! Checks if an override color is enabled
\r
60 virtual bool isOverrideColorEnabled(void) const override;
\r
63 //! Sets an image which should be displayed on the button when it is in the given state.
\r
64 virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
\r
65 video::ITexture* image=nullptr,
\r
66 const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;
\r
68 //! Sets an image which should be displayed on the button when it is in normal state.
\r
69 virtual void setImage(video::ITexture* image=nullptr) override;
\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) override;
\r
74 //! Sets an image which should be displayed on the button when it is in pressed state.
\r
75 virtual void setPressedImage(video::ITexture* image=nullptr) override;
\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, const core::rect<s32>& pos) override;
\r
80 //! Sets the text displayed by the button
\r
81 virtual void setText(const wchar_t* text) override;
\r
84 //! Sets the sprite bank used by the button
\r
85 virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;
\r
87 //! Sets the animated sprite for a specific button state
\r
88 /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite
\r
89 \param state: State of the button to set the sprite for
\r
90 \param index: The sprite number from the current sprite bank
\r
91 \param color: The color of the sprite
\r
93 virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
\r
94 video::SColor color=video::SColor(255,255,255,255),
\r
95 bool loop=false, bool scale=false) override;
\r
97 //! Get the sprite-index for the given state or -1 when no sprite is set
\r
98 virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;
\r
100 //! Get the sprite color for the given state. Color is only used when a sprite is set.
\r
101 virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;
\r
103 //! Returns if the sprite in the given state does loop
\r
104 virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;
\r
106 //! Returns if the sprite in the given state is scaled
\r
107 virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const override;
\r
109 //! Sets if the button should behave like a push button. Which means it
\r
110 //! can be in two states: Normal or Pressed. With a click on the button,
\r
111 //! the user can change the state of the button.
\r
112 virtual void setIsPushButton(bool isPushButton=true) override;
\r
114 //! Checks whether the button is a push button
\r
115 virtual bool isPushButton() const override;
\r
117 //! Sets the pressed state of the button if this is a pushbutton
\r
118 virtual void setPressed(bool pressed=true) override;
\r
120 //! Returns if the button is currently pressed
\r
121 virtual bool isPressed() const override;
\r
124 //! Returns if this element (or one of its direct children) is hovered
\r
125 bool isHovered() const;
\r
128 //! Sets if the button should use the skin to draw its border
\r
129 virtual void setDrawBorder(bool border=true) override;
\r
131 //! Checks if the button face and border are being drawn
\r
132 virtual bool isDrawingBorder() const 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) override;
\r
137 //! Checks if the alpha channel should be used for drawing images on the button
\r
138 virtual bool isAlphaChannelUsed() const override;
\r
140 //! Sets if the button should scale the button images to fit
\r
141 virtual void setScaleImage(bool scaleImage=true) override;
\r
143 //! Checks whether the button scales the used images
\r
144 virtual bool isScalingImage() const override;
\r
146 //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
\r
147 virtual bool getClickShiftState() const 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 override
\r
155 return ClickControlState;
\r
158 void setColor(video::SColor color);
\r
160 //! Set element properties from a StyleSpec corresponding to the button state
\r
161 void setFromState();
\r
163 //! Set element properties from a StyleSpec
\r
164 virtual void setFromStyle(const StyleSpec& style);
\r
166 //! Set the styles used for each state
\r
167 void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
\r
171 //! Do not drop returned handle
\r
172 static GUIButton* addButton(gui::IGUIEnvironment *environment,
\r
173 const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
\r
174 IGUIElement* parent, s32 id, const wchar_t* text,
\r
175 const wchar_t *tooltiptext=L"");
\r
178 void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
\r
179 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
\r
181 ISimpleTextureSource *getTextureSource() { return TSrc; }
\r
185 ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))
\r
189 ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))
\r
200 ButtonImage& operator=(const ButtonImage& other)
\r
202 if ( this == &other )
\r
206 other.Texture->grab();
\r
209 Texture = other.Texture;
\r
210 SourceRect = other.SourceRect;
\r
214 bool operator==(const ButtonImage& other) const
\r
216 return Texture == other.Texture && SourceRect == other.SourceRect;
\r
220 video::ITexture* Texture;
\r
221 core::rect<s32> SourceRect;
\r
224 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) 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
248 ButtonImage ButtonImages[gui::EGBIS_COUNT];
\r
250 std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
\r
252 gui::IGUIFont* OverrideFont;
\r
254 bool OverrideColorEnabled;
\r
255 video::SColor OverrideColor;
\r
257 u32 ClickTime, HoverTime, FocusTime;
\r
259 bool ClickShiftState;
\r
260 bool ClickControlState;
\r
264 bool UseAlphaChannel;
\r
268 video::SColor Colors[4];
\r
270 bool WasHovered = false;
\r
271 ISimpleTextureSource *TSrc;
\r
273 gui::IGUIStaticText *StaticText;
\r
275 core::rect<s32> BgMiddle;
\r
276 core::rect<s32> Padding;
\r
277 core::vector2d<s32> ContentOffset;
\r
278 video::SColor BgColor;
\r