Minetest 5.9.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 "guiSkin.h"
14#include "StyleSpec.h"
15
16using namespace irr;
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, bool scale=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
107 virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const override;
108
112 virtual void setIsPushButton(bool isPushButton=true) override;
113
115 virtual bool isPushButton() const override;
116
118 virtual void setPressed(bool pressed=true) override;
119
121 virtual bool isPressed() const override;
122
123 // PATCH
125 bool isHovered() const;
126
128 bool isFocused() const;
129 // END PATCH
130
132 virtual void setDrawBorder(bool border=true) override;
133
135 virtual bool isDrawingBorder() const override;
136
138 virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;
139
141 virtual bool isAlphaChannelUsed() const override;
142
144 virtual void setScaleImage(bool scaleImage=true) override;
145
147 virtual bool isScalingImage() const override;
148
150 virtual bool getClickShiftState() const override
151 {
152 return ClickShiftState;
153 }
154
156 virtual bool getClickControlState() const override
157 {
158 return ClickControlState;
159 }
160
161 void setColor(video::SColor color);
162 // PATCH
164 void setFromState();
165
167 virtual void setFromStyle(const StyleSpec& style);
168
170 void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
171 // END PATCH
172
173
175 static GUIButton* addButton(gui::IGUIEnvironment *environment,
176 const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
177 IGUIElement* parent, s32 id, const wchar_t* text,
178 const wchar_t *tooltiptext=L"");
179
180protected:
181 void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
182 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
183
185
187 {
188 ButtonImage() = default;
189
191 {
192 *this = other;
193 }
194
196 {
197 if ( Texture )
198 Texture->drop();
199 }
200
202 {
203 if ( this == &other )
204 return *this;
205
206 if (other.Texture)
207 other.Texture->grab();
208 if ( Texture )
209 Texture->drop();
210 Texture = other.Texture;
211 SourceRect = other.SourceRect;
212 return *this;
213 }
214
215 bool operator==(const ButtonImage& other) const
216 {
217 return Texture == other.Texture && SourceRect == other.SourceRect;
218 }
219
220
221 video::ITexture* Texture = nullptr;
222 core::rect<s32> SourceRect = core::rect<s32>(0,0,0,0);
223 };
224
225 gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;
226
227private:
228
230 {
231 bool operator==(const ButtonSprite &other) const
232 {
233 return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;
234 }
235
236 s32 Index = -1;
237 video::SColor Color;
238 bool Loop = false;
239 bool Scale = false;
240 };
241
242 ButtonSprite ButtonSprites[gui::EGBS_COUNT];
243 gui::IGUISpriteBank* SpriteBank = nullptr;
244
245 ButtonImage ButtonImages[gui::EGBIS_COUNT];
246
247 std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
248
249 gui::IGUIFont* OverrideFont = nullptr;
250
252 video::SColor OverrideColor = video::SColor(101,255,255,255);
253
254 u32 ClickTime = 0;
255 u32 HoverTime = 0;
256 u32 FocusTime = 0;
257
258 bool ClickShiftState = false;
259 bool ClickControlState = false;
260
261 bool IsPushButton = false;
262 bool Pressed = false;
263 bool UseAlphaChannel = false;
264 bool DrawBorder = true;
265 bool ScaleImage = false;
266
267 video::SColor Colors[4];
268 // PATCH
269 bool WasHovered = false;
270 bool WasFocused = false;
272
273 gui::IGUIStaticText *StaticText;
274
275 core::rect<s32> BgMiddle;
276 core::rect<s32> Padding;
277 core::vector2d<s32> ContentOffset;
278 video::SColor BgColor = video::SColor(0xFF,0xFF,0xFF,0xFF);
279 // END PATCH
280};
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:541
virtual video::SColor getOverrideColor() const override
Gets the override color.
Definition: guiButton.cpp:494
virtual void setIsPushButton(bool isPushButton=true) override
Sets if the button should behave like a push button.
Definition: guiButton.cpp:563
u32 ClickTime
Definition: guiButton.h:254
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:376
virtual void setPressed(bool pressed=true) override
Sets the pressed state of the button if this is a pushbutton.
Definition: guiButton.cpp:591
virtual bool isPushButton() const override
Checks whether the button is a push button.
Definition: guiButton.cpp:603
virtual void setFromStyle(const StyleSpec &style)
Set element properties from a StyleSpec.
Definition: guiButton.cpp:675
bool WasFocused
Definition: guiButton.h:270
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:247
bool UseAlphaChannel
Definition: guiButton.h:263
virtual gui::IGUIFont * getActiveFont() const override
Get the font which is used right now for drawing.
Definition: guiButton.cpp:475
void setStyles(const std::array< StyleSpec, StyleSpec::NUM_STATES > &styles)
Set the styles used for each state.
Definition: guiButton.cpp:764
gui::IGUISpriteBank * SpriteBank
Definition: guiButton.h:243
virtual void setText(const wchar_t *text) override
Sets the text displayed by the button.
Definition: guiButton.cpp:552
bool ScaleImage
Definition: guiButton.h:265
virtual void draw() override
draws the element and its children
Definition: guiButton.cpp:248
core::vector2d< s32 > ContentOffset
Definition: guiButton.h:277
void setColor(video::SColor color)
Definition: guiButton.cpp:646
gui::IGUIFont * OverrideFont
Definition: guiButton.h:249
virtual bool isPressed() const override
Returns if the button is currently pressed.
Definition: guiButton.cpp:570
virtual void setOverrideColor(video::SColor color) override
Sets another color for the button text.
Definition: guiButton.cpp:486
ISimpleTextureSource * getTextureSource()
Definition: guiButton.h:184
virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const override
Returns if the sprite in the given state is scaled.
Definition: guiButton.cpp:119
bool isFocused() const
Returns if this element (or one of its direct children) is focused.
Definition: guiButton.cpp:584
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:276
u32 FocusTime
Definition: guiButton.h:256
video::SColor BgColor
Definition: guiButton.h:278
gui::IGUIStaticText * StaticText
Definition: guiButton.h:273
virtual bool isOverrideColorEnabled(void) const override
Checks if an override color is enabled.
Definition: guiButton.cpp:509
core::rect< s32 > BgMiddle
Definition: guiButton.h:275
virtual bool OnEvent(const SEvent &event) override
called if an event happened.
Definition: guiButton.cpp:125
bool OverrideColorEnabled
Definition: guiButton.h:251
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:68
bool ClickControlState
Definition: guiButton.h:259
bool ClickShiftState
Definition: guiButton.h:258
bool isHovered() const
Returns if this element (or one of its direct children) is hovered.
Definition: guiButton.cpp:577
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:630
virtual bool isDrawingBorder() const override
Checks if the button face and border are being drawn.
Definition: guiButton.cpp:623
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:610
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:452
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:504
virtual ~GUIButton()
destructor
Definition: guiButton.cpp:50
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:261
void setFromState()
Set element properties from a StyleSpec corresponding to the button state.
Definition: guiButton.cpp:658
void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di &center)
Definition: guiButton.cpp:354
bool DrawBorder
Definition: guiButton.h:264
virtual bool isAlphaChannelUsed() const override
Checks if the alpha channel should be used for drawing images on the button.
Definition: guiButton.cpp:617
virtual bool getClickShiftState() const override
Get if the shift key was pressed in last EGET_BUTTON_CLICKED event.
Definition: guiButton.h:150
virtual bool getClickControlState() const override
Get if the control key was pressed in last EGET_BUTTON_CLICKED event.
Definition: guiButton.h:156
u32 HoverTime
Definition: guiButton.h:255
video::SColor Colors[4]
Definition: guiButton.h:267
ButtonSprite ButtonSprites[gui::EGBS_COUNT]
Definition: guiButton.h:242
video::SColor OverrideColor
Definition: guiButton.h:252
bool Pressed
Definition: guiButton.h:262
bool WasHovered
Definition: guiButton.h:269
virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color=video::SColor(255, 255, 255, 255), bool loop=false, bool scale=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:469
ISimpleTextureSource * TSrc
Definition: guiButton.h:271
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:245
virtual video::SColor getActiveColor() const override
Gets the currently used text color.
Definition: guiButton.cpp:499
Definition: texturesource.h:34
Definition: StyleSpec.h:32
Definition: shader.h:61
Definition: guiButton.h:187
core::rect< s32 > SourceRect
Definition: guiButton.h:222
video::ITexture * Texture
Definition: guiButton.h:221
bool operator==(const ButtonImage &other) const
Definition: guiButton.h:215
ButtonImage & operator=(const ButtonImage &other)
Definition: guiButton.h:201
~ButtonImage()
Definition: guiButton.h:195
ButtonImage(const ButtonImage &other)
Definition: guiButton.h:190
Definition: guiButton.h:230
s32 Index
Definition: guiButton.h:236
bool operator==(const ButtonSprite &other) const
Definition: guiButton.h:231
bool Loop
Definition: guiButton.h:238
bool Scale
Definition: guiButton.h:239
video::SColor Color
Definition: guiButton.h:237