1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Add focused styling to buttons (#13414)

This commit is contained in:
rubenwardy 2023-04-14 00:09:29 +01:00 committed by GitHub
parent 2a1bc82887
commit 9d1ae80e89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 8 deletions

View file

@ -61,13 +61,16 @@ public:
NUM_PROPERTIES,
NONE
};
enum State
// State is a bitfield, it's possible to have multiple of these at once
enum State : u8
{
STATE_DEFAULT = 0,
STATE_HOVERED = 1 << 0,
STATE_PRESSED = 1 << 1,
NUM_STATES = 1 << 2,
STATE_INVALID = 1 << 3,
STATE_FOCUSED = 1 << 0,
STATE_HOVERED = 1 << 1,
STATE_PRESSED = 1 << 2,
NUM_STATES = 1 << 3, // This includes all permutations
STATE_INVALID = 1 << 4,
};
private:
@ -150,6 +153,8 @@ public:
{
if (name == "default") {
return STATE_DEFAULT;
} else if (name == "focused") {
return STATE_FOCUSED;
} else if (name == "hovered") {
return STATE_HOVERED;
} else if (name == "pressed") {

View file

@ -200,6 +200,7 @@ bool GUIButton::OnEvent(const SEvent& event)
// mouse is outside of the formspec. Thus, we test the position here.
if ( !IsPushButton && AbsoluteClippingRect.isPointInside(
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y ))) {
Environment->setFocus(this);
setPressed(true);
}
@ -258,8 +259,10 @@ void GUIButton::draw()
// PATCH
// Track hovered state, if it has changed then we need to update the style.
bool hovered = isHovered();
if (hovered != WasHovered) {
bool focused = isFocused();
if (hovered != WasHovered || focused != WasFocused) {
WasHovered = hovered;
WasFocused = focused;
setFromState();
}
@ -387,7 +390,7 @@ EGUI_BUTTON_IMAGE_STATE GUIButton::getImageState(bool pressed, const ButtonImage
{
// figure state we should have
EGUI_BUTTON_IMAGE_STATE state = EGBIS_IMAGE_DISABLED;
bool focused = Environment->hasFocus((IGUIElement*)this);
bool focused = isFocused();
bool mouseOver = isHovered();
if (isEnabled())
{
@ -582,6 +585,12 @@ bool GUIButton::isHovered() const
IGUIElement *hovered = Environment->getHovered();
return hovered == this || (hovered != nullptr && hovered->getParent() == this);
}
//! Returns if this element (or one of its direct children) is focused
bool GUIButton::isFocused() const
{
return Environment->hasFocus((IGUIElement*)this, true);
}
// END PATCH
//! Sets the pressed state of the button if this is a pushbutton
@ -662,6 +671,9 @@ void GUIButton::setFromState()
if (isHovered())
state = static_cast<StyleSpec::State>(state | StyleSpec::STATE_HOVERED);
if (isFocused())
state = static_cast<StyleSpec::State>(state | StyleSpec::STATE_FOCUSED);
setFromStyle(StyleSpec::getStyleFromStatePropagation(Styles, state));
}

View file

@ -123,6 +123,9 @@ public:
// PATCH
//! Returns if this element (or one of its direct children) is hovered
bool isHovered() const;
//! Returns if this element (or one of its direct children) is focused
bool isFocused() const;
// END PATCH
//! Sets if the button should use the skin to draw its border
@ -268,6 +271,7 @@ private:
video::SColor Colors[4];
// PATCH
bool WasHovered = false;
bool WasFocused = false;
ISimpleTextureSource *TSrc;
gui::IGUIStaticText *StaticText;