editor_dir_dialog.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* editor_dir_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "servers/display_server.h"
  37. void EditorDirDialog::_update_dir(TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
  38. updating = true;
  39. String path = p_dir->get_path();
  40. p_item->set_metadata(0, p_dir->get_path());
  41. p_item->set_icon(0, tree->get_theme_icon(SNAME("Folder"), SNAME("EditorIcons")));
  42. p_item->set_icon_modulate(0, tree->get_theme_color(SNAME("folder_icon_modulate"), SNAME("FileDialog")));
  43. if (!p_item->get_parent()) {
  44. p_item->set_text(0, "res://");
  45. } else {
  46. if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) {
  47. p_item->set_collapsed(true);
  48. }
  49. p_item->set_text(0, p_dir->get_name());
  50. }
  51. //this should be handled by EditorFileSystem already
  52. //bool show_hidden = EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files");
  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::reload(const String &p_path) {
  60. if (!is_visible()) {
  61. must_reload = true;
  62. return;
  63. }
  64. tree->clear();
  65. TreeItem *root = tree->create_item();
  66. _update_dir(root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
  67. _item_collapsed(root);
  68. must_reload = false;
  69. }
  70. void EditorDirDialog::_notification(int p_what) {
  71. if (p_what == NOTIFICATION_ENTER_TREE) {
  72. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload), make_binds(""));
  73. reload();
  74. if (!tree->is_connected("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed))) {
  75. tree->connect("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed), varray(), CONNECT_DEFERRED);
  76. }
  77. if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
  78. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload), make_binds(""));
  79. }
  80. }
  81. if (p_what == NOTIFICATION_EXIT_TREE) {
  82. if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
  83. EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload));
  84. }
  85. }
  86. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  87. if (must_reload && is_visible()) {
  88. reload();
  89. }
  90. }
  91. }
  92. void EditorDirDialog::_item_collapsed(Object *p_item) {
  93. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  94. if (updating) {
  95. return;
  96. }
  97. if (item->is_collapsed()) {
  98. opened_paths.erase(item->get_metadata(0));
  99. } else {
  100. opened_paths.insert(item->get_metadata(0));
  101. }
  102. }
  103. void EditorDirDialog::_item_activated() {
  104. _ok_pressed(); // From AcceptDialog.
  105. }
  106. void EditorDirDialog::ok_pressed() {
  107. TreeItem *ti = tree->get_selected();
  108. if (!ti) {
  109. return;
  110. }
  111. String dir = ti->get_metadata(0);
  112. emit_signal(SNAME("dir_selected"), dir);
  113. hide();
  114. }
  115. void EditorDirDialog::_make_dir() {
  116. TreeItem *ti = tree->get_selected();
  117. if (!ti) {
  118. mkdirerr->set_text(TTR("Please select a base directory first."));
  119. mkdirerr->popup_centered();
  120. return;
  121. }
  122. makedialog->popup_centered(Size2(250, 80));
  123. makedirname->grab_focus();
  124. }
  125. void EditorDirDialog::_make_dir_confirm() {
  126. TreeItem *ti = tree->get_selected();
  127. if (!ti) {
  128. return;
  129. }
  130. String dir = ti->get_metadata(0);
  131. DirAccessRef d = DirAccess::open(dir);
  132. ERR_FAIL_COND_MSG(!d, "Cannot open directory '" + dir + "'.");
  133. Error err = d->make_dir(makedirname->get_text());
  134. if (err != OK) {
  135. mkdirerr->popup_centered(Size2(250, 80) * EDSCALE);
  136. } else {
  137. opened_paths.insert(dir);
  138. //reload(dir.plus_file(makedirname->get_text()));
  139. EditorFileSystem::get_singleton()->scan_changes(); //we created a dir, so rescan changes
  140. }
  141. makedirname->set_text(""); // reset label
  142. }
  143. void EditorDirDialog::_bind_methods() {
  144. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  145. }
  146. EditorDirDialog::EditorDirDialog() {
  147. updating = false;
  148. set_title(TTR("Choose a Directory"));
  149. set_hide_on_ok(false);
  150. tree = memnew(Tree);
  151. add_child(tree);
  152. tree->connect("item_activated", callable_mp(this, &EditorDirDialog::_item_activated));
  153. makedir = add_button(TTR("Create Folder"), DisplayServer::get_singleton()->get_swap_cancel_ok(), "makedir");
  154. makedir->connect("pressed", callable_mp(this, &EditorDirDialog::_make_dir));
  155. makedialog = memnew(ConfirmationDialog);
  156. makedialog->set_title(TTR("Create Folder"));
  157. add_child(makedialog);
  158. VBoxContainer *makevb = memnew(VBoxContainer);
  159. makedialog->add_child(makevb);
  160. //makedialog->set_child_rect(makevb);
  161. makedirname = memnew(LineEdit);
  162. makevb->add_margin_child(TTR("Name:"), makedirname);
  163. makedialog->register_text_enter(makedirname);
  164. makedialog->connect("confirmed", callable_mp(this, &EditorDirDialog::_make_dir_confirm));
  165. mkdirerr = memnew(AcceptDialog);
  166. mkdirerr->set_text(TTR("Could not create folder."));
  167. add_child(mkdirerr);
  168. get_ok_button()->set_text(TTR("Choose"));
  169. must_reload = false;
  170. }