filesystem_dock.cpp 44 KB

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