Luanti 5.15.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
15
17
18class GUIButton : public gui::IGUIButton
19{
20public:
21
23 GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
24 s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
25 bool noclip=false);
26
28 virtual ~GUIButton();
29
31 virtual bool OnEvent(const SEvent& event) override;
32
34 virtual void draw() override;
35
37 virtual void setOverrideFont(gui::IGUIFont* font=0) override;
38
40 virtual gui::IGUIFont* getOverrideFont() const override;
41
43 virtual gui::IGUIFont* getActiveFont() const override;
44
46 virtual void setOverrideColor(video::SColor color) override;
47
49 virtual video::SColor getOverrideColor() const override;
50
52 virtual video::SColor getActiveColor() const override;
53
55 virtual void enableOverrideColor(bool enable) override;
56
58 virtual bool isOverrideColorEnabled(void) const override;
59
60 // PATCH
62 virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
63 video::ITexture* image=nullptr,
64 const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;
65
67 virtual void setImage(video::ITexture* image=nullptr) override;
68
70 virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;
71
73 virtual void setPressedImage(video::ITexture* image=nullptr) override;
74
76 virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;
77
79 virtual void setText(const wchar_t* text) override;
80 // END PATCH
81
83 virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;
84
86
91 virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
92 video::SColor color=video::SColor(255,255,255,255),
93 bool loop=false) override;
94
96 virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;
97
99 virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;
100
102 virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;
103
107 virtual void setIsPushButton(bool isPushButton=true) override;
108
110 virtual bool isPushButton() const override;
111
113 virtual void setPressed(bool pressed=true) override;
114
116 virtual bool isPressed() const override;
117
118 // PATCH
120 bool isHovered() const;
121
123 bool isFocused() const;
124 // END PATCH
125
127 virtual void setDrawBorder(bool border=true) override;
128
130 virtual bool isDrawingBorder() const override;
131
133 virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;
134
136 virtual bool isAlphaChannelUsed() const override;
137
139 virtual void setScaleImage(bool scaleImage=true) override;
140
142 virtual bool isScalingImage() const override;
143
145 virtual bool getClickShiftState() const override
146 {
147 return ClickShiftState;
148 }
149
151 virtual bool getClickControlState() const override
152 {
153 return ClickControlState;
154 }
155
156 void setColor(video::SColor color);
157 // PATCH
159 void setFromState();
160
162 virtual void setFromStyle(const StyleSpec& style);
163
165 void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
166 // END PATCH
167
168
170 static GUIButton* addButton(gui::IGUIEnvironment *environment,
171 const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
172 IGUIElement* parent, s32 id, const wchar_t* text,
173 const wchar_t *tooltiptext=L"");
174
175protected:
176 void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
177 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
178
180
182 {
183 ButtonImage() = default;
184
186 {
187 *this = other;
188 }
189
191 {
192 if ( Texture )
193 Texture->drop();
194 }
195
197 {
198 if ( this == &other )
199 return *this;
200
201 if (other.Texture)
202 other.Texture->grab();
203 if ( Texture )
204 Texture->drop();
205 Texture = other.Texture;
206 SourceRect = other.SourceRect;
207 return *this;
208 }
209
210 bool operator==(const ButtonImage& other) const
211 {
212 return Texture == other.Texture && SourceRect == other.SourceRect;
213 }
214
215
216 video::ITexture* Texture = nullptr;
217 core::rect<s32> SourceRect = core::rect<s32>(0,0,0,0);
218 };
219
220 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;
221
222private:
223
225 {
226 bool operator==(const ButtonSprite &other) const
227 {
228 return Index == other.Index && Color == other.Color && Loop == other.Loop;
229 }
230
231 s32 Index = -1;
232 video::SColor Color;
233 bool Loop = false;
234 };
235
236 ButtonSprite ButtonSprites[gui::EGBS_COUNT];
237 gui::IGUISpriteBank* SpriteBank = nullptr;
238
239 ButtonImage ButtonImages[gui::EGBIS_COUNT];
240
241 std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
242
243 gui::IGUIFont* OverrideFont = nullptr;
244
246 video::SColor OverrideColor = video::SColor(101,255,255,255);
247
248 u32 ClickTime = 0;
249 u32 HoverTime = 0;
250 u32 FocusTime = 0;
251
252 bool ClickShiftState = false;
253 bool ClickControlState = false;
254
255 bool IsPushButton = false;
256 bool Pressed = false;
257 bool UseAlphaChannel = false;
258 bool DrawBorder = true;
259 bool ScaleImage = false;
260
261 video::SColor Colors[4];
262 // PATCH
263 bool WasHovered = false;
264 bool WasFocused = false;
266
267 gui::IGUIStaticText *StaticText;
268
269 core::rect<s32> BgMiddle;
270 core::rect<s32> Padding;
271 core::vector2d<s32> ContentOffset;
272 video::SColor BgColor = video::SColor(0xFF,0xFF,0xFF,0xFF);
273 // END PATCH
274};
Definition guiButton.h:19
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:248
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:264
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:241
bool UseAlphaChannel
Definition guiButton.h:257
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:765
gui::IGUISpriteBank * SpriteBank
Definition guiButton.h:237
virtual void setText(const wchar_t *text) override
Sets the text displayed by the button.
Definition guiButton.cpp:548
bool ScaleImage
Definition guiButton.h:259
virtual void draw() override
draws the element and its children
Definition guiButton.cpp:241
core::vector2d< s32 > ContentOffset
Definition guiButton.h:271
void setColor(video::SColor color)
Definition guiButton.cpp:642
gui::IGUIFont * OverrideFont
Definition guiButton.h:243
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:179
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:270
u32 FocusTime
Definition guiButton.h:250
video::SColor BgColor
Definition guiButton.h:272
gui::IGUIStaticText * StaticText
Definition guiButton.h:267
virtual bool isOverrideColorEnabled(void) const override
Checks if an override color is enabled.
Definition guiButton.cpp:505
core::rect< s32 > BgMiddle
Definition guiButton.h:269
virtual bool OnEvent(const SEvent &event) override
called if an event happened.
Definition guiButton.cpp:118
bool OverrideColorEnabled
Definition guiButton.h:245
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:253
bool ClickShiftState
Definition guiButton.h:252
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:255
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:258
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:145
virtual bool getClickControlState() const override
Get if the control key was pressed in last EGET_BUTTON_CLICKED event.
Definition guiButton.h:151
u32 HoverTime
Definition guiButton.h:249
video::SColor Colors[4]
Definition guiButton.h:261
ButtonSprite ButtonSprites[gui::EGBS_COUNT]
Definition guiButton.h:236
video::SColor OverrideColor
Definition guiButton.h:246
bool Pressed
Definition guiButton.h:256
bool WasHovered
Definition guiButton.h:263
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:265
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:239
virtual video::SColor getActiveColor() const override
Gets the currently used text color.
Definition guiButton.cpp:495
Definition texturesource.h:34
Definition StyleSpec.h:18
Definition guiButton.h:182
core::rect< s32 > SourceRect
Definition guiButton.h:217
video::ITexture * Texture
Definition guiButton.h:216
bool operator==(const ButtonImage &other) const
Definition guiButton.h:210
ButtonImage & operator=(const ButtonImage &other)
Definition guiButton.h:196
~ButtonImage()
Definition guiButton.h:190
ButtonImage(const ButtonImage &other)
Definition guiButton.h:185
Definition guiButton.h:225
s32 Index
Definition guiButton.h:231
bool operator==(const ButtonSprite &other) const
Definition guiButton.h:226
bool Loop
Definition guiButton.h:233
video::SColor Color
Definition guiButton.h:232