#include "GUINode.hpp" #include #include #include "openvic-extension/utility/ClassBindings.hpp" #include "openvic-extension/utility/UITools.hpp" #include "openvic-extension/utility/Utilities.hpp" 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) \ F(Label, label) \ F(Panel, panel) \ F(TextureProgressBar, progress_bar) \ F(TextureRect, texture_rect) \ F(GUIOverlappingElementsBox, gui_overlapping_elements_box) #define APPLY_TO_TEXTURE_TYPES(F) \ F(GFXIconTexture, gfx_icon_texture) \ F(GFXMaskedFlagTexture, gfx_masked_flag_texture) \ 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 {})); #define GET_BINDINGS(type, name) \ OV_BIND_SMETHOD(get_##name##_from_node, { "node" }); \ OV_BIND_METHOD(GUINode::get_##name##_from_nodepath, { "path" }); APPLY_TO_CHILD_TYPES(GET_BINDINGS) OV_BIND_SMETHOD(get_texture_from_node, { "node" }); OV_BIND_METHOD(GUINode::get_texture_from_nodepath, { "path" }); APPLY_TO_TEXTURE_TYPES(GET_BINDINGS) #undef GET_BINDINGS OV_BIND_METHOD(GUINode::hide_node, { "path" }); OV_BIND_METHOD(GUINode::hide_nodes, { "paths" }); } GUINode::GUINode() { set_anchors_and_offsets_preset(PRESET_FULL_RECT); set_h_grow_direction(GROW_DIRECTION_BOTH); set_v_grow_direction(GROW_DIRECTION_BOTH); set_mouse_filter(MOUSE_FILTER_IGNORE); } Control* GUINode::generate_gui_element(String const& gui_file, 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); } return result; } Error GUINode::add_gui_element(String const& gui_file, 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); err = FAILED; } if (result != nullptr) { add_child(result); } return err; } template T> static T* _cast_node(Node* node) { ERR_FAIL_NULL_V(node, nullptr); T* result = Object::cast_to(node); ERR_FAIL_NULL_V_MSG( result, nullptr, vformat("Failed to cast node %s from type %s to %s", node->get_name(), node->get_class(), T::get_class_static()) ); return result; } #define CHILD_GET_FUNCTIONS(type, name) \ type* GUINode::get_##name##_from_node(Node* node) { \ return _cast_node(node); \ } \ type* GUINode::get_##name##_from_nodepath(NodePath const& path) const { \ return _cast_node(get_node_internal(path)); \ } APPLY_TO_CHILD_TYPES(CHILD_GET_FUNCTIONS) #undef CHILD_GET_FUNCTIONS Ref GUINode::get_texture_from_node(Node* node) { ERR_FAIL_NULL_V(node, nullptr); if (TextureRect const* texture_rect = Object::cast_to(node); texture_rect != nullptr) { const Ref texture = texture_rect->get_texture(); ERR_FAIL_NULL_V_MSG(texture, nullptr, vformat("Failed to get Texture2D from TextureRect %s", node->get_name())); return texture; } else if (Button const* button = Object::cast_to