file_dialog.cpp 20 KB

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