From 41329f9750379b3c2e506d1e9ed7f6195c812920 Mon Sep 17 00:00:00 2001 From: Mateusz Adamczyk Date: Wed, 19 Oct 2016 21:26:14 +0200 Subject: Option to load script for node (#6839) --- tools/editor/scene_tree_dock.cpp | 64 ++++++++++++++++++++++++++++++++++++---- tools/editor/scene_tree_dock.h | 6 +++- 2 files changed, 64 insertions(+), 6 deletions(-) (limited to 'tools') diff --git a/tools/editor/scene_tree_dock.cpp b/tools/editor/scene_tree_dock.cpp index 56f10ff7f8..7ff2728310 100644 --- a/tools/editor/scene_tree_dock.cpp +++ b/tools/editor/scene_tree_dock.cpp @@ -81,7 +81,10 @@ void SceneTreeDock::_unhandled_key_input(InputEvent p_event) { _tool_selected(TOOL_DUPLICATE); } else if (ED_IS_SHORTCUT("scene_tree/add_script", p_event)) { - _tool_selected(TOOL_SCRIPT); + _tool_selected(TOOL_CREATE_SCRIPT); + } + else if (ED_IS_SHORTCUT("scene_tree/load_script", p_event)) { + _tool_selected(TOOL_LOAD_SCRIPT); } else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) { _tool_selected(TOOL_MOVE_UP); @@ -266,6 +269,24 @@ void SceneTreeDock::_replace_with_branch_scene(const String& p_file,Node* base) scene_tree->set_selected(instanced_scene); } + +void SceneTreeDock::_file_selected(String p_file) { + RES p_script = ResourceLoader::load(p_file, "Script"); + if (p_script.is_null()) { + accept->get_ok()->set_text(TTR("Ugh")); + accept->set_text(vformat(TTR("Error loading script from %s"), p_file)); + accept->popup_centered_minsize(); + return; + } + + Node *selected = scene_tree->get_selected(); + if (!selected) + return; + selected->set_script(p_script.get_ref_ptr()); + editor->push_item(p_script.operator->()); + file_dialog->hide(); +} + bool SceneTreeDock::_cyclical_dependency_exists(const String& p_target_scene_path, Node* p_desired_node) { int childCount = p_desired_node->get_child_count(); @@ -358,7 +379,22 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { //groups_editor->set_current(current); //groups_editor->popup_centered_ratio(); } break; - case TOOL_SCRIPT: { + case TOOL_LOAD_SCRIPT: { + Node *selected = scene_tree->get_selected(); + if (!selected) + break; + + file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE); + + List extensions; + ResourceLoader::get_recognized_extensions_for_type("Script", &extensions); + file_dialog->clear_filters(); + for (List::Element *E = extensions.front(); E; E = E->next()) + file_dialog->add_filter("*." + E->get() + " ; " + E->get().to_upper()); + + file_dialog->popup_centered_ratio(); + } break; + case TOOL_CREATE_SCRIPT: { Node *selected = scene_tree->get_selected(); if (!selected) @@ -672,6 +708,7 @@ void SceneTreeDock::_notification(int p_what) { button_add->set_icon(get_icon("Add","EditorIcons")); button_instance->set_icon(get_icon("Instance","EditorIcons")); button_create_script->set_icon(get_icon("Script","EditorIcons")); + button_load_script->set_icon(get_icon("Script", "EditorIcons")); filter_icon->set_texture(get_icon("Zoom","EditorIcons")); @@ -1303,8 +1340,10 @@ void SceneTreeDock::_selection_changed() { if (selection_size==1 && EditorNode::get_singleton()->get_editor_selection()->get_selection().front()->key()->get_script().is_null()) { button_create_script->show(); + button_load_script->show(); } else { button_create_script->hide(); + button_load_script->hide(); } //tool_buttons[TOOL_MULTI_EDIT]->set_disabled(EditorNode::get_singleton()->get_editor_selection()->get_selection().size()<2); @@ -1775,7 +1814,8 @@ void SceneTreeDock::_tree_rmb(const Vector2& p_menu_pos) { //menu->add_icon_item(get_icon("Groups","EditorIcons"),TTR("Edit Groups"),TOOL_GROUP); //menu->add_icon_item(get_icon("Connect","EditorIcons"),TTR("Edit Connections"),TOOL_CONNECT); menu->add_separator(); - menu->add_icon_shortcut(get_icon("Script","EditorIcons"),ED_GET_SHORTCUT("scene_tree/add_script"), TOOL_SCRIPT); + menu->add_icon_shortcut(get_icon("Script", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_script"), TOOL_CREATE_SCRIPT); + menu->add_icon_shortcut(get_icon("Script", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/load_script"), TOOL_LOAD_SCRIPT); menu->add_separator(); } @@ -1834,7 +1874,7 @@ void SceneTreeDock::_focus_node() { void SceneTreeDock::open_script_dialog(Node* p_for_node) { scene_tree->set_selected(p_for_node,false); - _tool_selected(TOOL_SCRIPT); + _tool_selected(TOOL_CREATE_SCRIPT); } void SceneTreeDock::_bind_methods() { @@ -1862,6 +1902,7 @@ void SceneTreeDock::_bind_methods() { ObjectTypeDB::bind_method(_MD("_tree_rmb"),&SceneTreeDock::_tree_rmb); ObjectTypeDB::bind_method(_MD("_filter_changed"),&SceneTreeDock::_filter_changed); ObjectTypeDB::bind_method(_MD("_focus_node"),&SceneTreeDock::_focus_node); + ObjectTypeDB::bind_method(_MD("_file_selected"), &SceneTreeDock::_file_selected); ObjectTypeDB::bind_method(_MD("instance"),&SceneTreeDock::instance); @@ -1886,6 +1927,7 @@ SceneTreeDock::SceneTreeDock(EditorNode *p_editor,Node *p_scene_root,EditorSelec ED_SHORTCUT("scene_tree/instance_scene",TTR("Instance Child Scene")); ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type")); ED_SHORTCUT("scene_tree/add_script", TTR("Add Script")); + ED_SHORTCUT("scene_tree/load_script", TTR("Load Script")); ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP); ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN); ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"),KEY_MASK_CMD | KEY_D); @@ -1922,12 +1964,19 @@ SceneTreeDock::SceneTreeDock(EditorNode *p_editor,Node *p_scene_root,EditorSelec tb = memnew( ToolButton ); - tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_SCRIPT, false)); + tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_CREATE_SCRIPT, false)); tb->set_tooltip(TTR("Create a new script for the selected node.")); tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_script")); filter_hbc->add_child(tb); button_create_script=tb; + tb = memnew(ToolButton); + tb->connect("pressed", this, "_tool_selected", make_binds(TOOL_LOAD_SCRIPT, false)); + tb->set_tooltip(TTR("Load a script for the selected node.")); + tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/load_script")); + filter_hbc->add_child(tb); + button_load_script = tb; + scene_tree = memnew( SceneTreeEditor(false,true,true )); vbc->add_child(scene_tree); @@ -1955,6 +2004,11 @@ SceneTreeDock::SceneTreeDock(EditorNode *p_editor,Node *p_scene_root,EditorSelec add_child(create_dialog); create_dialog->connect("create",this,"_create"); + file_dialog = memnew(EditorFileDialog); + add_child(file_dialog); + file_dialog->hide(); + file_dialog->connect("file_selected", this, "_file_selected"); + //groups_editor = memnew( GroupsEditor ); //add_child(groups_editor); //groups_editor->set_undo_redo(&editor_data->get_undo_redo()); diff --git a/tools/editor/scene_tree_dock.h b/tools/editor/scene_tree_dock.h index 8933a03883..ddd28cfd2d 100644 --- a/tools/editor/scene_tree_dock.h +++ b/tools/editor/scene_tree_dock.h @@ -58,7 +58,8 @@ class SceneTreeDock : public VBoxContainer { TOOL_REPLACE, TOOL_CONNECT, TOOL_GROUP, - TOOL_SCRIPT, + TOOL_CREATE_SCRIPT, + TOOL_LOAD_SCRIPT, TOOL_MOVE_UP, TOOL_MOVE_DOWN, TOOL_DUPLICATE, @@ -75,10 +76,12 @@ class SceneTreeDock : public VBoxContainer { int current_option; CreateDialog *create_dialog; + EditorFileDialog *file_dialog; ToolButton *button_add; ToolButton *button_instance; ToolButton *button_create_script; + ToolButton *button_load_script; SceneTreeEditor *scene_tree; @@ -155,6 +158,7 @@ class SceneTreeDock : public VBoxContainer { void _perform_instance_scenes(const Vector& p_files,Node* parent,int p_pos); void _replace_with_branch_scene(const String& p_file,Node* base); + void _file_selected(String p_file); protected: void _notification(int p_what); -- cgit v1.2.3