2
0

filesystem_dock.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /*************************************************************************/
  2. /* filesystem_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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 "filesystem_dock.h"
  30. #include "os/dir_access.h"
  31. #include "os/file_access.h"
  32. #include "global_config.h"
  33. #include "io/resource_loader.h"
  34. #include "os/os.h"
  35. #include "editor_node.h"
  36. #include "editor_settings.h"
  37. #include "scene/main/viewport.h"
  38. bool FileSystemDock::_create_tree(TreeItem *p_parent,EditorFileSystemDirectory *p_dir) {
  39. TreeItem *item = tree->create_item(p_parent);
  40. String dname=p_dir->get_name();
  41. if (dname=="")
  42. dname="res://";
  43. else {
  44. // collapse every tree item but the root folder
  45. item->set_collapsed(true);
  46. }
  47. item->set_text(0,dname);
  48. item->set_icon(0,get_icon("Folder","EditorIcons"));
  49. item->set_selectable(0,true);
  50. String lpath = p_dir->get_path();
  51. if (lpath!="res://" && lpath.ends_with("/")) {
  52. lpath=lpath.substr(0,lpath.length()-1);
  53. }
  54. item->set_metadata(0,lpath);
  55. if (lpath==path) {
  56. item->select(0);
  57. }
  58. for(int i=0;i<p_dir->get_subdir_count();i++)
  59. _create_tree(item,p_dir->get_subdir(i));
  60. return true;
  61. }
  62. void FileSystemDock::_update_tree() {
  63. tree->clear();
  64. updating_tree=true;
  65. TreeItem *root = tree->create_item();
  66. TreeItem *favorites = tree->create_item(root);
  67. favorites->set_icon(0, get_icon("Favorites","EditorIcons") );
  68. favorites->set_text(0,TTR("Favorites:"));
  69. favorites->set_selectable(0,false);
  70. Vector<String> faves = EditorSettings::get_singleton()->get_favorite_dirs();
  71. for(int i=0;i<faves.size();i++) {
  72. if (!faves[i].begins_with("res://"))
  73. continue;
  74. TreeItem *ti = tree->create_item(favorites);
  75. String fv = faves[i];
  76. if (fv=="res://")
  77. ti->set_text(0,"/");
  78. else
  79. ti->set_text(0,faves[i].get_file());
  80. ti->set_icon(0,get_icon("Folder","EditorIcons"));
  81. ti->set_selectable(0,true);
  82. ti->set_metadata(0,faves[i]);
  83. }
  84. _create_tree(root,EditorFileSystem::get_singleton()->get_filesystem());
  85. updating_tree=false;
  86. }
  87. void FileSystemDock::_notification(int p_what) {
  88. switch(p_what) {
  89. case NOTIFICATION_RESIZED: {
  90. bool new_mode = get_size().height < get_viewport_rect().size.height*3/4;
  91. if (new_mode != split_mode ) {
  92. split_mode=new_mode;
  93. //print_line("SPLIT MODE? "+itos(split_mode));
  94. if (split_mode) {
  95. file_list_vb->hide();
  96. tree->set_custom_minimum_size(Size2(0,0));
  97. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  98. button_back->show();
  99. } else {
  100. tree->show();
  101. file_list_vb->show();
  102. tree->set_custom_minimum_size(Size2(0,200)*EDSCALE);
  103. tree->set_v_size_flags(SIZE_FILL);
  104. button_back->hide();
  105. if (!EditorFileSystem::get_singleton()->is_scanning()) {
  106. _fs_changed();
  107. }
  108. }
  109. }
  110. } break;
  111. case NOTIFICATION_ENTER_TREE: {
  112. if (initialized)
  113. return;
  114. initialized=true;
  115. EditorFileSystem::get_singleton()->connect("filesystem_changed",this,"_fs_changed");
  116. EditorResourcePreview::get_singleton()->connect("preview_invalidated",this,"_preview_invalidated");
  117. button_reload->set_icon( get_icon("Reload","EditorIcons"));
  118. button_favorite->set_icon( get_icon("Favorites","EditorIcons"));
  119. //button_instance->set_icon( get_icon("Add","EditorIcons"));
  120. //button_open->set_icon( get_icon("Folder","EditorIcons"));
  121. button_back->set_icon( get_icon("Filesystem","EditorIcons"));
  122. if (display_mode == DISPLAY_THUMBNAILS) {
  123. button_display_mode->set_icon(get_icon("FileList","EditorIcons"));
  124. } else {
  125. button_display_mode->set_icon(get_icon("FileThumbnail","EditorIcons"));
  126. }
  127. button_display_mode->connect("pressed",this,"_change_file_display");
  128. //file_options->set_icon( get_icon("Tools","EditorIcons"));
  129. files->connect("item_activated",this,"_select_file");
  130. button_hist_next->connect("pressed",this,"_fw_history");
  131. button_hist_prev->connect("pressed",this,"_bw_history");
  132. search_icon->set_texture( get_icon("Zoom","EditorIcons"));
  133. button_hist_next->set_icon( get_icon("Forward","EditorIcons"));
  134. button_hist_prev->set_icon( get_icon("Back","EditorIcons"));
  135. file_options->connect("id_pressed",this,"_file_option");
  136. folder_options->connect("id_pressed",this,"_folder_option");
  137. button_back->connect("pressed",this,"_go_to_tree",varray(),CONNECT_DEFERRED);
  138. current_path->connect("text_entered",this,"_go_to_dir");
  139. _update_tree(); //maybe it finished already
  140. if (EditorFileSystem::get_singleton()->is_scanning()) {
  141. _set_scanning_mode();
  142. }
  143. } break;
  144. case NOTIFICATION_PROCESS: {
  145. if (EditorFileSystem::get_singleton()->is_scanning()) {
  146. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress()*100);
  147. }
  148. } break;
  149. case NOTIFICATION_EXIT_TREE: {
  150. } break;
  151. case NOTIFICATION_DRAG_BEGIN: {
  152. Dictionary dd = get_viewport()->gui_get_drag_data();
  153. if (tree->is_visible_in_tree() && dd.has("type") ) {
  154. if ( (String(dd["type"])=="files") || (String(dd["type"])=="files_and_dirs") || (String(dd["type"])=="resource")) {
  155. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  156. }
  157. if ( (String(dd["type"])=="favorite") ) {
  158. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  159. }
  160. }
  161. } break;
  162. case NOTIFICATION_DRAG_END: {
  163. tree->set_drop_mode_flags(0);
  164. } break;
  165. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  166. int new_mode = int(EditorSettings::get_singleton()->get("docks/filesystem/display_mode"));
  167. if (new_mode != display_mode) {
  168. set_display_mode(new_mode);
  169. } else {
  170. _update_files(true);
  171. }
  172. } break;
  173. }
  174. }
  175. void FileSystemDock::_dir_selected() {
  176. TreeItem *ti = tree->get_selected();
  177. if (!ti)
  178. return;
  179. String dir = ti->get_metadata(0);
  180. bool found=false;
  181. Vector<String> favorites = EditorSettings::get_singleton()->get_favorite_dirs();
  182. for(int i=0;i<favorites.size();i++) {
  183. if (favorites[i]==dir) {
  184. found=true;
  185. break;
  186. }
  187. }
  188. button_favorite->set_pressed(found);
  189. if (!split_mode) {
  190. _open_pressed(); //go directly to dir
  191. }
  192. }
  193. void FileSystemDock::_favorites_pressed() {
  194. TreeItem *sel = tree->get_selected();
  195. if (!sel)
  196. return ;
  197. String dir = sel->get_metadata(0);
  198. int idx = -1;
  199. Vector<String> favorites = EditorSettings::get_singleton()->get_favorite_dirs();
  200. for(int i=0;i<favorites.size();i++) {
  201. if (favorites[i]==dir) {
  202. idx=i;
  203. break;
  204. }
  205. }
  206. if (button_favorite->is_pressed() && idx==-1) {
  207. favorites.push_back(dir);
  208. EditorSettings::get_singleton()->set_favorite_dirs(favorites);
  209. _update_tree();
  210. } else if (!button_favorite->is_pressed() && idx!=-1) {
  211. favorites.remove(idx);
  212. EditorSettings::get_singleton()->set_favorite_dirs(favorites);
  213. _update_tree();
  214. }
  215. }
  216. String FileSystemDock::get_selected_path() const {
  217. TreeItem *sel = tree->get_selected();
  218. if (!sel)
  219. return "";
  220. String path = sel->get_metadata(0);
  221. return "res://"+path;
  222. }
  223. String FileSystemDock::get_current_path() const {
  224. return path;
  225. }
  226. void FileSystemDock::navigate_to_path(const String& p_path) {
  227. // If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
  228. String dir_path="";
  229. String file_name="";
  230. DirAccess* dirAccess=DirAccess::open("res://");
  231. if (dirAccess->file_exists(p_path)) {
  232. dir_path=p_path.get_base_dir();
  233. file_name=p_path.get_file();
  234. } else if (dirAccess->dir_exists(p_path)) {
  235. dir_path=p_path;
  236. } else {
  237. ERR_EXPLAIN(TTR("Cannot navigate to '" + p_path + "' as it has not been found in the file system!"));
  238. ERR_FAIL();
  239. }
  240. path=dir_path;
  241. _update_tree();
  242. tree->ensure_cursor_is_visible();
  243. if (!file_name.empty()) {
  244. _open_pressed(); // Seems to be the only way to get into the file view. This also pushes to history.
  245. // Focus the given file.
  246. for (int i=0; i<files->get_item_count(); i++) {
  247. if (files->get_item_text(i) == file_name) {
  248. files->select(i,true);
  249. files->ensure_current_is_visible();
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. void FileSystemDock::_thumbnail_done(const String& p_path,const Ref<Texture>& p_preview, const Variant& p_udata) {
  256. bool valid=false;
  257. if (search_box->is_visible()) {
  258. valid=true;
  259. } else {
  260. valid=(path==p_path.get_base_dir());
  261. }
  262. if (p_preview.is_valid() && valid) {
  263. Array uarr=p_udata;
  264. int idx=uarr[0];
  265. String file=uarr[1];
  266. if (idx>=files->get_item_count())
  267. return;
  268. if (files->get_item_text(idx)!=file)
  269. return;
  270. String fpath = files->get_item_metadata(idx);
  271. if (fpath!=p_path)
  272. return;
  273. files->set_item_icon(idx,p_preview);
  274. }
  275. }
  276. void FileSystemDock::_change_file_display() {
  277. if (button_display_mode->is_pressed()) {
  278. display_mode = DISPLAY_LIST;
  279. button_display_mode->set_icon( get_icon("FileThumbnail","EditorIcons"));
  280. } else {
  281. display_mode = DISPLAY_THUMBNAILS;
  282. button_display_mode->set_icon( get_icon("FileList","EditorIcons"));
  283. }
  284. EditorSettings::get_singleton()->set("docks/filesystem/display_mode", display_mode);
  285. _update_files(true);
  286. }
  287. void FileSystemDock::_search(EditorFileSystemDirectory *p_path,List<FileInfo>* matches,int p_max_items) {
  288. if (matches->size()>p_max_items)
  289. return;
  290. for(int i=0;i<p_path->get_subdir_count();i++) {
  291. _search(p_path->get_subdir(i),matches,p_max_items);
  292. }
  293. String match = search_box->get_text();
  294. for(int i=0;i<p_path->get_file_count();i++) {
  295. String file = p_path->get_file(i);
  296. if (file.find(match)!=-1) {
  297. FileInfo fi;
  298. fi.name=file;
  299. fi.type=p_path->get_file_type(i);
  300. fi.path=p_path->get_file_path(i);
  301. fi.import_status=0;
  302. matches->push_back(fi);
  303. if (matches->size()>p_max_items)
  304. return;
  305. }
  306. }
  307. }
  308. void FileSystemDock::_update_files(bool p_keep_selection) {
  309. Set<String> cselection;
  310. if (p_keep_selection) {
  311. for(int i=0;i<files->get_item_count();i++) {
  312. if (files->is_selected(i))
  313. cselection.insert(files->get_item_text(i));
  314. }
  315. }
  316. files->clear();
  317. current_path->set_text(path);
  318. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(path);
  319. if (!efd)
  320. return;
  321. int thumbnail_size = EditorSettings::get_singleton()->get("docks/filesystem/thumbnail_size");
  322. thumbnail_size*=EDSCALE;
  323. Ref<Texture> folder_thumbnail;
  324. Ref<Texture> file_thumbnail;
  325. bool use_thumbnails = (display_mode == DISPLAY_THUMBNAILS);
  326. bool use_folders = search_box->get_text().length()==0 && split_mode;
  327. if (use_thumbnails) { //thumbnails
  328. files->set_max_columns(0);
  329. files->set_icon_mode(ItemList::ICON_MODE_TOP);
  330. files->set_fixed_column_width(thumbnail_size*3/2);
  331. files->set_max_text_lines(2);
  332. files->set_fixed_icon_size(Size2(thumbnail_size,thumbnail_size));
  333. if (!has_icon("ResizedFolder","EditorIcons")) {
  334. Ref<ImageTexture> folder = get_icon("FolderBig","EditorIcons");
  335. Image img = folder->get_data();
  336. img.resize(thumbnail_size,thumbnail_size);
  337. Ref<ImageTexture> resized_folder = Ref<ImageTexture>( memnew( ImageTexture));
  338. resized_folder->create_from_image(img,0);
  339. Theme::get_default()->set_icon("ResizedFolder","EditorIcons",resized_folder);
  340. }
  341. folder_thumbnail = get_icon("ResizedFolder","EditorIcons");
  342. if (!has_icon("ResizedFile","EditorIcons")) {
  343. Ref<ImageTexture> file = get_icon("FileBig","EditorIcons");
  344. Image img = file->get_data();
  345. img.resize(thumbnail_size,thumbnail_size);
  346. Ref<ImageTexture> resized_file = Ref<ImageTexture>( memnew( ImageTexture));
  347. resized_file->create_from_image(img,0);
  348. Theme::get_default()->set_icon("ResizedFile","EditorIcons",resized_file);
  349. }
  350. file_thumbnail = get_icon("ResizedFile","EditorIcons");
  351. } else {
  352. files->set_icon_mode(ItemList::ICON_MODE_LEFT);
  353. files->set_max_columns(1);
  354. files->set_max_text_lines(1);
  355. files->set_fixed_column_width(0);
  356. files->set_fixed_icon_size(Size2());
  357. }
  358. if (use_folders) {
  359. if (path!="res://") {
  360. if (use_thumbnails) {
  361. files->add_item("..",folder_thumbnail,true);
  362. } else {
  363. files->add_item("..",get_icon("folder","FileDialog"),true);
  364. }
  365. String bd = path.get_base_dir();
  366. if (bd!="res://" && !bd.ends_with("/"))
  367. bd+="/";
  368. files->set_item_metadata(files->get_item_count()-1,bd);
  369. }
  370. for(int i=0;i<efd->get_subdir_count();i++) {
  371. String dname=efd->get_subdir(i)->get_name();
  372. if (use_thumbnails) {
  373. files->add_item(dname,folder_thumbnail,true);
  374. } else {
  375. files->add_item(dname,get_icon("folder","FileDialog"),true);
  376. }
  377. files->set_item_metadata(files->get_item_count()-1,path.plus_file(dname)+"/");
  378. if (cselection.has(dname))
  379. files->select(files->get_item_count()-1,false);
  380. }
  381. }
  382. List<FileInfo> filelist;
  383. if (search_box->get_text().length()) {
  384. if (search_box->get_text().length()>1) {
  385. _search(EditorFileSystem::get_singleton()->get_filesystem(),&filelist,128);
  386. }
  387. filelist.sort();
  388. } else {
  389. for(int i=0;i<efd->get_file_count();i++) {
  390. FileInfo fi;
  391. fi.name=efd->get_file(i);
  392. fi.path=path.plus_file(fi.name);
  393. fi.type=efd->get_file_type(i);
  394. fi.import_status=0;
  395. filelist.push_back(fi);
  396. }
  397. }
  398. StringName ei="EditorIcons"; //make it faster..
  399. StringName oi="Object";
  400. for(List<FileInfo>::Element *E=filelist.front();E;E=E->next()) {
  401. String fname=E->get().name;
  402. String fp = E->get().path;
  403. StringName type = E->get().type;
  404. Ref<Texture> type_icon;
  405. String tooltip=fname;
  406. if (E->get().import_status==0) {
  407. if (has_icon(type,ei)) {
  408. type_icon=get_icon(type,ei);
  409. } else {
  410. type_icon=get_icon(oi,ei);
  411. }
  412. } else if (E->get().import_status==1) {
  413. type_icon=get_icon("DependencyOk","EditorIcons");
  414. } else if (E->get().import_status==2) {
  415. type_icon=get_icon("DependencyChanged","EditorIcons");
  416. tooltip+"\nStatus: Needs Re-Import";
  417. } else if (E->get().import_status==3) {
  418. type_icon=get_icon("ImportFail","EditorIcons");
  419. tooltip+"\nStatus: Missing Dependencies";
  420. }
  421. if (E->get().sources.size()) {
  422. for(int i=0;i<E->get().sources.size();i++) {
  423. tooltip+="\nSource: "+E->get().sources[i];
  424. }
  425. }
  426. if (use_thumbnails) {
  427. files->add_item(fname,file_thumbnail,true);
  428. files->set_item_metadata(files->get_item_count()-1,fp);
  429. files->set_item_tag_icon(files->get_item_count()-1,type_icon);
  430. Array udata;
  431. udata.resize(2);
  432. udata[0]=files->get_item_count()-1;
  433. udata[1]=fname;
  434. EditorResourcePreview::get_singleton()->queue_resource_preview(fp,this,"_thumbnail_done",udata);
  435. } else {
  436. files->add_item(fname,type_icon,true);
  437. files->set_item_metadata(files->get_item_count()-1,fp);
  438. }
  439. if (cselection.has(fname))
  440. files->select(files->get_item_count()-1,false);
  441. files->set_item_tooltip(files->get_item_count()-1,tooltip);
  442. }
  443. }
  444. void FileSystemDock::_select_file(int p_idx) {
  445. files->select(p_idx,true);
  446. _file_option(FILE_OPEN);
  447. }
  448. void FileSystemDock::_go_to_tree() {
  449. tree->show();
  450. file_list_vb->hide();
  451. _update_tree();
  452. tree->grab_focus();
  453. tree->ensure_cursor_is_visible();
  454. button_favorite->show();
  455. //button_open->hide();
  456. //file_options->hide();
  457. }
  458. void FileSystemDock::_go_to_dir(const String& p_dir){
  459. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  460. if (da->change_dir(p_dir)==OK) {
  461. path=da->get_current_dir();
  462. _update_files(false);
  463. }
  464. current_path->set_text(path);
  465. memdelete(da);
  466. }
  467. void FileSystemDock::_preview_invalidated(const String& p_path) {
  468. if (p_path.get_base_dir()==path && search_box->get_text()==String() && file_list_vb->is_visible_in_tree()) {
  469. for(int i=0;i<files->get_item_count();i++) {
  470. if (files->get_item_metadata(i)==p_path) {
  471. //re-request preview
  472. Array udata;
  473. udata.resize(2);
  474. udata[0]=i;
  475. udata[1]=files->get_item_text(i);
  476. EditorResourcePreview::get_singleton()->queue_resource_preview(p_path,this,"_thumbnail_done",udata);
  477. break;
  478. }
  479. }
  480. }
  481. }
  482. void FileSystemDock::_fs_changed() {
  483. button_hist_prev->set_disabled(history_pos==0);
  484. button_hist_next->set_disabled(history_pos+1==history.size());
  485. scanning_vb->hide();
  486. split_box->show();
  487. if (tree->is_visible()) {
  488. button_favorite->show();
  489. _update_tree();
  490. }
  491. if (file_list_vb->is_visible()) {
  492. _update_files(true);
  493. }
  494. set_process(false);
  495. }
  496. void FileSystemDock::_set_scanning_mode() {
  497. split_box->hide();
  498. button_hist_prev->set_disabled(true);
  499. button_hist_next->set_disabled(true);
  500. scanning_vb->show();
  501. set_process(true);
  502. if (EditorFileSystem::get_singleton()->is_scanning()) {
  503. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress()*100);
  504. } else {
  505. scanning_progress->set_value(0);
  506. }
  507. }
  508. void FileSystemDock::_fw_history() {
  509. if (history_pos<history.size()-1)
  510. history_pos++;
  511. path=history[history_pos];
  512. if (tree->is_visible()) {
  513. _update_tree();
  514. tree->grab_focus();
  515. tree->ensure_cursor_is_visible();
  516. }
  517. if (file_list_vb->is_visible()) {
  518. _update_files(false);
  519. current_path->set_text(path);
  520. }
  521. button_hist_prev->set_disabled(history_pos==0);
  522. button_hist_next->set_disabled(history_pos+1==history.size());
  523. }
  524. void FileSystemDock::_bw_history() {
  525. if (history_pos>0)
  526. history_pos--;
  527. path=history[history_pos];
  528. if (tree->is_visible()) {
  529. _update_tree();
  530. tree->grab_focus();
  531. tree->ensure_cursor_is_visible();
  532. }
  533. if (file_list_vb->is_visible()) {
  534. _update_files(false);
  535. current_path->set_text(path);
  536. }
  537. button_hist_prev->set_disabled(history_pos==0);
  538. button_hist_next->set_disabled(history_pos+1==history.size());
  539. }
  540. void FileSystemDock::_push_to_history() {
  541. history.resize(history_pos+1);
  542. if (history[history_pos]!=path) {
  543. history.push_back(path);
  544. history_pos++;
  545. }
  546. button_hist_prev->set_disabled(history_pos==0);
  547. button_hist_next->set_disabled(history_pos+1==history.size());
  548. }
  549. void FileSystemDock::_find_inside_move_files(EditorFileSystemDirectory *efsd,Vector<String>& files) {
  550. for(int i=0;i<efsd->get_subdir_count();i++) {
  551. _find_inside_move_files(efsd->get_subdir(i),files);
  552. }
  553. for(int i=0;i<efsd->get_file_count();i++) {
  554. files.push_back(efsd->get_file_path(i));
  555. }
  556. }
  557. void FileSystemDock::_find_remaps(EditorFileSystemDirectory *efsd,Map<String,String> &renames,List<String>& to_remaps) {
  558. for(int i=0;i<efsd->get_subdir_count();i++) {
  559. _find_remaps(efsd->get_subdir(i),renames,to_remaps);
  560. }
  561. for(int i=0;i<efsd->get_file_count();i++) {
  562. Vector<String> deps=efsd->get_file_deps(i);
  563. for(int j=0;j<deps.size();j++) {
  564. if (renames.has(deps[j])) {
  565. to_remaps.push_back(efsd->get_file_path(i));
  566. break;
  567. }
  568. }
  569. }
  570. }
  571. void FileSystemDock::_rename_operation(const String& p_to_path) {
  572. if (move_files[0]==p_to_path) {
  573. EditorNode::get_singleton()->show_warning(TTR("Same source and destination files, doing nothing."));
  574. return;
  575. }
  576. if (FileAccess::exists(p_to_path)) {
  577. EditorNode::get_singleton()->show_warning("Target file exists, can't overwrite. Delete first.");
  578. return;
  579. }
  580. Map<String,String> renames;
  581. renames[move_files[0]]=p_to_path;
  582. List<String> remap;
  583. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(),renames,remap);
  584. print_line("found files to remap: "+itos(remap.size()));
  585. //perform remaps
  586. for(List<String>::Element *E=remap.front();E;E=E->next()) {
  587. Error err = ResourceLoader::rename_dependencies(E->get(),renames);
  588. print_line("remapping: "+E->get());
  589. if (err!=OK) {
  590. EditorNode::get_singleton()->add_io_error("Can't rename deps for:\n"+E->get()+"\n");
  591. }
  592. }
  593. //finally, perform moves
  594. DirAccess *da=DirAccess::create(DirAccess::ACCESS_RESOURCES);
  595. Error err = da->rename(move_files[0],p_to_path);
  596. print_line("moving file "+move_files[0]+" to "+p_to_path);
  597. if (err!=OK) {
  598. EditorNode::get_singleton()->add_io_error("Error moving file:\n"+move_files[0]+"\n");
  599. }
  600. //rescan everything
  601. memdelete(da);
  602. print_line("call rescan!");
  603. _rescan();
  604. }
  605. void FileSystemDock::_move_operation(const String& p_to_path) {
  606. if (p_to_path==path) {
  607. EditorNode::get_singleton()->show_warning(TTR("Same source and destination paths, doing nothing."));
  608. return;
  609. }
  610. //find files inside dirs to be moved
  611. Vector<String> inside_files;
  612. for(int i=0;i<move_dirs.size();i++) {
  613. if (p_to_path.begins_with(move_dirs[i])) {
  614. EditorNode::get_singleton()->show_warning(TTR("Can't move directories to within themselves."));
  615. return;
  616. }
  617. EditorFileSystemDirectory *efsd=EditorFileSystem::get_singleton()->get_filesystem_path(move_dirs[i]);
  618. if (!efsd)
  619. continue;
  620. _find_inside_move_files(efsd,inside_files);
  621. }
  622. //make list of remaps
  623. Map<String,String> renames;
  624. String repfrom=path=="res://"?path:String(path+"/");
  625. String repto=p_to_path=="res://"?p_to_path:String(p_to_path+"/");
  626. for(int i=0;i<move_files.size();i++) {
  627. renames[move_files[i]]=move_files[i].replace_first(repfrom,repto);
  628. print_line("move file "+move_files[i]+" -> "+renames[move_files[i]]);
  629. }
  630. for(int i=0;i<inside_files.size();i++) {
  631. renames[inside_files[i]]=inside_files[i].replace_first(repfrom,repto);
  632. print_line("inside file "+inside_files[i]+" -> "+renames[inside_files[i]]);
  633. }
  634. //make list of files that will be run the remapping
  635. List<String> remap;
  636. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(),renames,remap);
  637. print_line("found files to remap: "+itos(remap.size()));
  638. //perform remaps
  639. for(List<String>::Element *E=remap.front();E;E=E->next()) {
  640. Error err = ResourceLoader::rename_dependencies(E->get(),renames);
  641. print_line("remapping: "+E->get());
  642. if (err!=OK) {
  643. EditorNode::get_singleton()->add_io_error("Can't rename deps for:\n"+E->get()+"\n");
  644. }
  645. }
  646. //finally, perform moves
  647. DirAccess *da=DirAccess::create(DirAccess::ACCESS_RESOURCES);
  648. for(int i=0;i<move_files.size();i++) {
  649. String to = move_files[i].replace_first(repfrom,repto);
  650. Error err = da->rename(move_files[i],to);
  651. print_line("moving file "+move_files[i]+" to "+to);
  652. if (err!=OK) {
  653. EditorNode::get_singleton()->add_io_error("Error moving file:\n"+move_files[i]+"\n");
  654. }
  655. }
  656. for(int i=0;i<move_dirs.size();i++) {
  657. String mdir = move_dirs[i];
  658. if (mdir=="res://")
  659. continue;
  660. if (mdir.ends_with("/")) {
  661. mdir=mdir.substr(0,mdir.length()-1);
  662. }
  663. String to = p_to_path.plus_file(mdir.get_file());
  664. Error err = da->rename(mdir,to);
  665. print_line("moving dir "+mdir+" to "+to);
  666. if (err!=OK) {
  667. EditorNode::get_singleton()->add_io_error("Error moving dir:\n"+move_dirs[i]+"\n");
  668. }
  669. }
  670. memdelete(da);
  671. //rescan everything
  672. print_line("call rescan!");
  673. _rescan();
  674. }
  675. void FileSystemDock::_file_option(int p_option) {
  676. switch(p_option) {
  677. case FILE_SHOW_IN_EXPLORER:
  678. case FILE_OPEN: {
  679. int idx=-1;
  680. for(int i=0;i<files->get_item_count();i++) {
  681. if (files->is_selected(i)) {
  682. idx=i;
  683. break;
  684. }
  685. }
  686. if (idx<0)
  687. return;
  688. String path = files->get_item_metadata(idx);
  689. if (p_option == FILE_SHOW_IN_EXPLORER) {
  690. String dir = GlobalConfig::get_singleton()->globalize_path(path);
  691. dir = dir.substr(0, dir.find_last("/"));
  692. OS::get_singleton()->shell_open(String("file://")+dir);
  693. return;
  694. }
  695. if (path.ends_with("/")) {
  696. if (path!="res://") {
  697. path=path.substr(0,path.length()-1);
  698. }
  699. this->path=path;
  700. _update_files(false);
  701. current_path->set_text(path);
  702. _push_to_history();
  703. } else {
  704. if (ResourceLoader::get_resource_type(path)=="PackedScene") {
  705. editor->open_request(path);
  706. } else {
  707. editor->load_resource(path);
  708. }
  709. }
  710. } break;
  711. case FILE_INSTANCE: {
  712. Vector<String> paths;
  713. for (int i = 0; i<files->get_item_count(); i++) {
  714. if (!files->is_selected(i))
  715. continue;
  716. String path =files->get_item_metadata(i);
  717. if (EditorFileSystem::get_singleton()->get_file_type(path)=="PackedScene") {
  718. paths.push_back(path);
  719. }
  720. }
  721. if (!paths.empty()) {
  722. emit_signal("instance", paths);
  723. }
  724. } break;
  725. case FILE_DEPENDENCIES: {
  726. int idx = files->get_current();
  727. if (idx<0 || idx>=files->get_item_count())
  728. break;
  729. String path = files->get_item_metadata(idx);
  730. deps_editor->edit(path);
  731. } break;
  732. case FILE_OWNERS: {
  733. int idx = files->get_current();
  734. if (idx<0 || idx>=files->get_item_count())
  735. break;
  736. String path = files->get_item_metadata(idx);
  737. owners_editor->show(path);
  738. } break;
  739. case FILE_MOVE: {
  740. move_dirs.clear();
  741. move_files.clear();
  742. for(int i=0;i<files->get_item_count();i++) {
  743. String path = files->get_item_metadata(i);
  744. if (!files->is_selected(i))
  745. continue;
  746. if (files->get_item_text(i)=="..") {
  747. EditorNode::get_singleton()->show_warning(TTR("Can't operate on '..'"));
  748. return;
  749. }
  750. if (path.ends_with("/")) {
  751. move_dirs.push_back(path.substr(0,path.length()-1));
  752. } else {
  753. move_files.push_back(path);
  754. }
  755. }
  756. if (move_dirs.empty() && move_files.size()==1) {
  757. rename_dialog->clear_filters();
  758. rename_dialog->add_filter("*."+move_files[0].get_extension());
  759. rename_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  760. rename_dialog->set_current_path(move_files[0]);
  761. rename_dialog->popup_centered_ratio();
  762. rename_dialog->set_title(TTR("Pick New Name and Location For:")+" "+move_files[0].get_file());
  763. } else {
  764. //just move
  765. move_dialog->popup_centered_ratio();
  766. }
  767. } break;
  768. case FILE_REMOVE: {
  769. Vector<String> torem;
  770. for(int i=0;i<files->get_item_count();i++) {
  771. String path = files->get_item_metadata(i);
  772. if (path.ends_with("/") || !files->is_selected(i))
  773. continue;
  774. torem.push_back(path);
  775. }
  776. if (torem.empty()) {
  777. EditorNode::get_singleton()->show_warning(TTR("No files selected!"));
  778. break;
  779. }
  780. remove_dialog->show(torem);
  781. //1) find if used
  782. //2) warn
  783. } break;
  784. case FILE_INFO: {
  785. } break;
  786. case FILE_REIMPORT: {
  787. Vector<String> reimport;
  788. for(int i=0;i<files->get_item_count();i++) {
  789. if (!files->is_selected(i))
  790. continue;
  791. String path = files->get_item_metadata(i);
  792. reimport.push_back(path);
  793. }
  794. ERR_FAIL_COND(reimport.size()==0);
  795. /*
  796. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(reimport[0]);
  797. ERR_FAIL_COND(!rimd.is_valid());
  798. String editor=rimd->get_editor();
  799. if (editor.begins_with("texture_")) { //compatibility fix for old texture format
  800. editor="texture";
  801. }
  802. Ref<EditorImportPlugin> rimp = EditorImportExport::get_singleton()->get_import_plugin_by_name(editor);
  803. ERR_FAIL_COND(!rimp.is_valid());
  804. if (reimport.size()==1) {
  805. rimp->import_dialog(reimport[0]);
  806. } else {
  807. rimp->reimport_multiple_files(reimport);
  808. }
  809. */
  810. } break;
  811. case FILE_COPY_PATH:
  812. int idx = files->get_current();
  813. if (idx<0 || idx>=files->get_item_count())
  814. break;
  815. String path = files->get_item_metadata(idx);
  816. OS::get_singleton()->set_clipboard(path);
  817. }
  818. }
  819. void FileSystemDock::_folder_option(int p_option) {
  820. TreeItem *item = tree->get_selected();
  821. TreeItem *child = item->get_children();
  822. switch(p_option) {
  823. case FOLDER_EXPAND_ALL:
  824. item->set_collapsed(false);
  825. while(child) {
  826. child->set_collapsed(false);
  827. child = child->get_next();
  828. }
  829. break;
  830. case FOLDER_COLLAPSE_ALL:
  831. while(child) {
  832. child->set_collapsed(true);
  833. child = child->get_next();
  834. }
  835. break;
  836. }
  837. }
  838. void FileSystemDock::_open_pressed(){
  839. TreeItem *sel = tree->get_selected();
  840. if (!sel) {
  841. return;
  842. }
  843. path = sel->get_metadata(0);
  844. /*if (path!="res://" && path.ends_with("/")) {
  845. path=path.substr(0,path.length()-1);
  846. }*/
  847. //tree_mode=false;
  848. if (split_mode) {
  849. tree->hide();
  850. file_list_vb->show();
  851. button_favorite->hide();
  852. }
  853. //file_options->show();
  854. _update_files(false);
  855. current_path->set_text(path);
  856. _push_to_history();
  857. //emit_signal("open",path);
  858. }
  859. void FileSystemDock::_dir_rmb_pressed(const Vector2& p_pos) {
  860. folder_options->clear();
  861. folder_options->set_size(Size2(1,1));
  862. folder_options->add_item(TTR("Expand all"),FOLDER_EXPAND_ALL);
  863. folder_options->add_item(TTR("Collapse all"),FOLDER_COLLAPSE_ALL);
  864. folder_options->set_pos(files->get_global_pos() + p_pos);
  865. folder_options->popup();
  866. }
  867. void FileSystemDock::_search_changed(const String& p_text) {
  868. if (!search_box->is_visible_in_tree())
  869. return; //wtf
  870. _update_files(false);
  871. }
  872. void FileSystemDock::_rescan() {
  873. _set_scanning_mode();
  874. EditorFileSystem::get_singleton()->scan();
  875. }
  876. void FileSystemDock::fix_dependencies(const String& p_for_file) {
  877. deps_editor->edit(p_for_file);
  878. }
  879. void FileSystemDock::focus_on_filter() {
  880. if (!search_box->is_visible_in_tree()) {
  881. // Tree mode, switch to files list with search box
  882. tree->hide();
  883. file_list_vb->show();
  884. button_favorite->hide();
  885. }
  886. search_box->grab_focus();
  887. }
  888. void FileSystemDock::set_display_mode(int p_mode) {
  889. if (p_mode == display_mode)
  890. return;
  891. button_display_mode->set_pressed(p_mode == DISPLAY_LIST);
  892. _change_file_display();
  893. }
  894. Variant FileSystemDock::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  895. if (p_from==tree) {
  896. TreeItem *selected = tree->get_selected();
  897. if (!selected)
  898. return Variant();
  899. String path = selected->get_metadata(0);
  900. if (path==String())
  901. return Variant();
  902. if (!path.ends_with("/"))
  903. path=path+"/";
  904. Vector<String> paths;
  905. paths.push_back(path);
  906. Dictionary d = EditorNode::get_singleton()->drag_files(paths,p_from);
  907. if (selected->get_parent() && tree->get_root()->get_children()==selected->get_parent()) {
  908. //a favorite.. treat as such
  909. d["type"]="favorite";
  910. }
  911. return d;
  912. }
  913. if (p_from==files) {
  914. List<int> seldirs;
  915. List<int> selfiles;
  916. for (int i = 0; i<files->get_item_count(); i++) {
  917. if (files->is_selected(i)) {
  918. String path = files->get_item_metadata(i);
  919. if (path.ends_with("/"))
  920. seldirs.push_back(i);
  921. else
  922. selfiles.push_back(i);
  923. }
  924. }
  925. if (seldirs.empty() && selfiles.empty())
  926. return Variant();
  927. /*
  928. if (seldirs.size() && selfiles.size())
  929. return Variant(); //can't really mix files and dirs (i think?) - yes you can, commenting
  930. */
  931. /*if (selfiles.size()==1) {
  932. Ref<Resource> resource = ResourceLoader::load(files->get_item_metadata(selfiles.front()->get()));
  933. if (resource.is_valid()) {
  934. return EditorNode::get_singleton()->drag_resource(resource,p_from);
  935. }
  936. }*/
  937. if (selfiles.size()>0 && seldirs.size()==0) {
  938. Vector<String> fnames;
  939. for(List<int>::Element *E=selfiles.front();E;E=E->next()) {
  940. fnames.push_back(files->get_item_metadata(E->get()));
  941. }
  942. return EditorNode::get_singleton()->drag_files(fnames,p_from);
  943. }
  944. if (selfiles.size()>0 || seldirs.size()>0) {
  945. Vector<String> fnames;
  946. for(List<int>::Element *E=selfiles.front();E;E=E->next()) {
  947. fnames.push_back(files->get_item_metadata(E->get()));
  948. }
  949. for(List<int>::Element *E=seldirs.front();E;E=E->next()) {
  950. fnames.push_back(files->get_item_metadata(E->get()));
  951. }
  952. return EditorNode::get_singleton()->drag_files_and_dirs(fnames,p_from);
  953. }
  954. }
  955. return Variant();
  956. }
  957. bool FileSystemDock::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{
  958. Dictionary drag_data = p_data;
  959. if (drag_data.has("type") && String(drag_data["type"])=="favorite") {
  960. //moving favorite around
  961. TreeItem *ti = tree->get_item_at_pos(p_point);
  962. if (!ti)
  963. return false;
  964. int what = tree->get_drop_section_at_pos(p_point);
  965. if (ti==tree->get_root()->get_children()) {
  966. return (what==1); //the parent, first fav
  967. }
  968. if (ti->get_parent() && tree->get_root()->get_children()==ti->get_parent()) {
  969. return true; // a favorite
  970. }
  971. if (ti==tree->get_root()->get_children()->get_next()) {
  972. return (what==-1); //the tree, last fav
  973. }
  974. return false;
  975. }
  976. if (drag_data.has("type") && String(drag_data["type"])=="resource") {
  977. return true;
  978. }
  979. if (drag_data.has("type") && ( String(drag_data["type"])=="files" || String(drag_data["type"])=="files_and_dirs")) {
  980. Vector<String> fnames = drag_data["files"];
  981. if (p_from==files) {
  982. int at_pos = files->get_item_at_pos(p_point);
  983. if (at_pos!=-1) {
  984. String dir = files->get_item_metadata(at_pos);
  985. if (dir.ends_with("/"))
  986. return true;
  987. }
  988. }
  989. if (p_from==tree) {
  990. TreeItem *ti = tree->get_item_at_pos(p_point);
  991. if (!ti)
  992. return false;
  993. String path = ti->get_metadata(0);
  994. if (path==String())
  995. return false;
  996. return true;
  997. }
  998. }
  999. return false;
  1000. }
  1001. void FileSystemDock::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){
  1002. if (!can_drop_data_fw(p_point,p_data,p_from))
  1003. return;
  1004. Dictionary drag_data = p_data;
  1005. if (drag_data.has("type") && String(drag_data["type"])=="favorite") {
  1006. //moving favorite around
  1007. TreeItem *ti = tree->get_item_at_pos(p_point);
  1008. if (!ti)
  1009. return;
  1010. Vector<String> files = drag_data["files"];
  1011. ERR_FAIL_COND(files.size()!=1);
  1012. String swap = files[0];
  1013. if (swap!="res://" && swap.ends_with("/")) {
  1014. swap=swap.substr(0,swap.length()-1);
  1015. }
  1016. int what = tree->get_drop_section_at_pos(p_point);
  1017. TreeItem *swap_item=NULL;
  1018. if (ti==tree->get_root()->get_children()) {
  1019. swap_item=tree->get_root()->get_children()->get_children();
  1020. } else if (ti->get_parent() && tree->get_root()->get_children()==ti->get_parent()) {
  1021. if (what==-1) {
  1022. swap_item=ti;
  1023. } else {
  1024. swap_item=ti->get_next();
  1025. }
  1026. }
  1027. String swap_with;
  1028. if (swap_item) {
  1029. swap_with=swap_item->get_metadata(0);
  1030. if (swap_with!="res://" && swap_with.ends_with("/")) {
  1031. swap_with=swap_with.substr(0,swap_with.length()-1);
  1032. }
  1033. }
  1034. if (swap==swap_with)
  1035. return;
  1036. Vector<String> dirs = EditorSettings::get_singleton()->get_favorite_dirs();
  1037. ERR_FAIL_COND(dirs.find(swap)==-1);
  1038. ERR_FAIL_COND(swap_with!=String() && dirs.find(swap_with)==-1);
  1039. dirs.erase(swap);
  1040. if (swap_with==String()) {
  1041. dirs.push_back(swap);
  1042. } else {
  1043. int idx = dirs.find(swap_with);
  1044. dirs.insert(idx,swap);
  1045. }
  1046. EditorSettings::get_singleton()->set_favorite_dirs(dirs);
  1047. _update_tree();
  1048. return;
  1049. }
  1050. if (drag_data.has("type") && String(drag_data["type"])=="resource") {
  1051. Ref<Resource> res = drag_data["resource"];
  1052. if (!res.is_valid()) {
  1053. return;
  1054. }
  1055. if (p_from==tree) {
  1056. TreeItem *ti = tree->get_item_at_pos(p_point);
  1057. if (!ti)
  1058. return;
  1059. String path = ti->get_metadata(0);
  1060. if (path==String())
  1061. return;
  1062. EditorNode::get_singleton()->save_resource_as(res,path);
  1063. return;
  1064. }
  1065. if (p_from==files) {
  1066. String save_path=path;
  1067. int at_pos = files->get_item_at_pos(p_point);
  1068. if (at_pos!=-1) {
  1069. String to_dir = files->get_item_metadata(at_pos);
  1070. if (to_dir.ends_with("/")) {
  1071. save_path=to_dir;
  1072. if (save_path!="res://")
  1073. save_path=save_path.substr(0,save_path.length()-1);
  1074. }
  1075. }
  1076. EditorNode::get_singleton()->save_resource_as(res,save_path);
  1077. return;
  1078. }
  1079. }
  1080. if (drag_data.has("type") && ( String(drag_data["type"])=="files" || String(drag_data["type"])=="files_and_dirs")) {
  1081. if (p_from==files || p_from==tree) {
  1082. String to_dir;
  1083. if (p_from==files) {
  1084. int at_pos = files->get_item_at_pos(p_point);
  1085. ERR_FAIL_COND(at_pos==-1);
  1086. to_dir = files->get_item_metadata(at_pos);
  1087. } else {
  1088. TreeItem *ti = tree->get_item_at_pos(p_point);
  1089. if (!ti)
  1090. return;
  1091. to_dir = ti->get_metadata(0);
  1092. ERR_FAIL_COND(to_dir==String());
  1093. }
  1094. if (to_dir!="res://" && to_dir.ends_with("/")) {
  1095. to_dir=to_dir.substr(0,to_dir.length()-1);
  1096. }
  1097. Vector<String> fnames = drag_data["files"];
  1098. move_files.clear();
  1099. move_dirs.clear();
  1100. for(int i=0;i<fnames.size();i++) {
  1101. if (fnames[i].ends_with("/"))
  1102. move_dirs.push_back(fnames[i]);
  1103. else
  1104. move_files.push_back(fnames[i]);
  1105. }
  1106. _move_operation(to_dir);
  1107. }
  1108. }
  1109. }
  1110. void FileSystemDock::_files_list_rmb_select(int p_item,const Vector2& p_pos) {
  1111. Vector<String> filenames;
  1112. bool all_scenes=true;
  1113. bool all_can_reimport=true;
  1114. Set<String> types;
  1115. for(int i=0;i<files->get_item_count();i++) {
  1116. if (!files->is_selected(i))
  1117. continue;
  1118. String path = files->get_item_metadata(i);
  1119. if (files->get_item_text(i)=="..") {
  1120. // no operate on ..
  1121. return;
  1122. }
  1123. if (path.ends_with("/")) {
  1124. //no operate on dirs
  1125. return;
  1126. }
  1127. EditorFileSystemDirectory *efsd=NULL;
  1128. int pos;
  1129. efsd = EditorFileSystem::get_singleton()->find_file(path,&pos);
  1130. if (efsd) {
  1131. } else {
  1132. all_can_reimport=false;
  1133. }
  1134. filenames.push_back(path);
  1135. if (EditorFileSystem::get_singleton()->get_file_type(path)!="PackedScene")
  1136. all_scenes=false;
  1137. }
  1138. if (filenames.size()==0)
  1139. return;
  1140. file_options->clear();
  1141. file_options->set_size(Size2(1,1));
  1142. file_options->add_item(TTR("Open"),FILE_OPEN);
  1143. if (all_scenes) {
  1144. file_options->add_item(TTR("Instance"),FILE_INSTANCE);
  1145. }
  1146. file_options->add_separator();
  1147. if (filenames.size()==1) {
  1148. file_options->add_item(TTR("Edit Dependencies.."),FILE_DEPENDENCIES);
  1149. file_options->add_item(TTR("View Owners.."),FILE_OWNERS);
  1150. file_options->add_separator();
  1151. }
  1152. if (filenames.size()==1) {
  1153. file_options->add_item(TTR("Copy Path"), FILE_COPY_PATH);
  1154. file_options->add_item(TTR("Rename or Move.."),FILE_MOVE);
  1155. } else {
  1156. file_options->add_item(TTR("Move To.."),FILE_MOVE);
  1157. }
  1158. file_options->add_item(TTR("Delete"),FILE_REMOVE);
  1159. //file_options->add_item(TTR("Info"),FILE_INFO);
  1160. file_options->add_separator();
  1161. file_options->add_item(TTR("Show In File Manager"),FILE_SHOW_IN_EXPLORER);
  1162. if (all_can_reimport && types.size()==1) { //all can reimport and are of the same type
  1163. /*
  1164. bool valid=true;
  1165. Ref<EditorImportPlugin> rimp = EditorImportExport::get_singleton()->get_import_plugin_by_name(types.front()->get());
  1166. if (rimp.is_valid()) {
  1167. if (filenames.size()>1 && !rimp->can_reimport_multiple_files()) {
  1168. valid=false;
  1169. }
  1170. } else {
  1171. valid=false;
  1172. }
  1173. if (valid) {
  1174. file_options->add_separator();
  1175. file_options->add_item(TTR("Re-Import.."),FILE_REIMPORT);
  1176. }
  1177. */
  1178. }
  1179. file_options->set_pos(files->get_global_pos() + p_pos);
  1180. file_options->popup();
  1181. }
  1182. void FileSystemDock::select_file(const String& p_file) {
  1183. _go_to_dir(p_file.get_base_dir());
  1184. for(int i=0;i<files->get_item_count();i++) {
  1185. if (files->get_item_metadata(i)==p_file) {
  1186. files->select(i);
  1187. files->ensure_current_is_visible();
  1188. break;
  1189. }
  1190. }
  1191. }
  1192. void FileSystemDock::_file_multi_selected(int p_index,bool p_selected) {
  1193. _file_selected();
  1194. }
  1195. void FileSystemDock::_file_selected() {
  1196. //check import
  1197. Vector<String> imports;
  1198. String import_type;
  1199. for(int i=0;i<files->get_item_count();i++) {
  1200. if (!files->is_selected(i))
  1201. continue;
  1202. String p = files->get_item_metadata(i);
  1203. if (!FileAccess::exists(p+".import")) {
  1204. imports.clear();
  1205. break;
  1206. }
  1207. Ref<ConfigFile> cf;
  1208. cf.instance();
  1209. Error err = cf->load(p+".import");
  1210. if (err!=OK) {
  1211. imports.clear();
  1212. break;
  1213. }
  1214. String type = cf->get_value("remap","type");
  1215. if (import_type=="") {
  1216. import_type=type;
  1217. } else if (import_type!=type) {
  1218. //all should be the same type
  1219. imports.clear();
  1220. break;
  1221. }
  1222. imports.push_back(p);
  1223. }
  1224. if (imports.size()==0) {
  1225. EditorNode::get_singleton()->get_import_dock()->clear();
  1226. } else if (imports.size()==1) {
  1227. EditorNode::get_singleton()->get_import_dock()->set_edit_path(imports[0]);
  1228. } else {
  1229. EditorNode::get_singleton()->get_import_dock()->set_edit_multiple_paths(imports);
  1230. }
  1231. }
  1232. void FileSystemDock::_bind_methods() {
  1233. ClassDB::bind_method(D_METHOD("_update_tree"),&FileSystemDock::_update_tree);
  1234. ClassDB::bind_method(D_METHOD("_rescan"),&FileSystemDock::_rescan);
  1235. ClassDB::bind_method(D_METHOD("_favorites_pressed"),&FileSystemDock::_favorites_pressed);
  1236. //ClassDB::bind_method(D_METHOD("_instance_pressed"),&ScenesDock::_instance_pressed);
  1237. ClassDB::bind_method(D_METHOD("_open_pressed"),&FileSystemDock::_open_pressed);
  1238. ClassDB::bind_method(D_METHOD("_dir_rmb_pressed"),&FileSystemDock::_dir_rmb_pressed);
  1239. ClassDB::bind_method(D_METHOD("_thumbnail_done"),&FileSystemDock::_thumbnail_done);
  1240. ClassDB::bind_method(D_METHOD("_select_file"), &FileSystemDock::_select_file);
  1241. ClassDB::bind_method(D_METHOD("_go_to_tree"), &FileSystemDock::_go_to_tree);
  1242. ClassDB::bind_method(D_METHOD("_go_to_dir"), &FileSystemDock::_go_to_dir);
  1243. ClassDB::bind_method(D_METHOD("_change_file_display"), &FileSystemDock::_change_file_display);
  1244. ClassDB::bind_method(D_METHOD("_fw_history"), &FileSystemDock::_fw_history);
  1245. ClassDB::bind_method(D_METHOD("_bw_history"), &FileSystemDock::_bw_history);
  1246. ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed);
  1247. ClassDB::bind_method(D_METHOD("_dir_selected"), &FileSystemDock::_dir_selected);
  1248. ClassDB::bind_method(D_METHOD("_file_option"), &FileSystemDock::_file_option);
  1249. ClassDB::bind_method(D_METHOD("_folder_option"), &FileSystemDock::_folder_option);
  1250. ClassDB::bind_method(D_METHOD("_move_operation"), &FileSystemDock::_move_operation);
  1251. ClassDB::bind_method(D_METHOD("_rename_operation"), &FileSystemDock::_rename_operation);
  1252. ClassDB::bind_method(D_METHOD("_search_changed"), &FileSystemDock::_search_changed);
  1253. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &FileSystemDock::get_drag_data_fw);
  1254. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &FileSystemDock::can_drop_data_fw);
  1255. ClassDB::bind_method(D_METHOD("drop_data_fw"), &FileSystemDock::drop_data_fw);
  1256. ClassDB::bind_method(D_METHOD("_files_list_rmb_select"),&FileSystemDock::_files_list_rmb_select);
  1257. ClassDB::bind_method(D_METHOD("_preview_invalidated"),&FileSystemDock::_preview_invalidated);
  1258. ClassDB::bind_method(D_METHOD("_file_selected"),&FileSystemDock::_file_selected);
  1259. ClassDB::bind_method(D_METHOD("_file_multi_selected"),&FileSystemDock::_file_multi_selected);
  1260. ADD_SIGNAL(MethodInfo("instance", PropertyInfo(Variant::POOL_STRING_ARRAY, "files")));
  1261. ADD_SIGNAL(MethodInfo("open"));
  1262. }
  1263. FileSystemDock::FileSystemDock(EditorNode *p_editor) {
  1264. editor=p_editor;
  1265. HBoxContainer *toolbar_hbc = memnew( HBoxContainer );
  1266. add_child(toolbar_hbc);
  1267. button_hist_prev = memnew( ToolButton );
  1268. toolbar_hbc->add_child(button_hist_prev);
  1269. button_hist_prev->set_disabled(true);
  1270. button_hist_prev->set_tooltip(TTR("Previous Directory"));
  1271. button_hist_next = memnew( ToolButton );
  1272. toolbar_hbc->add_child(button_hist_next);
  1273. button_hist_next->set_disabled(true);
  1274. button_hist_prev->set_focus_mode(FOCUS_NONE);
  1275. button_hist_next->set_focus_mode(FOCUS_NONE);
  1276. button_hist_next->set_tooltip(TTR("Next Directory"));
  1277. current_path=memnew( LineEdit );
  1278. current_path->set_h_size_flags(SIZE_EXPAND_FILL);
  1279. toolbar_hbc->add_child(current_path);
  1280. button_reload = memnew( Button );
  1281. button_reload->set_flat(true);
  1282. button_reload->connect("pressed",this,"_rescan");
  1283. toolbar_hbc->add_child(button_reload);
  1284. button_reload->set_focus_mode(FOCUS_NONE);
  1285. button_reload->set_tooltip(TTR("Re-Scan Filesystem"));
  1286. button_reload->hide();
  1287. //toolbar_hbc->add_spacer();
  1288. button_favorite = memnew( Button );
  1289. button_favorite->set_flat(true);
  1290. button_favorite->set_toggle_mode(true);
  1291. button_favorite->connect("pressed",this,"_favorites_pressed");
  1292. toolbar_hbc->add_child(button_favorite);
  1293. button_favorite->set_tooltip(TTR("Toggle folder status as Favorite"));
  1294. button_favorite->set_focus_mode(FOCUS_NONE);
  1295. //Control *spacer = memnew( Control);
  1296. /*
  1297. button_open = memnew( Button );
  1298. button_open->set_flat(true);
  1299. button_open->connect("pressed",this,"_open_pressed");
  1300. toolbar_hbc->add_child(button_open);
  1301. button_open->hide();
  1302. button_open->set_focus_mode(FOCUS_NONE);
  1303. button_open->set_tooltip("Open the selected file.\nOpen as scene if a scene, or as resource otherwise.");
  1304. button_instance = memnew( Button );
  1305. button_instance->set_flat(true);
  1306. button_instance->connect("pressed",this,"_instance_pressed");
  1307. toolbar_hbc->add_child(button_instance);
  1308. button_instance->hide();
  1309. button_instance->set_focus_mode(FOCUS_NONE);
  1310. button_instance->set_tooltip(TTR("Instance the selected scene(s) as child of the selected node."));
  1311. */
  1312. file_options = memnew( PopupMenu );
  1313. add_child(file_options);
  1314. folder_options = memnew ( PopupMenu );
  1315. add_child(folder_options);
  1316. split_box = memnew( VSplitContainer );
  1317. add_child(split_box);
  1318. split_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1319. tree = memnew( Tree );
  1320. tree->set_hide_root(true);
  1321. split_box->add_child(tree);
  1322. tree->set_drag_forwarding(this);
  1323. tree->set_allow_rmb_select(true);
  1324. //tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1325. tree->connect("item_edited",this,"_favorite_toggled");
  1326. tree->connect("item_activated",this,"_open_pressed");
  1327. tree->connect("cell_selected",this,"_dir_selected");
  1328. tree->connect("item_rmb_selected",this,"_dir_rmb_pressed");
  1329. files = memnew( ItemList );
  1330. files->set_v_size_flags(SIZE_EXPAND_FILL);
  1331. files->set_select_mode(ItemList::SELECT_MULTI);
  1332. files->set_drag_forwarding(this);
  1333. files->connect("item_rmb_selected",this,"_files_list_rmb_select");
  1334. files->connect("item_selected",this,"_file_selected");
  1335. files->connect("multi_selected",this,"_file_multi_selected");
  1336. files->set_allow_rmb_select(true);
  1337. file_list_vb = memnew( VBoxContainer );
  1338. split_box->add_child(file_list_vb);
  1339. file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1340. path_hb = memnew( HBoxContainer );
  1341. file_list_vb->add_child(path_hb);
  1342. button_back = memnew( ToolButton );
  1343. path_hb->add_child(button_back);
  1344. button_back->hide();
  1345. search_box = memnew( LineEdit );
  1346. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1347. path_hb->add_child(search_box);
  1348. search_box->connect("text_changed",this,"_search_changed");
  1349. search_icon = memnew( TextureRect );
  1350. search_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  1351. path_hb->add_child(search_icon);
  1352. button_display_mode = memnew( ToolButton );
  1353. path_hb->add_child(button_display_mode);
  1354. button_display_mode->set_toggle_mode(true);
  1355. file_list_vb->add_child(files);
  1356. scanning_vb = memnew( VBoxContainer );
  1357. Label *slabel = memnew( Label );
  1358. slabel->set_text("Scanning Files,\nPlease Wait..");
  1359. slabel->set_align(Label::ALIGN_CENTER);
  1360. scanning_vb->add_child(slabel);
  1361. scanning_progress = memnew( ProgressBar );
  1362. scanning_vb->add_child(scanning_progress);
  1363. add_child(scanning_vb);
  1364. scanning_vb->hide();
  1365. deps_editor = memnew( DependencyEditor );
  1366. add_child(deps_editor);
  1367. owners_editor = memnew( DependencyEditorOwners);
  1368. add_child(owners_editor);
  1369. remove_dialog = memnew( DependencyRemoveDialog);
  1370. add_child(remove_dialog);
  1371. move_dialog = memnew( EditorDirDialog );
  1372. add_child(move_dialog);
  1373. move_dialog->connect("dir_selected",this,"_move_operation");
  1374. move_dialog->get_ok()->set_text(TTR("Move"));
  1375. rename_dialog = memnew( EditorFileDialog );
  1376. rename_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1377. rename_dialog->connect("file_selected",this,"_rename_operation");
  1378. add_child(rename_dialog);
  1379. updating_tree=false;
  1380. initialized=false;
  1381. history.push_back("res://");
  1382. history_pos=0;
  1383. split_mode=false;
  1384. display_mode = DISPLAY_THUMBNAILS;
  1385. path="res://";
  1386. add_constant_override("separation",3);
  1387. }
  1388. FileSystemDock::~FileSystemDock() {
  1389. }