Luanti 5.16.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 "ITexture.h"
11#include "SColor.h"
12#include "StyleSpec.h"
13
14namespace irr::gui {
15 class IGUISpriteBank;
16}
17
19
20class GUIButton : public gui::IGUIButton
21{
22public:
23
25 GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
26 s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
27 bool noclip=false);
28
30 virtual ~GUIButton();
31
33 virtual bool OnEvent(const SEvent& event) override;
34
36 virtual void draw() override;
37
39 virtual void setOverrideFont(gui::IGUIFont* font=0) override;
40
42 virtual gui::IGUIFont* getOverrideFont() const override;
43
45 virtual gui::IGUIFont* getActiveFont() const override;
46
48 virtual void setOverrideColor(video::SColor color) override;
49
51 virtual video::SColor getOverrideColor() const override;
52
54 virtual video::SColor getActiveColor() const override;
55
57 virtual void enableOverrideColor(bool enable) override;
58
60 virtual bool isOverrideColorEnabled(void) const override;
61
62 // PATCH
64 virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
65 video::ITexture* image=nullptr,
66 const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;
67
69 virtual void setImage(video::ITexture* image=nullptr) override;
70
72 virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;
73
75 virtual void setPressedImage(video::ITexture* image=nullptr) override;
76
78 virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;
79
81 virtual void setText(const wchar_t* text) override;
82 // END PATCH
83
85 virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;
86
88
93 virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
94 video::SColor color=video::SColor(255,255,255,255),
95 bool loop=false) override;
96
98 virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;
99
101 virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;
102
104 virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;
105
109 virtual void setIsPushButton(bool isPushButton=true) override;
110
112 virtual bool isPushButton() const override;
113
115 virtual void setPressed(bool pressed=true) override;
116
118 virtual bool isPressed() const override;
119
120 // PATCH
122 bool isHovered() const;
123
125 bool isFocused() const;
126 // END PATCH
127
129 virtual void setDrawBorder(bool border=true) override;
130
132 virtual bool isDrawingBorder() const override;
133
135 virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;
136
138 virtual bool isAlphaChannelUsed() const override;
139
141 virtual void setScaleImage(bool scaleImage=true) override;
142
144 virtual bool isScalingImage() const override;
145
147 virtual bool getClickShiftState() const override
148 {
149 return ClickShiftState;
150 }
151
153 virtual bool getClickControlState() const override
154 {
155 return ClickControlState;
156 }
157
158 void setColor(video::SColor color);
159 // PATCH
161 void setFromState();
162
164 virtual void setFromStyle(const StyleSpec& style);
165
167 void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
168 // END PATCH
169
170
172 static GUIButton* addButton(gui::IGUIEnvironment *environment,
173 const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
174 IGUIElement* parent, s32 id, const wchar_t* text,
175 const wchar_t *tooltiptext=L"");
176
177protected:
178 void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
179 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
180
182
184 {
185 ButtonImage() = default;
186
188 {
189 *this = other;
190 }
191
193 {
194 if ( Texture )
195 Texture->drop();
196 }
197
199 {
200 if ( this == &other )
201 return *this;
202
203 if (other.Texture)
204 other.Texture->grab();
205 if ( Texture )
206 Texture->drop();
207 Texture = other.Texture;
208 SourceRect = other.SourceRect;
209 return *this;
210 }
211
212 bool operator==(const ButtonImage& other) const
213 {
214 return Texture == other.Texture && SourceRect == other.SourceRect;
215 }
216
217
218 video::ITexture* Texture = nullptr;
219 core::rect<s32> SourceRect = core::rect<s32>(0,0,0,0);
220 };
221
222 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;
223
224private:
225
227 {
228 bool operator==(const ButtonSprite &other) const
229 {
230 return Index == other.Index && Color == other.Color && Loop == other.Loop;
231 }
232
233 s32 Index = -1;
234 video::SColor Color;
235 bool Loop = false;
236 };
237
238 ButtonSprite ButtonSprites[gui::EGBS_COUNT];
239 gui::IGUISpriteBank* SpriteBank = nullptr;
240
241 ButtonImage ButtonImages[gui::EGBIS_COUNT];
242
243 std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
244
245 gui::IGUIFont* OverrideFont = nullptr;
246
248 video::SColor OverrideColor = video::SColor(101,255,255,255);
249
250 u32 ClickTime = 0;
251 u32 HoverTime = 0;
252 u32 FocusTime = 0;
253
254 bool ClickShiftState = false;
255 bool ClickControlState = false;
256
257 bool IsPushButton = false;
258 bool Pressed = false;
259 bool UseAlphaChannel = false;
260 bool DrawBorder = true;
261 bool ScaleImage = false;
262
263 video::SColor Colors[4];
264 // PATCH
265 bool WasHovered = false;
266 bool WasFocused = false;
268
269 gui::IGUIStaticText *StaticText;
270
271 core::rect<s32> BgMiddle;
272 core::rect<s32> Padding;
273 core::vector2d<s32> ContentOffset;
274 video::SColor BgColor = video::SColor(0xFF,0xFF,0xFF,0xFF);
275 // END PATCH
276};
Definition guiButton.h:21
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:538
virtual video::SColor getOverrideColor() const override
Gets the override color.
Definition guiButton.cpp:491
virtual void setIsPushButton(bool isPushButton=true) override
Definition guiButton.cpp:560
u32 ClickTime
Definition guiButton.h:250
virtual void setScaleImage(bool scaleImage=true) override
Sets if the button should scale the button images to fit.
Definition guiButton.cpp:62
gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const
Definition guiButton.cpp:373
virtual void setPressed(bool pressed=true) override
Sets the pressed state of the button if this is a pushbutton.
Definition guiButton.cpp:588
virtual bool isPushButton() const override
Checks whether the button is a push button.
Definition guiButton.cpp:600
virtual void setFromStyle(const StyleSpec &style)
Set element properties from a StyleSpec.
Definition guiButton.cpp:672
bool WasFocused
Definition guiButton.h:266
virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override
Returns if the sprite in the given state does loop.
Definition guiButton.cpp:113
std::array< StyleSpec, StyleSpec::NUM_STATES > Styles
Definition guiButton.h:243
bool UseAlphaChannel
Definition guiButton.h:259
virtual gui::IGUIFont * getActiveFont() const override
Get the font which is used right now for drawing.
Definition guiButton.cpp:472
void setStyles(const std::array< StyleSpec, StyleSpec::NUM_STATES > &styles)
Set the styles used for each state.
Definition guiButton.cpp:766
gui::IGUISpriteBank * SpriteBank
Definition guiButton.h:239
virtual void setText(const wchar_t *text) override
Sets the text displayed by the button.
Definition guiButton.cpp:549
bool ScaleImage
Definition guiButton.h:261
virtual void draw() override
draws the element and its children
Definition guiButton.cpp:242
core::vector2d< s32 > ContentOffset
Definition guiButton.h:273
void setColor(video::SColor color)
Definition guiButton.cpp:643
gui::IGUIFont * OverrideFont
Definition guiButton.h:245
virtual bool isPressed() const override
Returns if the button is currently pressed.
Definition guiButton.cpp:567
virtual void setOverrideColor(video::SColor color) override
Sets another color for the button text.
Definition guiButton.cpp:483
ISimpleTextureSource * getTextureSource()
Definition guiButton.h:181
bool isFocused() const
Returns if this element (or one of its direct children) is focused.
Definition guiButton.cpp:581
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:272
u32 FocusTime
Definition guiButton.h:252
video::SColor BgColor
Definition guiButton.h:274
gui::IGUIStaticText * StaticText
Definition guiButton.h:269
virtual bool isOverrideColorEnabled(void) const override
Checks if an override color is enabled.
Definition guiButton.cpp:506
core::rect< s32 > BgMiddle
Definition guiButton.h:271
virtual bool OnEvent(const SEvent &event) override
called if an event happened.
Definition guiButton.cpp:119
bool OverrideColorEnabled
Definition guiButton.h:247
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:101
virtual bool isScalingImage() const override
Checks whether the button scales the used images.
Definition guiButton.cpp:69
bool ClickControlState
Definition guiButton.h:255
bool ClickShiftState
Definition guiButton.h:254
bool isHovered() const
Returns if this element (or one of its direct children) is hovered.
Definition guiButton.cpp:574
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:627
virtual bool isDrawingBorder() const override
Checks if the button face and border are being drawn.
Definition guiButton.cpp:620
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:107
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:607
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:449
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:501
virtual ~GUIButton()
destructor
Definition guiButton.cpp:51
GUIButton(gui::IGUIEnvironment *environment, gui::IGUIElement *parent, s32 id, core::rect< s32 > rectangle, ISimpleTextureSource *tsrc, bool noclip=false)
constructor
Definition guiButton.cpp:29
virtual void setSpriteBank(gui::IGUISpriteBank *bank=0) override
Sets the sprite bank used by the button.
Definition guiButton.cpp:82
bool IsPushButton
Definition guiButton.h:257
void setFromState()
Set element properties from a StyleSpec corresponding to the button state.
Definition guiButton.cpp:655
void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di &center)
Definition guiButton.cpp:348
bool DrawBorder
Definition guiButton.h:260
virtual bool isAlphaChannelUsed() const override
Checks if the alpha channel should be used for drawing images on the button.
Definition guiButton.cpp:614
virtual bool getClickShiftState() const override
Get if the shift key was pressed in last EGET_BUTTON_CLICKED event.
Definition guiButton.h:147
virtual bool getClickControlState() const override
Get if the control key was pressed in last EGET_BUTTON_CLICKED event.
Definition guiButton.h:153
u32 HoverTime
Definition guiButton.h:251
video::SColor Colors[4]
Definition guiButton.h:263
ButtonSprite ButtonSprites[gui::EGBS_COUNT]
Definition guiButton.h:238
video::SColor OverrideColor
Definition guiButton.h:248
bool Pressed
Definition guiButton.h:258
bool WasHovered
Definition guiButton.h:265
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:93
virtual gui::IGUIFont * getOverrideFont() const override
Gets the override font (if any).
Definition guiButton.cpp:466
ISimpleTextureSource * TSrc
Definition guiButton.h:267
virtual void setDrawBorder(bool border=true) override
Sets if the button should use the skin to draw its border.
Definition guiButton.cpp:76
ButtonImage ButtonImages[gui::EGBIS_COUNT]
Definition guiButton.h:241
virtual video::SColor getActiveColor() const override
Gets the currently used text color.
Definition guiButton.cpp:496
Definition texturesource.h:34
Definition StyleSpec.h:18
Definition guiButton.h:14
Definition guiButton.h:184
core::rect< s32 > SourceRect
Definition guiButton.h:219
video::ITexture * Texture
Definition guiButton.h:218
bool operator==(const ButtonImage &other) const
Definition guiButton.h:212
ButtonImage & operator=(const ButtonImage &other)
Definition guiButton.h:198
~ButtonImage()
Definition guiButton.h:192
ButtonImage(const ButtonImage &other)
Definition guiButton.h:187
Definition guiButton.h:227
s32 Index
Definition guiButton.h:233
bool operator==(const ButtonSprite &other) const
Definition guiButton.h:228
bool Loop
Definition guiButton.h:235
video::SColor Color
Definition guiButton.h:234