From fb9672c39a2cba01b279a473e1f8076cea461393 Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 29 Aug 2024 23:36:01 +0100 Subject: Use government based flag types in UI --- extension/deps/openvic-simulation | 2 +- .../classes/GFXMaskedFlagTexture.cpp | 42 +++++++++++++++++----- .../classes/GFXMaskedFlagTexture.hpp | 5 +-- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index 5813948..8d5068c 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit 5813948cd3ed6432de374664650d68afbff71915 +Subproject commit 8d5068c1db9a02a34d675536f41aee61ec3e482a diff --git a/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp b/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp index 3714c5f..41eaa4d 100644 --- a/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp @@ -96,7 +96,9 @@ Ref GFXMaskedFlagTexture::make_gfx_masked_flag_texture(GFX Ref masked_flag_texture; masked_flag_texture.instantiate(); ERR_FAIL_NULL_V(masked_flag_texture, nullptr); + ERR_FAIL_COND_V(masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag) != OK, nullptr); + return masked_flag_texture; } @@ -116,10 +118,12 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag(GFX::MaskedFlag const* new_gfx_m if (gfx_masked_flag == new_gfx_masked_flag) { return OK; } + if (new_gfx_masked_flag == nullptr) { clear(); return OK; } + AssetManager* asset_manager = AssetManager::get_singleton(); ERR_FAIL_NULL_V(asset_manager, FAILED); @@ -142,8 +146,10 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_fl if (gfx_masked_flag_name.is_empty()) { return set_gfx_masked_flag(nullptr); } + GFX::Sprite const* sprite = UITools::get_gfx_sprite(gfx_masked_flag_name); ERR_FAIL_NULL_V(sprite, FAILED); + GFX::MaskedFlag const* new_masked_flag = sprite->cast_to(); ERR_FAIL_NULL_V_MSG( new_masked_flag, FAILED, vformat( @@ -152,6 +158,7 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_fl Utilities::std_to_godot_string(GFX::MaskedFlag::get_type_static()) ) ); + return set_gfx_masked_flag(new_masked_flag); } @@ -165,8 +172,9 @@ Error GFXMaskedFlagTexture::set_flag_country_and_type( if (flag_country == new_flag_country && flag_type == new_flag_type) { return OK; } + if (new_flag_country != nullptr) { - GameSingleton* game_singleton = GameSingleton::get_singleton(); + GameSingleton const* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, FAILED); flag_image_rect = game_singleton->get_flag_sheet_rect(new_flag_country->get_index(), new_flag_type); @@ -181,6 +189,7 @@ Error GFXMaskedFlagTexture::set_flag_country_and_type( flag_type = String {}; flag_image.unref(); } + return _generate_combined_image(); } @@ -190,32 +199,49 @@ Error GFXMaskedFlagTexture::set_flag_country_name_and_type( if (new_flag_country_name.is_empty()) { return set_flag_country_and_type(nullptr, {}); } - GameSingleton* game_singleton = GameSingleton::get_singleton(); + + GameSingleton const* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, FAILED); + CountryDefinition const* new_flag_country = game_singleton->get_definition_manager().get_country_definition_manager().get_country_definition_by_identifier( Utilities::godot_to_std_string(new_flag_country_name) ); ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, vformat("Country not found: %s", new_flag_country_name)); + return set_flag_country_and_type(new_flag_country, new_flag_type); } -Error GFXMaskedFlagTexture::set_flag_country(CountryDefinition const* new_flag_country) { - // TODO - get country's current flag type from the game state - return set_flag_country_and_type( new_flag_country, {}); +Error GFXMaskedFlagTexture::set_flag_country(CountryInstance const* new_flag_country) { + if (new_flag_country == nullptr || new_flag_country->get_country_definition() == nullptr) { + return set_flag_country_and_type(nullptr, {}); + } + + GovernmentType const* government_type = new_flag_country->get_flag_government_type(); + + const StringName new_flag_type = government_type != nullptr + ? Utilities::std_to_godot_string(government_type->get_flag_type()) : String {}; + + return set_flag_country_and_type(new_flag_country->get_country_definition(), new_flag_type); } Error GFXMaskedFlagTexture::set_flag_country_name(String const& new_flag_country_name) { if (new_flag_country_name.is_empty()) { return set_flag_country(nullptr); } - GameSingleton* game_singleton = GameSingleton::get_singleton(); + + GameSingleton const* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, FAILED); - CountryDefinition const* new_flag_country = - game_singleton->get_definition_manager().get_country_definition_manager().get_country_definition_by_identifier( + + InstanceManager const* instance_manager = game_singleton->get_instance_manager(); + ERR_FAIL_NULL_V(instance_manager, FAILED); + + CountryInstance const* new_flag_country = + instance_manager->get_country_instance_manager().get_country_instance_by_identifier( Utilities::godot_to_std_string(new_flag_country_name) ); ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, vformat("Country not found: %s", new_flag_country_name)); + return set_flag_country(new_flag_country); } diff --git a/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp b/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp index 3d361e4..b26f0dd 100644 --- a/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp @@ -6,6 +6,7 @@ namespace OpenVic { struct CountryDefinition; + struct CountryInstance; class GFXMaskedFlagTexture : public GFXButtonStateHavingTexture { GDCLASS(GFXMaskedFlagTexture, GFXButtonStateHavingTexture) @@ -55,9 +56,9 @@ namespace OpenVic { /* Look up the specified country's current flag type, then call set_flag_country_and_type * with the country and its flag type as arguments. */ - godot::Error set_flag_country(CountryDefinition const* new_flag_country); + godot::Error set_flag_country(CountryInstance const* new_flag_country); - /* Look up the country with the specified identifier, then call set_flag_country with the country its argument. */ + /* Look up the country with the specified identifier, then call set_flag_country with the country as its argument. */ godot::Error set_flag_country_name(godot::String const& new_flag_country_name); /* Return the name of the selected flag's country, or an empty String if it's null. */ -- cgit v1.2.3-56-ga3b1