#include "GUINode.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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; #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) \ F(GUIScrollbar, gui_scrollbar) \ F(GUIListBox, gui_listbox) #define APPLY_TO_TEXTURE_TYPES(F) \ F(GFXSpriteTexture, gfx_sprite_texture) \ F(GFXMaskedFlagTexture, gfx_masked_flag_texture) \ F(GFXPieChartTexture, gfx_pie_chart_texture) void GUINode::_bind_methods() { 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" }); OV_BIND_METHOD(GUINode::get_click_mask); OV_BIND_METHOD(GUINode::set_click_mask, { "mask" }); ADD_PROPERTY( PropertyInfo(Variant::OBJECT, "click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask" ); OV_BIND_METHOD(GUINode::set_click_mask_from_nodepaths, { "paths" }); OV_BIND_METHOD(GUINode::update_click_mask); #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" }); OV_BIND_SMETHOD(int_to_formatted_string, { "val" }); OV_BIND_SMETHOD(float_to_formatted_string, { "val", "decimal_places" }); OV_BIND_SMETHOD(format_province_name, { "province_identifier" }); } 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_STOP); } 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_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_scene, String const& gui_element, String const& name) { Error err = OK; Control* result = nullptr; 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) { add_child(result); } 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); 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