From a1a9d2d6ce3c94bccf224efcffd4c7e11eebe1e1 Mon Sep 17 00:00:00 2001 From: wvpm <24685035+wvpm@users.noreply.github.com> Date: Wed, 30 Oct 2024 23:41:44 +0100 Subject: Fix UI bindings after RGO PR --- extension/deps/openvic-simulation | 2 +- .../classes/GFXPieChartTexture.hpp | 73 +++++++++++----------- .../openvic-extension/singletons/MenuSingleton.cpp | 10 +-- .../singletons/PopulationMenu.cpp | 19 +++--- 4 files changed, 55 insertions(+), 49 deletions(-) diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index c88cf59..8defcd5 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit c88cf59997529cbca008f9a2b629822de9deaa2a +Subproject commit 8defcd5daa1acd2c61aa1cd0a26478d472fed9b0 diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp index 69330a4..50616d9 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp @@ -11,6 +11,7 @@ namespace OpenVic { GDCLASS(GFXPieChartTexture, godot::ImageTexture) public: + using godot_pie_chart_data_t = godot::TypedArray; struct slice_t { godot::String name; godot::Color colour; @@ -29,56 +30,44 @@ namespace OpenVic { godot::Error _generate_pie_chart_image(); - protected: - static void _bind_methods(); - public: - GFXPieChartTexture(); - - // Position must be centred and normalised so that coords are in [-1, 1]. - slice_t const* get_slice(godot::Vector2 const& position) const; - - using godot_pie_chart_data_t = godot::TypedArray; - - /* Set slices given an Array of Dictionaries, each with the following key-value entries: - * - colour: Color - * - weight: float */ - godot::Error set_slices_array(godot_pie_chart_data_t const& new_slices); - /* Generate slice data from a distribution of objects satisfying HasGetIdentifierAndGetColour, sorted by their weight. * The resulting Array of Dictionaries can be used as an argument for set_slices_array. */ template static godot_pie_chart_data_t distribution_to_slices_array(Container const& dist) requires( ( - /* fixed_point_map_t, T derived from HasIdentifierAndColour */ - utility::is_specialization_of_v && - HasGetIdentifierAndGetColour> && - std::is_same_v - ) || ( - /* IndexedMap, T derived from HasIdentifierAndColour */ - utility::is_specialization_of_v && - HasGetIdentifierAndGetColour && - std::is_same_v + /* ordered_map, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v + /* IndexedMap, T derived from HasIdentifierAndColour */ + || utility::is_specialization_of_v ) + && HasGetIdentifierAndGetColour> + && std::convertible_to ) { using namespace godot; + using key_type = std::remove_pointer_t; - using entry_t = std::pair; + using entry_t = std::pair; + std::vector sorted_dist; + sorted_dist.reserve(dist.size()); if constexpr (utility::is_specialization_of_v) { - sorted_dist.reserve(dist.size()); - for (entry_t const& entry : dist) { - ERR_CONTINUE_MSG( - entry.first == nullptr, vformat("Null distribution key with value %f", entry.second.to_float()) - ); - sorted_dist.push_back(entry); + for (auto const& [key, non_float_value] : dist) { + const float value = static_cast(non_float_value); + + ERR_CONTINUE_MSG(key == nullptr, vformat("Null distribution key with value %f", value)); + + if (value != 0.0f) { + sorted_dist.emplace_back(key, value); + } } } else { for (size_t index = 0; index < dist.size(); ++index) { - fixed_point_t const& value = dist[index]; - if (value != 0) { + const float value = static_cast(dist[index]); + + if (value != 0.0f) { key_type const* key = &dist(index); sorted_dist.emplace_back(key, value); } @@ -93,16 +82,30 @@ namespace OpenVic { ERR_FAIL_COND_V(array.resize(sorted_dist.size()) != OK, {}); for (size_t idx = 0; idx < array.size(); ++idx) { - auto const& [key, val] = sorted_dist[idx]; + auto const& [key, value] = sorted_dist[idx]; Dictionary sub_dict; sub_dict[_slice_identifier_key()] = Utilities::std_to_godot_string(key->get_identifier()); sub_dict[_slice_colour_key()] = Utilities::to_godot_color(key->get_colour()); - sub_dict[_slice_weight_key()] = val.to_float(); + sub_dict[_slice_weight_key()] = value; array[idx] = std::move(sub_dict); } return array; } + protected: + static void _bind_methods(); + + public: + GFXPieChartTexture(); + + // Position must be centred and normalised so that coords are in [-1, 1]. + slice_t const* get_slice(godot::Vector2 const& position) const; + + /* Set slices given an Array of Dictionaries, each with the following key-value entries: + * - colour: Color + * - weight: float */ + godot::Error set_slices_array(godot_pie_chart_data_t const& new_slices); + /* Create a GFXPieChartTexture using the specified GFX::PieChart. Returns nullptr if gfx_pie_chart fails. */ static godot::Ref make_gfx_pie_chart_texture(GFX::PieChart const* gfx_pie_chart); diff --git a/extension/src/openvic-extension/singletons/MenuSingleton.cpp b/extension/src/openvic-extension/singletons/MenuSingleton.cpp index 8d7f9fe..cabe82b 100644 --- a/extension/src/openvic-extension/singletons/MenuSingleton.cpp +++ b/extension/src/openvic-extension/singletons/MenuSingleton.cpp @@ -2,8 +2,10 @@ #include +#include #include #include +#include #include "openvic-extension/classes/GFXPieChartTexture.hpp" #include "openvic-extension/classes/GUINode.hpp" @@ -472,10 +474,10 @@ Dictionary MenuSingleton::get_province_info_from_index(int32_t index) const { ret[province_info_controller_key] = Utilities::std_to_godot_string(controller->get_identifier()); } - GoodDefinition const* rgo = province->get_rgo(); - if (rgo != nullptr) { - ret[province_info_rgo_name_key] = Utilities::std_to_godot_string(rgo->get_identifier()); - ret[province_info_rgo_icon_key] = static_cast(rgo->get_index()); + GoodDefinition const* const rgo_good = province->get_rgo_good(); + if (rgo_good != nullptr) { + ret[province_info_rgo_name_key] = Utilities::std_to_godot_string(rgo_good->get_identifier()); + ret[province_info_rgo_icon_key] = static_cast(rgo_good->get_index()); } Crime const* crime = province->get_crime(); diff --git a/extension/src/openvic-extension/singletons/PopulationMenu.cpp b/extension/src/openvic-extension/singletons/PopulationMenu.cpp index 0c5a82d..700c57f 100644 --- a/extension/src/openvic-extension/singletons/PopulationMenu.cpp +++ b/extension/src/openvic-extension/singletons/PopulationMenu.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "openvic-extension/classes/GFXPieChartTexture.hpp" #include "openvic-extension/classes/GUINode.hpp" @@ -366,7 +367,7 @@ Error MenuSingleton::_population_menu_update_pops() { if (province_entry != nullptr && province_entry->selected) { for (Pop const& pop : province_entry->province.get_pops()) { population_menu.pops.push_back(&pop); - population_menu_t::pop_filter_t& filter = population_menu.pop_filters[&pop.get_type()]; + population_menu_t::pop_filter_t& filter = population_menu.pop_filters[pop.get_type()]; filter.count += pop.get_size(); // TODO - set filter.promotion_demotion_change } @@ -387,7 +388,7 @@ Error MenuSingleton::_population_menu_update_filtered_pops() { population_menu.vote_distribution.clear(); for (Pop const* pop : population_menu.pops) { - if (population_menu.pop_filters[&pop->get_type()].selected) { + if (population_menu.pop_filters[pop->get_type()].selected) { population_menu.filtered_pops.push_back(pop); } } @@ -395,12 +396,12 @@ Error MenuSingleton::_population_menu_update_filtered_pops() { for (Pop const* pop : population_menu.filtered_pops) { const fixed_point_t pop_size = fixed_point_t::parse(pop->get_size()); - population_menu.workforce_distribution[&pop->get_type()] += pop->get_size(); - population_menu.religion_distribution[&pop->get_religion()] += pop->get_size(); - population_menu.ideology_distribution += pop->get_ideologies() * fixed_point_t::parse(pop->get_size()); - population_menu.culture_distribution[&pop->get_culture()] += pop->get_size(); - population_menu.issue_distribution += pop->get_issues() * fixed_point_t::parse(pop->get_size()); - population_menu.vote_distribution += pop->get_votes() * fixed_point_t::parse(pop->get_size()); + population_menu.workforce_distribution[pop->get_type()] += pop_size; + population_menu.religion_distribution[&pop->get_religion()] += pop_size; + population_menu.ideology_distribution += pop->get_ideologies() * pop_size; + population_menu.culture_distribution[&pop->get_culture()] += pop_size; + population_menu.issue_distribution += pop->get_issues() * pop_size; + population_menu.vote_distribution += pop->get_votes() * pop_size; } normalise_fixed_point_map(population_menu.workforce_distribution); @@ -678,7 +679,7 @@ TypedArray MenuSingleton::get_population_menu_pop_rows(int32_t start Dictionary pop_dict; pop_dict[pop_size_key] = pop->get_size(); - pop_dict[pop_type_icon_key] = pop->get_type().get_sprite(); + pop_dict[pop_type_icon_key] = pop->get_type()->get_sprite(); pop_dict[pop_culture_key] = Utilities::std_to_godot_string(pop->get_culture().get_identifier()); pop_dict[pop_religion_icon_key] = pop->get_religion().get_icon(); if (pop->get_location() != nullptr) { -- cgit v1.2.3-56-ga3b1