aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-05-08 01:27:20 +0200
committer hop311 <hop3114@gmail.com>2024-05-08 01:27:20 +0200
commit719610ccebc023a4b21b5b8c716e73c2fe563c6d (patch)
tree5477212120bb0635ce61b6369267d5e59346cd98 /extension/src/openvic-extension
parentc29cc0dabe3e3c7d03280e74d2d10fc3cc479c7f (diff)
Updated number formatting functions
Diffstat (limited to 'extension/src/openvic-extension')
-rw-r--r--extension/src/openvic-extension/classes/GUINode.cpp26
-rw-r--r--extension/src/openvic-extension/classes/GUINode.hpp5
-rw-r--r--extension/src/openvic-extension/utility/Utilities.cpp26
-rw-r--r--extension/src/openvic-extension/utility/Utilities.hpp8
4 files changed, 50 insertions, 15 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
+}