scenes_dock.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* scenes_dock.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 "scenes_dock.h"
  30. #include "os/dir_access.h"
  31. #include "os/file_access.h"
  32. #include "globals.h"
  33. #include "scene/io/scene_loader.h"
  34. #include "io/resource_loader.h"
  35. #include "os/os.h"
  36. #include "editor_node.h"
  37. bool ScenesDock::_create_tree(TreeItem *p_parent,EditorFileSystemDirectory *p_dir) {
  38. String search_term = tree_filter->get_search_term();
  39. ScenesDockFilter::FilterOption file_filter = tree_filter->get_file_filter();
  40. TreeItem *item = tree->create_item(p_parent);
  41. item->set_text(0,p_dir->get_name()+"/");
  42. item->set_icon(0,get_icon("Folder","EditorIcons"));
  43. item->set_custom_bg_color(0,get_color("prop_subsection","Editor"));
  44. bool has_items=false;
  45. for(int i=0;i<p_dir->get_subdir_count();i++) {
  46. if (_create_tree(item,p_dir->get_subdir(i)))
  47. has_items=true;
  48. }
  49. for (int i=0;i<p_dir->get_file_count();i++) {
  50. String file_name = p_dir->get_file(i);
  51. String file_path = p_dir->get_file_path(i);
  52. // ScenesDockFilter::FILTER_PATH
  53. String search_from = file_path.right(6); // trim "res://"
  54. if (file_filter == ScenesDockFilter::FILTER_NAME)
  55. search_from = file_name;
  56. else if (file_filter == ScenesDockFilter::FILTER_FOLDER)
  57. search_from = file_path.right(6).get_base_dir();
  58. if (search_term!="" && search_from.findn(search_term)==-1)
  59. continue;
  60. bool isfave = favorites.has(file_path);
  61. if (button_favorite->is_pressed() && !isfave)
  62. continue;
  63. TreeItem *fitem = tree->create_item(item);
  64. fitem->set_cell_mode(0,TreeItem::CELL_MODE_CHECK);
  65. fitem->set_editable(0,true);
  66. fitem->set_checked(0,isfave);
  67. fitem->set_text(0,file_name);
  68. Ref<Texture> icon = get_icon( (has_icon(p_dir->get_file_type(i),"EditorIcons")?p_dir->get_file_type(i):String("Object")),"EditorIcons");
  69. fitem->set_icon(0, icon );
  70. fitem->set_metadata(0,file_path);
  71. //if (p_dir->files[i]->icon.is_valid()) {
  72. // fitem->set_icon(0,p_dir->files[i]->icon);
  73. // }
  74. has_items=true;
  75. }
  76. if (!has_items) {
  77. memdelete(item);
  78. return false;
  79. }
  80. return true;
  81. }
  82. void ScenesDock::_update_tree() {
  83. tree->clear();
  84. updating_tree=true;
  85. _create_tree(NULL,EditorFileSystem::get_singleton()->get_filesystem());
  86. updating_tree=false;
  87. }
  88. void ScenesDock::_notification(int p_what) {
  89. switch(p_what) {
  90. case NOTIFICATION_ENTER_SCENE: {
  91. EditorFileSystem::get_singleton()->connect("filesystem_changed",this,"_update_tree");
  92. button_reload->set_icon( get_icon("Reload","EditorIcons"));
  93. button_favorite->set_icon( get_icon("Favorites","EditorIcons"));
  94. button_instance->set_icon( get_icon("Add","EditorIcons"));
  95. button_open->set_icon( get_icon("Folder","EditorIcons"));
  96. String path = Globals::get_singleton()->get_resource_path()+"/favorites.cfg";
  97. FileAccess *f=FileAccess::open(path,FileAccess::READ);
  98. if (f) {
  99. String l = f->get_line();
  100. while(l!="") {
  101. favorites.insert(l);
  102. l = f->get_line();
  103. }
  104. f->close();
  105. memdelete(f);
  106. }
  107. _update_tree(); //maybe it finished already
  108. } break;
  109. case NOTIFICATION_EXIT_SCENE: {
  110. } break;
  111. case NOTIFICATION_PROCESS: {
  112. } break;
  113. }
  114. }
  115. void ScenesDock::_favorite_toggled() {
  116. if (updating_tree)
  117. return;
  118. TreeItem *sel = tree->get_selected();
  119. if (!sel)
  120. return; //?
  121. bool faved = sel->is_checked(0);
  122. String path = sel->get_metadata(0);
  123. if (faved)
  124. favorites.insert(path);
  125. else
  126. favorites.erase(path);
  127. timer->start();
  128. }
  129. void ScenesDock::_favorites_toggled(bool p_toggled) {
  130. _update_tree();
  131. }
  132. String ScenesDock::get_selected_path() const {
  133. TreeItem *sel = tree->get_selected();
  134. if (!sel)
  135. return "";
  136. String path = sel->get_metadata(0);
  137. return "res://"+path;
  138. }
  139. void ScenesDock::_instance_pressed() {
  140. TreeItem *sel = tree->get_selected();
  141. if (!sel)
  142. return;
  143. String path = sel->get_metadata(0);
  144. emit_signal("instance",path);
  145. }
  146. void ScenesDock::_open_pressed(){
  147. TreeItem *sel = tree->get_selected();
  148. if (!sel)
  149. return;
  150. String path = sel->get_metadata(0);
  151. if (ResourceLoader::get_resource_type(path)=="PackedScene") {
  152. editor->open_request(path);
  153. } else {
  154. /*
  155. RES res = ResourceLoader::load(path);
  156. if (res.is_null()) {
  157. return;
  158. }*/
  159. editor->load_resource(path);
  160. }
  161. // emit_signal("open",path);
  162. }
  163. void ScenesDock::_save_favorites() {
  164. String path = Globals::get_singleton()->get_resource_path()+"/favorites.cfg";
  165. FileAccess *f=FileAccess::open(path,FileAccess::WRITE);
  166. ERR_FAIL_COND(!f);
  167. for(Set<String>::Element *E=favorites.front();E;E=E->next() ) {
  168. CharString utf8f = E->get().utf8();
  169. f->store_buffer((const uint8_t*)utf8f.get_data(),utf8f.length());
  170. f->store_8('\n');
  171. }
  172. f->close();
  173. memdelete(f);
  174. }
  175. void ScenesDock::_rescan() {
  176. EditorFileSystem::get_singleton()->scan();
  177. }
  178. void ScenesDock::_bind_methods() {
  179. ObjectTypeDB::bind_method(_MD("_update_tree"),&ScenesDock::_update_tree);
  180. ObjectTypeDB::bind_method(_MD("_rescan"),&ScenesDock::_rescan);
  181. ObjectTypeDB::bind_method(_MD("_favorites_toggled"),&ScenesDock::_favorites_toggled);
  182. ObjectTypeDB::bind_method(_MD("_favorite_toggled"),&ScenesDock::_favorite_toggled);
  183. ObjectTypeDB::bind_method(_MD("_instance_pressed"),&ScenesDock::_instance_pressed);
  184. ObjectTypeDB::bind_method(_MD("_open_pressed"),&ScenesDock::_open_pressed);
  185. ObjectTypeDB::bind_method(_MD("_save_favorites"),&ScenesDock::_save_favorites);
  186. ADD_SIGNAL(MethodInfo("instance"));
  187. ADD_SIGNAL(MethodInfo("open"));
  188. }
  189. ScenesDock::ScenesDock(EditorNode *p_editor) {
  190. editor=p_editor;
  191. HBoxContainer *toolbar_hbc = memnew( HBoxContainer );
  192. add_child(toolbar_hbc);
  193. button_reload = memnew( Button );
  194. button_reload->set_flat(true);
  195. button_reload->connect("pressed",this,"_rescan");
  196. toolbar_hbc->add_child(button_reload);
  197. button_favorite = memnew( Button );
  198. button_favorite->set_flat(true);
  199. button_favorite->set_toggle_mode(true);
  200. button_favorite->connect("toggled",this,"_favorites_toggled");
  201. toolbar_hbc->add_child(button_favorite);
  202. toolbar_hbc->add_spacer();
  203. button_open = memnew( Button );
  204. button_open->set_flat(true);
  205. button_open->connect("pressed",this,"_open_pressed");
  206. toolbar_hbc->add_child(button_open);
  207. button_instance = memnew( Button );
  208. button_instance->set_flat(true);
  209. button_instance->connect("pressed",this,"_instance_pressed");
  210. toolbar_hbc->add_child(button_instance);
  211. tree = memnew( Tree );
  212. tree_filter=memnew( ScenesDockFilter() );
  213. tree_filter->connect("filter_changed", this, "_update_tree");
  214. add_child(tree_filter);
  215. add_child(tree);
  216. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  217. tree->connect("item_edited",this,"_favorite_toggled");
  218. timer = memnew( Timer );
  219. timer->set_one_shot(true);
  220. timer->set_wait_time(2);
  221. timer->connect("timeout",this,"_save_favorites");
  222. add_child(timer);
  223. updating_tree=false;
  224. }
  225. ScenesDock::~ScenesDock() {
  226. }
  227. void ScenesDockFilter::_setup_filters() {
  228. filter_option->clear();
  229. filter_option->add_item("Path");
  230. filter_option->add_item("Name");
  231. filter_option->add_item("Folder");
  232. #if 0
  233. List<String> extensions;
  234. ResourceLoader::get_recognized_extensions_for_type("",&extensions);
  235. file_filter->add_item("All Files (*)");
  236. filters.push_back("*");
  237. List<String> filter_texts;
  238. for(int i=0;i<extensions.size();i++) {
  239. filter_texts.push_back("*."+extensions[i]+" ; "+extensions[i].to_upper());
  240. filters.push_back(extensions[i]);
  241. }
  242. for(int i=0;i<filter_texts.size();i++) {
  243. String flt=filter_texts[i].get_slice(";",0).strip_edges();
  244. String desc=filter_texts[i].get_slice(";",1).strip_edges();
  245. if (desc.length())
  246. file_filter->add_item(desc+" ( "+flt+" )");
  247. else
  248. file_filter->add_item("( "+flt+" )");
  249. }
  250. #endif
  251. }
  252. void ScenesDockFilter::_command(int p_command) {
  253. switch (p_command) {
  254. case CMD_CLEAR_FILTER: {
  255. if (search_box->get_text()!="") {
  256. search_box->clear();
  257. emit_signal("filter_changed");
  258. }
  259. }break;
  260. }
  261. }
  262. void ScenesDockFilter::_search_text_changed(const String &p_newtext) {
  263. emit_signal("filter_changed");
  264. }
  265. String ScenesDockFilter::get_search_term() {
  266. return search_box->get_text().strip_edges();
  267. }
  268. ScenesDockFilter::FilterOption ScenesDockFilter::get_file_filter() {
  269. return _current_filter;
  270. }
  271. void ScenesDockFilter::_file_filter_selected(int p_idx) {
  272. FilterOption selected = (FilterOption)(filter_option->get_selected());
  273. if (_current_filter != selected ) {
  274. _current_filter = selected;
  275. emit_signal("filter_changed");
  276. }
  277. }
  278. void ScenesDockFilter::_notification(int p_what) {
  279. switch(p_what) {
  280. case NOTIFICATION_ENTER_SCENE: {
  281. clear_search_button->set_icon(get_icon("CloseHover","EditorIcons"));
  282. } break;
  283. }
  284. }
  285. void ScenesDockFilter::_bind_methods() {
  286. ObjectTypeDB::bind_method(_MD("_command"),&ScenesDockFilter::_command);
  287. ObjectTypeDB::bind_method(_MD("_search_text_changed"), &ScenesDockFilter::_search_text_changed);
  288. ObjectTypeDB::bind_method(_MD("_file_filter_selected"), &ScenesDockFilter::_file_filter_selected);
  289. ADD_SIGNAL( MethodInfo("filter_changed") );
  290. }
  291. ScenesDockFilter::ScenesDockFilter() {
  292. _current_filter = FILTER_PATH;
  293. filter_option = memnew( OptionButton );
  294. filter_option->set_custom_minimum_size(Size2(60,10));
  295. filter_option->set_clip_text(true);
  296. filter_option->connect("item_selected", this, "_file_filter_selected");
  297. add_child(filter_option);
  298. _setup_filters();
  299. search_box = memnew( LineEdit );
  300. search_box->connect("text_changed",this,"_search_text_changed");
  301. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  302. add_child(search_box);
  303. clear_search_button = memnew( ToolButton );
  304. clear_search_button->connect("pressed",this,"_command",make_binds(CMD_CLEAR_FILTER));
  305. add_child(clear_search_button);
  306. }