project_manager.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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->hide();
  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. }
  796. void ProjectManager::_panel_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  797. Ref<InputEventMouseButton> mb = p_ev;
  798. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  799. String clicked = p_hb->get_meta("name");
  800. String clicked_main_scene = p_hb->get_meta("main_scene");
  801. if (mb->get_shift() && selected_list.size() > 0 && last_clicked != "" && clicked != last_clicked) {
  802. int clicked_id = -1;
  803. int last_clicked_id = -1;
  804. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  805. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  806. if (!hb) continue;
  807. if (hb->get_meta("name") == clicked) clicked_id = i;
  808. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  809. }
  810. if (last_clicked_id != -1 && clicked_id != -1) {
  811. int min = clicked_id < last_clicked_id ? clicked_id : last_clicked_id;
  812. int max = clicked_id > last_clicked_id ? clicked_id : last_clicked_id;
  813. for (int i = 0; i < scroll_children->get_child_count(); ++i) {
  814. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  815. if (!hb) continue;
  816. if (i != clicked_id && (i < min || i > max) && !mb->get_control()) {
  817. selected_list.erase(hb->get_meta("name"));
  818. } else if (i >= min && i <= max) {
  819. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  820. }
  821. }
  822. }
  823. } else if (selected_list.has(clicked) && mb->get_control()) {
  824. selected_list.erase(clicked);
  825. } else {
  826. last_clicked = clicked;
  827. if (mb->get_control() || selected_list.size() == 0) {
  828. selected_list.insert(clicked, clicked_main_scene);
  829. } else {
  830. selected_list.clear();
  831. selected_list.insert(clicked, clicked_main_scene);
  832. }
  833. }
  834. _update_project_buttons();
  835. if (mb->is_doubleclick())
  836. _open_selected_projects_ask(); //open if doubleclicked
  837. }
  838. }
  839. void ProjectManager::_unhandled_input(const Ref<InputEvent> &p_ev) {
  840. Ref<InputEventKey> k = p_ev;
  841. if (k.is_valid()) {
  842. if (!k->is_pressed())
  843. return;
  844. if (tabs->get_current_tab() != 0)
  845. return;
  846. bool scancode_handled = true;
  847. switch (k->get_scancode()) {
  848. case KEY_ENTER: {
  849. _open_selected_projects_ask();
  850. } break;
  851. case KEY_DELETE: {
  852. _erase_project();
  853. } break;
  854. case KEY_HOME: {
  855. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  856. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  857. if (hb) {
  858. selected_list.clear();
  859. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  860. scroll->set_v_scroll(0);
  861. _update_project_buttons();
  862. break;
  863. }
  864. }
  865. } break;
  866. case KEY_END: {
  867. for (int i = scroll_children->get_child_count() - 1; i >= 0; i--) {
  868. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  869. if (hb) {
  870. selected_list.clear();
  871. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  872. scroll->set_v_scroll(scroll_children->get_size().y);
  873. _update_project_buttons();
  874. break;
  875. }
  876. }
  877. } break;
  878. case KEY_UP: {
  879. if (k->get_shift())
  880. break;
  881. if (selected_list.size()) {
  882. bool found = false;
  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) continue;
  886. String current = hb->get_meta("name");
  887. if (found) {
  888. selected_list.clear();
  889. selected_list.insert(current, hb->get_meta("main_scene"));
  890. int offset_diff = scroll->get_v_scroll() - hb->get_position().y;
  891. if (offset_diff > 0)
  892. scroll->set_v_scroll(scroll->get_v_scroll() - offset_diff);
  893. _update_project_buttons();
  894. break;
  895. } else if (current == selected_list.back()->key()) {
  896. found = true;
  897. }
  898. }
  899. break;
  900. }
  901. FALLTHROUGH;
  902. }
  903. case KEY_DOWN: {
  904. if (k->get_shift())
  905. break;
  906. bool found = selected_list.empty();
  907. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  908. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  909. if (!hb) continue;
  910. String current = hb->get_meta("name");
  911. if (found) {
  912. selected_list.clear();
  913. selected_list.insert(current, hb->get_meta("main_scene"));
  914. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  915. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  916. if (offset_diff > 0)
  917. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  918. _update_project_buttons();
  919. break;
  920. } else if (current == selected_list.back()->key()) {
  921. found = true;
  922. }
  923. }
  924. } break;
  925. case KEY_F: {
  926. if (k->get_command())
  927. this->project_filter->search_box->grab_focus();
  928. else
  929. scancode_handled = false;
  930. } break;
  931. default: {
  932. scancode_handled = false;
  933. } break;
  934. }
  935. if (scancode_handled) {
  936. accept_event();
  937. }
  938. }
  939. }
  940. void ProjectManager::_favorite_pressed(Node *p_hb) {
  941. String clicked = p_hb->get_meta("name");
  942. bool favorite = !p_hb->get_meta("favorite");
  943. String proj = clicked.replace(":::", ":/");
  944. proj = proj.replace("::", "/");
  945. if (favorite) {
  946. EditorSettings::get_singleton()->set("favorite_projects/" + clicked, proj);
  947. } else {
  948. EditorSettings::get_singleton()->erase("favorite_projects/" + clicked);
  949. }
  950. EditorSettings::get_singleton()->save();
  951. call_deferred("_load_recent_projects");
  952. }
  953. void ProjectManager::_load_recent_projects() {
  954. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  955. String search_term = project_filter->get_search_term();
  956. while (scroll_children->get_child_count() > 0) {
  957. memdelete(scroll_children->get_child(0));
  958. }
  959. Map<String, String> selected_list_copy = selected_list;
  960. List<PropertyInfo> properties;
  961. EditorSettings::get_singleton()->get_property_list(&properties);
  962. Color font_color = gui_base->get_color("font_color", "Tree");
  963. bool set_ordered_latest_modification;
  964. ProjectListFilter::FilterOption filter_order_option = project_order_filter->get_filter_option();
  965. if (filter_order_option == ProjectListFilter::FILTER_NAME) {
  966. set_ordered_latest_modification = false;
  967. } else {
  968. set_ordered_latest_modification = true;
  969. }
  970. EditorSettings::get_singleton()->set("project_manager/sorting_order", (int)filter_order_option);
  971. List<ProjectItem> projects;
  972. List<ProjectItem> favorite_projects;
  973. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  974. String _name = E->get().name;
  975. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  976. continue;
  977. String path = EditorSettings::get_singleton()->get(_name);
  978. if (filter_option == ProjectListFilter::FILTER_PATH && search_term != "" && path.findn(search_term) == -1)
  979. continue;
  980. String project = _name.get_slice("/", 1);
  981. String conf = path.plus_file("project.godot");
  982. int config_version = 0; // Assume 0 until we know better
  983. bool favorite = (_name.begins_with("favorite_projects/")) ? true : false;
  984. bool grayed = false;
  985. uint64_t last_modified = 0;
  986. if (FileAccess::exists(conf)) {
  987. last_modified = FileAccess::get_modified_time(conf);
  988. String fscache = path.plus_file(".fscache");
  989. if (FileAccess::exists(fscache)) {
  990. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  991. if (cache_modified > last_modified)
  992. last_modified = cache_modified;
  993. }
  994. } else {
  995. grayed = true;
  996. }
  997. ProjectItem item(project, path, conf, config_version, last_modified, favorite, grayed, set_ordered_latest_modification);
  998. if (favorite)
  999. favorite_projects.push_back(item);
  1000. else
  1001. projects.push_back(item);
  1002. }
  1003. projects.sort();
  1004. favorite_projects.sort();
  1005. for (List<ProjectItem>::Element *E = projects.front(); E;) {
  1006. List<ProjectItem>::Element *next = E->next();
  1007. if (favorite_projects.find(E->get()) != NULL)
  1008. projects.erase(E->get());
  1009. E = next;
  1010. }
  1011. for (List<ProjectItem>::Element *E = favorite_projects.back(); E; E = E->prev()) {
  1012. projects.push_front(E->get());
  1013. }
  1014. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  1015. for (List<ProjectItem>::Element *E = projects.front(); E; E = E->next()) {
  1016. ProjectItem &item = E->get();
  1017. String project = item.project;
  1018. String path = item.path;
  1019. String conf = item.conf;
  1020. Ref<ConfigFile> cf = memnew(ConfigFile);
  1021. Error cf_err = cf->load(conf);
  1022. String project_name = TTR("Unnamed Project");
  1023. if (cf_err == OK && cf->has_section_key("application", "config/name")) {
  1024. project_name = static_cast<String>(cf->get_value("application", "config/name")).xml_unescape();
  1025. }
  1026. if (filter_option == ProjectListFilter::FILTER_NAME && search_term != "" && project_name.findn(search_term) == -1)
  1027. continue;
  1028. Ref<Texture> icon;
  1029. String main_scene;
  1030. if (cf_err == OK) {
  1031. item.config_version = (int)cf->get_value("", "config_version", 0);
  1032. if (item.config_version > ProjectSettings::CONFIG_VERSION) {
  1033. // Comes from an incompatible (more recent) Godot version, grey it out
  1034. item.grayed = true;
  1035. }
  1036. String appicon = cf->get_value("application", "config/icon", "");
  1037. if (appicon != "") {
  1038. Ref<Image> img;
  1039. img.instance();
  1040. Error err = img->load(appicon.replace_first("res://", path + "/"));
  1041. if (err == OK) {
  1042. Ref<Texture> default_icon = get_icon("DefaultProjectIcon", "EditorIcons");
  1043. img->resize(default_icon->get_width(), default_icon->get_height());
  1044. Ref<ImageTexture> it = memnew(ImageTexture);
  1045. it->create_from_image(img);
  1046. icon = it;
  1047. }
  1048. }
  1049. main_scene = cf->get_value("application", "run/main_scene", "");
  1050. }
  1051. if (icon.is_null()) {
  1052. icon = get_icon("DefaultProjectIcon", "EditorIcons");
  1053. }
  1054. selected_list_copy.erase(project);
  1055. bool is_favorite = item.favorite;
  1056. bool is_grayed = item.grayed;
  1057. HBoxContainer *hb = memnew(HBoxContainer);
  1058. hb->set_meta("name", project);
  1059. hb->set_meta("main_scene", main_scene);
  1060. hb->set_meta("favorite", is_favorite);
  1061. hb->connect("draw", this, "_panel_draw", varray(hb));
  1062. hb->connect("gui_input", this, "_panel_input", varray(hb));
  1063. hb->add_constant_override("separation", 10 * EDSCALE);
  1064. VBoxContainer *favorite_box = memnew(VBoxContainer);
  1065. TextureButton *favorite = memnew(TextureButton);
  1066. favorite->set_normal_texture(favorite_icon);
  1067. if (!is_favorite)
  1068. favorite->set_modulate(Color(1, 1, 1, 0.2));
  1069. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  1070. favorite_box->add_child(favorite);
  1071. favorite_box->set_alignment(BoxContainer::ALIGN_CENTER);
  1072. hb->add_child(favorite_box);
  1073. TextureRect *tf = memnew(TextureRect);
  1074. tf->set_texture(icon);
  1075. hb->add_child(tf);
  1076. VBoxContainer *vb = memnew(VBoxContainer);
  1077. if (is_grayed)
  1078. vb->set_modulate(Color(0.5, 0.5, 0.5));
  1079. vb->set_name("project");
  1080. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1081. hb->add_child(vb);
  1082. Control *ec = memnew(Control);
  1083. ec->set_custom_minimum_size(Size2(0, 1));
  1084. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  1085. vb->add_child(ec);
  1086. Label *title = memnew(Label(project_name));
  1087. title->add_font_override("font", gui_base->get_font("title", "EditorFonts"));
  1088. title->add_color_override("font_color", font_color);
  1089. title->set_clip_text(true);
  1090. vb->add_child(title);
  1091. HBoxContainer *path_hb = memnew(HBoxContainer);
  1092. path_hb->set_name("path_box");
  1093. path_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1094. vb->add_child(path_hb);
  1095. Button *show = memnew(Button);
  1096. show->set_name("show");
  1097. show->set_icon(get_icon("Filesystem", "EditorIcons"));
  1098. show->set_flat(true);
  1099. show->set_modulate(Color(1, 1, 1, 0.5));
  1100. path_hb->add_child(show);
  1101. show->connect("pressed", this, "_show_project", varray(path));
  1102. show->set_tooltip(TTR("Show in File Manager"));
  1103. Label *fpath = memnew(Label(path));
  1104. fpath->set_name("path");
  1105. path_hb->add_child(fpath);
  1106. fpath->set_h_size_flags(SIZE_EXPAND_FILL);
  1107. fpath->set_modulate(Color(1, 1, 1, 0.5));
  1108. fpath->add_color_override("font_color", font_color);
  1109. fpath->set_clip_text(true);
  1110. scroll_children->add_child(hb);
  1111. }
  1112. for (Map<String, String>::Element *E = selected_list_copy.front(); E; E = E->next()) {
  1113. String key = E->key();
  1114. selected_list.erase(key);
  1115. }
  1116. scroll->set_v_scroll(0);
  1117. _update_project_buttons();
  1118. EditorSettings::get_singleton()->save();
  1119. tabs->set_current_tab(0);
  1120. }
  1121. void ProjectManager::_on_projects_updated() {
  1122. _load_recent_projects();
  1123. }
  1124. void ProjectManager::_on_project_created(const String &dir) {
  1125. project_filter->clear();
  1126. bool has_already = false;
  1127. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1128. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1129. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path_box/path")));
  1130. if (fpath->get_text() == dir) {
  1131. has_already = true;
  1132. break;
  1133. }
  1134. }
  1135. if (has_already) {
  1136. _update_scroll_position(dir);
  1137. } else {
  1138. _load_recent_projects();
  1139. _update_scroll_position(dir);
  1140. }
  1141. _open_selected_projects_ask();
  1142. }
  1143. void ProjectManager::_update_scroll_position(const String &dir) {
  1144. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1145. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1146. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path_box/path")));
  1147. if (fpath->get_text() == dir) {
  1148. last_clicked = hb->get_meta("name");
  1149. selected_list.clear();
  1150. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  1151. _update_project_buttons();
  1152. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  1153. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  1154. if (offset_diff > 0)
  1155. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  1156. break;
  1157. }
  1158. }
  1159. }
  1160. void ProjectManager::_confirm_update_settings() {
  1161. _open_selected_projects();
  1162. }
  1163. void ProjectManager::_open_selected_projects() {
  1164. for (const Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1165. const String &selected = E->key();
  1166. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1167. String conf = path.plus_file("project.godot");
  1168. if (!FileAccess::exists(conf)) {
  1169. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1170. dialog_error->popup_centered_minsize();
  1171. return;
  1172. }
  1173. print_line("Editing project: " + path + " (" + selected + ")");
  1174. List<String> args;
  1175. args.push_back("--path");
  1176. args.push_back(path);
  1177. args.push_back("--editor");
  1178. if (OS::get_singleton()->is_disable_crash_handler()) {
  1179. args.push_back("--disable-crash-handler");
  1180. }
  1181. String exec = OS::get_singleton()->get_executable_path();
  1182. OS::ProcessID pid = 0;
  1183. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1184. ERR_FAIL_COND(err);
  1185. }
  1186. get_tree()->quit();
  1187. }
  1188. void ProjectManager::_open_selected_projects_ask() {
  1189. if (selected_list.size() < 1) {
  1190. return;
  1191. }
  1192. if (selected_list.size() > 1) {
  1193. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  1194. multi_open_ask->popup_centered_minsize();
  1195. return;
  1196. }
  1197. // Update the project settings or don't open
  1198. String path = EditorSettings::get_singleton()->get("projects/" + selected_list.front()->key());
  1199. String conf = path.plus_file("project.godot");
  1200. // FIXME: We already parse those in _load_recent_projects, we could instead make
  1201. // its `projects` list global and reuse its parsed metadata here.
  1202. Ref<ConfigFile> cf = memnew(ConfigFile);
  1203. Error cf_err = cf->load(conf);
  1204. if (cf_err != OK) {
  1205. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1206. dialog_error->popup_centered_minsize();
  1207. return;
  1208. }
  1209. int config_version = (int)cf->get_value("", "config_version", 0);
  1210. // Check if the config_version property was empty or 0
  1211. if (config_version == 0) {
  1212. 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 will not be able to open the project with previous versions of the engine anymore."), conf));
  1213. ask_update_settings->popup_centered_minsize();
  1214. return;
  1215. }
  1216. // Check if we need to convert project settings from an earlier engine version
  1217. if (config_version < ProjectSettings::CONFIG_VERSION) {
  1218. 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 will not be able to open the project with previous versions of the engine anymore."), conf));
  1219. ask_update_settings->popup_centered_minsize();
  1220. return;
  1221. }
  1222. // Check if the file was generated by a newer, incompatible engine version
  1223. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1224. 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));
  1225. dialog_error->popup_centered_minsize();
  1226. return;
  1227. }
  1228. // Open if the project is up-to-date
  1229. _open_selected_projects();
  1230. }
  1231. void ProjectManager::_run_project_confirm() {
  1232. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1233. const String &selected_main = E->get();
  1234. if (selected_main == "") {
  1235. run_error_diag->set_text(TTR("Can't run project: no main scene defined.\nPlease edit the project and set the main scene in \"Project Settings\" under the \"Application\" category."));
  1236. run_error_diag->popup_centered();
  1237. return;
  1238. }
  1239. const String &selected = E->key();
  1240. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1241. if (!DirAccess::exists(path + "/.import")) {
  1242. run_error_diag->set_text(TTR("Can't run project: Assets need to be imported.\nPlease edit the project to trigger the initial import."));
  1243. run_error_diag->popup_centered();
  1244. return;
  1245. }
  1246. print_line("Running project: " + path + " (" + selected + ")");
  1247. List<String> args;
  1248. args.push_back("--path");
  1249. args.push_back(path);
  1250. if (OS::get_singleton()->is_disable_crash_handler()) {
  1251. args.push_back("--disable-crash-handler");
  1252. }
  1253. String exec = OS::get_singleton()->get_executable_path();
  1254. OS::ProcessID pid = 0;
  1255. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1256. ERR_FAIL_COND(err);
  1257. }
  1258. }
  1259. void ProjectManager::_run_project() {
  1260. if (selected_list.size() < 1) {
  1261. return;
  1262. }
  1263. if (selected_list.size() > 1) {
  1264. multi_run_ask->set_text(TTR("Are you sure to run more than one project?"));
  1265. multi_run_ask->popup_centered_minsize();
  1266. } else {
  1267. _run_project_confirm();
  1268. }
  1269. }
  1270. void ProjectManager::_show_project(const String &p_path) {
  1271. OS::get_singleton()->shell_open(String("file://") + p_path);
  1272. }
  1273. void ProjectManager::_scan_dir(const String &path, List<String> *r_projects) {
  1274. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1275. da->change_dir(path);
  1276. da->list_dir_begin();
  1277. String n = da->get_next();
  1278. while (n != String()) {
  1279. if (da->current_is_dir() && !n.begins_with(".")) {
  1280. _scan_dir(da->get_current_dir().plus_file(n), r_projects);
  1281. } else if (n == "project.godot") {
  1282. r_projects->push_back(da->get_current_dir());
  1283. }
  1284. n = da->get_next();
  1285. }
  1286. da->list_dir_end();
  1287. memdelete(da);
  1288. }
  1289. void ProjectManager::_scan_begin(const String &p_base) {
  1290. print_line("Scanning projects at: " + p_base);
  1291. List<String> projects;
  1292. _scan_dir(p_base, &projects);
  1293. print_line("Found " + itos(projects.size()) + " projects.");
  1294. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  1295. String proj = E->get().replace("/", "::");
  1296. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  1297. }
  1298. EditorSettings::get_singleton()->save();
  1299. _load_recent_projects();
  1300. }
  1301. void ProjectManager::_scan_projects() {
  1302. scan_dir->popup_centered_ratio();
  1303. }
  1304. void ProjectManager::_new_project() {
  1305. npdialog->set_mode(ProjectDialog::MODE_NEW);
  1306. npdialog->show_dialog();
  1307. }
  1308. void ProjectManager::_import_project() {
  1309. npdialog->set_mode(ProjectDialog::MODE_IMPORT);
  1310. npdialog->show_dialog();
  1311. }
  1312. void ProjectManager::_rename_project() {
  1313. if (selected_list.size() == 0) {
  1314. return;
  1315. }
  1316. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1317. const String &selected = E->key();
  1318. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1319. npdialog->set_project_path(path);
  1320. npdialog->set_mode(ProjectDialog::MODE_RENAME);
  1321. npdialog->show_dialog();
  1322. }
  1323. }
  1324. void ProjectManager::_erase_project_confirm() {
  1325. if (selected_list.size() == 0) {
  1326. return;
  1327. }
  1328. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1329. EditorSettings::get_singleton()->erase("projects/" + E->key());
  1330. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  1331. }
  1332. EditorSettings::get_singleton()->save();
  1333. selected_list.clear();
  1334. last_clicked = "";
  1335. _load_recent_projects();
  1336. }
  1337. void ProjectManager::_erase_project() {
  1338. if (selected_list.size() == 0)
  1339. return;
  1340. erase_ask->set_text(TTR("Remove project from the list? (Folder contents will not be modified)"));
  1341. erase_ask->popup_centered_minsize();
  1342. }
  1343. void ProjectManager::_language_selected(int p_id) {
  1344. String lang = language_btn->get_item_metadata(p_id);
  1345. EditorSettings::get_singleton()->set("interface/editor/editor_language", lang);
  1346. language_btn->set_text(lang);
  1347. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1348. language_restart_ask->set_text(TTR("Language changed.\nThe UI will update next time the editor or project manager starts."));
  1349. language_restart_ask->popup_centered();
  1350. }
  1351. void ProjectManager::_restart_confirm() {
  1352. List<String> args = OS::get_singleton()->get_cmdline_args();
  1353. String exec = OS::get_singleton()->get_executable_path();
  1354. OS::ProcessID pid = 0;
  1355. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1356. ERR_FAIL_COND(err);
  1357. get_tree()->quit();
  1358. }
  1359. void ProjectManager::_exit_dialog() {
  1360. get_tree()->quit();
  1361. }
  1362. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  1363. npdialog->set_mode(ProjectDialog::MODE_INSTALL);
  1364. npdialog->set_zip_path(p_zip_path);
  1365. npdialog->set_zip_title(p_title);
  1366. npdialog->show_dialog();
  1367. }
  1368. void ProjectManager::_files_dropped(PoolStringArray p_files, int p_screen) {
  1369. Set<String> folders_set;
  1370. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1371. for (int i = 0; i < p_files.size(); i++) {
  1372. String file = p_files[i];
  1373. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  1374. }
  1375. memdelete(da);
  1376. if (folders_set.size() > 0) {
  1377. PoolStringArray folders;
  1378. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  1379. folders.append(E->get());
  1380. }
  1381. bool confirm = true;
  1382. if (folders.size() == 1) {
  1383. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1384. if (dir->change_dir(folders[0]) == OK) {
  1385. dir->list_dir_begin();
  1386. String file = dir->get_next();
  1387. while (confirm && file != String()) {
  1388. if (!dir->current_is_dir() && file.ends_with("project.godot")) {
  1389. confirm = false;
  1390. }
  1391. file = dir->get_next();
  1392. }
  1393. dir->list_dir_end();
  1394. }
  1395. memdelete(dir);
  1396. }
  1397. if (confirm) {
  1398. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  1399. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  1400. multi_scan_ask->set_text(vformat(TTR("You are about the scan %s folders for existing Godot projects. Do you confirm?"), folders.size()));
  1401. multi_scan_ask->popup_centered_minsize();
  1402. } else {
  1403. _scan_multiple_folders(folders);
  1404. }
  1405. }
  1406. }
  1407. void ProjectManager::_scan_multiple_folders(PoolStringArray p_files) {
  1408. for (int i = 0; i < p_files.size(); i++) {
  1409. _scan_begin(p_files.get(i));
  1410. }
  1411. }
  1412. void ProjectManager::_bind_methods() {
  1413. ClassDB::bind_method("_open_selected_projects_ask", &ProjectManager::_open_selected_projects_ask);
  1414. ClassDB::bind_method("_open_selected_projects", &ProjectManager::_open_selected_projects);
  1415. ClassDB::bind_method("_run_project", &ProjectManager::_run_project);
  1416. ClassDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  1417. ClassDB::bind_method("_show_project", &ProjectManager::_show_project);
  1418. ClassDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  1419. ClassDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  1420. ClassDB::bind_method("_import_project", &ProjectManager::_import_project);
  1421. ClassDB::bind_method("_new_project", &ProjectManager::_new_project);
  1422. ClassDB::bind_method("_rename_project", &ProjectManager::_rename_project);
  1423. ClassDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  1424. ClassDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  1425. ClassDB::bind_method("_language_selected", &ProjectManager::_language_selected);
  1426. ClassDB::bind_method("_restart_confirm", &ProjectManager::_restart_confirm);
  1427. ClassDB::bind_method("_exit_dialog", &ProjectManager::_exit_dialog);
  1428. ClassDB::bind_method("_load_recent_projects", &ProjectManager::_load_recent_projects);
  1429. ClassDB::bind_method("_on_projects_updated", &ProjectManager::_on_projects_updated);
  1430. ClassDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  1431. ClassDB::bind_method("_update_scroll_position", &ProjectManager::_update_scroll_position);
  1432. ClassDB::bind_method("_panel_draw", &ProjectManager::_panel_draw);
  1433. ClassDB::bind_method("_panel_input", &ProjectManager::_panel_input);
  1434. ClassDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  1435. ClassDB::bind_method("_favorite_pressed", &ProjectManager::_favorite_pressed);
  1436. ClassDB::bind_method("_install_project", &ProjectManager::_install_project);
  1437. ClassDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  1438. ClassDB::bind_method("_open_asset_library", &ProjectManager::_open_asset_library);
  1439. ClassDB::bind_method("_confirm_update_settings", &ProjectManager::_confirm_update_settings);
  1440. ClassDB::bind_method(D_METHOD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  1441. }
  1442. void ProjectManager::_open_asset_library() {
  1443. asset_library->disable_community_support();
  1444. tabs->set_current_tab(1);
  1445. }
  1446. ProjectManager::ProjectManager() {
  1447. // load settings
  1448. if (!EditorSettings::get_singleton())
  1449. EditorSettings::create();
  1450. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  1451. {
  1452. int display_scale = EditorSettings::get_singleton()->get("interface/editor/display_scale");
  1453. float custom_display_scale = EditorSettings::get_singleton()->get("interface/editor/custom_display_scale");
  1454. switch (display_scale) {
  1455. case 0: {
  1456. // Try applying a suitable display scale automatically
  1457. const int screen = OS::get_singleton()->get_current_screen();
  1458. editor_set_scale(OS::get_singleton()->get_screen_dpi(screen) >= 192 && OS::get_singleton()->get_screen_size(screen).x > 2000 ? 2.0 : 1.0);
  1459. } break;
  1460. case 1: {
  1461. editor_set_scale(0.75);
  1462. } break;
  1463. case 2: {
  1464. editor_set_scale(1.0);
  1465. } break;
  1466. case 3: {
  1467. editor_set_scale(1.25);
  1468. } break;
  1469. case 4: {
  1470. editor_set_scale(1.5);
  1471. } break;
  1472. case 5: {
  1473. editor_set_scale(1.75);
  1474. } break;
  1475. case 6: {
  1476. editor_set_scale(2.0);
  1477. } break;
  1478. default: {
  1479. editor_set_scale(custom_display_scale);
  1480. } break;
  1481. }
  1482. #ifndef OSX_ENABLED
  1483. // The macOS platform implementation uses its own hiDPI window resizing code
  1484. // TODO: Resize windows on hiDPI displays on Windows and Linux and remove the line below
  1485. OS::get_singleton()->set_window_size(OS::get_singleton()->get_window_size() * MAX(1, EDSCALE));
  1486. #endif
  1487. }
  1488. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  1489. set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1490. set_theme(create_editor_theme());
  1491. gui_base = memnew(Control);
  1492. add_child(gui_base);
  1493. gui_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1494. gui_base->set_theme(create_custom_theme());
  1495. Panel *panel = memnew(Panel);
  1496. gui_base->add_child(panel);
  1497. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1498. panel->add_style_override("panel", gui_base->get_stylebox("Background", "EditorStyles"));
  1499. VBoxContainer *vb = memnew(VBoxContainer);
  1500. panel->add_child(vb);
  1501. vb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 8 * EDSCALE);
  1502. vb->add_constant_override("separation", 8 * EDSCALE);
  1503. String cp;
  1504. cp += 0xA9;
  1505. OS::get_singleton()->set_window_title(VERSION_NAME + String(" - ") + TTR("Project Manager") + " - " + cp + " 2007-2019 Juan Linietsky, Ariel Manzur & Godot Contributors");
  1506. HBoxContainer *top_hb = memnew(HBoxContainer);
  1507. vb->add_child(top_hb);
  1508. Label *l = memnew(Label);
  1509. l->set_text(VERSION_NAME + String(" - ") + TTR("Project Manager"));
  1510. top_hb->add_child(l);
  1511. top_hb->add_spacer();
  1512. l = memnew(Label);
  1513. String hash = String(VERSION_HASH);
  1514. if (hash.length() != 0)
  1515. hash = "." + hash.left(7);
  1516. l->set_text("v" VERSION_FULL_BUILD "" + hash);
  1517. l->set_align(Label::ALIGN_CENTER);
  1518. top_hb->add_child(l);
  1519. Control *center_box = memnew(Control);
  1520. center_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1521. vb->add_child(center_box);
  1522. tabs = memnew(TabContainer);
  1523. center_box->add_child(tabs);
  1524. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1525. HBoxContainer *tree_hb = memnew(HBoxContainer);
  1526. projects_hb = tree_hb;
  1527. projects_hb->set_name(TTR("Project List"));
  1528. tabs->add_child(tree_hb);
  1529. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  1530. tree_hb->add_child(search_tree_vb);
  1531. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1532. HBoxContainer *sort_filters = memnew(HBoxContainer);
  1533. Label *sort_label = memnew(Label);
  1534. sort_label->set_text(TTR("Sort:"));
  1535. sort_filters->add_child(sort_label);
  1536. Vector<String> sort_filter_titles;
  1537. sort_filter_titles.push_back("Name");
  1538. sort_filter_titles.push_back("Last Modified");
  1539. project_order_filter = memnew(ProjectListFilter);
  1540. project_order_filter->_setup_filters(sort_filter_titles);
  1541. project_order_filter->set_filter_size(150);
  1542. sort_filters->add_child(project_order_filter);
  1543. project_order_filter->connect("filter_changed", this, "_load_recent_projects");
  1544. project_order_filter->set_custom_minimum_size(Size2(180, 10) * EDSCALE);
  1545. int projects_sorting_order = (int)EditorSettings::get_singleton()->get("project_manager/sorting_order");
  1546. project_order_filter->set_filter_option((ProjectListFilter::FilterOption)projects_sorting_order);
  1547. sort_filters->add_spacer(true);
  1548. Label *search_label = memnew(Label);
  1549. search_label->set_text(TTR("Search:"));
  1550. sort_filters->add_child(search_label);
  1551. HBoxContainer *search_filters = memnew(HBoxContainer);
  1552. Vector<String> vec2;
  1553. vec2.push_back("Name");
  1554. vec2.push_back("Path");
  1555. project_filter = memnew(ProjectListFilter);
  1556. project_filter->_setup_filters(vec2);
  1557. project_filter->add_search_box();
  1558. search_filters->add_child(project_filter);
  1559. project_filter->connect("filter_changed", this, "_load_recent_projects");
  1560. project_filter->set_custom_minimum_size(Size2(280, 10) * EDSCALE);
  1561. sort_filters->add_child(search_filters);
  1562. search_tree_vb->add_child(sort_filters);
  1563. PanelContainer *pc = memnew(PanelContainer);
  1564. pc->add_style_override("panel", gui_base->get_stylebox("bg", "Tree"));
  1565. search_tree_vb->add_child(pc);
  1566. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  1567. scroll = memnew(ScrollContainer);
  1568. pc->add_child(scroll);
  1569. scroll->set_enable_h_scroll(false);
  1570. VBoxContainer *tree_vb = memnew(VBoxContainer);
  1571. tree_hb->add_child(tree_vb);
  1572. scroll_children = memnew(VBoxContainer);
  1573. scroll_children->set_h_size_flags(SIZE_EXPAND_FILL);
  1574. scroll->add_child(scroll_children);
  1575. Button *open = memnew(Button);
  1576. open->set_text(TTR("Edit"));
  1577. tree_vb->add_child(open);
  1578. open->connect("pressed", this, "_open_selected_projects_ask");
  1579. open_btn = open;
  1580. Button *run = memnew(Button);
  1581. run->set_text(TTR("Run"));
  1582. tree_vb->add_child(run);
  1583. run->connect("pressed", this, "_run_project");
  1584. run_btn = run;
  1585. tree_vb->add_child(memnew(HSeparator));
  1586. Button *scan = memnew(Button);
  1587. scan->set_text(TTR("Scan"));
  1588. tree_vb->add_child(scan);
  1589. scan->connect("pressed", this, "_scan_projects");
  1590. tree_vb->add_child(memnew(HSeparator));
  1591. scan_dir = memnew(FileDialog);
  1592. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  1593. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  1594. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  1595. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  1596. gui_base->add_child(scan_dir);
  1597. scan_dir->connect("dir_selected", this, "_scan_begin");
  1598. Button *create = memnew(Button);
  1599. create->set_text(TTR("New Project"));
  1600. tree_vb->add_child(create);
  1601. create->connect("pressed", this, "_new_project");
  1602. Button *import = memnew(Button);
  1603. import->set_text(TTR("Import"));
  1604. tree_vb->add_child(import);
  1605. import->connect("pressed", this, "_import_project");
  1606. Button *rename = memnew(Button);
  1607. rename->set_text(TTR("Rename"));
  1608. tree_vb->add_child(rename);
  1609. rename->connect("pressed", this, "_rename_project");
  1610. rename_btn = rename;
  1611. Button *erase = memnew(Button);
  1612. erase->set_text(TTR("Remove"));
  1613. tree_vb->add_child(erase);
  1614. erase->connect("pressed", this, "_erase_project");
  1615. erase_btn = erase;
  1616. tree_vb->add_spacer();
  1617. if (StreamPeerSSL::is_available()) {
  1618. asset_library = memnew(EditorAssetLibrary(true));
  1619. asset_library->set_name(TTR("Templates"));
  1620. tabs->add_child(asset_library);
  1621. asset_library->connect("install_asset", this, "_install_project");
  1622. } else {
  1623. WARN_PRINT("Asset Library not available, as it requires SSL to work.");
  1624. }
  1625. HBoxContainer *settings_hb = memnew(HBoxContainer);
  1626. settings_hb->set_alignment(BoxContainer::ALIGN_END);
  1627. settings_hb->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  1628. language_btn = memnew(OptionButton);
  1629. language_btn->set_flat(true);
  1630. language_btn->set_focus_mode(Control::FOCUS_NONE);
  1631. Vector<String> editor_languages;
  1632. List<PropertyInfo> editor_settings_properties;
  1633. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  1634. for (List<PropertyInfo>::Element *E = editor_settings_properties.front(); E; E = E->next()) {
  1635. PropertyInfo &pi = E->get();
  1636. if (pi.name == "interface/editor/editor_language") {
  1637. editor_languages = pi.hint_string.split(",");
  1638. }
  1639. }
  1640. String current_lang = EditorSettings::get_singleton()->get("interface/editor/editor_language");
  1641. for (int i = 0; i < editor_languages.size(); i++) {
  1642. String lang = editor_languages[i];
  1643. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang);
  1644. language_btn->add_item(lang_name + " [" + lang + "]", i);
  1645. language_btn->set_item_metadata(i, lang);
  1646. if (current_lang == lang) {
  1647. language_btn->select(i);
  1648. language_btn->set_text(lang);
  1649. }
  1650. }
  1651. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1652. settings_hb->add_child(language_btn);
  1653. language_btn->connect("item_selected", this, "_language_selected");
  1654. center_box->add_child(settings_hb);
  1655. settings_hb->set_anchors_and_margins_preset(Control::PRESET_TOP_RIGHT);
  1656. CenterContainer *cc = memnew(CenterContainer);
  1657. Button *cancel = memnew(Button);
  1658. cancel->set_text(TTR("Exit"));
  1659. cancel->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1660. cc->add_child(cancel);
  1661. cancel->connect("pressed", this, "_exit_dialog");
  1662. vb->add_child(cc);
  1663. //////////////////////////////////////////////////////////////
  1664. language_restart_ask = memnew(ConfirmationDialog);
  1665. language_restart_ask->get_ok()->set_text(TTR("Restart Now"));
  1666. language_restart_ask->get_ok()->connect("pressed", this, "_restart_confirm");
  1667. language_restart_ask->get_cancel()->set_text(TTR("Continue"));
  1668. gui_base->add_child(language_restart_ask);
  1669. erase_ask = memnew(ConfirmationDialog);
  1670. erase_ask->get_ok()->set_text(TTR("Remove"));
  1671. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  1672. gui_base->add_child(erase_ask);
  1673. multi_open_ask = memnew(ConfirmationDialog);
  1674. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  1675. multi_open_ask->get_ok()->connect("pressed", this, "_open_selected_projects");
  1676. gui_base->add_child(multi_open_ask);
  1677. multi_run_ask = memnew(ConfirmationDialog);
  1678. multi_run_ask->get_ok()->set_text(TTR("Run"));
  1679. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  1680. gui_base->add_child(multi_run_ask);
  1681. multi_scan_ask = memnew(ConfirmationDialog);
  1682. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  1683. gui_base->add_child(multi_scan_ask);
  1684. ask_update_settings = memnew(ConfirmationDialog);
  1685. ask_update_settings->get_ok()->connect("pressed", this, "_confirm_update_settings");
  1686. gui_base->add_child(ask_update_settings);
  1687. OS::get_singleton()->set_low_processor_usage_mode(true);
  1688. npdialog = memnew(ProjectDialog);
  1689. gui_base->add_child(npdialog);
  1690. npdialog->connect("projects_updated", this, "_on_projects_updated");
  1691. npdialog->connect("project_created", this, "_on_project_created");
  1692. _load_recent_projects();
  1693. if (EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path")) {
  1694. _scan_begin(EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path"));
  1695. }
  1696. last_clicked = "";
  1697. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  1698. run_error_diag = memnew(AcceptDialog);
  1699. gui_base->add_child(run_error_diag);
  1700. run_error_diag->set_title(TTR("Can't run project"));
  1701. dialog_error = memnew(AcceptDialog);
  1702. gui_base->add_child(dialog_error);
  1703. open_templates = memnew(ConfirmationDialog);
  1704. open_templates->set_text(TTR("You don't currently have any projects.\nWould you like to explore the official example projects in the Asset Library?"));
  1705. open_templates->get_ok()->set_text(TTR("Open Asset Library"));
  1706. open_templates->connect("confirmed", this, "_open_asset_library");
  1707. add_child(open_templates);
  1708. }
  1709. ProjectManager::~ProjectManager() {
  1710. if (EditorSettings::get_singleton())
  1711. EditorSettings::destroy();
  1712. }
  1713. void ProjectListFilter::_setup_filters(Vector<String> options) {
  1714. filter_option->clear();
  1715. for (int i = 0; i < options.size(); i++)
  1716. filter_option->add_item(TTR(options[i]));
  1717. }
  1718. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  1719. emit_signal("filter_changed");
  1720. }
  1721. String ProjectListFilter::get_search_term() {
  1722. return search_box->get_text().strip_edges();
  1723. }
  1724. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  1725. return _current_filter;
  1726. }
  1727. void ProjectListFilter::set_filter_option(FilterOption option) {
  1728. filter_option->select((int)option);
  1729. _filter_option_selected(0);
  1730. }
  1731. void ProjectListFilter::_filter_option_selected(int p_idx) {
  1732. FilterOption selected = (FilterOption)(filter_option->get_selected());
  1733. if (_current_filter != selected) {
  1734. _current_filter = selected;
  1735. emit_signal("filter_changed");
  1736. }
  1737. }
  1738. void ProjectListFilter::_notification(int p_what) {
  1739. if (p_what == NOTIFICATION_ENTER_TREE && has_search_box) {
  1740. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  1741. search_box->set_clear_button_enabled(true);
  1742. }
  1743. }
  1744. void ProjectListFilter::_bind_methods() {
  1745. ClassDB::bind_method(D_METHOD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  1746. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  1747. ADD_SIGNAL(MethodInfo("filter_changed"));
  1748. }
  1749. void ProjectListFilter::add_search_box() {
  1750. search_box = memnew(LineEdit);
  1751. search_box->connect("text_changed", this, "_search_text_changed");
  1752. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1753. add_child(search_box);
  1754. has_search_box = true;
  1755. }
  1756. void ProjectListFilter::set_filter_size(int h_size) {
  1757. filter_option->set_custom_minimum_size(Size2(h_size * EDSCALE, 10 * EDSCALE));
  1758. }
  1759. ProjectListFilter::ProjectListFilter() {
  1760. _current_filter = FILTER_NAME;
  1761. filter_option = memnew(OptionButton);
  1762. set_filter_size(80);
  1763. filter_option->set_clip_text(true);
  1764. filter_option->connect("item_selected", this, "_filter_option_selected");
  1765. add_child(filter_option);
  1766. has_search_box = false;
  1767. }
  1768. void ProjectListFilter::clear() {
  1769. if (has_search_box) {
  1770. search_box->clear();
  1771. }
  1772. }