2019-03-15 19:39:23 +01:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-12-09 21:06:51 +01:00
|
|
|
#include <IGUIStaticText.h>
|
|
|
|
#include "irrlicht_changes/static_text.h"
|
2019-03-15 19:39:23 +01:00
|
|
|
#include "IGUIButton.h"
|
|
|
|
#include "IGUISpriteBank.h"
|
|
|
|
#include "ITexture.h"
|
|
|
|
#include "SColor.h"
|
|
|
|
#include "guiSkin.h"
|
2020-04-11 22:39:30 +02:00
|
|
|
#include "StyleSpec.h"
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
using namespace irr;
|
|
|
|
|
2019-12-09 21:06:51 +01:00
|
|
|
class ISimpleTextureSource;
|
|
|
|
|
2019-03-15 19:39:23 +01:00
|
|
|
class GUIButton : public gui::IGUIButton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
//! constructor
|
|
|
|
GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
|
2020-04-11 22:39:30 +02:00
|
|
|
s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
|
|
|
|
bool noclip=false);
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! destructor
|
|
|
|
virtual ~GUIButton();
|
|
|
|
|
|
|
|
//! called if an event happened.
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual bool OnEvent(const SEvent& event) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! draws the element and its children
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void draw() override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! sets another skin independent font. if this is set to zero, the button uses the font of the skin.
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setOverrideFont(gui::IGUIFont* font=0) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Gets the override font (if any)
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual gui::IGUIFont* getOverrideFont() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Get the font which is used right now for drawing
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual gui::IGUIFont* getActiveFont() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets another color for the button text.
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual void setOverrideColor(video::SColor color) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Gets the override color
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual video::SColor getOverrideColor() const override;
|
2020-05-17 01:03:33 +02:00
|
|
|
|
|
|
|
//! Gets the currently used text color
|
|
|
|
virtual video::SColor getActiveColor() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets if the button text should use the override color or the color in the gui skin.
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual void enableOverrideColor(bool enable) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Checks if an override color is enabled
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual bool isOverrideColorEnabled(void) const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2019-10-12 18:44:23 +02:00
|
|
|
// PATCH
|
2019-03-15 19:39:23 +01:00
|
|
|
//! Sets an image which should be displayed on the button when it is in the given state.
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
|
2019-10-12 18:44:23 +02:00
|
|
|
video::ITexture* image=nullptr,
|
2023-02-18 00:29:34 +01:00
|
|
|
const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets an image which should be displayed on the button when it is in normal state.
|
2019-10-12 18:44:23 +02:00
|
|
|
virtual void setImage(video::ITexture* image=nullptr) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets an image which should be displayed on the button when it is in normal state.
|
2019-10-12 18:44:23 +02:00
|
|
|
virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets an image which should be displayed on the button when it is in pressed state.
|
2019-10-12 18:44:23 +02:00
|
|
|
virtual void setPressedImage(video::ITexture* image=nullptr) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets an image which should be displayed on the button when it is in pressed state.
|
2019-10-12 18:44:23 +02:00
|
|
|
virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;
|
|
|
|
|
2019-12-09 21:06:51 +01:00
|
|
|
//! Sets the text displayed by the button
|
|
|
|
virtual void setText(const wchar_t* text) override;
|
2019-10-12 18:44:23 +02:00
|
|
|
// END PATCH
|
|
|
|
|
2019-03-15 19:39:23 +01:00
|
|
|
//! Sets the sprite bank used by the button
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets the animated sprite for a specific button state
|
|
|
|
/** \param index: Number of the sprite within the sprite bank, use -1 for no sprite
|
|
|
|
\param state: State of the button to set the sprite for
|
|
|
|
\param index: The sprite number from the current sprite bank
|
|
|
|
\param color: The color of the sprite
|
|
|
|
*/
|
|
|
|
virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
|
|
|
|
video::SColor color=video::SColor(255,255,255,255),
|
2023-02-18 00:29:34 +01:00
|
|
|
bool loop=false, bool scale=false) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Get the sprite-index for the given state or -1 when no sprite is set
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Get the sprite color for the given state. Color is only used when a sprite is set.
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Returns if the sprite in the given state does loop
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Returns if the sprite in the given state is scaled
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets if the button should behave like a push button. Which means it
|
|
|
|
//! can be in two states: Normal or Pressed. With a click on the button,
|
|
|
|
//! the user can change the state of the button.
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setIsPushButton(bool isPushButton=true) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Checks whether the button is a push button
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual bool isPushButton() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets the pressed state of the button if this is a pushbutton
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setPressed(bool pressed=true) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Returns if the button is currently pressed
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual bool isPressed() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2019-12-09 21:06:51 +01:00
|
|
|
// PATCH
|
|
|
|
//! Returns if this element (or one of its direct children) is hovered
|
|
|
|
bool isHovered() const;
|
2023-04-14 01:09:29 +02:00
|
|
|
|
|
|
|
//! Returns if this element (or one of its direct children) is focused
|
|
|
|
bool isFocused() const;
|
2019-12-09 21:06:51 +01:00
|
|
|
// END PATCH
|
|
|
|
|
2019-03-15 19:39:23 +01:00
|
|
|
//! Sets if the button should use the skin to draw its border
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setDrawBorder(bool border=true) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Checks if the button face and border are being drawn
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual bool isDrawingBorder() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets if the alpha channel should be used for drawing images on the button (default is false)
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Checks if the alpha channel should be used for drawing images on the button
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual bool isAlphaChannelUsed() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Sets if the button should scale the button images to fit
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual void setScaleImage(bool scaleImage=true) override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Checks whether the button scales the used images
|
2019-10-05 17:13:50 +02:00
|
|
|
virtual bool isScalingImage() const override;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
//! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual bool getClickShiftState() const override
|
2019-03-15 19:39:23 +01:00
|
|
|
{
|
|
|
|
return ClickShiftState;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get if the control key was pressed in last EGET_BUTTON_CLICKED event
|
2023-02-18 00:29:34 +01:00
|
|
|
virtual bool getClickControlState() const override
|
2019-03-15 19:39:23 +01:00
|
|
|
{
|
|
|
|
return ClickControlState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setColor(video::SColor color);
|
2019-10-12 18:44:23 +02:00
|
|
|
// PATCH
|
2020-04-11 22:39:30 +02:00
|
|
|
//! Set element properties from a StyleSpec corresponding to the button state
|
|
|
|
void setFromState();
|
2019-12-09 21:06:51 +01:00
|
|
|
|
|
|
|
//! Set element properties from a StyleSpec
|
2020-04-11 22:39:30 +02:00
|
|
|
virtual void setFromStyle(const StyleSpec& style);
|
|
|
|
|
|
|
|
//! Set the styles used for each state
|
|
|
|
void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
|
2019-10-12 18:44:23 +02:00
|
|
|
// END PATCH
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
//! Do not drop returned handle
|
2020-04-11 22:39:30 +02:00
|
|
|
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"");
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
|
|
|
|
gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
|
|
|
|
|
2020-04-11 22:39:30 +02:00
|
|
|
ISimpleTextureSource *getTextureSource() { return TSrc; }
|
|
|
|
|
2019-03-15 19:39:23 +01:00
|
|
|
struct ButtonImage
|
|
|
|
{
|
2023-08-12 18:03:00 +02:00
|
|
|
ButtonImage() = default;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
ButtonImage(const ButtonImage& other)
|
2019-03-15 19:39:23 +01:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ButtonImage()
|
|
|
|
{
|
|
|
|
if ( Texture )
|
|
|
|
Texture->drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonImage& operator=(const ButtonImage& other)
|
|
|
|
{
|
|
|
|
if ( this == &other )
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (other.Texture)
|
|
|
|
other.Texture->grab();
|
|
|
|
if ( Texture )
|
|
|
|
Texture->drop();
|
|
|
|
Texture = other.Texture;
|
|
|
|
SourceRect = other.SourceRect;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ButtonImage& other) const
|
|
|
|
{
|
|
|
|
return Texture == other.Texture && SourceRect == other.SourceRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
video::ITexture* Texture = nullptr;
|
|
|
|
core::rect<s32> SourceRect = core::rect<s32>(0,0,0,0);
|
2019-03-15 19:39:23 +01:00
|
|
|
};
|
|
|
|
|
2019-12-09 21:06:51 +01:00
|
|
|
gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
struct ButtonSprite
|
|
|
|
{
|
2023-08-12 18:03:00 +02:00
|
|
|
bool operator==(const ButtonSprite &other) const
|
2019-12-09 21:06:51 +01:00
|
|
|
{
|
|
|
|
return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;
|
|
|
|
}
|
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
s32 Index = -1;
|
2019-12-09 21:06:51 +01:00
|
|
|
video::SColor Color;
|
2023-08-12 18:03:00 +02:00
|
|
|
bool Loop = false;
|
|
|
|
bool Scale = false;
|
2019-12-09 21:06:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ButtonSprite ButtonSprites[gui::EGBS_COUNT];
|
2023-08-12 18:03:00 +02:00
|
|
|
gui::IGUISpriteBank* SpriteBank = nullptr;
|
2019-12-09 21:06:51 +01:00
|
|
|
|
2019-03-15 19:39:23 +01:00
|
|
|
ButtonImage ButtonImages[gui::EGBIS_COUNT];
|
|
|
|
|
2020-04-11 22:39:30 +02:00
|
|
|
std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
|
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
gui::IGUIFont* OverrideFont = nullptr;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
bool OverrideColorEnabled = false;
|
|
|
|
video::SColor OverrideColor = video::SColor(101,255,255,255);
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
u32 ClickTime = 0;
|
|
|
|
u32 HoverTime = 0;
|
|
|
|
u32 FocusTime = 0;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
bool ClickShiftState = false;
|
|
|
|
bool ClickControlState = false;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
2023-08-12 18:03:00 +02:00
|
|
|
bool IsPushButton = false;
|
|
|
|
bool Pressed = false;
|
|
|
|
bool UseAlphaChannel = false;
|
|
|
|
bool DrawBorder = true;
|
|
|
|
bool ScaleImage = false;
|
2019-03-15 19:39:23 +01:00
|
|
|
|
|
|
|
video::SColor Colors[4];
|
2019-10-12 18:44:23 +02:00
|
|
|
// PATCH
|
2020-04-11 22:39:30 +02:00
|
|
|
bool WasHovered = false;
|
2023-04-14 01:09:29 +02:00
|
|
|
bool WasFocused = false;
|
2020-04-11 22:39:30 +02:00
|
|
|
ISimpleTextureSource *TSrc;
|
2019-12-09 21:06:51 +01:00
|
|
|
|
|
|
|
gui::IGUIStaticText *StaticText;
|
2020-01-26 20:35:26 +01:00
|
|
|
|
|
|
|
core::rect<s32> BgMiddle;
|
2020-05-02 13:32:02 +02:00
|
|
|
core::rect<s32> Padding;
|
|
|
|
core::vector2d<s32> ContentOffset;
|
2023-08-12 18:10:19 +02:00
|
|
|
video::SColor BgColor = video::SColor(0xFF,0xFF,0xFF,0xFF);
|
2019-10-12 18:44:23 +02:00
|
|
|
// END PATCH
|
2019-03-15 19:39:23 +01:00
|
|
|
};
|