Pārlūkot izejas kodu

Don't try to move if dragging a folder into itself

If a folder would be moved to an invalid location disallow the drag
Don't treat dragging a file/folder to its current location as invalid
Allow dragging onto empty space / files in the files list
Fix dragging a folder onto "Favourites" starting an invalid move
MillionOstrich 7 gadi atpakaļ
vecāks
revīzija
9c65924b3d
2 mainītis faili ar 32 papildinājumiem un 44 dzēšanām
  1. 31 44
      editor/filesystem_dock.cpp
  2. 1 0
      editor/filesystem_dock.h

+ 31 - 44
editor/filesystem_dock.cpp

@@ -1292,32 +1292,20 @@ bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_da
 	}
 	}
 
 
 	if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
 	if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
+		String to_dir = _get_drag_target_folder(p_point, p_from);
+		if (to_dir.empty())
+			return false;
 
 
+		//Attempting to move a folder into itself will fail later
+		//Rather than bring up a message don't try to do it in the first place
+		to_dir = to_dir.ends_with("/") ? to_dir : (to_dir + "/");
 		Vector<String> fnames = drag_data["files"];
 		Vector<String> fnames = drag_data["files"];
-
-		if (p_from == files) {
-
-			int at_pos = files->get_item_at_position(p_point);
-			if (at_pos != -1) {
-
-				String dir = files->get_item_metadata(at_pos);
-				if (dir.ends_with("/"))
-					return true;
-			}
-		}
-
-		if (p_from == tree) {
-
-			TreeItem *ti = tree->get_item_at_position(p_point);
-			if (!ti)
-				return false;
-
-			String fpath = ti->get_metadata(0);
-			if (fpath == String())
+		for (int i = 0; i < fnames.size(); ++i) {
+			if (fnames[i].ends_with("/") && to_dir.begins_with(fnames[i]))
 				return false;
 				return false;
-
-			return true;
 		}
 		}
+
+		return true;
 	}
 	}
 
 
 	return false;
 	return false;
@@ -1431,28 +1419,8 @@ void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data,
 	}
 	}
 
 
 	if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
 	if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
-
-		if (p_from == files || p_from == tree) {
-
-			String to_dir;
-
-			if (p_from == files) {
-
-				int at_pos = files->get_item_at_position(p_point);
-				ERR_FAIL_COND(at_pos == -1);
-				to_dir = files->get_item_metadata(at_pos);
-			} else {
-				TreeItem *ti = tree->get_item_at_position(p_point);
-				if (!ti)
-					return;
-				to_dir = ti->get_metadata(0);
-				ERR_FAIL_COND(to_dir == String());
-			}
-
-			if (to_dir != "res://" && to_dir.ends_with("/")) {
-				to_dir = to_dir.substr(0, to_dir.length() - 1);
-			}
-
+		String to_dir = _get_drag_target_folder(p_point, p_from);
+		if (!to_dir.empty()) {
 			Vector<String> fnames = drag_data["files"];
 			Vector<String> fnames = drag_data["files"];
 			to_move.clear();
 			to_move.clear();
 			for (int i = 0; i < fnames.size(); i++) {
 			for (int i = 0; i < fnames.size(); i++) {
@@ -1463,6 +1431,25 @@ void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data,
 	}
 	}
 }
 }
 
 
+String FileSystemDock::_get_drag_target_folder(const Point2 &p_point, Control *p_from) const {
+	if (p_from == files) {
+		int pos = files->get_item_at_position(p_point, true);
+		if (pos == -1)
+			return path;
+
+		String target = files->get_item_metadata(pos);
+		return target.ends_with("/") ? target : path;
+	}
+
+	if (p_from == tree) {
+		TreeItem *ti = tree->get_item_at_position(p_point);
+		if (ti && ti != tree->get_root()->get_children())
+			return ti->get_metadata(0);
+	}
+
+	return String();
+}
+
 void FileSystemDock::_files_list_rmb_select(int p_item, const Vector2 &p_pos) {
 void FileSystemDock::_files_list_rmb_select(int p_item, const Vector2 &p_pos) {
 
 
 	//Right clicking ".." should clear current selection
 	//Right clicking ".." should clear current selection

+ 1 - 0
editor/filesystem_dock.h

@@ -209,6 +209,7 @@ private:
 	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
 	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;
 	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 drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
+	String _get_drag_target_folder(const Point2 &p_point, Control *p_from) const;
 
 
 	void _preview_invalidated(const String &p_path);
 	void _preview_invalidated(const String &p_path);
 	void _thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata);
 	void _thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata);