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 --- extension/deps/openvic-simulation | 2 +- extension/src/openvic-extension/UIAdapter.cpp | 419 ------------------- extension/src/openvic-extension/UIAdapter.hpp | 28 -- .../openvic-extension/classes/GFXIconTexture.cpp | 9 +- .../openvic-extension/classes/GFXIconTexture.hpp | 4 +- .../classes/GFXMaskedFlagTexture.cpp | 31 +- .../classes/GFXMaskedFlagTexture.hpp | 11 +- .../classes/GFXPieChartTexture.cpp | 40 +- .../classes/GFXPieChartTexture.hpp | 41 +- .../src/openvic-extension/classes/GUINode.cpp | 173 ++++---- .../src/openvic-extension/classes/GUINode.hpp | 50 ++- .../classes/GUIOverlappingElementsBox.cpp | 177 ++++++++ .../classes/GUIOverlappingElementsBox.hpp | 51 +++ extension/src/openvic-extension/register_types.cpp | 2 + .../openvic-extension/singletons/AssetManager.cpp | 45 --- .../openvic-extension/singletons/AssetManager.hpp | 11 - .../openvic-extension/singletons/GameSingleton.cpp | 135 ++++--- .../openvic-extension/singletons/GameSingleton.hpp | 6 +- .../src/openvic-extension/utility/UITools.cpp | 450 +++++++++++++++++++++ .../src/openvic-extension/utility/UITools.hpp | 18 + .../src/openvic-extension/utility/Utilities.cpp | 15 + .../src/openvic-extension/utility/Utilities.hpp | 20 +- 22 files changed, 1011 insertions(+), 727 deletions(-) delete mode 100644 extension/src/openvic-extension/UIAdapter.cpp delete mode 100644 extension/src/openvic-extension/UIAdapter.hpp create mode 100644 extension/src/openvic-extension/classes/GUIOverlappingElementsBox.cpp create mode 100644 extension/src/openvic-extension/classes/GUIOverlappingElementsBox.hpp create mode 100644 extension/src/openvic-extension/utility/UITools.cpp create mode 100644 extension/src/openvic-extension/utility/UITools.hpp (limited to 'extension') diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index 14e47d5..bff91f7 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit 14e47d58b85f657ec1fed8abf88219f09bd3efbb +Subproject commit bff91f78f9c5339079c10adfbf8232e5159c1a2d diff --git a/extension/src/openvic-extension/UIAdapter.cpp b/extension/src/openvic-extension/UIAdapter.cpp deleted file mode 100644 index cbe898c..0000000 --- a/extension/src/openvic-extension/UIAdapter.cpp +++ /dev/null @@ -1,419 +0,0 @@ -#include "UIAdapter.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#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; -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, 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 { - { GUI::Icon::get_type_static(), &generate_icon }, - { GUI::Button::get_type_static(), &generate_button }, - { GUI::Checkbox::get_type_static(), &generate_checkbox }, - { GUI::Text::get_type_static(), &generate_text }, - { GUI::OverlappingElementsBox::get_type_static(), &generate_overlapping_elements }, - { GUI::ListBox::get_type_static(), &generate_listbox }, - { GUI::Window::get_type_static(), &generate_window } - }; - const decltype(type_map)::const_iterator it = type_map.find(element->get_type()); - if (it != type_map.end()) { - 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; - return false; - } -} - -template T> -static T* new_control(GUI::Element const& element, String const& name) { - T* node = memnew(T); - ERR_FAIL_NULL_V(node, nullptr); - - using enum GUI::Element::orientation_t; - using enum Control::LayoutPreset; - static const std::map orientation_map { - { UPPER_LEFT, PRESET_TOP_LEFT }, { LOWER_LEFT, PRESET_BOTTOM_LEFT }, - { LOWER_RIGHT, PRESET_BOTTOM_RIGHT }, { UPPER_RIGHT, PRESET_TOP_RIGHT }, - { CENTER, PRESET_CENTER } - }; - - 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); - } else { - UtilityFunctions::push_error("Invalid orientation for GUI element ", - std_view_to_godot_string(element.get_name())); - } - node->set_position(Utilities::to_godot_fvec2(element.get_position())); - node->set_focus_mode(Control::FOCUS_NONE); - - return node; -} - -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; - const String icon_name = std_view_to_godot_string(icon.get_name()); - - /* Change to use sprite type to choose Godot node type! */ - bool ret = true; - if (icon.get_sprite() != nullptr) { - 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::TextureSprite const* texture_sprite = icon.get_sprite()->cast_to(); - Ref texture = GFXIconTexture::make_gfx_icon_texture(texture_sprite, icon.get_frame()); - if (texture.is_valid()) { - godot_texture_rect->set_texture(texture); - } else { - UtilityFunctions::push_error("Failed to make GFXIconTexture for GUI icon ", icon_name); - ret = false; - } - - result = godot_texture_rect; - } 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::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 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, 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()); - const Ref back_texture = asset_manager.get_texture(back_texture_file); - if (back_texture.is_valid()) { - godot_progress_bar->set_under_texture(back_texture); - } else { - UtilityFunctions::push_error("Failed to load progress bar base sprite ", back_texture_file, " for GUI icon ", icon_name); - ret = false; - } - - const StringName progress_texture_file = - std_view_to_godot_string_name(icon.get_sprite()->cast_to()->get_progress_texture_file()); - const Ref progress_texture = asset_manager.get_texture(progress_texture_file); - if (progress_texture.is_valid()) { - godot_progress_bar->set_progress_texture(progress_texture); - } else { - UtilityFunctions::push_error( - "Failed to load progress bar base sprite ", progress_texture_file, " for GUI icon ", icon_name - ); - ret = false; - } - - 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 { - UtilityFunctions::push_error("Invalid sprite type ", std_view_to_godot_string(icon.get_sprite()->get_type()), - " for GUI icon ", icon_name); - ret = false; - } - } else { - UtilityFunctions::push_error("Null sprite for GUI icon ", icon_name); - ret = false; - } - return ret; -} - -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