From d7318f69653ca090575d1243256fcafe8d9ca25f Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 11 May 2016 11:46:08 -0300 Subject: -begun implementing drag & drop editor wide -filesystem dock dnd support -property list dnd support -scene tree dnd support --- tools/editor/editor_node.cpp | 126 ++++++++++++- tools/editor/editor_node.h | 10 +- tools/editor/plugins/canvas_item_editor_plugin.cpp | 2 + tools/editor/property_editor.cpp | 200 +++++++++++++++++++- tools/editor/property_editor.h | 13 ++ tools/editor/scene_tree_dock.cpp | 209 +++++++++++++++++++-- tools/editor/scene_tree_dock.h | 4 + tools/editor/scene_tree_editor.cpp | 97 ++++++++++ tools/editor/scene_tree_editor.h | 6 + tools/editor/scenes_dock.cpp | 145 +++++++++++++- tools/editor/scenes_dock.h | 3 + 11 files changed, 798 insertions(+), 17 deletions(-) (limited to 'tools/editor') diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index 2736a75ac..8e5087b40 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -523,7 +523,7 @@ void EditorNode::save_resource(const Ref& p_resource) { } } -void EditorNode::save_resource_as(const Ref& p_resource) { +void EditorNode::save_resource_as(const Ref& p_resource,const String& p_at_path) { file->set_mode(EditorFileDialog::MODE_SAVE_FILE); bool relpaths = (p_resource->has_meta("__editor_relpaths__") && p_resource->get_meta("__editor_relpaths__").operator bool()); @@ -546,7 +546,21 @@ void EditorNode::save_resource_as(const Ref& p_resource) { } //file->set_current_path(current_path); - if (p_resource->get_path()!="") { + + if (p_at_path!=String()) { + + file->set_current_dir(p_at_path); + if (p_resource->get_path().is_resource_file()) { + file->set_current_file(p_resource->get_path().get_file()); + } else { + if (extensions.size()) { + file->set_current_file("new_"+p_resource->get_type().to_lower()+"."+preferred.front()->get().to_lower()); + } else { + file->set_current_file(String()); + } + } + } else if (p_resource->get_path()!="") { + file->set_current_path(p_resource->get_path()); if (extensions.size()) { String ext=p_resource->get_path().extension().to_lower(); @@ -4881,6 +4895,114 @@ void EditorNode::remove_control_from_dock(Control* p_control) { _update_dock_slots_visibility(); } +Variant EditorNode::drag_resource(const Ref& p_res,Control* p_from) { + + + Control *drag_control = memnew( Control ); + TextureFrame *drag_preview = memnew( TextureFrame ); + Label* label=memnew( Label ); + + Ref preview; + + { + //todo make proper previews + Ref pic = gui_base->get_icon("FileBig","EditorIcons"); + Image img = pic->get_data(); + img.resize(48,48); //meh + Ref resized_pic = Ref( memnew( ImageTexture) ); + resized_pic->create_from_image(img); + preview=resized_pic; + } + + drag_preview->set_texture(preview); + drag_control->add_child(drag_preview); + if (p_res->get_path().is_resource_file()) { + label->set_text(p_res->get_path().get_file()); + } else if (p_res->get_name()!="") { + label->set_text(p_res->get_name()); + } else { + label->set_text(p_res->get_type()); + + } + + drag_control->add_child(label); + + p_from->set_drag_preview(drag_control); //wait until it enters scene + + label->set_pos( Point2((preview->get_width()-label->get_minimum_size().width)/2,preview->get_height()) ); + + Dictionary drag_data; + drag_data["type"]="resource"; + drag_data["resource"]=p_res; + drag_data["from"]=p_from; + + + return drag_data; + +} + +Variant EditorNode::drag_files(const Vector& p_files, Control *p_from){ + + VBoxContainer *files = memnew( VBoxContainer ); + + int max_files=6; + + for(int i=0;iset_text(p_files[i].get_file()); + files->add_child(label); + } + + if (p_files.size()>max_files) { + + Label* label=memnew( Label ); + label->set_text(itos(p_files.size()-max_files)+" "+TTR("More File(s)")); + files->add_child(label); + + } + Dictionary drag_data; + drag_data["type"]="files"; + drag_data["files"]=p_files; + drag_data["from"]=p_from; + + p_from->set_drag_preview(files); //wait until it enters scene + + return drag_data; + +} + +Variant EditorNode::drag_files_and_dirs(const Vector& p_files, Control *p_from){ + + VBoxContainer *files = memnew( VBoxContainer ); + + int max_files=6; + + for(int i=0;iset_text(p_files[i].get_file()); + files->add_child(label); + } + + if (p_files.size()>max_files) { + + Label* label=memnew( Label ); + label->set_text(itos(p_files.size()-max_files)+" "+TTR("More File(s) and/or Directory(s)")); + files->add_child(label); + + } + Dictionary drag_data; + drag_data["type"]="files_and_dirs"; + drag_data["files"]=p_files; + drag_data["from"]=p_from; + + p_from->set_drag_preview(files); //wait until it enters scene + + return drag_data; + +} + void EditorNode::_bind_methods() { diff --git a/tools/editor/editor_node.h b/tools/editor/editor_node.h index c3b7a817c..b3faa21cf 100644 --- a/tools/editor/editor_node.h +++ b/tools/editor/editor_node.h @@ -584,6 +584,9 @@ public: static void add_editor_plugin(EditorPlugin *p_editor); static void remove_editor_plugin(EditorPlugin *p_editor); + + + void add_control_to_dock(DockSlot p_slot,Control* p_control); void remove_control_from_dock(Control* p_control); @@ -599,7 +602,7 @@ public: void save_resource_in_path(const Ref& p_resource,const String& p_path); void save_resource(const Ref& p_resource); - void save_resource_as(const Ref& p_resource); + void save_resource_as(const Ref& p_resource, const String &p_at_path=String()); static bool has_unsaved_changes() { return singleton->unsaved_cache; } @@ -696,6 +699,11 @@ public: void hide_bottom_panel(); void remove_bottom_panel_item(Control *p_item); + Variant drag_resource(const Ref& p_res,Control* p_from); + Variant drag_files(const Vector& p_files,Control* p_from); + Variant drag_files_and_dirs(const Vector& p_files,Control* p_from); + + EditorNode(); ~EditorNode(); void get_singleton(const char* arg1, bool arg2); diff --git a/tools/editor/plugins/canvas_item_editor_plugin.cpp b/tools/editor/plugins/canvas_item_editor_plugin.cpp index 7ece65e75..0213dbda5 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.cpp +++ b/tools/editor/plugins/canvas_item_editor_plugin.cpp @@ -572,6 +572,8 @@ bool CanvasItemEditor::_select(CanvasItem *item, Point2 p_click_pos, bool p_appe _append_canvas_item(item); viewport->update(); + return true; + } else { //regular selection diff --git a/tools/editor/property_editor.cpp b/tools/editor/property_editor.cpp index 7c5aca579..6f8091015 100644 --- a/tools/editor/property_editor.cpp +++ b/tools/editor/property_editor.cpp @@ -43,7 +43,8 @@ #include "array_property_edit.h" #include "editor_help.h" #include "scene/resources/packed_scene.h" - +#include "scene/main/viewport.h" +#include "editor_file_system.h" void CustomPropertyEditor::_notification(int p_what) { @@ -224,6 +225,10 @@ void CustomPropertyEditor::_menu_option(int p_which) { } +void CustomPropertyEditor::hide_menu() { + menu->hide(); +} + Variant CustomPropertyEditor::get_variant() const { return v; @@ -2257,6 +2262,179 @@ void PropertyEditor::_check_reload_status(const String&p_name, TreeItem* item) { } } + + +bool PropertyEditor::_is_drop_valid(const Dictionary& p_drag_data, const Dictionary& p_item_data) const { + + Dictionary d = p_item_data; + + if (d.has("type")) { + + int type = d["type"]; + if (type==Variant::OBJECT && d.has("hint") && d.has("hint_text") && int(d["hint"])==PROPERTY_HINT_RESOURCE_TYPE) { + + + String allowed_type=d["hint_text"]; + + Dictionary drag_data = p_drag_data; + if (drag_data.has("type") && String(drag_data["type"])=="resource") { + Ref res = drag_data["resource"]; + for(int i=0;iget_type(),at)) { + return true; + } + } + + } + if (drag_data.has("type") && String(drag_data["type"])=="files") { + + Vector files = drag_data["files"]; + + if (files.size()==1) { + String file = files[0]; + String ftype = EditorFileSystem::get_singleton()->get_file_type(file); + if (ftype!="") { + + for(int i=0;igui_get_drag_data(),p_at->get_metadata(0))) + p_at->set_custom_bg_color(1,Color(0.7,0.5,0.2),true); + + if (p_at->get_children()) { + _mark_drop_fields(p_at->get_children()); + } + + if (p_at->get_next()) { + _mark_drop_fields(p_at->get_next()); + } +} + +Variant PropertyEditor::get_drag_data_fw(const Point2& p_point,Control* p_from) { + + TreeItem *item = tree->get_item_at_pos(p_point); + if (!item) + return Variant(); + + int col = tree->get_column_at_pos(p_point); + if (col!=1) + return Variant(); + + + Dictionary d = item->get_metadata(0); + if (!d.has("name")) + return Variant(); + + Variant val = obj->get(d["name"]); + + if (val.get_type()==Variant::OBJECT) { + RES res = val; + if (res.is_valid()) { + + custom_editor->hide_menu(); + return EditorNode::get_singleton()->drag_resource(res,p_from); + } + } + + return Variant(); +} + +bool PropertyEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{ + + TreeItem *item = tree->get_item_at_pos(p_point); + if (!item) + return false; + + int col = tree->get_column_at_pos(p_point); + if (col!=1) + return false; + + return _is_drop_valid(p_data,item->get_metadata(0)); + +} +void PropertyEditor::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){ + + TreeItem *item = tree->get_item_at_pos(p_point); + if (!item) + return; + + int col = tree->get_column_at_pos(p_point); + if (col!=1) + return; + + if (!_is_drop_valid(p_data,item->get_metadata(0))) + return; + + Dictionary d = item->get_metadata(0); + + if (!d.has("name")) + return; + + String name=d["name"]; + + Dictionary drag_data = p_data; + if (drag_data.has("type") && String(drag_data["type"])=="resource") { + Ref res = drag_data["resource"]; + if (res.is_valid()) { + _edit_set(name,res); + return; + } + } + + if (drag_data.has("type") && String(drag_data["type"])=="files") { + + Vector files = drag_data["files"]; + + if (files.size()==1) { + String file = files[0]; + RES res = ResourceLoader::load(file); + if (res.is_valid()) { + _edit_set(name,res); + return; + } + } + } +} + + +void PropertyEditor::_clear_drop_fields(TreeItem* p_at) { + + Dictionary d = p_at->get_metadata(0); + + if (d.has("type")) { + + int type = d["type"]; + if (type==Variant::OBJECT) { + p_at->clear_custom_bg_color(1); + } + + } + + if (p_at->get_children()) { + _clear_drop_fields(p_at->get_children()); + } + + if (p_at->get_next()) { + _clear_drop_fields(p_at->get_next()); + } +} + void PropertyEditor::_notification(int p_what) { if (p_what==NOTIFICATION_ENTER_TREE) { @@ -2270,6 +2448,20 @@ void PropertyEditor::_notification(int p_what) { } + if (p_what==NOTIFICATION_DRAG_BEGIN) { + + if (is_visible() && tree->get_root()) { + _mark_drop_fields(tree->get_root()); + } + } + + if (p_what==NOTIFICATION_DRAG_END) { + if (is_visible() && tree->get_root()) { + _clear_drop_fields(tree->get_root()); + } + + } + if (p_what==NOTIFICATION_FIXED_PROCESS) { @@ -3661,6 +3853,10 @@ void PropertyEditor::_bind_methods() { ObjectTypeDB::bind_method( "_filter_changed",&PropertyEditor::_filter_changed); ObjectTypeDB::bind_method( "update_tree",&PropertyEditor::update_tree); + ObjectTypeDB::bind_method(_MD("get_drag_data_fw"), &PropertyEditor::get_drag_data_fw); + ObjectTypeDB::bind_method(_MD("can_drop_data_fw"), &PropertyEditor::can_drop_data_fw); + ObjectTypeDB::bind_method(_MD("drop_data_fw"), &PropertyEditor::drop_data_fw); + ADD_SIGNAL( MethodInfo("property_toggled",PropertyInfo( Variant::STRING, "property"),PropertyInfo( Variant::BOOL, "value"))); ADD_SIGNAL( MethodInfo("resource_selected", PropertyInfo( Variant::OBJECT, "res"),PropertyInfo( Variant::STRING, "prop") ) ); ADD_SIGNAL( MethodInfo("property_keyed",PropertyInfo( Variant::STRING, "property"))); @@ -3778,6 +3974,8 @@ PropertyEditor::PropertyEditor() { tree->connect("item_edited", this,"_item_edited",varray(),CONNECT_DEFERRED); tree->connect("cell_selected", this,"_item_selected"); + tree->set_drag_forwarding(this); + set_fixed_process(true); custom_editor = memnew( CustomPropertyEditor ); diff --git a/tools/editor/property_editor.h b/tools/editor/property_editor.h index 693bfb3ef..ac58011d4 100644 --- a/tools/editor/property_editor.h +++ b/tools/editor/property_editor.h @@ -137,6 +137,10 @@ protected: static void _bind_methods(); public: + + + void hide_menu(); + Variant get_variant() const; String get_name() const; @@ -219,6 +223,15 @@ class PropertyEditor : public Control { void _filter_changed(const String& p_text); + void _mark_drop_fields(TreeItem* p_at); + void _clear_drop_fields(TreeItem* p_at); + + bool _is_drop_valid(const Dictionary& p_drag_data, const Dictionary& p_item_data) const; + 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); + + UndoRedo *undo_redo; protected: diff --git a/tools/editor/scene_tree_dock.cpp b/tools/editor/scene_tree_dock.cpp index 113d18ea7..252e7c890 100644 --- a/tools/editor/scene_tree_dock.cpp +++ b/tools/editor/scene_tree_dock.cpp @@ -40,6 +40,9 @@ #include "tools/editor/plugins/animation_player_editor_plugin.h" #include "animation_editor.h" + + + void SceneTreeDock::_unhandled_key_input(InputEvent p_event) { uint32_t sc = p_event.key.get_scancode_with_modifiers(); @@ -943,16 +946,37 @@ bool SceneTreeDock::_validate_no_foreign() { void SceneTreeDock::_node_reparent(NodePath p_path,bool p_keep_global_xform) { - Node *node = scene_tree->get_selected(); - ERR_FAIL_COND(!node); - ERR_FAIL_COND(node==edited_scene); Node *new_parent = scene_root->get_node(p_path); ERR_FAIL_COND(!new_parent); + //ok all valid + + List selection = editor_selection->get_selected_node_list(); + + if (selection.empty()) + return; //nothing to reparent + + Vector nodes; + + for(List::Element *E=selection.front();E;E=E->next()) { + nodes.push_back(E->get()); + } + + _do_reparent(new_parent,-1,nodes,p_keep_global_xform); + +} + + +void SceneTreeDock::_do_reparent(Node* p_new_parent,int p_position_in_parent,Vector p_nodes,bool p_keep_global_xform) { + + + Node *new_parent = p_new_parent; + ERR_FAIL_COND(!new_parent); + Node *validate=new_parent; while(validate) { - if (editor_selection->is_selected(validate)) { + if (p_nodes.find(validate)!=-1) { ERR_EXPLAIN("Selection changed at some point.. can't reparent"); ERR_FAIL(); return; @@ -964,20 +988,20 @@ void SceneTreeDock::_node_reparent(NodePath p_path,bool p_keep_global_xform) { List selection = editor_selection->get_selected_node_list(); - if (selection.empty()) + if (p_nodes.size()==0) return; //nothing to reparent //sort by tree order, so re-adding is easy - selection.sort_custom(); + p_nodes.sort_custom(); editor_data->get_undo_redo().create_action(TTR("Reparent Node")); List > path_renames; - for(List::Element *E=selection.front();E;E=E->next()) { + for(int ni=0;niget(); + Node *node = p_nodes[ni]; fill_path_renames(node,new_parent,&path_renames); @@ -994,6 +1018,9 @@ void SceneTreeDock::_node_reparent(NodePath p_path,bool p_keep_global_xform) { editor_data->get_undo_redo().add_do_method(node->get_parent(),"remove_child",node); editor_data->get_undo_redo().add_do_method(new_parent,"add_child",node); + if (p_position_in_parent>=0) + editor_data->get_undo_redo().add_do_method(new_parent,"move_child",node,p_position_in_parent+ni); + ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger(); String new_name = new_parent->validate_child_name(node->get_name()); editor_data->get_undo_redo().add_do_method(sed,"live_debug_reparent_node",edited_scene->get_path_to(node),edited_scene->get_path_to(new_parent),new_name,-1); @@ -1030,9 +1057,9 @@ void SceneTreeDock::_node_reparent(NodePath p_path,bool p_keep_global_xform) { - for(List::Element *E=selection.front();E;E=E->next()) { + for(int ni=0;niget(); + Node *node = p_nodes[ni]; List owned; node->get_owned_by(node->get_owner(),&owned); @@ -1078,7 +1105,6 @@ void SceneTreeDock::_node_reparent(NodePath p_path,bool p_keep_global_xform) { //node->set_owner(owner); } - void SceneTreeDock::_script_created(Ref