editor_dir_dialog.cpp 6.4 KB

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