editor_dir_dialog.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "core/io/dir_access.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "scene/gui/check_box.h"
  38. #include "scene/gui/tree.h"
  39. #include "servers/display_server.h"
  40. void EditorDirDialog::_update_dir(TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
  41. updating = true;
  42. String path = p_dir->get_path();
  43. p_item->set_metadata(0, p_dir->get_path());
  44. p_item->set_icon(0, tree->get_theme_icon(SNAME("Folder"), SNAME("EditorIcons")));
  45. p_item->set_icon_modulate(0, tree->get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")));
  46. if (!p_item->get_parent()) {
  47. p_item->set_text(0, "res://");
  48. } else {
  49. if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) {
  50. p_item->set_collapsed(true);
  51. }
  52. p_item->set_text(0, p_dir->get_name());
  53. }
  54. updating = false;
  55. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  56. TreeItem *ti = tree->create_item(p_item);
  57. _update_dir(ti, p_dir->get_subdir(i));
  58. }
  59. }
  60. void EditorDirDialog::reload(const String &p_path) {
  61. if (!is_visible()) {
  62. must_reload = true;
  63. return;
  64. }
  65. tree->clear();
  66. TreeItem *root = tree->create_item();
  67. _update_dir(root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
  68. _item_collapsed(root);
  69. must_reload = false;
  70. }
  71. bool EditorDirDialog::is_copy_pressed() const {
  72. return copy->is_pressed();
  73. }
  74. void EditorDirDialog::_notification(int p_what) {
  75. switch (p_what) {
  76. case NOTIFICATION_ENTER_TREE: {
  77. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
  78. reload();
  79. if (!tree->is_connected("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed))) {
  80. tree->connect("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed), CONNECT_DEFERRED);
  81. }
  82. if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
  83. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
  84. }
  85. } break;
  86. case NOTIFICATION_EXIT_TREE: {
  87. if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
  88. EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload));
  89. }
  90. } break;
  91. case NOTIFICATION_VISIBILITY_CHANGED: {
  92. if (must_reload && is_visible()) {
  93. reload();
  94. }
  95. } break;
  96. }
  97. }
  98. void EditorDirDialog::_copy_toggled(bool p_pressed) {
  99. if (p_pressed) {
  100. set_ok_button_text(TTR("Copy"));
  101. } else {
  102. set_ok_button_text(TTR("Move"));
  103. }
  104. }
  105. void EditorDirDialog::_item_collapsed(Object *p_item) {
  106. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  107. if (updating) {
  108. return;
  109. }
  110. if (item->is_collapsed()) {
  111. opened_paths.erase(item->get_metadata(0));
  112. } else {
  113. opened_paths.insert(item->get_metadata(0));
  114. }
  115. }
  116. void EditorDirDialog::_item_activated() {
  117. _ok_pressed(); // From AcceptDialog.
  118. }
  119. void EditorDirDialog::ok_pressed() {
  120. TreeItem *ti = tree->get_selected();
  121. if (!ti) {
  122. return;
  123. }
  124. String dir = ti->get_metadata(0);
  125. emit_signal(SNAME("dir_selected"), dir);
  126. hide();
  127. }
  128. void EditorDirDialog::_make_dir() {
  129. TreeItem *ti = tree->get_selected();
  130. if (!ti) {
  131. mkdirerr->set_text(TTR("Please select a base directory first."));
  132. mkdirerr->popup_centered();
  133. return;
  134. }
  135. makedialog->popup_centered(Size2(250, 80));
  136. makedirname->grab_focus();
  137. }
  138. void EditorDirDialog::_make_dir_confirm() {
  139. TreeItem *ti = tree->get_selected();
  140. if (!ti) {
  141. return;
  142. }
  143. String dir = ti->get_metadata(0);
  144. Ref<DirAccess> d = DirAccess::open(dir);
  145. ERR_FAIL_COND_MSG(d.is_null(), "Cannot open directory '" + dir + "'.");
  146. const String stripped_dirname = makedirname->get_text().strip_edges();
  147. if (d->dir_exists(stripped_dirname)) {
  148. mkdirerr->set_text(TTR("Could not create folder. File with that name already exists."));
  149. mkdirerr->popup_centered();
  150. return;
  151. }
  152. Error err = d->make_dir(stripped_dirname);
  153. if (err != OK) {
  154. mkdirerr->popup_centered(Size2(250, 80) * EDSCALE);
  155. } else {
  156. opened_paths.insert(dir);
  157. EditorFileSystem::get_singleton()->scan_changes(); // We created a dir, so rescan changes.
  158. }
  159. makedirname->set_text(""); // reset label
  160. }
  161. void EditorDirDialog::_bind_methods() {
  162. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  163. }
  164. EditorDirDialog::EditorDirDialog() {
  165. set_title(TTR("Choose a Directory"));
  166. set_hide_on_ok(false);
  167. VBoxContainer *vb = memnew(VBoxContainer);
  168. add_child(vb);
  169. tree = memnew(Tree);
  170. vb->add_child(tree);
  171. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  172. tree->connect("item_activated", callable_mp(this, &EditorDirDialog::_item_activated));
  173. copy = memnew(CheckBox);
  174. vb->add_child(copy);
  175. copy->set_text(TTR("Copy File(s)"));
  176. copy->connect("toggled", callable_mp(this, &EditorDirDialog::_copy_toggled));
  177. makedir = add_button(TTR("Create Folder"), DisplayServer::get_singleton()->get_swap_cancel_ok(), "makedir");
  178. makedir->connect("pressed", callable_mp(this, &EditorDirDialog::_make_dir));
  179. makedialog = memnew(ConfirmationDialog);
  180. makedialog->set_title(TTR("Create Folder"));
  181. add_child(makedialog);
  182. VBoxContainer *makevb = memnew(VBoxContainer);
  183. makedialog->add_child(makevb);
  184. makedirname = memnew(LineEdit);
  185. makevb->add_margin_child(TTR("Name:"), makedirname);
  186. makedialog->register_text_enter(makedirname);
  187. makedialog->connect("confirmed", callable_mp(this, &EditorDirDialog::_make_dir_confirm));
  188. mkdirerr = memnew(AcceptDialog);
  189. mkdirerr->set_text(TTR("Could not create folder."));
  190. add_child(mkdirerr);
  191. set_ok_button_text(TTR("Move"));
  192. }