From 719610ccebc023a4b21b5b8c716e73c2fe563c6d Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 8 May 2024 00:27:20 +0100 Subject: Updated number formatting functions --- .../src/openvic-extension/classes/GUINode.cpp | 26 +++++++++++++++------- .../src/openvic-extension/classes/GUINode.hpp | 5 +++-- .../src/openvic-extension/utility/Utilities.cpp | 26 ++++++++++++++++++++-- .../src/openvic-extension/utility/Utilities.hpp | 8 ++++--- 4 files changed, 50 insertions(+), 15 deletions(-) (limited to 'extension/src') diff --git a/extension/src/openvic-extension/classes/GUINode.cpp b/extension/src/openvic-extension/classes/GUINode.cpp index c9af7e2..452efc8 100644 --- a/extension/src/openvic-extension/classes/GUINode.cpp +++ b/extension/src/openvic-extension/classes/GUINode.cpp @@ -84,8 +84,9 @@ void GUINode::_bind_methods() { OV_BIND_METHOD(GUINode::hide_node, { "path" }); OV_BIND_METHOD(GUINode::hide_nodes, { "paths" }); - OV_BIND_SMETHOD(int_to_formatted_string, { "val" }); - OV_BIND_SMETHOD(float_to_formatted_string, { "val", "decimal_places" }); + OV_BIND_SMETHOD(int_to_string_suffixed, { "val" }); + OV_BIND_SMETHOD(float_to_string_suffixed, { "val" }); + OV_BIND_SMETHOD(float_to_string_dp, { "val", "decimal_places" }); OV_BIND_SMETHOD(format_province_name, { "province_identifier" }); } @@ -221,17 +222,26 @@ Error GUINode::hide_nodes(TypedArray const& paths) const { return ret; } -String GUINode::int_to_formatted_string(int64_t val) { - return Utilities::int_to_formatted_string(val); +String GUINode::int_to_string_suffixed(int64_t val) { + return Utilities::int_to_string_suffixed(val); } -String GUINode::float_to_formatted_string(float val, int32_t decimal_places) { - return Utilities::float_to_formatted_string(val, decimal_places); +String GUINode::float_to_string_suffixed(float val) { + return Utilities::float_to_string_suffixed(val); +} + +String GUINode::float_to_string_dp(float val, int32_t decimal_places) { + return Utilities::float_to_string_dp(val, decimal_places); } String GUINode::format_province_name(String const& province_identifier) { - static const String province_prefix = "PROV"; - return province_prefix + province_identifier; + if (!province_identifier.is_empty()) { + static const String province_prefix = "PROV"; + return province_prefix + province_identifier; + } else { + static const String no_province = "NO PROVINCE"; + return no_province; + } } Ref GUINode::get_click_mask() const { diff --git a/extension/src/openvic-extension/classes/GUINode.hpp b/extension/src/openvic-extension/classes/GUINode.hpp index 8d926cc..60c0050 100644 --- a/extension/src/openvic-extension/classes/GUINode.hpp +++ b/extension/src/openvic-extension/classes/GUINode.hpp @@ -85,8 +85,9 @@ namespace OpenVic { godot::Error hide_node(godot::NodePath const& path) const; godot::Error hide_nodes(godot::TypedArray const& paths) const; - static godot::String int_to_formatted_string(int64_t val); - static godot::String float_to_formatted_string(float val, int32_t decimal_places); + static godot::String int_to_string_suffixed(int64_t val); + static godot::String float_to_string_suffixed(float val); + static godot::String float_to_string_dp(float val, int32_t decimal_places); static godot::String format_province_name(godot::String const& province_identifier); godot::Ref get_click_mask() const; diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp index 4389e95..694b658 100644 --- a/extension/src/openvic-extension/utility/Utilities.cpp +++ b/extension/src/openvic-extension/utility/Utilities.cpp @@ -13,7 +13,7 @@ using namespace OpenVic; /* Int to 2 decimal place string in terms of the largest suffix less than or equal to it, * or normal integer string if less than the smallest suffix. */ -String Utilities::int_to_formatted_string(int64_t val) { +String Utilities::int_to_string_suffixed(int64_t val) { static const std::vector> suffixes { { 1'000'000'000'000, "T" }, { 1'000'000'000, "B" }, @@ -36,8 +36,30 @@ String Utilities::int_to_formatted_string(int64_t val) { return (negative ? "-" : "") + String::num_int64(val); } +String Utilities::float_to_string_suffixed(float val) { + const float abs_val = std::abs(val); + + if (abs_val < 10'000.0f) { + return float_to_string_dp(val, 1); + } + + if (abs_val < 1'000'000.0f) { + return float_to_string_dp(val / 1'000.0f, 2) + "k"; + } + + if (abs_val < 1'000'000'000.0f) { + return float_to_string_dp(val / 1'000'000.0f, 2) + "M"; + } + + if (abs_val < 1'000'000'000'000.0f) { + return float_to_string_dp(val / 1'000'000'000.0f, 2) + "B"; + } + + return float_to_string_dp(val / 1'000'000'000'000.0f, 2) + "T"; +} + /* Float to string formatted with the specified number of decimal places. */ -String Utilities::float_to_formatted_string(float val, int32_t decimal_places) { +String Utilities::float_to_string_dp(float val, int32_t decimal_places) { return String::num(val, decimal_places).pad_decimals(decimal_places); } diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp index 15ff6b6..0cd9edc 100644 --- a/extension/src/openvic-extension/utility/Utilities.hpp +++ b/extension/src/openvic-extension/utility/Utilities.hpp @@ -33,9 +33,11 @@ namespace OpenVic::Utilities { return std_to_godot_string_name(static_cast(str)); } - godot::String int_to_formatted_string(int64_t val); + godot::String int_to_string_suffixed(int64_t val); - godot::String float_to_formatted_string(float val, int32_t decimal_places); + godot::String float_to_string_suffixed(float val); + + godot::String float_to_string_dp(float val, int32_t decimal_places); constexpr real_t to_real_t(std::floating_point auto val) { return static_cast(val); @@ -83,4 +85,4 @@ namespace OpenVic::Utilities { namespace literals { constexpr real_t operator""_real(long double val) { return to_real_t(val); } } -} \ No newline at end of file +} -- cgit v1.2.3-56-ga3b1 From 9ffe4b0bbada54b77a1cd7af30f562034c57f10c Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 8 May 2024 00:31:42 +0100 Subject: Population menu rebel icons --- .../singletons/PopulationMenu.cpp | 43 +++++++++++++------ .../NationManagementScreen/PopulationMenu.gd | 50 ++++++++++++++++++++-- 2 files changed, 77 insertions(+), 16 deletions(-) (limited to 'extension/src') diff --git a/extension/src/openvic-extension/singletons/PopulationMenu.cpp b/extension/src/openvic-extension/singletons/PopulationMenu.cpp index a598ceb..609142a 100644 --- a/extension/src/openvic-extension/singletons/PopulationMenu.cpp +++ b/extension/src/openvic-extension/singletons/PopulationMenu.cpp @@ -400,6 +400,18 @@ void MenuSingleton::_population_menu_update_filtered_pops() { _population_menu_sort_pops(); } +template T> +static bool compare_translated_identifiers(Object const& object, T const& lhs, T const& rhs) { + return object.tr(std_view_to_godot_string(lhs.get_identifier())) + < object.tr(std_view_to_godot_string(rhs.get_identifier())); +} + +template T> +static bool compare_translated_identifiers(Object const& object, T const* lhs, T const* rhs) { + return (lhs != nullptr ? object.tr(std_view_to_godot_string(lhs->get_identifier())) : godot::String {}) + < (rhs != nullptr ? object.tr(std_view_to_godot_string(rhs->get_identifier())) : godot::String {}); +} + MenuSingleton::sort_func_t MenuSingleton::_get_population_menu_sort_func(population_menu_t::PopSortKey sort_key) const { using enum population_menu_t::PopSortKey; switch (sort_key) { @@ -409,23 +421,19 @@ MenuSingleton::sort_func_t MenuSingleton::_get_population_menu_sort_func(populat }; case SORT_TYPE: return [this](Pop const* a, Pop const* b) -> bool { - return tr(std_view_to_godot_string(a->get_type().get_identifier())) - < tr(std_view_to_godot_string(b->get_type().get_identifier())); + return compare_translated_identifiers(*this, a->get_type(), b->get_type()); }; case SORT_CULTURE: return [this](Pop const* a, Pop const* b) -> bool { - return tr(std_view_to_godot_string(a->get_culture().get_identifier())) - < tr(std_view_to_godot_string(b->get_culture().get_identifier())); + return compare_translated_identifiers(*this, a->get_culture(), b->get_culture()); }; case SORT_RELIGION: return [this](Pop const* a, Pop const* b) -> bool { - return tr(std_view_to_godot_string(a->get_religion().get_identifier())) - < tr(std_view_to_godot_string(b->get_religion().get_identifier())); + return compare_translated_identifiers(*this, a->get_religion(), b->get_religion()); }; case SORT_LOCATION: return [this](Pop const* a, Pop const* b) -> bool { - return tr(a->get_location() != nullptr ? std_view_to_godot_string(a->get_location()->get_identifier()) : String {}) - < tr(b->get_location() != nullptr ? std_view_to_godot_string(b->get_location()->get_identifier()) : String {}); + return compare_translated_identifiers(*this, a->get_location(), b->get_location()); }; case SORT_MILITANCY: return [](Pop const* a, Pop const* b) -> bool { @@ -464,7 +472,11 @@ MenuSingleton::sort_func_t MenuSingleton::_get_population_menu_sort_func(populat return a->get_luxury_needs_fulfilled() < b->get_luxury_needs_fulfilled(); }; case SORT_REBEL_FACTION: - return [](Pop const* a, Pop const* b) -> bool { return false; }; // TODO - implement + return [this](Pop const* a, Pop const* b) -> bool { + // TODO - include country adjective for [pan-]nationalist rebels + // TODO - handle social/political reform movements + return compare_translated_identifiers(*this, a->get_rebel_type(), b->get_rebel_type()); + }; case SORT_SIZE_CHANGE: return [](Pop const* a, Pop const* b) -> bool { return a->get_total_change() < b->get_total_change(); @@ -566,7 +578,10 @@ TypedArray MenuSingleton::get_population_menu_pop_rows(int32_t start static const StringName pop_luxury_needs_key = "luxury_needs"; // TODO - goods not available on market or goods not affordale + price (for all 3 needs types) - // TODO - rebel faction icon and name/description + static const StringName pop_rebel_icon_key = "rebel_icon"; + // TODO - rebel faction name/description + // TODO - icons for social/political reform movements + // TODO - flags for country-related rebels static const StringName pop_size_change_key = "size_change"; // TODO - size change breakdown @@ -585,8 +600,9 @@ TypedArray MenuSingleton::get_population_menu_pop_rows(int32_t start pop_dict[pop_type_icon_key] = pop->get_type().get_sprite(); pop_dict[pop_culture_key] = std_view_to_godot_string(pop->get_culture().get_identifier()); pop_dict[pop_religion_icon_key] = pop->get_religion().get_icon(); - pop_dict[pop_location_key] = - pop->get_location() != nullptr ? std_view_to_godot_string(pop->get_location()->get_identifier()) : String {}; + if (pop->get_location() != nullptr) { + pop_dict[pop_location_key] = std_view_to_godot_string(pop->get_location()->get_identifier()); + } pop_dict[pop_militancy_key] = pop->get_militancy().to_float(); pop_dict[pop_consciousness_key] = pop->get_consciousness().to_float(); pop_dict[pop_ideology_key] = GFXPieChartTexture::distribution_to_slices_array(pop->get_ideologies()); @@ -596,6 +612,9 @@ TypedArray MenuSingleton::get_population_menu_pop_rows(int32_t start pop_dict[pop_life_needs_key] = pop->get_life_needs_fulfilled().to_float(); pop_dict[pop_everyday_needs_key] = pop->get_everyday_needs_fulfilled().to_float(); pop_dict[pop_luxury_needs_key] = pop->get_luxury_needs_fulfilled().to_float(); + if (pop->get_rebel_type() != nullptr) { + pop_dict[pop_rebel_icon_key] = pop->get_rebel_type()->get_icon(); + } pop_dict[pop_size_change_key] = pop->get_total_change(); pop_dict[pop_literacy_key] = pop->get_literacy().to_float(); diff --git a/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd b/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd index ef0f404..15f8dca 100644 --- a/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd +++ b/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd @@ -49,9 +49,13 @@ var _pop_list_cash_labels : Array[Label] var _pop_list_life_needs_progressbars : Array[TextureProgressBar] var _pop_list_everyday_needs_progressbars : Array[TextureProgressBar] var _pop_list_luxury_needs_progressbars : Array[TextureProgressBar] +var _pop_list_rebel_texture_rects : Array[TextureRect] var _pop_list_rebel_icons : Array[GFXSpriteTexture] +var _pop_list_social_movement_texture_rects : Array[TextureRect] var _pop_list_social_movement_icons : Array[GFXSpriteTexture] +var _pop_list_political_movement_texture_rects : Array[TextureRect] var _pop_list_political_movement_icons : Array[GFXSpriteTexture] +var _pop_list_national_movement_texture_rects : Array[TextureRect] var _pop_list_national_movement_flags : Array[GFXMaskedFlagTexture] var _pop_list_size_change_icons : Array[GFXSpriteTexture] var _pop_list_literacy_labels : Array[Label] @@ -357,13 +361,33 @@ func _setup_pop_list() -> void: _pop_list_luxury_needs_progressbars.push_back(GUINode.get_progress_bar_from_node(pop_row_panel.get_node(^"./luxneed_progress"))) - _pop_list_rebel_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_row_panel.get_node(^"./pop_revolt"))) + var pop_list_rebel_texture_rect : TextureRect = GUINode.get_texture_rect_from_node(pop_row_panel.get_node(^"./pop_revolt")) + _pop_list_rebel_texture_rects.push_back(pop_list_rebel_texture_rect) + if pop_list_rebel_texture_rect: + _pop_list_rebel_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_list_rebel_texture_rect)) + else: + _pop_list_rebel_icons.push_back(null) - _pop_list_social_movement_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_row_panel.get_node(^"./pop_movement_social"))) + var pop_list_social_movement_texture_rect : TextureRect = GUINode.get_texture_rect_from_node(pop_row_panel.get_node(^"./pop_movement_social")) + _pop_list_social_movement_texture_rects.push_back(pop_list_social_movement_texture_rect) + if pop_list_social_movement_texture_rect: + _pop_list_social_movement_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_list_social_movement_texture_rect)) + else: + _pop_list_social_movement_icons.push_back(null) - _pop_list_political_movement_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_row_panel.get_node(^"./pop_movement_political"))) + var pop_list_political_movement_texture_rect : TextureRect = GUINode.get_texture_rect_from_node(pop_row_panel.get_node(^"./pop_movement_political")) + _pop_list_political_movement_texture_rects.push_back(pop_list_political_movement_texture_rect) + if pop_list_political_movement_texture_rect: + _pop_list_political_movement_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_list_political_movement_texture_rect)) + else: + _pop_list_political_movement_icons.push_back(null) - _pop_list_national_movement_flags.push_back(GUINode.get_gfx_masked_flag_texture_from_node(pop_row_panel.get_node(^"./pop_movement_flag"))) + var pop_list_national_movement_texture_rect : TextureRect = GUINode.get_texture_rect_from_node(pop_row_panel.get_node(^"./pop_movement_flag")) + _pop_list_national_movement_texture_rects.push_back(pop_list_national_movement_texture_rect) + if pop_list_national_movement_texture_rect: + _pop_list_national_movement_flags.push_back(GUINode.get_gfx_masked_flag_texture_from_node(pop_list_national_movement_texture_rect)) + else: + _pop_list_national_movement_flags.push_back(null) _pop_list_size_change_icons.push_back(GUINode.get_gfx_sprite_texture_from_node(pop_row_panel.get_node(^"./growth_indicator"))) @@ -561,6 +585,7 @@ func _update_pop_list() -> void: const pop_life_needs_key : StringName = &"life_needs" const pop_everyday_needs_key : StringName = &"everyday_needs" const pop_luxury_needs_key : StringName = &"luxury_needs" + const pop_rebel_icon_key : StringName = &"rebel_icon" const pop_size_change_key : StringName = &"size_change" const pop_literacy_key : StringName = &"literacy" @@ -594,6 +619,23 @@ func _update_pop_list() -> void: _pop_list_everyday_needs_progressbars[index].set_value_no_signal(pop_row[pop_everyday_needs_key]) if _pop_list_luxury_needs_progressbars[index]: _pop_list_luxury_needs_progressbars[index].set_value_no_signal(pop_row[pop_luxury_needs_key]) + if _pop_list_rebel_texture_rects[index]: + var rebel_icon : int = pop_row.get(pop_rebel_icon_key, 0) + if rebel_icon > 0: + if _pop_list_rebel_icons[index]: + _pop_list_rebel_icons[index].set_icon_index(rebel_icon) + _pop_list_rebel_texture_rects[index].show() + else: + _pop_list_rebel_texture_rects[index].hide() + + # TODO - handle social/political reform and country rebels + if _pop_list_social_movement_texture_rects[index]: + _pop_list_social_movement_texture_rects[index].hide() + if _pop_list_political_movement_texture_rects[index]: + _pop_list_political_movement_texture_rects[index].hide() + if _pop_list_national_movement_texture_rects[index]: + _pop_list_national_movement_texture_rects[index].hide() + if _pop_list_size_change_icons[index]: _pop_list_size_change_icons[index].set_icon_index(get_growth_icon_index(pop_row[pop_size_change_key])) if _pop_list_literacy_labels[index]: -- cgit v1.2.3-56-ga3b1