From 4e9764ee29fb7b453862835d5aa3a081b0f9a269 Mon Sep 17 00:00:00 2001 From: hop311 Date: Mon, 18 Dec 2023 23:38:54 +0000 Subject: Back to UI Work - UIAdapter -> UITools with cleaner API - GUIOverlappingElementsBox (for core and modifier icons) - Improved GUINode API - Province building slots - TypeHints for files in the GameSession folder - Incorporate SIM strong colour types --- .../classes/GFXPieChartTexture.hpp | 41 +++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'extension/src/openvic-extension/classes/GFXPieChartTexture.hpp') diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp index 315b00e..ad8e751 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp @@ -4,6 +4,8 @@ #include +#include "openvic-extension/utility/Utilities.hpp" + namespace OpenVic { class GFXPieChartTexture : public godot::ImageTexture { GDCLASS(GFXPieChartTexture, godot::ImageTexture) @@ -23,11 +25,40 @@ namespace OpenVic { public: GFXPieChartTexture(); - /* Set slices given new_slices, an Array of Dictionaries, each with the following keys: + /* Set slices given an Array of Dictionaries, each with the following key-value entries: * - colour: Color - * - weight: float - */ - godot::Error set_slices(godot::Array const& new_slices); + * - weight: float */ + godot::Error set_slices_array(godot::TypedArray const& new_slices); + + /* Generate slice data from a distribution of HasIdentifierAndColour derived objects, sorted by their weight. + * The resulting Array of Dictionaries can be used as an argument for set_slices_array. */ + template T> + static godot::TypedArray distribution_to_slices_array(fixed_point_map_t const& dist) { + using entry_t = std::pair; + std::vector sorted_dist; + sorted_dist.reserve(dist.size()); + for (entry_t const& entry : dist) { + ERR_CONTINUE_MSG( + entry.first == nullptr, godot::vformat("Null distribution key with value %f", entry.second.to_float()) + ); + sorted_dist.push_back(entry); + } + std::sort(sorted_dist.begin(), sorted_dist.end(), [](entry_t const& lhs, entry_t const& rhs) -> bool { + return lhs.second < rhs.second; + }); + static const godot::StringName identifier_key = "identifier"; + static const godot::StringName colour_key = "colour"; + static const godot::StringName weight_key = "weight"; + godot::TypedArray array; + for (auto const& [key, val] : sorted_dist) { + godot::Dictionary sub_dict; + sub_dict[identifier_key] = Utilities::std_view_to_godot_string(key->get_identifier()); + sub_dict[colour_key] = Utilities::to_godot_color(key->get_colour()); + sub_dict[weight_key] = val.to_float(); + array.push_back(sub_dict); + } + return array; + } /* Create a GFXPieChartTexture using the specific GFX::PieChart. * Returns nullptr if setting gfx_pie_chart fails. */ @@ -43,7 +74,7 @@ namespace OpenVic { /* Search for a GFX::PieChart with the specfied name and, if successful, set it using set_gfx_pie_chart. */ godot::Error set_gfx_pie_chart_name(godot::String const& gfx_pie_chart_name); - /* Return the name of the GFX::PieChart, or an empty String if it's null */ + /* Return the name of the GFX::PieChart, or an empty String if it's null. */ godot::String get_gfx_pie_chart_name() const; }; } -- cgit v1.2.3-56-ga3b1