From fd375bdb35d8a7b2ac9cf3dd02cdb0f197451a0b Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 7 Dec 2023 22:45:19 +0000 Subject: Big UI commit - GUINode, MaskedFlag, PieChart, etc --- .../src/openvic-extension/utility/Utilities.cpp | 31 ++++++++++++++++++++++ .../src/openvic-extension/utility/Utilities.hpp | 4 +++ 2 files changed, 35 insertions(+) (limited to 'extension/src/openvic-extension/utility') diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp index e3bcce6..099b5a9 100644 --- a/extension/src/openvic-extension/utility/Utilities.cpp +++ b/extension/src/openvic-extension/utility/Utilities.cpp @@ -13,6 +13,37 @@ using namespace godot; 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) { + static const std::vector> suffixes { + { 1'000'000'000'000, "T" }, + { 1'000'000'000, "B" }, + { 1'000'000, "M" }, + { 1'000, "k" } + }; + static constexpr int64_t decimal_places_multiplier = 100; + const bool negative = val < 0; + if (negative) { + val = -val; + } + for (auto const& [suffix_val, suffix_str] : suffixes) { + if (val >= suffix_val) { + const int64_t whole = val / suffix_val; + const int64_t frac = (val * decimal_places_multiplier / suffix_val) % decimal_places_multiplier; + return (negative ? "-" : "") + String::num_int64(whole) + "." + + (frac < 10 ? "0" : "") + String::num_int64(frac) + suffix_str; + } + } + return (negative ? "-" : "") + String::num_int64(val); +} + +/* Float to formatted to 4 decimal place string. */ +String Utilities::float_to_formatted_string(float val) { + static constexpr int64_t decimal_places = 4; + return String::num(val, decimal_places).pad_decimals(decimal_places); +} + /* Date formatted like this: "January 1, 1836" (with the month localised, if possible). */ String Utilities::date_to_formatted_string(Date date) { std::string const& month_name = date.get_month_name(); diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp index 9537dda..6eeb285 100644 --- a/extension/src/openvic-extension/utility/Utilities.hpp +++ b/extension/src/openvic-extension/utility/Utilities.hpp @@ -31,6 +31,10 @@ namespace OpenVic::Utilities { return std_to_godot_string_name(static_cast(str)); } + godot::String int_to_formatted_string(int64_t val); + + godot::String float_to_formatted_string(float val); + godot::String date_to_formatted_string(Date date); inline godot::Color to_godot_color(colour_t colour) { -- cgit v1.2.3-56-ga3b1