editor_dir_dialog.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**************************************************************************/
  2. /* editor_dir_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_dir_dialog.h"
  31. #include "editor/directory_create_dialog.h"
  32. #include "editor/editor_file_system.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/tree.h"
  35. #include "servers/display_server.h"
  36. void EditorDirDialog::_update_dir(TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
  37. updating = true;
  38. const String path = p_dir->get_path();
  39. p_item->set_metadata(0, path);
  40. p_item->set_icon(0, tree->get_editor_theme_icon(SNAME("Folder")));
  41. p_item->set_icon_modulate(0, tree->get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")));
  42. if (!p_item->get_parent()) {
  43. p_item->set_text(0, "res://");
  44. } else {
  45. if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) {
  46. p_item->set_collapsed(true);
  47. }
  48. p_item->set_text(0, p_dir->get_name());
  49. }
  50. if (path == new_dir_path || !p_item->get_parent()) {
  51. p_item->select(0);
  52. }
  53. updating = false;
  54. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  55. TreeItem *ti = tree->create_item(p_item);
  56. _update_dir(ti, p_dir->get_subdir(i));
  57. }
  58. }
  59. void EditorDirDialog::config(const Vector<String> &p_paths) {
  60. ERR_FAIL_COND(p_paths.is_empty());
  61. if (p_paths.size() == 1) {
  62. String path = p_paths[0];
  63. if (path.ends_with("/")) {
  64. path = path.substr(0, path.length() - 1);
  65. }
  66. // TRANSLATORS: %s is the file name that will be moved or duplicated.
  67. set_title(vformat(TTR("Move/Duplicate: %s"), path.get_file()));
  68. } else {
  69. // TRANSLATORS: %d is the number of files that will be moved or duplicated.
  70. set_title(vformat(TTRN("Move/Duplicate %d Item", "Move/Duplicate %d Items", p_paths.size()), p_paths.size()));
  71. }
  72. }
  73. void EditorDirDialog::reload(const String &p_path) {
  74. if (!is_visible()) {
  75. must_reload = true;
  76. return;
  77. }
  78. tree->clear();
  79. TreeItem *root = tree->create_item();
  80. _update_dir(root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
  81. _item_collapsed(root);
  82. new_dir_path.clear();
  83. must_reload = false;
  84. }
  85. void EditorDirDialog::_notification(int p_what) {
  86. switch (p_what) {
  87. case NOTIFICATION_ENTER_TREE: {
  88. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
  89. reload();
  90. if (!tree->is_connected("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed))) {
  91. tree->connect("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed), CONNECT_DEFERRED);
  92. }
  93. if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
  94. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
  95. }
  96. } break;
  97. case NOTIFICATION_EXIT_TREE: {
  98. if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
  99. EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload));
  100. }
  101. } break;
  102. case NOTIFICATION_VISIBILITY_CHANGED: {
  103. if (must_reload && is_visible()) {
  104. reload();
  105. }
  106. } break;
  107. }
  108. }
  109. void EditorDirDialog::_item_collapsed(Object *p_item) {
  110. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  111. if (updating) {
  112. return;
  113. }
  114. if (item->is_collapsed()) {
  115. opened_paths.erase(item->get_metadata(0));
  116. } else {
  117. opened_paths.insert(item->get_metadata(0));
  118. }
  119. }
  120. void EditorDirDialog::_item_activated() {
  121. TreeItem *ti = tree->get_selected();
  122. ERR_FAIL_NULL(ti);
  123. if (ti->get_child_count() > 0) {
  124. ti->set_collapsed(!ti->is_collapsed());
  125. }
  126. }
  127. void EditorDirDialog::_copy_pressed() {
  128. TreeItem *ti = tree->get_selected();
  129. ERR_FAIL_NULL(ti);
  130. hide();
  131. emit_signal(SNAME("copy_pressed"), ti->get_metadata(0));
  132. }
  133. void EditorDirDialog::ok_pressed() {
  134. TreeItem *ti = tree->get_selected();
  135. ERR_FAIL_NULL(ti);
  136. hide();
  137. emit_signal(SNAME("move_pressed"), ti->get_metadata(0));
  138. }
  139. void EditorDirDialog::_make_dir() {
  140. TreeItem *ti = tree->get_selected();
  141. ERR_FAIL_NULL(ti);
  142. makedialog->config(ti->get_metadata(0));
  143. makedialog->popup_centered();
  144. }
  145. void EditorDirDialog::_make_dir_confirm(const String &p_path) {
  146. // Multiple level of directories can be created at once.
  147. String base_dir = p_path.get_base_dir();
  148. while (true) {
  149. opened_paths.insert(base_dir + "/");
  150. if (base_dir == "res://") {
  151. break;
  152. }
  153. base_dir = base_dir.get_base_dir();
  154. }
  155. new_dir_path = p_path + "/";
  156. EditorFileSystem::get_singleton()->scan_changes(); // We created a dir, so rescan changes.
  157. }
  158. void EditorDirDialog::_bind_methods() {
  159. ADD_SIGNAL(MethodInfo("copy_pressed", PropertyInfo(Variant::STRING, "dir")));
  160. ADD_SIGNAL(MethodInfo("move_pressed", PropertyInfo(Variant::STRING, "dir")));
  161. }
  162. EditorDirDialog::EditorDirDialog() {
  163. set_hide_on_ok(false);
  164. VBoxContainer *vb = memnew(VBoxContainer);
  165. add_child(vb);
  166. HBoxContainer *hb = memnew(HBoxContainer);
  167. vb->add_child(hb);
  168. hb->add_child(memnew(Label(TTR("Choose target directory:"))));
  169. hb->add_spacer();
  170. makedir = memnew(Button(TTR("Create Folder")));
  171. hb->add_child(makedir);
  172. makedir->connect("pressed", callable_mp(this, &EditorDirDialog::_make_dir));
  173. tree = memnew(Tree);
  174. vb->add_child(tree);
  175. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  176. tree->connect("item_activated", callable_mp(this, &EditorDirDialog::_item_activated));
  177. set_ok_button_text(TTR("Move"));
  178. copy = add_button(TTR("Copy"), !DisplayServer::get_singleton()->get_swap_cancel_ok());
  179. copy->connect("pressed", callable_mp(this, &EditorDirDialog::_copy_pressed));
  180. makedialog = memnew(DirectoryCreateDialog);
  181. add_child(makedialog);
  182. makedialog->connect("dir_created", callable_mp(this, &EditorDirDialog::_make_dir_confirm));
  183. }