From 49d3ad6eceeb061f2a3aaf7fd711858e168163de Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 7 Dec 2023 22:45:19 +0000 Subject: [PATCH] Big UI commit - GUINode, MaskedFlag, PieChart, etc --- extension/deps/openvic-simulation | 2 +- extension/src/openvic-extension/UIAdapter.cpp | 171 +++++++------ extension/src/openvic-extension/UIAdapter.hpp | 27 +- .../classes/GFXMaskedFlagTexture.cpp | 183 ++++++++++++++ .../classes/GFXMaskedFlagTexture.hpp | 55 ++++ .../classes/GFXPieChartTexture.cpp | 167 ++++++++++++ .../classes/GFXPieChartTexture.hpp | 49 ++++ .../src/openvic-extension/classes/GUINode.cpp | 161 ++++++++++++ .../src/openvic-extension/classes/GUINode.hpp | 53 ++++ .../src/openvic-extension/register_types.cpp | 6 + .../singletons/GameSingleton.cpp | 174 +++++++++++-- .../singletons/GameSingleton.hpp | 11 +- .../openvic-extension/utility/Utilities.cpp | 31 +++ .../openvic-extension/utility/Utilities.hpp | 4 + game/src/Game/Autoload/Events.gd | 2 - game/src/Game/Autoload/Events/Loader.gd | 6 - game/src/Game/GameSession/GameSession.gd | 6 - game/src/Game/GameSession/GameSession.tscn | 15 +- game/src/Game/GameSession/GameSpeedPanel.gd | 43 ---- game/src/Game/GameSession/GameSpeedPanel.tscn | 67 ----- .../Game/GameSession/ProvinceOverviewPanel.gd | 237 +++++++++++++----- game/src/Game/GameSession/Topbar.gd | 84 +++++++ game/src/Game/LoadingScreen.gd | 6 - 23 files changed, 1241 insertions(+), 319 deletions(-) create mode 100644 extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp create mode 100644 extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp create mode 100644 extension/src/openvic-extension/classes/GFXPieChartTexture.cpp create mode 100644 extension/src/openvic-extension/classes/GFXPieChartTexture.hpp create mode 100644 extension/src/openvic-extension/classes/GUINode.cpp create mode 100644 extension/src/openvic-extension/classes/GUINode.hpp delete mode 100644 game/src/Game/Autoload/Events/Loader.gd delete mode 100644 game/src/Game/GameSession/GameSpeedPanel.gd delete mode 100644 game/src/Game/GameSession/GameSpeedPanel.tscn create mode 100644 game/src/Game/GameSession/Topbar.gd diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index 444a2772..34fbe278 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit 444a27726695478e44e0166e75df1f354b6432d5 +Subproject commit 34fbe27857d77d6f193d6d1848b953a4596cedd2 diff --git a/extension/src/openvic-extension/UIAdapter.cpp b/extension/src/openvic-extension/UIAdapter.cpp index 1478a5af..cbe898c8 100644 --- a/extension/src/openvic-extension/UIAdapter.cpp +++ b/extension/src/openvic-extension/UIAdapter.cpp @@ -12,6 +12,8 @@ #include #include "openvic-extension/classes/GFXIconTexture.hpp" +#include "openvic-extension/classes/GFXMaskedFlagTexture.hpp" +#include "openvic-extension/classes/GFXPieChartTexture.hpp" #include "openvic-extension/utility/Utilities.hpp" using namespace godot; @@ -20,12 +22,14 @@ using namespace OpenVic; using OpenVic::Utilities::std_view_to_godot_string; using OpenVic::Utilities::std_view_to_godot_string_name; -bool GodotGUIBuilder::generate_element(GUI::Element const* element, AssetManager& asset_manager, Control*& result) { +bool GodotGUIBuilder::generate_element( + GUI::Element const* element, String const& name, AssetManager& asset_manager, Control*& result +) { if (element == nullptr) { UtilityFunctions::push_error("Invalid element passed to GodotGUIBuilder - null!"); return false; } - static const std::map type_map { + static const std::map type_map { { GUI::Icon::get_type_static(), &generate_icon }, { GUI::Button::get_type_static(), &generate_button }, { GUI::Checkbox::get_type_static(), &generate_checkbox }, @@ -36,7 +40,7 @@ bool GodotGUIBuilder::generate_element(GUI::Element const* element, AssetManager }; const decltype(type_map)::const_iterator it = type_map.find(element->get_type()); if (it != type_map.end()) { - return it->second(*element, asset_manager, result); + return it->second(*element, name, asset_manager, result); } else { UtilityFunctions::push_error("Invalid GUI element type: ", std_view_to_godot_string(element->get_type())); result = nullptr; @@ -45,7 +49,7 @@ bool GodotGUIBuilder::generate_element(GUI::Element const* element, AssetManager } template T> -static T* new_control(GUI::Element const& element) { +static T* new_control(GUI::Element const& element, String const& name) { T* node = memnew(T); ERR_FAIL_NULL_V(node, nullptr); @@ -57,7 +61,12 @@ static T* new_control(GUI::Element const& element) { { CENTER, PRESET_CENTER } }; - node->set_name(std_view_to_godot_string(element.get_name())); + if (name.is_empty()) { + node->set_name(std_view_to_godot_string(element.get_name())); + } else { + node->set_name(name); + } + const decltype(orientation_map)::const_iterator it = orientation_map.find(element.get_orientation()); if (it != orientation_map.end()) { node->set_anchors_and_offsets_preset(it->second); @@ -71,7 +80,9 @@ static T* new_control(GUI::Element const& element) { return node; } -bool GodotGUIBuilder::generate_icon(GUI::Element const& element, AssetManager& asset_manager, Control*& result) { +bool GodotGUIBuilder::generate_icon( + GUI::Element const& element, String const& name, AssetManager& asset_manager, Control*& result +) { GUI::Icon const& icon = static_cast(element); result = nullptr; @@ -81,11 +92,8 @@ bool GodotGUIBuilder::generate_icon(GUI::Element const& element, AssetManager& a bool ret = true; if (icon.get_sprite() != nullptr) { if (icon.get_sprite()->is_type()) { - TextureRect* godot_texture_rect = new_control(icon); - if (godot_texture_rect == nullptr) { - UtilityFunctions::push_error("Failed to create TextureRect for GUI icon ", icon_name); - return false; - } + TextureRect* godot_texture_rect = new_control(icon, name); + ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name)); GFX::TextureSprite const* texture_sprite = icon.get_sprite()->cast_to(); Ref texture = GFXIconTexture::make_gfx_icon_texture(texture_sprite, icon.get_frame()); @@ -98,29 +106,24 @@ bool GodotGUIBuilder::generate_icon(GUI::Element const& element, AssetManager& a result = godot_texture_rect; } else if (icon.get_sprite()->is_type()) { - TextureRect* godot_texture_rect = new_control(icon); - if (godot_texture_rect == nullptr) { - UtilityFunctions::push_error("Failed to create TextureRect for GUI icon ", icon_name); - return false; - } + TextureRect* godot_texture_rect = new_control(icon, name); + ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name)); - const StringName texture_file = - std_view_to_godot_string_name(icon.get_sprite()->cast_to()->get_overlay_file()); - const Ref texture = asset_manager.get_texture(texture_file); + GFX::MaskedFlag const* masked_flag = icon.get_sprite()->cast_to(); + Ref texture = GFXMaskedFlagTexture::make_gfx_masked_flag_texture(masked_flag); if (texture.is_valid()) { godot_texture_rect->set_texture(texture); } else { - UtilityFunctions::push_error("Failed to load masked flag sprite ", texture_file, " for GUI icon ", icon_name); + UtilityFunctions::push_error("Failed to make GFXMaskedFlagTexture for GUI icon ", icon_name); ret = false; } result = godot_texture_rect; } else if (icon.get_sprite()->is_type()) { - TextureProgressBar* godot_progress_bar = new_control(icon); - if (godot_progress_bar == nullptr) { - UtilityFunctions::push_error("Failed to create TextureProgressBar for GUI icon ", icon_name); - return false; - } + TextureProgressBar* godot_progress_bar = new_control(icon, name); + ERR_FAIL_NULL_V_MSG( + godot_progress_bar, false, vformat("Failed to create TextureProgressBar for GUI icon %s", icon_name) + ); const StringName back_texture_file = std_view_to_godot_string_name(icon.get_sprite()->cast_to()->get_back_texture_file()); @@ -146,7 +149,23 @@ bool GodotGUIBuilder::generate_icon(GUI::Element const& element, AssetManager& a result = godot_progress_bar; } else if (icon.get_sprite()->is_type()) { + TextureRect* godot_texture_rect = new_control(icon, name); + ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name)); + + GFX::PieChart const* pie_chart = icon.get_sprite()->cast_to(); + Ref texture = GFXPieChartTexture::make_gfx_pie_chart_texture(pie_chart); + if (texture.is_valid()) { + godot_texture_rect->set_texture(texture); + // TODO - work out why this is needed + Vector2 pos = godot_texture_rect->get_position(); + pos.x -= texture->get_width() / 2; + godot_texture_rect->set_position(pos); + } else { + UtilityFunctions::push_error("Failed to make GFXPieChartTexture for GUI icon ", icon_name); + ret = false; + } + result = godot_texture_rect; } else if (icon.get_sprite()->is_type()) { } else { @@ -161,21 +180,21 @@ bool GodotGUIBuilder::generate_icon(GUI::Element const& element, AssetManager& a return ret; } -bool GodotGUIBuilder::generate_button(GUI::Element const& element, AssetManager& asset_manager, Control*& result) { +bool GodotGUIBuilder::generate_button( + GUI::Element const& element, String const& name, AssetManager& asset_manager, Control*& result +) { GUI::Button const& button = static_cast(element); // TODO - shortcut, sprite, text result = nullptr; const String button_name = std_view_to_godot_string(button.get_name()); - Button* godot_button = new_control