diff options
author | Hop311 <Hop3114@gmail.com> | 2024-08-30 23:29:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-30 23:29:57 +0200 |
commit | f54e454afb90f8868e7c62529e2a388fdaadf20b (patch) | |
tree | f19dbcdfe613397e86dc52cc34e0a443bd0f3e96 /extension/src/openvic-extension/utility | |
parent | 855e5b087459da19caf230cf22d99462680b268e (diff) | |
parent | d7672f406406eea46625bc725690651f28211e19 (diff) |
Merge pull request #251 from OpenVicProject/gui-text-label
Add GUILabel (colour code + currency icon support)
Diffstat (limited to 'extension/src/openvic-extension/utility')
-rw-r--r-- | extension/src/openvic-extension/utility/UITools.cpp | 60 | ||||
-rw-r--r-- | extension/src/openvic-extension/utility/Utilities.cpp | 5 | ||||
-rw-r--r-- | extension/src/openvic-extension/utility/Utilities.hpp | 3 |
3 files changed, 27 insertions, 41 deletions
diff --git a/extension/src/openvic-extension/utility/UITools.cpp b/extension/src/openvic-extension/utility/UITools.cpp index cffab22..723fb24 100644 --- a/extension/src/openvic-extension/utility/UITools.cpp +++ b/extension/src/openvic-extension/utility/UITools.cpp @@ -2,7 +2,6 @@ #include <godot_cpp/classes/button.hpp> #include <godot_cpp/classes/color_rect.hpp> -#include <godot_cpp/classes/label.hpp> #include <godot_cpp/classes/line_edit.hpp> #include <godot_cpp/classes/panel.hpp> #include <godot_cpp/classes/style_box_empty.hpp> @@ -16,6 +15,7 @@ #include "openvic-extension/classes/GFXSpriteTexture.hpp" #include "openvic-extension/classes/GFXMaskedFlagTexture.hpp" #include "openvic-extension/classes/GFXPieChartTexture.hpp" +#include "openvic-extension/classes/GUILabel.hpp" #include "openvic-extension/classes/GUIListBox.hpp" #include "openvic-extension/classes/GUIOverlappingElementsBox.hpp" #include "openvic-extension/classes/GUIScrollbar.hpp" @@ -500,55 +500,26 @@ 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<GUI::Text const&>(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())); + GUILabel* gui_label = nullptr; + bool ret = new_control(gui_label, text, args.name); + ERR_FAIL_NULL_V_MSG(gui_label, false, vformat("Failed to create GUILabel 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); + gui_label->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); - using enum GUI::AlignedElement::format_t; - static const ordered_map<GUI::AlignedElement::format_t, HorizontalAlignment> format_map { - { left, HORIZONTAL_ALIGNMENT_LEFT }, - { centre, HORIZONTAL_ALIGNMENT_CENTER }, - { right, HORIZONTAL_ALIGNMENT_RIGHT } - }; + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + GFX::Font::colour_codes_t const* override_colour_codes = game_singleton != nullptr + ? &game_singleton->get_definition_manager().get_ui_manager().get_universal_colour_codes() : nullptr; - 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 (gui_label->set_gui_text(&text, override_colour_codes) != OK) { + UtilityFunctions::push_error("Error initialising GUILabel 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> 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 = gui_label; return ret; } @@ -564,7 +535,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<real_t>(val); } |