From 258bcc535da4ae040da63006b4b95cf113dca9ed Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 18 Sep 2024 23:12:41 +0100 Subject: Add RuleSet and ModifierValue tooltip making functions --- .../openvic-extension/singletons/MenuSingleton.cpp | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'extension/src/openvic-extension/singletons/MenuSingleton.cpp') diff --git a/extension/src/openvic-extension/singletons/MenuSingleton.cpp b/extension/src/openvic-extension/singletons/MenuSingleton.cpp index 5074507..c4b704d 100644 --- a/extension/src/openvic-extension/singletons/MenuSingleton.cpp +++ b/extension/src/openvic-extension/singletons/MenuSingleton.cpp @@ -3,6 +3,7 @@ #include #include +#include #include "openvic-extension/classes/GFXPieChartTexture.hpp" #include "openvic-extension/classes/GUINode.hpp" @@ -106,6 +107,92 @@ String MenuSingleton::get_country_adjective(CountryInstance const& country) cons return tr(Utilities::std_to_godot_string(StringUtils::append_string_views(country.get_identifier(), adjective))); } +String MenuSingleton::make_modifier_effects_tooltip(ModifierValue const& modifier) const { + if (modifier.empty()) { + return {}; + } + + String result; + + for (auto const& [effect, value] : modifier.get_values()) { + if (!result.is_empty()) { + result += "\n"; + } + + result += tr(Utilities::std_to_godot_string(effect->get_localisation_key())); + + static const String post_name_text = ": " + GUILabel::get_colour_marker(); + result += post_name_text; + + if (value == 0) { + result += "Y"; + } else if (effect->is_positive_good() == value > 0) { + result += "G"; + } else { + result += "R"; + } + + if (value >= 0) { + result += "+"; + } + + static constexpr int32_t DECIMAL_PLACES = 2; + + using enum ModifierEffect::format_t; + + switch (effect->get_format()) { + case PROPORTION_DECIMAL: + result += GUINode::float_to_string_dp((value * 100).to_float(), DECIMAL_PLACES) + "%"; + break; + case PERCENTAGE_DECIMAL: + result += GUINode::float_to_string_dp(value.to_float(), DECIMAL_PLACES) + "%"; + break; + case INT: + result += String::num_int64(value.to_int64_t()); + break; + case RAW_DECIMAL: [[fallthrough]]; + default: // Use raw decimal as fallback format + result += GUINode::float_to_string_dp(value.to_float(), DECIMAL_PLACES); + break; + } + + static const String end_text = GUILabel::get_colour_marker() + String { "!" }; + result += end_text; + } + + return result; +} + +String MenuSingleton::make_rules_tooltip(RuleSet const& rules) const { + if (rules.empty()) { + return {}; + } + + static const StringName yes_key = "YES"; + static const StringName no_key = "NO"; + + static const String start_text = ": " + GUILabel::get_colour_marker(); + static const String end_text = GUILabel::get_colour_marker() + String { "!" }; + + const String enabled_text = start_text + String { "G" } + tr(yes_key) + end_text; + const String disabled_text = start_text + String { "R" } + tr(no_key) + end_text; + + String result; + + for (auto const& [rule_group, rule_map] : rules.get_rule_groups()) { + for (auto const& [rule, enabled] : rule_map) { + if (!result.is_empty()) { + result += "\n"; + } + + result += tr(Utilities::std_to_godot_string(rule->get_localisation_key())) + + (enabled ? enabled_text : disabled_text); + } + } + + return result; +} + void MenuSingleton::_bind_methods() { OV_BIND_SMETHOD(get_tooltip_separator); OV_BIND_METHOD(MenuSingleton::get_country_name_from_identifier, { "country_identifier" }); -- cgit v1.2.3-56-ga3b1