editor_dir_dialog.cpp 6.2 KB

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