From 1455861632cd50f48f6e8ef8c50004087eff36f1 Mon Sep 17 00:00:00 2001 From: hop311 Date: Fri, 2 Feb 2024 20:19:21 +0000 Subject: Basic Nation Management Screen framework --- extension/deps/openvic-simulation | 2 +- .../openvic-extension/classes/GFXIconTexture.cpp | 2 - .../classes/GFXPieChartTexture.cpp | 6 +- .../src/openvic-extension/classes/GUINode.cpp | 26 ++-- .../src/openvic-extension/classes/GUINode.hpp | 6 +- .../src/openvic-extension/utility/UITools.cpp | 135 +++++++++++++++------ .../src/openvic-extension/utility/UITools.hpp | 5 +- .../src/openvic-extension/utility/Utilities.cpp | 2 - .../src/openvic-extension/utility/Utilities.hpp | 16 +-- game/src/Game/Autoload/Events.gd | 6 +- .../Autoload/Events/NationManagementScreens.gd | 25 ++++ game/src/Game/GameSession/GameSession.tscn | 50 +++++++- .../NationManagementScreen/BudgetMenu.gd | 34 ++++++ .../NationManagementScreen/DiplomacyMenu.gd | 34 ++++++ .../NationManagementScreen/MilitaryMenu.gd | 34 ++++++ .../NationManagementScreen/PoliticsMenu.gd | 34 ++++++ .../NationManagementScreen/PopulationMenu.gd | 34 ++++++ .../NationManagementScreen/ProductionMenu.gd | 34 ++++++ .../NationManagementScreen/TechnologyMenu.gd | 34 ++++++ .../NationManagementScreen/TradeMenu.gd | 34 ++++++ game/src/Game/GameSession/ProvinceOverviewPanel.gd | 8 +- game/src/Game/GameSession/Topbar.gd | 41 ++++++- game/src/Game/GameStart.gd | 2 +- game/src/Game/GlobalClass/NationManagement.gd | 13 ++ 24 files changed, 539 insertions(+), 78 deletions(-) create mode 100644 game/src/Game/Autoload/Events/NationManagementScreens.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/BudgetMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/DiplomacyMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/MilitaryMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/PoliticsMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/PopulationMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/ProductionMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/TechnologyMenu.gd create mode 100644 game/src/Game/GameSession/NationManagementScreen/TradeMenu.gd create mode 100644 game/src/Game/GlobalClass/NationManagement.gd diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index 068c13e..5216e89 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit 068c13ede817d17df599ca3481261bf17ed95604 +Subproject commit 5216e893ebc5253b123bbf15b7509745d38f5a80 diff --git a/extension/src/openvic-extension/classes/GFXIconTexture.cpp b/extension/src/openvic-extension/classes/GFXIconTexture.cpp index 5d29c07..99df7e4 100644 --- a/extension/src/openvic-extension/classes/GFXIconTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXIconTexture.cpp @@ -3,7 +3,6 @@ #include #include "openvic-extension/singletons/AssetManager.hpp" -#include "openvic-extension/singletons/GameSingleton.hpp" #include "openvic-extension/utility/ClassBindings.hpp" #include "openvic-extension/utility/UITools.hpp" #include "openvic-extension/utility/Utilities.hpp" @@ -11,7 +10,6 @@ using namespace godot; using namespace OpenVic; -using OpenVic::Utilities::godot_to_std_string; using OpenVic::Utilities::std_view_to_godot_string; using OpenVic::Utilities::std_view_to_godot_string_name; diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp index c9a2a72..f4c7851 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp @@ -1,16 +1,14 @@ #include "GFXPieChartTexture.hpp" -#include "openvic-extension/singletons/AssetManager.hpp" -#include "openvic-extension/singletons/GameSingleton.hpp" +#include + #include "openvic-extension/utility/ClassBindings.hpp" #include "openvic-extension/utility/UITools.hpp" using namespace godot; using namespace OpenVic; -using OpenVic::Utilities::godot_to_std_string; using OpenVic::Utilities::std_view_to_godot_string; -using OpenVic::Utilities::std_view_to_godot_string_name; StringName const& GFXPieChartTexture::_slice_identifier_key() { static StringName const slice_identifier_key = "identifier"; diff --git a/extension/src/openvic-extension/classes/GUINode.cpp b/extension/src/openvic-extension/classes/GUINode.cpp index 89701e0..f95e3cd 100644 --- a/extension/src/openvic-extension/classes/GUINode.cpp +++ b/extension/src/openvic-extension/classes/GUINode.cpp @@ -10,9 +10,6 @@ using namespace godot; using namespace OpenVic; -using OpenVic::Utilities::godot_to_std_string; -using OpenVic::Utilities::std_view_to_godot_string; - #define APPLY_TO_CHILD_TYPES(F) \ F(Button, button) \ F(CheckBox, check_box) \ @@ -28,8 +25,9 @@ using OpenVic::Utilities::std_view_to_godot_string; F(GFXPieChartTexture, gfx_pie_chart_texture) void GUINode::_bind_methods() { - OV_BIND_SMETHOD(generate_gui_element, { "gui_file", "gui_element", "name" }, DEFVAL(String {})); - OV_BIND_METHOD(GUINode::add_gui_element, { "gui_file", "gui_element", "name" }, DEFVAL(String {})); + OV_BIND_SMETHOD(generate_gui_element, { "gui_scene", "gui_element", "name" }, DEFVAL(String {})); + OV_BIND_METHOD(GUINode::add_gui_element, { "gui_scene", "gui_element", "name" }, DEFVAL(String {})); + OV_BIND_SMETHOD(get_gui_position, { "gui_scene", "gui_position" }); #define GET_BINDINGS(type, name) \ OV_BIND_SMETHOD(get_##name##_from_node, { "node" }); \ @@ -55,19 +53,19 @@ GUINode::GUINode() { set_mouse_filter(MOUSE_FILTER_IGNORE); } -Control* GUINode::generate_gui_element(String const& gui_file, String const& gui_element, String const& name) { +Control* GUINode::generate_gui_element(String const& gui_scene, String const& gui_element, String const& name) { Control* result = nullptr; - if (!UITools::generate_gui_element(gui_file, gui_element, name, result)) { - UtilityFunctions::push_error("Error generating GUI element ", gui_element, " from GUI file ", gui_file); + if (!UITools::generate_gui_element(gui_scene, gui_element, name, result)) { + UtilityFunctions::push_error("Error generating GUI element ", gui_element, " from GUI scene ", gui_scene); } return result; } -Error GUINode::add_gui_element(String const& gui_file, String const& gui_element, String const& name) { +Error GUINode::add_gui_element(String const& gui_scene, String const& gui_element, String const& name) { Error err = OK; Control* result = nullptr; - if (!UITools::generate_gui_element(gui_file, gui_element, name, result)) { - UtilityFunctions::push_error("Error generating GUI element ", gui_element, " from GUI file ", gui_file); + if (!UITools::generate_gui_element(gui_scene, gui_element, name, result)) { + UtilityFunctions::push_error("Error generating GUI element ", gui_element, " from GUI scene ", gui_scene); err = FAILED; } if (result != nullptr) { @@ -76,6 +74,12 @@ Error GUINode::add_gui_element(String const& gui_file, String const& gui_element return err; } +Vector2 GUINode::get_gui_position(String const& gui_scene, String const& gui_position) { + GUI::Position const* position = UITools::get_gui_position(gui_scene, gui_position); + ERR_FAIL_NULL_V(position, {}); + return Utilities::to_godot_fvec2(position->get_position()); +} + template T> static T* _cast_node(Node* node) { ERR_FAIL_NULL_V(node, nullptr); diff --git a/extension/src/openvic-extension/classes/GUINode.hpp b/extension/src/openvic-extension/classes/GUINode.hpp index e38ed1f..0fbfc66 100644 --- a/extension/src/openvic-extension/classes/GUINode.hpp +++ b/extension/src/openvic-extension/classes/GUINode.hpp @@ -25,13 +25,15 @@ namespace OpenVic { GUINode(); static godot::Control* generate_gui_element( - godot::String const& gui_file, godot::String const& gui_element, godot::String const& name = "" + godot::String const& gui_scene, godot::String const& gui_element, godot::String const& name = "" ); godot::Error add_gui_element( - godot::String const& gui_file, godot::String const& gui_element, godot::String const& name = "" + godot::String const& gui_scene, godot::String const& gui_element, godot::String const& name = "" ); + static godot::Vector2 get_gui_position(godot::String const& gui_scene, godot::String const& gui_position); + static godot::Button* get_button_from_node(godot::Node* node); static godot::CheckBox* get_check_box_from_node(godot::Node* node); static godot::Label* get_label_from_node(godot::Node* node); diff --git a/extension/src/openvic-extension/utility/UITools.cpp b/extension/src/openvic-extension/utility/UITools.cpp index e1cd873..1fcc574 100644 --- a/extension/src/openvic-extension/utility/UITools.cpp +++ b/extension/src/openvic-extension/utility/UITools.cpp @@ -37,17 +37,28 @@ GFX::Sprite const* UITools::get_gfx_sprite(godot::String const& gfx_sprite) { return sprite; } -GUI::Element const* UITools::get_gui_element(godot::String const& gui_file, godot::String const& gui_element) { +GUI::Element const* UITools::get_gui_element(godot::String const& gui_scene, godot::String const& gui_element) { GameSingleton const* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, nullptr); GUI::Scene const* scene = - game_singleton->get_game_manager().get_ui_manager().get_scene_by_identifier(godot_to_std_string(gui_file)); - ERR_FAIL_NULL_V_MSG(scene, nullptr, vformat("Failed to find GUI file %s", gui_file)); + game_singleton->get_game_manager().get_ui_manager().get_scene_by_identifier(godot_to_std_string(gui_scene)); + ERR_FAIL_NULL_V_MSG(scene, nullptr, vformat("Failed to find GUI scene %s", gui_scene)); GUI::Element const* element = scene->get_scene_element_by_identifier(godot_to_std_string(gui_element)); - ERR_FAIL_NULL_V_MSG(element, nullptr, vformat("Failed to find GUI element %s in GUI file %s", gui_element, gui_file)); + ERR_FAIL_NULL_V_MSG(element, nullptr, vformat("Failed to find GUI element %s in GUI scene %s", gui_element, gui_scene)); return element; } +GUI::Position const* UITools::get_gui_position(String const& gui_scene, String const& gui_position) { + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + ERR_FAIL_NULL_V(game_singleton, nullptr); + GUI::Scene const* scene = + game_singleton->get_game_manager().get_ui_manager().get_scene_by_identifier(godot_to_std_string(gui_scene)); + ERR_FAIL_NULL_V_MSG(scene, nullptr, vformat("Failed to find GUI scene %s", gui_scene)); + GUI::Position const* position = scene->get_scene_position_by_identifier(godot_to_std_string(gui_position)); + ERR_FAIL_NULL_V_MSG(position, nullptr, vformat("Failed to find GUI position %s in GUI scene %s", gui_position, gui_scene)); + return position; +} + /* GUI::Element tree -> godot::Control tree conversion code below: */ namespace OpenVic { @@ -65,9 +76,9 @@ namespace OpenVic { } template T> -static T* new_control(GUI::Element const& element, String const& name) { - T* node = memnew(T); - ERR_FAIL_NULL_V(node, nullptr); +static bool new_control(T*& node, GUI::Element const& element, String const& name) { + node = memnew(T); + ERR_FAIL_NULL_V(node, false); using enum GUI::Element::orientation_t; using enum Control::LayoutPreset; @@ -83,20 +94,32 @@ static T* new_control(GUI::Element const& element, String const& name) { node->set_name(name); } + bool ret = true; 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); } else { UtilityFunctions::push_error("Invalid orientation for GUI element ", std_view_to_godot_string(element.get_name())); + ret = false; } + node->set_position(Utilities::to_godot_fvec2(element.get_position())); node->set_h_size_flags(Control::SizeFlags::SIZE_SHRINK_BEGIN); node->set_v_size_flags(Control::SizeFlags::SIZE_SHRINK_BEGIN); node->set_focus_mode(Control::FOCUS_NONE); - return node; + return ret; } +static bool add_theme_stylebox(Control* control, StringName const& theme_name, Ref const& texture) { + Ref stylebox; + stylebox.instantiate(); + ERR_FAIL_NULL_V(stylebox, false); + stylebox->set_texture(texture); + control->add_theme_stylebox_override(theme_name, stylebox); + return true; +}; + static bool generate_icon(generate_gui_args_t&& args) { GUI::Icon const& icon = static_cast(args.element); @@ -106,7 +129,8 @@ static bool generate_icon(generate_gui_args_t&& args) { bool ret = true; if (icon.get_sprite() != nullptr) { if (icon.get_sprite()->is_type()) { - TextureRect* godot_texture_rect = new_control(icon, args.name); + TextureRect* godot_texture_rect = nullptr; + ret &= new_control(godot_texture_rect, icon, args.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(); @@ -120,7 +144,8 @@ static bool generate_icon(generate_gui_args_t&& args) { args.result = godot_texture_rect; } else if (icon.get_sprite()->is_type()) { - TextureRect* godot_texture_rect = new_control(icon, args.name); + TextureRect* godot_texture_rect = nullptr; + ret &= new_control(godot_texture_rect, icon, args.name); ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name)); GFX::MaskedFlag const* masked_flag = icon.get_sprite()->cast_to(); @@ -134,7 +159,8 @@ static bool generate_icon(generate_gui_args_t&& args) { args.result = godot_texture_rect; } else if (icon.get_sprite()->is_type()) { - TextureProgressBar* godot_progress_bar = new_control(icon, args.name); + TextureProgressBar* godot_progress_bar = nullptr; + ret &= new_control(godot_progress_bar, icon, args.name); ERR_FAIL_NULL_V_MSG( godot_progress_bar, false, vformat("Failed to create TextureProgressBar for GUI icon %s", icon_name) ); @@ -210,7 +236,8 @@ static bool generate_icon(generate_gui_args_t&& args) { args.result = godot_progress_bar; } else if (icon.get_sprite()->is_type()) { - TextureRect* godot_texture_rect = new_control(icon, args.name); + TextureRect* godot_texture_rect = nullptr; + ret &= new_control(godot_texture_rect, icon, args.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(); @@ -219,7 +246,7 @@ static bool generate_icon(generate_gui_args_t&& args) { 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; + pos.x -= texture->get_width() / 2.0f; godot_texture_rect->set_position(pos); } else { UtilityFunctions::push_error("Failed to make GFXPieChartTexture for GUI icon ", icon_name); @@ -228,12 +255,18 @@ static bool generate_icon(generate_gui_args_t&& args) { args.result = godot_texture_rect; } else if (icon.get_sprite()->is_type()) { - + // TODO - generate line chart } else { UtilityFunctions::push_error("Invalid sprite type ", std_view_to_godot_string(icon.get_sprite()->get_type()), " for GUI icon ", icon_name); ret = false; } + + if (args.result != nullptr) { + const float scale = icon.get_scale(); + args.result->set_scale({ scale, scale }); + // TODO - rotation (may have to translate as godot rotates around the top left corner) + } } else { UtilityFunctions::push_error("Null sprite for GUI icon ", icon_name); ret = false; @@ -247,7 +280,8 @@ static bool generate_button(generate_gui_args_t&& args) { // TODO - shortcut, sprite, text const String button_name = std_view_to_godot_string(button.get_name()); - Button* godot_button = new_control