filesystem_dock.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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-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 "globals.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. item->set_text(0,dname);
  44. item->set_icon(0,get_icon("Folder","EditorIcons"));
  45. item->set_selectable(0,true);
  46. String lpath = p_dir->get_path();
  47. if (lpath!="res://" && lpath.ends_with("/")) {
  48. lpath=lpath.substr(0,lpath.length()-1);
  49. }
  50. item->set_metadata(0,lpath);
  51. if (lpath==path) {
  52. item->select(0);
  53. }
  54. for(int i=0;i<p_dir->get_subdir_count();i++)
  55. _create_tree(item,p_dir->get_subdir(i));
  56. return true;
  57. }
  58. void FileSystemDock::_update_tree() {
  59. tree->clear();
  60. updating_tree=true;
  61. TreeItem *root = tree->create_item();
  62. TreeItem *favorites = tree->create_item(root);
  63. favorites->set_icon(0, get_icon("Favorites","EditorIcons") );
  64. favorites->set_text(0,TTR("Favorites:"));
  65. favorites->set_selectable(0,false);
  66. Vector<String> faves = EditorSettings::get_singleton()->get_favorite_dirs();
  67. for(int i=0;i<faves.size();i++) {
  68. if (!faves[i].begins_with("res://"))
  69. continue;
  70. TreeItem *ti = tree->create_item(favorites);
  71. String fv = faves[i];
  72. if (fv=="res://")
  73. ti->set_text(0,"/");
  74. else
  75. ti->set_text(0,faves[i].get_file());
  76. ti->set_icon(0,get_icon("Folder","EditorIcons"));
  77. ti->set_selectable(0,true);
  78. ti->set_metadata(0,faves[i]);
  79. }
  80. _create_tree(root,EditorFileSystem::get_singleton()->get_filesystem());
  81. updating_tree=false;
  82. }
  83. void FileSystemDock::_notification(int p_what) {
  84. switch(p_what) {
  85. case NOTIFICATION_RESIZED: {
  86. bool new_mode = get_size().height < get_viewport_rect().size.height*3/4;
  87. if (new_mode != split_mode ) {
  88. split_mode=new_mode;
  89. //print_line("SPLIT MODE? "+itos(split_mode));
  90. if (split_mode) {
  91. file_list_vb->hide();
  92. tree->set_custom_minimum_size(Size2(0,0));
  93. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  94. button_back->show();
  95. } else {
  96. tree->show();
  97. file_list_vb->show();
  98. tree->set_custom_minimum_size(Size2(0,200)*EDSCALE);
  99. tree->set_v_size_flags(SIZE_FILL);
  100. button_back->hide();
  101. if (!EditorFileSystem::get_singleton()->is_scanning()) {
  102. _fs_changed();
  103. }
  104. }
  105. }
  106. } break;
  107. case NOTIFICATION_ENTER_TREE: {
  108. if (initialized)
  109. return;
  110. initialized=true;
  111. EditorFileSystem::get_singleton()->connect("filesystem_changed",this,"_fs_changed");
  112. EditorResourcePreview::get_singleton()->connect("preview_invalidated",this,"_preview_invalidated");
  113. button_reload->set_icon( get_icon("Reload","EditorIcons"));
  114. button_favorite->set_icon( get_icon("Favorites","EditorIcons"));
  115. //button_instance->set_icon( get_icon("Add","EditorIcons"));
  116. //button_open->set_icon( get_icon("Folder","EditorIcons"));
  117. button_back->set_icon( get_icon("Filesystem","EditorIcons"));
  118. if (display_mode == DISPLAY_THUMBNAILS) {
  119. button_display_mode->set_icon(get_icon("FileList","EditorIcons"));
  120. } else {
  121. button_display_mode->set_icon(get_icon("FileThumbnail","EditorIcons"));
  122. }
  123. button_display_mode->connect("pressed",this,"_change_file_display");
  124. //file_options->set_icon( get_icon("Tools","EditorIcons"));
  125. files->connect("item_activated",this,"_select_file");
  126. button_hist_next->connect("pressed",this,"_fw_history");
  127. button_hist_prev->connect("pressed",this,"_bw_history");
  128. search_icon->set_texture( get_icon("Zoom","EditorIcons"));
  129. button_hist_next->set_icon( get_icon("Forward","EditorIcons"));
  130. button_hist_prev->set_icon( get_icon("Back","EditorIcons"));
  131. file_options->connect("item_pressed",this,"_file_option");
  132. button_back->connect("pressed",this,"_go_to_tree",varray(),CONNECT_DEFERRED);
  133. current_path->connect("text_entered",this,"_go_to_dir");
  134. _update_tree(); //maybe it finished already
  135. if (EditorFileSystem::get_singleton()->is_scanning()) {
  136. _set_scannig_mode();
  137. }
  138. } break;
  139. case NOTIFICATION_PROCESS: {
  140. if (EditorFileSystem::get_singleton()->is_scanning()) {
  141. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress()*100);
  142. }
  143. } break;
  144. case NOTIFICATION_EXIT_TREE: {
  145. } break;
  146. case NOTIFICATION_DRAG_BEGIN: {
  147. Dictionary dd = get_viewport()->gui_get_drag_data();
  148. if (tree->is_visible() && dd.has("type") ) {
  149. if ( (String(dd["type"])=="files") || (String(dd["type"])=="files_and_dirs") || (String(dd["type"])=="resource")) {
  150. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  151. }
  152. if ( (String(dd["type"])=="favorite") ) {
  153. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  154. }
  155. }
  156. } break;
  157. case NOTIFICATION_DRAG_END: {
  158. tree->set_drop_mode_flags(0);
  159. } break;
  160. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  161. int new_mode = int(EditorSettings::get_singleton()->get("filesystem_dock/display_mode"));
  162. if (new_mode != display_mode) {
  163. set_display_mode(new_mode);
  164. } else {
  165. _update_files(true);
  166. }
  167. } break;
  168. }
  169. }
  170. void FileSystemDock::_dir_selected() {
  171. TreeItem *ti = tree->get_selected();
  172. if (!ti)
  173. return;
  174. String dir = ti->get_metadata(0);
  175. bool found=false;
  176. Vector<String> favorites = EditorSettings::get_singleton()->get_favorite_dirs();
  177. for(int i=0;i<favorites.size();i++) {
  178. if (favorites[i]==dir) {
  179. found=true;
  180. break;
  181. }
  182. }
  183. button_favorite->set_pressed(found);
  184. if (!split_mode) {
  185. _open_pressed(); //go directly to dir
  186. }
  187. }
  188. void FileSystemDock::_favorites_pressed() {
  189. TreeItem *sel = tree->get_selected();
  190. if (!sel)
  191. return ;
  192. String dir = sel->get_metadata(0);
  193. int idx = -1;
  194. Vector<String> favorites = EditorSettings::get_singleton()->get_favorite_dirs();
  195. for(int i=0;i<favorites.size();i++) {
  196. if (favorites[i]==dir) {
  197. idx=i;
  198. break;
  199. }
  200. }
  201. if (button_favorite->is_pressed() && idx==-1) {
  202. favorites.push_back(dir);
  203. EditorSettings::get_singleton()->set_favorite_dirs(favorites);
  204. _update_tree();
  205. } else if (!button_favorite->is_pressed() && idx!=-1) {
  206. favorites.remove(idx);
  207. EditorSettings::get_singleton()->set_favorite_dirs(favorites);
  208. _update_tree();
  209. }
  210. }
  211. String FileSystemDock::get_selected_path() const {
  212. TreeItem *sel = tree->get_selected();
  213. if (!sel)
  214. return "";
  215. String path = sel->get_metadata(0);
  216. return "res://"+path;
  217. }
  218. String FileSystemDock::get_current_path() const {
  219. return path;
  220. }
  221. void FileSystemDock::_thumbnail_done(const String& p_path,const Ref<Texture>& p_preview, const Variant& p_udata) {
  222. bool valid=false;
  223. if (!search_box->is_hidden()) {
  224. valid=true;
  225. } else {
  226. valid=(path==p_path.get_base_dir());
  227. }
  228. if (p_preview.is_valid() && valid) {
  229. Array uarr=p_udata;
  230. int idx=uarr[0];
  231. String file=uarr[1];
  232. if (idx>=files->get_item_count())
  233. return;
  234. if (files->get_item_text(idx)!=file)
  235. return;
  236. String fpath = files->get_item_metadata(idx);
  237. if (fpath!=p_path)
  238. return;
  239. files->set_item_icon(idx,p_preview);
  240. }
  241. }
  242. void FileSystemDock::_change_file_display() {
  243. if (button_display_mode->is_pressed()) {
  244. display_mode = DISPLAY_LIST;
  245. button_display_mode->set_icon( get_icon("FileThumbnail","EditorIcons"));
  246. } else {
  247. display_mode = DISPLAY_THUMBNAILS;
  248. button_display_mode->set_icon( get_icon("FileList","EditorIcons"));
  249. }
  250. EditorSettings::get_singleton()->set("filesystem_dock/display_mode", display_mode);
  251. _update_files(true);
  252. }
  253. void FileSystemDock::_search(EditorFileSystemDirectory *p_path,List<FileInfo>* matches,int p_max_items) {
  254. if (matches->size()>p_max_items)
  255. return;
  256. for(int i=0;i<p_path->get_subdir_count();i++) {
  257. _search(p_path->get_subdir(i),matches,p_max_items);
  258. }
  259. String match = search_box->get_text();
  260. for(int i=0;i<p_path->get_file_count();i++) {
  261. String file = p_path->get_file(i);
  262. if (file.find(match)!=-1) {
  263. FileInfo fi;
  264. fi.name=file;
  265. fi.type=p_path->get_file_type(i);
  266. fi.path=p_path->get_file_path(i);
  267. if (p_path->get_file_meta(i)) {
  268. if (p_path->is_missing_sources(i)) {
  269. fi.import_status=3;
  270. } else if (p_path->have_sources_changed(i)) {
  271. fi.import_status=2;
  272. } else {
  273. fi.import_status=1;
  274. }
  275. } else {
  276. fi.import_status=0;
  277. }
  278. for(int j=0;j<p_path->get_source_count(i);j++) {
  279. String s = EditorImportPlugin::expand_source_path(p_path->get_source_file(i,j));
  280. if (p_path->is_source_file_missing(i,j)) {
  281. s+=" (Missing)";
  282. }
  283. fi.sources.push_back(s);
  284. }
  285. matches->push_back(fi);
  286. if (matches->size()>p_max_items)
  287. return;
  288. }
  289. }
  290. }
  291. void FileSystemDock::_update_files(bool p_keep_selection) {
  292. Set<String> cselection;
  293. if (p_keep_selection) {
  294. for(int i=0;i<files->get_item_count();i++) {
  295. if (files->is_selected(i))
  296. cselection.insert(files->get_item_text(i));
  297. }
  298. }
  299. files->clear();
  300. current_path->set_text(path);
  301. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_path(path);
  302. if (!efd)
  303. return;
  304. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem_dock/thumbnail_size");
  305. thumbnail_size*=EDSCALE;
  306. Ref<Texture> folder_thumbnail;
  307. Ref<Texture> file_thumbnail;
  308. bool use_thumbnails = (display_mode == DISPLAY_THUMBNAILS);
  309. bool use_folders = search_box->get_text().length()==0 && split_mode;
  310. if (use_thumbnails) { //thumbnails
  311. files->set_max_columns(0);
  312. files->set_icon_mode(ItemList::ICON_MODE_TOP);
  313. files->set_fixed_column_width(thumbnail_size*3/2);
  314. files->set_max_text_lines(2);
  315. files->set_fixed_icon_size(Size2(thumbnail_size,thumbnail_size));
  316. if (!has_icon("ResizedFolder","EditorIcons")) {
  317. Ref<ImageTexture> folder = get_icon("FolderBig","EditorIcons");
  318. Image img = folder->get_data();
  319. img.resize(thumbnail_size,thumbnail_size);
  320. Ref<ImageTexture> resized_folder = Ref<ImageTexture>( memnew( ImageTexture));
  321. resized_folder->create_from_image(img,0);
  322. Theme::get_default()->set_icon("ResizedFolder","EditorIcons",resized_folder);
  323. }
  324. folder_thumbnail = get_icon("ResizedFolder","EditorIcons");
  325. if (!has_icon("ResizedFile","EditorIcons")) {
  326. Ref<ImageTexture> file = get_icon("FileBig","EditorIcons");
  327. Image img = file->get_data();
  328. img.resize(thumbnail_size,thumbnail_size);
  329. Ref<ImageTexture> resized_file = Ref<ImageTexture>( memnew( ImageTexture));
  330. resized_file->create_from_image(img,0);
  331. Theme::get_default()->set_icon("ResizedFile","EditorIcons",resized_file);
  332. }
  333. file_thumbnail = get_icon("ResizedFile","EditorIcons");
  334. } else {
  335. files->set_icon_mode(ItemList::ICON_MODE_LEFT);
  336. files->set_max_columns(1);
  337. files->set_max_text_lines(1);
  338. files->set_fixed_column_width(0);
  339. files->set_fixed_icon_size(Size2());
  340. }
  341. if (use_folders) {
  342. if (path!="res://") {
  343. if (use_thumbnails) {
  344. files->add_item("..",folder_thumbnail,true);
  345. } else {
  346. files->add_item("..",get_icon("folder","FileDialog"),true);
  347. }
  348. String bd = path.get_base_dir();
  349. if (bd!="res://" && !bd.ends_with("/"))
  350. bd+="/";
  351. files->set_item_metadata(files->get_item_count()-1,bd);
  352. }
  353. for(int i=0;i<efd->get_subdir_count();i++) {
  354. String dname=efd->get_subdir(i)->get_name();
  355. if (use_thumbnails) {
  356. files->add_item(dname,folder_thumbnail,true);
  357. } else {
  358. files->add_item(dname,get_icon("folder","FileDialog"),true);
  359. }
  360. files->set_item_metadata(files->get_item_count()-1,path.plus_file(dname)+"/");
  361. if (cselection.has(dname))
  362. files->select(files->get_item_count()-1,false);
  363. }
  364. }
  365. List<FileInfo> filelist;
  366. if (search_box->get_text().length()) {
  367. if (search_box->get_text().length()>1) {
  368. _search(EditorFileSystem::get_singleton()->get_filesystem(),&filelist,128);
  369. }
  370. filelist.sort();
  371. } else {
  372. for(int i=0;i<efd->get_file_count();i++) {
  373. FileInfo fi;
  374. fi.name=efd->get_file(i);
  375. fi.path=path.plus_file(fi.name);
  376. fi.type=efd->get_file_type(i);
  377. if (efd->get_file_meta(i)) {
  378. if (efd->is_missing_sources(i)) {
  379. fi.import_status=3;
  380. } else if (efd->have_sources_changed(i)) {
  381. fi.import_status=2;
  382. } else {
  383. fi.import_status=1;
  384. }
  385. for(int j=0;j<efd->get_source_count(i);j++) {
  386. String s = EditorImportPlugin::expand_source_path(efd->get_source_file(i,j));
  387. if (efd->is_source_file_missing(i,j)) {
  388. s+=" (Missing)";
  389. }
  390. fi.sources.push_back(s);
  391. }
  392. } else {
  393. fi.import_status=0;
  394. }
  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()) {
  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_hidden()) {
  488. button_favorite->show();
  489. _update_tree();
  490. }
  491. if (!file_list_vb->is_hidden()) {
  492. _update_files(true);
  493. }
  494. set_process(false);
  495. }
  496. void FileSystemDock::_set_scannig_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_hidden()) {
  513. _update_tree();
  514. tree->grab_focus();
  515. tree->ensure_cursor_is_visible();
  516. }
  517. if (!file_list_vb->is_hidden()) {
  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_hidden()) {
  529. _update_tree();
  530. tree->grab_focus();
  531. tree->ensure_cursor_is_visible();
  532. }
  533. if (!file_list_vb->is_hidden()) {
  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_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].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. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(reimport[0]);
  796. ERR_FAIL_COND(!rimd.is_valid());
  797. String editor=rimd->get_editor();
  798. if (editor.begins_with("texture_")) { //compatibility fix for old texture format
  799. editor="texture";
  800. }
  801. Ref<EditorImportPlugin> rimp = EditorImportExport::get_singleton()->get_import_plugin_by_name(editor);
  802. ERR_FAIL_COND(!rimp.is_valid());
  803. if (reimport.size()==1) {
  804. rimp->import_dialog(reimport[0]);
  805. } else {
  806. rimp->reimport_multiple_files(reimport);
  807. }
  808. } break;
  809. case FILE_COPY_PATH:
  810. int idx = files->get_current();
  811. if (idx<0 || idx>=files->get_item_count())
  812. break;
  813. String path = files->get_item_metadata(idx);
  814. OS::get_singleton()->set_clipboard(path);
  815. }
  816. }
  817. void FileSystemDock::_open_pressed(){
  818. TreeItem *sel = tree->get_selected();
  819. if (!sel) {
  820. return;
  821. }
  822. path = sel->get_metadata(0);
  823. /*if (path!="res://" && path.ends_with("/")) {
  824. path=path.substr(0,path.length()-1);
  825. }*/
  826. //tree_mode=false;
  827. if (split_mode) {
  828. tree->hide();
  829. file_list_vb->show();
  830. button_favorite->hide();
  831. }
  832. //file_options->show();
  833. _update_files(false);
  834. current_path->set_text(path);
  835. _push_to_history();
  836. // emit_signal("open",path);
  837. }
  838. void FileSystemDock::_search_changed(const String& p_text) {
  839. if (!search_box->is_visible())
  840. return; //wtf
  841. _update_files(false);
  842. }
  843. void FileSystemDock::_rescan() {
  844. _set_scannig_mode();
  845. EditorFileSystem::get_singleton()->scan();
  846. }
  847. void FileSystemDock::fix_dependencies(const String& p_for_file) {
  848. deps_editor->edit(p_for_file);
  849. }
  850. void FileSystemDock::focus_on_filter() {
  851. }
  852. void FileSystemDock::set_display_mode(int p_mode) {
  853. if (p_mode == display_mode)
  854. return;
  855. button_display_mode->set_pressed(p_mode == DISPLAY_LIST);
  856. _change_file_display();
  857. }
  858. Variant FileSystemDock::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  859. if (p_from==tree) {
  860. TreeItem *selected = tree->get_selected();
  861. if (!selected)
  862. return Variant();
  863. String path = selected->get_metadata(0);
  864. if (path==String())
  865. return Variant();
  866. if (!path.ends_with("/"))
  867. path=path+"/";
  868. Vector<String> paths;
  869. paths.push_back(path);
  870. Dictionary d = EditorNode::get_singleton()->drag_files(paths,p_from);
  871. if (selected->get_parent() && tree->get_root()->get_children()==selected->get_parent()) {
  872. //a favorite.. treat as such
  873. d["type"]="favorite";
  874. }
  875. return d;
  876. }
  877. if (p_from==files) {
  878. List<int> seldirs;
  879. List<int> selfiles;
  880. for (int i = 0; i<files->get_item_count(); i++) {
  881. if (files->is_selected(i)) {
  882. String path = files->get_item_metadata(i);
  883. if (path.ends_with("/"))
  884. seldirs.push_back(i);
  885. else
  886. selfiles.push_back(i);
  887. }
  888. }
  889. if (seldirs.empty() && selfiles.empty())
  890. return Variant();
  891. //if (seldirs.size() && selfiles.size())
  892. // return Variant(); //can't really mix files and dirs (i think?) - yes you can, commenting
  893. /*if (selfiles.size()==1) {
  894. Ref<Resource> resource = ResourceLoader::load(files->get_item_metadata(selfiles.front()->get()));
  895. if (resource.is_valid()) {
  896. return EditorNode::get_singleton()->drag_resource(resource,p_from);
  897. }
  898. }*/
  899. if (selfiles.size()>0 && seldirs.size()==0) {
  900. Vector<String> fnames;
  901. for(List<int>::Element *E=selfiles.front();E;E=E->next()) {
  902. fnames.push_back(files->get_item_metadata(E->get()));
  903. }
  904. return EditorNode::get_singleton()->drag_files(fnames,p_from);
  905. }
  906. if (selfiles.size()>0 || seldirs.size()>0) {
  907. Vector<String> fnames;
  908. for(List<int>::Element *E=selfiles.front();E;E=E->next()) {
  909. fnames.push_back(files->get_item_metadata(E->get()));
  910. }
  911. for(List<int>::Element *E=seldirs.front();E;E=E->next()) {
  912. fnames.push_back(files->get_item_metadata(E->get()));
  913. }
  914. return EditorNode::get_singleton()->drag_files_and_dirs(fnames,p_from);
  915. }
  916. }
  917. return Variant();
  918. }
  919. bool FileSystemDock::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{
  920. Dictionary drag_data = p_data;
  921. if (drag_data.has("type") && String(drag_data["type"])=="favorite") {
  922. //moving favorite around
  923. TreeItem *ti = tree->get_item_at_pos(p_point);
  924. if (!ti)
  925. return false;
  926. int what = tree->get_drop_section_at_pos(p_point);
  927. if (ti==tree->get_root()->get_children()) {
  928. return (what==1); //the parent, first fav
  929. }
  930. if (ti->get_parent() && tree->get_root()->get_children()==ti->get_parent()) {
  931. return true; // a favorite
  932. }
  933. if (ti==tree->get_root()->get_children()->get_next()) {
  934. return (what==-1); //the tree, last fav
  935. }
  936. return false;
  937. }
  938. if (drag_data.has("type") && String(drag_data["type"])=="resource") {
  939. return true;
  940. }
  941. if (drag_data.has("type") && ( String(drag_data["type"])=="files" || String(drag_data["type"])=="files_and_dirs")) {
  942. Vector<String> fnames = drag_data["files"];
  943. if (p_from==files) {
  944. int at_pos = files->get_item_at_pos(p_point);
  945. if (at_pos!=-1) {
  946. String dir = files->get_item_metadata(at_pos);
  947. if (dir.ends_with("/"))
  948. return true;
  949. }
  950. }
  951. if (p_from==tree) {
  952. TreeItem *ti = tree->get_item_at_pos(p_point);
  953. if (!ti)
  954. return false;
  955. String path = ti->get_metadata(0);
  956. if (path==String())
  957. return false;
  958. return true;
  959. }
  960. }
  961. return false;
  962. }
  963. void FileSystemDock::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){
  964. if (!can_drop_data_fw(p_point,p_data,p_from))
  965. return;
  966. Dictionary drag_data = p_data;
  967. if (drag_data.has("type") && String(drag_data["type"])=="favorite") {
  968. //moving favorite around
  969. TreeItem *ti = tree->get_item_at_pos(p_point);
  970. if (!ti)
  971. return;
  972. Vector<String> files = drag_data["files"];
  973. ERR_FAIL_COND(files.size()!=1);
  974. String swap = files[0];
  975. if (swap!="res://" && swap.ends_with("/")) {
  976. swap=swap.substr(0,swap.length()-1);
  977. }
  978. int what = tree->get_drop_section_at_pos(p_point);
  979. TreeItem *swap_item=NULL;
  980. if (ti==tree->get_root()->get_children()) {
  981. swap_item=tree->get_root()->get_children()->get_children();
  982. } else if (ti->get_parent() && tree->get_root()->get_children()==ti->get_parent()) {
  983. if (what==-1) {
  984. swap_item=ti;
  985. } else {
  986. swap_item=ti->get_next();
  987. }
  988. }
  989. String swap_with;
  990. if (swap_item) {
  991. swap_with=swap_item->get_metadata(0);
  992. if (swap_with!="res://" && swap_with.ends_with("/")) {
  993. swap_with=swap_with.substr(0,swap_with.length()-1);
  994. }
  995. }
  996. if (swap==swap_with)
  997. return;
  998. Vector<String> dirs = EditorSettings::get_singleton()->get_favorite_dirs();
  999. ERR_FAIL_COND(dirs.find(swap)==-1);
  1000. ERR_FAIL_COND(swap_with!=String() && dirs.find(swap_with)==-1);
  1001. dirs.erase(swap);
  1002. if (swap_with==String()) {
  1003. dirs.push_back(swap);
  1004. } else {
  1005. int idx = dirs.find(swap_with);
  1006. dirs.insert(idx,swap);
  1007. }
  1008. EditorSettings::get_singleton()->set_favorite_dirs(dirs);
  1009. _update_tree();
  1010. return;
  1011. }
  1012. if (drag_data.has("type") && String(drag_data["type"])=="resource") {
  1013. Ref<Resource> res = drag_data["resource"];
  1014. if (!res.is_valid()) {
  1015. return;
  1016. }
  1017. if (p_from==tree) {
  1018. TreeItem *ti = tree->get_item_at_pos(p_point);
  1019. if (!ti)
  1020. return;
  1021. String path = ti->get_metadata(0);
  1022. if (path==String())
  1023. return;
  1024. EditorNode::get_singleton()->save_resource_as(res,path);
  1025. return;
  1026. }
  1027. if (p_from==files) {
  1028. String save_path=path;
  1029. int at_pos = files->get_item_at_pos(p_point);
  1030. if (at_pos!=-1) {
  1031. String to_dir = files->get_item_metadata(at_pos);
  1032. if (to_dir.ends_with("/")) {
  1033. save_path=to_dir;
  1034. if (save_path!="res://")
  1035. save_path=save_path.substr(0,save_path.length()-1);
  1036. }
  1037. }
  1038. EditorNode::get_singleton()->save_resource_as(res,save_path);
  1039. return;
  1040. }
  1041. }
  1042. if (drag_data.has("type") && ( String(drag_data["type"])=="files" || String(drag_data["type"])=="files_and_dirs")) {
  1043. if (p_from==files || p_from==tree) {
  1044. String to_dir;
  1045. if (p_from==files) {
  1046. int at_pos = files->get_item_at_pos(p_point);
  1047. ERR_FAIL_COND(at_pos==-1);
  1048. to_dir = files->get_item_metadata(at_pos);
  1049. } else {
  1050. TreeItem *ti = tree->get_item_at_pos(p_point);
  1051. if (!ti)
  1052. return;
  1053. to_dir = ti->get_metadata(0);
  1054. ERR_FAIL_COND(to_dir==String());
  1055. }
  1056. if (to_dir!="res://" && to_dir.ends_with("/")) {
  1057. to_dir=to_dir.substr(0,to_dir.length()-1);
  1058. }
  1059. Vector<String> fnames = drag_data["files"];
  1060. move_files.clear();
  1061. move_dirs.clear();
  1062. for(int i=0;i<fnames.size();i++) {
  1063. if (fnames[i].ends_with("/"))
  1064. move_dirs.push_back(fnames[i]);
  1065. else
  1066. move_files.push_back(fnames[i]);
  1067. }
  1068. _move_operation(to_dir);
  1069. }
  1070. }
  1071. }
  1072. void FileSystemDock::_files_list_rmb_select(int p_item,const Vector2& p_pos) {
  1073. Vector<String> filenames;
  1074. bool all_scenes=true;
  1075. bool all_can_reimport=true;
  1076. Set<String> types;
  1077. for(int i=0;i<files->get_item_count();i++) {
  1078. if (!files->is_selected(i))
  1079. continue;
  1080. String path = files->get_item_metadata(i);
  1081. if (files->get_item_text(i)=="..") {
  1082. // no operate on ..
  1083. return;
  1084. }
  1085. if (path.ends_with("/")) {
  1086. //no operate on dirs
  1087. return;
  1088. }
  1089. EditorFileSystemDirectory *efsd=NULL;
  1090. int pos;
  1091. efsd = EditorFileSystem::get_singleton()->find_file(path,&pos);
  1092. if (efsd) {
  1093. if (!efsd->get_file_meta(pos)) {
  1094. all_can_reimport=false;
  1095. } else {
  1096. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(path);
  1097. if (rimd.is_valid()) {
  1098. String editor=rimd->get_editor();
  1099. if (editor.begins_with("texture_")) { //compatibility fix for old texture format
  1100. editor="texture";
  1101. }
  1102. types.insert(editor);
  1103. } else {
  1104. all_can_reimport=false;
  1105. }
  1106. }
  1107. } else {
  1108. all_can_reimport=false;
  1109. }
  1110. filenames.push_back(path);
  1111. if (EditorFileSystem::get_singleton()->get_file_type(path)!="PackedScene")
  1112. all_scenes=false;
  1113. }
  1114. if (filenames.size()==0)
  1115. return;
  1116. file_options->clear();
  1117. file_options->set_size(Size2(1,1));
  1118. file_options->add_item(TTR("Open"),FILE_OPEN);
  1119. if (all_scenes) {
  1120. file_options->add_item(TTR("Instance"),FILE_INSTANCE);
  1121. }
  1122. file_options->add_separator();
  1123. if (filenames.size()==1) {
  1124. file_options->add_item(TTR("Edit Dependencies.."),FILE_DEPENDENCIES);
  1125. file_options->add_item(TTR("View Owners.."),FILE_OWNERS);
  1126. file_options->add_separator();
  1127. }
  1128. if (filenames.size()==1) {
  1129. file_options->add_item(TTR("Copy Path"), FILE_COPY_PATH);
  1130. file_options->add_item(TTR("Rename or Move.."),FILE_MOVE);
  1131. } else {
  1132. file_options->add_item(TTR("Move To.."),FILE_MOVE);
  1133. }
  1134. file_options->add_item(TTR("Delete"),FILE_REMOVE);
  1135. //file_options->add_item(TTR("Info"),FILE_INFO);
  1136. file_options->add_separator();
  1137. file_options->add_item(TTR("Show In File Manager"),FILE_SHOW_IN_EXPLORER);
  1138. if (all_can_reimport && types.size()==1) { //all can reimport and are of the same type
  1139. bool valid=true;
  1140. Ref<EditorImportPlugin> rimp = EditorImportExport::get_singleton()->get_import_plugin_by_name(types.front()->get());
  1141. if (rimp.is_valid()) {
  1142. if (filenames.size()>1 && !rimp->can_reimport_multiple_files()) {
  1143. valid=false;
  1144. }
  1145. } else {
  1146. valid=false;
  1147. }
  1148. if (valid) {
  1149. file_options->add_separator();
  1150. file_options->add_item(TTR("Re-Import.."),FILE_REIMPORT);
  1151. }
  1152. }
  1153. file_options->set_pos(files->get_global_pos() + p_pos);
  1154. file_options->popup();
  1155. }
  1156. void FileSystemDock::_bind_methods() {
  1157. ClassDB::bind_method(_MD("_update_tree"),&FileSystemDock::_update_tree);
  1158. ClassDB::bind_method(_MD("_rescan"),&FileSystemDock::_rescan);
  1159. ClassDB::bind_method(_MD("_favorites_pressed"),&FileSystemDock::_favorites_pressed);
  1160. // ClassDB::bind_method(_MD("_instance_pressed"),&ScenesDock::_instance_pressed);
  1161. ClassDB::bind_method(_MD("_open_pressed"),&FileSystemDock::_open_pressed);
  1162. ClassDB::bind_method(_MD("_thumbnail_done"),&FileSystemDock::_thumbnail_done);
  1163. ClassDB::bind_method(_MD("_select_file"), &FileSystemDock::_select_file);
  1164. ClassDB::bind_method(_MD("_go_to_tree"), &FileSystemDock::_go_to_tree);
  1165. ClassDB::bind_method(_MD("_go_to_dir"), &FileSystemDock::_go_to_dir);
  1166. ClassDB::bind_method(_MD("_change_file_display"), &FileSystemDock::_change_file_display);
  1167. ClassDB::bind_method(_MD("_fw_history"), &FileSystemDock::_fw_history);
  1168. ClassDB::bind_method(_MD("_bw_history"), &FileSystemDock::_bw_history);
  1169. ClassDB::bind_method(_MD("_fs_changed"), &FileSystemDock::_fs_changed);
  1170. ClassDB::bind_method(_MD("_dir_selected"), &FileSystemDock::_dir_selected);
  1171. ClassDB::bind_method(_MD("_file_option"), &FileSystemDock::_file_option);
  1172. ClassDB::bind_method(_MD("_move_operation"), &FileSystemDock::_move_operation);
  1173. ClassDB::bind_method(_MD("_rename_operation"), &FileSystemDock::_rename_operation);
  1174. ClassDB::bind_method(_MD("_search_changed"), &FileSystemDock::_search_changed);
  1175. ClassDB::bind_method(_MD("get_drag_data_fw"), &FileSystemDock::get_drag_data_fw);
  1176. ClassDB::bind_method(_MD("can_drop_data_fw"), &FileSystemDock::can_drop_data_fw);
  1177. ClassDB::bind_method(_MD("drop_data_fw"), &FileSystemDock::drop_data_fw);
  1178. ClassDB::bind_method(_MD("_files_list_rmb_select"),&FileSystemDock::_files_list_rmb_select);
  1179. ClassDB::bind_method(_MD("_preview_invalidated"),&FileSystemDock::_preview_invalidated);
  1180. ADD_SIGNAL(MethodInfo("instance", PropertyInfo(Variant::STRING_ARRAY, "files")));
  1181. ADD_SIGNAL(MethodInfo("open"));
  1182. }
  1183. FileSystemDock::FileSystemDock(EditorNode *p_editor) {
  1184. editor=p_editor;
  1185. HBoxContainer *toolbar_hbc = memnew( HBoxContainer );
  1186. add_child(toolbar_hbc);
  1187. button_hist_prev = memnew( ToolButton );
  1188. toolbar_hbc->add_child(button_hist_prev);
  1189. button_hist_prev->set_disabled(true);
  1190. button_hist_prev->set_tooltip(TTR("Previous Directory"));
  1191. button_hist_next = memnew( ToolButton );
  1192. toolbar_hbc->add_child(button_hist_next);
  1193. button_hist_next->set_disabled(true);
  1194. button_hist_prev->set_focus_mode(FOCUS_NONE);
  1195. button_hist_next->set_focus_mode(FOCUS_NONE);
  1196. button_hist_next->set_tooltip(TTR("Next Directory"));
  1197. current_path=memnew( LineEdit );
  1198. current_path->set_h_size_flags(SIZE_EXPAND_FILL);
  1199. toolbar_hbc->add_child(current_path);
  1200. button_reload = memnew( Button );
  1201. button_reload->set_flat(true);
  1202. button_reload->connect("pressed",this,"_rescan");
  1203. toolbar_hbc->add_child(button_reload);
  1204. button_reload->set_focus_mode(FOCUS_NONE);
  1205. button_reload->set_tooltip(TTR("Re-Scan Filesystem"));
  1206. button_reload->hide();
  1207. //toolbar_hbc->add_spacer();
  1208. button_favorite = memnew( Button );
  1209. button_favorite->set_flat(true);
  1210. button_favorite->set_toggle_mode(true);
  1211. button_favorite->connect("pressed",this,"_favorites_pressed");
  1212. toolbar_hbc->add_child(button_favorite);
  1213. button_favorite->set_tooltip(TTR("Toggle folder status as Favorite"));
  1214. button_favorite->set_focus_mode(FOCUS_NONE);
  1215. // Control *spacer = memnew( Control);
  1216. /*
  1217. button_open = memnew( Button );
  1218. button_open->set_flat(true);
  1219. button_open->connect("pressed",this,"_open_pressed");
  1220. toolbar_hbc->add_child(button_open);
  1221. button_open->hide();
  1222. button_open->set_focus_mode(FOCUS_NONE);
  1223. button_open->set_tooltip("Open the selected file.\nOpen as scene if a scene, or as resource otherwise.");
  1224. button_instance = memnew( Button );
  1225. button_instance->set_flat(true);
  1226. button_instance->connect("pressed",this,"_instance_pressed");
  1227. toolbar_hbc->add_child(button_instance);
  1228. button_instance->hide();
  1229. button_instance->set_focus_mode(FOCUS_NONE);
  1230. button_instance->set_tooltip(TTR("Instance the selected scene(s) as child of the selected node."));
  1231. */
  1232. file_options = memnew( PopupMenu );
  1233. add_child(file_options);
  1234. split_box = memnew( VSplitContainer );
  1235. add_child(split_box);
  1236. split_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1237. tree = memnew( Tree );
  1238. tree->set_hide_root(true);
  1239. split_box->add_child(tree);
  1240. tree->set_drag_forwarding(this);
  1241. //tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1242. tree->connect("item_edited",this,"_favorite_toggled");
  1243. tree->connect("item_activated",this,"_open_pressed");
  1244. tree->connect("cell_selected",this,"_dir_selected");
  1245. files = memnew( ItemList );
  1246. files->set_v_size_flags(SIZE_EXPAND_FILL);
  1247. files->set_select_mode(ItemList::SELECT_MULTI);
  1248. files->set_drag_forwarding(this);
  1249. files->connect("item_rmb_selected",this,"_files_list_rmb_select");
  1250. files->set_allow_rmb_select(true);
  1251. file_list_vb = memnew( VBoxContainer );
  1252. split_box->add_child(file_list_vb);
  1253. file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1254. path_hb = memnew( HBoxContainer );
  1255. file_list_vb->add_child(path_hb);
  1256. button_back = memnew( ToolButton );
  1257. path_hb->add_child(button_back);
  1258. button_back->hide();
  1259. search_box = memnew( LineEdit );
  1260. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1261. path_hb->add_child(search_box);
  1262. search_box->connect("text_changed",this,"_search_changed");
  1263. search_icon = memnew( TextureFrame );
  1264. search_icon->set_stretch_mode(TextureFrame::STRETCH_KEEP_CENTERED);
  1265. path_hb->add_child(search_icon);
  1266. button_display_mode = memnew( ToolButton );
  1267. path_hb->add_child(button_display_mode);
  1268. button_display_mode->set_toggle_mode(true);
  1269. file_list_vb->add_child(files);
  1270. scanning_vb = memnew( VBoxContainer );
  1271. Label *slabel = memnew( Label );
  1272. slabel->set_text("Scanning Files,\nPlease Wait..");
  1273. slabel->set_align(Label::ALIGN_CENTER);
  1274. scanning_vb->add_child(slabel);
  1275. scanning_progress = memnew( ProgressBar );
  1276. scanning_vb->add_child(scanning_progress);
  1277. add_child(scanning_vb);
  1278. scanning_vb->hide();
  1279. deps_editor = memnew( DependencyEditor );
  1280. add_child(deps_editor);
  1281. owners_editor = memnew( DependencyEditorOwners);
  1282. add_child(owners_editor);
  1283. remove_dialog = memnew( DependencyRemoveDialog);
  1284. add_child(remove_dialog);
  1285. move_dialog = memnew( EditorDirDialog );
  1286. add_child(move_dialog);
  1287. move_dialog->connect("dir_selected",this,"_move_operation");
  1288. move_dialog->get_ok()->set_text(TTR("Move"));
  1289. rename_dialog = memnew( EditorFileDialog );
  1290. rename_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1291. rename_dialog->connect("file_selected",this,"_rename_operation");
  1292. add_child(rename_dialog);
  1293. updating_tree=false;
  1294. initialized=false;
  1295. history.push_back("res://");
  1296. history_pos=0;
  1297. split_mode=false;
  1298. display_mode = DISPLAY_THUMBNAILS;
  1299. path="res://";
  1300. add_constant_override("separation",3);
  1301. }
  1302. FileSystemDock::~FileSystemDock() {
  1303. }