From 8299a896d7658ffe86272ea0b21b8e5f7c600510 Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 20 Mar 2024 22:48:47 +0000 Subject: [PATCH] Add selected button state --- .../classes/GFXButtonStateTexture.cpp | 44 ++++++++++++------- .../classes/GFXButtonStateTexture.hpp | 5 ++- .../classes/GUIScrollbar.cpp | 2 +- .../src/openvic-extension/utility/UITools.cpp | 4 +- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp b/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp index 7cbc0a65..f6f3c22f 100644 --- a/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp @@ -39,14 +39,15 @@ void GFXButtonStateTexture::_bind_methods() { OV_BIND_METHOD(GFXButtonStateTexture::set_button_state, { "new_button_state" }); OV_BIND_METHOD(GFXButtonStateTexture::get_button_state); - OV_BIND_SMETHOD(button_state_to_theme_name, { "button_state" }); - OV_BIND_METHOD(GFXButtonStateTexture::get_button_state_theme); + OV_BIND_SMETHOD(button_state_to_name, { "button_state" }); + OV_BIND_METHOD(GFXButtonStateTexture::get_button_state_name); OV_BIND_METHOD(GFXButtonStateTexture::generate_state_image, { "source_image" }); BIND_ENUM_CONSTANT(HOVER); BIND_ENUM_CONSTANT(PRESSED); BIND_ENUM_CONSTANT(DISABLED); + BIND_ENUM_CONSTANT(SELECTED); } GFXButtonStateTexture::GFXButtonStateTexture() : button_state { HOVER }, state_image {}, state_texture {} {} @@ -67,7 +68,10 @@ Ref GFXButtonStateTexture::make_gfx_button_state_texture( } void GFXButtonStateTexture::set_button_state(ButtonState new_button_state) { - ERR_FAIL_COND(new_button_state != HOVER && new_button_state != PRESSED && new_button_state != DISABLED); + ERR_FAIL_COND( + new_button_state != HOVER && new_button_state != PRESSED && + new_button_state != DISABLED && new_button_state != SELECTED + ); button_state = new_button_state; } @@ -106,8 +110,15 @@ Error GFXButtonStateTexture::generate_state_image( const float luma = colour.get_luminance(); return { luma, luma, luma, colour.a }; }; + static constexpr auto selected_colour = [](Color const& colour) -> Color { + return { std::max(colour.r - 0.3f, 0.0f), std::max(colour.g - 0.3f, 0.0f), std::max(colour.b - 0.3f, 0.0f), colour.a }; + }; - const auto colour_func = button_state == HOVER ? hover_colour : button_state == PRESSED ? pressed_colour : disabled_colour; + const auto colour_func = + button_state == HOVER ? hover_colour : + button_state == PRESSED ? pressed_colour : + button_state == DISABLED ? disabled_colour : + selected_colour; for (Vector2i point { 0, 0 }; point.y < state_image->get_height(); ++point.y) { for (point.x = 0; point.x < state_image->get_width(); ++point.x) { @@ -123,25 +134,28 @@ Error GFXButtonStateTexture::generate_state_image( return OK; } -StringName const& GFXButtonStateTexture::button_state_to_theme_name(ButtonState button_state) { - static const StringName theme_name_hover = "hover"; - static const StringName theme_name_pressed = "pressed"; - static const StringName theme_name_disabled = "disabled"; - static const StringName theme_name_error = "INVALID BUTTON STATE"; +StringName const& GFXButtonStateTexture::button_state_to_name(ButtonState button_state) { + static const StringName name_hover = "hover"; + static const StringName name_pressed = "pressed"; + static const StringName name_disabled = "disabled"; + static const StringName name_selected = "selected"; + static const StringName name_error = "INVALID BUTTON STATE"; switch (button_state) { case HOVER: - return theme_name_hover; + return name_hover; case PRESSED: - return theme_name_pressed; + return name_pressed; case DISABLED: - return theme_name_disabled; + return name_disabled; + case SELECTED: + return name_selected; default: - return theme_name_error; + return name_error; } } -StringName const& GFXButtonStateTexture::get_button_state_theme() const { - return button_state_to_theme_name(button_state); +StringName const& GFXButtonStateTexture::get_button_state_name() const { + return button_state_to_name(button_state); } void GFXButtonStateHavingTexture::_bind_methods() { diff --git a/extension/src/openvic-extension/classes/GFXButtonStateTexture.hpp b/extension/src/openvic-extension/classes/GFXButtonStateTexture.hpp index b57ea460..d97ca2bf 100644 --- a/extension/src/openvic-extension/classes/GFXButtonStateTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXButtonStateTexture.hpp @@ -32,6 +32,7 @@ namespace OpenVic { HOVER, PRESSED, DISABLED, + SELECTED, BUTTON_STATE_COUNT }; @@ -62,8 +63,8 @@ namespace OpenVic { godot::Vector2i const& new_cornered_tile_border_size ); - static godot::StringName const& button_state_to_theme_name(ButtonState button_state); - godot::StringName const& get_button_state_theme() const; + static godot::StringName const& button_state_to_name(ButtonState button_state); + godot::StringName const& get_button_state_name() const; }; class GFXButtonStateHavingTexture : public GFXCorneredTileSupportingTexture { diff --git a/extension/src/openvic-extension/classes/GUIScrollbar.cpp b/extension/src/openvic-extension/classes/GUIScrollbar.cpp index 93eb00ba..cdfa2fd2 100644 --- a/extension/src/openvic-extension/classes/GUIScrollbar.cpp +++ b/extension/src/openvic-extension/classes/GUIScrollbar.cpp @@ -394,7 +394,7 @@ Error GUIScrollbar::set_gui_scrollbar(GUI::Scrollbar const* new_gui_scrollbar) { for (GFXButtonStateTexture::ButtonState state : { HOVER, PRESSED }) { ERR_FAIL_NULL_V_MSG(texture->get_button_state_texture(state), false, vformat( "Failed to generate %s texture for %s element %s for GUIScrollbar %s!", - GFXButtonStateTexture::button_state_to_theme_name(state), target, element_name, gui_scrollbar_name + GFXButtonStateTexture::button_state_to_name(state), target, element_name, gui_scrollbar_name )); } } diff --git a/extension/src/openvic-extension/utility/UITools.cpp b/extension/src/openvic-extension/utility/UITools.cpp index 23dcced6..4af2b741 100644 --- a/extension/src/openvic-extension/utility/UITools.cpp +++ b/extension/src/openvic-extension/utility/UITools.cpp @@ -347,11 +347,11 @@ static bool generate_button(generate_gui_args_t&& args) { Ref button_state_texture = texture->get_button_state_texture(button_state); if (button_state_texture.is_valid()) { ret &= add_theme_stylebox( - godot_button, button_state_texture->get_button_state_theme(), button_state_texture + godot_button, button_state_texture->get_button_state_name(), button_state_texture ); } else { UtilityFunctions::push_error( - "Failed to make ", GFXButtonStateTexture::button_state_to_theme_name(button_state), + "Failed to make ", GFXButtonStateTexture::button_state_to_name(button_state), " GFXButtonStateTexture for GUI button ", button_name ); ret = false;