Skip to content

Commit 98d9423

Browse files
authored
Merge pull request #12402 from MillionOstrich/filesystem-drag-move
Filesystem drag & drop moving fixes
2 parents 346b4b5 + 078371c commit 98d9423

File tree

2 files changed

+38
-79
lines changed

2 files changed

+38
-79
lines changed

editor/filesystem_dock.cpp

+37-79
Original file line numberDiff line numberDiff line change
@@ -1246,36 +1246,25 @@ bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_da
12461246
}
12471247

12481248
if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
1249-
return true;
1249+
String to_dir = _get_drag_target_folder(p_point, p_from);
1250+
return !to_dir.empty();
12501251
}
12511252

12521253
if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
1254+
String to_dir = _get_drag_target_folder(p_point, p_from);
1255+
if (to_dir.empty())
1256+
return false;
12531257

1258+
//Attempting to move a folder into itself will fail later
1259+
//Rather than bring up a message don't try to do it in the first place
1260+
to_dir = to_dir.ends_with("/") ? to_dir : (to_dir + "/");
12541261
Vector<String> fnames = drag_data["files"];
1255-
1256-
if (p_from == files) {
1257-
1258-
int at_pos = files->get_item_at_position(p_point);
1259-
if (at_pos != -1) {
1260-
1261-
String dir = files->get_item_metadata(at_pos);
1262-
if (dir.ends_with("/"))
1263-
return true;
1264-
}
1265-
}
1266-
1267-
if (p_from == tree) {
1268-
1269-
TreeItem *ti = tree->get_item_at_position(p_point);
1270-
if (!ti)
1271-
return false;
1272-
1273-
String fpath = ti->get_metadata(0);
1274-
if (fpath == String())
1262+
for (int i = 0; i < fnames.size(); ++i) {
1263+
if (fnames[i].ends_with("/") && to_dir.begins_with(fnames[i]))
12751264
return false;
1276-
1277-
return true;
12781265
}
1266+
1267+
return true;
12791268
}
12801269

12811270
return false;
@@ -1351,66 +1340,16 @@ void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data,
13511340

13521341
if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
13531342
Ref<Resource> res = drag_data["resource"];
1354-
1355-
if (!res.is_valid()) {
1356-
return;
1357-
}
1358-
1359-
if (p_from == tree) {
1360-
1361-
TreeItem *ti = tree->get_item_at_position(p_point);
1362-
if (!ti)
1363-
return;
1364-
1365-
String fpath = ti->get_metadata(0);
1366-
if (fpath == String())
1367-
return;
1368-
1369-
EditorNode::get_singleton()->save_resource_as(res, fpath);
1370-
return;
1371-
}
1372-
1373-
if (p_from == files) {
1374-
String save_path = path;
1375-
1376-
int at_pos = files->get_item_at_position(p_point);
1377-
if (at_pos != -1) {
1378-
String to_dir = files->get_item_metadata(at_pos);
1379-
if (to_dir.ends_with("/")) {
1380-
save_path = to_dir;
1381-
if (save_path != "res://")
1382-
save_path = save_path.substr(0, save_path.length() - 1);
1383-
}
1384-
}
1385-
1386-
EditorNode::get_singleton()->save_resource_as(res, save_path);
1387-
return;
1343+
String to_dir = _get_drag_target_folder(p_point, p_from);
1344+
if (res.is_valid() && !to_dir.empty()) {
1345+
EditorNode::get_singleton()->push_item(res.ptr());
1346+
EditorNode::get_singleton()->save_resource_as(res, to_dir);
13881347
}
13891348
}
13901349

13911350
if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
1392-
1393-
if (p_from == files || p_from == tree) {
1394-
1395-
String to_dir;
1396-
1397-
if (p_from == files) {
1398-
1399-
int at_pos = files->get_item_at_position(p_point);
1400-
ERR_FAIL_COND(at_pos == -1);
1401-
to_dir = files->get_item_metadata(at_pos);
1402-
} else {
1403-
TreeItem *ti = tree->get_item_at_position(p_point);
1404-
if (!ti)
1405-
return;
1406-
to_dir = ti->get_metadata(0);
1407-
ERR_FAIL_COND(to_dir == String());
1408-
}
1409-
1410-
if (to_dir != "res://" && to_dir.ends_with("/")) {
1411-
to_dir = to_dir.substr(0, to_dir.length() - 1);
1412-
}
1413-
1351+
String to_dir = _get_drag_target_folder(p_point, p_from);
1352+
if (!to_dir.empty()) {
14141353
Vector<String> fnames = drag_data["files"];
14151354
to_move.clear();
14161355
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,
14211360
}
14221361
}
14231362

1363+
String FileSystemDock::_get_drag_target_folder(const Point2 &p_point, Control *p_from) const {
1364+
if (p_from == files) {
1365+
int pos = files->get_item_at_position(p_point, true);
1366+
if (pos == -1)
1367+
return path;
1368+
1369+
String target = files->get_item_metadata(pos);
1370+
return target.ends_with("/") ? target : path;
1371+
}
1372+
1373+
if (p_from == tree) {
1374+
TreeItem *ti = tree->get_item_at_position(p_point);
1375+
if (ti && ti != tree->get_root()->get_children())
1376+
return ti->get_metadata(0);
1377+
}
1378+
1379+
return String();
1380+
}
1381+
14241382
void FileSystemDock::_files_list_rmb_select(int p_item, const Vector2 &p_pos) {
14251383

14261384
//Right clicking ".." should clear current selection

editor/filesystem_dock.h

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class FileSystemDock : public VBoxContainer {
209209
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
210210
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
211211
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
212+
String _get_drag_target_folder(const Point2 &p_point, Control *p_from) const;
212213

213214
void _preview_invalidated(const String &p_path);
214215
void _thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata);

0 commit comments

Comments
 (0)