Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
guiButton.h
Go to the documentation of this file.
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#pragma once
6
7#include <IGUIStaticText.h>
9#include "IGUIButton.h"
10#include "IGUISpriteBank.h"
11#include "ITexture.h"
12#include "SColor.h"
13#include "StyleSpec.h"
14
15using namespace irr;
16
18
19class GUIButton : public gui::IGUIButton
20{
21public:
22
24 GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
25 s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
26 bool noclip=false);
27
29 virtual ~GUIButton();
30
32 virtual bool OnEvent(const SEvent& event) override;
33
35 virtual void draw() override;
36
38 virtual void setOverrideFont(gui::IGUIFont* font=0) override;
39
41 virtual gui::IGUIFont* getOverrideFont() const override;
42
44 virtual gui::IGUIFont* getActiveFont() const override;
45
47 virtual void setOverrideColor(video::SColor color) override;
48
50 virtual video::SColor getOverrideColor() const override;
51
53 virtual video::SColor getActiveColor() const override;
54
56 virtual void enableOverrideColor(bool enable) override;
57
59 virtual bool isOverrideColorEnabled(void) const override;
60
61 // PATCH
63 virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
64 video::ITexture* image=nullptr,
65 const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;
66
68 virtual void setImage(video::ITexture* image=nullptr) override;
69
71 virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;
72
74 virtual void setPressedImage(video::ITexture* image=nullptr) override;
75
77 virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;
78
80 virtual void setText(const wchar_t* text) override;
81 // END PATCH
82
84 virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;
85
87
92 virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
93 video::SColor color=video::SColor(255,255,255,255),
94 bool loop=false) override;
95
97 virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;
98
100 virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;
101
103 virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;
104
108 virtual void setIsPushButton(bool isPushButton=true) override;
109
111 virtual bool isPushButton() const override;
112
114 virtual void setPressed(bool pressed=true) override;
115
117 virtual bool isPressed() const override;
118
119 // PATCH
121 bool isHovered() const;
122
124 bool isFocused() const;
125 // END PATCH
126
128 virtual void setDrawBorder(bool border=true) override;
129
131 virtual bool isDrawingBorder() const override;
132
134 virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;
135
137 virtual bool isAlphaChannelUsed() const override;
138
140 virtual void setScaleImage(bool scaleImage=true) override;
141
143 virtual bool isScalingImage() const override;
144
146 virtual bool getClickShiftState() const override
147 {
148 return ClickShiftState;
149 }
150
152 virtual bool getClickControlState() const override
153 {
154 return ClickControlState;
155 }
156
157 void setColor(video::SColor color);
158 // PATCH
160 void setFromState();
161
163 virtual void setFromStyle(const StyleSpec& style);
164
166 void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
167 // END PATCH
168
169
171 static GUIButton* addButton(gui::IGUIEnvironment *environment,
172 const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
173 IGUIElement* parent, s32 id, const wchar_t* text,
174 const wchar_t *tooltiptext=L"");
175
176protected:
177 void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
178 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
179
181
183 {
184 ButtonImage() = default;
185
187 {
188 *this = other;
189 }
190
192 {
193 if ( Texture )
194 Texture->drop();
195 }
196
198 {
199 if ( this == &other )
200 return *this;
201
202 if (other.Texture)
203 other.Texture->grab();
204 if ( Texture )
205 Texture->drop();
206 Texture = other.Texture;
207 SourceRect = other.SourceRect;
208 return *this;
209 }
210
211 bool operator==(const ButtonImage& other) const
212 {
213 return Texture == other.Texture && SourceRect == other.SourceRect;
214 }
215
216
217 video::ITexture* Texture = nullptr;
218 core::rect<s32> SourceRect = core::rect<s32>(0,0,0,0);
219 };
220
221 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;
222
223private:
224
226 {
227 bool operator==(const ButtonSprite &other) const
228 {
229 return Index == other.Index && Color == other.Color && Loop == other.Loop;
230 }
231
232 s32 Index = -1;
233 video::SColor Color;
234 bool Loop = false;
235 };
236
237 ButtonSprite ButtonSprites[gui::EGBS_COUNT];
238 gui::IGUISpriteBank* SpriteBank = nullptr;
239
240 ButtonImage ButtonImages[gui::EGBIS_COUNT];
241
242 std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
243
244 gui::IGUIFont* OverrideFont = nullptr;
245
247 video::SColor OverrideColor = video::SColor(101,255,255,255);
248
249 u32 ClickTime = 0;
250 u32 HoverTime = 0;
251 u32 FocusTime = 0;
252
253 bool ClickShiftState = false;
254 bool ClickControlState = false;
255
256 bool IsPushButton = false;
257 bool Pressed = false;
258 bool UseAlphaChannel = false;
259 bool DrawBorder = true;
260 bool ScaleImage = false;
261
262 video::SColor Colors[4];
263 // PATCH
264 bool WasHovered = false;
265 bool WasFocused = false;
267
268 gui::IGUIStaticText *StaticText;
269
270 core::rect<s32> BgMiddle;
271 core::rect<s32> Padding;
272 core::vector2d<s32> ContentOffset;
273 video::SColor BgColor = video::SColor(0xFF,0xFF,0xFF,0xFF);
274 // END PATCH
275};
Definition guiButton.h:20
virtual void setPressedImage(video::ITexture *image=nullptr) override
Sets an image which should be displayed on the button when it is in pressed state.
Definition guiButton.cpp:537
virtual video::SColor getOverrideColor() const override
Gets the override color.
Definition guiButton.cpp:490
virtual void setIsPushButton(bool isPushButton=true) override
Sets if the button should behave like a push button.
Definition guiButton.cpp:559
u32 ClickTime
Definition guiButton.h:249
virtual void setScaleImage(bool scaleImage=true) override
Sets if the button should scale the button images to fit.
Definition guiButton.cpp:61
gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const
Definition guiButton.cpp:372
virtual void setPressed(bool pressed=true) override
Sets the pressed state of the button if this is a pushbutton.
Definition guiButton.cpp:587
virtual bool isPushButton() const override
Checks whether the button is a push button.
Definition guiButton.cpp:599
virtual void setFromStyle(const StyleSpec &style)
Set element properties from a StyleSpec.
Definition guiButton.cpp:671
bool WasFocused
Definition guiButton.h:265
virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override
Returns if the sprite in the given state does loop.
Definition guiButton.cpp:112
std::array< StyleSpec, StyleSpec::NUM_STATES > Styles
Definition guiButton.h:242
bool UseAlphaChannel
Definition guiButton.h:258
virtual gui::IGUIFont * getActiveFont() const override
Get the font which is used right now for drawing.
Definition guiButton.cpp:471
void setStyles(const std::array< StyleSpec, StyleSpec::NUM_STATES > &styles)
Set the styles used for each state.
Definition guiButton.cpp:760
gui::IGUISpriteBank * SpriteBank
Definition guiButton.h:238
virtual void setText(const wchar_t *text) override
Sets the text displayed by the button.
Definition guiButton.cpp:548
bool ScaleImage
Definition guiButton.h:260
virtual void draw() override
draws the element and its children
Definition guiButton.cpp:241
core::vector2d< s32 > ContentOffset
Definition guiButton.h:272
void setColor(video::SColor color)
Definition guiButton.cpp:642
gui::IGUIFont * OverrideFont
Definition guiButton.h:244
virtual bool isPressed() const override
Returns if the button is currently pressed.
Definition guiButton.cpp:566
virtual void setOverrideColor(video::SColor color) override
Sets another color for the button text.
Definition guiButton.cpp:482
ISimpleTextureSource * getTextureSource()
Definition guiButton.h:180
bool isFocused() const
Returns if this element (or one of its direct children) is focused.
Definition guiButton.cpp:580
virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state, video::ITexture *image=nullptr, const core::rect< s32 > &sourceRect=core::rect< s32 >(0, 0, 0, 0)) override
Sets an image which should be displayed on the button when it is in the given state.
core::rect< s32 > Padding
Definition guiButton.h:271
u32 FocusTime
Definition guiButton.h:251
video::SColor BgColor
Definition guiButton.h:273
gui::IGUIStaticText * StaticText
Definition guiButton.h:268
virtual bool isOverrideColorEnabled(void) const override
Checks if an override color is enabled.
Definition guiButton.cpp:505
core::rect< s32 > BgMiddle
Definition guiButton.h:270
virtual bool OnEvent(const SEvent &event) override
called if an event happened.
Definition guiButton.cpp:118
bool OverrideColorEnabled
Definition guiButton.h:246
virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override
Get the sprite-index for the given state or -1 when no sprite is set.
Definition guiButton.cpp:100
virtual bool isScalingImage() const override
Checks whether the button scales the used images.
Definition guiButton.cpp:68
bool ClickControlState
Definition guiButton.h:254
bool ClickShiftState
Definition guiButton.h:253
bool isHovered() const
Returns if this element (or one of its direct children) is hovered.
Definition guiButton.cpp:573
static GUIButton * addButton(gui::IGUIEnvironment *environment, const core::rect< s32 > &rectangle, ISimpleTextureSource *tsrc, IGUIElement *parent, s32 id, const wchar_t *text, const wchar_t *tooltiptext=L"")
Do not drop returned handle.
Definition guiButton.cpp:626
virtual bool isDrawingBorder() const override
Checks if the button face and border are being drawn.
Definition guiButton.cpp:619
virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override
Get the sprite color for the given state. Color is only used when a sprite is set.
Definition guiButton.cpp:106
virtual void setUseAlphaChannel(bool useAlphaChannel=true) override
Sets if the alpha channel should be used for drawing images on the button (default is false)
Definition guiButton.cpp:606
virtual void setOverrideFont(gui::IGUIFont *font=0) override
sets another skin independent font. if this is set to zero, the button uses the font of the skin.
Definition guiButton.cpp:448
virtual void enableOverrideColor(bool enable) override
Sets if the button text should use the override color or the color in the gui skin.
Definition guiButton.cpp:500
virtual ~GUIButton()
destructor
Definition guiButton.cpp:50
GUIButton(gui::IGUIEnvironment *environment, gui::IGUIElement *parent, s32 id, core::rect< s32 > rectangle, ISimpleTextureSource *tsrc, bool noclip=false)
constructor
Definition guiButton.cpp:28
virtual void setSpriteBank(gui::IGUISpriteBank *bank=0) override
Sets the sprite bank used by the button.
Definition guiButton.cpp:81
bool IsPushButton
Definition guiButton.h:256
void setFromState()
Set element properties from a StyleSpec corresponding to the button state.
Definition guiButton.cpp:654
void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di &center)
Definition guiButton.cpp:347
bool DrawBorder
Definition guiButton.h:259
virtual bool isAlphaChannelUsed() const override
Checks if the alpha channel should be used for drawing images on the button.
Definition guiButton.cpp:613
virtual bool getClickShiftState() const override
Get if the shift key was pressed in last EGET_BUTTON_CLICKED event.
Definition guiButton.h:146
virtual bool getClickControlState() const override
Get if the control key was pressed in last EGET_BUTTON_CLICKED event.
Definition guiButton.h:152
u32 HoverTime
Definition guiButton.h:250
video::SColor Colors[4]
Definition guiButton.h:262
ButtonSprite ButtonSprites[gui::EGBS_COUNT]
Definition guiButton.h:237
video::SColor OverrideColor
Definition guiButton.h:247
bool Pressed
Definition guiButton.h:257
bool WasHovered
Definition guiButton.h:264
virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color=video::SColor(255, 255, 255, 255), bool loop=false) override
Sets the animated sprite for a specific button state.
Definition guiButton.cpp:92
virtual gui::IGUIFont * getOverrideFont() const override
Gets the override font (if any)
Definition guiButton.cpp:465
ISimpleTextureSource * TSrc
Definition guiButton.h:266
virtual void setDrawBorder(bool border=true) override
Sets if the button should use the skin to draw its border.
Definition guiButton.cpp:75
ButtonImage ButtonImages[gui::EGBIS_COUNT]
Definition guiButton.h:240
virtual video::SColor getActiveColor() const override
Gets the currently used text color.
Definition guiButton.cpp:495
Definition texturesource.h:25
Definition StyleSpec.h:18
Definition clientmap.h:30
Definition guiButton.h:183
core::rect< s32 > SourceRect
Definition guiButton.h:218
video::ITexture * Texture
Definition guiButton.h:217
bool operator==(const ButtonImage &other) const
Definition guiButton.h:211
ButtonImage & operator=(const ButtonImage &other)
Definition guiButton.h:197
~ButtonImage()
Definition guiButton.h:191
ButtonImage(const ButtonImage &other)
Definition guiButton.h:186
Definition guiButton.h:226
s32 Index
Definition guiButton.h:232
bool operator==(const ButtonSprite &other) const
Definition guiButton.h:227
bool Loop
Definition guiButton.h:234
video::SColor Color
Definition guiButton.h:233