From 15ce82c23b819906985d883f07798c8b6cc1443a Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 8 Jun 2022 19:13:06 +0200 Subject: [PATCH] Full cleanup of MatMakerGDEditor. --- .../editor/mat_maker_gd_editor.cpp | 539 +++++------------- .../editor/mat_maker_gd_editor.h | 39 +- 2 files changed, 149 insertions(+), 429 deletions(-) diff --git a/modules/material_maker/editor/mat_maker_gd_editor.cpp b/modules/material_maker/editor/mat_maker_gd_editor.cpp index 0f00d3706..ecef9cedb 100644 --- a/modules/material_maker/editor/mat_maker_gd_editor.cpp +++ b/modules/material_maker/editor/mat_maker_gd_editor.cpp @@ -1,36 +1,43 @@ #include "mat_maker_gd_editor.h" -NodePath MatMakerGDEditor::get_graph_edit_path() { - return graph_edit_path; -} +#include "../algos/mm_algos.h" -void MatMakerGDEditor::set_graph_edit_path(const NodePath &val) { - graph_edit_path = val; -} +#include "core/io/resource_loader.h" +#include "core/undo_redo.h" +#include "modules/material_maker/editor/mm_create_name_popup.h" +#include "scene/gui/box_container.h" +#include "scene/gui/button.h" +#include "scene/gui/control.h" +#include "scene/gui/graph_edit.h" +#include "scene/gui/panel_container.h" -NodePath MatMakerGDEditor::get_add_popup_path() { - return add_popup_path; -} +#include "../nodes/mm_material.h" +#include "../nodes/mm_node.h" +#include "../nodes/mm_node_universal_property.h" +#include "mm_graph_node.h" -void MatMakerGDEditor::set_add_popup_path(const NodePath &val) { - add_popup_path = val; -} +#include "editor/editor_plugin.h" GraphEdit *MatMakerGDEditor::get_graph_edit() { return _graph_edit; } -void MatMakerGDEditor::set_graph_edit(GraphEdit *val) { - _graph_edit = val; -} - Ref MatMakerGDEditor::get_mm_material() { return _material; } -void MatMakerGDEditor::set_mm_material(const Ref &val) { - _material = val; +void MatMakerGDEditor::set_mm_material(const Ref &object) { + if (_material.is_valid()) { + _material->disconnect("changed", this, "on_material_changed"); + } + + _material = object; + recreate(); + + if (_material.is_valid()) { + _material->connect("changed", this, "on_material_changed"); + } } int MatMakerGDEditor::get_ignore_material_change_event() const { @@ -41,6 +48,16 @@ void MatMakerGDEditor::set_ignore_material_change_event(const int val) { _ignore_material_change_event = val; } +void MatMakerGDEditor::ignore_changes(const bool val) { + if (val) { + _ignore_material_change_event += 1; + } + + else { + _ignore_material_change_event -= 1; + } +} + bool MatMakerGDEditor::get_recreation_in_progress() const { return _recreation_in_progress; } @@ -49,31 +66,20 @@ void MatMakerGDEditor::set_recreation_in_progress(const bool val) { _recreation_in_progress = val; } -EditorPlugin *MatMakerGDEditor::get_plugin() { - return _plugin; -} - -void MatMakerGDEditor::set_plugin(EditorPlugin *plugin) { - _plugin = plugin; - _undo_redo = plugin->get_undo_redo(); -} - UndoRedo *MatMakerGDEditor::get_undo_redo() { + if (!_undo_redo) { + _undo_redo = memnew(UndoRedo); + } + return _undo_redo; } -void MatMakerGDEditor::set_undo_redo(UndoRedo *val) { - _undo_redo = val; +void MatMakerGDEditor::set_undo_redo(UndoRedo *ur) { + _undo_redo = ur; } -void MatMakerGDEditor::_enter_tree() { - ensure_objs(); -} - -void MatMakerGDEditor::ensure_objs() { - if (!_graph_edit) { - _graph_edit = get_node(graph_edit_path); - +void MatMakerGDEditor::_notification(int p_what) { + if (p_what == NOTIFICATION_POSTINITIALIZE) { _graph_edit->add_valid_connection_type(MMNodeUniversalProperty::SLOT_TYPE_IMAGE, MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); _graph_edit->add_valid_connection_type(MMNodeUniversalProperty::SLOT_TYPE_INT, MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); _graph_edit->add_valid_connection_type(MMNodeUniversalProperty::SLOT_TYPE_FLOAT, MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); @@ -107,19 +113,18 @@ void MatMakerGDEditor::recreate() { } _recreation_in_progress = true; - ensure_objs(); - _graph_edit.clear_connections(); + _graph_edit->clear_connections(); for (int i = 0; i < _graph_edit->get_child_count(); ++i) { - GraphNode *c = Object::cast_to(_graph_edit->get_child(i)); + MMGraphNode *c = Object::cast_to(_graph_edit->get_child(i)); if (c) { _graph_edit->remove_child(c); - c->queue_free(); + c->queue_delete(); } } - if (!_material) { + if (!_material.is_valid()) { return; } @@ -129,7 +134,7 @@ void MatMakerGDEditor::recreate() { Ref n = _material->nodes[i]; MMGraphNode *gn = memnew(MMGraphNode); - gn->set_editor(this); + gn->set_editor_node(this); gn->set_node(_material, n); _graph_edit->add_child(gn); } @@ -140,32 +145,32 @@ void MatMakerGDEditor::recreate() { Ref n = _material->nodes[i]; if (n.is_valid()) { - for (int j = 0; j < n->input_property.size(); ++j) { + for (int j = 0; j < n->input_properties.size(); ++j) { Ref ip = n->input_properties[j]; if (ip.is_valid()) { - Node *input_node = find_graph_node_for(n); - Node *output_node = find_graph_node_for(ip.input_property.owner); - int to_slot = input_node.get_input_property_graph_node_slot_index(ip); - int from_slot = output_node.get_output_property_graph_node_slot_index(ip.input_property); - _graph_edit.connect_node(output_node.name, from_slot, input_node.name, to_slot); + MMGraphNode *input_node = find_graph_node_for(n); + MMGraphNode *output_node = find_graph_node_for(ip->get_input_property()->get_owner()); + int to_slot = input_node->get_input_property_graph_node_slot_index(ip); + int from_slot = output_node->get_output_property_graph_node_slot_index(ip->get_input_property()); + _graph_edit->connect_node(output_node->get_name(), from_slot, input_node->get_name(), to_slot); } } } } - _material.render(); + _material->render(); _recreation_in_progress = false; ignore_changes(false); } -MMGraphNode *MatMakerGDEditor::find_graph_node_for(const Ref &nnode) { +MMGraphNode *MatMakerGDEditor::find_graph_node_for(const Ref &nnode) { for (int i = 0; i < _graph_edit->get_child_count(); ++i) { - GraphNode *c = Object::cast_to(_graph_edit->get_child(i)); + MMGraphNode *c = Object::cast_to(_graph_edit->get_child(i)); if (c) { if (c->has_method("get_material_node")) { - Ref n = c->get_material_node(); + Ref n = c->get_material_node(); if (n == nnode) { return c; @@ -177,19 +182,6 @@ MMGraphNode *MatMakerGDEditor::find_graph_node_for(const Ref &nnode) { return nullptr; } -void MatMakerGDEditor::set_mmmaterial(const Ref &object) { - if (_material) { - _material->disconnect("changed", this, "on_material_changed"); - } - - _material = object; - recreate(); - - if (_material) { - _material->connect("changed", this, "on_material_changed"); - } -} - void MatMakerGDEditor::on_material_changed() { if (_ignore_material_change_event > 0) { return; @@ -202,19 +194,10 @@ void MatMakerGDEditor::on_material_changed() { call_deferred("recreate"); } -void MatMakerGDEditor::ignore_changes(const bool val) { - if (val) { - _ignore_material_change_event += 1; - } - - else { - _ignore_material_change_event -= 1; - } -} - void MatMakerGDEditor::on_graph_edit_connection_request(const String &from, const int from_slot, const String &to, const int to_slot) { - GraphNode *from_node = _graph_edit.get_node(from); - GraphNode *to_node = _graph_edit.get_node(to); + MMGraphNode *from_node = Object::cast_to(_graph_edit->get_node(from)); + MMGraphNode *to_node = Object::cast_to(_graph_edit->get_node(to)); + ignore_changes(true); _material->cancel_render_and_wait(); @@ -226,8 +209,9 @@ void MatMakerGDEditor::on_graph_edit_connection_request(const String &from, cons } void MatMakerGDEditor::on_graph_edit_disconnection_request(const String &from, const int from_slot, const String &to, const int to_slot) { - GraphNode *from_node = _graph_edit.get_node(from); - GraphNode *to_node = _graph_edit.get_node(to); + MMGraphNode *from_node = Object::cast_to(_graph_edit->get_node(from)); + MMGraphNode *to_node = Object::cast_to(_graph_edit->get_node(to)); + ignore_changes(true); _material->cancel_render_and_wait(); @@ -238,362 +222,120 @@ void MatMakerGDEditor::on_graph_edit_disconnection_request(const String &from, c ignore_changes(false); } -void MatMakerGDEditor::on_graph_node_close_request(const GraphNode &node) { - if (_material) { +void MatMakerGDEditor::on_graph_node_close_request(Node *p_node) { + MMGraphNode *node = Object::cast_to(p_node); + + if (_material.is_valid()) { ignore_changes(true); + _material->cancel_render_and_wait(); //_material.remove_node(node._node); - _undo_redo->create_action("MMGD: Remove Node"); - _undo_redo->add_do_method(_material, "remove_node", node._node); - _undo_redo->add_undo_method(_material, "add_node", node._node); - _undo_redo->commit_action(); + get_undo_redo()->create_action("MMGD: Remove Node"); + get_undo_redo()->add_do_method(*_material, "remove_node", node->get_node()); + get_undo_redo()->add_undo_method(*_material, "add_node", node->get_node()); + get_undo_redo()->commit_action(); + recreate(); ignore_changes(false); } } void MatMakerGDEditor::_on_AddButton_pressed() { - get_node(add_popup_path)->opup_centered(); + _create_popup->popup_centered(); } -void MatMakerGDEditor::_on_AddPopup_ok_pressed(const String &script_path) { - if (!_material) { +void MatMakerGDEditor::_on_AddPopup_ok_pressed(const int type, const String &data) { + if (!_material.is_valid()) { return; } - ensure_objs(); _material->cancel_render_and_wait(); - //Variant = load(script_path); - MMGraphNode *nnode = memnew(MMGraphNode); + Ref nnode; - if (!nnode) { - print_error("_on_AddPopup_ok_pressed: Error !nnode! script: " + script_path); + if (type == MMAlgos::MMNODE_REGISTRY_TYPE_CLASS) { + nnode = Ref(ClassDB::instance(data)); + } else if (type == MMAlgos::MMNODE_REGISTRY_TYPE_SCRIPT) { + Ref