editor_dir_dialog.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*************************************************************************/
  2. /* editor_dir_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "editor_dir_dialog.h"
  30. #include "os/os.h"
  31. #include "os/keyboard.h"
  32. #include "tools/editor/editor_settings.h"
  33. #include "tools/editor/editor_file_system.h"
  34. void EditorDirDialog::_update_dir(TreeItem* p_item) {
  35. updating=true;
  36. p_item->clear_children();
  37. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  38. String cdir = p_item->get_metadata(0);
  39. da->change_dir(cdir);
  40. da->list_dir_begin();
  41. String p=da->get_next();
  42. List<String> dirs;
  43. bool ishidden;
  44. bool show_hidden = EditorSettings::get_singleton()->get("file_dialog/show_hidden_files");
  45. while(p!="") {
  46. ishidden = da->current_is_hidden();
  47. if (show_hidden || !ishidden) {
  48. if (da->current_is_dir() && !p.begins_with(".")) {
  49. dirs.push_back(p);
  50. }
  51. }
  52. p=da->get_next();
  53. }
  54. dirs.sort();
  55. for(List<String>::Element *E=dirs.front();E;E=E->next()) {
  56. TreeItem *ti = tree->create_item(p_item);
  57. ti->set_text(0,E->get());
  58. ti->set_icon(0,get_icon("Folder","EditorIcons"));
  59. ti->set_collapsed(true);
  60. }
  61. memdelete(da);
  62. updating=false;
  63. }
  64. void EditorDirDialog::reload() {
  65. if (!is_visible()) {
  66. must_reload=true;
  67. return;
  68. }
  69. tree->clear();
  70. TreeItem *root = tree->create_item();
  71. root->set_metadata(0,"res://");
  72. root->set_icon(0,get_icon("Folder","EditorIcons"));
  73. root->set_text(0,"/");
  74. _update_dir(root);
  75. _item_collapsed(root);
  76. must_reload=false;
  77. }
  78. void EditorDirDialog::_notification(int p_what) {
  79. if (p_what==NOTIFICATION_ENTER_TREE) {
  80. reload();
  81. if (!tree->is_connected("item_collapsed",this,"_item_collapsed")) {
  82. tree->connect("item_collapsed",this,"_item_collapsed",varray(),CONNECT_DEFERRED);
  83. }
  84. if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed",this,"reload")) {
  85. EditorFileSystem::get_singleton()->connect("filesystem_changed",this,"reload");
  86. }
  87. }
  88. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  89. if (must_reload && is_visible()) {
  90. reload();
  91. }
  92. }
  93. }
  94. void EditorDirDialog::_item_collapsed(Object* _p_item){
  95. TreeItem *p_item=_p_item->cast_to<TreeItem>();
  96. if (updating || p_item->is_collapsed())
  97. return;
  98. TreeItem *ci = p_item->get_children();
  99. while(ci) {
  100. String p =ci->get_metadata(0);
  101. if (p=="") {
  102. String pp = p_item->get_metadata(0);
  103. ci->set_metadata(0,pp.plus_file(ci->get_text(0)));
  104. _update_dir(ci);
  105. }
  106. ci=ci->get_next();
  107. }
  108. }
  109. void EditorDirDialog::set_current_path(const String& p_path) {
  110. reload();
  111. String p = p_path;
  112. if (p.begins_with("res://"))
  113. p.replace_first("res://","");
  114. Vector<String> dirs = p.split("/");
  115. TreeItem *r=tree->get_root();
  116. for(int i=0;i<dirs.size();i++) {
  117. String d = dirs[i];
  118. TreeItem *p = r->get_children();
  119. while(p) {
  120. if (p->get_text(0)==d)
  121. break;
  122. p=p->get_next();
  123. }
  124. ERR_FAIL_COND(!p);
  125. String pp = p->get_metadata(0);
  126. if (pp=="") {
  127. _update_dir(p);
  128. updating=true;
  129. p->set_collapsed(false);
  130. updating=false;
  131. _item_collapsed(p);
  132. }
  133. r=p;
  134. }
  135. r->select(0);
  136. }
  137. void EditorDirDialog::ok_pressed() {
  138. TreeItem *ti=tree->get_selected();
  139. if (!ti)
  140. return;
  141. String dir = ti->get_metadata(0);
  142. emit_signal("dir_selected",dir);
  143. hide();
  144. }
  145. void EditorDirDialog::_make_dir() {
  146. TreeItem *ti=tree->get_selected();
  147. if (!ti) {
  148. mkdirerr->set_text("Please select a base directory first");
  149. mkdirerr->popup_centered_minsize();
  150. return;
  151. }
  152. makedialog->popup_centered_minsize(Size2(250,80));
  153. makedirname->grab_focus();
  154. }
  155. void EditorDirDialog::_make_dir_confirm() {
  156. TreeItem *ti=tree->get_selected();
  157. if (!ti)
  158. return;
  159. String dir = ti->get_metadata(0);
  160. DirAccess *d = DirAccess::open(dir);
  161. ERR_FAIL_COND(!d);
  162. Error err = d->make_dir(makedirname->get_text());
  163. if (err!=OK) {
  164. mkdirerr->popup_centered_minsize(Size2(250,80));
  165. } else {
  166. reload();
  167. }
  168. makedirname->set_text(""); // reset label
  169. }
  170. void EditorDirDialog::_bind_methods() {
  171. ObjectTypeDB::bind_method(_MD("_item_collapsed"),&EditorDirDialog::_item_collapsed);
  172. ObjectTypeDB::bind_method(_MD("_make_dir"),&EditorDirDialog::_make_dir);
  173. ObjectTypeDB::bind_method(_MD("_make_dir_confirm"),&EditorDirDialog::_make_dir_confirm);
  174. ObjectTypeDB::bind_method(_MD("reload"),&EditorDirDialog::reload);
  175. ADD_SIGNAL(MethodInfo("dir_selected",PropertyInfo(Variant::STRING,"dir")));
  176. }
  177. EditorDirDialog::EditorDirDialog() {
  178. updating=false;
  179. set_title(TTR("Choose a Directory"));
  180. set_hide_on_ok(false);
  181. tree = memnew( Tree );
  182. add_child(tree);
  183. set_child_rect(tree);
  184. tree->connect("item_activated",this,"_ok");
  185. makedir = add_button(TTR("Create Folder"),OS::get_singleton()->get_swap_ok_cancel()?true:false,"makedir");
  186. makedir->connect("pressed",this,"_make_dir");
  187. makedialog = memnew( ConfirmationDialog );
  188. makedialog->set_title(TTR("Create Folder"));
  189. add_child(makedialog);
  190. VBoxContainer *makevb= memnew( VBoxContainer );
  191. makedialog->add_child(makevb);
  192. makedialog->set_child_rect(makevb);
  193. makedirname = memnew( LineEdit );
  194. makevb->add_margin_child(TTR("Name:"),makedirname);
  195. makedialog->register_text_enter(makedirname);
  196. makedialog->connect("confirmed",this,"_make_dir_confirm");
  197. mkdirerr = memnew( AcceptDialog );
  198. mkdirerr->set_text(TTR("Could not create folder."));
  199. add_child(mkdirerr);
  200. get_ok()->set_text(TTR("Choose"));
  201. must_reload=false;
  202. }