From 5eaf0bcc203c94c21aac9b80cef046655aea4f04 Mon Sep 17 00:00:00 2001 From: MarianoGNU Date: Thu, 19 May 2016 17:38:06 -0300 Subject: [PATCH] Add Drag&Drop to ShaderGraphEditor's texture uniform graph node. --- .../plugins/shader_graph_editor_plugin.cpp | 105 ++++++++++++++++++ .../plugins/shader_graph_editor_plugin.h | 6 +- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/tools/editor/plugins/shader_graph_editor_plugin.cpp b/tools/editor/plugins/shader_graph_editor_plugin.cpp index f618f41cf8c..a70dad48c1f 100644 --- a/tools/editor/plugins/shader_graph_editor_plugin.cpp +++ b/tools/editor/plugins/shader_graph_editor_plugin.cpp @@ -2311,7 +2311,9 @@ void ShaderGraphView::_create_node(int p_id) { TextureFrame *tex = memnew( TextureFrame ); tex->set_expand(true); tex->set_custom_minimum_size(Size2(80,80)); + tex->set_drag_forwarding(this); gn->add_child(tex); + tex->set_ignore_mouse(false); tex->set_texture(graph->texture_input_node_get_value(type,p_id)); ToolButton *edit = memnew( ToolButton ); edit->set_text("edit.."); @@ -2517,6 +2519,105 @@ void ShaderGraphView::_sg_updated() { } } +Variant ShaderGraphView::get_drag_data_fw(const Point2 &p_point, Control *p_from) +{ + TextureFrame* frame = p_from->cast_to(); + if (!frame) + return Variant(); + + if (!frame->get_texture().is_valid()) + return Variant(); + + RES res = frame->get_texture(); + return EditorNode::get_singleton()->drag_resource(res,p_from); + + return Variant(); +} + +bool ShaderGraphView::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const +{ + if (p_data.get_type() != Variant::DICTIONARY) + return false; + + Dictionary d = p_data; + + if (d.has("type")){ + if (d["type"] == "resource" && d.has("resource")) { + Variant val = d["resource"]; + + if (val.get_type()==Variant::OBJECT) { + RES res = val; + if (res.is_valid() && res->cast_to()) + return true; + } + } + else if (d["type"] == "files" && d.has("files")) { + Vector files = d["files"]; + if (files.size() != 1) + return false; + return (ResourceLoader::get_resource_type(files[0]) == "ImageTexture"); + } + } + + return false; +} + +void ShaderGraphView::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) +{ + if (!can_drop_data_fw(p_point, p_data, p_from)) + return; + + TextureFrame *frame = p_from->cast_to(); + if (!frame) + return; + + Dictionary d = p_data; + Ref tex; + + if (d.has("type")) { + if (d["type"] == "resource" && d.has("resource")){ + Variant val = d["resource"]; + + if (val.get_type()==Variant::OBJECT) { + RES res = val; + if (res.is_valid()) + tex = Ref(res->cast_to()); + } + } + else if (d["type"] == "files" && d.has("files")) { + Vector files = d["files"]; + RES res = ResourceLoader::load(files[0]); + if (res.is_valid()) + tex = Ref(res->cast_to()); + } + } + + if (!tex.is_valid()) return; + + GraphNode *gn = frame->get_parent()->cast_to(); + if (!gn) return; + + int id = -1; + for(Map::Element *E = node_map.front();E;E=E->next()) + if (E->get() == gn) { + id = E->key(); + break; + } + print_line(String::num(double(id))); + if (id < 0) return; + + if (graph->node_get_type(type,id)==ShaderGraph::NODE_TEXTURE_INPUT) { + + UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo(); + ur->create_action(TTR("Change Texture Uniform")); + ur->add_do_method(graph.ptr(),"texture_input_node_set_value",type,id,tex); + ur->add_undo_method(graph.ptr(),"texture_input_node_set_value",type,id,graph->texture_input_node_get_value(type,id)); + ur->add_do_method(this,"_update_graph"); + ur->add_undo_method(this,"_update_graph"); + ur->commit_action(); + } +} + void ShaderGraphView::set_graph(Ref p_graph){ @@ -2623,6 +2724,10 @@ void ShaderGraphView::_bind_methods() { ObjectTypeDB::bind_method("_color_ramp_changed",&ShaderGraphView::_color_ramp_changed); ObjectTypeDB::bind_method("_curve_changed",&ShaderGraphView::_curve_changed); + ObjectTypeDB::bind_method(_MD("get_drag_data_fw"), &ShaderGraphView::get_drag_data_fw); + ObjectTypeDB::bind_method(_MD("can_drop_data_fw"), &ShaderGraphView::can_drop_data_fw); + ObjectTypeDB::bind_method(_MD("drop_data_fw"), &ShaderGraphView::drop_data_fw); + ObjectTypeDB::bind_method("_sg_updated",&ShaderGraphView::_sg_updated); } diff --git a/tools/editor/plugins/shader_graph_editor_plugin.h b/tools/editor/plugins/shader_graph_editor_plugin.h index 0336696911c..b33432807bd 100644 --- a/tools/editor/plugins/shader_graph_editor_plugin.h +++ b/tools/editor/plugins/shader_graph_editor_plugin.h @@ -116,7 +116,7 @@ public: GraphCurveMapEdit(); }; -class ShaderGraphView : public Node { +class ShaderGraphView : public Control { OBJ_TYPE(ShaderGraphView,Node); @@ -181,6 +181,10 @@ class ShaderGraphView : public Node { void _curve_changed(int p_id,Node* p_curve); void _sg_updated(); Map node_map; + + Variant get_drag_data_fw(const Point2& p_point,Control* p_from); + bool can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const; + void drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from); protected: void _notification(int p_what); static void _bind_methods();