summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2017-11-10 09:58:07 +0100
committerGitHub <noreply@github.com>2017-11-10 09:58:07 +0100
commit98d9423fa3ecf215a270f1209315de900ae06dcf (patch)
tree74cef8849f27df43bf1ae7e651e64083c77beaf1
parent346b4b5a1c099203c3b23d72042ea5fd844059d7 (diff)
parent078371cfb64e573b614a86c21f8dd0c5ce917ba7 (diff)
Merge pull request #12402 from MillionOstrich/filesystem-drag-move
Filesystem drag & drop moving fixes
-rw-r--r--editor/filesystem_dock.cpp116
-rw-r--r--editor/filesystem_dock.h1
2 files changed, 38 insertions, 79 deletions
diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp
index 96fa9773ad..9314839768 100644
--- a/editor/filesystem_dock.cpp
+++ b/editor/filesystem_dock.cpp
@@ -1246,36 +1246,25 @@ bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_da
}
if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
- return true;
+ String to_dir = _get_drag_target_folder(p_point, p_from);
+ return !to_dir.empty();
}
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"];
-
- 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 true;
}
+
+ return true;
}
return false;
@@ -1351,66 +1340,16 @@ void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data,
if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
Ref<Resource> res = drag_data["resource"];
-
- if (!res.is_valid()) {
- return;
- }
-
- if (p_from == tree) {
-
- TreeItem *ti = tree->get_item_at_position(p_point);
- if (!ti)
- return;
-
- String fpath = ti->get_metadata(0);
- if (fpath == String())
- return;
-
- EditorNode::get_singleton()->save_resource_as(res, fpath);
- return;
- }
-
- if (p_from == files) {
- String save_path = path;
-
- int at_pos = files->get_item_at_position(p_point);
- if (at_pos != -1) {
- String to_dir = files->get_item_metadata(at_pos);
- if (to_dir.ends_with("/")) {
- save_path = to_dir;
- if (save_path != "res://")
- save_path = save_path.substr(0, save_path.length() - 1);
- }
- }
-
- EditorNode::get_singleton()->save_resource_as(res, save_path);
- return;
+ String to_dir = _get_drag_target_folder(p_point, p_from);
+ if (res.is_valid() && !to_dir.empty()) {
+ EditorNode::get_singleton()->push_item(res.ptr());
+ EditorNode::get_singleton()->save_resource_as(res, to_dir);
}
}
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"];
to_move.clear();
for (int i = 0; i < fnames.size(); i++) {
@@ -1421,6 +1360,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) {
//Right clicking ".." should clear current selection
diff --git a/editor/filesystem_dock.h b/editor/filesystem_dock.h
index 2cb0573a3d..249621564d 100644
--- a/editor/filesystem_dock.h
+++ b/editor/filesystem_dock.h
@@ -209,6 +209,7 @@ private:
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);
+ String _get_drag_target_folder(const Point2 &p_point, Control *p_from) const;
void _preview_invalidated(const String &p_path);
void _thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata);