From 7c85ab11e840c281a2499dcc6dd3219c33e7d37f Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 15 Aug 2024 00:13:54 +0100 Subject: Add GUITextLabel (colour code + currency icon support) --- .../src/openvic-extension/utility/UITools.cpp | 58 ++++++---------------- .../src/openvic-extension/utility/Utilities.cpp | 5 ++ .../src/openvic-extension/utility/Utilities.hpp | 3 ++ 3 files changed, 24 insertions(+), 42 deletions(-) (limited to 'extension/src/openvic-extension/utility') diff --git a/extension/src/openvic-extension/utility/UITools.cpp b/extension/src/openvic-extension/utility/UITools.cpp index cffab22..f972681 100644 --- a/extension/src/openvic-extension/utility/UITools.cpp +++ b/extension/src/openvic-extension/utility/UITools.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -19,6 +18,7 @@ #include "openvic-extension/classes/GUIListBox.hpp" #include "openvic-extension/classes/GUIOverlappingElementsBox.hpp" #include "openvic-extension/classes/GUIScrollbar.hpp" +#include "openvic-extension/classes/GUITextLabel.hpp" #include "openvic-extension/singletons/AssetManager.hpp" #include "openvic-extension/singletons/GameSingleton.hpp" #include "openvic-extension/utility/Utilities.hpp" @@ -500,55 +500,22 @@ static bool generate_checkbox(generate_gui_args_t&& args) { } static bool generate_text(generate_gui_args_t&& args) { - using namespace OpenVic::Utilities::literals; - GUI::Text const& text = static_cast(args.element); const String text_name = Utilities::std_to_godot_string(text.get_name()); - Label* godot_label = nullptr; - bool ret = new_control(godot_label, text, args.name); - ERR_FAIL_NULL_V_MSG(godot_label, false, vformat("Failed to create Label for GUI text %s", text_name)); - - godot_label->set_text(Utilities::std_to_godot_string(text.get_text())); + GUITextLabel* text_label = nullptr; + bool ret = new_control(text_label, text, args.name); + ERR_FAIL_NULL_V_MSG(text_label, false, vformat("Failed to create GUITextLabel for GUI text %s", text_name)); - static const Vector2 default_padding { 1.0_real, 0.0_real }; - const Vector2 border_size = Utilities::to_godot_fvec2(text.get_border_size()) + default_padding; - const Vector2 max_size = Utilities::to_godot_fvec2(text.get_max_size()); - godot_label->set_position(godot_label->get_position() + border_size); - godot_label->set_custom_minimum_size(max_size - 2 * border_size); + text_label->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); - using enum GUI::AlignedElement::format_t; - static const ordered_map format_map { - { left, HORIZONTAL_ALIGNMENT_LEFT }, - { centre, HORIZONTAL_ALIGNMENT_CENTER }, - { right, HORIZONTAL_ALIGNMENT_RIGHT } - }; - - const decltype(format_map)::const_iterator it = format_map.find(text.get_format()); - if (it != format_map.end()) { - godot_label->set_horizontal_alignment(it->second); - } else { - UtilityFunctions::push_error("Invalid text format (horizontal alignment) for GUI text ", text_name); + if (text_label->set_gui_text(&text) != OK) { + UtilityFunctions::push_error("Error initialising GUITextLabel for GUI text ", text_name); ret = false; } - if (text.get_font() != nullptr) { - const StringName font_file = Utilities::std_to_godot_string(text.get_font()->get_fontname()); - const Ref font = args.asset_manager.get_font(font_file); - if (font.is_valid()) { - static const StringName font_theme = "font"; - godot_label->add_theme_font_override(font_theme, font); - } else { - UtilityFunctions::push_error("Failed to load font \"", font_file, "\" for GUI text ", text_name); - ret = false; - } - const Color colour = Utilities::to_godot_color(text.get_font()->get_colour()); - static const StringName font_color_theme = "font_color"; - godot_label->add_theme_color_override(font_color_theme, colour); - } - - args.result = godot_label; + args.result = text_label; return ret; } @@ -564,7 +531,14 @@ static bool generate_overlapping_elements(generate_gui_args_t&& args) { vformat("Failed to create GUIOverlappingElementsBox for GUI overlapping elements %s", overlapping_elements_name) ); box->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); - ret &= box->set_gui_overlapping_elements_box(&overlapping_elements) == OK; + + if (box->set_gui_overlapping_elements_box(&overlapping_elements) != OK) { + UtilityFunctions::push_error( + "Error initialising GUIOverlappingElementsBox for GUI overlapping elements ", overlapping_elements_name + ); + ret = false; + } + args.result = box; return ret; } diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp index 4a774a7..1fcdea8 100644 --- a/extension/src/openvic-extension/utility/Utilities.cpp +++ b/extension/src/openvic-extension/utility/Utilities.cpp @@ -63,6 +63,11 @@ String Utilities::float_to_string_dp(float val, int32_t decimal_places) { return String::num(val, decimal_places).pad_decimals(decimal_places); } +String Utilities::float_to_string_dp_dynamic(float val) { + const float abs_val = std::abs(val); + return float_to_string_dp(val, abs_val < 2.0f ? 3 : abs_val < 10.0f ? 2 : 1); +} + /* Date formatted like this: "January 1, 1836" (with the month localised, if possible). */ String Utilities::date_to_formatted_string(Date date) { const String month_name = Utilities::std_to_godot_string(date.get_month_name()); diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp index 49314ca..48be1e0 100644 --- a/extension/src/openvic-extension/utility/Utilities.hpp +++ b/extension/src/openvic-extension/utility/Utilities.hpp @@ -27,6 +27,9 @@ namespace OpenVic::Utilities { godot::String float_to_string_dp(float val, int32_t decimal_places); + // 3dp if abs(val) < 2 else 2dp if abs(val) < 10 else 1dp + godot::String float_to_string_dp_dynamic(float val); + constexpr real_t to_real_t(std::floating_point auto val) { return static_cast(val); } -- cgit v1.2.3-56-ga3b1