project_manager.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  1. /*************************************************************************/
  2. /* project_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/io/config_file.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/io/stream_peer_ssl.h"
  34. #include "core/io/zip_io.h"
  35. #include "core/os/dir_access.h"
  36. #include "core/os/file_access.h"
  37. #include "core/os/keyboard.h"
  38. #include "core/os/os.h"
  39. #include "core/translation.h"
  40. #include "core/version.h"
  41. #include "core/version_hash.gen.h"
  42. #include "editor_scale.h"
  43. #include "editor_settings.h"
  44. #include "editor_themes.h"
  45. #include "scene/gui/center_container.h"
  46. #include "scene/gui/line_edit.h"
  47. #include "scene/gui/margin_container.h"
  48. #include "scene/gui/panel_container.h"
  49. #include "scene/gui/separator.h"
  50. #include "scene/gui/texture_rect.h"
  51. #include "scene/gui/tool_button.h"
  52. class ProjectDialog : public ConfirmationDialog {
  53. GDCLASS(ProjectDialog, ConfirmationDialog);
  54. public:
  55. enum Mode {
  56. MODE_NEW,
  57. MODE_IMPORT,
  58. MODE_INSTALL,
  59. MODE_RENAME
  60. };
  61. private:
  62. enum MessageType {
  63. MESSAGE_ERROR,
  64. MESSAGE_WARNING,
  65. MESSAGE_SUCCESS
  66. };
  67. enum InputType {
  68. PROJECT_PATH,
  69. INSTALL_PATH
  70. };
  71. Mode mode;
  72. Button *browse;
  73. Button *install_browse;
  74. Button *create_dir;
  75. Container *name_container;
  76. Container *path_container;
  77. Container *install_path_container;
  78. Container *rasterizer_container;
  79. Ref<ButtonGroup> rasterizer_button_group;
  80. Label *msg;
  81. LineEdit *project_path;
  82. LineEdit *project_name;
  83. LineEdit *install_path;
  84. TextureRect *status_rect;
  85. TextureRect *install_status_rect;
  86. FileDialog *fdialog;
  87. FileDialog *fdialog_install;
  88. String zip_path;
  89. String zip_title;
  90. AcceptDialog *dialog_error;
  91. String fav_dir;
  92. String created_folder_path;
  93. void set_message(const String &p_msg, MessageType p_type = MESSAGE_SUCCESS, InputType input_type = PROJECT_PATH) {
  94. msg->set_text(p_msg);
  95. Ref<Texture> current_path_icon = status_rect->get_texture();
  96. Ref<Texture> current_install_icon = install_status_rect->get_texture();
  97. Ref<Texture> new_icon;
  98. switch (p_type) {
  99. case MESSAGE_ERROR: {
  100. msg->add_color_override("font_color", get_color("error_color", "Editor"));
  101. msg->set_modulate(Color(1, 1, 1, 1));
  102. new_icon = get_icon("StatusError", "EditorIcons");
  103. } break;
  104. case MESSAGE_WARNING: {
  105. msg->add_color_override("font_color", get_color("warning_color", "Editor"));
  106. msg->set_modulate(Color(1, 1, 1, 1));
  107. new_icon = get_icon("StatusWarning", "EditorIcons");
  108. } break;
  109. case MESSAGE_SUCCESS: {
  110. msg->set_modulate(Color(1, 1, 1, 0));
  111. new_icon = get_icon("StatusSuccess", "EditorIcons");
  112. } break;
  113. }
  114. if (current_path_icon != new_icon && input_type == PROJECT_PATH) {
  115. status_rect->set_texture(new_icon);
  116. } else if (current_install_icon != new_icon && input_type == INSTALL_PATH) {
  117. install_status_rect->set_texture(new_icon);
  118. }
  119. set_size(Size2(500, 0) * EDSCALE);
  120. }
  121. String _test_path() {
  122. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  123. String valid_path, valid_install_path;
  124. if (d->change_dir(project_path->get_text()) == OK) {
  125. valid_path = project_path->get_text();
  126. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  127. valid_path = project_path->get_text().strip_edges();
  128. } else if (project_path->get_text().ends_with(".zip")) {
  129. if (d->file_exists(project_path->get_text())) {
  130. valid_path = project_path->get_text();
  131. }
  132. } else if (project_path->get_text().strip_edges().ends_with(".zip")) {
  133. if (d->file_exists(project_path->get_text().strip_edges())) {
  134. valid_path = project_path->get_text().strip_edges();
  135. }
  136. }
  137. if (valid_path == "") {
  138. set_message(TTR("The path does not exist."), MESSAGE_ERROR);
  139. memdelete(d);
  140. get_ok()->set_disabled(true);
  141. return "";
  142. }
  143. if (mode == MODE_IMPORT && valid_path.ends_with(".zip")) {
  144. if (d->change_dir(install_path->get_text()) == OK) {
  145. valid_install_path = install_path->get_text();
  146. } else if (d->change_dir(install_path->get_text().strip_edges()) == OK) {
  147. valid_install_path = install_path->get_text().strip_edges();
  148. }
  149. if (valid_install_path == "") {
  150. set_message(TTR("The path does not exist."), MESSAGE_ERROR, INSTALL_PATH);
  151. memdelete(d);
  152. get_ok()->set_disabled(true);
  153. return "";
  154. }
  155. }
  156. if (mode == MODE_IMPORT || mode == MODE_RENAME) {
  157. if (valid_path != "" && !d->file_exists("project.godot")) {
  158. if (valid_path.ends_with(".zip")) {
  159. FileAccess *src_f = NULL;
  160. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  161. unzFile pkg = unzOpen2(valid_path.utf8().get_data(), &io);
  162. if (!pkg) {
  163. set_message(TTR("Error opening package file, not in zip format."), MESSAGE_ERROR);
  164. memdelete(d);
  165. get_ok()->set_disabled(true);
  166. unzClose(pkg);
  167. return "";
  168. }
  169. int ret = unzGoToFirstFile(pkg);
  170. while (ret == UNZ_OK) {
  171. unz_file_info info;
  172. char fname[16384];
  173. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  174. if (String(fname).ends_with("project.godot")) {
  175. break;
  176. }
  177. ret = unzGoToNextFile(pkg);
  178. }
  179. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  180. set_message(TTR("Invalid '.zip' project file, does not contain a 'project.godot' file."), MESSAGE_ERROR);
  181. memdelete(d);
  182. get_ok()->set_disabled(true);
  183. unzClose(pkg);
  184. return "";
  185. }
  186. unzClose(pkg);
  187. // check if the specified install folder is empty, even though this is not an error, it is good to check here
  188. d->list_dir_begin();
  189. bool is_empty = true;
  190. String n = d->get_next();
  191. while (n != String()) {
  192. if (n != "." && n != "..") {
  193. is_empty = false;
  194. break;
  195. }
  196. n = d->get_next();
  197. }
  198. d->list_dir_end();
  199. if (!is_empty) {
  200. set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH);
  201. memdelete(d);
  202. get_ok()->set_disabled(true);
  203. return "";
  204. }
  205. } else {
  206. set_message(TTR("Please choose a 'project.godot' or '.zip' file."), MESSAGE_ERROR);
  207. memdelete(d);
  208. install_path_container->hide();
  209. get_ok()->set_disabled(true);
  210. return "";
  211. }
  212. } else if (valid_path.ends_with("zip")) {
  213. set_message(TTR("Directory already contains a Godot project."), MESSAGE_ERROR, INSTALL_PATH);
  214. memdelete(d);
  215. get_ok()->set_disabled(true);
  216. return "";
  217. }
  218. } else {
  219. // check if the specified folder is empty, even though this is not an error, it is good to check here
  220. d->list_dir_begin();
  221. bool is_empty = true;
  222. String n = d->get_next();
  223. while (n != String()) {
  224. if (n != "." && n != "..") { // i don't know if this is enough to guarantee an empty dir
  225. is_empty = false;
  226. break;
  227. }
  228. n = d->get_next();
  229. }
  230. d->list_dir_end();
  231. if (!is_empty) {
  232. set_message(TTR("Please choose an empty folder."), MESSAGE_ERROR);
  233. memdelete(d);
  234. get_ok()->set_disabled(true);
  235. return "";
  236. }
  237. }
  238. set_message("");
  239. set_message("", MESSAGE_SUCCESS, INSTALL_PATH);
  240. memdelete(d);
  241. get_ok()->set_disabled(false);
  242. return valid_path;
  243. }
  244. void _path_text_changed(const String &p_path) {
  245. String sp = _test_path();
  246. if (sp != "") {
  247. // set the project name to the select folder name
  248. if (project_name->get_text() == "") {
  249. sp = sp.replace("\\", "/");
  250. int lidx = sp.find_last("/");
  251. if (lidx != -1) {
  252. sp = sp.substr(lidx + 1, sp.length());
  253. }
  254. if (sp == "" && mode == MODE_IMPORT)
  255. sp = TTR("Imported Project");
  256. project_name->set_text(sp);
  257. _text_changed(sp);
  258. }
  259. }
  260. if (created_folder_path != "" && created_folder_path != p_path) {
  261. _remove_created_folder();
  262. }
  263. }
  264. void _file_selected(const String &p_path) {
  265. String p = p_path;
  266. if (mode == MODE_IMPORT) {
  267. if (p.ends_with("project.godot")) {
  268. p = p.get_base_dir();
  269. install_path_container->hide();
  270. get_ok()->set_disabled(false);
  271. } else if (p.ends_with(".zip")) {
  272. install_path->set_text(p.get_base_dir());
  273. install_path_container->show();
  274. get_ok()->set_disabled(false);
  275. } else {
  276. set_message(TTR("Please choose a 'project.godot' or '.zip' file."), MESSAGE_ERROR);
  277. get_ok()->set_disabled(true);
  278. return;
  279. }
  280. }
  281. String sp = p.simplify_path();
  282. project_path->set_text(sp);
  283. _path_text_changed(sp);
  284. if (p.ends_with(".zip")) {
  285. install_path->call_deferred("grab_focus");
  286. } else {
  287. get_ok()->call_deferred("grab_focus");
  288. }
  289. }
  290. void _path_selected(const String &p_path) {
  291. String p = p_path;
  292. String sp = p.simplify_path();
  293. project_path->set_text(sp);
  294. _path_text_changed(sp);
  295. get_ok()->call_deferred("grab_focus");
  296. }
  297. void _install_path_selected(const String &p_path) {
  298. String p = p_path;
  299. String sp = p.simplify_path();
  300. install_path->set_text(sp);
  301. _path_text_changed(sp);
  302. get_ok()->call_deferred("grab_focus");
  303. }
  304. void _browse_path() {
  305. fdialog->set_current_dir(project_path->get_text());
  306. if (mode == MODE_IMPORT) {
  307. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  308. fdialog->clear_filters();
  309. fdialog->add_filter("project.godot ; " VERSION_NAME " Project");
  310. fdialog->add_filter("*.zip ; Zip File");
  311. } else {
  312. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  313. }
  314. fdialog->popup_centered_ratio();
  315. }
  316. void _browse_install_path() {
  317. fdialog_install->set_current_dir(install_path->get_text());
  318. fdialog_install->set_mode(FileDialog::MODE_OPEN_DIR);
  319. fdialog_install->popup_centered_ratio();
  320. }
  321. void _create_folder() {
  322. if (project_name->get_text() == "" || created_folder_path != "" || project_name->get_text().ends_with(".") || project_name->get_text().ends_with(" ")) {
  323. set_message(TTR("Invalid Project Name."), MESSAGE_WARNING);
  324. return;
  325. }
  326. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  327. if (d->change_dir(project_path->get_text()) == OK) {
  328. if (!d->dir_exists(project_name->get_text())) {
  329. if (d->make_dir(project_name->get_text()) == OK) {
  330. d->change_dir(project_name->get_text());
  331. String dir_str = d->get_current_dir();
  332. project_path->set_text(dir_str);
  333. _path_text_changed(dir_str);
  334. created_folder_path = d->get_current_dir();
  335. create_dir->set_disabled(true);
  336. } else {
  337. dialog_error->set_text(TTR("Couldn't create folder."));
  338. dialog_error->popup_centered_minsize();
  339. }
  340. } else {
  341. dialog_error->set_text(TTR("There is already a folder in this path with the specified name."));
  342. dialog_error->popup_centered_minsize();
  343. }
  344. }
  345. memdelete(d);
  346. }
  347. void _text_changed(const String &p_text) {
  348. if (mode != MODE_NEW)
  349. return;
  350. _test_path();
  351. if (p_text == "")
  352. set_message(TTR("It would be a good idea to name your project."), MESSAGE_WARNING);
  353. }
  354. void ok_pressed() {
  355. String dir = project_path->get_text();
  356. if (mode == MODE_RENAME) {
  357. String dir2 = _test_path();
  358. if (dir2 == "") {
  359. set_message(TTR("Invalid project path (changed anything?)."), MESSAGE_ERROR);
  360. return;
  361. }
  362. ProjectSettings *current = memnew(ProjectSettings);
  363. int err = current->setup(dir2, "");
  364. if (err != OK) {
  365. set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR);
  366. } else {
  367. ProjectSettings::CustomMap edited_settings;
  368. edited_settings["application/config/name"] = project_name->get_text();
  369. if (current->save_custom(dir2.plus_file("project.godot"), edited_settings, Vector<String>(), true) != OK) {
  370. set_message(TTR("Couldn't edit project.godot in project path."), MESSAGE_ERROR);
  371. }
  372. }
  373. hide();
  374. emit_signal("projects_updated");
  375. } else {
  376. if (mode == MODE_IMPORT) {
  377. if (project_path->get_text().ends_with(".zip")) {
  378. mode = MODE_INSTALL;
  379. ok_pressed();
  380. return;
  381. }
  382. } else {
  383. if (mode == MODE_NEW) {
  384. ProjectSettings::CustomMap initial_settings;
  385. if (rasterizer_button_group->get_pressed_button()->get_meta("driver_name") == "GLES3") {
  386. initial_settings["rendering/quality/driver/driver_name"] = "GLES3";
  387. } else {
  388. initial_settings["rendering/quality/driver/driver_name"] = "GLES2";
  389. initial_settings["rendering/vram_compression/import_etc2"] = false;
  390. initial_settings["rendering/vram_compression/import_etc"] = true;
  391. }
  392. initial_settings["application/config/name"] = project_name->get_text();
  393. initial_settings["application/config/icon"] = "res://icon.png";
  394. initial_settings["rendering/environment/default_environment"] = "res://default_env.tres";
  395. if (ProjectSettings::get_singleton()->save_custom(dir.plus_file("project.godot"), initial_settings, Vector<String>(), false) != OK) {
  396. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  397. } else {
  398. ResourceSaver::save(dir.plus_file("icon.png"), get_icon("DefaultProjectIcon", "EditorIcons"));
  399. FileAccess *f = FileAccess::open(dir.plus_file("default_env.tres"), FileAccess::WRITE);
  400. if (!f) {
  401. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  402. } else {
  403. f->store_line("[gd_resource type=\"Environment\" load_steps=2 format=2]");
  404. f->store_line("[sub_resource type=\"ProceduralSky\" id=1]");
  405. f->store_line("[resource]");
  406. f->store_line("background_mode = 2");
  407. f->store_line("background_sky = SubResource( 1 )");
  408. memdelete(f);
  409. }
  410. }
  411. } else if (mode == MODE_INSTALL) {
  412. if (project_path->get_text().ends_with(".zip")) {
  413. dir = install_path->get_text();
  414. zip_path = project_path->get_text();
  415. }
  416. FileAccess *src_f = NULL;
  417. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  418. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  419. if (!pkg) {
  420. dialog_error->set_text(TTR("Error opening package file, not in zip format."));
  421. dialog_error->popup_centered_minsize();
  422. return;
  423. }
  424. int ret = unzGoToFirstFile(pkg);
  425. Vector<String> failed_files;
  426. int idx = 0;
  427. while (ret == UNZ_OK) {
  428. //get filename
  429. unz_file_info info;
  430. char fname[16384];
  431. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  432. String path = fname;
  433. int depth = 1; //stuff from github comes with tag
  434. bool skip = false;
  435. while (depth > 0) {
  436. int pp = path.find("/");
  437. if (pp == -1) {
  438. skip = true;
  439. break;
  440. }
  441. path = path.substr(pp + 1, path.length());
  442. depth--;
  443. }
  444. if (skip || path == String()) {
  445. //
  446. } else if (path.ends_with("/")) { // a dir
  447. path = path.substr(0, path.length() - 1);
  448. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  449. da->make_dir(dir.plus_file(path));
  450. memdelete(da);
  451. } else {
  452. Vector<uint8_t> data;
  453. data.resize(info.uncompressed_size);
  454. //read
  455. unzOpenCurrentFile(pkg);
  456. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  457. unzCloseCurrentFile(pkg);
  458. FileAccess *f = FileAccess::open(dir.plus_file(path), FileAccess::WRITE);
  459. if (f) {
  460. f->store_buffer(data.ptr(), data.size());
  461. memdelete(f);
  462. } else {
  463. failed_files.push_back(path);
  464. }
  465. }
  466. idx++;
  467. ret = unzGoToNextFile(pkg);
  468. }
  469. unzClose(pkg);
  470. if (failed_files.size()) {
  471. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  472. for (int i = 0; i < failed_files.size(); i++) {
  473. if (i > 15) {
  474. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  475. break;
  476. }
  477. msg += failed_files[i] + "\n";
  478. }
  479. dialog_error->set_text(msg);
  480. dialog_error->popup_centered_minsize();
  481. } else if (!project_path->get_text().ends_with(".zip")) {
  482. dialog_error->set_text(TTR("Package installed successfully!"));
  483. dialog_error->popup_centered_minsize();
  484. }
  485. }
  486. }
  487. dir = dir.replace("\\", "/");
  488. if (dir.ends_with("/"))
  489. dir = dir.substr(0, dir.length() - 1);
  490. String proj = dir.replace("/", "::");
  491. EditorSettings::get_singleton()->set("projects/" + proj, dir);
  492. EditorSettings::get_singleton()->save();
  493. hide();
  494. emit_signal("project_created", dir);
  495. }
  496. }
  497. void _remove_created_folder() {
  498. if (created_folder_path != "") {
  499. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  500. d->remove(created_folder_path);
  501. memdelete(d);
  502. create_dir->set_disabled(false);
  503. created_folder_path = "";
  504. }
  505. }
  506. void cancel_pressed() {
  507. _remove_created_folder();
  508. project_path->clear();
  509. _path_text_changed("");
  510. project_name->clear();
  511. _text_changed("");
  512. if (status_rect->get_texture() == get_icon("StatusError", "EditorIcons"))
  513. msg->show();
  514. if (install_status_rect->get_texture() == get_icon("StatusError", "EditorIcons"))
  515. msg->show();
  516. }
  517. void _notification(int p_what) {
  518. if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST)
  519. _remove_created_folder();
  520. }
  521. protected:
  522. static void _bind_methods() {
  523. ClassDB::bind_method("_browse_path", &ProjectDialog::_browse_path);
  524. ClassDB::bind_method("_create_folder", &ProjectDialog::_create_folder);
  525. ClassDB::bind_method("_text_changed", &ProjectDialog::_text_changed);
  526. ClassDB::bind_method("_path_text_changed", &ProjectDialog::_path_text_changed);
  527. ClassDB::bind_method("_path_selected", &ProjectDialog::_path_selected);
  528. ClassDB::bind_method("_file_selected", &ProjectDialog::_file_selected);
  529. ClassDB::bind_method("_install_path_selected", &ProjectDialog::_install_path_selected);
  530. ClassDB::bind_method("_browse_install_path", &ProjectDialog::_browse_install_path);
  531. ADD_SIGNAL(MethodInfo("project_created"));
  532. ADD_SIGNAL(MethodInfo("projects_updated"));
  533. }
  534. public:
  535. void set_zip_path(const String &p_path) {
  536. zip_path = p_path;
  537. }
  538. void set_zip_title(const String &p_title) {
  539. zip_title = p_title;
  540. }
  541. void set_mode(Mode p_mode) {
  542. mode = p_mode;
  543. }
  544. void set_project_path(const String &p_path) {
  545. project_path->set_text(p_path);
  546. }
  547. void show_dialog() {
  548. if (mode == MODE_RENAME) {
  549. project_path->set_editable(false);
  550. browse->hide();
  551. install_browse->hide();
  552. set_title(TTR("Rename Project"));
  553. get_ok()->set_text(TTR("Rename"));
  554. name_container->show();
  555. status_rect->hide();
  556. msg->hide();
  557. install_path_container->hide();
  558. install_status_rect->hide();
  559. rasterizer_container->hide();
  560. get_ok()->set_disabled(false);
  561. ProjectSettings *current = memnew(ProjectSettings);
  562. int err = current->setup(project_path->get_text(), "");
  563. if (err != OK) {
  564. set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR);
  565. status_rect->show();
  566. msg->show();
  567. get_ok()->set_disabled(true);
  568. } else if (current->has_setting("application/config/name")) {
  569. String proj = current->get("application/config/name");
  570. project_name->set_text(proj);
  571. _text_changed(proj);
  572. }
  573. project_name->call_deferred("grab_focus");
  574. create_dir->hide();
  575. } else {
  576. fav_dir = EditorSettings::get_singleton()->get("filesystem/directories/default_project_path");
  577. if (fav_dir != "") {
  578. project_path->set_text(fav_dir);
  579. fdialog->set_current_dir(fav_dir);
  580. } else {
  581. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  582. project_path->set_text(d->get_current_dir());
  583. fdialog->set_current_dir(d->get_current_dir());
  584. memdelete(d);
  585. }
  586. String proj = TTR("New Game Project");
  587. project_name->set_text(proj);
  588. _text_changed(proj);
  589. project_path->set_editable(true);
  590. browse->set_disabled(false);
  591. browse->show();
  592. install_browse->set_disabled(false);
  593. install_browse->show();
  594. create_dir->show();
  595. status_rect->show();
  596. install_status_rect->show();
  597. msg->show();
  598. if (mode == MODE_IMPORT) {
  599. set_title(TTR("Import Existing Project"));
  600. get_ok()->set_text(TTR("Import & Edit"));
  601. name_container->hide();
  602. install_path_container->hide();
  603. rasterizer_container->hide();
  604. project_path->grab_focus();
  605. } else if (mode == MODE_NEW) {
  606. set_title(TTR("Create New Project"));
  607. get_ok()->set_text(TTR("Create & Edit"));
  608. name_container->show();
  609. install_path_container->hide();
  610. rasterizer_container->show();
  611. project_name->call_deferred("grab_focus");
  612. project_name->call_deferred("select_all");
  613. } else if (mode == MODE_INSTALL) {
  614. set_title(TTR("Install Project:") + " " + zip_title);
  615. get_ok()->set_text(TTR("Install & Edit"));
  616. name_container->show();
  617. install_path_container->hide();
  618. rasterizer_container->hide();
  619. project_path->grab_focus();
  620. }
  621. _test_path();
  622. }
  623. popup_centered_minsize(Size2(500, 0) * EDSCALE);
  624. }
  625. ProjectDialog() {
  626. VBoxContainer *vb = memnew(VBoxContainer);
  627. add_child(vb);
  628. name_container = memnew(VBoxContainer);
  629. vb->add_child(name_container);
  630. Label *l = memnew(Label);
  631. l->set_text(TTR("Project Name:"));
  632. name_container->add_child(l);
  633. HBoxContainer *pnhb = memnew(HBoxContainer);
  634. name_container->add_child(pnhb);
  635. project_name = memnew(LineEdit);
  636. project_name->set_h_size_flags(SIZE_EXPAND_FILL);
  637. pnhb->add_child(project_name);
  638. create_dir = memnew(Button);
  639. pnhb->add_child(create_dir);
  640. create_dir->set_text(TTR("Create Folder"));
  641. create_dir->connect("pressed", this, "_create_folder");
  642. path_container = memnew(VBoxContainer);
  643. vb->add_child(path_container);
  644. l = memnew(Label);
  645. l->set_text(TTR("Project Path:"));
  646. path_container->add_child(l);
  647. HBoxContainer *pphb = memnew(HBoxContainer);
  648. path_container->add_child(pphb);
  649. project_path = memnew(LineEdit);
  650. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  651. pphb->add_child(project_path);
  652. install_path_container = memnew(VBoxContainer);
  653. vb->add_child(install_path_container);
  654. l = memnew(Label);
  655. l->set_text(TTR("Project Installation Path:"));
  656. install_path_container->add_child(l);
  657. HBoxContainer *iphb = memnew(HBoxContainer);
  658. install_path_container->add_child(iphb);
  659. install_path = memnew(LineEdit);
  660. install_path->set_h_size_flags(SIZE_EXPAND_FILL);
  661. iphb->add_child(install_path);
  662. // status icon
  663. status_rect = memnew(TextureRect);
  664. status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  665. pphb->add_child(status_rect);
  666. browse = memnew(Button);
  667. browse->set_text(TTR("Browse"));
  668. browse->connect("pressed", this, "_browse_path");
  669. pphb->add_child(browse);
  670. // install status icon
  671. install_status_rect = memnew(TextureRect);
  672. install_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  673. iphb->add_child(install_status_rect);
  674. install_browse = memnew(Button);
  675. install_browse->set_text(TTR("Browse"));
  676. install_browse->connect("pressed", this, "_browse_install_path");
  677. iphb->add_child(install_browse);
  678. msg = memnew(Label);
  679. msg->set_align(Label::ALIGN_CENTER);
  680. vb->add_child(msg);
  681. // rasterizer selection
  682. rasterizer_container = memnew(VBoxContainer);
  683. vb->add_child(rasterizer_container);
  684. l = memnew(Label);
  685. l->set_text(TTR("Renderer:"));
  686. rasterizer_container->add_child(l);
  687. Container *rshb = memnew(HBoxContainer);
  688. rasterizer_container->add_child(rshb);
  689. rasterizer_button_group.instance();
  690. Container *rvb = memnew(VBoxContainer);
  691. rvb->set_h_size_flags(SIZE_EXPAND_FILL);
  692. rshb->add_child(rvb);
  693. Button *rs_button = memnew(CheckBox);
  694. rs_button->set_button_group(rasterizer_button_group);
  695. rs_button->set_text(TTR("OpenGL ES 3.0"));
  696. rs_button->set_meta("driver_name", "GLES3");
  697. rs_button->set_pressed(true);
  698. rvb->add_child(rs_button);
  699. l = memnew(Label);
  700. l->set_text(TTR("Higher visual quality\nAll features available\nIncompatible with older hardware\nNot recommended for web games"));
  701. rvb->add_child(l);
  702. rshb->add_child(memnew(VSeparator));
  703. rvb = memnew(VBoxContainer);
  704. rvb->set_h_size_flags(SIZE_EXPAND_FILL);
  705. rshb->add_child(rvb);
  706. rs_button = memnew(CheckBox);
  707. rs_button->set_button_group(rasterizer_button_group);
  708. rs_button->set_text(TTR("OpenGL ES 2.0"));
  709. rs_button->set_meta("driver_name", "GLES2");
  710. rvb->add_child(rs_button);
  711. l = memnew(Label);
  712. l->set_text(TTR("Lower visual quality\nSome features not available\nWorks on most hardware\nRecommended for web games"));
  713. rvb->add_child(l);
  714. l = memnew(Label);
  715. l->set_text(TTR("Renderer can be changed later, but scenes may need to be adjusted."));
  716. l->set_align(Label::ALIGN_CENTER);
  717. rasterizer_container->add_child(l);
  718. fdialog = memnew(FileDialog);
  719. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  720. fdialog_install = memnew(FileDialog);
  721. fdialog_install->set_access(FileDialog::ACCESS_FILESYSTEM);
  722. add_child(fdialog);
  723. add_child(fdialog_install);
  724. project_name->connect("text_changed", this, "_text_changed");
  725. project_path->connect("text_changed", this, "_path_text_changed");
  726. install_path->connect("text_changed", this, "_path_text_changed");
  727. fdialog->connect("dir_selected", this, "_path_selected");
  728. fdialog->connect("file_selected", this, "_file_selected");
  729. fdialog_install->connect("dir_selected", this, "_install_path_selected");
  730. fdialog_install->connect("file_selected", this, "_install_path_selected");
  731. set_hide_on_ok(false);
  732. mode = MODE_NEW;
  733. dialog_error = memnew(AcceptDialog);
  734. add_child(dialog_error);
  735. }
  736. };
  737. struct ProjectItem {
  738. String project;
  739. String path;
  740. String conf;
  741. int config_version;
  742. uint64_t last_modified;
  743. bool favorite;
  744. bool grayed;
  745. bool ordered_latest_modification;
  746. ProjectItem() {}
  747. ProjectItem(const String &p_project, const String &p_path, const String &p_conf, int p_config_version, uint64_t p_last_modified, bool p_favorite = false, bool p_grayed = false, const bool p_ordered_latest_modification = true) {
  748. project = p_project;
  749. path = p_path;
  750. conf = p_conf;
  751. config_version = p_config_version;
  752. last_modified = p_last_modified;
  753. favorite = p_favorite;
  754. grayed = p_grayed;
  755. ordered_latest_modification = p_ordered_latest_modification;
  756. }
  757. _FORCE_INLINE_ bool operator<(const ProjectItem &l) const {
  758. if (ordered_latest_modification)
  759. return last_modified > l.last_modified;
  760. return project < l.project;
  761. }
  762. _FORCE_INLINE_ bool operator==(const ProjectItem &l) const { return project == l.project; }
  763. };
  764. void ProjectManager::_notification(int p_what) {
  765. switch (p_what) {
  766. case NOTIFICATION_ENTER_TREE: {
  767. Engine::get_singleton()->set_editor_hint(false);
  768. } break;
  769. case NOTIFICATION_READY: {
  770. if (scroll_children->get_child_count() == 0 && StreamPeerSSL::is_available())
  771. open_templates->popup_centered_minsize();
  772. } break;
  773. case NOTIFICATION_VISIBILITY_CHANGED: {
  774. set_process_unhandled_input(is_visible_in_tree());
  775. } break;
  776. }
  777. }
  778. void ProjectManager::_panel_draw(Node *p_hb) {
  779. HBoxContainer *hb = Object::cast_to<HBoxContainer>(p_hb);
  780. 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"));
  781. if (selected_list.has(hb->get_meta("name"))) {
  782. hb->draw_style_box(gui_base->get_stylebox("selected", "Tree"), Rect2(Point2(), hb->get_size() - Size2(10, 0) * EDSCALE));
  783. }
  784. }
  785. void ProjectManager::_update_project_buttons() {
  786. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  787. CanvasItem *item = Object::cast_to<CanvasItem>(scroll_children->get_child(i));
  788. item->update();
  789. }
  790. bool empty_selection = selected_list.empty();
  791. erase_btn->set_disabled(empty_selection);
  792. open_btn->set_disabled(empty_selection);
  793. rename_btn->set_disabled(empty_selection);
  794. run_btn->set_disabled(empty_selection);
  795. bool missing_projects = false;
  796. Map<String, String> list_all_projects;
  797. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  798. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  799. if (hb) {
  800. list_all_projects.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  801. }
  802. }
  803. for (Map<String, String>::Element *E = list_all_projects.front(); E; E = E->next()) {
  804. String project_name = E->key().replace(":::", ":/").replace("::", "/") + "/project.godot";
  805. if (!FileAccess::exists(project_name)) {
  806. missing_projects = true;
  807. break;
  808. }
  809. }
  810. erase_missing_btn->set_visible(missing_projects);
  811. }
  812. void ProjectManager::_panel_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  813. Ref<InputEventMouseButton> mb = p_ev;
  814. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  815. String clicked = p_hb->get_meta("name");
  816. String clicked_main_scene = p_hb->get_meta("main_scene");
  817. if (mb->get_shift() && selected_list.size() > 0 && last_clicked != "" && clicked != last_clicked) {
  818. int clicked_id = -1;
  819. int last_clicked_id = -1;
  820. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  821. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  822. if (!hb) continue;
  823. if (hb->get_meta("name") == clicked) clicked_id = i;
  824. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  825. }
  826. if (last_clicked_id != -1 && clicked_id != -1) {
  827. int min = clicked_id < last_clicked_id ? clicked_id : last_clicked_id;
  828. int max = clicked_id > last_clicked_id ? clicked_id : last_clicked_id;
  829. for (int i = 0; i < scroll_children->get_child_count(); ++i) {
  830. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  831. if (!hb) continue;
  832. if (i != clicked_id && (i < min || i > max) && !mb->get_control()) {
  833. selected_list.erase(hb->get_meta("name"));
  834. } else if (i >= min && i <= max) {
  835. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  836. }
  837. }
  838. }
  839. } else if (selected_list.has(clicked) && mb->get_control()) {
  840. selected_list.erase(clicked);
  841. } else {
  842. last_clicked = clicked;
  843. if (mb->get_control() || selected_list.size() == 0) {
  844. selected_list.insert(clicked, clicked_main_scene);
  845. } else {
  846. selected_list.clear();
  847. selected_list.insert(clicked, clicked_main_scene);
  848. }
  849. }
  850. _update_project_buttons();
  851. if (mb->is_doubleclick())
  852. _open_selected_projects_ask(); //open if doubleclicked
  853. }
  854. }
  855. void ProjectManager::_unhandled_input(const Ref<InputEvent> &p_ev) {
  856. Ref<InputEventKey> k = p_ev;
  857. if (k.is_valid()) {
  858. if (!k->is_pressed())
  859. return;
  860. if (tabs->get_current_tab() != 0)
  861. return;
  862. bool scancode_handled = true;
  863. switch (k->get_scancode()) {
  864. case KEY_ENTER: {
  865. _open_selected_projects_ask();
  866. } break;
  867. case KEY_DELETE: {
  868. _erase_project();
  869. } break;
  870. case KEY_HOME: {
  871. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  872. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  873. if (hb) {
  874. selected_list.clear();
  875. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  876. scroll->set_v_scroll(0);
  877. _update_project_buttons();
  878. break;
  879. }
  880. }
  881. } break;
  882. case KEY_END: {
  883. for (int i = scroll_children->get_child_count() - 1; i >= 0; i--) {
  884. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  885. if (hb) {
  886. selected_list.clear();
  887. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  888. scroll->set_v_scroll(scroll_children->get_size().y);
  889. _update_project_buttons();
  890. break;
  891. }
  892. }
  893. } break;
  894. case KEY_UP: {
  895. if (k->get_shift())
  896. break;
  897. if (selected_list.size()) {
  898. bool found = false;
  899. for (int i = scroll_children->get_child_count() - 1; i >= 0; i--) {
  900. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  901. if (!hb) continue;
  902. String current = hb->get_meta("name");
  903. if (found) {
  904. selected_list.clear();
  905. selected_list.insert(current, hb->get_meta("main_scene"));
  906. int offset_diff = scroll->get_v_scroll() - hb->get_position().y;
  907. if (offset_diff > 0)
  908. scroll->set_v_scroll(scroll->get_v_scroll() - offset_diff);
  909. _update_project_buttons();
  910. break;
  911. } else if (current == selected_list.back()->key()) {
  912. found = true;
  913. }
  914. }
  915. break;
  916. }
  917. FALLTHROUGH;
  918. }
  919. case KEY_DOWN: {
  920. if (k->get_shift())
  921. break;
  922. bool found = selected_list.empty();
  923. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  924. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  925. if (!hb) continue;
  926. String current = hb->get_meta("name");
  927. if (found) {
  928. selected_list.clear();
  929. selected_list.insert(current, hb->get_meta("main_scene"));
  930. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  931. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  932. if (offset_diff > 0)
  933. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  934. _update_project_buttons();
  935. break;
  936. } else if (current == selected_list.back()->key()) {
  937. found = true;
  938. }
  939. }
  940. } break;
  941. case KEY_F: {
  942. if (k->get_command())
  943. this->project_filter->search_box->grab_focus();
  944. else
  945. scancode_handled = false;
  946. } break;
  947. default: {
  948. scancode_handled = false;
  949. } break;
  950. }
  951. if (scancode_handled) {
  952. accept_event();
  953. }
  954. }
  955. }
  956. void ProjectManager::_favorite_pressed(Node *p_hb) {
  957. String clicked = p_hb->get_meta("name");
  958. bool favorite = !p_hb->get_meta("favorite");
  959. String proj = clicked.replace(":::", ":/");
  960. proj = proj.replace("::", "/");
  961. if (favorite) {
  962. EditorSettings::get_singleton()->set("favorite_projects/" + clicked, proj);
  963. } else {
  964. EditorSettings::get_singleton()->erase("favorite_projects/" + clicked);
  965. }
  966. EditorSettings::get_singleton()->save();
  967. call_deferred("_load_recent_projects");
  968. }
  969. void ProjectManager::_load_recent_projects() {
  970. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  971. String search_term = project_filter->get_search_term();
  972. while (scroll_children->get_child_count() > 0) {
  973. memdelete(scroll_children->get_child(0));
  974. }
  975. Map<String, String> selected_list_copy = selected_list;
  976. List<PropertyInfo> properties;
  977. EditorSettings::get_singleton()->get_property_list(&properties);
  978. Color font_color = gui_base->get_color("font_color", "Tree");
  979. bool set_ordered_latest_modification;
  980. ProjectListFilter::FilterOption filter_order_option = project_order_filter->get_filter_option();
  981. if (filter_order_option == ProjectListFilter::FILTER_NAME) {
  982. set_ordered_latest_modification = false;
  983. } else {
  984. set_ordered_latest_modification = true;
  985. }
  986. EditorSettings::get_singleton()->set("project_manager/sorting_order", (int)filter_order_option);
  987. List<ProjectItem> projects;
  988. List<ProjectItem> favorite_projects;
  989. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  990. String _name = E->get().name;
  991. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  992. continue;
  993. String path = EditorSettings::get_singleton()->get(_name);
  994. if (filter_option == ProjectListFilter::FILTER_PATH && search_term != "" && path.findn(search_term) == -1)
  995. continue;
  996. String project = _name.get_slice("/", 1);
  997. String conf = path.plus_file("project.godot");
  998. int config_version = 0; // Assume 0 until we know better
  999. bool favorite = (_name.begins_with("favorite_projects/")) ? true : false;
  1000. bool grayed = false;
  1001. uint64_t last_modified = 0;
  1002. if (FileAccess::exists(conf)) {
  1003. last_modified = FileAccess::get_modified_time(conf);
  1004. String fscache = path.plus_file(".fscache");
  1005. if (FileAccess::exists(fscache)) {
  1006. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  1007. if (cache_modified > last_modified)
  1008. last_modified = cache_modified;
  1009. }
  1010. } else {
  1011. grayed = true;
  1012. }
  1013. ProjectItem item(project, path, conf, config_version, last_modified, favorite, grayed, set_ordered_latest_modification);
  1014. if (favorite)
  1015. favorite_projects.push_back(item);
  1016. else
  1017. projects.push_back(item);
  1018. }
  1019. projects.sort();
  1020. favorite_projects.sort();
  1021. for (List<ProjectItem>::Element *E = projects.front(); E;) {
  1022. List<ProjectItem>::Element *next = E->next();
  1023. if (favorite_projects.find(E->get()) != NULL)
  1024. projects.erase(E->get());
  1025. E = next;
  1026. }
  1027. for (List<ProjectItem>::Element *E = favorite_projects.back(); E; E = E->prev()) {
  1028. projects.push_front(E->get());
  1029. }
  1030. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  1031. for (List<ProjectItem>::Element *E = projects.front(); E; E = E->next()) {
  1032. ProjectItem &item = E->get();
  1033. String project = item.project;
  1034. String path = item.path;
  1035. String conf = item.conf;
  1036. Ref<ConfigFile> cf = memnew(ConfigFile);
  1037. Error cf_err = cf->load(conf);
  1038. String project_name = TTR("Unnamed Project");
  1039. if (cf_err == OK && cf->has_section_key("application", "config/name")) {
  1040. project_name = static_cast<String>(cf->get_value("application", "config/name")).xml_unescape();
  1041. }
  1042. if (filter_option == ProjectListFilter::FILTER_NAME && search_term != "" && project_name.findn(search_term) == -1)
  1043. continue;
  1044. Ref<Texture> icon;
  1045. String main_scene;
  1046. if (cf_err == OK) {
  1047. item.config_version = (int)cf->get_value("", "config_version", 0);
  1048. if (item.config_version > ProjectSettings::CONFIG_VERSION) {
  1049. // Comes from an incompatible (more recent) Godot version, grey it out
  1050. item.grayed = true;
  1051. }
  1052. String appicon = cf->get_value("application", "config/icon", "");
  1053. if (appicon != "") {
  1054. Ref<Image> img;
  1055. img.instance();
  1056. Error err = img->load(appicon.replace_first("res://", path + "/"));
  1057. if (err == OK) {
  1058. Ref<Texture> default_icon = get_icon("DefaultProjectIcon", "EditorIcons");
  1059. img->resize(default_icon->get_width(), default_icon->get_height());
  1060. Ref<ImageTexture> it = memnew(ImageTexture);
  1061. it->create_from_image(img);
  1062. icon = it;
  1063. }
  1064. }
  1065. main_scene = cf->get_value("application", "run/main_scene", "");
  1066. }
  1067. if (icon.is_null()) {
  1068. icon = get_icon("DefaultProjectIcon", "EditorIcons");
  1069. }
  1070. selected_list_copy.erase(project);
  1071. bool is_favorite = item.favorite;
  1072. bool is_grayed = item.grayed;
  1073. HBoxContainer *hb = memnew(HBoxContainer);
  1074. hb->set_meta("name", project);
  1075. hb->set_meta("main_scene", main_scene);
  1076. hb->set_meta("favorite", is_favorite);
  1077. hb->connect("draw", this, "_panel_draw", varray(hb));
  1078. hb->connect("gui_input", this, "_panel_input", varray(hb));
  1079. hb->add_constant_override("separation", 10 * EDSCALE);
  1080. VBoxContainer *favorite_box = memnew(VBoxContainer);
  1081. TextureButton *favorite = memnew(TextureButton);
  1082. favorite->set_normal_texture(favorite_icon);
  1083. if (!is_favorite)
  1084. favorite->set_modulate(Color(1, 1, 1, 0.2));
  1085. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  1086. favorite_box->add_child(favorite);
  1087. favorite_box->set_alignment(BoxContainer::ALIGN_CENTER);
  1088. hb->add_child(favorite_box);
  1089. TextureRect *tf = memnew(TextureRect);
  1090. tf->set_texture(icon);
  1091. hb->add_child(tf);
  1092. VBoxContainer *vb = memnew(VBoxContainer);
  1093. if (is_grayed)
  1094. vb->set_modulate(Color(0.5, 0.5, 0.5));
  1095. vb->set_name("project");
  1096. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1097. hb->add_child(vb);
  1098. Control *ec = memnew(Control);
  1099. ec->set_custom_minimum_size(Size2(0, 1));
  1100. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  1101. vb->add_child(ec);
  1102. Label *title = memnew(Label(project_name));
  1103. title->add_font_override("font", gui_base->get_font("title", "EditorFonts"));
  1104. title->add_color_override("font_color", font_color);
  1105. title->set_clip_text(true);
  1106. vb->add_child(title);
  1107. HBoxContainer *path_hb = memnew(HBoxContainer);
  1108. path_hb->set_name("path_box");
  1109. path_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1110. vb->add_child(path_hb);
  1111. Button *show = memnew(Button);
  1112. show->set_name("show");
  1113. show->set_icon(get_icon("Filesystem", "EditorIcons"));
  1114. show->set_flat(true);
  1115. show->set_modulate(Color(1, 1, 1, 0.5));
  1116. path_hb->add_child(show);
  1117. show->connect("pressed", this, "_show_project", varray(path));
  1118. show->set_tooltip(TTR("Show in File Manager"));
  1119. Label *fpath = memnew(Label(path));
  1120. fpath->set_name("path");
  1121. path_hb->add_child(fpath);
  1122. fpath->set_h_size_flags(SIZE_EXPAND_FILL);
  1123. fpath->set_modulate(Color(1, 1, 1, 0.5));
  1124. fpath->add_color_override("font_color", font_color);
  1125. fpath->set_clip_text(true);
  1126. scroll_children->add_child(hb);
  1127. }
  1128. for (Map<String, String>::Element *E = selected_list_copy.front(); E; E = E->next()) {
  1129. String key = E->key();
  1130. selected_list.erase(key);
  1131. }
  1132. scroll->set_v_scroll(0);
  1133. _update_project_buttons();
  1134. EditorSettings::get_singleton()->save();
  1135. tabs->set_current_tab(0);
  1136. }
  1137. void ProjectManager::_on_projects_updated() {
  1138. _load_recent_projects();
  1139. }
  1140. void ProjectManager::_on_project_created(const String &dir) {
  1141. project_filter->clear();
  1142. bool has_already = false;
  1143. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1144. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1145. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path_box/path")));
  1146. if (fpath->get_text() == dir) {
  1147. has_already = true;
  1148. break;
  1149. }
  1150. }
  1151. if (has_already) {
  1152. _update_scroll_position(dir);
  1153. } else {
  1154. _load_recent_projects();
  1155. _update_scroll_position(dir);
  1156. }
  1157. _open_selected_projects_ask();
  1158. }
  1159. void ProjectManager::_update_scroll_position(const String &dir) {
  1160. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1161. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1162. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path_box/path")));
  1163. if (fpath->get_text() == dir) {
  1164. last_clicked = hb->get_meta("name");
  1165. selected_list.clear();
  1166. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  1167. _update_project_buttons();
  1168. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  1169. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  1170. if (offset_diff > 0)
  1171. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. void ProjectManager::_confirm_update_settings() {
  1177. _open_selected_projects();
  1178. }
  1179. void ProjectManager::_open_selected_projects() {
  1180. for (const Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1181. const String &selected = E->key();
  1182. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1183. String conf = path.plus_file("project.godot");
  1184. if (!FileAccess::exists(conf)) {
  1185. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1186. dialog_error->popup_centered_minsize();
  1187. return;
  1188. }
  1189. print_line("Editing project: " + path + " (" + selected + ")");
  1190. List<String> args;
  1191. args.push_back("--path");
  1192. args.push_back(path);
  1193. args.push_back("--editor");
  1194. if (OS::get_singleton()->is_disable_crash_handler()) {
  1195. args.push_back("--disable-crash-handler");
  1196. }
  1197. String exec = OS::get_singleton()->get_executable_path();
  1198. OS::ProcessID pid = 0;
  1199. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1200. ERR_FAIL_COND(err);
  1201. }
  1202. get_tree()->quit();
  1203. }
  1204. void ProjectManager::_open_selected_projects_ask() {
  1205. if (selected_list.size() < 1) {
  1206. return;
  1207. }
  1208. if (selected_list.size() > 1) {
  1209. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  1210. multi_open_ask->popup_centered_minsize();
  1211. return;
  1212. }
  1213. // Update the project settings or don't open
  1214. String path = EditorSettings::get_singleton()->get("projects/" + selected_list.front()->key());
  1215. String conf = path.plus_file("project.godot");
  1216. // FIXME: We already parse those in _load_recent_projects, we could instead make
  1217. // its `projects` list global and reuse its parsed metadata here.
  1218. Ref<ConfigFile> cf = memnew(ConfigFile);
  1219. Error cf_err = cf->load(conf);
  1220. if (cf_err != OK) {
  1221. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1222. dialog_error->popup_centered_minsize();
  1223. return;
  1224. }
  1225. int config_version = (int)cf->get_value("", "config_version", 0);
  1226. // Check if the config_version property was empty or 0
  1227. if (config_version == 0) {
  1228. ask_update_settings->set_text(vformat(TTR("The following project settings file does not specify the version of Godot through which it was created.\n\n%s\n\nIf you proceed with opening it, it will be converted to Godot's current configuration file format.\nWarning: You won't be able to open the project with previous versions of the engine anymore."), conf));
  1229. ask_update_settings->popup_centered_minsize();
  1230. return;
  1231. }
  1232. // Check if we need to convert project settings from an earlier engine version
  1233. if (config_version < ProjectSettings::CONFIG_VERSION) {
  1234. ask_update_settings->set_text(vformat(TTR("The following project settings file was generated by an older engine version, and needs to be converted for this version:\n\n%s\n\nDo you want to convert it?\nWarning: You won't be able to open the project with previous versions of the engine anymore."), conf));
  1235. ask_update_settings->popup_centered_minsize();
  1236. return;
  1237. }
  1238. // Check if the file was generated by a newer, incompatible engine version
  1239. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1240. dialog_error->set_text(vformat(TTR("Can't open project at '%s'.") + "\n" + TTR("The project settings were created by a newer engine version, whose settings are not compatible with this version."), path));
  1241. dialog_error->popup_centered_minsize();
  1242. return;
  1243. }
  1244. // Open if the project is up-to-date
  1245. _open_selected_projects();
  1246. }
  1247. void ProjectManager::_run_project_confirm() {
  1248. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1249. const String &selected_main = E->get();
  1250. if (selected_main == "") {
  1251. run_error_diag->set_text(TTR("Can't run project: no main scene defined.\nPlease edit the project and set the main scene in the Project Settings under the \"Application\" category."));
  1252. run_error_diag->popup_centered();
  1253. return;
  1254. }
  1255. const String &selected = E->key();
  1256. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1257. if (!DirAccess::exists(path + "/.import")) {
  1258. run_error_diag->set_text(TTR("Can't run project: Assets need to be imported.\nPlease edit the project to trigger the initial import."));
  1259. run_error_diag->popup_centered();
  1260. return;
  1261. }
  1262. print_line("Running project: " + path + " (" + selected + ")");
  1263. List<String> args;
  1264. args.push_back("--path");
  1265. args.push_back(path);
  1266. if (OS::get_singleton()->is_disable_crash_handler()) {
  1267. args.push_back("--disable-crash-handler");
  1268. }
  1269. String exec = OS::get_singleton()->get_executable_path();
  1270. OS::ProcessID pid = 0;
  1271. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1272. ERR_FAIL_COND(err);
  1273. }
  1274. }
  1275. void ProjectManager::_run_project() {
  1276. if (selected_list.size() < 1) {
  1277. return;
  1278. }
  1279. if (selected_list.size() > 1) {
  1280. multi_run_ask->set_text(vformat(TTR("Are you sure to run %d projects at once?"), selected_list.size()));
  1281. multi_run_ask->popup_centered_minsize();
  1282. } else {
  1283. _run_project_confirm();
  1284. }
  1285. }
  1286. void ProjectManager::_show_project(const String &p_path) {
  1287. OS::get_singleton()->shell_open(String("file://") + p_path);
  1288. }
  1289. void ProjectManager::_scan_dir(DirAccess *da, float pos, float total, List<String> *r_projects) {
  1290. List<String> subdirs;
  1291. da->list_dir_begin();
  1292. String n = da->get_next();
  1293. while (n != String()) {
  1294. if (da->current_is_dir() && !n.begins_with(".")) {
  1295. subdirs.push_front(n);
  1296. } else if (n == "project.godot") {
  1297. r_projects->push_back(da->get_current_dir());
  1298. }
  1299. n = da->get_next();
  1300. }
  1301. da->list_dir_end();
  1302. int m = 0;
  1303. for (List<String>::Element *E = subdirs.front(); E; E = E->next()) {
  1304. da->change_dir(E->get());
  1305. float slice = total / subdirs.size();
  1306. _scan_dir(da, pos + slice * m, slice, r_projects);
  1307. da->change_dir("..");
  1308. m++;
  1309. }
  1310. }
  1311. void ProjectManager::_scan_begin(const String &p_base) {
  1312. print_line("Scanning projects at: " + p_base);
  1313. List<String> projects;
  1314. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1315. da->change_dir(p_base);
  1316. _scan_dir(da, 0, 1, &projects);
  1317. memdelete(da);
  1318. print_line("Found " + itos(projects.size()) + " projects.");
  1319. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  1320. String proj = E->get().replace("/", "::");
  1321. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  1322. }
  1323. EditorSettings::get_singleton()->save();
  1324. _load_recent_projects();
  1325. }
  1326. void ProjectManager::_scan_projects() {
  1327. scan_dir->popup_centered_ratio();
  1328. }
  1329. void ProjectManager::_new_project() {
  1330. npdialog->set_mode(ProjectDialog::MODE_NEW);
  1331. npdialog->show_dialog();
  1332. }
  1333. void ProjectManager::_import_project() {
  1334. npdialog->set_mode(ProjectDialog::MODE_IMPORT);
  1335. npdialog->show_dialog();
  1336. }
  1337. void ProjectManager::_rename_project() {
  1338. if (selected_list.size() == 0) {
  1339. return;
  1340. }
  1341. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1342. const String &selected = E->key();
  1343. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1344. npdialog->set_project_path(path);
  1345. npdialog->set_mode(ProjectDialog::MODE_RENAME);
  1346. npdialog->show_dialog();
  1347. }
  1348. }
  1349. void ProjectManager::_erase_project_confirm() {
  1350. if (selected_list.size() == 0) {
  1351. return;
  1352. }
  1353. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1354. EditorSettings::get_singleton()->erase("projects/" + E->key());
  1355. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  1356. }
  1357. EditorSettings::get_singleton()->save();
  1358. selected_list.clear();
  1359. last_clicked = "";
  1360. _load_recent_projects();
  1361. }
  1362. void ProjectManager::_erase_missing_projects_confirm() {
  1363. Map<String, String> list_all_projects;
  1364. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1365. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1366. if (hb) {
  1367. list_all_projects.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  1368. }
  1369. }
  1370. if (list_all_projects.size() == 0) {
  1371. return;
  1372. }
  1373. int deleted_projects = 0;
  1374. int remaining_projects = 0;
  1375. for (Map<String, String>::Element *E = list_all_projects.front(); E; E = E->next()) {
  1376. String project_name = E->key().replace(":::", ":/").replace("::", "/") + "/project.godot";
  1377. if (!FileAccess::exists(project_name)) {
  1378. deleted_projects++;
  1379. EditorSettings::get_singleton()->erase("projects/" + E->key());
  1380. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  1381. } else {
  1382. remaining_projects++;
  1383. }
  1384. }
  1385. print_line("Deleted " + itos(deleted_projects) + " projects, remaining " + itos(remaining_projects) + " projects");
  1386. EditorSettings::get_singleton()->save();
  1387. selected_list.clear();
  1388. last_clicked = "";
  1389. _load_recent_projects();
  1390. }
  1391. void ProjectManager::_erase_project() {
  1392. if (selected_list.size() == 0)
  1393. return;
  1394. String confirm_message;
  1395. if (selected_list.size() >= 2) {
  1396. confirm_message = vformat(TTR("Remove %d projects from the list?\nThe project folders' contents won't be modified."), selected_list.size());
  1397. } else {
  1398. confirm_message = TTR("Remove this project from the list?\nThe project folder's contents won't be modified.");
  1399. }
  1400. erase_ask->set_text(confirm_message);
  1401. erase_ask->popup_centered_minsize();
  1402. }
  1403. void ProjectManager::_erase_missing_projects() {
  1404. erase_missing_ask->set_text(TTR("Remove all missing projects from the list? (Folders contents will not be modified)"));
  1405. erase_missing_ask->popup_centered_minsize();
  1406. }
  1407. void ProjectManager::_language_selected(int p_id) {
  1408. String lang = language_btn->get_item_metadata(p_id);
  1409. EditorSettings::get_singleton()->set("interface/editor/editor_language", lang);
  1410. language_btn->set_text(lang);
  1411. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1412. language_restart_ask->set_text(TTR("Language changed.\nThe interface will update after restarting the editor or project manager."));
  1413. language_restart_ask->popup_centered();
  1414. }
  1415. void ProjectManager::_restart_confirm() {
  1416. List<String> args = OS::get_singleton()->get_cmdline_args();
  1417. String exec = OS::get_singleton()->get_executable_path();
  1418. OS::ProcessID pid = 0;
  1419. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1420. ERR_FAIL_COND(err);
  1421. get_tree()->quit();
  1422. }
  1423. void ProjectManager::_exit_dialog() {
  1424. get_tree()->quit();
  1425. }
  1426. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  1427. npdialog->set_mode(ProjectDialog::MODE_INSTALL);
  1428. npdialog->set_zip_path(p_zip_path);
  1429. npdialog->set_zip_title(p_title);
  1430. npdialog->show_dialog();
  1431. }
  1432. void ProjectManager::_files_dropped(PoolStringArray p_files, int p_screen) {
  1433. Set<String> folders_set;
  1434. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1435. for (int i = 0; i < p_files.size(); i++) {
  1436. String file = p_files[i];
  1437. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  1438. }
  1439. memdelete(da);
  1440. if (folders_set.size() > 0) {
  1441. PoolStringArray folders;
  1442. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  1443. folders.append(E->get());
  1444. }
  1445. bool confirm = true;
  1446. if (folders.size() == 1) {
  1447. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1448. if (dir->change_dir(folders[0]) == OK) {
  1449. dir->list_dir_begin();
  1450. String file = dir->get_next();
  1451. while (confirm && file != String()) {
  1452. if (!dir->current_is_dir() && file.ends_with("project.godot")) {
  1453. confirm = false;
  1454. }
  1455. file = dir->get_next();
  1456. }
  1457. dir->list_dir_end();
  1458. }
  1459. memdelete(dir);
  1460. }
  1461. if (confirm) {
  1462. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  1463. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  1464. multi_scan_ask->set_text(
  1465. vformat(TTR("Are you sure to scan %s folders for existing Godot projects?\nThis could take a while."), folders.size()));
  1466. multi_scan_ask->popup_centered_minsize();
  1467. } else {
  1468. _scan_multiple_folders(folders);
  1469. }
  1470. }
  1471. }
  1472. void ProjectManager::_scan_multiple_folders(PoolStringArray p_files) {
  1473. for (int i = 0; i < p_files.size(); i++) {
  1474. _scan_begin(p_files.get(i));
  1475. }
  1476. }
  1477. void ProjectManager::_bind_methods() {
  1478. ClassDB::bind_method("_open_selected_projects_ask", &ProjectManager::_open_selected_projects_ask);
  1479. ClassDB::bind_method("_open_selected_projects", &ProjectManager::_open_selected_projects);
  1480. ClassDB::bind_method("_run_project", &ProjectManager::_run_project);
  1481. ClassDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  1482. ClassDB::bind_method("_show_project", &ProjectManager::_show_project);
  1483. ClassDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  1484. ClassDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  1485. ClassDB::bind_method("_import_project", &ProjectManager::_import_project);
  1486. ClassDB::bind_method("_new_project", &ProjectManager::_new_project);
  1487. ClassDB::bind_method("_rename_project", &ProjectManager::_rename_project);
  1488. ClassDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  1489. ClassDB::bind_method("_erase_missing_projects", &ProjectManager::_erase_missing_projects);
  1490. ClassDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  1491. ClassDB::bind_method("_erase_missing_projects_confirm", &ProjectManager::_erase_missing_projects_confirm);
  1492. ClassDB::bind_method("_language_selected", &ProjectManager::_language_selected);
  1493. ClassDB::bind_method("_restart_confirm", &ProjectManager::_restart_confirm);
  1494. ClassDB::bind_method("_exit_dialog", &ProjectManager::_exit_dialog);
  1495. ClassDB::bind_method("_load_recent_projects", &ProjectManager::_load_recent_projects);
  1496. ClassDB::bind_method("_on_projects_updated", &ProjectManager::_on_projects_updated);
  1497. ClassDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  1498. ClassDB::bind_method("_update_scroll_position", &ProjectManager::_update_scroll_position);
  1499. ClassDB::bind_method("_panel_draw", &ProjectManager::_panel_draw);
  1500. ClassDB::bind_method("_panel_input", &ProjectManager::_panel_input);
  1501. ClassDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  1502. ClassDB::bind_method("_favorite_pressed", &ProjectManager::_favorite_pressed);
  1503. ClassDB::bind_method("_install_project", &ProjectManager::_install_project);
  1504. ClassDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  1505. ClassDB::bind_method("_open_asset_library", &ProjectManager::_open_asset_library);
  1506. ClassDB::bind_method("_confirm_update_settings", &ProjectManager::_confirm_update_settings);
  1507. ClassDB::bind_method(D_METHOD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  1508. }
  1509. void ProjectManager::_open_asset_library() {
  1510. asset_library->disable_community_support();
  1511. tabs->set_current_tab(1);
  1512. }
  1513. ProjectManager::ProjectManager() {
  1514. // load settings
  1515. if (!EditorSettings::get_singleton())
  1516. EditorSettings::create();
  1517. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  1518. {
  1519. int display_scale = EditorSettings::get_singleton()->get("interface/editor/display_scale");
  1520. float custom_display_scale = EditorSettings::get_singleton()->get("interface/editor/custom_display_scale");
  1521. switch (display_scale) {
  1522. case 0: {
  1523. // Try applying a suitable display scale automatically
  1524. const int screen = OS::get_singleton()->get_current_screen();
  1525. editor_set_scale(OS::get_singleton()->get_screen_dpi(screen) >= 192 && OS::get_singleton()->get_screen_size(screen).x > 2000 ? 2.0 : 1.0);
  1526. } break;
  1527. case 1: {
  1528. editor_set_scale(0.75);
  1529. } break;
  1530. case 2: {
  1531. editor_set_scale(1.0);
  1532. } break;
  1533. case 3: {
  1534. editor_set_scale(1.25);
  1535. } break;
  1536. case 4: {
  1537. editor_set_scale(1.5);
  1538. } break;
  1539. case 5: {
  1540. editor_set_scale(1.75);
  1541. } break;
  1542. case 6: {
  1543. editor_set_scale(2.0);
  1544. } break;
  1545. default: {
  1546. editor_set_scale(custom_display_scale);
  1547. } break;
  1548. }
  1549. #ifndef OSX_ENABLED
  1550. // The macOS platform implementation uses its own hiDPI window resizing code
  1551. // TODO: Resize windows on hiDPI displays on Windows and Linux and remove the line below
  1552. OS::get_singleton()->set_window_size(OS::get_singleton()->get_window_size() * MAX(1, EDSCALE));
  1553. #endif
  1554. }
  1555. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  1556. set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1557. set_theme(create_editor_theme());
  1558. gui_base = memnew(Control);
  1559. add_child(gui_base);
  1560. gui_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1561. gui_base->set_theme(create_custom_theme());
  1562. Panel *panel = memnew(Panel);
  1563. gui_base->add_child(panel);
  1564. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1565. panel->add_style_override("panel", gui_base->get_stylebox("Background", "EditorStyles"));
  1566. VBoxContainer *vb = memnew(VBoxContainer);
  1567. panel->add_child(vb);
  1568. vb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 8 * EDSCALE);
  1569. vb->add_constant_override("separation", 8 * EDSCALE);
  1570. String cp;
  1571. cp += 0xA9;
  1572. OS::get_singleton()->set_window_title(VERSION_NAME + String(" - ") + TTR("Project Manager") + " - " + cp + " 2007-2019 Juan Linietsky, Ariel Manzur & Godot Contributors");
  1573. HBoxContainer *top_hb = memnew(HBoxContainer);
  1574. vb->add_child(top_hb);
  1575. Label *l = memnew(Label);
  1576. l->set_text(VERSION_NAME + String(" - ") + TTR("Project Manager"));
  1577. top_hb->add_child(l);
  1578. top_hb->add_spacer();
  1579. l = memnew(Label);
  1580. String hash = String(VERSION_HASH);
  1581. if (hash.length() != 0)
  1582. hash = "." + hash.left(7);
  1583. l->set_text("v" VERSION_FULL_BUILD "" + hash);
  1584. l->set_align(Label::ALIGN_CENTER);
  1585. top_hb->add_child(l);
  1586. Control *center_box = memnew(Control);
  1587. center_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1588. vb->add_child(center_box);
  1589. tabs = memnew(TabContainer);
  1590. center_box->add_child(tabs);
  1591. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1592. HBoxContainer *tree_hb = memnew(HBoxContainer);
  1593. projects_hb = tree_hb;
  1594. projects_hb->set_name(TTR("Project List"));
  1595. tabs->add_child(tree_hb);
  1596. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  1597. tree_hb->add_child(search_tree_vb);
  1598. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1599. HBoxContainer *sort_filters = memnew(HBoxContainer);
  1600. Label *sort_label = memnew(Label);
  1601. sort_label->set_text(TTR("Sort:"));
  1602. sort_filters->add_child(sort_label);
  1603. Vector<String> sort_filter_titles;
  1604. sort_filter_titles.push_back("Name");
  1605. sort_filter_titles.push_back("Last Modified");
  1606. project_order_filter = memnew(ProjectListFilter);
  1607. project_order_filter->_setup_filters(sort_filter_titles);
  1608. project_order_filter->set_filter_size(150);
  1609. sort_filters->add_child(project_order_filter);
  1610. project_order_filter->connect("filter_changed", this, "_load_recent_projects");
  1611. project_order_filter->set_custom_minimum_size(Size2(180, 10) * EDSCALE);
  1612. int projects_sorting_order = (int)EditorSettings::get_singleton()->get("project_manager/sorting_order");
  1613. project_order_filter->set_filter_option((ProjectListFilter::FilterOption)projects_sorting_order);
  1614. sort_filters->add_spacer(true);
  1615. Label *search_label = memnew(Label);
  1616. search_label->set_text(TTR("Search:"));
  1617. sort_filters->add_child(search_label);
  1618. HBoxContainer *search_filters = memnew(HBoxContainer);
  1619. Vector<String> vec2;
  1620. vec2.push_back("Name");
  1621. vec2.push_back("Path");
  1622. project_filter = memnew(ProjectListFilter);
  1623. project_filter->_setup_filters(vec2);
  1624. project_filter->add_search_box();
  1625. search_filters->add_child(project_filter);
  1626. project_filter->connect("filter_changed", this, "_load_recent_projects");
  1627. project_filter->set_custom_minimum_size(Size2(280, 10) * EDSCALE);
  1628. sort_filters->add_child(search_filters);
  1629. search_tree_vb->add_child(sort_filters);
  1630. PanelContainer *pc = memnew(PanelContainer);
  1631. pc->add_style_override("panel", gui_base->get_stylebox("bg", "Tree"));
  1632. search_tree_vb->add_child(pc);
  1633. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  1634. scroll = memnew(ScrollContainer);
  1635. pc->add_child(scroll);
  1636. scroll->set_enable_h_scroll(false);
  1637. VBoxContainer *tree_vb = memnew(VBoxContainer);
  1638. tree_hb->add_child(tree_vb);
  1639. scroll_children = memnew(VBoxContainer);
  1640. scroll_children->set_h_size_flags(SIZE_EXPAND_FILL);
  1641. scroll->add_child(scroll_children);
  1642. Button *open = memnew(Button);
  1643. open->set_text(TTR("Edit"));
  1644. tree_vb->add_child(open);
  1645. open->connect("pressed", this, "_open_selected_projects_ask");
  1646. open_btn = open;
  1647. Button *run = memnew(Button);
  1648. run->set_text(TTR("Run"));
  1649. tree_vb->add_child(run);
  1650. run->connect("pressed", this, "_run_project");
  1651. run_btn = run;
  1652. tree_vb->add_child(memnew(HSeparator));
  1653. Button *scan = memnew(Button);
  1654. scan->set_text(TTR("Scan"));
  1655. tree_vb->add_child(scan);
  1656. scan->connect("pressed", this, "_scan_projects");
  1657. tree_vb->add_child(memnew(HSeparator));
  1658. scan_dir = memnew(FileDialog);
  1659. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  1660. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  1661. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  1662. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  1663. gui_base->add_child(scan_dir);
  1664. scan_dir->connect("dir_selected", this, "_scan_begin");
  1665. Button *create = memnew(Button);
  1666. create->set_text(TTR("New Project"));
  1667. tree_vb->add_child(create);
  1668. create->connect("pressed", this, "_new_project");
  1669. Button *import = memnew(Button);
  1670. import->set_text(TTR("Import"));
  1671. tree_vb->add_child(import);
  1672. import->connect("pressed", this, "_import_project");
  1673. Button *rename = memnew(Button);
  1674. rename->set_text(TTR("Rename"));
  1675. tree_vb->add_child(rename);
  1676. rename->connect("pressed", this, "_rename_project");
  1677. rename_btn = rename;
  1678. Button *erase = memnew(Button);
  1679. erase->set_text(TTR("Remove"));
  1680. tree_vb->add_child(erase);
  1681. erase->connect("pressed", this, "_erase_project");
  1682. erase_btn = erase;
  1683. Button *erase_missing = memnew(Button);
  1684. erase_missing->set_text(TTR("Remove Missing"));
  1685. tree_vb->add_child(erase_missing);
  1686. erase_missing->connect("pressed", this, "_erase_missing_projects");
  1687. erase_missing_btn = erase_missing;
  1688. tree_vb->add_spacer();
  1689. if (StreamPeerSSL::is_available()) {
  1690. asset_library = memnew(EditorAssetLibrary(true));
  1691. asset_library->set_name(TTR("Templates"));
  1692. tabs->add_child(asset_library);
  1693. asset_library->connect("install_asset", this, "_install_project");
  1694. } else {
  1695. WARN_PRINT("Asset Library not available, as it requires SSL to work.");
  1696. }
  1697. HBoxContainer *settings_hb = memnew(HBoxContainer);
  1698. settings_hb->set_alignment(BoxContainer::ALIGN_END);
  1699. settings_hb->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  1700. language_btn = memnew(OptionButton);
  1701. language_btn->set_flat(true);
  1702. language_btn->set_focus_mode(Control::FOCUS_NONE);
  1703. Vector<String> editor_languages;
  1704. List<PropertyInfo> editor_settings_properties;
  1705. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  1706. for (List<PropertyInfo>::Element *E = editor_settings_properties.front(); E; E = E->next()) {
  1707. PropertyInfo &pi = E->get();
  1708. if (pi.name == "interface/editor/editor_language") {
  1709. editor_languages = pi.hint_string.split(",");
  1710. }
  1711. }
  1712. String current_lang = EditorSettings::get_singleton()->get("interface/editor/editor_language");
  1713. for (int i = 0; i < editor_languages.size(); i++) {
  1714. String lang = editor_languages[i];
  1715. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang);
  1716. language_btn->add_item(lang_name + " [" + lang + "]", i);
  1717. language_btn->set_item_metadata(i, lang);
  1718. if (current_lang == lang) {
  1719. language_btn->select(i);
  1720. language_btn->set_text(lang);
  1721. }
  1722. }
  1723. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1724. settings_hb->add_child(language_btn);
  1725. language_btn->connect("item_selected", this, "_language_selected");
  1726. center_box->add_child(settings_hb);
  1727. settings_hb->set_anchors_and_margins_preset(Control::PRESET_TOP_RIGHT);
  1728. CenterContainer *cc = memnew(CenterContainer);
  1729. Button *cancel = memnew(Button);
  1730. cancel->set_text(TTR("Exit"));
  1731. cancel->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1732. cc->add_child(cancel);
  1733. cancel->connect("pressed", this, "_exit_dialog");
  1734. vb->add_child(cc);
  1735. //////////////////////////////////////////////////////////////
  1736. language_restart_ask = memnew(ConfirmationDialog);
  1737. language_restart_ask->get_ok()->set_text(TTR("Restart Now"));
  1738. language_restart_ask->get_ok()->connect("pressed", this, "_restart_confirm");
  1739. language_restart_ask->get_cancel()->set_text(TTR("Continue"));
  1740. gui_base->add_child(language_restart_ask);
  1741. erase_missing_ask = memnew(ConfirmationDialog);
  1742. erase_missing_ask->get_ok()->set_text(TTR("Remove All"));
  1743. erase_missing_ask->get_ok()->connect("pressed", this, "_erase_missing_projects_confirm");
  1744. gui_base->add_child(erase_missing_ask);
  1745. erase_ask = memnew(ConfirmationDialog);
  1746. erase_ask->get_ok()->set_text(TTR("Remove"));
  1747. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  1748. gui_base->add_child(erase_ask);
  1749. multi_open_ask = memnew(ConfirmationDialog);
  1750. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  1751. multi_open_ask->get_ok()->connect("pressed", this, "_open_selected_projects");
  1752. gui_base->add_child(multi_open_ask);
  1753. multi_run_ask = memnew(ConfirmationDialog);
  1754. multi_run_ask->get_ok()->set_text(TTR("Run"));
  1755. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  1756. gui_base->add_child(multi_run_ask);
  1757. multi_scan_ask = memnew(ConfirmationDialog);
  1758. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  1759. gui_base->add_child(multi_scan_ask);
  1760. ask_update_settings = memnew(ConfirmationDialog);
  1761. ask_update_settings->get_ok()->connect("pressed", this, "_confirm_update_settings");
  1762. gui_base->add_child(ask_update_settings);
  1763. OS::get_singleton()->set_low_processor_usage_mode(true);
  1764. npdialog = memnew(ProjectDialog);
  1765. gui_base->add_child(npdialog);
  1766. npdialog->connect("projects_updated", this, "_on_projects_updated");
  1767. npdialog->connect("project_created", this, "_on_project_created");
  1768. _load_recent_projects();
  1769. if (EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path")) {
  1770. _scan_begin(EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path"));
  1771. }
  1772. last_clicked = "";
  1773. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  1774. run_error_diag = memnew(AcceptDialog);
  1775. gui_base->add_child(run_error_diag);
  1776. run_error_diag->set_title(TTR("Can't run project"));
  1777. dialog_error = memnew(AcceptDialog);
  1778. gui_base->add_child(dialog_error);
  1779. open_templates = memnew(ConfirmationDialog);
  1780. open_templates->set_text(TTR("You currently don't have any projects.\nWould you like to explore official example projects in the Asset Library?"));
  1781. open_templates->get_ok()->set_text(TTR("Open Asset Library"));
  1782. open_templates->connect("confirmed", this, "_open_asset_library");
  1783. add_child(open_templates);
  1784. }
  1785. ProjectManager::~ProjectManager() {
  1786. if (EditorSettings::get_singleton())
  1787. EditorSettings::destroy();
  1788. }
  1789. void ProjectListFilter::_setup_filters(Vector<String> options) {
  1790. filter_option->clear();
  1791. for (int i = 0; i < options.size(); i++)
  1792. filter_option->add_item(TTR(options[i]));
  1793. }
  1794. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  1795. emit_signal("filter_changed");
  1796. }
  1797. String ProjectListFilter::get_search_term() {
  1798. return search_box->get_text().strip_edges();
  1799. }
  1800. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  1801. return _current_filter;
  1802. }
  1803. void ProjectListFilter::set_filter_option(FilterOption option) {
  1804. filter_option->select((int)option);
  1805. _filter_option_selected(0);
  1806. }
  1807. void ProjectListFilter::_filter_option_selected(int p_idx) {
  1808. FilterOption selected = (FilterOption)(filter_option->get_selected());
  1809. if (_current_filter != selected) {
  1810. _current_filter = selected;
  1811. emit_signal("filter_changed");
  1812. }
  1813. }
  1814. void ProjectListFilter::_notification(int p_what) {
  1815. if (p_what == NOTIFICATION_ENTER_TREE && has_search_box) {
  1816. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  1817. search_box->set_clear_button_enabled(true);
  1818. }
  1819. }
  1820. void ProjectListFilter::_bind_methods() {
  1821. ClassDB::bind_method(D_METHOD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  1822. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  1823. ADD_SIGNAL(MethodInfo("filter_changed"));
  1824. }
  1825. void ProjectListFilter::add_search_box() {
  1826. search_box = memnew(LineEdit);
  1827. search_box->connect("text_changed", this, "_search_text_changed");
  1828. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1829. add_child(search_box);
  1830. has_search_box = true;
  1831. }
  1832. void ProjectListFilter::set_filter_size(int h_size) {
  1833. filter_option->set_custom_minimum_size(Size2(h_size * EDSCALE, 10 * EDSCALE));
  1834. }
  1835. ProjectListFilter::ProjectListFilter() {
  1836. _current_filter = FILTER_NAME;
  1837. filter_option = memnew(OptionButton);
  1838. set_filter_size(80);
  1839. filter_option->set_clip_text(true);
  1840. filter_option->connect("item_selected", this, "_filter_option_selected");
  1841. add_child(filter_option);
  1842. has_search_box = false;
  1843. }
  1844. void ProjectListFilter::clear() {
  1845. if (has_search_box) {
  1846. search_box->clear();
  1847. }
  1848. }