project_manager.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /*************************************************************************/
  2. /* project_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "project_manager.h"
  31. #include "editor_settings.h"
  32. #include "io/config_file.h"
  33. #include "os/dir_access.h"
  34. #include "os/file_access.h"
  35. #include "os/keyboard.h"
  36. #include "os/os.h"
  37. #include "scene/gui/separator.h"
  38. #include "scene/gui/tool_button.h"
  39. #include "version.h"
  40. #include "io/stream_peer_ssl.h"
  41. #include "scene/gui/center_container.h"
  42. #include "scene/gui/line_edit.h"
  43. #include "scene/gui/panel_container.h"
  44. #include "io/resource_saver.h"
  45. #include "scene/gui/margin_container.h"
  46. #include "scene/gui/texture_frame.h"
  47. #include "editor_initialize_ssl.h"
  48. #include "editor_scale.h"
  49. #include "editor_themes.h"
  50. #include "io/zip_io.h"
  51. class NewProjectDialog : public ConfirmationDialog {
  52. OBJ_TYPE(NewProjectDialog, ConfirmationDialog);
  53. public:
  54. enum Mode {
  55. MODE_NEW,
  56. MODE_IMPORT,
  57. MODE_INSTALL
  58. };
  59. private:
  60. Mode mode;
  61. Label *pp, *pn;
  62. Label *error;
  63. LineEdit *project_path;
  64. LineEdit *project_name;
  65. FileDialog *fdialog;
  66. String zip_path;
  67. String zip_title;
  68. AcceptDialog *dialog_error;
  69. String _test_path() {
  70. error->set_text("");
  71. get_ok()->set_disabled(true);
  72. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  73. String valid_path;
  74. if (d->change_dir(project_path->get_text()) == OK) {
  75. valid_path = project_path->get_text();
  76. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  77. valid_path = project_path->get_text().strip_edges();
  78. }
  79. if (valid_path == "") {
  80. error->set_text(TTR("Invalid project path, the path must exist!"));
  81. memdelete(d);
  82. return "";
  83. }
  84. if (mode != MODE_IMPORT) {
  85. if (d->file_exists("engine.cfg")) {
  86. error->set_text(TTR("Invalid project path, engine.cfg must not exist."));
  87. memdelete(d);
  88. return "";
  89. }
  90. } else {
  91. if (valid_path != "" && !d->file_exists("engine.cfg")) {
  92. error->set_text(TTR("Invalid project path, engine.cfg must exist."));
  93. memdelete(d);
  94. return "";
  95. }
  96. }
  97. memdelete(d);
  98. get_ok()->set_disabled(false);
  99. return valid_path;
  100. }
  101. void _path_text_changed(const String &p_path) {
  102. String sp = _test_path();
  103. if (sp != "") {
  104. sp = sp.replace("\\", "/");
  105. int lidx = sp.find_last("/");
  106. if (lidx != -1) {
  107. sp = sp.substr(lidx + 1, sp.length());
  108. }
  109. if (sp == "" && mode == MODE_IMPORT)
  110. sp = TTR("Imported Project");
  111. project_name->set_text(sp);
  112. }
  113. }
  114. void _file_selected(const String &p_path) {
  115. String p = p_path;
  116. if (mode == MODE_IMPORT) {
  117. if (p.ends_with("engine.cfg")) {
  118. p = p.get_base_dir();
  119. }
  120. }
  121. String sp = p.simplify_path();
  122. project_path->set_text(sp);
  123. _path_text_changed(sp);
  124. get_ok()->call_deferred("grab_focus");
  125. }
  126. void _path_selected(const String &p_path) {
  127. String p = p_path;
  128. String sp = p.simplify_path();
  129. project_path->set_text(sp);
  130. _path_text_changed(sp);
  131. get_ok()->call_deferred("grab_focus");
  132. }
  133. void _browse_path() {
  134. if (mode == MODE_IMPORT) {
  135. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  136. fdialog->clear_filters();
  137. fdialog->add_filter("engine.cfg ; " _MKSTR(VERSION_NAME) " Project");
  138. } else {
  139. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  140. }
  141. fdialog->popup_centered_ratio();
  142. }
  143. void _text_changed(const String &p_text) {
  144. _test_path();
  145. }
  146. void ok_pressed() {
  147. String dir = _test_path();
  148. if (dir == "") {
  149. error->set_text(TTR("Invalid project path (changed anything?)."));
  150. return;
  151. }
  152. if (mode == MODE_IMPORT) {
  153. // nothing to do
  154. } else {
  155. if (mode == MODE_NEW) {
  156. FileAccess *f = FileAccess::open(dir.plus_file("/engine.cfg"), FileAccess::WRITE);
  157. if (!f) {
  158. error->set_text(TTR("Couldn't create engine.cfg in project path."));
  159. } else {
  160. f->store_line("; Engine configuration file.");
  161. f->store_line("; It's best edited using the editor UI and not directly,");
  162. f->store_line("; since the parameters that go here are not all obvious.");
  163. f->store_line("; ");
  164. f->store_line("; Format: ");
  165. f->store_line("; [section] ; section goes between []");
  166. f->store_line("; param=value ; assign values to parameters");
  167. f->store_line("\n");
  168. f->store_line("[application]");
  169. f->store_line("\n");
  170. f->store_line("name=\"" + project_name->get_text() + "\"");
  171. f->store_line("icon=\"res://icon.png\"");
  172. f->store_line("\n");
  173. f->store_line("[physics_2d]");
  174. f->store_line("\n");
  175. f->store_line("motion_fix_enabled=true");
  176. memdelete(f);
  177. ResourceSaver::save(dir.plus_file("/icon.png"), get_icon("DefaultProjectIcon", "EditorIcons"));
  178. }
  179. } else if (mode == MODE_INSTALL) {
  180. FileAccess *src_f = NULL;
  181. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  182. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  183. if (!pkg) {
  184. dialog_error->set_text("Error opening package file, not in zip format.");
  185. return;
  186. }
  187. int ret = unzGoToFirstFile(pkg);
  188. Vector<String> failed_files;
  189. int idx = 0;
  190. while (ret == UNZ_OK) {
  191. //get filename
  192. unz_file_info info;
  193. char fname[16384];
  194. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  195. String path = fname;
  196. int depth = 1; //stuff from github comes with tag
  197. bool skip = false;
  198. while (depth > 0) {
  199. int pp = path.find("/");
  200. if (pp == -1) {
  201. skip = true;
  202. break;
  203. }
  204. path = path.substr(pp + 1, path.length());
  205. depth--;
  206. }
  207. if (skip || path == String()) {
  208. //
  209. } else if (path.ends_with("/")) { // a dir
  210. path = path.substr(0, path.length() - 1);
  211. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  212. da->make_dir(dir.plus_file(path));
  213. memdelete(da);
  214. } else {
  215. Vector<uint8_t> data;
  216. data.resize(info.uncompressed_size);
  217. //read
  218. unzOpenCurrentFile(pkg);
  219. unzReadCurrentFile(pkg, data.ptr(), data.size());
  220. unzCloseCurrentFile(pkg);
  221. FileAccess *f = FileAccess::open(dir.plus_file(path), FileAccess::WRITE);
  222. if (f) {
  223. f->store_buffer(data.ptr(), data.size());
  224. memdelete(f);
  225. } else {
  226. failed_files.push_back(path);
  227. }
  228. }
  229. idx++;
  230. ret = unzGoToNextFile(pkg);
  231. }
  232. unzClose(pkg);
  233. if (failed_files.size()) {
  234. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  235. for (int i = 0; i < failed_files.size(); i++) {
  236. if (i > 15) {
  237. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  238. break;
  239. }
  240. msg += failed_files[i] + "\n";
  241. }
  242. dialog_error->set_text(msg);
  243. dialog_error->popup_centered_minsize();
  244. } else {
  245. dialog_error->set_text(TTR("Package Installed Successfully!"));
  246. dialog_error->popup_centered_minsize();
  247. }
  248. }
  249. }
  250. dir = dir.replace("\\", "/");
  251. if (dir.ends_with("/"))
  252. dir = dir.substr(0, dir.length() - 1);
  253. String proj = dir.replace("/", "::");
  254. EditorSettings::get_singleton()->set("projects/" + proj, dir);
  255. EditorSettings::get_singleton()->save();
  256. hide();
  257. emit_signal("project_created", dir);
  258. }
  259. protected:
  260. static void _bind_methods() {
  261. ObjectTypeDB::bind_method("_browse_path", &NewProjectDialog::_browse_path);
  262. ObjectTypeDB::bind_method("_text_changed", &NewProjectDialog::_text_changed);
  263. ObjectTypeDB::bind_method("_path_text_changed", &NewProjectDialog::_path_text_changed);
  264. ObjectTypeDB::bind_method("_path_selected", &NewProjectDialog::_path_selected);
  265. ObjectTypeDB::bind_method("_file_selected", &NewProjectDialog::_file_selected);
  266. ADD_SIGNAL(MethodInfo("project_created"));
  267. }
  268. public:
  269. void set_zip_path(const String &p_path) {
  270. zip_path = p_path;
  271. }
  272. void set_zip_title(const String &p_title) {
  273. zip_title = p_title;
  274. }
  275. void set_mode(Mode p_mode) {
  276. mode = p_mode;
  277. }
  278. void show_dialog() {
  279. project_path->clear();
  280. project_name->clear();
  281. if (mode == MODE_IMPORT) {
  282. set_title(TTR("Import Existing Project"));
  283. get_ok()->set_text(TTR("Import"));
  284. pp->set_text(TTR("Project Path (Must Exist):"));
  285. pn->set_text(TTR("Project Name:"));
  286. pn->hide();
  287. project_name->hide();
  288. popup_centered(Size2(500, 125) * EDSCALE);
  289. } else if (mode == MODE_NEW) {
  290. set_title(TTR("Create New Project"));
  291. get_ok()->set_text(TTR("Create"));
  292. pp->set_text(TTR("Project Path:"));
  293. pn->set_text(TTR("Project Name:"));
  294. pn->show();
  295. project_name->show();
  296. popup_centered(Size2(500, 145) * EDSCALE);
  297. } else if (mode == MODE_INSTALL) {
  298. set_title(TTR("Install Project:") + " " + zip_title);
  299. get_ok()->set_text(TTR("Install"));
  300. pp->set_text(TTR("Project Path:"));
  301. pn->hide();
  302. project_name->hide();
  303. popup_centered(Size2(500, 125) * EDSCALE);
  304. }
  305. project_path->grab_focus();
  306. _test_path();
  307. }
  308. NewProjectDialog() {
  309. VBoxContainer *vb = memnew(VBoxContainer);
  310. add_child(vb);
  311. set_child_rect(vb);
  312. Label *l = memnew(Label);
  313. l->set_text(TTR("Project Path:"));
  314. vb->add_child(l);
  315. pp = l;
  316. project_path = memnew(LineEdit);
  317. MarginContainer *mc = memnew(MarginContainer);
  318. vb->add_child(mc);
  319. HBoxContainer *pphb = memnew(HBoxContainer);
  320. mc->add_child(pphb);
  321. pphb->add_child(project_path);
  322. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  323. Button *browse = memnew(Button);
  324. pphb->add_child(browse);
  325. browse->set_text(TTR("Browse"));
  326. browse->connect("pressed", this, "_browse_path");
  327. l = memnew(Label);
  328. l->set_text(TTR("Project Name:"));
  329. l->set_pos(Point2(5, 50));
  330. vb->add_child(l);
  331. pn = l;
  332. project_name = memnew(LineEdit);
  333. mc = memnew(MarginContainer);
  334. vb->add_child(mc);
  335. mc->add_child(project_name);
  336. project_name->set_text(TTR("New Game Project"));
  337. l = memnew(Label);
  338. l->set_text(TTR("That's a BINGO!"));
  339. vb->add_child(l);
  340. error = l;
  341. l->add_color_override("font_color", Color(1, 0.4, 0.3, 0.8));
  342. l->set_align(Label::ALIGN_CENTER);
  343. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  344. project_path->set_text(d->get_current_dir());
  345. memdelete(d);
  346. fdialog = memnew(FileDialog);
  347. add_child(fdialog);
  348. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  349. fdialog->set_current_dir(EditorSettings::get_singleton()->get("global/default_project_path"));
  350. project_name->connect("text_changed", this, "_text_changed");
  351. project_path->connect("text_changed", this, "_path_text_changed");
  352. fdialog->connect("dir_selected", this, "_path_selected");
  353. fdialog->connect("file_selected", this, "_file_selected");
  354. set_hide_on_ok(false);
  355. mode = MODE_NEW;
  356. dialog_error = memnew(AcceptDialog);
  357. add_child(dialog_error);
  358. }
  359. };
  360. struct ProjectItem {
  361. String project;
  362. String path;
  363. String conf;
  364. uint64_t last_modified;
  365. bool favorite;
  366. ProjectItem() {}
  367. ProjectItem(const String &p_project, const String &p_path, const String &p_conf, uint64_t p_last_modified, bool p_favorite = false) {
  368. project = p_project;
  369. path = p_path;
  370. conf = p_conf;
  371. last_modified = p_last_modified;
  372. favorite = p_favorite;
  373. }
  374. _FORCE_INLINE_ bool operator<(const ProjectItem &l) const { return last_modified > l.last_modified; }
  375. _FORCE_INLINE_ bool operator==(const ProjectItem &l) const { return project == l.project; }
  376. };
  377. void ProjectManager::_notification(int p_what) {
  378. if (p_what == NOTIFICATION_ENTER_TREE) {
  379. get_tree()->set_editor_hint(true);
  380. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  381. set_process_unhandled_input(is_visible());
  382. }
  383. }
  384. void ProjectManager::_panel_draw(Node *p_hb) {
  385. HBoxContainer *hb = p_hb->cast_to<HBoxContainer>();
  386. hb->draw_line(Point2(0, hb->get_size().y + 1), Point2(hb->get_size().x - 10, hb->get_size().y + 1), get_color("guide_color", "Tree"));
  387. if (selected_list.has(hb->get_meta("name"))) {
  388. hb->draw_style_box(get_stylebox("selected", "Tree"), Rect2(Point2(), hb->get_size() - Size2(10, 0)));
  389. }
  390. }
  391. void ProjectManager::_update_project_buttons() {
  392. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  393. CanvasItem *item = scroll_childs->get_child(i)->cast_to<CanvasItem>();
  394. item->update();
  395. }
  396. bool has_runnable_scene = false;
  397. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  398. const String &selected_main = E->get();
  399. if (selected_main == "") continue;
  400. has_runnable_scene = true;
  401. break;
  402. }
  403. show_btn->set_disabled(selected_list.size() < 1);
  404. erase_btn->set_disabled(selected_list.size() < 1);
  405. open_btn->set_disabled(selected_list.size() < 1);
  406. run_btn->set_disabled(!has_runnable_scene);
  407. }
  408. void ProjectManager::_panel_input(const InputEvent &p_ev, Node *p_hb) {
  409. if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.pressed && p_ev.mouse_button.button_index == BUTTON_LEFT) {
  410. String clicked = p_hb->get_meta("name");
  411. String clicked_main_scene = p_hb->get_meta("main_scene");
  412. if (p_ev.key.mod.shift && selected_list.size() > 0 && last_clicked != "" && clicked != last_clicked) {
  413. int clicked_id = -1;
  414. int last_clicked_id = -1;
  415. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  416. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  417. if (!hb) continue;
  418. if (hb->get_meta("name") == clicked) clicked_id = i;
  419. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  420. }
  421. if (last_clicked_id != -1 && clicked_id != -1) {
  422. int min = clicked_id < last_clicked_id ? clicked_id : last_clicked_id;
  423. int max = clicked_id > last_clicked_id ? clicked_id : last_clicked_id;
  424. for (int i = 0; i < scroll_childs->get_child_count(); ++i) {
  425. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  426. if (!hb) continue;
  427. if (i != clicked_id && (i < min || i > max) && !p_ev.key.mod.control) {
  428. selected_list.erase(hb->get_meta("name"));
  429. } else if (i >= min && i <= max) {
  430. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  431. }
  432. }
  433. }
  434. } else if (selected_list.has(clicked) && p_ev.key.mod.control) {
  435. selected_list.erase(clicked);
  436. } else {
  437. last_clicked = clicked;
  438. if (p_ev.key.mod.control || selected_list.size() == 0) {
  439. selected_list.insert(clicked, clicked_main_scene);
  440. } else {
  441. selected_list.clear();
  442. selected_list.insert(clicked, clicked_main_scene);
  443. }
  444. }
  445. _update_project_buttons();
  446. if (p_ev.mouse_button.doubleclick)
  447. _open_project(); //open if doubleclicked
  448. }
  449. }
  450. void ProjectManager::_unhandled_input(const InputEvent &p_ev) {
  451. if (p_ev.type == InputEvent::KEY) {
  452. const InputEventKey &k = p_ev.key;
  453. if (!k.pressed)
  454. return;
  455. if (tabs->get_current_tab() != 0)
  456. return;
  457. bool scancode_handled = true;
  458. switch (k.scancode) {
  459. case KEY_RETURN: {
  460. _open_project();
  461. } break;
  462. case KEY_HOME: {
  463. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  464. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  465. if (hb) {
  466. selected_list.clear();
  467. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  468. scroll->set_v_scroll(0);
  469. _update_project_buttons();
  470. break;
  471. }
  472. }
  473. } break;
  474. case KEY_END: {
  475. for (int i = scroll_childs->get_child_count() - 1; i >= 0; i--) {
  476. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  477. if (hb) {
  478. selected_list.clear();
  479. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  480. scroll->set_v_scroll(scroll_childs->get_size().y);
  481. _update_project_buttons();
  482. break;
  483. }
  484. }
  485. } break;
  486. case KEY_UP: {
  487. if (k.mod.shift)
  488. break;
  489. if (selected_list.size()) {
  490. bool found = false;
  491. for (int i = scroll_childs->get_child_count() - 1; i >= 0; i--) {
  492. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  493. if (!hb) continue;
  494. String current = hb->get_meta("name");
  495. if (found) {
  496. selected_list.clear();
  497. selected_list.insert(current, hb->get_meta("main_scene"));
  498. int offset_diff = scroll->get_v_scroll() - hb->get_pos().y;
  499. if (offset_diff > 0)
  500. scroll->set_v_scroll(scroll->get_v_scroll() - offset_diff);
  501. _update_project_buttons();
  502. break;
  503. } else if (current == selected_list.back()->key()) {
  504. found = true;
  505. }
  506. }
  507. break;
  508. }
  509. // else fallthrough to key_down
  510. }
  511. case KEY_DOWN: {
  512. if (k.mod.shift)
  513. break;
  514. bool found = selected_list.empty();
  515. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  516. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  517. if (!hb) continue;
  518. String current = hb->get_meta("name");
  519. if (found) {
  520. selected_list.clear();
  521. selected_list.insert(current, hb->get_meta("main_scene"));
  522. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  523. int offset_diff = (hb->get_pos().y + hb->get_size().y) - last_y_visible;
  524. if (offset_diff > 0)
  525. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  526. _update_project_buttons();
  527. break;
  528. } else if (current == selected_list.back()->key()) {
  529. found = true;
  530. }
  531. }
  532. } break;
  533. case KEY_F: {
  534. if (k.mod.command)
  535. this->project_filter->search_box->grab_focus();
  536. else
  537. scancode_handled = false;
  538. } break;
  539. default: {
  540. scancode_handled = false;
  541. } break;
  542. }
  543. if (scancode_handled) {
  544. accept_event();
  545. }
  546. }
  547. }
  548. void ProjectManager::_favorite_pressed(Node *p_hb) {
  549. String clicked = p_hb->get_meta("name");
  550. bool favorite = !p_hb->get_meta("favorite");
  551. String proj = clicked.replace(":::", ":/");
  552. proj = proj.replace("::", "/");
  553. if (favorite) {
  554. EditorSettings::get_singleton()->set("favorite_projects/" + clicked, proj);
  555. } else {
  556. EditorSettings::get_singleton()->erase("favorite_projects/" + clicked);
  557. }
  558. EditorSettings::get_singleton()->save();
  559. call_deferred("_load_recent_projects");
  560. }
  561. void ProjectManager::_load_recent_projects() {
  562. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  563. String search_term = project_filter->get_search_term();
  564. while (scroll_childs->get_child_count() > 0) {
  565. memdelete(scroll_childs->get_child(0));
  566. }
  567. Map<String, String> selected_list_copy = selected_list;
  568. List<PropertyInfo> properties;
  569. EditorSettings::get_singleton()->get_property_list(&properties);
  570. Color font_color = get_color("font_color", "Tree");
  571. List<ProjectItem> projects;
  572. List<ProjectItem> favorite_projects;
  573. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  574. String _name = E->get().name;
  575. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  576. continue;
  577. String path = EditorSettings::get_singleton()->get(_name);
  578. if (filter_option == ProjectListFilter::FILTER_PATH && search_term != "" && path.findn(search_term) == -1)
  579. continue;
  580. String project = _name.get_slice("/", 1);
  581. String conf = path.plus_file("engine.cfg");
  582. bool favorite = (_name.begins_with("favorite_projects/")) ? true : false;
  583. uint64_t last_modified = 0;
  584. if (FileAccess::exists(conf)) {
  585. last_modified = FileAccess::get_modified_time(conf);
  586. String fscache = path.plus_file(".fscache");
  587. if (FileAccess::exists(fscache)) {
  588. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  589. if (cache_modified > last_modified)
  590. last_modified = cache_modified;
  591. }
  592. ProjectItem item(project, path, conf, last_modified, favorite);
  593. if (favorite)
  594. favorite_projects.push_back(item);
  595. else
  596. projects.push_back(item);
  597. } else {
  598. //project doesn't exist on disk but it's in the XML settings file
  599. EditorSettings::get_singleton()->erase(_name); //remove it
  600. }
  601. }
  602. projects.sort();
  603. favorite_projects.sort();
  604. for (List<ProjectItem>::Element *E = projects.front(); E;) {
  605. List<ProjectItem>::Element *next = E->next();
  606. if (favorite_projects.find(E->get()) != NULL)
  607. projects.erase(E->get());
  608. E = next;
  609. }
  610. for (List<ProjectItem>::Element *E = favorite_projects.back(); E; E = E->prev()) {
  611. projects.push_front(E->get());
  612. }
  613. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  614. for (List<ProjectItem>::Element *E = projects.front(); E; E = E->next()) {
  615. ProjectItem &item = E->get();
  616. String project = item.project;
  617. String path = item.path;
  618. String conf = item.conf;
  619. bool is_favorite = item.favorite;
  620. Ref<ConfigFile> cf = memnew(ConfigFile);
  621. Error err = cf->load(conf);
  622. ERR_CONTINUE(err != OK);
  623. String project_name = TTR("Unnamed Project");
  624. if (cf->has_section_key("application", "name")) {
  625. project_name = static_cast<String>(cf->get_value("application", "name")).xml_unescape();
  626. }
  627. if (filter_option == ProjectListFilter::FILTER_NAME && search_term != "" && project_name.findn(search_term) == -1)
  628. continue;
  629. Ref<Texture> icon;
  630. if (cf->has_section_key("application", "icon")) {
  631. String appicon = cf->get_value("application", "icon");
  632. if (appicon != "") {
  633. Image img;
  634. Error err = img.load(appicon.replace_first("res://", path + "/"));
  635. if (err == OK) {
  636. img.resize(64, 64);
  637. Ref<ImageTexture> it = memnew(ImageTexture);
  638. it->create_from_image(img);
  639. icon = it;
  640. }
  641. }
  642. }
  643. if (icon.is_null()) {
  644. icon = get_icon("DefaultProjectIcon", "EditorIcons");
  645. }
  646. String main_scene;
  647. if (cf->has_section_key("application", "main_scene")) {
  648. main_scene = cf->get_value("application", "main_scene");
  649. }
  650. selected_list_copy.erase(project);
  651. HBoxContainer *hb = memnew(HBoxContainer);
  652. hb->set_meta("name", project);
  653. hb->set_meta("main_scene", main_scene);
  654. hb->set_meta("favorite", is_favorite);
  655. hb->connect("draw", this, "_panel_draw", varray(hb));
  656. hb->connect("input_event", this, "_panel_input", varray(hb));
  657. VBoxContainer *favorite_box = memnew(VBoxContainer);
  658. TextureButton *favorite = memnew(TextureButton);
  659. favorite->set_normal_texture(favorite_icon);
  660. if (!is_favorite)
  661. favorite->set_opacity(0.2);
  662. favorite->set_v_size_flags(SIZE_EXPAND);
  663. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  664. favorite_box->add_child(favorite);
  665. hb->add_child(favorite_box);
  666. TextureFrame *tf = memnew(TextureFrame);
  667. tf->set_texture(icon);
  668. tf->set_v_size_flags(SIZE_EXPAND);
  669. hb->add_child(tf);
  670. VBoxContainer *vb = memnew(VBoxContainer);
  671. vb->set_name("project");
  672. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  673. hb->add_child(vb);
  674. Control *ec = memnew(Control);
  675. ec->set_custom_minimum_size(Size2(0, 1));
  676. vb->add_child(ec);
  677. Label *title = memnew(Label(project_name));
  678. title->add_font_override("font", get_font("large", "Fonts"));
  679. title->add_color_override("font_color", font_color);
  680. title->set_clip_text(true);
  681. vb->add_child(title);
  682. Label *fpath = memnew(Label(path));
  683. fpath->set_name("path");
  684. vb->add_child(fpath);
  685. fpath->set_opacity(0.5);
  686. fpath->add_color_override("font_color", font_color);
  687. fpath->set_clip_text(true);
  688. scroll_childs->add_child(hb);
  689. }
  690. for (Map<String, String>::Element *E = selected_list_copy.front(); E; E = E->next()) {
  691. String key = E->key();
  692. selected_list.erase(key);
  693. }
  694. scroll->set_v_scroll(0);
  695. _update_project_buttons();
  696. EditorSettings::get_singleton()->save();
  697. tabs->set_current_tab(0);
  698. }
  699. void ProjectManager::_on_project_created(const String &dir) {
  700. bool has_already = false;
  701. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  702. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  703. Label *fpath = hb->get_node(NodePath("project/path"))->cast_to<Label>();
  704. if (fpath->get_text() == dir) {
  705. has_already = true;
  706. break;
  707. }
  708. }
  709. if (has_already) {
  710. _update_scroll_pos(dir);
  711. } else {
  712. _load_recent_projects();
  713. _update_scroll_pos(dir);
  714. }
  715. _open_project();
  716. }
  717. void ProjectManager::_update_scroll_pos(const String &dir) {
  718. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  719. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  720. Label *fpath = hb->get_node(NodePath("project/path"))->cast_to<Label>();
  721. if (fpath->get_text() == dir) {
  722. last_clicked = hb->get_meta("name");
  723. selected_list.clear();
  724. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  725. _update_project_buttons();
  726. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  727. int offset_diff = (hb->get_pos().y + hb->get_size().y) - last_y_visible;
  728. if (offset_diff > 0)
  729. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  730. break;
  731. }
  732. }
  733. }
  734. void ProjectManager::_open_project_confirm() {
  735. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  736. const String &selected = E->key();
  737. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  738. print_line("Opening project: " + path + " (" + selected + ")");
  739. List<String> args;
  740. args.push_back("-path");
  741. args.push_back(path);
  742. args.push_back("-editor");
  743. if (OS::get_singleton()->is_disable_crash_handler()) {
  744. args.push_back("--disable-crash-handler");
  745. }
  746. String exec = OS::get_singleton()->get_executable_path();
  747. OS::ProcessID pid = 0;
  748. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  749. ERR_FAIL_COND(err);
  750. }
  751. get_tree()->quit();
  752. }
  753. void ProjectManager::_open_project() {
  754. if (selected_list.size() < 1) {
  755. return;
  756. }
  757. if (selected_list.size() > 1) {
  758. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  759. multi_open_ask->popup_centered_minsize();
  760. } else {
  761. _open_project_confirm();
  762. }
  763. }
  764. void ProjectManager::_run_project_confirm() {
  765. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  766. const String &selected_main = E->get();
  767. if (selected_main == "") continue;
  768. const String &selected = E->key();
  769. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  770. print_line("Opening project: " + path + " (" + selected + ")");
  771. List<String> args;
  772. args.push_back("-path");
  773. args.push_back(path);
  774. if (OS::get_singleton()->is_disable_crash_handler()) {
  775. args.push_back("--disable-crash-handler");
  776. }
  777. String exec = OS::get_singleton()->get_executable_path();
  778. OS::ProcessID pid = 0;
  779. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  780. ERR_FAIL_COND(err);
  781. }
  782. // get_scene()->quit(); do not quit
  783. }
  784. void ProjectManager::_run_project() {
  785. if (selected_list.size() < 1) {
  786. return;
  787. }
  788. if (selected_list.size() > 1) {
  789. multi_run_ask->set_text(TTR("Are you sure to run more than one project?"));
  790. multi_run_ask->popup_centered_minsize();
  791. } else {
  792. _run_project_confirm();
  793. }
  794. }
  795. void ProjectManager::_scan_dir(DirAccess *da, float pos, float total, List<String> *r_projects) {
  796. List<String> subdirs;
  797. da->list_dir_begin();
  798. String n = da->get_next();
  799. while (n != String()) {
  800. if (da->current_is_dir() && !n.begins_with(".")) {
  801. subdirs.push_front(n);
  802. } else if (n == "engine.cfg") {
  803. r_projects->push_back(da->get_current_dir());
  804. }
  805. n = da->get_next();
  806. }
  807. da->list_dir_end();
  808. int m = 0;
  809. for (List<String>::Element *E = subdirs.front(); E; E = E->next()) {
  810. da->change_dir(E->get());
  811. float slice = total / subdirs.size();
  812. _scan_dir(da, pos + slice * m, slice, r_projects);
  813. da->change_dir("..");
  814. m++;
  815. }
  816. }
  817. void ProjectManager::_scan_begin(const String &p_base) {
  818. print_line("Scan project at: " + p_base);
  819. List<String> projects;
  820. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  821. da->change_dir(p_base);
  822. _scan_dir(da, 0, 1, &projects);
  823. memdelete(da);
  824. print_line("found: " + itos(projects.size()) + " projects.");
  825. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  826. String proj = E->get().replace("/", "::");
  827. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  828. }
  829. EditorSettings::get_singleton()->save();
  830. _load_recent_projects();
  831. }
  832. void ProjectManager::_scan_projects() {
  833. scan_dir->popup_centered_ratio();
  834. }
  835. void ProjectManager::_new_project() {
  836. npdialog->set_mode(NewProjectDialog::MODE_NEW);
  837. npdialog->show_dialog();
  838. }
  839. void ProjectManager::_import_project() {
  840. npdialog->set_mode(NewProjectDialog::MODE_IMPORT);
  841. npdialog->show_dialog();
  842. }
  843. void ProjectManager::_erase_project_confirm() {
  844. if (selected_list.size() == 0) {
  845. return;
  846. }
  847. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  848. EditorSettings::get_singleton()->erase("projects/" + E->key());
  849. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  850. }
  851. EditorSettings::get_singleton()->save();
  852. selected_list.clear();
  853. last_clicked = "";
  854. _load_recent_projects();
  855. }
  856. void ProjectManager::_erase_project() {
  857. if (selected_list.size() == 0)
  858. return;
  859. erase_ask->set_text(TTR("Remove project from the list? (Folder contents will not be modified)"));
  860. erase_ask->popup_centered_minsize();
  861. }
  862. void ProjectManager::_show_project() {
  863. if (selected_list.size() == 0)
  864. return;
  865. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  866. const String &selected = E->key();
  867. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  868. String conf = path + "/engine.cfg";
  869. if (!FileAccess::exists(conf)) {
  870. multi_open_ask->set_text(TTR("Can't show project"));
  871. multi_open_ask->popup_centered_minsize();
  872. return;
  873. }
  874. OS::get_singleton()->shell_open(String("file://") + path);
  875. }
  876. }
  877. void ProjectManager::_exit_dialog() {
  878. get_tree()->quit();
  879. }
  880. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  881. npdialog->set_mode(NewProjectDialog::MODE_INSTALL);
  882. npdialog->set_zip_path(p_zip_path);
  883. npdialog->set_zip_title(p_title);
  884. npdialog->show_dialog();
  885. }
  886. void ProjectManager::_files_dropped(StringArray p_files, int p_screen) {
  887. Set<String> folders_set;
  888. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  889. for (int i = 0; i < p_files.size(); i++) {
  890. String file = p_files[i];
  891. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  892. }
  893. memdelete(da);
  894. if (folders_set.size() > 0) {
  895. StringArray folders;
  896. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  897. folders.append(E->get());
  898. }
  899. bool confirm = true;
  900. if (folders.size() == 1) {
  901. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  902. if (dir->change_dir(folders[0]) == OK) {
  903. dir->list_dir_begin();
  904. String file = dir->get_next();
  905. while (confirm && file != String()) {
  906. if (!dir->current_is_dir() && file.ends_with("engine.cfg")) {
  907. confirm = false;
  908. }
  909. file = dir->get_next();
  910. }
  911. dir->list_dir_end();
  912. }
  913. memdelete(dir);
  914. }
  915. if (confirm) {
  916. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  917. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  918. multi_scan_ask->set_text(vformat(TTR("You are about the scan %s folders for existing Godot projects. Do you confirm?"), folders.size()));
  919. multi_scan_ask->popup_centered_minsize();
  920. } else {
  921. _scan_multiple_folders(folders);
  922. }
  923. }
  924. }
  925. void ProjectManager::_scan_multiple_folders(StringArray p_files) {
  926. for (int i = 0; i < p_files.size(); i++) {
  927. _scan_begin(p_files.get(i));
  928. }
  929. }
  930. void ProjectManager::_bind_methods() {
  931. ObjectTypeDB::bind_method("_open_project", &ProjectManager::_open_project);
  932. ObjectTypeDB::bind_method("_open_project_confirm", &ProjectManager::_open_project_confirm);
  933. ObjectTypeDB::bind_method("_run_project", &ProjectManager::_run_project);
  934. ObjectTypeDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  935. ObjectTypeDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  936. ObjectTypeDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  937. ObjectTypeDB::bind_method("_import_project", &ProjectManager::_import_project);
  938. ObjectTypeDB::bind_method("_new_project", &ProjectManager::_new_project);
  939. ObjectTypeDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  940. ObjectTypeDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  941. ObjectTypeDB::bind_method("_show_project", &ProjectManager::_show_project);
  942. ObjectTypeDB::bind_method("_exit_dialog", &ProjectManager::_exit_dialog);
  943. ObjectTypeDB::bind_method("_load_recent_projects", &ProjectManager::_load_recent_projects);
  944. ObjectTypeDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  945. ObjectTypeDB::bind_method("_update_scroll_pos", &ProjectManager::_update_scroll_pos);
  946. ObjectTypeDB::bind_method("_panel_draw", &ProjectManager::_panel_draw);
  947. ObjectTypeDB::bind_method("_panel_input", &ProjectManager::_panel_input);
  948. ObjectTypeDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  949. ObjectTypeDB::bind_method("_favorite_pressed", &ProjectManager::_favorite_pressed);
  950. ObjectTypeDB::bind_method("_install_project", &ProjectManager::_install_project);
  951. ObjectTypeDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  952. ObjectTypeDB::bind_method(_MD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  953. }
  954. ProjectManager::ProjectManager() {
  955. // load settings
  956. if (!EditorSettings::get_singleton())
  957. EditorSettings::create();
  958. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  959. {
  960. int dpi_mode = EditorSettings::get_singleton()->get("global/hidpi_mode");
  961. if (dpi_mode == 0) {
  962. editor_set_hidpi(OS::get_singleton()->get_screen_dpi(0) >= 192 && OS::get_singleton()->get_screen_size(OS::get_singleton()->get_current_screen()).x > 2000);
  963. } else if (dpi_mode == 2) {
  964. editor_set_hidpi(true);
  965. } else {
  966. editor_set_hidpi(false);
  967. }
  968. }
  969. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("file_dialog/show_hidden_files"));
  970. set_area_as_parent_rect();
  971. set_theme(create_default_theme());
  972. gui_base = memnew(Control);
  973. add_child(gui_base);
  974. gui_base->set_area_as_parent_rect();
  975. Panel *panel = memnew(Panel);
  976. gui_base->add_child(panel);
  977. panel->set_area_as_parent_rect();
  978. VBoxContainer *vb = memnew(VBoxContainer);
  979. panel->add_child(vb);
  980. vb->set_area_as_parent_rect(20 * EDSCALE);
  981. vb->set_margin(MARGIN_TOP, 4 * EDSCALE);
  982. vb->set_margin(MARGIN_BOTTOM, 4 * EDSCALE);
  983. vb->add_constant_override("separation", 15 * EDSCALE);
  984. String cp;
  985. cp.push_back(0xA9);
  986. cp.push_back(0);
  987. OS::get_singleton()->set_window_title(_MKSTR(VERSION_NAME) + String(" - ") + TTR("Project Manager") + " - " + cp + " 2007-2020 Juan Linietsky, Ariel Manzur & Godot Contributors");
  988. HBoxContainer *top_hb = memnew(HBoxContainer);
  989. vb->add_child(top_hb);
  990. CenterContainer *ccl = memnew(CenterContainer);
  991. Label *l = memnew(Label);
  992. l->set_text(_MKSTR(VERSION_NAME) + String(" - ") + TTR("Project Manager"));
  993. l->add_font_override("font", get_font("doc", "EditorFonts"));
  994. ccl->add_child(l);
  995. top_hb->add_child(ccl);
  996. top_hb->add_spacer();
  997. l = memnew(Label);
  998. l->set_text("v" VERSION_MKSTRING);
  999. //l->add_font_override("font",get_font("bold","Fonts"));
  1000. l->set_align(Label::ALIGN_CENTER);
  1001. top_hb->add_child(l);
  1002. //vb->add_child(memnew(HSeparator));
  1003. //vb->add_margin_child("\n",memnew(Control));
  1004. tabs = memnew(TabContainer);
  1005. vb->add_child(tabs);
  1006. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  1007. HBoxContainer *tree_hb = memnew(HBoxContainer);
  1008. projects_hb = tree_hb;
  1009. projects_hb->set_name(TTR("Project List"));
  1010. tabs->add_child(tree_hb);
  1011. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  1012. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1013. tree_hb->add_child(search_tree_vb);
  1014. HBoxContainer *search_box = memnew(HBoxContainer);
  1015. search_box->add_spacer(true);
  1016. project_filter = memnew(ProjectListFilter);
  1017. search_box->add_child(project_filter);
  1018. project_filter->connect("filter_changed", this, "_load_recent_projects");
  1019. project_filter->set_custom_minimum_size(Size2(250, 10));
  1020. search_tree_vb->add_child(search_box);
  1021. PanelContainer *pc = memnew(PanelContainer);
  1022. pc->add_style_override("panel", get_stylebox("bg", "Tree"));
  1023. search_tree_vb->add_child(pc);
  1024. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  1025. scroll = memnew(ScrollContainer);
  1026. pc->add_child(scroll);
  1027. scroll->set_enable_h_scroll(false);
  1028. VBoxContainer *tree_vb = memnew(VBoxContainer);
  1029. tree_hb->add_child(tree_vb);
  1030. scroll_childs = memnew(VBoxContainer);
  1031. scroll_childs->set_h_size_flags(SIZE_EXPAND_FILL);
  1032. scroll->add_child(scroll_childs);
  1033. //HBoxContainer *hb = memnew( HBoxContainer );
  1034. //vb->add_child(hb);
  1035. Button *open = memnew(Button);
  1036. open->set_text(TTR("Edit"));
  1037. tree_vb->add_child(open);
  1038. open->connect("pressed", this, "_open_project");
  1039. open_btn = open;
  1040. Button *run = memnew(Button);
  1041. run->set_text(TTR("Run"));
  1042. tree_vb->add_child(run);
  1043. run->connect("pressed", this, "_run_project");
  1044. run_btn = run;
  1045. tree_vb->add_child(memnew(HSeparator));
  1046. Button *scan = memnew(Button);
  1047. scan->set_text(TTR("Scan"));
  1048. tree_vb->add_child(scan);
  1049. scan->connect("pressed", this, "_scan_projects");
  1050. tree_vb->add_child(memnew(HSeparator));
  1051. scan_dir = memnew(FileDialog);
  1052. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  1053. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  1054. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  1055. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("global/default_project_path"));
  1056. gui_base->add_child(scan_dir);
  1057. scan_dir->connect("dir_selected", this, "_scan_begin");
  1058. Button *create = memnew(Button);
  1059. create->set_text(TTR("New Project"));
  1060. tree_vb->add_child(create);
  1061. create->connect("pressed", this, "_new_project");
  1062. Button *import = memnew(Button);
  1063. import->set_text(TTR("Import"));
  1064. tree_vb->add_child(import);
  1065. import->connect("pressed", this, "_import_project");
  1066. Button *erase = memnew(Button);
  1067. erase->set_text(TTR("Remove"));
  1068. tree_vb->add_child(erase);
  1069. erase->connect("pressed", this, "_erase_project");
  1070. erase_btn = erase;
  1071. Button *showinfilemanager = memnew(Button);
  1072. showinfilemanager->set_text(TTR("Show Files"));
  1073. tree_vb->add_child(showinfilemanager);
  1074. showinfilemanager->connect("pressed", this, "_show_project");
  1075. show_btn = showinfilemanager;
  1076. tree_vb->add_spacer();
  1077. if (StreamPeerSSL::is_available()) {
  1078. asset_library = memnew(EditorAssetLibrary(true));
  1079. asset_library->set_name(TTR("Templates"));
  1080. tabs->add_child(asset_library);
  1081. asset_library->connect("install_asset", this, "_install_project");
  1082. } else {
  1083. WARN_PRINT("Asset Library not available, as it requires SSL to work.");
  1084. }
  1085. CenterContainer *cc = memnew(CenterContainer);
  1086. Button *cancel = memnew(Button);
  1087. cancel->set_text(TTR("Exit"));
  1088. cancel->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1089. cc->add_child(cancel);
  1090. cancel->connect("pressed", this, "_exit_dialog");
  1091. vb->add_child(cc);
  1092. //
  1093. erase_ask = memnew(ConfirmationDialog);
  1094. erase_ask->get_ok()->set_text(TTR("Remove"));
  1095. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  1096. gui_base->add_child(erase_ask);
  1097. multi_open_ask = memnew(ConfirmationDialog);
  1098. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  1099. multi_open_ask->get_ok()->connect("pressed", this, "_open_project_confirm");
  1100. gui_base->add_child(multi_open_ask);
  1101. multi_run_ask = memnew(ConfirmationDialog);
  1102. multi_run_ask->get_ok()->set_text(TTR("Run"));
  1103. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  1104. gui_base->add_child(multi_run_ask);
  1105. multi_scan_ask = memnew(ConfirmationDialog);
  1106. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  1107. gui_base->add_child(multi_scan_ask);
  1108. OS::get_singleton()->set_low_processor_usage_mode(true);
  1109. npdialog = memnew(NewProjectDialog);
  1110. gui_base->add_child(npdialog);
  1111. npdialog->connect("project_created", this, "_on_project_created");
  1112. _load_recent_projects();
  1113. if (EditorSettings::get_singleton()->get("global/autoscan_project_path")) {
  1114. _scan_begin(EditorSettings::get_singleton()->get("global/autoscan_project_path"));
  1115. }
  1116. //get_ok()->set_text("Open");
  1117. //get_ok()->set_text("Exit");
  1118. last_clicked = "";
  1119. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  1120. gui_base->set_theme(create_editor_theme());
  1121. }
  1122. ProjectManager::~ProjectManager() {
  1123. if (EditorSettings::get_singleton())
  1124. EditorSettings::destroy();
  1125. }
  1126. void ProjectListFilter::_setup_filters() {
  1127. filter_option->clear();
  1128. filter_option->add_item(TTR("Name"));
  1129. filter_option->add_item(TTR("Path"));
  1130. }
  1131. void ProjectListFilter::_command(int p_command) {
  1132. switch (p_command) {
  1133. case CMD_CLEAR_FILTER: {
  1134. if (search_box->get_text() != "") {
  1135. search_box->clear();
  1136. emit_signal("filter_changed");
  1137. }
  1138. } break;
  1139. }
  1140. }
  1141. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  1142. emit_signal("filter_changed");
  1143. }
  1144. String ProjectListFilter::get_search_term() {
  1145. return search_box->get_text().strip_edges();
  1146. }
  1147. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  1148. return _current_filter;
  1149. }
  1150. void ProjectListFilter::_filter_option_selected(int p_idx) {
  1151. FilterOption selected = (FilterOption)(filter_option->get_selected());
  1152. if (_current_filter != selected) {
  1153. _current_filter = selected;
  1154. emit_signal("filter_changed");
  1155. }
  1156. }
  1157. void ProjectListFilter::_notification(int p_what) {
  1158. switch (p_what) {
  1159. case NOTIFICATION_ENTER_TREE: {
  1160. clear_search_button->set_icon(get_icon("CloseHover", "EditorIcons"));
  1161. } break;
  1162. }
  1163. }
  1164. void ProjectListFilter::_bind_methods() {
  1165. ObjectTypeDB::bind_method(_MD("_command"), &ProjectListFilter::_command);
  1166. ObjectTypeDB::bind_method(_MD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  1167. ObjectTypeDB::bind_method(_MD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  1168. ADD_SIGNAL(MethodInfo("filter_changed"));
  1169. }
  1170. ProjectListFilter::ProjectListFilter() {
  1171. editor_initialize_certificates(); //for asset sharing
  1172. _current_filter = FILTER_NAME;
  1173. filter_option = memnew(OptionButton);
  1174. filter_option->set_custom_minimum_size(Size2(80 * EDSCALE, 10 * EDSCALE));
  1175. filter_option->set_clip_text(true);
  1176. filter_option->connect("item_selected", this, "_filter_option_selected");
  1177. add_child(filter_option);
  1178. _setup_filters();
  1179. search_box = memnew(LineEdit);
  1180. search_box->connect("text_changed", this, "_search_text_changed");
  1181. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1182. add_child(search_box);
  1183. clear_search_button = memnew(ToolButton);
  1184. clear_search_button->connect("pressed", this, "_command", make_binds(CMD_CLEAR_FILTER));
  1185. add_child(clear_search_button);
  1186. }