file_dialog.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*************************************************************************/
  2. /* file_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 "file_dialog.h"
  30. #include "scene/gui/label.h"
  31. #include "print_string.h"
  32. #include "os/keyboard.h"
  33. FileDialog::GetIconFunc FileDialog::get_icon_func=NULL;
  34. FileDialog::GetIconFunc FileDialog::get_large_icon_func=NULL;
  35. FileDialog::RegisterFunc FileDialog::register_func=NULL;
  36. FileDialog::RegisterFunc FileDialog::unregister_func=NULL;
  37. VBoxContainer *FileDialog::get_vbox() {
  38. return vbox;
  39. }
  40. void FileDialog::_notification(int p_what) {
  41. if (p_what==NOTIFICATION_ENTER_TREE) {
  42. refresh->set_icon(get_icon("Reload","EditorIcons"));
  43. }
  44. if (p_what==NOTIFICATION_DRAW) {
  45. //RID ci = get_canvas_item();
  46. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  47. }
  48. }
  49. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  50. tree->set_select_mode(p_enable?Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  51. };
  52. Vector<String> FileDialog::get_selected_files() const {
  53. Vector<String> list;
  54. TreeItem* item = tree->get_root();
  55. while ( (item = tree->get_next_selected(item)) ) {
  56. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  57. };
  58. return list;
  59. };
  60. void FileDialog::update_dir() {
  61. dir->set_text(dir_access->get_current_dir());
  62. }
  63. void FileDialog::_dir_entered(String p_dir) {
  64. dir_access->change_dir(p_dir);
  65. file->set_text("");
  66. invalidate();
  67. update_dir();
  68. }
  69. void FileDialog::_file_entered(const String& p_file) {
  70. _action_pressed();
  71. }
  72. void FileDialog::_save_confirm_pressed() {
  73. String f=dir_access->get_current_dir().plus_file(file->get_text());
  74. emit_signal("file_selected",f);
  75. hide();
  76. }
  77. void FileDialog::_post_popup() {
  78. ConfirmationDialog::_post_popup();
  79. if (invalidated) {
  80. update_file_list();
  81. invalidated=false;
  82. }
  83. if (mode==MODE_SAVE_FILE)
  84. file->grab_focus();
  85. else
  86. tree->grab_focus();
  87. }
  88. void FileDialog::_action_pressed() {
  89. if (mode==MODE_OPEN_FILES) {
  90. TreeItem *ti=tree->get_next_selected(NULL);
  91. String fbase=dir_access->get_current_dir();
  92. DVector<String> files;
  93. while(ti) {
  94. files.push_back( fbase.plus_file(ti->get_text(0)) );
  95. ti=tree->get_next_selected(ti);
  96. }
  97. if (files.size()) {
  98. emit_signal("files_selected",files);
  99. hide();
  100. }
  101. return;
  102. }
  103. String f=dir_access->get_current_dir().plus_file(file->get_text());
  104. if (mode==MODE_OPEN_FILE && dir_access->file_exists(f)) {
  105. emit_signal("file_selected",f);
  106. hide();
  107. }
  108. if (mode==MODE_OPEN_DIR) {
  109. String path=dir_access->get_current_dir();
  110. /*if (tree->get_selected()) {
  111. Dictionary d = tree->get_selected()->get_metadata(0);
  112. if (d["dir"]) {
  113. path=path+"/"+String(d["name"]);
  114. }
  115. }*/
  116. path=path.replace("\\","/");
  117. emit_signal("dir_selected",path);
  118. hide();
  119. }
  120. if (mode==MODE_SAVE_FILE) {
  121. bool valid=false;
  122. if (filter->get_selected()==filter->get_item_count()-1) {
  123. valid=true; //match none
  124. } else if (filters.size()>1 && filter->get_selected()==0) {
  125. // match all filters
  126. for (int i=0;i<filters.size();i++) {
  127. String flt=filters[i].get_slice(";",0);
  128. for (int j=0;j<flt.get_slice_count(",");j++) {
  129. String str = flt.get_slice(",",j).strip_edges();
  130. if (f.match(str)) {
  131. valid=true;
  132. break;
  133. }
  134. }
  135. if (valid)
  136. break;
  137. }
  138. } else {
  139. int idx=filter->get_selected();
  140. if (filters.size()>1)
  141. idx--;
  142. if (idx>=0 && idx<filters.size()) {
  143. String flt=filters[idx].get_slice(";",0);
  144. int filterSliceCount=flt.get_slice_count(",");
  145. for (int j=0;j<filterSliceCount;j++) {
  146. String str = (flt.get_slice(",",j).strip_edges());
  147. if (f.match(str)) {
  148. valid=true;
  149. break;
  150. }
  151. }
  152. if (!valid && filterSliceCount>0) {
  153. String str = (flt.get_slice(",",0).strip_edges());
  154. f+=str.substr(1, str.length()-1);
  155. file->set_text(f.get_file());
  156. valid=true;
  157. }
  158. } else {
  159. valid=true;
  160. }
  161. }
  162. if (!valid) {
  163. exterr->popup_centered_minsize(Size2(250,80));
  164. return;
  165. }
  166. if (dir_access->file_exists(f)) {
  167. confirm_save->set_text("File Exists, Overwrite?");
  168. confirm_save->popup_centered(Size2(200,80));
  169. } else {
  170. emit_signal("file_selected",f);
  171. hide();
  172. }
  173. }
  174. }
  175. void FileDialog::_cancel_pressed() {
  176. file->set_text("");
  177. invalidate();
  178. hide();
  179. }
  180. void FileDialog::_tree_selected() {
  181. TreeItem *ti=tree->get_selected();
  182. if (!ti)
  183. return;
  184. Dictionary d=ti->get_metadata(0);
  185. if (!d["dir"]) {
  186. file->set_text(d["name"]);
  187. }
  188. }
  189. void FileDialog::_tree_dc_selected() {
  190. TreeItem *ti=tree->get_selected();
  191. if (!ti)
  192. return;
  193. Dictionary d=ti->get_metadata(0);
  194. if (d["dir"]) {
  195. dir_access->change_dir(d["name"]);
  196. if (mode==MODE_OPEN_FILE || mode==MODE_OPEN_FILES || mode==MODE_OPEN_DIR)
  197. file->set_text("");
  198. call_deferred("_update_file_list");
  199. call_deferred("_update_dir");
  200. } else {
  201. _action_pressed();
  202. }
  203. }
  204. void FileDialog::update_file_list() {
  205. tree->clear();
  206. dir_access->list_dir_begin();
  207. TreeItem *root = tree->create_item();
  208. Ref<Texture> folder = get_icon("folder");
  209. List<String> files;
  210. List<String> dirs;
  211. bool isdir;
  212. bool ishidden;
  213. bool show_hidden = show_hidden_files;
  214. String item;
  215. while ((item=dir_access->get_next(&isdir))!="") {
  216. ishidden = dir_access->current_is_hidden();
  217. if (show_hidden || !ishidden) {
  218. if (!isdir)
  219. files.push_back(item);
  220. else
  221. dirs.push_back(item);
  222. }
  223. }
  224. dirs.sort_custom<NoCaseComparator>();
  225. files.sort_custom<NoCaseComparator>();
  226. while(!dirs.empty()) {
  227. if (dirs.front()->get()!=".") {
  228. TreeItem *ti=tree->create_item(root);
  229. ti->set_text(0,dirs.front()->get()+"/");
  230. ti->set_icon(0,folder);
  231. Dictionary d;
  232. d["name"]=dirs.front()->get();
  233. d["dir"]=true;
  234. ti->set_metadata(0,d);
  235. }
  236. dirs.pop_front();
  237. }
  238. dirs.clear();
  239. List<String> patterns;
  240. // build filter
  241. if (filter->get_selected()==filter->get_item_count()-1) {
  242. // match all
  243. } else if (filters.size()>1 && filter->get_selected()==0) {
  244. // match all filters
  245. for (int i=0;i<filters.size();i++) {
  246. String f=filters[i].get_slice(";",0);
  247. for (int j=0;j<f.get_slice_count(",");j++) {
  248. patterns.push_back(f.get_slice(",",j).strip_edges());
  249. }
  250. }
  251. } else {
  252. int idx=filter->get_selected();
  253. if (filters.size()>1)
  254. idx--;
  255. if (idx>=0 && idx<filters.size()) {
  256. String f=filters[idx].get_slice(";",0);
  257. for (int j=0;j<f.get_slice_count(",");j++) {
  258. patterns.push_back(f.get_slice(",",j).strip_edges());
  259. }
  260. }
  261. }
  262. String base_dir = dir_access->get_current_dir();
  263. while(!files.empty()) {
  264. bool match=patterns.empty();
  265. for(List<String>::Element *E=patterns.front();E;E=E->next()) {
  266. if (files.front()->get().matchn(E->get())) {
  267. match=true;
  268. break;
  269. }
  270. }
  271. if (match) {
  272. TreeItem *ti=tree->create_item(root);
  273. ti->set_text(0,files.front()->get());
  274. if (get_icon_func) {
  275. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  276. ti->set_icon(0,icon);
  277. }
  278. if (mode==MODE_OPEN_DIR) {
  279. ti->set_custom_color(0,get_color("files_disabled"));
  280. ti->set_selectable(0,false);
  281. }
  282. Dictionary d;
  283. d["name"]=files.front()->get();
  284. d["dir"]=false;
  285. ti->set_metadata(0,d);
  286. if (file->get_text()==files.front()->get())
  287. ti->select(0);
  288. }
  289. files.pop_front();
  290. }
  291. if (tree->get_root() && tree->get_root()->get_children())
  292. tree->get_root()->get_children()->select(0);
  293. files.clear();
  294. }
  295. void FileDialog::_filter_selected(int) {
  296. update_file_list();
  297. }
  298. void FileDialog::update_filters() {
  299. filter->clear();
  300. if (filters.size()>1) {
  301. String all_filters;
  302. const int max_filters=5;
  303. for(int i=0;i<MIN( max_filters, filters.size()) ;i++) {
  304. String flt=filters[i].get_slice(";",0);
  305. if (i>0)
  306. all_filters+=",";
  307. all_filters+=flt;
  308. }
  309. if (max_filters<filters.size())
  310. all_filters+=", ...";
  311. filter->add_item("All Recognized ( "+all_filters+" )");
  312. }
  313. for(int i=0;i<filters.size();i++) {
  314. String flt=filters[i].get_slice(";",0).strip_edges();
  315. String desc=filters[i].get_slice(";",1).strip_edges();
  316. if (desc.length())
  317. filter->add_item(desc+" ( "+flt+" )");
  318. else
  319. filter->add_item("( "+flt+" )");
  320. }
  321. filter->add_item("All Files (*)");
  322. }
  323. void FileDialog::clear_filters() {
  324. filters.clear();
  325. update_filters();
  326. invalidate();
  327. }
  328. void FileDialog::add_filter(const String& p_filter) {
  329. filters.push_back(p_filter);
  330. update_filters();
  331. invalidate();
  332. }
  333. String FileDialog::get_current_dir() const {
  334. return dir->get_text();
  335. }
  336. String FileDialog::get_current_file() const {
  337. return file->get_text();
  338. }
  339. String FileDialog::get_current_path() const {
  340. return dir->get_text().plus_file(file->get_text());
  341. }
  342. void FileDialog::set_current_dir(const String& p_dir) {
  343. dir_access->change_dir(p_dir);
  344. update_dir();
  345. invalidate();
  346. }
  347. void FileDialog::set_current_file(const String& p_file) {
  348. file->set_text(p_file);
  349. update_dir();
  350. invalidate();
  351. int lp = p_file.find_last(".");
  352. if (lp!=-1) {
  353. file->select(0,lp);
  354. file->grab_focus();
  355. }
  356. }
  357. void FileDialog::set_current_path(const String& p_path) {
  358. if (!p_path.size())
  359. return;
  360. int pos=MAX( p_path.find_last("/"), p_path.find_last("\\") );
  361. if (pos==-1) {
  362. set_current_file(p_path);
  363. } else {
  364. String dir=p_path.substr(0,pos);
  365. String file=p_path.substr(pos+1,p_path.length());
  366. set_current_dir(dir);
  367. set_current_file(file);
  368. }
  369. }
  370. void FileDialog::set_mode(Mode p_mode) {
  371. mode=p_mode;
  372. switch(mode) {
  373. case MODE_OPEN_FILE: get_ok()->set_text("Open"); set_title("Open a File"); makedir->hide(); break;
  374. case MODE_OPEN_FILES: get_ok()->set_text("Open"); set_title("Open File(s)"); makedir->hide(); break;
  375. case MODE_SAVE_FILE: get_ok()->set_text("Save"); set_title("Save a File"); makedir->show(); break;
  376. case MODE_OPEN_DIR: get_ok()->set_text("Open"); set_title("Open a Directory"); makedir->show(); break;
  377. }
  378. if (mode==MODE_OPEN_FILES) {
  379. tree->set_select_mode(Tree::SELECT_MULTI);
  380. } else {
  381. tree->set_select_mode(Tree::SELECT_SINGLE);
  382. }
  383. }
  384. FileDialog::Mode FileDialog::get_mode() const {
  385. return mode;
  386. }
  387. void FileDialog::set_access(Access p_access) {
  388. ERR_FAIL_INDEX(p_access,3);
  389. if (access==p_access)
  390. return;
  391. memdelete( dir_access );
  392. switch(p_access) {
  393. case ACCESS_FILESYSTEM: {
  394. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  395. } break;
  396. case ACCESS_RESOURCES: {
  397. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  398. } break;
  399. case ACCESS_USERDATA: {
  400. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  401. } break;
  402. }
  403. access=p_access;
  404. _update_drives();
  405. invalidate();
  406. update_filters();
  407. update_dir();
  408. }
  409. void FileDialog::invalidate() {
  410. if (is_visible()) {
  411. update_file_list();
  412. invalidated=false;
  413. } else {
  414. invalidated=true;
  415. }
  416. }
  417. FileDialog::Access FileDialog::get_access() const{
  418. return access;
  419. }
  420. void FileDialog::_make_dir_confirm() {
  421. Error err = dir_access->make_dir( makedirname->get_text() );
  422. if (err==OK) {
  423. dir_access->change_dir(makedirname->get_text());
  424. invalidate();
  425. update_filters();
  426. update_dir();
  427. } else {
  428. mkdirerr->popup_centered_minsize(Size2(250,50));
  429. }
  430. }
  431. void FileDialog::_make_dir() {
  432. makedialog->popup_centered_minsize(Size2(250,80));
  433. makedirname->grab_focus();
  434. }
  435. void FileDialog::_select_drive(int p_idx) {
  436. String d = drives->get_item_text(p_idx);
  437. dir_access->change_dir(d);
  438. file->set_text("");
  439. invalidate();
  440. update_dir();
  441. }
  442. void FileDialog::_update_drives() {
  443. int dc = dir_access->get_drive_count();
  444. if (dc==0 || access!=ACCESS_FILESYSTEM) {
  445. drives->hide();
  446. } else {
  447. drives->clear();
  448. drives->show();
  449. for(int i=0;i<dir_access->get_drive_count();i++) {
  450. String d = dir_access->get_drive(i);
  451. drives->add_item(dir_access->get_drive(i));
  452. }
  453. drives->select(dir_access->get_current_drive());
  454. }
  455. }
  456. bool FileDialog::default_show_hidden_files=true;
  457. void FileDialog::_bind_methods() {
  458. ObjectTypeDB::bind_method(_MD("_tree_selected"),&FileDialog::_tree_selected);
  459. ObjectTypeDB::bind_method(_MD("_tree_db_selected"),&FileDialog::_tree_dc_selected);
  460. ObjectTypeDB::bind_method(_MD("_dir_entered"),&FileDialog::_dir_entered);
  461. ObjectTypeDB::bind_method(_MD("_file_entered"),&FileDialog::_file_entered);
  462. ObjectTypeDB::bind_method(_MD("_action_pressed"),&FileDialog::_action_pressed);
  463. ObjectTypeDB::bind_method(_MD("_cancel_pressed"),&FileDialog::_cancel_pressed);
  464. ObjectTypeDB::bind_method(_MD("_filter_selected"),&FileDialog::_filter_selected);
  465. ObjectTypeDB::bind_method(_MD("_save_confirm_pressed"),&FileDialog::_save_confirm_pressed);
  466. ObjectTypeDB::bind_method(_MD("clear_filters"),&FileDialog::clear_filters);
  467. ObjectTypeDB::bind_method(_MD("add_filter","filter"),&FileDialog::add_filter);
  468. ObjectTypeDB::bind_method(_MD("get_current_dir"),&FileDialog::get_current_dir);
  469. ObjectTypeDB::bind_method(_MD("get_current_file"),&FileDialog::get_current_file);
  470. ObjectTypeDB::bind_method(_MD("get_current_path"),&FileDialog::get_current_path);
  471. ObjectTypeDB::bind_method(_MD("set_current_dir","dir"),&FileDialog::set_current_dir);
  472. ObjectTypeDB::bind_method(_MD("set_current_file","file"),&FileDialog::set_current_file);
  473. ObjectTypeDB::bind_method(_MD("set_current_path","path"),&FileDialog::set_current_path);
  474. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&FileDialog::set_mode);
  475. ObjectTypeDB::bind_method(_MD("get_mode"),&FileDialog::get_mode);
  476. ObjectTypeDB::bind_method(_MD("get_vbox:VBoxContainer"),&FileDialog::get_vbox);
  477. ObjectTypeDB::bind_method(_MD("set_access","access"),&FileDialog::set_access);
  478. ObjectTypeDB::bind_method(_MD("get_access"),&FileDialog::get_access);
  479. ObjectTypeDB::bind_method(_MD("set_show_hidden_files"),&FileDialog::set_show_hidden_files);
  480. ObjectTypeDB::bind_method(_MD("is_showing_hidden_files"),&FileDialog::is_showing_hidden_files);
  481. ObjectTypeDB::bind_method(_MD("_select_drive"),&FileDialog::_select_drive);
  482. ObjectTypeDB::bind_method(_MD("_make_dir"),&FileDialog::_make_dir);
  483. ObjectTypeDB::bind_method(_MD("_make_dir_confirm"),&FileDialog::_make_dir_confirm);
  484. ObjectTypeDB::bind_method(_MD("_update_file_list"),&FileDialog::update_file_list);
  485. ObjectTypeDB::bind_method(_MD("_update_dir"),&FileDialog::update_dir);
  486. ObjectTypeDB::bind_method(_MD("invalidate"),&FileDialog::invalidate);
  487. ADD_SIGNAL(MethodInfo("file_selected",PropertyInfo( Variant::STRING,"path")));
  488. ADD_SIGNAL(MethodInfo("files_selected",PropertyInfo( Variant::STRING_ARRAY,"paths")));
  489. ADD_SIGNAL(MethodInfo("dir_selected",PropertyInfo( Variant::STRING,"dir")));
  490. BIND_CONSTANT( MODE_OPEN_FILE );
  491. BIND_CONSTANT( MODE_OPEN_FILES );
  492. BIND_CONSTANT( MODE_OPEN_DIR );
  493. BIND_CONSTANT( MODE_SAVE_FILE );
  494. BIND_CONSTANT( ACCESS_RESOURCES );
  495. BIND_CONSTANT( ACCESS_USERDATA );
  496. BIND_CONSTANT( ACCESS_FILESYSTEM );
  497. }
  498. void FileDialog::set_show_hidden_files(bool p_show) {
  499. show_hidden_files=p_show;
  500. invalidate();
  501. }
  502. bool FileDialog::is_showing_hidden_files() const {
  503. return show_hidden_files;
  504. }
  505. void FileDialog::set_default_show_hidden_files(bool p_show) {
  506. default_show_hidden_files=p_show;
  507. }
  508. FileDialog::FileDialog() {
  509. show_hidden_files=default_show_hidden_files;
  510. VBoxContainer *vbc = memnew( VBoxContainer );
  511. add_child(vbc);
  512. set_child_rect(vbc);
  513. mode=MODE_SAVE_FILE;
  514. set_title("Save a File");
  515. dir = memnew(LineEdit);
  516. HBoxContainer *pathhb = memnew( HBoxContainer );
  517. pathhb->add_child(dir);
  518. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  519. refresh = memnew( ToolButton );
  520. refresh->connect("pressed",this,"_update_file_list");
  521. pathhb->add_child(refresh);
  522. drives = memnew( OptionButton );
  523. pathhb->add_child(drives);
  524. drives->connect("item_selected",this,"_select_drive");
  525. makedir = memnew( Button );
  526. makedir->set_text("Create Folder");
  527. makedir->connect("pressed",this,"_make_dir");
  528. pathhb->add_child(makedir);
  529. vbc->add_margin_child("Path:",pathhb);
  530. tree = memnew(Tree);
  531. tree->set_hide_root(true);
  532. vbc->add_margin_child("Directories & Files:",tree,true);
  533. file = memnew(LineEdit);
  534. //add_child(file);
  535. vbc->add_margin_child("File:",file);
  536. filter = memnew( OptionButton );
  537. //add_child(filter);
  538. vbc->add_margin_child("Filter:",filter);
  539. filter->set_clip_text(true);//too many extensions overflow it
  540. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  541. access=ACCESS_RESOURCES;
  542. _update_drives();
  543. connect("confirmed", this,"_action_pressed");
  544. //cancel->connect("pressed", this,"_cancel_pressed");
  545. tree->connect("cell_selected", this,"_tree_selected",varray(),CONNECT_DEFERRED);
  546. tree->connect("item_activated", this,"_tree_db_selected",varray());
  547. dir->connect("text_entered", this,"_dir_entered");
  548. file->connect("text_entered", this,"_file_entered");
  549. filter->connect("item_selected", this,"_filter_selected");
  550. confirm_save = memnew( ConfirmationDialog );
  551. confirm_save->set_as_toplevel(true);
  552. add_child(confirm_save);
  553. confirm_save->connect("confirmed", this,"_save_confirm_pressed");
  554. makedialog = memnew( ConfirmationDialog );
  555. makedialog->set_title("Create Folder");
  556. VBoxContainer *makevb= memnew( VBoxContainer );
  557. makedialog->add_child(makevb);
  558. makedialog->set_child_rect(makevb);
  559. makedirname = memnew( LineEdit );
  560. makevb->add_margin_child("Name:",makedirname);
  561. add_child(makedialog);
  562. makedialog->register_text_enter(makedirname);
  563. makedialog->connect("confirmed",this,"_make_dir_confirm");
  564. mkdirerr = memnew( AcceptDialog );
  565. mkdirerr->set_text("Could not create folder.");
  566. add_child(mkdirerr);
  567. exterr = memnew( AcceptDialog );
  568. exterr->set_text("Must use a valid extension.");
  569. add_child(exterr);
  570. //update_file_list();
  571. update_filters();
  572. update_dir();
  573. set_hide_on_ok(false);
  574. vbox=vbc;
  575. invalidated=true;
  576. if (register_func)
  577. register_func(this);
  578. }
  579. FileDialog::~FileDialog() {
  580. if (unregister_func)
  581. unregister_func(this);
  582. memdelete(dir_access);
  583. }
  584. void LineEditFileChooser::_bind_methods() {
  585. ObjectTypeDB::bind_method(_MD("_browse"),&LineEditFileChooser::_browse);
  586. ObjectTypeDB::bind_method(_MD("_chosen"),&LineEditFileChooser::_chosen);
  587. ObjectTypeDB::bind_method(_MD("get_button:Button"),&LineEditFileChooser::get_button);
  588. ObjectTypeDB::bind_method(_MD("get_line_edit:LineEdit"),&LineEditFileChooser::get_line_edit);
  589. ObjectTypeDB::bind_method(_MD("get_file_dialog:FileDialog"),&LineEditFileChooser::get_file_dialog);
  590. }
  591. void LineEditFileChooser::_chosen(const String& p_text){
  592. line_edit->set_text(p_text);
  593. line_edit->emit_signal("text_entered",p_text);
  594. }
  595. void LineEditFileChooser::_browse() {
  596. dialog->popup_centered_ratio();
  597. }
  598. LineEditFileChooser::LineEditFileChooser() {
  599. line_edit = memnew( LineEdit );
  600. add_child(line_edit);
  601. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  602. button = memnew( Button );
  603. button->set_text(" .. ");
  604. add_child(button);
  605. button->connect("pressed",this,"_browse");
  606. dialog = memnew( FileDialog);
  607. add_child(dialog);
  608. dialog->connect("file_selected",this,"_chosen");
  609. dialog->connect("dir_selected",this,"_chosen");
  610. dialog->connect("files_selected",this,"_chosen");
  611. }