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 --- .../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 +-- 4 files changed, 109 insertions(+), 49 deletions(-) (limited to 'extension/src/openvic-extension/utility') 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