editor_dir_dialog.cpp 6.0 KB

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