Browse Source

Merge pull request #12869 from ianb96/script_list_move

Script List Reorder
Rémi Verschelde 7 years ago
parent
commit
05a780a101
2 changed files with 309 additions and 32 deletions
  1. 297 30
      editor/plugins/script_editor_plugin.cpp
  2. 12 2
      editor/plugins/script_editor_plugin.h

+ 297 - 30
editor/plugins/script_editor_plugin.cpp

@@ -865,20 +865,7 @@ void ScriptEditor::_menu_option(int p_option) {
 		} break;
 		case SEARCH_CLASSES: {
 
-			String current;
-
-			if (tab_container->get_tab_count() > 0) {
-				EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(tab_container->get_current_tab()));
-				if (eh) {
-					current = eh->get_class();
-				}
-			}
-
 			help_index->popup();
-
-			if (current != "") {
-				help_index->call_deferred("select_class", current);
-			}
 		} break;
 		case SEARCH_WEBSITE: {
 
@@ -890,8 +877,13 @@ void ScriptEditor::_menu_option(int p_option) {
 			_history_forward();
 		} break;
 		case WINDOW_PREV: {
+
 			_history_back();
 		} break;
+		case WINDOW_SORT: {
+			_sort_list_on_update = true;
+			_update_script_names();
+		} break;
 		case DEBUG_SHOW: {
 			if (debugger) {
 				bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW));
@@ -926,10 +918,6 @@ void ScriptEditor::_menu_option(int p_option) {
 	if (current) {
 
 		switch (p_option) {
-			case FILE_NEW: {
-				script_create_dialog->config("Node", ".gd");
-				script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
-			} break;
 			case FILE_SAVE: {
 
 				if (_test_script_times_on_disk())
@@ -1041,26 +1029,22 @@ void ScriptEditor::_menu_option(int p_option) {
 					debugger->debug_continue();
 
 			} break;
-			case WINDOW_MOVE_LEFT: {
+			case WINDOW_MOVE_UP: {
 
 				if (tab_container->get_current_tab() > 0) {
-					tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() - 1);
-					script_list->call_deferred("select", tab_container->get_current_tab() - 1);
 					tab_container->move_child(current, tab_container->get_current_tab() - 1);
+					tab_container->set_current_tab(tab_container->get_current_tab() - 1);
 					_update_script_names();
 				}
 			} break;
-			case WINDOW_MOVE_RIGHT: {
+			case WINDOW_MOVE_DOWN: {
 
 				if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
-					tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() + 1);
-					script_list->call_deferred("select", tab_container->get_current_tab() + 1);
 					tab_container->move_child(current, tab_container->get_current_tab() + 1);
+					tab_container->set_current_tab(tab_container->get_current_tab() + 1);
 					_update_script_names();
 				}
-
 			} break;
-
 			default: {
 
 				if (p_option >= WINDOW_SELECT_BASE) {
@@ -1077,6 +1061,11 @@ void ScriptEditor::_menu_option(int p_option) {
 
 			switch (p_option) {
 
+				case SEARCH_CLASSES: {
+
+					help_index->popup();
+					help_index->call_deferred("select_class", help->get_class());
+				} break;
 				case HELP_SEARCH_FIND: {
 					help->popup_search();
 				} break;
@@ -1092,6 +1081,22 @@ void ScriptEditor::_menu_option(int p_option) {
 				case CLOSE_ALL: {
 					_close_all_tabs();
 				} break;
+				case WINDOW_MOVE_UP: {
+
+					if (tab_container->get_current_tab() > 0) {
+						tab_container->move_child(help, tab_container->get_current_tab() - 1);
+						tab_container->set_current_tab(tab_container->get_current_tab() - 1);
+						_update_script_names();
+					}
+				} break;
+				case WINDOW_MOVE_DOWN: {
+
+					if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
+						tab_container->move_child(help, tab_container->get_current_tab() + 1);
+						tab_container->set_current_tab(tab_container->get_current_tab() + 1);
+						_update_script_names();
+					}
+				} break;
 			}
 		}
 	}
@@ -1361,6 +1366,7 @@ struct _ScriptEditorItemData {
 	String tooltip;
 	bool used;
 	int category;
+	Node *ref;
 
 	bool operator<(const _ScriptEditorItemData &id) const {
 
@@ -1526,6 +1532,7 @@ void ScriptEditor::_update_script_names() {
 			sd.index = i;
 			sd.used = used.has(se->get_edited_script());
 			sd.category = 0;
+			sd.ref = se;
 
 			switch (sort_by) {
 				case SORT_BY_NAME: {
@@ -1565,16 +1572,38 @@ void ScriptEditor::_update_script_names() {
 			_ScriptEditorItemData sd;
 			sd.icon = icon;
 			sd.name = name;
-			sd.sort_key = name;
+			sd.sort_key = name.to_lower();
 			sd.tooltip = tooltip;
 			sd.index = i;
 			sd.used = false;
 			sd.category = split_script_help ? 1 : 0;
+			sd.ref = eh;
+
 			sedata.push_back(sd);
 		}
 	}
 
-	sedata.sort();
+	if (_sort_list_on_update) {
+		sedata.sort();
+
+		// change actual order of tab_container so that the order can be rearranged by user
+		int cur_tab = tab_container->get_current_tab();
+		int prev_tab = tab_container->get_previous_tab();
+		int new_cur_tab = -1;
+		int new_prev_tab = -1;
+		for (int i = 0; i < sedata.size(); i++) {
+			tab_container->move_child(sedata[i].ref, i);
+			if (new_prev_tab == -1 && sedata[i].index == prev_tab) {
+				new_prev_tab = i;
+			}
+			if (new_cur_tab == -1 && sedata[i].index == cur_tab) {
+				new_cur_tab = i;
+			}
+		}
+		tab_container->call_deferred("set_current_tab", new_prev_tab);
+		tab_container->call_deferred("set_current_tab", new_cur_tab);
+		_sort_list_on_update = false;
+	}
 
 	for (int i = 0; i < sedata.size(); i++) {
 
@@ -1903,8 +1932,171 @@ void ScriptEditor::_script_split_dragged(float) {
 	_save_layout();
 }
 
+Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
+
+	// return Variant(); // return this if drag disabled
+
+	Node *cur_node = tab_container->get_child(tab_container->get_current_tab());
+
+	HBoxContainer *drag_preview = memnew(HBoxContainer);
+	String preview_name = "";
+	Ref<Texture> preview_icon;
+
+	ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(cur_node);
+	if (se) {
+		preview_name = se->get_name();
+		preview_icon = se->get_icon();
+	}
+	EditorHelp *eh = Object::cast_to<EditorHelp>(cur_node);
+	if (eh) {
+		preview_name = eh->get_class();
+		preview_icon = get_icon("Help", "EditorIcons");
+	}
+
+	if (!preview_icon.is_null()) {
+		TextureRect *tf = memnew(TextureRect);
+		tf->set_texture(preview_icon);
+		drag_preview->add_child(tf);
+	}
+	Label *label = memnew(Label(preview_name));
+	drag_preview->add_child(label);
+	set_drag_preview(drag_preview);
+
+	Dictionary drag_data;
+	drag_data["type"] = "script_list_element"; // using a custom type because node caused problems when dragging to scene tree
+	drag_data["script_list_element"] = cur_node;
+
+	return drag_data;
+}
+
+bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
+
+	Dictionary d = p_data;
+	if (!d.has("type"))
+		return false;
+
+	if (String(d["type"]) == "script_list_element") {
+
+		Node *node = d["script_list_element"];
+
+		ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
+		if (se) {
+			return true;
+		}
+		EditorHelp *eh = Object::cast_to<EditorHelp>(node);
+		if (eh) {
+			return true;
+		}
+	}
+
+	if (String(d["type"]) == "nodes") {
+
+		Array nodes = d["nodes"];
+		if (nodes.size() == 0)
+			return false;
+		Node *node = get_node((nodes[0]));
+
+		ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
+		if (se) {
+			return true;
+		}
+		EditorHelp *eh = Object::cast_to<EditorHelp>(node);
+		if (eh) {
+			return true;
+		}
+	}
+
+	if (String(d["type"]) == "files") {
+
+		Vector<String> files = d["files"];
+
+		if (files.size() == 0)
+			return false; //weird
+
+		for (int i = 0; i < files.size(); i++) {
+			String file = files[i];
+			if (file == "" || !FileAccess::exists(file))
+				continue;
+			Ref<Script> scr = ResourceLoader::load(file);
+			if (scr.is_valid()) {
+				return true;
+			}
+		}
+		return true;
+	}
+
+	return false;
+}
+
+void ScriptEditor::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;
+
+	Dictionary d = p_data;
+	if (!d.has("type"))
+		return;
+
+	if (String(d["type"]) == "script_list_element") {
+
+		Node *node = d["script_list_element"];
+
+		ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
+		EditorHelp *eh = Object::cast_to<EditorHelp>(node);
+		if (se || eh) {
+			int new_index = script_list->get_item_at_position(p_point);
+			tab_container->move_child(node, new_index);
+			tab_container->set_current_tab(new_index);
+			_update_script_names();
+		}
+	}
+
+	if (String(d["type"]) == "nodes") {
+
+		Array nodes = d["nodes"];
+		if (nodes.size() == 0)
+			return;
+		Node *node = get_node(nodes[0]);
+
+		ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
+		EditorHelp *eh = Object::cast_to<EditorHelp>(node);
+		if (se || eh) {
+			int new_index = script_list->get_item_at_position(p_point);
+			tab_container->move_child(node, new_index);
+			tab_container->set_current_tab(new_index);
+			_update_script_names();
+		}
+	}
+
+	if (String(d["type"]) == "files") {
+
+		Vector<String> files = d["files"];
+
+		int new_index = script_list->get_item_at_position(p_point);
+		int num_tabs_before = tab_container->get_child_count();
+		for (int i = 0; i < files.size(); i++) {
+			String file = files[i];
+			if (file == "" || !FileAccess::exists(file))
+				continue;
+			Ref<Script> scr = ResourceLoader::load(file);
+			if (scr.is_valid()) {
+				edit(scr);
+				if (tab_container->get_child_count() > num_tabs_before) {
+					tab_container->move_child(tab_container->get_child(tab_container->get_child_count() - 1), new_index);
+					num_tabs_before = tab_container->get_child_count();
+				} else {
+					tab_container->move_child(tab_container->get_child(tab_container->get_current_tab()), new_index);
+				}
+			}
+		}
+		tab_container->set_current_tab(new_index);
+		_update_script_names();
+	}
+}
+
 void ScriptEditor::_unhandled_input(const Ref<InputEvent> &p_event) {
-	if (p_event->is_pressed() || !is_visible_in_tree()) return;
+	if (!is_visible_in_tree() || !p_event->is_pressed() || p_event->is_echo())
+		return;
 	if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
 		int next_tab = script_list->get_current() + 1;
 		next_tab %= script_list->get_item_count();
@@ -1917,6 +2109,64 @@ void ScriptEditor::_unhandled_input(const Ref<InputEvent> &p_event) {
 		_go_to_tab(script_list->get_item_metadata(next_tab));
 		_update_script_names();
 	}
+	if (ED_IS_SHORTCUT("script_editor/window_move_up", p_event)) {
+		_menu_option(WINDOW_MOVE_UP);
+	}
+	if (ED_IS_SHORTCUT("script_editor/window_move_down", p_event)) {
+		_menu_option(WINDOW_MOVE_DOWN);
+	}
+	ERR_EXPLAIN("uh: " + p_event->as_text());
+	ERR_FAIL_COND(true);
+}
+
+void ScriptEditor::_script_list_gui_input(const Ref<InputEvent> &ev) {
+
+	Ref<InputEventMouseButton> mb = ev;
+	if (mb.is_valid() && mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
+
+		_make_script_list_context_menu();
+	}
+}
+
+void ScriptEditor::_make_script_list_context_menu() {
+
+	context_menu->clear();
+
+	int selected = tab_container->get_current_tab();
+	if (selected < 0 || selected >= tab_container->get_child_count())
+		return;
+
+	ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
+	if (se) {
+		context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_SAVE);
+		context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_SAVE_AS);
+		context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_CLOSE);
+		context_menu->add_separator();
+		context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft"), FILE_TOOL_RELOAD_SOFT);
+
+		Ref<Script> scr = se->get_edited_script();
+		if (!scr.is_null() && scr->is_tool()) {
+			context_menu->add_separator();
+			context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file"), FILE_RUN);
+		}
+	} else {
+		context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_CLOSE);
+	}
+
+	EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(selected));
+	if (eh) {
+		// nothing
+	}
+
+	context_menu->add_separator();
+	context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up"), WINDOW_MOVE_UP);
+	context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down"), WINDOW_MOVE_DOWN);
+	context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort"), WINDOW_SORT);
+	context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_scripts_panel"), TOGGLE_SCRIPTS_PANEL);
+
+	context_menu->set_position(get_global_transform().xform(get_local_mouse_position()));
+	context_menu->set_size(Vector2(1, 1));
+	context_menu->popup();
 }
 
 void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
@@ -2243,9 +2493,14 @@ void ScriptEditor::_bind_methods() {
 	ClassDB::bind_method("_history_back", &ScriptEditor::_history_back);
 	ClassDB::bind_method("_live_auto_reload_running_scripts", &ScriptEditor::_live_auto_reload_running_scripts);
 	ClassDB::bind_method("_unhandled_input", &ScriptEditor::_unhandled_input);
+	ClassDB::bind_method("_script_list_gui_input", &ScriptEditor::_script_list_gui_input);
 	ClassDB::bind_method("_script_changed", &ScriptEditor::_script_changed);
 	ClassDB::bind_method("_update_recent_scripts", &ScriptEditor::_update_recent_scripts);
 
+	ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &ScriptEditor::get_drag_data_fw);
+	ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &ScriptEditor::can_drop_data_fw);
+	ClassDB::bind_method(D_METHOD("drop_data_fw"), &ScriptEditor::drop_data_fw);
+
 	ClassDB::bind_method(D_METHOD("get_current_script"), &ScriptEditor::_get_current_script);
 	ClassDB::bind_method(D_METHOD("get_open_scripts"), &ScriptEditor::_get_open_scripts);
 
@@ -2287,6 +2542,15 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
 	script_split->set_split_offset(140);
 	//list_split->set_split_offset(500);
 
+	_sort_list_on_update = true;
+	script_list->connect("gui_input", this, "_script_list_gui_input");
+	script_list->set_allow_rmb_select(true);
+	script_list->set_drag_forwarding(this);
+
+	context_menu = memnew(PopupMenu);
+	add_child(context_menu);
+	context_menu->connect("id_pressed", this, "_menu_option");
+
 	members_overview = memnew(ItemList);
 	list_split->add_child(members_overview);
 	members_overview->set_custom_minimum_size(Size2(0, 100)); //need to give a bit of limit to avoid it from disappearing
@@ -2303,8 +2567,11 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
 
 	tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
 
-	ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_GREATER);
-	ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_LESS);
+	ED_SHORTCUT("script_editor/window_sort", TTR("Sort"));
+	ED_SHORTCUT("script_editor/window_move_up", TTR("Move Up"), KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_UP);
+	ED_SHORTCUT("script_editor/window_move_down", TTR("Move Down"), KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_DOWN);
+	ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_PERIOD); // these should be KEY_GREATER and KEY_LESS but those don't work
+	ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_COLON);
 	set_process_unhandled_input(true);
 
 	file_menu = memnew(MenuButton);

+ 12 - 2
editor/plugins/script_editor_plugin.h

@@ -150,10 +150,11 @@ class ScriptEditor : public PanelContainer {
 		SEARCH_WEBSITE,
 		HELP_SEARCH_FIND,
 		HELP_SEARCH_FIND_NEXT,
-		WINDOW_MOVE_LEFT,
-		WINDOW_MOVE_RIGHT,
+		WINDOW_MOVE_UP,
+		WINDOW_MOVE_DOWN,
 		WINDOW_NEXT,
 		WINDOW_PREV,
+		WINDOW_SORT,
 		WINDOW_SELECT_BASE = 100
 	};
 
@@ -173,6 +174,7 @@ class ScriptEditor : public PanelContainer {
 	MenuButton *edit_menu;
 	MenuButton *script_search_menu;
 	MenuButton *debug_menu;
+	PopupMenu *context_menu;
 	Timer *autosave_timer;
 	uint64_t idle;
 
@@ -292,6 +294,7 @@ class ScriptEditor : public PanelContainer {
 	void _update_members_overview_visibility();
 	void _update_members_overview();
 	void _update_script_names();
+	bool _sort_list_on_update;
 
 	void _members_overview_selected(int p_idx);
 	void _script_selected(int p_idx);
@@ -306,8 +309,15 @@ class ScriptEditor : public PanelContainer {
 
 	void _script_split_dragged(float);
 
+	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);
+
 	void _unhandled_input(const Ref<InputEvent> &p_event);
 
+	void _script_list_gui_input(const Ref<InputEvent> &ev);
+	void _make_script_list_context_menu();
+
 	void _help_search(String p_text);
 	void _help_index(String p_text);