diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp index 417566da..5d955a37 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp @@ -7,6 +7,7 @@ using namespace godot; using namespace OpenVic; +using namespace OpenVic::Utilities::literals; StringName const& GFXPieChartTexture::_slice_identifier_key() { static const StringName slice_identifier_key = "identifier"; @@ -21,7 +22,35 @@ StringName const& GFXPieChartTexture::_slice_weight_key() { return slice_weight_key; } -static constexpr float TWO_PI = 2.0f * std::numbers::pi_v; +GFXPieChartTexture::slice_t const* GFXPieChartTexture::get_slice(Vector2 const& position) const { + if (slices.empty() || position.length_squared() > 1.0_real) { + return nullptr; + } + + static constexpr float TWO_PI = 2.0f * std::numbers::pi_v; + + /* Calculate the anti-clockwise angle between the point and the centre of the image. + * The y coordinate is negated as the image coordinate system's y increases downwards. */ + float theta = atan2(-position.y, position.x); + if (theta < 0.0f) { + theta += TWO_PI; + } + + /* Rescale angle so that total_weight is a full rotation. */ + theta *= total_weight / TWO_PI; + + /* Find the slice theta lies in. */ + for (slice_t const& slice : slices) { + theta -= slice.weight; + + if (theta <= 0.0f) { + return &slice; + } + } + + /* Default to the first slice in case theta never reaches 0 due to floating point inaccuracy. */ + return &slices.front(); +} Error GFXPieChartTexture::_generate_pie_chart_image() { ERR_FAIL_NULL_V(gfx_pie_chart, FAILED); @@ -30,60 +59,37 @@ Error GFXPieChartTexture::_generate_pie_chart_image() { vformat("Invalid GFX::PieChart size for GFXPieChartTexture - %d", gfx_pie_chart->get_size()) ); - const int32_t pie_chart_size = 2 * gfx_pie_chart->get_size(); + const int32_t pie_chart_radius = gfx_pie_chart->get_size(); + const int32_t pie_chart_diameter = 2 * pie_chart_radius; /* Whether we've already set the ImageTexture to an image of the right dimensions, * and so can update it without creating and setting a new image, or not. */ - const bool can_update = pie_chart_image.is_valid() && pie_chart_image->get_width() == pie_chart_size - && pie_chart_image->get_height() == pie_chart_size; + const bool can_update = pie_chart_image.is_valid() && pie_chart_image->get_width() == pie_chart_diameter + && pie_chart_image->get_height() == pie_chart_diameter; if (!can_update) { - pie_chart_image = Image::create(pie_chart_size, pie_chart_size, false, Image::FORMAT_RGBA8); + pie_chart_image = Image::create(pie_chart_diameter, pie_chart_diameter, false, Image::FORMAT_RGBA8); ERR_FAIL_NULL_V(pie_chart_image, FAILED); } static const Color background_colour { 0.0f, 0.0f, 0.0f, 0.0f }; if (!slices.empty()) { - const float pie_chart_radius = gfx_pie_chart->get_size(); - - const Vector2 centre_translation = Vector2 { 0.5f, 0.5f } - static_cast(pie_chart_image->get_size()) * 0.5f; - - for (Vector2i point { 0, 0 }; point.y < pie_chart_image->get_height(); ++point.y) { - - for (point.x = 0; point.x < pie_chart_image->get_width(); ++point.x) { - - const Vector2 offset = centre_translation + point; - - if (offset.length() <= pie_chart_radius) { - - /* Calculate the anti-clockwise angle between the point and the centre of the image. - * The y coordinate is negated as the image coordinate system's y increases downwards. */ - float theta = atan2(-offset.y, offset.x); - if (theta < 0.0f) { - theta += TWO_PI; - } - - /* Rescale angle so that total_weight is a full rotation. */ - theta *= total_weight / TWO_PI; + for (Vector2i point { 0, 0 }; point.y < pie_chart_diameter; ++point.y) { - /* Default to the first colour in case theta never reaches 0 due to floating point inaccuracy. */ - Color colour = slices.front().first; + for (point.x = 0; point.x < pie_chart_diameter; ++point.x) { - /* Find the slice theta lies in. */ - for (slice_t const& slice : slices) { - theta -= slice.second; + Vector2 offset = point; + // Move to the centre of the pixel + offset += Vector2 { 0.5_real, 0.5_real }; + // Normalise to [0, 2] + offset /= pie_chart_radius; + // Translate to [-1, 1] + offset -= Vector2 { 1.0_real, 1.0_real }; - if (theta <= 0.0f) { - colour = slice.first; - break; - } - } + slice_t const* slice = get_slice(offset); - pie_chart_image->set_pixelv(point, colour); - } else { - pie_chart_image->set_pixelv(point, background_colour); - } + pie_chart_image->set_pixelv(point, slice != nullptr ? slice->colour : background_colour); } } } else { @@ -107,12 +113,15 @@ Error GFXPieChartTexture::set_slices_array(godot_pie_chart_data_t const& new_sli for (int32_t i = 0; i < new_slices.size(); ++i) { Dictionary const& slice_dict = new_slices[i]; ERR_CONTINUE_MSG( - !slice_dict.has(_slice_colour_key()) || !slice_dict.has(_slice_weight_key()), + !slice_dict.has(_slice_identifier_key()) || !slice_dict.has(_slice_colour_key()) + || !slice_dict.has(_slice_weight_key()), vformat("Invalid slice keys at index %d", i) ); - const slice_t slice = std::make_pair(slice_dict[_slice_colour_key()], slice_dict[_slice_weight_key()]); - if (slice.second > 0.0f) { - total_weight += slice.second; + const slice_t slice { + slice_dict[_slice_identifier_key()], slice_dict[_slice_colour_key()], slice_dict[_slice_weight_key()] + }; + if (slice.weight > 0.0f) { + total_weight += slice.weight; slices.push_back(slice); } } diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp index 9642f4ef..3610efb0 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp @@ -10,11 +10,17 @@ namespace OpenVic { class GFXPieChartTexture : public godot::ImageTexture { GDCLASS(GFXPieChartTexture, godot::ImageTexture) - using slice_t = std::pair; + public: + struct slice_t { + godot::String name; + godot::Color colour; + float weight; + }; + private: GFX::PieChart const* PROPERTY(gfx_pie_chart); std::vector slices; - float total_weight; + float PROPERTY(total_weight); godot::Ref pie_chart_image; static godot::StringName const& _slice_identifier_key(); @@ -29,6 +35,9 @@ namespace OpenVic { public: GFXPieChartTexture(); + // Position must be centred and normalised so that coords are in [-1, 1]. + slice_t const* get_slice(godot::Vector2 const& position) const; + using godot_pie_chart_data_t = godot::TypedArray; /* Set slices given an Array of Dictionaries, each with the following key-value entries: diff --git a/extension/src/openvic-extension/classes/GUIButton.cpp b/extension/src/openvic-extension/classes/GUIButton.cpp new file mode 100644 index 00000000..e35d67a9 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIButton.cpp @@ -0,0 +1,109 @@ +#include "GUIButton.hpp" + +#include + +#include + +#include "openvic-extension/singletons/AssetManager.hpp" +#include "openvic-extension/utility/Utilities.hpp" + +using namespace godot; +using namespace OpenVic; + +GUI_TOOLTIP_IMPLEMENTATIONS(GUIButton) + +void GUIButton::_bind_methods() { + GUI_TOOLTIP_BIND_METHODS(GUIButton) +} + +void GUIButton::_notification(int what) { + _tooltip_notification(what); +} + +GUIButton::GUIButton() : tooltip_active { false } {} + +Error GUIButton::set_gfx_button_state_having_texture(Ref const& texture) { + ERR_FAIL_NULL_V(texture, FAILED); + + Error err = OK; + + set_custom_minimum_size(texture->get_size()); + + { + Ref 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 button_state_texture = texture->get_button_state_texture(button_state); + + if (button_state_texture.is_valid()) { + Ref 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 = 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 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 00000000..3873a4de --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIButton.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include + +#include "openvic-extension/classes/GFXButtonStateTexture.hpp" +#include "openvic-extension/classes/GUIHasTooltip.hpp" + +namespace OpenVic { + class GUIButton : public godot::Button { + GDCLASS(GUIButton, godot::Button) + + GUI_TOOLTIP_DEFINITIONS + + protected: + static void _bind_methods(); + + void _notification(int what); + + godot::Error set_gfx_button_state_having_texture(godot::Ref const& texture); + + public: + GUIButton(); + + godot::Error set_gfx_font(GFX::Font const* gfx_font); + }; +} diff --git a/extension/src/openvic-extension/classes/GUIHasTooltip.hpp b/extension/src/openvic-extension/classes/GUIHasTooltip.hpp new file mode 100644 index 00000000..22413ec7 --- /dev/null +++ b/extension/src/openvic-extension/classes/GUIHasTooltip.hpp @@ -0,0 +1,121 @@ +#pragma once + +#include +#include +#include +#include + +#include + +#include "openvic-extension/singletons/MenuSingleton.hpp" +#include "openvic-extension/utility/ClassBindings.hpp" +#include "openvic-extension/utility/Utilities.hpp" + +/* To add tooltip functionality to a class: + * - the class must be derived from Control. + * - add GUI_TOOLTIP_DEFINITIONS to the class definition, bearing in mind that it leaves visibility as private. + * - add GUI_TOOLTIP_IMPLEMENTATIONS(CLASS) to the class' source file. + * - add GUI_TOOLTIP_BIND_METHODS(CLASS) to the class' _bind_methods implementation. + * - call _tooltip_notification from the class' _notification method. + * - initialise tooltip_active to false in the class' constructor. */ + +#define GUI_TOOLTIP_DEFINITIONS \ + public: \ + void set_tooltip_string_and_substitution_dict( \ + godot::String const& new_tooltip_string, godot::Dictionary const& new_tooltip_substitution_dict \ + ); \ + void set_tooltip_string(godot::String const& new_tooltip_string); \ + void set_tooltip_substitution_dict(godot::Dictionary const& new_tooltip_substitution_dict); \ + void clear_tooltip(); \ + private: \ + godot::String PROPERTY(tooltip_string); \ + godot::Dictionary PROPERTY(tooltip_substitution_dict); \ + bool PROPERTY_CUSTOM_PREFIX(tooltip_active, is); \ + void _tooltip_notification(int what); \ + void _set_tooltip_active(bool new_tooltip_active); \ + void _set_tooltip_visibility(bool visible); + +#define GUI_TOOLTIP_IMPLEMENTATIONS(CLASS) \ + void CLASS::set_tooltip_string_and_substitution_dict( \ + String const& new_tooltip_string, Dictionary const& new_tooltip_substitution_dict \ + ) { \ + if (get_mouse_filter() == MOUSE_FILTER_IGNORE) { \ + UtilityFunctions::push_error("Tooltips won't work for \"", get_name(), "\" as it has MOUSE_FILTER_IGNORE"); \ + } \ + if (tooltip_string != new_tooltip_string || tooltip_substitution_dict != new_tooltip_substitution_dict) { \ + tooltip_string = new_tooltip_string; \ + tooltip_substitution_dict = new_tooltip_substitution_dict; \ + if (tooltip_active) { \ + _set_tooltip_visibility(!tooltip_string.is_empty()); \ + } \ + } \ + } \ + void CLASS::set_tooltip_string(String const& new_tooltip_string) { \ + if (get_mouse_filter() == MOUSE_FILTER_IGNORE) { \ + UtilityFunctions::push_error("Tooltips won't work for \"", get_name(), "\" as it has MOUSE_FILTER_IGNORE"); \ + } \ + if (tooltip_string != new_tooltip_string) { \ + tooltip_string = new_tooltip_string; \ + if (tooltip_active) { \ + _set_tooltip_visibility(!tooltip_string.is_empty()); \ + } \ + } \ + } \ + void CLASS::set_tooltip_substitution_dict(Dictionary const& new_tooltip_substitution_dict) { \ + if (get_mouse_filter() == MOUSE_FILTER_IGNORE) { \ + UtilityFunctions::push_error("Tooltips won't work for \"", get_name(), "\" as it has MOUSE_FILTER_IGNORE"); \ + } \ + if (tooltip_substitution_dict != new_tooltip_substitution_dict) { \ + tooltip_substitution_dict = new_tooltip_substitution_dict; \ + if (tooltip_active) { \ + _set_tooltip_visibility(!tooltip_string.is_empty()); \ + } \ + } \ + } \ + void CLASS::clear_tooltip() { \ + set_tooltip_string_and_substitution_dict({}, {}); \ + } \ + void CLASS::_tooltip_notification(int what) { \ + if (what == NOTIFICATION_MOUSE_ENTER_SELF) { \ + _set_tooltip_active(true); \ + } else if (what == NOTIFICATION_MOUSE_EXIT_SELF) { \ + _set_tooltip_active(false); \ + } \ + } \ + void CLASS::_set_tooltip_active(bool new_tooltip_active) { \ + if (tooltip_active != new_tooltip_active) { \ + tooltip_active = new_tooltip_active; \ + if (!tooltip_string.is_empty()) { \ + _set_tooltip_visibility(tooltip_active); \ + } \ + } \ + } \ + void CLASS::_set_tooltip_visibility(bool visible) { \ + MenuSingleton* menu_singleton = MenuSingleton::get_singleton(); \ + ERR_FAIL_NULL(menu_singleton); \ + if (visible) { \ + menu_singleton->show_control_tooltip(tooltip_string, tooltip_substitution_dict, this); \ + } else { \ + menu_singleton->hide_tooltip(); \ + } \ + } + +#define GUI_TOOLTIP_BIND_METHODS(CLASS) \ + OV_BIND_METHOD(CLASS::get_tooltip_string); \ + OV_BIND_METHOD(CLASS::set_tooltip_string, { "new_tooltip_string" }); \ + OV_BIND_METHOD(CLASS::get_tooltip_substitution_dict); \ + OV_BIND_METHOD(CLASS::set_tooltip_substitution_dict, { "new_tooltip_substitution_dict" }); \ + OV_BIND_METHOD( \ + CLASS::set_tooltip_string_and_substitution_dict, { "new_tooltip_string", "new_tooltip_substitution_dict" } \ + ); \ + OV_BIND_METHOD(CLASS::clear_tooltip); \ + OV_BIND_METHOD(CLASS::is_tooltip_active); \ + ADD_PROPERTY( \ + PropertyInfo(Variant::STRING, "tooltip_string", PROPERTY_HINT_MULTILINE_TEXT), \ + "set_tooltip_string", "get_tooltip_string" \ + ); \ + ADD_PROPERTY( \ + PropertyInfo(Variant::DICTIONARY, "tooltip_substitution_dict"), \ + "set_tooltip_substitution_dict", "get_tooltip_substitution_dict" \ + ); \ + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tooltip_active"), "", "is_tooltip_active"); diff --git a/extension/src/openvic-extension/classes/GUIIcon.cpp b/extension/src/openvic-extension/classes/GUIIcon.cpp new file mode 100644 index 00000000..68e7ec41 --- /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 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 00000000..e8e3014a --- /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 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 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 00000000..1bba947f --- /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 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 00000000..10fc1791 --- /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 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 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/GUILabel.cpp b/extension/src/openvic-extension/classes/GUILabel.cpp index 9fd6b603..732dec26 100644 --- a/extension/src/openvic-extension/classes/GUILabel.cpp +++ b/extension/src/openvic-extension/classes/GUILabel.cpp @@ -14,7 +14,30 @@ using namespace OpenVic::Utilities::literals; static constexpr int32_t DEFAULT_FONT_SIZE = 16; +GUI_TOOLTIP_IMPLEMENTATIONS(GUILabel) + +String const& GUILabel::get_colour_marker() { + static const String COLOUR_MARKER = String::chr(0xA7); // § + return COLOUR_MARKER; +} + +String const& GUILabel::get_currency_marker() { + static const String CURRENCY_MARKER = String::chr(0xA4); // ¤ + return CURRENCY_MARKER; +} + +String const& GUILabel::get_substitution_marker() { + static const String SUBSTITUTION_MARKER = String::chr(0x24); // $ + return SUBSTITUTION_MARKER; +} + void GUILabel::_bind_methods() { + GUI_TOOLTIP_BIND_METHODS(GUILabel) + + OV_BIND_SMETHOD(get_colour_marker); + OV_BIND_SMETHOD(get_currency_marker); + OV_BIND_SMETHOD(get_substitution_marker); + OV_BIND_METHOD(GUILabel::clear); OV_BIND_METHOD(GUILabel::get_gui_text_name); @@ -80,6 +103,8 @@ void GUILabel::_bind_methods() { } void GUILabel::_notification(int what) { + _tooltip_notification(what); + switch (what) { case NOTIFICATION_RESIZED: case NOTIFICATION_TRANSLATION_CHANGED: { @@ -146,7 +171,8 @@ void GUILabel::_notification(int what) { } GUILabel::GUILabel() - : gui_text { nullptr }, + : tooltip_active { false }, + gui_text { nullptr }, text {}, substitution_dict {}, horizontal_alignment { HORIZONTAL_ALIGNMENT_LEFT }, @@ -454,29 +480,30 @@ void GUILabel::_update_lines() { } String GUILabel::generate_substituted_text(String const& base_text) const { - static const String SUBSTITUTION_MARKER = String::chr(0x24); // $ - String result; int64_t start_pos = 0; int64_t marker_start_pos; - while ((marker_start_pos = base_text.find(SUBSTITUTION_MARKER, start_pos)) != -1) { + while ((marker_start_pos = base_text.find(get_substitution_marker(), start_pos)) != -1) { result += base_text.substr(start_pos, marker_start_pos - start_pos); - int64_t marker_end_pos = base_text.find(SUBSTITUTION_MARKER, marker_start_pos + SUBSTITUTION_MARKER.length()); + int64_t marker_end_pos = base_text.find( + get_substitution_marker(), marker_start_pos + get_substitution_marker().length() + ); if (marker_end_pos == -1) { marker_end_pos = base_text.length(); } String key = base_text.substr( - marker_start_pos + SUBSTITUTION_MARKER.length(), marker_end_pos - marker_start_pos - SUBSTITUTION_MARKER.length() + marker_start_pos + get_substitution_marker().length(), + marker_end_pos - marker_start_pos - get_substitution_marker().length() ); String value = substitution_dict.get(key, String {}); // Use the un-substituted key if no value is found or the value is empty result += value.is_empty() ? key : is_auto_translating() ? tr(value) : value; - start_pos = marker_end_pos + SUBSTITUTION_MARKER.length(); + start_pos = marker_end_pos + get_substitution_marker().length(); } if (start_pos < base_text.length()) { @@ -489,25 +516,23 @@ String GUILabel::generate_substituted_text(String const& base_text) const { std::pair GUILabel::generate_display_text_and_colour_instructions( String const& substituted_text ) const { - static const String COLOUR_MARKER = String::chr(0xA7); // § - String result; colour_instructions_t colour_instructions; int64_t start_pos = 0; int64_t marker_pos; - while ((marker_pos = substituted_text.find(COLOUR_MARKER, start_pos)) != -1) { + while ((marker_pos = substituted_text.find(get_colour_marker(), start_pos)) != -1) { result += substituted_text.substr(start_pos, marker_pos - start_pos); - if (marker_pos + COLOUR_MARKER.length() < substituted_text.length()) { - const char32_t colour_code = substituted_text[marker_pos + COLOUR_MARKER.length()]; + if (marker_pos + get_colour_marker().length() < substituted_text.length()) { + const char32_t colour_code = substituted_text[marker_pos + get_colour_marker().length()]; // Check that the colour code can be safely cast to a char if (colour_code >> sizeof(char) * CHAR_BIT == 0) { colour_instructions.emplace_back(result.length(), static_cast(colour_code)); } - start_pos = marker_pos + COLOUR_MARKER.length() + 1; + start_pos = marker_pos + get_colour_marker().length() + 1; } else { return { std::move(result), std::move(colour_instructions) }; } @@ -588,8 +613,6 @@ void GUILabel::separate_lines( void GUILabel::separate_currency_segments( String const& string, Color const& colour, line_t& line ) const { - static const String CURRENCY_MARKER = String::chr(0xA4); // ¤ - const auto push_string_segment = [this, &string, &colour, &line](int64_t start, int64_t end) -> void { String substring = string.substr(start, end - start); const real_t width = get_string_width(substring); @@ -602,7 +625,7 @@ void GUILabel::separate_currency_segments( const real_t currency_width = currency_texture.is_valid() ? currency_texture->get_width() : 0.0_real; - while ((marker_pos = string.find(CURRENCY_MARKER, start_pos)) != -1) { + while ((marker_pos = string.find(get_currency_marker(), start_pos)) != -1) { if (start_pos < marker_pos) { push_string_segment(start_pos, marker_pos); } @@ -610,7 +633,7 @@ void GUILabel::separate_currency_segments( line.segments.push_back(currency_segment_t {}); line.width += currency_width; - start_pos = marker_pos + CURRENCY_MARKER.length(); + start_pos = marker_pos + get_currency_marker().length(); } if (start_pos < string.length()) { diff --git a/extension/src/openvic-extension/classes/GUILabel.hpp b/extension/src/openvic-extension/classes/GUILabel.hpp index e0982b22..102ad948 100644 --- a/extension/src/openvic-extension/classes/GUILabel.hpp +++ b/extension/src/openvic-extension/classes/GUILabel.hpp @@ -8,11 +8,14 @@ #include #include "openvic-extension/classes/GFXSpriteTexture.hpp" +#include "openvic-extension/classes/GUIHasTooltip.hpp" namespace OpenVic { class GUILabel : public godot::Control { GDCLASS(GUILabel, godot::Control) + GUI_TOOLTIP_DEFINITIONS + using colour_instructions_t = std::vector>; GUI::Text const* PROPERTY(gui_text); @@ -55,6 +58,10 @@ namespace OpenVic { void _notification(int what); public: + static godot::String const& get_colour_marker(); + static godot::String const& get_currency_marker(); + static godot::String const& get_substitution_marker(); + GUILabel(); /* Reset gui_text to nullptr and reset current text. */ diff --git a/extension/src/openvic-extension/classes/GUIListBox.cpp b/extension/src/openvic-extension/classes/GUIListBox.cpp index 9165b14d..958807fd 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 const& event) { +void GUIListBox::_gui_input(Ref 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 00000000..968cebbb --- /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 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 00000000..4bdc6c15 --- /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 gfx_masked_flag_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_masked_flag(GFX::MaskedFlag const* gfx_masked_flag); + + godot::Ref 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 00000000..8c3c1f89 --- /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 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 00000000..131c93de --- /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 gfx_masked_flag_texture; + + protected: + static void _bind_methods(); + + public: + godot::Error set_gfx_masked_flag(GFX::MaskedFlag const* gfx_masked_flag); + + godot::Ref 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 25ef8211..59bad92c 100644 --- a/extension/src/openvic-extension/classes/GUINode.cpp +++ b/extension/src/openvic-extension/classes/GUINode.cpp @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -17,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -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 GUINode::get_texture_from_node(Node* node) { ERR_FAIL_NULL_V(node, nullptr); if (TextureRect const* texture_rect = Object::cast_to(node); texture_rect != nullptr) { const Ref 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