editor_dir_dialog.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/os/keyboard.h"
  32. #include "core/os/os.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor_scale.h"
  36. void EditorDirDialog::_update_dir(TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
  37. updating = true;
  38. String path = p_dir->get_path();
  39. p_item->set_metadata(0, p_dir->get_path());
  40. p_item->set_icon(0, get_icon("Folder", "EditorIcons"));
  41. p_item->set_icon_modulate(0, get_color("folder_icon_modulate", "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 == String() || !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. //this should be handled by EditorFileSystem already
  51. //bool show_hidden = EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files");
  52. updating = false;
  53. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  54. TreeItem *ti = tree->create_item(p_item);
  55. _update_dir(ti, p_dir->get_subdir(i));
  56. }
  57. }
  58. void EditorDirDialog::reload(const String &p_path) {
  59. if (!is_visible_in_tree()) {
  60. must_reload = true;
  61. return;
  62. }
  63. tree->clear();
  64. TreeItem *root = tree->create_item();
  65. _update_dir(root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
  66. _item_collapsed(root);
  67. must_reload = false;
  68. }
  69. void EditorDirDialog::_notification(int p_what) {
  70. if (p_what == NOTIFICATION_ENTER_TREE) {
  71. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "reload");
  72. reload();
  73. if (!tree->is_connected("item_collapsed", this, "_item_collapsed")) {
  74. tree->connect("item_collapsed", this, "_item_collapsed", varray(), CONNECT_DEFERRED);
  75. }
  76. if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", this, "reload")) {
  77. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "reload");
  78. }
  79. }
  80. if (p_what == NOTIFICATION_EXIT_TREE) {
  81. if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", this, "reload")) {
  82. EditorFileSystem::get_singleton()->disconnect("filesystem_changed", this, "reload");
  83. }
  84. }
  85. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  86. if (must_reload && is_visible_in_tree()) {
  87. reload();
  88. }
  89. }
  90. }
  91. void EditorDirDialog::_item_collapsed(Object *p_item) {
  92. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  93. if (updating) {
  94. return;
  95. }
  96. if (item->is_collapsed()) {
  97. opened_paths.erase(item->get_metadata(0));
  98. } else {
  99. opened_paths.insert(item->get_metadata(0));
  100. }
  101. }
  102. void EditorDirDialog::ok_pressed() {
  103. TreeItem *ti = tree->get_selected();
  104. if (!ti) {
  105. return;
  106. }
  107. String dir = ti->get_metadata(0);
  108. emit_signal("dir_selected", dir);
  109. hide();
  110. }
  111. void EditorDirDialog::_make_dir() {
  112. TreeItem *ti = tree->get_selected();
  113. if (!ti) {
  114. mkdirerr->set_text(TTR("Please select a base directory first."));
  115. mkdirerr->popup_centered_minsize();
  116. return;
  117. }
  118. makedialog->popup_centered_minsize(Size2(250, 80));
  119. makedirname->grab_focus();
  120. }
  121. void EditorDirDialog::_make_dir_confirm() {
  122. TreeItem *ti = tree->get_selected();
  123. if (!ti) {
  124. return;
  125. }
  126. String dir = ti->get_metadata(0);
  127. DirAccessRef d = DirAccess::open(dir);
  128. ERR_FAIL_COND_MSG(!d, "Cannot open directory '" + dir + "'.");
  129. String dir_name = makedirname->get_text();
  130. if (dir_name.begins_with(".")) {
  131. mkdirerr->set_text(TTR("Could not use a name with a leading dot."));
  132. mkdirerr->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  133. return;
  134. }
  135. Error err = d->make_dir(dir_name);
  136. if (err != OK) {
  137. mkdirerr->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  138. } else {
  139. opened_paths.insert(dir);
  140. //reload(dir.plus_file(makedirname->get_text()));
  141. EditorFileSystem::get_singleton()->scan_changes(); //we created a dir, so rescan changes
  142. }
  143. makedirname->set_text(""); // reset label
  144. }
  145. void EditorDirDialog::_bind_methods() {
  146. ClassDB::bind_method(D_METHOD("_item_collapsed"), &EditorDirDialog::_item_collapsed);
  147. ClassDB::bind_method(D_METHOD("_make_dir"), &EditorDirDialog::_make_dir);
  148. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &EditorDirDialog::_make_dir_confirm);
  149. ClassDB::bind_method(D_METHOD("reload"), &EditorDirDialog::reload, DEFVAL(""));
  150. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  151. }
  152. EditorDirDialog::EditorDirDialog() {
  153. updating = false;
  154. set_title(TTR("Choose a Directory"));
  155. set_hide_on_ok(false);
  156. tree = memnew(Tree);
  157. add_child(tree);
  158. tree->connect("item_activated", this, "_ok");
  159. makedir = add_button(TTR("Create Folder"), OS::get_singleton()->get_swap_ok_cancel(), "makedir");
  160. makedir->connect("pressed", this, "_make_dir");
  161. makedialog = memnew(ConfirmationDialog);
  162. makedialog->set_title(TTR("Create Folder"));
  163. add_child(makedialog);
  164. VBoxContainer *makevb = memnew(VBoxContainer);
  165. makedialog->add_child(makevb);
  166. //makedialog->set_child_rect(makevb);
  167. makedirname = memnew(LineEdit);
  168. makevb->add_margin_child(TTR("Name:"), makedirname);
  169. makedialog->register_text_enter(makedirname);
  170. makedialog->connect("confirmed", this, "_make_dir_confirm");
  171. mkdirerr = memnew(AcceptDialog);
  172. mkdirerr->set_text(TTR("Could not create folder."));
  173. add_child(mkdirerr);
  174. get_ok()->set_text(TTR("Choose"));
  175. must_reload = false;
  176. }