editor_dir_dialog.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. tree->clear();
  66. TreeItem *root = tree->create_item();
  67. root->set_metadata(0,"res://");
  68. root->set_icon(0,get_icon("Folder","EditorIcons"));
  69. root->set_text(0,"/");
  70. _update_dir(root);
  71. _item_collapsed(root);
  72. }
  73. void EditorDirDialog::_notification(int p_what) {
  74. if (p_what==NOTIFICATION_ENTER_TREE) {
  75. reload();
  76. tree->connect("item_collapsed",this,"_item_collapsed",varray(),CONNECT_DEFERRED);
  77. EditorFileSystem::get_singleton()->connect("filesystem_changed",this,"reload");
  78. }
  79. }
  80. void EditorDirDialog::_item_collapsed(Object* _p_item){
  81. TreeItem *p_item=_p_item->cast_to<TreeItem>();
  82. if (updating || p_item->is_collapsed())
  83. return;
  84. TreeItem *ci = p_item->get_children();
  85. while(ci) {
  86. String p =ci->get_metadata(0);
  87. if (p=="") {
  88. String pp = p_item->get_metadata(0);
  89. ci->set_metadata(0,pp.plus_file(ci->get_text(0)));
  90. _update_dir(ci);
  91. }
  92. ci=ci->get_next();
  93. }
  94. }
  95. void EditorDirDialog::set_current_path(const String& p_path) {
  96. reload();
  97. String p = p_path;
  98. if (p.begins_with("res://"))
  99. p.replace_first("res://","");
  100. Vector<String> dirs = p.split("/");
  101. TreeItem *r=tree->get_root();
  102. for(int i=0;i<dirs.size();i++) {
  103. String d = dirs[i];
  104. TreeItem *p = r->get_children();
  105. while(p) {
  106. if (p->get_text(0)==d)
  107. break;
  108. p=p->get_next();
  109. }
  110. ERR_FAIL_COND(!p);
  111. String pp = p->get_metadata(0);
  112. if (pp=="") {
  113. _update_dir(p);
  114. updating=true;
  115. p->set_collapsed(false);
  116. updating=false;
  117. _item_collapsed(p);
  118. }
  119. r=p;
  120. }
  121. r->select(0);
  122. }
  123. void EditorDirDialog::ok_pressed() {
  124. TreeItem *ti=tree->get_selected();
  125. if (!ti)
  126. return;
  127. String dir = ti->get_metadata(0);
  128. emit_signal("dir_selected",dir);
  129. hide();
  130. }
  131. void EditorDirDialog::_make_dir() {
  132. TreeItem *ti=tree->get_selected();
  133. if (!ti)
  134. return;
  135. makedialog->popup_centered_minsize(Size2(250,80));
  136. }
  137. void EditorDirDialog::_make_dir_confirm() {
  138. TreeItem *ti=tree->get_selected();
  139. if (!ti)
  140. return;
  141. String dir = ti->get_metadata(0);
  142. DirAccess *d = DirAccess::open(dir);
  143. ERR_FAIL_COND(!d);
  144. Error err = d->make_dir(makedirname->get_text());
  145. if (err!=OK) {
  146. mkdirerr->popup_centered_minsize(Size2(250,80));
  147. } else {
  148. reload();
  149. }
  150. makedirname->set_text(""); // reset label
  151. }
  152. void EditorDirDialog::_bind_methods() {
  153. ObjectTypeDB::bind_method(_MD("_item_collapsed"),&EditorDirDialog::_item_collapsed);
  154. ObjectTypeDB::bind_method(_MD("_make_dir"),&EditorDirDialog::_make_dir);
  155. ObjectTypeDB::bind_method(_MD("_make_dir_confirm"),&EditorDirDialog::_make_dir_confirm);
  156. ObjectTypeDB::bind_method(_MD("reload"),&EditorDirDialog::reload);
  157. ADD_SIGNAL(MethodInfo("dir_selected",PropertyInfo(Variant::STRING,"dir")));
  158. }
  159. EditorDirDialog::EditorDirDialog() {
  160. updating=false;
  161. set_title(TTR("Choose a Directory"));
  162. set_hide_on_ok(false);
  163. tree = memnew( Tree );
  164. add_child(tree);
  165. set_child_rect(tree);
  166. tree->connect("item_activated",this,"_ok");
  167. makedir = add_button(TTR("Create Folder"),OS::get_singleton()->get_swap_ok_cancel()?true:false,"makedir");
  168. makedir->connect("pressed",this,"_make_dir");
  169. makedialog = memnew( ConfirmationDialog );
  170. makedialog->set_title(TTR("Create Folder"));
  171. add_child(makedialog);
  172. VBoxContainer *makevb= memnew( VBoxContainer );
  173. makedialog->add_child(makevb);
  174. makedialog->set_child_rect(makevb);
  175. makedirname = memnew( LineEdit );
  176. makevb->add_margin_child(TTR("Name:"),makedirname);
  177. makedialog->register_text_enter(makedirname);
  178. makedialog->connect("confirmed",this,"_make_dir_confirm");
  179. mkdirerr = memnew( AcceptDialog );
  180. mkdirerr->set_text(TTR("Could not create folder."));
  181. add_child(mkdirerr);
  182. get_ok()->set_text(TTR("Choose"));
  183. }