diff options
author | hop311 <hop3114@gmail.com> | 2024-08-28 19:46:30 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-08-28 23:51:31 +0200 |
commit | 88acb31bd43f0e163522837bb1d0dd7da2977c4a (patch) | |
tree | 241cbf8cf21a7cdfe1c870469ac3b3ef3064bdb3 /extension/src/openvic-extension/classes | |
parent | d7672f406406eea46625bc725690651f28211e19 (diff) |
Switch to using custom UI nodes
Diffstat (limited to 'extension/src/openvic-extension/classes')
19 files changed, 873 insertions, 64 deletions
diff --git a/extension/src/openvic-extension/classes/GUIButton.cpp b/extension/src/openvic-extension/classes/GUIButton.cpp new file mode 100644 index 0000000..323b03c --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIButton.cpp @@ -0,0 +1,99 @@ +#include "GUIButton.hpp" + +#include <array> + +#include <godot_cpp/variant/utility_functions.hpp> + +#include "openvic-extension/singletons/AssetManager.hpp" +#include "openvic-extension/utility/Utilities.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUIButton::_bind_methods() {} + +Error GUIButton::set_gfx_button_state_having_texture(Ref<GFXButtonStateHavingTexture> const& texture) { + ERR_FAIL_NULL_V(texture, FAILED); + + Error err = OK; + + set_custom_minimum_size(texture->get_size()); + + { + Ref<StyleBoxTexture> stylebox = AssetManager::make_stylebox_texture(texture); + + if (stylebox.is_valid()) { + static const StringName normal_theme = "normal"; + + add_theme_stylebox_override(normal_theme, stylebox); + } else { + UtilityFunctions::push_error("Failed to make StyleBoxTexture for GUIButton ", get_name()); + + err = FAILED; + } + } + + using enum GFXButtonStateTexture::ButtonState; + + for (GFXButtonStateTexture::ButtonState button_state : { HOVER, PRESSED, DISABLED }) { + Ref<GFXButtonStateTexture> button_state_texture = texture->get_button_state_texture(button_state); + + if (button_state_texture.is_valid()) { + Ref<StyleBoxTexture> stylebox = AssetManager::make_stylebox_texture(button_state_texture); + + if (stylebox.is_valid()) { + add_theme_stylebox_override(button_state_texture->get_button_state_name(), stylebox); + } else { + UtilityFunctions::push_error( + "Failed to make ", GFXButtonStateTexture::button_state_to_name(button_state), + " StyleBoxTexture for GUIButton ", get_name() + ); + + err = FAILED; + } + } else { + UtilityFunctions::push_error( + "Failed to make ", GFXButtonStateTexture::button_state_to_name(button_state), + " GFXButtonStateTexture for GUIButton ", get_name() + ); + + err = FAILED; + } + } + + return err; +} + +Error GUIButton::set_gfx_font(GFX::Font const* gfx_font) { + ERR_FAIL_NULL_V(gfx_font, FAILED); + + AssetManager* asset_manager = AssetManager::get_singleton(); + ERR_FAIL_NULL_V(asset_manager, FAILED); + + Error err = OK; + + const StringName font_file = Utilities::std_to_godot_string(gfx_font->get_fontname()); + const Ref<Font> font = asset_manager->get_font(font_file); + + if (font.is_valid()) { + static const StringName font_theme = "font"; + + add_theme_font_override(font_theme, font); + } else { + UtilityFunctions::push_error("Failed to load font \"", font_file, "\" for GUIButton ", get_name()); + + err = FAILED; + } + + static const std::array<StringName, 5> button_font_themes { + "font_color", "font_hover_color", "font_hover_pressed_color", "font_pressed_color", "font_disabled_color" + }; + + const Color colour = Utilities::to_godot_color(gfx_font->get_colour()); + + for (StringName const& theme_name : button_font_themes) { + add_theme_color_override(theme_name, colour); + } + + return err; +} diff --git a/extension/src/openvic-extension/classes/GUIButton.hpp b/extension/src/openvic-extension/classes/GUIButton.hpp new file mode 100644 index 0000000..4e53125 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIButton.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include <godot_cpp/classes/button.hpp> + +#include <openvic-simulation/interface/GFXSprite.hpp> + +#include "openvic-extension/classes/GFXButtonStateTexture.hpp" + +namespace OpenVic { + class GUIButton : public godot::Button { + GDCLASS(GUIButton, godot::Button) + + protected: + static void _bind_methods(); + + godot::Error set_gfx_button_state_having_texture(godot::Ref<GFXButtonStateHavingTexture> const& texture); + + public: + godot::Error set_gfx_font(GFX::Font const* gfx_font); + }; +} diff --git a/extension/src/openvic-extension/classes/GUIIcon.cpp b/extension/src/openvic-extension/classes/GUIIcon.cpp new file mode 100644 index 0000000..68e7ec4 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIIcon.cpp @@ -0,0 +1,82 @@ +#include "GUIIcon.hpp" + +#include "openvic-extension/utility/ClassBindings.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUIIcon::_bind_methods() { + OV_BIND_METHOD(GUIIcon::get_gfx_sprite_texture); + + OV_BIND_METHOD(GUIIcon::set_gfx_texture_sprite_name, { "gfx_texture_sprite_name", "icon" }, DEFVAL(GFX::NO_FRAMES)); + OV_BIND_METHOD(GUIIcon::get_gfx_texture_sprite_name); + + OV_BIND_METHOD(GUIIcon::set_icon_index, { "icon_index" }); + OV_BIND_METHOD(GUIIcon::get_icon_index); + + OV_BIND_METHOD(GUIIcon::set_toggled_icon, { "toggled" }); +} + +Error GUIIcon::set_gfx_texture_sprite(GFX::TextureSprite const* gfx_texture_sprite, GFX::frame_t icon) { + const bool needs_setting = gfx_sprite_texture.is_null(); + + if (needs_setting) { + gfx_sprite_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + } + + const Error err = gfx_sprite_texture->set_gfx_texture_sprite(gfx_texture_sprite, icon); + + if (needs_setting) { + set_texture(gfx_sprite_texture); + } + + return err; +} + +Ref<GFXSpriteTexture> GUIIcon::get_gfx_sprite_texture() const { + ERR_FAIL_NULL_V(gfx_sprite_texture, nullptr); + + return gfx_sprite_texture; +} + +Error GUIIcon::set_gfx_texture_sprite_name(String const& gfx_texture_sprite_name, GFX::frame_t icon) { + const bool needs_setting = gfx_sprite_texture.is_null(); + + if (needs_setting) { + gfx_sprite_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + } + + const Error err = gfx_sprite_texture->set_gfx_texture_sprite_name(gfx_texture_sprite_name, icon); + + if (needs_setting) { + set_texture(gfx_sprite_texture); + } + + return err; +} + +String GUIIcon::get_gfx_texture_sprite_name() const { + ERR_FAIL_NULL_V(gfx_sprite_texture, {}); + + return gfx_sprite_texture->get_gfx_texture_sprite_name(); +} + +Error GUIIcon::set_icon_index(GFX::frame_t icon_index) const { + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + + return gfx_sprite_texture->set_icon_index(icon_index); +} + +GFX::frame_t GUIIcon::get_icon_index() const { + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + + return gfx_sprite_texture->get_icon_index(); +} + +Error GUIIcon::set_toggled_icon(bool toggled) const { + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + + return gfx_sprite_texture->set_toggled_icon(toggled); +} diff --git a/extension/src/openvic-extension/classes/GUIIcon.hpp b/extension/src/openvic-extension/classes/GUIIcon.hpp new file mode 100644 index 0000000..e8e3014 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIIcon.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "openvic-extension/classes/GFXSpriteTexture.hpp" +#include "openvic-extension/classes/GUITextureRect.hpp" + +namespace OpenVic { + class GUIIcon : public GUITextureRect { + GDCLASS(GUIIcon, GUITextureRect) + + godot::Ref<GFXSpriteTexture> gfx_sprite_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_texture_sprite( + GFX::TextureSprite const* gfx_texture_sprite, GFX::frame_t icon = GFX::NO_FRAMES + ); + + godot::Ref<GFXSpriteTexture> get_gfx_sprite_texture() const; + + godot::Error set_gfx_texture_sprite_name( + godot::String const& gfx_texture_sprite_name, GFX::frame_t icon = GFX::NO_FRAMES + ); + + godot::String get_gfx_texture_sprite_name() const; + + godot::Error set_icon_index(GFX::frame_t icon_index) const; + + GFX::frame_t get_icon_index() const; + + godot::Error set_toggled_icon(bool toggled) const; + }; +} diff --git a/extension/src/openvic-extension/classes/GUIIconButton.cpp b/extension/src/openvic-extension/classes/GUIIconButton.cpp new file mode 100644 index 0000000..1bba947 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIIconButton.cpp @@ -0,0 +1,86 @@ +#include "GUIIconButton.hpp" + +#include "openvic-extension/utility/ClassBindings.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUIIconButton::_bind_methods() { + OV_BIND_METHOD(GUIIconButton::get_gfx_sprite_texture); + + OV_BIND_METHOD(GUIIconButton::set_gfx_texture_sprite_name, { "gfx_texture_sprite_name", "icon" }, DEFVAL(GFX::NO_FRAMES)); + OV_BIND_METHOD(GUIIconButton::get_gfx_texture_sprite_name); + + OV_BIND_METHOD(GUIIconButton::set_icon_index, { "icon_index" }); + OV_BIND_METHOD(GUIIconButton::get_icon_index); + + OV_BIND_METHOD(GUIIconButton::set_toggled_icon, { "toggled" }); +} + +Error GUIIconButton::set_gfx_texture_sprite(GFX::TextureSprite const* gfx_texture_sprite, GFX::frame_t icon) { + const bool needs_setting = gfx_sprite_texture.is_null(); + + if (needs_setting) { + gfx_sprite_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + } + + Error err = gfx_sprite_texture->set_gfx_texture_sprite(gfx_texture_sprite, icon); + + if (needs_setting && set_gfx_button_state_having_texture(gfx_sprite_texture) != OK) { + err = FAILED; + } + + return err; +} + +Ref<GFXSpriteTexture> GUIIconButton::get_gfx_sprite_texture() const { + ERR_FAIL_NULL_V(gfx_sprite_texture, nullptr); + + return gfx_sprite_texture; +} + +Error GUIIconButton::set_gfx_texture_sprite_name(String const& gfx_texture_sprite_name, GFX::frame_t icon) { + const bool needs_setting = gfx_sprite_texture.is_null(); + + if (needs_setting) { + gfx_sprite_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + } + + Error err = gfx_sprite_texture->set_gfx_texture_sprite_name(gfx_texture_sprite_name, icon); + + if (needs_setting && set_gfx_button_state_having_texture(gfx_sprite_texture) != OK) { + err = FAILED; + } + + return err; +} + +String GUIIconButton::get_gfx_texture_sprite_name() const { + ERR_FAIL_NULL_V(gfx_sprite_texture, {}); + + return gfx_sprite_texture->get_gfx_texture_sprite_name(); +} + +Error GUIIconButton::set_icon_index(GFX::frame_t icon_index) const { + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + + return gfx_sprite_texture->set_icon_index(icon_index); +} + +GFX::frame_t GUIIconButton::get_icon_index() const { + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + + return gfx_sprite_texture->get_icon_index(); +} + +Error GUIIconButton::set_toggled_icon(bool toggled) const { + ERR_FAIL_NULL_V(gfx_sprite_texture, FAILED); + + return gfx_sprite_texture->set_toggled_icon(toggled); +} + +void GUIIconButton::_toggled(bool toggled_on) { + set_toggled_icon(toggled_on); +} diff --git a/extension/src/openvic-extension/classes/GUIIconButton.hpp b/extension/src/openvic-extension/classes/GUIIconButton.hpp new file mode 100644 index 0000000..10fc179 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIIconButton.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "openvic-extension/classes/GFXSpriteTexture.hpp" +#include "openvic-extension/classes/GUIButton.hpp" + +namespace OpenVic { + class GUIIconButton : public GUIButton { + GDCLASS(GUIIconButton, GUIButton) + + godot::Ref<GFXSpriteTexture> gfx_sprite_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_texture_sprite( + GFX::TextureSprite const* gfx_texture_sprite, GFX::frame_t icon = GFX::NO_FRAMES + ); + + godot::Ref<GFXSpriteTexture> get_gfx_sprite_texture() const; + + godot::Error set_gfx_texture_sprite_name( + godot::String const& gfx_texture_sprite_name, GFX::frame_t icon = GFX::NO_FRAMES + ); + + godot::String get_gfx_texture_sprite_name() const; + + godot::Error set_icon_index(GFX::frame_t icon_index) const; + + GFX::frame_t get_icon_index() const; + + godot::Error set_toggled_icon(bool toggled) const; + + void _toggled(bool toggled_on) override; + }; +} diff --git a/extension/src/openvic-extension/classes/GUIListBox.cpp b/extension/src/openvic-extension/classes/GUIListBox.cpp index 9165b14..958807f 100644 --- a/extension/src/openvic-extension/classes/GUIListBox.cpp +++ b/extension/src/openvic-extension/classes/GUIListBox.cpp @@ -146,7 +146,7 @@ Vector2 GUIListBox::_get_minimum_size() const { } } -void GUIListBox::_gui_input(godot::Ref<godot::InputEvent> const& event) { +void GUIListBox::_gui_input(Ref<InputEvent> const& event) { ERR_FAIL_NULL(event); if (scrollbar == nullptr) { diff --git a/extension/src/openvic-extension/classes/GUIMaskedFlag.cpp b/extension/src/openvic-extension/classes/GUIMaskedFlag.cpp new file mode 100644 index 0000000..968cebb --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIMaskedFlag.cpp @@ -0,0 +1,88 @@ +#include "GUIMaskedFlag.hpp" + +#include "openvic-extension/utility/ClassBindings.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUIMaskedFlag::_bind_methods() { + OV_BIND_METHOD(GUIMaskedFlag::get_gfx_masked_flag_texture); + + OV_BIND_METHOD(GUIMaskedFlag::set_gfx_masked_flag_name, { "gfx_masked_flag_name" }); + OV_BIND_METHOD(GUIMaskedFlag::get_gfx_masked_flag_name); + + OV_BIND_METHOD(GUIMaskedFlag::set_flag_country_name_and_type, { "flag_country_name", "flag_type" }); + OV_BIND_METHOD(GUIMaskedFlag::set_flag_country_name, { "flag_country_name" }); + OV_BIND_METHOD(GUIMaskedFlag::get_flag_country_name); + OV_BIND_METHOD(GUIMaskedFlag::get_flag_type); +} + +Error GUIMaskedFlag::set_gfx_masked_flag(GFX::MaskedFlag const* gfx_masked_flag) { + const bool needs_setting = gfx_masked_flag_texture.is_null(); + + if (needs_setting) { + gfx_masked_flag_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + } + + const Error err = gfx_masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag); + + if (needs_setting) { + set_texture(gfx_masked_flag_texture); + } + + return err; +} + +Ref<GFXMaskedFlagTexture> GUIMaskedFlag::get_gfx_masked_flag_texture() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, nullptr); + + return gfx_masked_flag_texture; +} + +Error GUIMaskedFlag::set_gfx_masked_flag_name(String const& gfx_masked_flag_name) { + const bool needs_setting = gfx_masked_flag_texture.is_null(); + + if (needs_setting) { + gfx_masked_flag_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + } + + const Error err = gfx_masked_flag_texture->set_gfx_masked_flag_name(gfx_masked_flag_name); + + if (needs_setting) { + set_texture(gfx_masked_flag_texture); + } + + return err; +} + +String GUIMaskedFlag::get_gfx_masked_flag_name() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, {}); + + return gfx_masked_flag_texture->get_gfx_masked_flag_name(); +} + +Error GUIMaskedFlag::set_flag_country_name_and_type(String const& flag_country_name, StringName const& flag_type) const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + + return gfx_masked_flag_texture->set_flag_country_name_and_type(flag_country_name, flag_type); +} + +Error GUIMaskedFlag::set_flag_country_name(String const& flag_country_name) const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + + return gfx_masked_flag_texture->set_flag_country_name(flag_country_name); +} + +String GUIMaskedFlag::get_flag_country_name() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, {}); + + return gfx_masked_flag_texture->get_flag_country_name(); +} + +String GUIMaskedFlag::get_flag_type() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, {}); + + return gfx_masked_flag_texture->get_flag_type(); +} diff --git a/extension/src/openvic-extension/classes/GUIMaskedFlag.hpp b/extension/src/openvic-extension/classes/GUIMaskedFlag.hpp new file mode 100644 index 0000000..4bdc6c1 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIMaskedFlag.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "openvic-extension/classes/GFXMaskedFlagTexture.hpp" +#include "openvic-extension/classes/GUITextureRect.hpp" + +namespace OpenVic { + class GUIMaskedFlag : public GUITextureRect { + GDCLASS(GUIMaskedFlag, GUITextureRect) + + godot::Ref<GFXMaskedFlagTexture> gfx_masked_flag_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_masked_flag(GFX::MaskedFlag const* gfx_masked_flag); + + godot::Ref<GFXMaskedFlagTexture> get_gfx_masked_flag_texture() const; + + godot::Error set_gfx_masked_flag_name(godot::String const& gfx_masked_flag_name); + + godot::String get_gfx_masked_flag_name() const; + + godot::Error set_flag_country_name_and_type( + godot::String const& flag_country_name, godot::StringName const& flag_type + ) const; + + godot::Error set_flag_country_name(godot::String const& flag_country_name) const; + + godot::String get_flag_country_name() const; + + godot::String get_flag_type() const; + }; +} diff --git a/extension/src/openvic-extension/classes/GUIMaskedFlagButton.cpp b/extension/src/openvic-extension/classes/GUIMaskedFlagButton.cpp new file mode 100644 index 0000000..8c3c1f8 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIMaskedFlagButton.cpp @@ -0,0 +1,88 @@ +#include "GUIMaskedFlagButton.hpp" + +#include "openvic-extension/utility/ClassBindings.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUIMaskedFlagButton::_bind_methods() { + OV_BIND_METHOD(GUIMaskedFlagButton::get_gfx_masked_flag_texture); + + OV_BIND_METHOD(GUIMaskedFlagButton::set_gfx_masked_flag_name, { "gfx_masked_flag_name" }); + OV_BIND_METHOD(GUIMaskedFlagButton::get_gfx_masked_flag_name); + + OV_BIND_METHOD(GUIMaskedFlagButton::set_flag_country_name_and_type, { "flag_country_name", "flag_type" }); + OV_BIND_METHOD(GUIMaskedFlagButton::set_flag_country_name, { "flag_country_name" }); + OV_BIND_METHOD(GUIMaskedFlagButton::get_flag_country_name); + OV_BIND_METHOD(GUIMaskedFlagButton::get_flag_type); +} + +Error GUIMaskedFlagButton::set_gfx_masked_flag(GFX::MaskedFlag const* gfx_masked_flag) { + const bool needs_setting = gfx_masked_flag_texture.is_null(); + + if (needs_setting) { + gfx_masked_flag_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + } + + Error err = gfx_masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag); + + if (needs_setting && set_gfx_button_state_having_texture(gfx_masked_flag_texture) != OK) { + err = FAILED; + } + + return err; +} + +Ref<GFXMaskedFlagTexture> GUIMaskedFlagButton::get_gfx_masked_flag_texture() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, nullptr); + + return gfx_masked_flag_texture; +} + +Error GUIMaskedFlagButton::set_gfx_masked_flag_name(String const& gfx_masked_flag_name) { + const bool needs_setting = gfx_masked_flag_texture.is_null(); + + if (needs_setting) { + gfx_masked_flag_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + } + + Error err = gfx_masked_flag_texture->set_gfx_masked_flag_name(gfx_masked_flag_name); + + if (needs_setting && set_gfx_button_state_having_texture(gfx_masked_flag_texture) != OK) { + err = FAILED; + } + + return err; +} + +String GUIMaskedFlagButton::get_gfx_masked_flag_name() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, {}); + + return gfx_masked_flag_texture->get_gfx_masked_flag_name(); +} + +Error GUIMaskedFlagButton::set_flag_country_name_and_type(String const& flag_country_name, StringName const& flag_type) const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + + return gfx_masked_flag_texture->set_flag_country_name_and_type(flag_country_name, flag_type); +} + +Error GUIMaskedFlagButton::set_flag_country_name(String const& flag_country_name) const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, FAILED); + + return gfx_masked_flag_texture->set_flag_country_name(flag_country_name); +} + +String GUIMaskedFlagButton::get_flag_country_name() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, {}); + + return gfx_masked_flag_texture->get_flag_country_name(); +} + +String GUIMaskedFlagButton::get_flag_type() const { + ERR_FAIL_NULL_V(gfx_masked_flag_texture, {}); + + return gfx_masked_flag_texture->get_flag_type(); +} diff --git a/extension/src/openvic-extension/classes/GUIMaskedFlagButton.hpp b/extension/src/openvic-extension/classes/GUIMaskedFlagButton.hpp new file mode 100644 index 0000000..131c93d --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIMaskedFlagButton.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "openvic-extension/classes/GFXMaskedFlagTexture.hpp" +#include "openvic-extension/classes/GUIButton.hpp" + +namespace OpenVic { + class GUIMaskedFlagButton : public GUIButton { + GDCLASS(GUIMaskedFlagButton, GUIButton) + + godot::Ref<GFXMaskedFlagTexture> gfx_masked_flag_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_masked_flag(GFX::MaskedFlag const* gfx_masked_flag); + + godot::Ref<GFXMaskedFlagTexture> get_gfx_masked_flag_texture() const; + + godot::Error set_gfx_masked_flag_name(godot::String const& gfx_masked_flag_name); + + godot::String get_gfx_masked_flag_name() const; + + godot::Error set_flag_country_name_and_type( + godot::String const& flag_country_name, godot::StringName const& flag_type + ) const; + + godot::Error set_flag_country_name(godot::String const& flag_country_name) const; + + godot::String get_flag_country_name() const; + + godot::String get_flag_type() const; + }; +} diff --git a/extension/src/openvic-extension/classes/GUINode.cpp b/extension/src/openvic-extension/classes/GUINode.cpp index 25ef821..59bad92 100644 --- a/extension/src/openvic-extension/classes/GUINode.cpp +++ b/extension/src/openvic-extension/classes/GUINode.cpp @@ -3,7 +3,6 @@ #include <limits> #include <godot_cpp/classes/bit_map.hpp> -#include <godot_cpp/classes/button.hpp> #include <godot_cpp/classes/canvas_item.hpp> #include <godot_cpp/classes/check_box.hpp> #include <godot_cpp/classes/control.hpp> @@ -17,7 +16,6 @@ #include <godot_cpp/classes/style_box_texture.hpp> #include <godot_cpp/classes/texture2d.hpp> #include <godot_cpp/classes/texture_progress_bar.hpp> -#include <godot_cpp/classes/texture_rect.hpp> #include <godot_cpp/core/defs.hpp> #include <godot_cpp/core/error_macros.hpp> #include <godot_cpp/core/object.hpp> @@ -39,21 +37,19 @@ using namespace godot; using namespace OpenVic; #define APPLY_TO_CHILD_TYPES(F) \ - F(Button, button) \ + F(GUIIconButton, gui_icon_button) \ + F(GUIMaskedFlagButton, gui_masked_flag_button) \ F(GUILabel, gui_label) \ F(Panel, panel) \ - F(TextureProgressBar, progress_bar) \ - F(TextureRect, texture_rect) \ + F(GUIProgressBar, gui_progress_bar) \ + F(GUIIcon, gui_icon) \ + F(GUIMaskedFlag, gui_masked_flag) \ + F(GUIPieChart, gui_pie_chart) \ F(GUIOverlappingElementsBox, gui_overlapping_elements_box) \ F(GUIScrollbar, gui_scrollbar) \ F(GUIListBox, gui_listbox) \ F(LineEdit, line_edit) -#define APPLY_TO_TEXTURE_TYPES(F) \ - F(GFXSpriteTexture, gfx_sprite_texture) \ - F(GFXMaskedFlagTexture, gfx_masked_flag_texture) \ - F(GFXPieChartTexture, gfx_pie_chart_texture) - void GUINode::_bind_methods() { OV_BIND_SMETHOD(generate_gui_element, { "gui_scene", "gui_element", "name" }, DEFVAL(String {})); OV_BIND_METHOD(GUINode::add_gui_element, { "gui_scene", "gui_element", "name" }, DEFVAL(String {})); @@ -74,13 +70,11 @@ void GUINode::_bind_methods() { APPLY_TO_CHILD_TYPES(GET_BINDINGS) +#undef GET_BINDINGS + OV_BIND_SMETHOD(get_texture_from_node, { "node" }); OV_BIND_METHOD(GUINode::get_texture_from_nodepath, { "path" }); - APPLY_TO_TEXTURE_TYPES(GET_BINDINGS) - -#undef GET_BINDINGS - OV_BIND_METHOD(GUINode::hide_node, { "path" }); OV_BIND_METHOD(GUINode::hide_nodes, { "paths" }); @@ -151,33 +145,37 @@ APPLY_TO_CHILD_TYPES(CHILD_GET_FUNCTIONS) #undef CHILD_GET_FUNCTIONS +#undef APPLY_TO_CHILD_TYPES + Ref<Texture2D> GUINode::get_texture_from_node(Node* node) { ERR_FAIL_NULL_V(node, nullptr); if (TextureRect const* texture_rect = Object::cast_to<TextureRect>(node); texture_rect != nullptr) { const Ref<Texture2D> texture = texture_rect->get_texture(); ERR_FAIL_NULL_V_MSG(texture, nullptr, vformat("Failed to get Texture2D from TextureRect %s", node->get_name())); return texture; - } else if (Button const* button = Object::cast_to<Button>(node); button != nullptr) { + } else if (GUIButton const* button = Object::cast_to<GUIButton>(node); button != nullptr) { static const StringName theme_name_normal = "normal"; const Ref<StyleBox> stylebox = button->get_theme_stylebox(theme_name_normal); ERR_FAIL_NULL_V_MSG( - stylebox, nullptr, vformat("Failed to get StyleBox %s from Button %s", theme_name_normal, node->get_name()) + stylebox, nullptr, vformat("Failed to get StyleBox %s from GUIButton %s", theme_name_normal, node->get_name()) ); const Ref<StyleBoxTexture> stylebox_texture = stylebox; ERR_FAIL_NULL_V_MSG( stylebox_texture, nullptr, vformat( - "Failed to cast StyleBox %s from Button %s to type StyleBoxTexture", theme_name_normal, node->get_name() + "Failed to cast StyleBox %s from GUIButton %s to type StyleBoxTexture", theme_name_normal, node->get_name() ) ); const Ref<Texture2D> result = stylebox_texture->get_texture(); ERR_FAIL_NULL_V_MSG( result, nullptr, - vformat("Failed to get Texture2D from StyleBoxTexture %s from Button %s", theme_name_normal, node->get_name()) + vformat("Failed to get Texture2D from StyleBoxTexture %s from GUIButton %s", theme_name_normal, node->get_name()) ); return result; } ERR_FAIL_V_MSG( - nullptr, vformat("Failed to cast node %s from type %s to TextureRect or Button", node->get_name(), node->get_class()) + nullptr, vformat( + "Failed to cast node %s from type %s to TextureRect or GUIButton", node->get_name(), node->get_class() + ) ); } @@ -185,30 +183,6 @@ Ref<Texture2D> GUINode::get_texture_from_nodepath(NodePath const& path) const { return get_texture_from_node(get_node_internal(path)); } -template<std::derived_from<Texture2D> T> -static Ref<T> _cast_texture(Ref<Texture2D> const& texture) { - ERR_FAIL_NULL_V(texture, nullptr); - const Ref<T> result = texture; - ERR_FAIL_NULL_V_MSG( - result, nullptr, vformat("Failed to cast Texture2D from type %s to %s", texture->get_class(), T::get_class_static()) - ); - return result; -} - -#define TEXTURE_GET_FUNCTIONS(type, name) \ - Ref<type> GUINode::get_##name##_from_node(Node* node) { \ - return _cast_texture<type>(get_texture_from_node(node)); \ - } \ - Ref<type> GUINode::get_##name##_from_nodepath(NodePath const& path) const { \ - return _cast_texture<type>(get_texture_from_nodepath(path)); \ - } - -APPLY_TO_TEXTURE_TYPES(TEXTURE_GET_FUNCTIONS) - -#undef TEXTURE_GET_FUNCTIONS - -#undef APPLY_TO_CHILD_TYPES - Error GUINode::hide_node(NodePath const& path) const { CanvasItem* node = _cast_node<CanvasItem>(get_node_internal(path)); ERR_FAIL_NULL_V(node, FAILED); diff --git a/extension/src/openvic-extension/classes/GUINode.hpp b/extension/src/openvic-extension/classes/GUINode.hpp index 73ca92b..453263a 100644 --- a/extension/src/openvic-extension/classes/GUINode.hpp +++ b/extension/src/openvic-extension/classes/GUINode.hpp @@ -1,7 +1,6 @@ #pragma once #include <godot_cpp/classes/bit_map.hpp> -#include <godot_cpp/classes/button.hpp> #include <godot_cpp/classes/control.hpp> #include <godot_cpp/classes/image.hpp> #include <godot_cpp/classes/input_event.hpp> @@ -10,20 +9,21 @@ #include <godot_cpp/classes/panel.hpp> #include <godot_cpp/classes/ref.hpp> #include <godot_cpp/classes/texture2d.hpp> -#include <godot_cpp/classes/texture_progress_bar.hpp> -#include <godot_cpp/classes/texture_rect.hpp> #include <godot_cpp/templates/vector.hpp> #include <godot_cpp/variant/node_path.hpp> #include <godot_cpp/variant/rect2.hpp> #include <godot_cpp/variant/string.hpp> #include <godot_cpp/variant/vector2.hpp> -#include "openvic-extension/classes/GFXMaskedFlagTexture.hpp" -#include "openvic-extension/classes/GFXPieChartTexture.hpp" -#include "openvic-extension/classes/GFXSpriteTexture.hpp" +#include "openvic-extension/classes/GUIIcon.hpp" +#include "openvic-extension/classes/GUIIconButton.hpp" #include "openvic-extension/classes/GUILabel.hpp" #include "openvic-extension/classes/GUIListBox.hpp" +#include "openvic-extension/classes/GUIMaskedFlag.hpp" +#include "openvic-extension/classes/GUIMaskedFlagButton.hpp" #include "openvic-extension/classes/GUIOverlappingElementsBox.hpp" +#include "openvic-extension/classes/GUIPieChart.hpp" +#include "openvic-extension/classes/GUIProgressBar.hpp" #include "openvic-extension/classes/GUIScrollbar.hpp" namespace OpenVic { @@ -51,36 +51,35 @@ namespace OpenVic { static godot::Vector2 get_gui_position(godot::String const& gui_scene, godot::String const& gui_position); - static godot::Button* get_button_from_node(godot::Node* node); + static GUIIconButton* get_gui_icon_button_from_node(godot::Node* node); + static GUIMaskedFlagButton* get_gui_masked_flag_button_from_node(godot::Node* node); static GUILabel* get_gui_label_from_node(godot::Node* node); static godot::Panel* get_panel_from_node(godot::Node* node); - static godot::TextureProgressBar* get_progress_bar_from_node(godot::Node* node); - static godot::TextureRect* get_texture_rect_from_node(godot::Node* node); + static GUIProgressBar* get_gui_progress_bar_from_node(godot::Node* node); + static GUIIcon* get_gui_icon_from_node(godot::Node* node); + static GUIMaskedFlag* get_gui_masked_flag_from_node(godot::Node* node); + static GUIPieChart* get_gui_pie_chart_from_node(godot::Node* node); static GUIOverlappingElementsBox* get_gui_overlapping_elements_box_from_node(godot::Node* node); static GUIScrollbar* get_gui_scrollbar_from_node(godot::Node* node); static GUIListBox* get_gui_listbox_from_node(godot::Node* node); static godot::LineEdit* get_line_edit_from_node(godot::Node* node); - godot::Button* get_button_from_nodepath(godot::NodePath const& path) const; + GUIIconButton* get_gui_icon_button_from_nodepath(godot::NodePath const& path) const; + GUIMaskedFlagButton* get_gui_masked_flag_button_from_nodepath(godot::NodePath const& path) const; GUILabel* get_gui_label_from_nodepath(godot::NodePath const& path) const; godot::Panel* get_panel_from_nodepath(godot::NodePath const& path) const; - godot::TextureProgressBar* get_progress_bar_from_nodepath(godot::NodePath const& path) const; - godot::TextureRect* get_texture_rect_from_nodepath(godot::NodePath const& path) const; + GUIProgressBar* get_gui_progress_bar_from_nodepath(godot::NodePath const& path) const; + GUIIcon* get_gui_icon_from_nodepath(godot::NodePath const& path) const; + GUIMaskedFlag* get_gui_masked_flag_from_nodepath(godot::NodePath const& path) const; + GUIPieChart* get_gui_pie_chart_from_nodepath(godot::NodePath const& path) const; GUIOverlappingElementsBox* get_gui_overlapping_elements_box_from_nodepath(godot::NodePath const& path) const; GUIScrollbar* get_gui_scrollbar_from_nodepath(godot::NodePath const& path) const; GUIListBox* get_gui_listbox_from_nodepath(godot::NodePath const& path) const; godot::LineEdit* get_line_edit_from_nodepath(godot::NodePath const& path) const; - /* Helper functions to get textures from TextureRects and Buttons. */ + /* Helper functions to get textures from TextureRects and GUIButtons. */ static godot::Ref<godot::Texture2D> get_texture_from_node(godot::Node* node); - static godot::Ref<GFXSpriteTexture> get_gfx_sprite_texture_from_node(godot::Node* node); - static godot::Ref<GFXMaskedFlagTexture> get_gfx_masked_flag_texture_from_node(godot::Node* node); - static godot::Ref<GFXPieChartTexture> get_gfx_pie_chart_texture_from_node(godot::Node* node); - godot::Ref<godot::Texture2D> get_texture_from_nodepath(godot::NodePath const& path) const; - godot::Ref<GFXSpriteTexture> get_gfx_sprite_texture_from_nodepath(godot::NodePath const& path) const; - godot::Ref<GFXMaskedFlagTexture> get_gfx_masked_flag_texture_from_nodepath(godot::NodePath const& path) const; - godot::Ref<GFXPieChartTexture> get_gfx_pie_chart_texture_from_nodepath(godot::NodePath const& path) const; godot::Error hide_node(godot::NodePath const& path) const; godot::Error hide_nodes(godot::TypedArray<godot::NodePath> const& paths) const; diff --git a/extension/src/openvic-extension/classes/GUIPieChart.cpp b/extension/src/openvic-extension/classes/GUIPieChart.cpp new file mode 100644 index 0000000..a8ed087 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIPieChart.cpp @@ -0,0 +1,66 @@ +#include "GUIPieChart.hpp" + +#include "openvic-extension/utility/ClassBindings.hpp" + +using namespace godot; +using namespace OpenVic; +using namespace OpenVic::Utilities::literals; + +void GUIPieChart::_bind_methods() { + OV_BIND_METHOD(GUIPieChart::get_gfx_pie_chart_texture); + OV_BIND_METHOD(GUIPieChart::set_gfx_pie_chart_name, { "gfx_pie_chart_name" }); + OV_BIND_METHOD(GUIPieChart::get_gfx_pie_chart_name); + OV_BIND_METHOD(GUIPieChart::set_slices_array, { "new_slices" }); +} + +Error GUIPieChart::set_gfx_pie_chart(GFX::PieChart const* gfx_pie_chart) { + const bool needs_setting = gfx_pie_chart_texture.is_null(); + + if (needs_setting) { + gfx_pie_chart_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_pie_chart_texture, FAILED); + } + + const Error err = gfx_pie_chart_texture->set_gfx_pie_chart(gfx_pie_chart); + + if (needs_setting) { + set_texture(gfx_pie_chart_texture); + } + + return err; +} + +Ref<GFXPieChartTexture> GUIPieChart::get_gfx_pie_chart_texture() const { + ERR_FAIL_NULL_V(gfx_pie_chart_texture, nullptr); + + return gfx_pie_chart_texture; +} + +Error GUIPieChart::set_gfx_pie_chart_name(String const& gfx_pie_chart_name) { + const bool needs_setting = gfx_pie_chart_texture.is_null(); + + if (needs_setting) { + gfx_pie_chart_texture.instantiate(); + ERR_FAIL_NULL_V(gfx_pie_chart_texture, FAILED); + } + + const Error err = gfx_pie_chart_texture->set_gfx_pie_chart_name(gfx_pie_chart_name); + + if (needs_setting) { + set_texture(gfx_pie_chart_texture); + } + + return err; +} + +String GUIPieChart::get_gfx_pie_chart_name() const { + ERR_FAIL_NULL_V(gfx_pie_chart_texture, {}); + + return gfx_pie_chart_texture->get_gfx_pie_chart_name(); +} + +Error GUIPieChart::set_slices_array(GFXPieChartTexture::godot_pie_chart_data_t const& new_slices) const { + ERR_FAIL_NULL_V(gfx_pie_chart_texture, FAILED); + + return gfx_pie_chart_texture->set_slices_array(new_slices); +} diff --git a/extension/src/openvic-extension/classes/GUIPieChart.hpp b/extension/src/openvic-extension/classes/GUIPieChart.hpp new file mode 100644 index 0000000..6b4ac87 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIPieChart.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include <godot_cpp/classes/texture_rect.hpp> + +#include <openvic-simulation/interface/GFXSprite.hpp> + +#include "openvic-extension/classes/GFXPieChartTexture.hpp" + +namespace OpenVic { + class GUIPieChart : public godot::TextureRect { + GDCLASS(GUIPieChart, godot::TextureRect) + + godot::Ref<GFXPieChartTexture> gfx_pie_chart_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_pie_chart(GFX::PieChart const* gfx_pie_chart); + + godot::Ref<GFXPieChartTexture> get_gfx_pie_chart_texture() const; + + godot::Error set_gfx_pie_chart_name(godot::String const& gfx_pie_chart_name); + + godot::String get_gfx_pie_chart_name() const; + + godot::Error set_slices_array(GFXPieChartTexture::godot_pie_chart_data_t const& new_slices) const; + }; +} diff --git a/extension/src/openvic-extension/classes/GUIProgressBar.cpp b/extension/src/openvic-extension/classes/GUIProgressBar.cpp new file mode 100644 index 0000000..6021746 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIProgressBar.cpp @@ -0,0 +1,104 @@ +#include "GUIProgressBar.hpp" + +#include <godot_cpp/variant/utility_functions.hpp> + +#include "openvic-extension/singletons/AssetManager.hpp" +#include "openvic-extension/utility/Utilities.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUIProgressBar::_bind_methods() {} + +Error GUIProgressBar::set_gfx_progress_bar(GFX::ProgressBar const* progress_bar) { + ERR_FAIL_NULL_V(progress_bar, FAILED); + + AssetManager* asset_manager = AssetManager::get_singleton(); + ERR_FAIL_NULL_V(asset_manager, FAILED); + + Error err = OK; + + static constexpr double MIN_VALUE = 0.0, MAX_VALUE = 1.0; + static constexpr uint32_t STEPS = 100; + + set_nine_patch_stretch(true); + set_step((MAX_VALUE - MIN_VALUE) / STEPS); + set_min(MIN_VALUE); + set_max(MAX_VALUE); + + using enum AssetManager::LoadFlags; + + Ref<ImageTexture> back_texture; + if (!progress_bar->get_back_texture_file().empty()) { + const StringName back_texture_file = Utilities::std_to_godot_string(progress_bar->get_back_texture_file()); + back_texture = asset_manager->get_texture(back_texture_file, LOAD_FLAG_CACHE_TEXTURE | LOAD_FLAG_FLIP_Y); + if (back_texture.is_null()) { + UtilityFunctions::push_error( + "Failed to load sprite back texture ", back_texture_file, " for GUIProgressBar ", get_name() + ); + err = FAILED; + } + } + if (back_texture.is_null()) { + const Color back_colour = Utilities::to_godot_color(progress_bar->get_back_colour()); + back_texture = Utilities::make_solid_colour_texture( + back_colour, progress_bar->get_size().x, progress_bar->get_size().y + ); + if (back_texture.is_null()) { + UtilityFunctions::push_error( + "Failed to generate sprite ", back_colour, " back texture for GUIProgressBar ", get_name() + ); + err = FAILED; + } + } + if (back_texture.is_valid()) { + set_under_texture(back_texture); + } else { + UtilityFunctions::push_error( + "Failed to create and set sprite back texture for GUIProgressBar ", get_name() + ); + err = FAILED; + } + + Ref<ImageTexture> progress_texture; + if (!progress_bar->get_progress_texture_file().empty()) { + const StringName progress_texture_file = + Utilities::std_to_godot_string(progress_bar->get_progress_texture_file()); + progress_texture = asset_manager->get_texture(progress_texture_file, LOAD_FLAG_CACHE_TEXTURE | LOAD_FLAG_FLIP_Y); + if (progress_texture.is_null()) { + UtilityFunctions::push_error( + "Failed to load sprite progress texture ", progress_texture_file, " for GUIProgressBar ", + get_name() + ); + err = FAILED; + } + } + if (progress_texture.is_null()) { + const Color progress_colour = Utilities::to_godot_color(progress_bar->get_progress_colour()); + progress_texture = Utilities::make_solid_colour_texture( + progress_colour, progress_bar->get_size().x, progress_bar->get_size().y + ); + if (progress_texture.is_null()) { + UtilityFunctions::push_error( + "Failed to generate sprite ", progress_colour, " progress texture for GUIProgressBar ", + get_name() + ); + err = FAILED; + } + } + if (progress_texture.is_valid()) { + set_progress_texture(progress_texture); + } else { + UtilityFunctions::push_error( + "Failed to create and set sprite progress texture for GUIProgressBar ", get_name() + ); + err = FAILED; + } + + // TODO - work out why progress bar is missing bottom border pixel (e.g. province building expansion bar) + set_custom_minimum_size( + Utilities::to_godot_fvec2(static_cast<fvec2_t>(progress_bar->get_size())) + ); + + return err; +} diff --git a/extension/src/openvic-extension/classes/GUIProgressBar.hpp b/extension/src/openvic-extension/classes/GUIProgressBar.hpp new file mode 100644 index 0000000..130ac91 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIProgressBar.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include <godot_cpp/classes/texture_progress_bar.hpp> + +#include "openvic-simulation/interface/GFXSprite.hpp" + +namespace OpenVic { + class GUIProgressBar : public godot::TextureProgressBar { + GDCLASS(GUIProgressBar, godot::TextureProgressBar) + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_progress_bar(GFX::ProgressBar const* progress_bar); + }; +} diff --git a/extension/src/openvic-extension/classes/GUITextureRect.cpp b/extension/src/openvic-extension/classes/GUITextureRect.cpp new file mode 100644 index 0000000..13fd3bb --- /dev/null +++ b/extension/src/openvic-extension/classes/GUITextureRect.cpp @@ -0,0 +1,6 @@ +#include "GUITextureRect.hpp" + +using namespace godot; +using namespace OpenVic; + +void GUITextureRect::_bind_methods() {} diff --git a/extension/src/openvic-extension/classes/GUITextureRect.hpp b/extension/src/openvic-extension/classes/GUITextureRect.hpp new file mode 100644 index 0000000..afcf4da --- /dev/null +++ b/extension/src/openvic-extension/classes/GUITextureRect.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include <godot_cpp/classes/texture_rect.hpp> + +namespace OpenVic { + class GUITextureRect : public godot::TextureRect { + GDCLASS(GUITextureRect, godot::TextureRect) + + protected: + static void _bind_methods(); + }; +} |