diff options
author | hop311 <hop3114@gmail.com> | 2024-05-08 01:27:20 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-05-08 01:27:20 +0200 |
commit | 719610ccebc023a4b21b5b8c716e73c2fe563c6d (patch) | |
tree | 5477212120bb0635ce61b6369267d5e59346cd98 | |
parent | c29cc0dabe3e3c7d03280e74d2d10fc3cc479c7f (diff) |
Updated number formatting functions
6 files changed, 61 insertions, 27 deletions
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<NodePath> 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<BitMap> 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<godot::NodePath> 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<godot::BitMap> 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<std::pair<int64_t, String>> 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<std::string>(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<real_t>(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 +} diff --git a/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd b/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd index 5de2d25..ef0f404 100644 --- a/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd +++ b/game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd @@ -436,7 +436,7 @@ func _update_province_list(scroll_index : int = -1) -> void: ) if _province_list_size_labels[index]: - _province_list_size_labels[index].set_text(GUINode.int_to_formatted_string(province_list_info[size_key])) + _province_list_size_labels[index].set_text(GUINode.int_to_string_suffixed(province_list_info[size_key])) if _province_list_growth_icons[index]: _province_list_growth_icons[index].set_icon_index(get_growth_icon_index(province_list_info[change_key])) @@ -529,7 +529,7 @@ func _update_distributions(): var weight_label : Label = GUINode.get_label_from_node(child.get_node(^"./legend_value")) if weight_label: - weight_label.set_text("%s%%" % GUINode.float_to_formatted_string(distribution_row[slice_weight_key] * 100.0, 1)) + weight_label.set_text("%s%%" % GUINode.float_to_string_dp(distribution_row[slice_weight_key] * 100.0, 1)) func _update_pop_list() -> void: if _pop_list_scrollbar: @@ -567,7 +567,7 @@ func _update_pop_list() -> void: var pop_row : Dictionary = pop_rows[index] if _pop_list_size_labels[index]: - _pop_list_size_labels[index].set_text(GUINode.int_to_formatted_string(pop_row[pop_size_key])) + _pop_list_size_labels[index].set_text(GUINode.int_to_string_suffixed(pop_row[pop_size_key])) if _pop_list_type_icons[index]: _pop_list_type_icons[index].set_icon_index(pop_row[pop_type_icon_key]) if _pop_list_culture_labels[index]: @@ -575,11 +575,11 @@ func _update_pop_list() -> void: if _pop_list_religion_icons[index]: _pop_list_religion_icons[index].set_icon_index(pop_row[pop_religion_icon_key]) if _pop_list_location_labels[index]: - _pop_list_location_labels[index].set_text(GUINode.format_province_name(pop_row[pop_location_key])) + _pop_list_location_labels[index].set_text(GUINode.format_province_name(pop_row.get(pop_location_key, ""))) if _pop_list_militancy_labels[index]: - _pop_list_militancy_labels[index].set_text(GUINode.float_to_formatted_string(pop_row[pop_militancy_key], 2)) + _pop_list_militancy_labels[index].set_text(GUINode.float_to_string_dp(pop_row[pop_militancy_key], 2)) if _pop_list_consciousness_labels[index]: - _pop_list_consciousness_labels[index].set_text(GUINode.float_to_formatted_string(pop_row[pop_consciousness_key], 2)) + _pop_list_consciousness_labels[index].set_text(GUINode.float_to_string_dp(pop_row[pop_consciousness_key], 2)) if _pop_list_ideology_charts[index]: _pop_list_ideology_charts[index].set_slices_array(pop_row[pop_ideology_key]) if _pop_list_issues_charts[index]: @@ -587,7 +587,7 @@ func _update_pop_list() -> void: if _pop_list_unemployment_progressbars[index]: _pop_list_unemployment_progressbars[index].set_value_no_signal(pop_row[pop_unemployment_key]) if _pop_list_cash_labels[index]: - _pop_list_cash_labels[index].set_text(GUINode.float_to_formatted_string(pop_row[pop_cash_key], 2)) + _pop_list_cash_labels[index].set_text(GUINode.float_to_string_dp(pop_row[pop_cash_key], 2)) if _pop_list_life_needs_progressbars[index]: _pop_list_life_needs_progressbars[index].set_value_no_signal(pop_row[pop_life_needs_key]) if _pop_list_everyday_needs_progressbars[index]: @@ -597,7 +597,7 @@ func _update_pop_list() -> void: 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]: - _pop_list_literacy_labels[index].set_text("%s%%" % GUINode.float_to_formatted_string(pop_row[pop_literacy_key], 2)) + _pop_list_literacy_labels[index].set_text("%s%%" % GUINode.float_to_string_dp(pop_row[pop_literacy_key], 2)) _pop_list_rows[index].show() else: diff --git a/game/src/Game/GameSession/ProvinceOverviewPanel.gd b/game/src/Game/GameSession/ProvinceOverviewPanel.gd index 42f6765..fd089e7 100644 --- a/game/src/Game/GameSession/ProvinceOverviewPanel.gd +++ b/game/src/Game/GameSession/ProvinceOverviewPanel.gd @@ -176,7 +176,6 @@ func _ready() -> void: if population_menu_button: population_menu_button.pressed.connect( func() -> void: - pass MenuSingleton.population_menu_select_province(_selected_index) _on_close_button_pressed() Events.NationManagementScreens.open_nation_management_screen(NationManagement.Screen.POPULATION) @@ -323,14 +322,14 @@ func _update_info() -> void: if _rgo_income_label: # TODO - add £ sign and replace placeholder with actual value - _rgo_income_label.text = GUINode.float_to_formatted_string(12.34567, 3) + _rgo_income_label.text = "%s £" % GUINode.float_to_string_dp(12.34567, 3) if _rgo_employment_percentage_texture: pass if _rgo_employment_population_label: # TODO - replace placeholder with actual value - _rgo_employment_population_label.text = GUINode.int_to_formatted_string(_province_info.get(_province_info_total_population_key, 0) / 10) + _rgo_employment_population_label.text = GUINode.int_to_string_suffixed(_province_info.get(_province_info_total_population_key, 0) / 10) if _rgo_employment_percentage_label: pass @@ -345,7 +344,7 @@ func _update_info() -> void: pass if _total_population_label: - _total_population_label.text = GUINode.int_to_formatted_string(_province_info.get(_province_info_total_population_key, 0)) + _total_population_label.text = GUINode.int_to_string_suffixed(_province_info.get(_province_info_total_population_key, 0)) if _migration_label: pass |