diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2016-05-21 11:33:16 +0200 |
---|---|---|
committer | Rémi Verschelde <remi@verschelde.fr> | 2016-05-21 11:33:16 +0200 |
commit | 2a6bd0c357fbc537f0e0260cf9c14101f3d53355 (patch) | |
tree | f36a3ac07f7dacebdd71f40840c08b8e2b46d991 /tools | |
parent | 8061be1465638da27cd1dfcc4897f4913e36ece9 (diff) | |
parent | 5eaf0bcc203c94c21aac9b80cef046655aea4f04 (diff) |
Merge pull request #4722 from MarianoGnu/master
Add Drag&Drop to ShaderGraphEditor's texture uniform graph node.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/editor/plugins/shader_graph_editor_plugin.cpp | 105 | ||||
-rw-r--r-- | tools/editor/plugins/shader_graph_editor_plugin.h | 6 |
2 files changed, 110 insertions, 1 deletions
diff --git a/tools/editor/plugins/shader_graph_editor_plugin.cpp b/tools/editor/plugins/shader_graph_editor_plugin.cpp index f618f41cf8..a70dad48c1 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<TextureFrame>(); + 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<Texture>()) + return true; + } + } + else if (d["type"] == "files" && d.has("files")) { + Vector<String> 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<TextureFrame>(); + if (!frame) + return; + + Dictionary d = p_data; + Ref<Texture> 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<Texture>(res->cast_to<Texture>()); + } + } + else if (d["type"] == "files" && d.has("files")) { + Vector<String> files = d["files"]; + RES res = ResourceLoader::load(files[0]); + if (res.is_valid()) + tex = Ref<Texture>(res->cast_to<Texture>()); + } + } + + if (!tex.is_valid()) return; + + GraphNode *gn = frame->get_parent()->cast_to<GraphNode>(); + if (!gn) return; + + int id = -1; + for(Map<int,GraphNode*>::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<ShaderGraph> 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 0336696911..b33432807b 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<int,GraphNode*> 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(); |