editor_dir_dialog.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "editor/editor_file_system.h"
  32. #include "editor/editor_settings.h"
  33. #include "os/keyboard.h"
  34. #include "os/os.h"
  35. void EditorDirDialog::_update_dir(TreeItem *p_item) {
  36. updating = true;
  37. p_item->clear_children();
  38. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  39. String cdir = p_item->get_metadata(0);
  40. da->change_dir(cdir);
  41. da->list_dir_begin();
  42. String p = da->get_next();
  43. List<String> dirs;
  44. bool ishidden;
  45. bool show_hidden = EditorSettings::get_singleton()->get("file_dialog/show_hidden_files");
  46. while (p != "") {
  47. ishidden = da->current_is_hidden();
  48. if (show_hidden || !ishidden) {
  49. if (da->current_is_dir() && !p.begins_with(".")) {
  50. dirs.push_back(p);
  51. }
  52. }
  53. p = da->get_next();
  54. }
  55. dirs.sort();
  56. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  57. TreeItem *ti = tree->create_item(p_item);
  58. ti->set_text(0, E->get());
  59. ti->set_icon(0, get_icon("Folder", "EditorIcons"));
  60. ti->set_collapsed(true);
  61. }
  62. memdelete(da);
  63. updating = false;
  64. }
  65. void EditorDirDialog::reload() {
  66. if (!is_visible()) {
  67. must_reload = true;
  68. return;
  69. }
  70. tree->clear();
  71. TreeItem *root = tree->create_item();
  72. root->set_metadata(0, "res://");
  73. root->set_icon(0, get_icon("Folder", "EditorIcons"));
  74. root->set_text(0, "/");
  75. _update_dir(root);
  76. _item_collapsed(root);
  77. must_reload = false;
  78. }
  79. void EditorDirDialog::_notification(int p_what) {
  80. if (p_what == NOTIFICATION_ENTER_TREE) {
  81. reload();
  82. if (!tree->is_connected("item_collapsed", this, "_item_collapsed")) {
  83. tree->connect("item_collapsed", this, "_item_collapsed", varray(), CONNECT_DEFERRED);
  84. }
  85. if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", this, "reload")) {
  86. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "reload");
  87. }
  88. }
  89. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  90. if (must_reload && is_visible()) {
  91. reload();
  92. }
  93. }
  94. }
  95. void EditorDirDialog::_item_collapsed(Object *_p_item) {
  96. TreeItem *p_item = _p_item->cast_to<TreeItem>();
  97. if (updating || p_item->is_collapsed())
  98. return;
  99. TreeItem *ci = p_item->get_children();
  100. while (ci) {
  101. String p = ci->get_metadata(0);
  102. if (p == "") {
  103. String pp = p_item->get_metadata(0);
  104. ci->set_metadata(0, pp.plus_file(ci->get_text(0)));
  105. _update_dir(ci);
  106. }
  107. ci = ci->get_next();
  108. }
  109. }
  110. void EditorDirDialog::set_current_path(const String &p_path) {
  111. reload();
  112. String p = p_path;
  113. if (p.begins_with("res://"))
  114. p = p.replace_first("res://", "");
  115. Vector<String> dirs = p.split("/", false);
  116. TreeItem *r = tree->get_root();
  117. for (int i = 0; i < dirs.size(); i++) {
  118. String d = dirs[i];
  119. TreeItem *p = r->get_children();
  120. while (p) {
  121. if (p->get_text(0) == d)
  122. break;
  123. p = p->get_next();
  124. }
  125. ERR_FAIL_COND(!p);
  126. String pp = p->get_metadata(0);
  127. if (pp == "") {
  128. p->set_metadata(0, String(r->get_metadata(0)).plus_file(d));
  129. _update_dir(p);
  130. }
  131. updating = true;
  132. p->set_collapsed(false);
  133. updating = false;
  134. _item_collapsed(p);
  135. r = p;
  136. }
  137. r->select(0);
  138. }
  139. void EditorDirDialog::ok_pressed() {
  140. TreeItem *ti = tree->get_selected();
  141. if (!ti)
  142. return;
  143. String dir = ti->get_metadata(0);
  144. emit_signal("dir_selected", dir);
  145. hide();
  146. }
  147. void EditorDirDialog::_make_dir() {
  148. TreeItem *ti = tree->get_selected();
  149. if (!ti) {
  150. mkdirerr->set_text("Please select a base directory first");
  151. mkdirerr->popup_centered_minsize();
  152. return;
  153. }
  154. makedialog->popup_centered_minsize(Size2(250, 80));
  155. makedirname->grab_focus();
  156. }
  157. void EditorDirDialog::_make_dir_confirm() {
  158. TreeItem *ti = tree->get_selected();
  159. if (!ti)
  160. return;
  161. String dir = ti->get_metadata(0);
  162. DirAccess *d = DirAccess::open(dir);
  163. ERR_FAIL_COND(!d);
  164. Error err = d->make_dir(makedirname->get_text());
  165. if (err != OK) {
  166. mkdirerr->popup_centered_minsize(Size2(250, 80));
  167. } else {
  168. set_current_path(dir.plus_file(makedirname->get_text()));
  169. }
  170. makedirname->set_text(""); // reset label
  171. }
  172. void EditorDirDialog::_bind_methods() {
  173. ObjectTypeDB::bind_method(_MD("_item_collapsed"), &EditorDirDialog::_item_collapsed);
  174. ObjectTypeDB::bind_method(_MD("_make_dir"), &EditorDirDialog::_make_dir);
  175. ObjectTypeDB::bind_method(_MD("_make_dir_confirm"), &EditorDirDialog::_make_dir_confirm);
  176. ObjectTypeDB::bind_method(_MD("reload"), &EditorDirDialog::reload);
  177. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  178. }
  179. EditorDirDialog::EditorDirDialog() {
  180. updating = false;
  181. set_title(TTR("Choose a Directory"));
  182. set_hide_on_ok(false);
  183. tree = memnew(Tree);
  184. add_child(tree);
  185. set_child_rect(tree);
  186. tree->connect("item_activated", this, "_ok");
  187. makedir = add_button(TTR("Create Folder"), OS::get_singleton()->get_swap_ok_cancel() ? true : false, "makedir");
  188. makedir->connect("pressed", this, "_make_dir");
  189. makedialog = memnew(ConfirmationDialog);
  190. makedialog->set_title(TTR("Create Folder"));
  191. add_child(makedialog);
  192. VBoxContainer *makevb = memnew(VBoxContainer);
  193. makedialog->add_child(makevb);
  194. makedialog->set_child_rect(makevb);
  195. makedirname = memnew(LineEdit);
  196. makevb->add_margin_child(TTR("Name:"), makedirname);
  197. makedialog->register_text_enter(makedirname);
  198. makedialog->connect("confirmed", this, "_make_dir_confirm");
  199. mkdirerr = memnew(AcceptDialog);
  200. mkdirerr->set_text(TTR("Could not create folder."));
  201. add_child(mkdirerr);
  202. get_ok()->set_text(TTR("Choose"));
  203. must_reload = false;
  204. }