project_manager.cpp 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878
  1. /*************************************************************************/
  2. /* project_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "editor_scale.h"
  42. #include "editor_settings.h"
  43. #include "editor_themes.h"
  44. #include "scene/gui/center_container.h"
  45. #include "scene/gui/line_edit.h"
  46. #include "scene/gui/margin_container.h"
  47. #include "scene/gui/panel_container.h"
  48. #include "scene/gui/separator.h"
  49. #include "scene/gui/texture_rect.h"
  50. #include "scene/gui/tool_button.h"
  51. #include "servers/navigation_server.h"
  52. // Used to test for GLES3 support.
  53. #ifndef SERVER_ENABLED
  54. #include "drivers/gles3/rasterizer_gles3.h"
  55. #endif
  56. static inline String get_project_key_from_path(const String &dir) {
  57. return dir.replace("/", "::");
  58. }
  59. class ProjectDialog : public ConfirmationDialog {
  60. GDCLASS(ProjectDialog, ConfirmationDialog);
  61. public:
  62. enum Mode {
  63. MODE_NEW,
  64. MODE_IMPORT,
  65. MODE_INSTALL,
  66. MODE_RENAME
  67. };
  68. private:
  69. enum MessageType {
  70. MESSAGE_ERROR,
  71. MESSAGE_WARNING,
  72. MESSAGE_SUCCESS
  73. };
  74. enum InputType {
  75. PROJECT_PATH,
  76. INSTALL_PATH
  77. };
  78. Mode mode;
  79. Button *browse;
  80. Button *install_browse;
  81. Button *create_dir;
  82. Container *name_container;
  83. Container *path_container;
  84. Container *install_path_container;
  85. Container *rasterizer_container;
  86. Ref<ButtonGroup> rasterizer_button_group;
  87. Label *msg;
  88. LineEdit *project_path;
  89. LineEdit *project_name;
  90. LineEdit *install_path;
  91. TextureRect *status_rect;
  92. TextureRect *install_status_rect;
  93. FileDialog *fdialog;
  94. FileDialog *fdialog_install;
  95. String zip_path;
  96. String zip_title;
  97. AcceptDialog *dialog_error;
  98. String fav_dir;
  99. String created_folder_path;
  100. void set_message(const String &p_msg, MessageType p_type = MESSAGE_SUCCESS, InputType input_type = PROJECT_PATH) {
  101. msg->set_text(p_msg);
  102. Ref<Texture> current_path_icon = status_rect->get_texture();
  103. Ref<Texture> current_install_icon = install_status_rect->get_texture();
  104. Ref<Texture> new_icon;
  105. switch (p_type) {
  106. case MESSAGE_ERROR: {
  107. msg->add_color_override("font_color", get_color("error_color", "Editor"));
  108. msg->set_modulate(Color(1, 1, 1, 1));
  109. new_icon = get_icon("StatusError", "EditorIcons");
  110. } break;
  111. case MESSAGE_WARNING: {
  112. msg->add_color_override("font_color", get_color("warning_color", "Editor"));
  113. msg->set_modulate(Color(1, 1, 1, 1));
  114. new_icon = get_icon("StatusWarning", "EditorIcons");
  115. } break;
  116. case MESSAGE_SUCCESS: {
  117. msg->set_modulate(Color(1, 1, 1, 0));
  118. new_icon = get_icon("StatusSuccess", "EditorIcons");
  119. } break;
  120. }
  121. if (current_path_icon != new_icon && input_type == PROJECT_PATH) {
  122. status_rect->set_texture(new_icon);
  123. } else if (current_install_icon != new_icon && input_type == INSTALL_PATH) {
  124. install_status_rect->set_texture(new_icon);
  125. }
  126. set_size(Size2(500, 0) * EDSCALE);
  127. }
  128. String _test_path() {
  129. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  130. String valid_path, valid_install_path;
  131. if (d->change_dir(project_path->get_text()) == OK) {
  132. valid_path = project_path->get_text();
  133. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  134. valid_path = project_path->get_text().strip_edges();
  135. } else if (project_path->get_text().ends_with(".zip")) {
  136. if (d->file_exists(project_path->get_text())) {
  137. valid_path = project_path->get_text();
  138. }
  139. } else if (project_path->get_text().strip_edges().ends_with(".zip")) {
  140. if (d->file_exists(project_path->get_text().strip_edges())) {
  141. valid_path = project_path->get_text().strip_edges();
  142. }
  143. }
  144. if (valid_path == "") {
  145. set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR);
  146. memdelete(d);
  147. get_ok()->set_disabled(true);
  148. return "";
  149. }
  150. if (mode == MODE_IMPORT && valid_path.ends_with(".zip")) {
  151. if (d->change_dir(install_path->get_text()) == OK) {
  152. valid_install_path = install_path->get_text();
  153. } else if (d->change_dir(install_path->get_text().strip_edges()) == OK) {
  154. valid_install_path = install_path->get_text().strip_edges();
  155. }
  156. if (valid_install_path == "") {
  157. set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR, INSTALL_PATH);
  158. memdelete(d);
  159. get_ok()->set_disabled(true);
  160. return "";
  161. }
  162. }
  163. if (mode == MODE_IMPORT || mode == MODE_RENAME) {
  164. if (valid_path != "" && !d->file_exists("project.godot")) {
  165. if (valid_path.ends_with(".zip")) {
  166. FileAccess *src_f = nullptr;
  167. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  168. unzFile pkg = unzOpen2(valid_path.utf8().get_data(), &io);
  169. if (!pkg) {
  170. set_message(TTR("Error opening package file (it's not in ZIP format)."), MESSAGE_ERROR);
  171. memdelete(d);
  172. get_ok()->set_disabled(true);
  173. unzClose(pkg);
  174. return "";
  175. }
  176. int ret = unzGoToFirstFile(pkg);
  177. while (ret == UNZ_OK) {
  178. unz_file_info info;
  179. char fname[16384];
  180. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  181. if (String::utf8(fname).ends_with("project.godot")) {
  182. break;
  183. }
  184. ret = unzGoToNextFile(pkg);
  185. }
  186. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  187. set_message(TTR("Invalid \".zip\" project file; it doesn't contain a \"project.godot\" file."), MESSAGE_ERROR);
  188. memdelete(d);
  189. get_ok()->set_disabled(true);
  190. unzClose(pkg);
  191. return "";
  192. }
  193. unzClose(pkg);
  194. // check if the specified install folder is empty, even though this is not an error, it is good to check here
  195. d->list_dir_begin();
  196. bool is_empty = true;
  197. String n = d->get_next();
  198. while (n != String()) {
  199. if (!n.begins_with(".")) {
  200. // Allow `.`, `..` (reserved current/parent folder names)
  201. // and hidden files/folders to be present.
  202. // For instance, this lets users initialize a Git repository
  203. // and still be able to create a project in the directory afterwards.
  204. is_empty = false;
  205. break;
  206. }
  207. n = d->get_next();
  208. }
  209. d->list_dir_end();
  210. if (!is_empty) {
  211. set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH);
  212. memdelete(d);
  213. get_ok()->set_disabled(true);
  214. return "";
  215. }
  216. } else {
  217. set_message(TTR("Please choose a \"project.godot\" or \".zip\" file."), MESSAGE_ERROR);
  218. memdelete(d);
  219. install_path_container->hide();
  220. get_ok()->set_disabled(true);
  221. return "";
  222. }
  223. } else if (valid_path.ends_with("zip")) {
  224. set_message(TTR("This directory already contains a Godot project."), MESSAGE_ERROR, INSTALL_PATH);
  225. memdelete(d);
  226. get_ok()->set_disabled(true);
  227. return "";
  228. }
  229. } else {
  230. // check if the specified folder is empty, even though this is not an error, it is good to check here
  231. d->list_dir_begin();
  232. bool is_empty = true;
  233. String n = d->get_next();
  234. while (n != String()) {
  235. if (!n.begins_with(".")) {
  236. // Allow `.`, `..` (reserved current/parent folder names)
  237. // and hidden files/folders to be present.
  238. // For instance, this lets users initialize a Git repository
  239. // and still be able to create a project in the directory afterwards.
  240. is_empty = false;
  241. break;
  242. }
  243. n = d->get_next();
  244. }
  245. d->list_dir_end();
  246. if (!is_empty) {
  247. set_message(TTR("Please choose an empty folder."), MESSAGE_ERROR);
  248. memdelete(d);
  249. get_ok()->set_disabled(true);
  250. return "";
  251. }
  252. }
  253. set_message("");
  254. set_message("", MESSAGE_SUCCESS, INSTALL_PATH);
  255. memdelete(d);
  256. get_ok()->set_disabled(false);
  257. return valid_path;
  258. }
  259. void _path_text_changed(const String &p_path) {
  260. String sp = _test_path();
  261. if (sp != "") {
  262. // If the project name is empty or default, infer the project name from the selected folder name
  263. if (project_name->get_text().strip_edges() == "" || project_name->get_text().strip_edges() == TTR("New Game Project")) {
  264. sp = sp.replace("\\", "/");
  265. int lidx = sp.rfind("/");
  266. if (lidx != -1) {
  267. sp = sp.substr(lidx + 1, sp.length()).capitalize();
  268. }
  269. if (sp == "" && mode == MODE_IMPORT) {
  270. sp = TTR("Imported Project");
  271. }
  272. project_name->set_text(sp);
  273. _text_changed(sp);
  274. }
  275. }
  276. if (created_folder_path != "" && created_folder_path != p_path) {
  277. _remove_created_folder();
  278. }
  279. }
  280. void _file_selected(const String &p_path) {
  281. String p = p_path;
  282. if (mode == MODE_IMPORT) {
  283. if (p.ends_with("project.godot")) {
  284. p = p.get_base_dir();
  285. install_path_container->hide();
  286. get_ok()->set_disabled(false);
  287. } else if (p.ends_with(".zip")) {
  288. install_path->set_text(p.get_base_dir());
  289. install_path_container->show();
  290. get_ok()->set_disabled(false);
  291. } else {
  292. set_message(TTR("Please choose a \"project.godot\" or \".zip\" file."), MESSAGE_ERROR);
  293. get_ok()->set_disabled(true);
  294. return;
  295. }
  296. }
  297. String sp = p.simplify_path();
  298. project_path->set_text(sp);
  299. _path_text_changed(sp);
  300. if (p.ends_with(".zip")) {
  301. install_path->call_deferred("grab_focus");
  302. } else {
  303. get_ok()->call_deferred("grab_focus");
  304. }
  305. }
  306. void _path_selected(const String &p_path) {
  307. String sp = p_path.simplify_path();
  308. project_path->set_text(sp);
  309. _path_text_changed(sp);
  310. get_ok()->call_deferred("grab_focus");
  311. }
  312. void _install_path_selected(const String &p_path) {
  313. String sp = p_path.simplify_path();
  314. install_path->set_text(sp);
  315. _path_text_changed(sp);
  316. get_ok()->call_deferred("grab_focus");
  317. }
  318. void _browse_path() {
  319. fdialog->set_current_dir(project_path->get_text());
  320. if (mode == MODE_IMPORT) {
  321. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  322. fdialog->clear_filters();
  323. fdialog->add_filter(vformat("project.godot ; %s %s", VERSION_NAME, TTR("Project")));
  324. fdialog->add_filter("*.zip ; " + TTR("ZIP File"));
  325. } else {
  326. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  327. }
  328. fdialog->popup_centered_ratio();
  329. }
  330. void _browse_install_path() {
  331. fdialog_install->set_current_dir(install_path->get_text());
  332. fdialog_install->set_mode(FileDialog::MODE_OPEN_DIR);
  333. fdialog_install->popup_centered_ratio();
  334. }
  335. void _create_folder() {
  336. const String project_name_no_edges = project_name->get_text().strip_edges();
  337. if (project_name_no_edges == "" || created_folder_path != "" || project_name_no_edges.ends_with(".")) {
  338. set_message(TTR("Invalid project name."), MESSAGE_WARNING);
  339. return;
  340. }
  341. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  342. if (d->change_dir(project_path->get_text()) == OK) {
  343. if (!d->dir_exists(project_name_no_edges)) {
  344. if (d->make_dir(project_name_no_edges) == OK) {
  345. d->change_dir(project_name_no_edges);
  346. String dir_str = d->get_current_dir();
  347. project_path->set_text(dir_str);
  348. _path_text_changed(dir_str);
  349. created_folder_path = d->get_current_dir();
  350. create_dir->set_disabled(true);
  351. } else {
  352. dialog_error->set_text(TTR("Couldn't create folder."));
  353. dialog_error->popup_centered_minsize();
  354. }
  355. } else {
  356. dialog_error->set_text(TTR("There is already a folder in this path with the specified name."));
  357. dialog_error->popup_centered_minsize();
  358. }
  359. }
  360. memdelete(d);
  361. }
  362. void _text_changed(const String &p_text) {
  363. if (mode != MODE_NEW) {
  364. return;
  365. }
  366. _test_path();
  367. if (p_text.strip_edges() == "") {
  368. set_message(TTR("It would be a good idea to name your project."), MESSAGE_ERROR);
  369. }
  370. }
  371. void ok_pressed() {
  372. String dir = project_path->get_text();
  373. if (mode == MODE_RENAME) {
  374. String dir2 = _test_path();
  375. if (dir2 == "") {
  376. set_message(TTR("Invalid project path (changed anything?)."), MESSAGE_ERROR);
  377. return;
  378. }
  379. ProjectSettings *current = memnew(ProjectSettings);
  380. int err = current->setup(dir2, "");
  381. if (err != OK) {
  382. set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR);
  383. } else {
  384. ProjectSettings::CustomMap edited_settings;
  385. edited_settings["application/config/name"] = project_name->get_text().strip_edges();
  386. if (current->save_custom(dir2.plus_file("project.godot"), edited_settings, Vector<String>(), true) != OK) {
  387. set_message(TTR("Couldn't edit project.godot in project path."), MESSAGE_ERROR);
  388. }
  389. }
  390. hide();
  391. emit_signal("projects_updated");
  392. } else {
  393. if (mode == MODE_IMPORT) {
  394. if (project_path->get_text().ends_with(".zip")) {
  395. mode = MODE_INSTALL;
  396. ok_pressed();
  397. return;
  398. }
  399. } else {
  400. if (mode == MODE_NEW) {
  401. ProjectSettings::CustomMap initial_settings;
  402. if (rasterizer_button_group->get_pressed_button()->get_meta("driver_name") == "GLES3") {
  403. initial_settings["rendering/quality/driver/driver_name"] = "GLES3";
  404. } else {
  405. initial_settings["rendering/quality/driver/driver_name"] = "GLES2";
  406. initial_settings["rendering/vram_compression/import_etc2"] = false;
  407. initial_settings["rendering/vram_compression/import_etc"] = true;
  408. }
  409. initial_settings["application/config/name"] = project_name->get_text().strip_edges();
  410. initial_settings["application/config/icon"] = "res://icon.png";
  411. initial_settings["rendering/environment/default_environment"] = "res://default_env.tres";
  412. initial_settings["physics/common/enable_pause_aware_picking"] = true;
  413. initial_settings["gui/common/drop_mouse_on_gui_input_disabled"] = true;
  414. if (ProjectSettings::get_singleton()->save_custom(dir.plus_file("project.godot"), initial_settings, Vector<String>(), false) != OK) {
  415. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  416. } else {
  417. ResourceSaver::save(dir.plus_file("icon.png"), create_unscaled_default_project_icon());
  418. FileAccess *f = FileAccess::open(dir.plus_file("default_env.tres"), FileAccess::WRITE);
  419. if (!f) {
  420. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  421. } else {
  422. f->store_line("[gd_resource type=\"Environment\" load_steps=2 format=2]");
  423. f->store_line("");
  424. f->store_line("[sub_resource type=\"ProceduralSky\" id=1]");
  425. f->store_line("");
  426. f->store_line("[resource]");
  427. f->store_line("background_mode = 2");
  428. f->store_line("background_sky = SubResource( 1 )");
  429. memdelete(f);
  430. }
  431. }
  432. } else if (mode == MODE_INSTALL) {
  433. if (project_path->get_text().ends_with(".zip")) {
  434. dir = install_path->get_text();
  435. zip_path = project_path->get_text();
  436. }
  437. FileAccess *src_f = nullptr;
  438. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  439. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  440. if (!pkg) {
  441. dialog_error->set_text(TTR("Error opening package file, not in ZIP format."));
  442. dialog_error->popup_centered_minsize();
  443. return;
  444. }
  445. // Find the zip_root
  446. String zip_root;
  447. int ret = unzGoToFirstFile(pkg);
  448. while (ret == UNZ_OK) {
  449. unz_file_info info;
  450. char fname[16384];
  451. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  452. String name = String::utf8(fname);
  453. if (name.ends_with("project.godot")) {
  454. zip_root = name.substr(0, name.rfind("project.godot"));
  455. break;
  456. }
  457. ret = unzGoToNextFile(pkg);
  458. }
  459. ret = unzGoToFirstFile(pkg);
  460. Vector<String> failed_files;
  461. int idx = 0;
  462. while (ret == UNZ_OK) {
  463. //get filename
  464. unz_file_info info;
  465. char fname[16384];
  466. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  467. String path = String::utf8(fname);
  468. if (path == String() || path == zip_root || !zip_root.is_subsequence_of(path)) {
  469. //
  470. } else if (path.ends_with("/")) { // a dir
  471. path = path.substr(0, path.length() - 1);
  472. String rel_path = path.substr(zip_root.length());
  473. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  474. da->make_dir(dir.plus_file(rel_path));
  475. memdelete(da);
  476. } else {
  477. Vector<uint8_t> data;
  478. data.resize(info.uncompressed_size);
  479. String rel_path = path.substr(zip_root.length());
  480. //read
  481. unzOpenCurrentFile(pkg);
  482. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  483. unzCloseCurrentFile(pkg);
  484. FileAccess *f = FileAccess::open(dir.plus_file(rel_path), FileAccess::WRITE);
  485. if (f) {
  486. f->store_buffer(data.ptr(), data.size());
  487. memdelete(f);
  488. } else {
  489. failed_files.push_back(rel_path);
  490. }
  491. }
  492. idx++;
  493. ret = unzGoToNextFile(pkg);
  494. }
  495. unzClose(pkg);
  496. if (failed_files.size()) {
  497. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  498. for (int i = 0; i < failed_files.size(); i++) {
  499. if (i > 15) {
  500. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  501. break;
  502. }
  503. msg += failed_files[i] + "\n";
  504. }
  505. dialog_error->set_text(msg);
  506. dialog_error->popup_centered_minsize();
  507. } else if (!project_path->get_text().ends_with(".zip")) {
  508. dialog_error->set_text(TTR("Package installed successfully!"));
  509. dialog_error->popup_centered_minsize();
  510. }
  511. }
  512. }
  513. dir = dir.replace("\\", "/");
  514. if (dir.ends_with("/")) {
  515. dir = dir.substr(0, dir.length() - 1);
  516. }
  517. String proj = get_project_key_from_path(dir);
  518. EditorSettings::get_singleton()->set("projects/" + proj, dir);
  519. EditorSettings::get_singleton()->save();
  520. hide();
  521. emit_signal("project_created", dir);
  522. }
  523. }
  524. void _remove_created_folder() {
  525. if (created_folder_path != "") {
  526. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  527. d->remove(created_folder_path);
  528. memdelete(d);
  529. create_dir->set_disabled(false);
  530. created_folder_path = "";
  531. }
  532. }
  533. void cancel_pressed() {
  534. _remove_created_folder();
  535. project_path->clear();
  536. _path_text_changed("");
  537. project_name->clear();
  538. _text_changed("");
  539. if (status_rect->get_texture() == get_icon("StatusError", "EditorIcons")) {
  540. msg->show();
  541. }
  542. if (install_status_rect->get_texture() == get_icon("StatusError", "EditorIcons")) {
  543. msg->show();
  544. }
  545. }
  546. void _notification(int p_what) {
  547. if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST) {
  548. _remove_created_folder();
  549. }
  550. }
  551. protected:
  552. static void _bind_methods() {
  553. ClassDB::bind_method("_browse_path", &ProjectDialog::_browse_path);
  554. ClassDB::bind_method("_create_folder", &ProjectDialog::_create_folder);
  555. ClassDB::bind_method("_text_changed", &ProjectDialog::_text_changed);
  556. ClassDB::bind_method("_path_text_changed", &ProjectDialog::_path_text_changed);
  557. ClassDB::bind_method("_path_selected", &ProjectDialog::_path_selected);
  558. ClassDB::bind_method("_file_selected", &ProjectDialog::_file_selected);
  559. ClassDB::bind_method("_install_path_selected", &ProjectDialog::_install_path_selected);
  560. ClassDB::bind_method("_browse_install_path", &ProjectDialog::_browse_install_path);
  561. ADD_SIGNAL(MethodInfo("project_created"));
  562. ADD_SIGNAL(MethodInfo("projects_updated"));
  563. }
  564. public:
  565. void set_zip_path(const String &p_path) {
  566. zip_path = p_path;
  567. }
  568. void set_zip_title(const String &p_title) {
  569. zip_title = p_title;
  570. }
  571. void set_mode(Mode p_mode) {
  572. mode = p_mode;
  573. }
  574. void set_project_path(const String &p_path) {
  575. project_path->set_text(p_path);
  576. }
  577. void show_dialog() {
  578. if (mode == MODE_RENAME) {
  579. project_path->set_editable(false);
  580. browse->hide();
  581. install_browse->hide();
  582. set_title(TTR("Rename Project"));
  583. get_ok()->set_text(TTR("Rename"));
  584. name_container->show();
  585. status_rect->hide();
  586. msg->hide();
  587. install_path_container->hide();
  588. install_status_rect->hide();
  589. rasterizer_container->hide();
  590. get_ok()->set_disabled(false);
  591. ProjectSettings *current = memnew(ProjectSettings);
  592. int err = current->setup(project_path->get_text(), "");
  593. if (err != OK) {
  594. set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR);
  595. status_rect->show();
  596. msg->show();
  597. get_ok()->set_disabled(true);
  598. } else if (current->has_setting("application/config/name")) {
  599. String proj = current->get("application/config/name");
  600. project_name->set_text(proj);
  601. _text_changed(proj);
  602. }
  603. project_name->call_deferred("grab_focus");
  604. create_dir->hide();
  605. } else {
  606. fav_dir = EditorSettings::get_singleton()->get("filesystem/directories/default_project_path");
  607. if (fav_dir != "") {
  608. project_path->set_text(fav_dir);
  609. fdialog->set_current_dir(fav_dir);
  610. } else {
  611. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  612. project_path->set_text(d->get_current_dir());
  613. fdialog->set_current_dir(d->get_current_dir());
  614. memdelete(d);
  615. }
  616. String proj = TTR("New Game Project");
  617. project_name->set_text(proj);
  618. _text_changed(proj);
  619. project_path->set_editable(true);
  620. browse->set_disabled(false);
  621. browse->show();
  622. install_browse->set_disabled(false);
  623. install_browse->show();
  624. create_dir->show();
  625. status_rect->show();
  626. install_status_rect->show();
  627. msg->show();
  628. if (mode == MODE_IMPORT) {
  629. set_title(TTR("Import Existing Project"));
  630. get_ok()->set_text(TTR("Import & Edit"));
  631. name_container->hide();
  632. install_path_container->hide();
  633. rasterizer_container->hide();
  634. project_path->grab_focus();
  635. } else if (mode == MODE_NEW) {
  636. set_title(TTR("Create New Project"));
  637. get_ok()->set_text(TTR("Create & Edit"));
  638. name_container->show();
  639. install_path_container->hide();
  640. rasterizer_container->show();
  641. project_name->call_deferred("grab_focus");
  642. project_name->call_deferred("select_all");
  643. } else if (mode == MODE_INSTALL) {
  644. set_title(TTR("Install Project:") + " " + zip_title);
  645. get_ok()->set_text(TTR("Install & Edit"));
  646. project_name->set_text(zip_title);
  647. name_container->show();
  648. install_path_container->hide();
  649. rasterizer_container->hide();
  650. project_path->grab_focus();
  651. }
  652. _test_path();
  653. }
  654. // Reset the dialog to its initial size. Otherwise, the dialog window would be too large
  655. // when opening a small dialog after closing a large dialog.
  656. set_size(get_minimum_size());
  657. popup_centered_minsize(Size2(500, 0) * EDSCALE);
  658. }
  659. ProjectDialog() {
  660. VBoxContainer *vb = memnew(VBoxContainer);
  661. add_child(vb);
  662. name_container = memnew(VBoxContainer);
  663. vb->add_child(name_container);
  664. Label *l = memnew(Label);
  665. l->set_text(TTR("Project Name:"));
  666. name_container->add_child(l);
  667. HBoxContainer *pnhb = memnew(HBoxContainer);
  668. name_container->add_child(pnhb);
  669. project_name = memnew(LineEdit);
  670. project_name->set_h_size_flags(SIZE_EXPAND_FILL);
  671. pnhb->add_child(project_name);
  672. create_dir = memnew(Button);
  673. pnhb->add_child(create_dir);
  674. create_dir->set_text(TTR("Create Folder"));
  675. create_dir->connect("pressed", this, "_create_folder");
  676. path_container = memnew(VBoxContainer);
  677. vb->add_child(path_container);
  678. l = memnew(Label);
  679. l->set_text(TTR("Project Path:"));
  680. path_container->add_child(l);
  681. HBoxContainer *pphb = memnew(HBoxContainer);
  682. path_container->add_child(pphb);
  683. project_path = memnew(LineEdit);
  684. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  685. pphb->add_child(project_path);
  686. install_path_container = memnew(VBoxContainer);
  687. vb->add_child(install_path_container);
  688. l = memnew(Label);
  689. l->set_text(TTR("Project Installation Path:"));
  690. install_path_container->add_child(l);
  691. HBoxContainer *iphb = memnew(HBoxContainer);
  692. install_path_container->add_child(iphb);
  693. install_path = memnew(LineEdit);
  694. install_path->set_h_size_flags(SIZE_EXPAND_FILL);
  695. iphb->add_child(install_path);
  696. // status icon
  697. status_rect = memnew(TextureRect);
  698. status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  699. pphb->add_child(status_rect);
  700. browse = memnew(Button);
  701. browse->set_text(TTR("Browse"));
  702. browse->connect("pressed", this, "_browse_path");
  703. pphb->add_child(browse);
  704. // install status icon
  705. install_status_rect = memnew(TextureRect);
  706. install_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  707. iphb->add_child(install_status_rect);
  708. install_browse = memnew(Button);
  709. install_browse->set_text(TTR("Browse"));
  710. install_browse->connect("pressed", this, "_browse_install_path");
  711. iphb->add_child(install_browse);
  712. msg = memnew(Label);
  713. msg->set_align(Label::ALIGN_CENTER);
  714. vb->add_child(msg);
  715. // rasterizer selection
  716. rasterizer_container = memnew(VBoxContainer);
  717. vb->add_child(rasterizer_container);
  718. l = memnew(Label);
  719. l->set_text(TTR("Renderer:"));
  720. rasterizer_container->add_child(l);
  721. Container *rshb = memnew(HBoxContainer);
  722. rasterizer_container->add_child(rshb);
  723. rasterizer_button_group.instance();
  724. // Enable GLES3 by default as it's the default value for the project setting.
  725. #ifndef SERVER_ENABLED
  726. bool gles3_viable = RasterizerGLES3::is_viable() == OK;
  727. #else
  728. // Whatever, project manager isn't even used in headless builds.
  729. bool gles3_viable = false;
  730. #endif
  731. Container *rvb = memnew(VBoxContainer);
  732. rvb->set_h_size_flags(SIZE_EXPAND_FILL);
  733. rshb->add_child(rvb);
  734. Button *rs_button = memnew(CheckBox);
  735. rs_button->set_button_group(rasterizer_button_group);
  736. rs_button->set_text(TTR("OpenGL ES 3.0"));
  737. rs_button->set_meta("driver_name", "GLES3");
  738. rvb->add_child(rs_button);
  739. if (gles3_viable) {
  740. rs_button->set_pressed(true);
  741. } else {
  742. // If GLES3 can't be used, don't let users shoot themselves in the foot.
  743. rs_button->set_disabled(true);
  744. l = memnew(Label);
  745. l->set_text(TTR("Not supported by your GPU drivers."));
  746. rvb->add_child(l);
  747. }
  748. l = memnew(Label);
  749. l->set_text(TTR("Higher visual quality\nAll features available\nIncompatible with older hardware\nNot recommended for web games"));
  750. rvb->add_child(l);
  751. rshb->add_child(memnew(VSeparator));
  752. rvb = memnew(VBoxContainer);
  753. rvb->set_h_size_flags(SIZE_EXPAND_FILL);
  754. rshb->add_child(rvb);
  755. rs_button = memnew(CheckBox);
  756. rs_button->set_button_group(rasterizer_button_group);
  757. rs_button->set_text(TTR("OpenGL ES 2.0"));
  758. rs_button->set_meta("driver_name", "GLES2");
  759. rs_button->set_pressed(!gles3_viable);
  760. rvb->add_child(rs_button);
  761. l = memnew(Label);
  762. l->set_text(TTR("Lower visual quality\nSome features not available\nWorks on most hardware\nRecommended for web games"));
  763. rvb->add_child(l);
  764. l = memnew(Label);
  765. l->set_text(TTR("Renderer can be changed later, but scenes may need to be adjusted."));
  766. l->set_align(Label::ALIGN_CENTER);
  767. rasterizer_container->add_child(l);
  768. fdialog = memnew(FileDialog);
  769. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  770. fdialog_install = memnew(FileDialog);
  771. fdialog_install->set_access(FileDialog::ACCESS_FILESYSTEM);
  772. add_child(fdialog);
  773. add_child(fdialog_install);
  774. project_name->connect("text_changed", this, "_text_changed");
  775. project_path->connect("text_changed", this, "_path_text_changed");
  776. install_path->connect("text_changed", this, "_path_text_changed");
  777. fdialog->connect("dir_selected", this, "_path_selected");
  778. fdialog->connect("file_selected", this, "_file_selected");
  779. fdialog_install->connect("dir_selected", this, "_install_path_selected");
  780. fdialog_install->connect("file_selected", this, "_install_path_selected");
  781. set_hide_on_ok(false);
  782. mode = MODE_NEW;
  783. dialog_error = memnew(AcceptDialog);
  784. add_child(dialog_error);
  785. }
  786. };
  787. class ProjectListItemControl : public HBoxContainer {
  788. GDCLASS(ProjectListItemControl, HBoxContainer)
  789. public:
  790. TextureButton *favorite_button;
  791. TextureRect *icon;
  792. bool icon_needs_reload;
  793. bool hover;
  794. ProjectListItemControl() {
  795. favorite_button = nullptr;
  796. icon = nullptr;
  797. icon_needs_reload = true;
  798. hover = false;
  799. set_focus_mode(FocusMode::FOCUS_ALL);
  800. }
  801. void set_is_favorite(bool fav) {
  802. favorite_button->set_modulate(fav ? Color(1, 1, 1, 1) : Color(1, 1, 1, 0.2));
  803. }
  804. void _notification(int p_what) {
  805. switch (p_what) {
  806. case NOTIFICATION_MOUSE_ENTER: {
  807. hover = true;
  808. update();
  809. } break;
  810. case NOTIFICATION_MOUSE_EXIT: {
  811. hover = false;
  812. update();
  813. } break;
  814. case NOTIFICATION_DRAW: {
  815. if (hover) {
  816. draw_style_box(get_stylebox("hover", "Tree"), Rect2(Point2(), get_size() - Size2(10, 0) * EDSCALE));
  817. }
  818. } break;
  819. }
  820. }
  821. };
  822. class ProjectList : public ScrollContainer {
  823. GDCLASS(ProjectList, ScrollContainer)
  824. public:
  825. static const char *SIGNAL_SELECTION_CHANGED;
  826. static const char *SIGNAL_PROJECT_ASK_OPEN;
  827. enum MenuOptions {
  828. GLOBAL_NEW_WINDOW,
  829. GLOBAL_OPEN_PROJECT
  830. };
  831. // Can often be passed by copy
  832. struct Item {
  833. String project_key;
  834. String project_name;
  835. String description;
  836. String path;
  837. String icon;
  838. String main_scene;
  839. uint64_t last_modified;
  840. bool favorite;
  841. bool grayed;
  842. bool missing;
  843. int version;
  844. ProjectListItemControl *control;
  845. Item() {}
  846. Item(const String &p_project,
  847. const String &p_name,
  848. const String &p_description,
  849. const String &p_path,
  850. const String &p_icon,
  851. const String &p_main_scene,
  852. uint64_t p_last_modified,
  853. bool p_favorite,
  854. bool p_grayed,
  855. bool p_missing,
  856. int p_version) {
  857. project_key = p_project;
  858. project_name = p_name;
  859. description = p_description;
  860. path = p_path;
  861. icon = p_icon;
  862. main_scene = p_main_scene;
  863. last_modified = p_last_modified;
  864. favorite = p_favorite;
  865. grayed = p_grayed;
  866. missing = p_missing;
  867. version = p_version;
  868. control = nullptr;
  869. }
  870. _FORCE_INLINE_ bool operator==(const Item &l) const {
  871. return project_key == l.project_key;
  872. }
  873. };
  874. bool project_opening_initiated;
  875. ProjectList();
  876. ~ProjectList();
  877. void update_dock_menu();
  878. void load_projects();
  879. void set_search_term(String p_search_term);
  880. void set_order_option(ProjectListFilter::FilterOption p_option);
  881. void sort_projects();
  882. int get_project_count() const;
  883. void select_project(int p_index);
  884. void erase_selected_projects(bool p_delete_project_contents);
  885. Vector<Item> get_selected_projects() const;
  886. const Set<String> &get_selected_project_keys() const;
  887. void ensure_project_visible(int p_index);
  888. int get_single_selected_index() const;
  889. bool is_any_project_missing() const;
  890. void erase_missing_projects();
  891. int refresh_project(const String &dir_path);
  892. private:
  893. static void _bind_methods();
  894. void _notification(int p_what);
  895. void _panel_draw(Node *p_hb);
  896. void _panel_input(const Ref<InputEvent> &p_ev, Node *p_hb);
  897. void _favorite_pressed(Node *p_hb);
  898. void _show_project(const String &p_path);
  899. void select_range(int p_begin, int p_end);
  900. void toggle_select(int p_index);
  901. void create_project_item_control(int p_index);
  902. void remove_project(int p_index, bool p_update_settings);
  903. void update_icons_async();
  904. void load_project_icon(int p_index);
  905. static void load_project_data(const String &p_property_key, Item &p_item, bool p_favorite);
  906. String _search_term;
  907. ProjectListFilter::FilterOption _order_option;
  908. Set<String> _selected_project_keys;
  909. String _last_clicked; // Project key
  910. VBoxContainer *_scroll_children;
  911. int _icon_load_index;
  912. Vector<Item> _projects;
  913. };
  914. struct ProjectListComparator {
  915. ProjectListFilter::FilterOption order_option;
  916. // operator<
  917. _FORCE_INLINE_ bool operator()(const ProjectList::Item &a, const ProjectList::Item &b) const {
  918. if (a.favorite && !b.favorite) {
  919. return true;
  920. }
  921. if (b.favorite && !a.favorite) {
  922. return false;
  923. }
  924. switch (order_option) {
  925. case ProjectListFilter::FILTER_PATH:
  926. return a.project_key < b.project_key;
  927. case ProjectListFilter::FILTER_MODIFIED:
  928. return a.last_modified > b.last_modified;
  929. default:
  930. return a.project_name < b.project_name;
  931. }
  932. }
  933. };
  934. ProjectList::ProjectList() {
  935. _order_option = ProjectListFilter::FILTER_MODIFIED;
  936. _scroll_children = memnew(VBoxContainer);
  937. _scroll_children->set_h_size_flags(SIZE_EXPAND_FILL);
  938. add_child(_scroll_children);
  939. _icon_load_index = 0;
  940. project_opening_initiated = false;
  941. }
  942. ProjectList::~ProjectList() {
  943. }
  944. void ProjectList::update_icons_async() {
  945. _icon_load_index = 0;
  946. set_process(true);
  947. }
  948. void ProjectList::_notification(int p_what) {
  949. if (p_what == NOTIFICATION_PROCESS) {
  950. // Load icons as a coroutine to speed up launch when you have hundreds of projects
  951. if (_icon_load_index < _projects.size()) {
  952. Item &item = _projects.write[_icon_load_index];
  953. if (item.control->icon_needs_reload) {
  954. load_project_icon(_icon_load_index);
  955. }
  956. _icon_load_index++;
  957. } else {
  958. set_process(false);
  959. }
  960. }
  961. }
  962. void ProjectList::load_project_icon(int p_index) {
  963. Item &item = _projects.write[p_index];
  964. Ref<Texture> default_icon = get_icon("DefaultProjectIcon", "EditorIcons");
  965. Ref<Texture> icon;
  966. if (item.icon != "") {
  967. Ref<Image> img;
  968. img.instance();
  969. Error err = img->load(item.icon.replace_first("res://", item.path + "/"));
  970. if (err == OK) {
  971. img->resize(default_icon->get_width(), default_icon->get_height(), Image::INTERPOLATE_LANCZOS);
  972. Ref<ImageTexture> it = memnew(ImageTexture);
  973. it->create_from_image(img);
  974. icon = it;
  975. }
  976. }
  977. if (icon.is_null()) {
  978. icon = default_icon;
  979. }
  980. item.control->icon->set_texture(icon);
  981. item.control->icon_needs_reload = false;
  982. }
  983. void ProjectList::load_project_data(const String &p_property_key, Item &p_item, bool p_favorite) {
  984. String path = EditorSettings::get_singleton()->get(p_property_key);
  985. String conf = path.plus_file("project.godot");
  986. bool grayed = false;
  987. bool missing = false;
  988. Ref<ConfigFile> cf = memnew(ConfigFile);
  989. Error cf_err = cf->load(conf);
  990. int config_version = 0;
  991. String project_name = TTR("Unnamed Project");
  992. if (cf_err == OK) {
  993. String cf_project_name = static_cast<String>(cf->get_value("application", "config/name", ""));
  994. if (cf_project_name != "") {
  995. project_name = cf_project_name.xml_unescape();
  996. }
  997. config_version = (int)cf->get_value("", "config_version", 0);
  998. }
  999. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1000. // Comes from an incompatible (more recent) Godot version, grey it out
  1001. grayed = true;
  1002. }
  1003. String description = cf->get_value("application", "config/description", "");
  1004. String icon = cf->get_value("application", "config/icon", "");
  1005. String main_scene = cf->get_value("application", "run/main_scene", "");
  1006. uint64_t last_modified = 0;
  1007. if (FileAccess::exists(conf)) {
  1008. last_modified = FileAccess::get_modified_time(conf);
  1009. String fscache = path.plus_file(".fscache");
  1010. if (FileAccess::exists(fscache)) {
  1011. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  1012. if (cache_modified > last_modified) {
  1013. last_modified = cache_modified;
  1014. }
  1015. }
  1016. } else {
  1017. grayed = true;
  1018. missing = true;
  1019. print_line("Project is missing: " + conf);
  1020. }
  1021. String project_key = p_property_key.get_slice("/", 1);
  1022. p_item = Item(project_key, project_name, description, path, icon, main_scene, last_modified, p_favorite, grayed, missing, config_version);
  1023. }
  1024. void ProjectList::load_projects() {
  1025. // This is a full, hard reload of the list. Don't call this unless really required, it's expensive.
  1026. // If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
  1027. // Clear whole list
  1028. for (int i = 0; i < _projects.size(); ++i) {
  1029. Item &project = _projects.write[i];
  1030. CRASH_COND(project.control == nullptr);
  1031. memdelete(project.control); // Why not queue_free()?
  1032. }
  1033. _projects.clear();
  1034. _last_clicked = "";
  1035. _selected_project_keys.clear();
  1036. // Load data
  1037. // TODO Would be nice to change how projects and favourites are stored... it complicates things a bit.
  1038. // Use a dictionary associating project path to metadata (like is_favorite).
  1039. List<PropertyInfo> properties;
  1040. EditorSettings::get_singleton()->get_property_list(&properties);
  1041. Set<String> favorites;
  1042. // Find favourites...
  1043. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1044. String property_key = E->get().name;
  1045. if (property_key.begins_with("favorite_projects/")) {
  1046. favorites.insert(property_key);
  1047. }
  1048. }
  1049. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1050. // This is actually something like "projects/C:::Documents::Godot::Projects::MyGame"
  1051. String property_key = E->get().name;
  1052. if (!property_key.begins_with("projects/")) {
  1053. continue;
  1054. }
  1055. String project_key = property_key.get_slice("/", 1);
  1056. bool favorite = favorites.has("favorite_projects/" + project_key);
  1057. Item item;
  1058. load_project_data(property_key, item, favorite);
  1059. _projects.push_back(item);
  1060. }
  1061. // Create controls
  1062. for (int i = 0; i < _projects.size(); ++i) {
  1063. create_project_item_control(i);
  1064. }
  1065. sort_projects();
  1066. set_v_scroll(0);
  1067. update_icons_async();
  1068. update_dock_menu();
  1069. }
  1070. void ProjectList::update_dock_menu() {
  1071. OS::get_singleton()->global_menu_clear("_dock");
  1072. int favs_added = 0;
  1073. int total_added = 0;
  1074. for (int i = 0; i < _projects.size(); ++i) {
  1075. if (!_projects[i].grayed && !_projects[i].missing) {
  1076. if (_projects[i].favorite) {
  1077. favs_added++;
  1078. } else {
  1079. if (favs_added != 0) {
  1080. OS::get_singleton()->global_menu_add_separator("_dock");
  1081. }
  1082. favs_added = 0;
  1083. }
  1084. OS::get_singleton()->global_menu_add_item("_dock", _projects[i].project_name + " ( " + _projects[i].path + " )", GLOBAL_OPEN_PROJECT, Variant(_projects[i].path.plus_file("project.godot")));
  1085. total_added++;
  1086. }
  1087. }
  1088. if (total_added != 0) {
  1089. OS::get_singleton()->global_menu_add_separator("_dock");
  1090. }
  1091. OS::get_singleton()->global_menu_add_item("_dock", TTR("New Window"), GLOBAL_NEW_WINDOW, Variant());
  1092. }
  1093. void ProjectList::create_project_item_control(int p_index) {
  1094. // Will be added last in the list, so make sure indexes match
  1095. ERR_FAIL_COND(p_index != _scroll_children->get_child_count());
  1096. Item &item = _projects.write[p_index];
  1097. ERR_FAIL_COND(item.control != nullptr); // Already created
  1098. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  1099. Color font_color = get_color("font_color", "Tree");
  1100. ProjectListItemControl *hb = memnew(ProjectListItemControl);
  1101. hb->connect("draw", this, "_panel_draw", varray(hb));
  1102. hb->connect("gui_input", this, "_panel_input", varray(hb));
  1103. hb->add_constant_override("separation", 10 * EDSCALE);
  1104. hb->set_tooltip(item.description);
  1105. VBoxContainer *favorite_box = memnew(VBoxContainer);
  1106. favorite_box->set_name("FavoriteBox");
  1107. TextureButton *favorite = memnew(TextureButton);
  1108. favorite->set_name("FavoriteButton");
  1109. favorite->set_normal_texture(favorite_icon);
  1110. // This makes the project's "hover" style display correctly when hovering the favorite icon
  1111. favorite->set_mouse_filter(MOUSE_FILTER_PASS);
  1112. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  1113. favorite_box->add_child(favorite);
  1114. favorite_box->set_alignment(BoxContainer::ALIGN_CENTER);
  1115. hb->add_child(favorite_box);
  1116. hb->favorite_button = favorite;
  1117. hb->set_is_favorite(item.favorite);
  1118. TextureRect *tf = memnew(TextureRect);
  1119. // The project icon may not be loaded by the time the control is displayed,
  1120. // so use a loading placeholder.
  1121. tf->set_texture(get_icon("ProjectIconLoading", "EditorIcons"));
  1122. tf->set_v_size_flags(SIZE_SHRINK_CENTER);
  1123. if (item.missing) {
  1124. tf->set_modulate(Color(1, 1, 1, 0.5));
  1125. }
  1126. hb->add_child(tf);
  1127. hb->icon = tf;
  1128. VBoxContainer *vb = memnew(VBoxContainer);
  1129. if (item.grayed) {
  1130. vb->set_modulate(Color(1, 1, 1, 0.5));
  1131. }
  1132. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1133. hb->add_child(vb);
  1134. Control *ec = memnew(Control);
  1135. ec->set_custom_minimum_size(Size2(0, 1));
  1136. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  1137. vb->add_child(ec);
  1138. Label *title = memnew(Label(!item.missing ? item.project_name : TTR("Missing Project")));
  1139. title->add_font_override("font", get_font("title", "EditorFonts"));
  1140. title->add_color_override("font_color", font_color);
  1141. title->set_clip_text(true);
  1142. vb->add_child(title);
  1143. HBoxContainer *path_hb = memnew(HBoxContainer);
  1144. path_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1145. vb->add_child(path_hb);
  1146. Button *show = memnew(Button);
  1147. // Display a folder icon if the project directory can be opened, or a "broken file" icon if it can't.
  1148. show->set_icon(get_icon(!item.missing ? "Load" : "FileBroken", "EditorIcons"));
  1149. if (!item.grayed) {
  1150. // Don't make the icon less prominent if the parent is already grayed out.
  1151. show->set_modulate(Color(1, 1, 1, 0.5));
  1152. }
  1153. path_hb->add_child(show);
  1154. if (!item.missing) {
  1155. show->connect("pressed", this, "_show_project", varray(item.path));
  1156. show->set_tooltip(TTR("Show in File Manager"));
  1157. } else {
  1158. show->set_tooltip(TTR("Error: Project is missing on the filesystem."));
  1159. }
  1160. Label *fpath = memnew(Label(item.path));
  1161. path_hb->add_child(fpath);
  1162. fpath->set_h_size_flags(SIZE_EXPAND_FILL);
  1163. fpath->set_modulate(Color(1, 1, 1, 0.5));
  1164. fpath->add_color_override("font_color", font_color);
  1165. fpath->set_clip_text(true);
  1166. _scroll_children->add_child(hb);
  1167. item.control = hb;
  1168. }
  1169. void ProjectList::set_search_term(String p_search_term) {
  1170. _search_term = p_search_term;
  1171. }
  1172. void ProjectList::set_order_option(ProjectListFilter::FilterOption p_option) {
  1173. if (_order_option != p_option) {
  1174. _order_option = p_option;
  1175. EditorSettings::get_singleton()->set("project_manager/sorting_order", (int)_order_option);
  1176. EditorSettings::get_singleton()->save();
  1177. }
  1178. }
  1179. void ProjectList::sort_projects() {
  1180. SortArray<Item, ProjectListComparator> sorter;
  1181. sorter.compare.order_option = _order_option;
  1182. sorter.sort(_projects.ptrw(), _projects.size());
  1183. for (int i = 0; i < _projects.size(); ++i) {
  1184. Item &item = _projects.write[i];
  1185. bool visible = true;
  1186. if (_search_term != "") {
  1187. String search_path;
  1188. if (_search_term.find("/") != -1) {
  1189. // Search path will match the whole path
  1190. search_path = item.path;
  1191. } else {
  1192. // Search path will only match the last path component to make searching more strict
  1193. search_path = item.path.get_file();
  1194. }
  1195. // When searching, display projects whose name or path contain the search term
  1196. visible = item.project_name.findn(_search_term) != -1 || search_path.findn(_search_term) != -1;
  1197. }
  1198. item.control->set_visible(visible);
  1199. }
  1200. for (int i = 0; i < _projects.size(); ++i) {
  1201. Item &item = _projects.write[i];
  1202. item.control->get_parent()->move_child(item.control, i);
  1203. }
  1204. // Rewind the coroutine because order of projects changed
  1205. update_icons_async();
  1206. update_dock_menu();
  1207. }
  1208. const Set<String> &ProjectList::get_selected_project_keys() const {
  1209. // Faster if that's all you need
  1210. return _selected_project_keys;
  1211. }
  1212. Vector<ProjectList::Item> ProjectList::get_selected_projects() const {
  1213. Vector<Item> items;
  1214. if (_selected_project_keys.size() == 0) {
  1215. return items;
  1216. }
  1217. items.resize(_selected_project_keys.size());
  1218. int j = 0;
  1219. for (int i = 0; i < _projects.size(); ++i) {
  1220. const Item &item = _projects[i];
  1221. if (_selected_project_keys.has(item.project_key)) {
  1222. items.write[j++] = item;
  1223. }
  1224. }
  1225. ERR_FAIL_COND_V(j != items.size(), items);
  1226. return items;
  1227. }
  1228. void ProjectList::ensure_project_visible(int p_index) {
  1229. const Item &item = _projects[p_index];
  1230. ensure_control_visible(item.control);
  1231. }
  1232. int ProjectList::get_single_selected_index() const {
  1233. if (_selected_project_keys.size() == 0) {
  1234. // Default selection
  1235. return 0;
  1236. }
  1237. String key;
  1238. if (_selected_project_keys.size() == 1) {
  1239. // Only one selected
  1240. key = _selected_project_keys.front()->get();
  1241. } else {
  1242. // Multiple selected, consider the last clicked one as "main"
  1243. key = _last_clicked;
  1244. }
  1245. for (int i = 0; i < _projects.size(); ++i) {
  1246. if (_projects[i].project_key == key) {
  1247. return i;
  1248. }
  1249. }
  1250. return 0;
  1251. }
  1252. void ProjectList::remove_project(int p_index, bool p_update_settings) {
  1253. const Item item = _projects[p_index]; // Take a copy
  1254. _selected_project_keys.erase(item.project_key);
  1255. if (_last_clicked == item.project_key) {
  1256. _last_clicked = "";
  1257. }
  1258. memdelete(item.control);
  1259. _projects.remove(p_index);
  1260. if (p_update_settings) {
  1261. EditorSettings::get_singleton()->erase("projects/" + item.project_key);
  1262. EditorSettings::get_singleton()->erase("favorite_projects/" + item.project_key);
  1263. // Not actually saving the file, in case you are doing more changes to settings
  1264. }
  1265. update_dock_menu();
  1266. }
  1267. bool ProjectList::is_any_project_missing() const {
  1268. for (int i = 0; i < _projects.size(); ++i) {
  1269. if (_projects[i].missing) {
  1270. return true;
  1271. }
  1272. }
  1273. return false;
  1274. }
  1275. void ProjectList::erase_missing_projects() {
  1276. if (_projects.empty()) {
  1277. return;
  1278. }
  1279. int deleted_count = 0;
  1280. int remaining_count = 0;
  1281. for (int i = 0; i < _projects.size(); ++i) {
  1282. const Item &item = _projects[i];
  1283. if (item.missing) {
  1284. remove_project(i, true);
  1285. --i;
  1286. ++deleted_count;
  1287. } else {
  1288. ++remaining_count;
  1289. }
  1290. }
  1291. print_line("Removed " + itos(deleted_count) + " projects from the list, remaining " + itos(remaining_count) + " projects");
  1292. EditorSettings::get_singleton()->save();
  1293. }
  1294. int ProjectList::refresh_project(const String &dir_path) {
  1295. // Reads editor settings and reloads information about a specific project.
  1296. // If it wasn't loaded and should be in the list, it is added (i.e new project).
  1297. // If it isn't in the list anymore, it is removed.
  1298. // If it is in the list but doesn't exist anymore, it is marked as missing.
  1299. String project_key = get_project_key_from_path(dir_path);
  1300. // Read project manager settings
  1301. bool is_favourite = false;
  1302. bool should_be_in_list = false;
  1303. String property_key = "projects/" + project_key;
  1304. {
  1305. List<PropertyInfo> properties;
  1306. EditorSettings::get_singleton()->get_property_list(&properties);
  1307. String favorite_property_key = "favorite_projects/" + project_key;
  1308. bool found = false;
  1309. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1310. String prop = E->get().name;
  1311. if (!found && prop == property_key) {
  1312. found = true;
  1313. } else if (!is_favourite && prop == favorite_property_key) {
  1314. is_favourite = true;
  1315. }
  1316. }
  1317. should_be_in_list = found;
  1318. }
  1319. bool was_selected = _selected_project_keys.has(project_key);
  1320. // Remove item in any case
  1321. for (int i = 0; i < _projects.size(); ++i) {
  1322. const Item &existing_item = _projects[i];
  1323. if (existing_item.path == dir_path) {
  1324. remove_project(i, false);
  1325. break;
  1326. }
  1327. }
  1328. int index = -1;
  1329. if (should_be_in_list) {
  1330. // Recreate it with updated info
  1331. Item item;
  1332. load_project_data(property_key, item, is_favourite);
  1333. _projects.push_back(item);
  1334. create_project_item_control(_projects.size() - 1);
  1335. sort_projects();
  1336. for (int i = 0; i < _projects.size(); ++i) {
  1337. if (_projects[i].project_key == project_key) {
  1338. if (was_selected) {
  1339. select_project(i);
  1340. ensure_project_visible(i);
  1341. }
  1342. load_project_icon(i);
  1343. index = i;
  1344. break;
  1345. }
  1346. }
  1347. }
  1348. return index;
  1349. }
  1350. int ProjectList::get_project_count() const {
  1351. return _projects.size();
  1352. }
  1353. void ProjectList::select_project(int p_index) {
  1354. Vector<Item> previous_selected_items = get_selected_projects();
  1355. _selected_project_keys.clear();
  1356. for (int i = 0; i < previous_selected_items.size(); ++i) {
  1357. previous_selected_items[i].control->update();
  1358. }
  1359. toggle_select(p_index);
  1360. }
  1361. inline void sort(int &a, int &b) {
  1362. if (a > b) {
  1363. int temp = a;
  1364. a = b;
  1365. b = temp;
  1366. }
  1367. }
  1368. void ProjectList::select_range(int p_begin, int p_end) {
  1369. sort(p_begin, p_end);
  1370. select_project(p_begin);
  1371. for (int i = p_begin + 1; i <= p_end; ++i) {
  1372. toggle_select(i);
  1373. }
  1374. }
  1375. void ProjectList::toggle_select(int p_index) {
  1376. Item &item = _projects.write[p_index];
  1377. if (_selected_project_keys.has(item.project_key)) {
  1378. _selected_project_keys.erase(item.project_key);
  1379. } else {
  1380. _selected_project_keys.insert(item.project_key);
  1381. }
  1382. item.control->update();
  1383. }
  1384. void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
  1385. if (_selected_project_keys.size() == 0) {
  1386. return;
  1387. }
  1388. for (int i = 0; i < _projects.size(); ++i) {
  1389. Item &item = _projects.write[i];
  1390. if (_selected_project_keys.has(item.project_key) && item.control->is_visible()) {
  1391. EditorSettings::get_singleton()->erase("projects/" + item.project_key);
  1392. EditorSettings::get_singleton()->erase("favorite_projects/" + item.project_key);
  1393. if (p_delete_project_contents) {
  1394. OS::get_singleton()->move_to_trash(item.path);
  1395. }
  1396. memdelete(item.control);
  1397. _projects.remove(i);
  1398. --i;
  1399. }
  1400. }
  1401. EditorSettings::get_singleton()->save();
  1402. _selected_project_keys.clear();
  1403. _last_clicked = "";
  1404. update_dock_menu();
  1405. }
  1406. // Draws selected project highlight
  1407. void ProjectList::_panel_draw(Node *p_hb) {
  1408. Control *hb = Object::cast_to<Control>(p_hb);
  1409. 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"));
  1410. String key = _projects[p_hb->get_index()].project_key;
  1411. if (_selected_project_keys.has(key)) {
  1412. hb->draw_style_box(get_stylebox("selected", "Tree"), Rect2(Point2(), hb->get_size() - Size2(10, 0) * EDSCALE));
  1413. }
  1414. }
  1415. // Input for each item in the list
  1416. void ProjectList::_panel_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  1417. Ref<InputEventMouseButton> mb = p_ev;
  1418. int clicked_index = p_hb->get_index();
  1419. const Item &clicked_project = _projects[clicked_index];
  1420. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1421. if (mb->get_shift() && _selected_project_keys.size() > 0 && _last_clicked != "" && clicked_project.project_key != _last_clicked) {
  1422. int anchor_index = -1;
  1423. for (int i = 0; i < _projects.size(); ++i) {
  1424. const Item &p = _projects[i];
  1425. if (p.project_key == _last_clicked) {
  1426. anchor_index = p.control->get_index();
  1427. break;
  1428. }
  1429. }
  1430. CRASH_COND(anchor_index == -1);
  1431. select_range(anchor_index, clicked_index);
  1432. } else if (mb->get_control()) {
  1433. toggle_select(clicked_index);
  1434. } else {
  1435. _last_clicked = clicked_project.project_key;
  1436. select_project(clicked_index);
  1437. }
  1438. emit_signal(SIGNAL_SELECTION_CHANGED);
  1439. // Do not allow opening a project more than once using a single project manager instance.
  1440. // Opening the same project in several editor instances at once can lead to various issues.
  1441. if (!mb->get_control() && mb->is_doubleclick() && !project_opening_initiated) {
  1442. emit_signal(SIGNAL_PROJECT_ASK_OPEN);
  1443. }
  1444. }
  1445. }
  1446. void ProjectList::_favorite_pressed(Node *p_hb) {
  1447. ProjectListItemControl *control = Object::cast_to<ProjectListItemControl>(p_hb);
  1448. int index = control->get_index();
  1449. Item item = _projects.write[index]; // Take copy
  1450. item.favorite = !item.favorite;
  1451. if (item.favorite) {
  1452. EditorSettings::get_singleton()->set("favorite_projects/" + item.project_key, item.path);
  1453. } else {
  1454. EditorSettings::get_singleton()->erase("favorite_projects/" + item.project_key);
  1455. }
  1456. EditorSettings::get_singleton()->save();
  1457. _projects.write[index] = item;
  1458. control->set_is_favorite(item.favorite);
  1459. sort_projects();
  1460. if (item.favorite) {
  1461. for (int i = 0; i < _projects.size(); ++i) {
  1462. if (_projects[i].project_key == item.project_key) {
  1463. ensure_project_visible(i);
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. update_dock_menu();
  1469. }
  1470. void ProjectList::_show_project(const String &p_path) {
  1471. OS::get_singleton()->shell_open(String("file://") + p_path);
  1472. }
  1473. const char *ProjectList::SIGNAL_SELECTION_CHANGED = "selection_changed";
  1474. const char *ProjectList::SIGNAL_PROJECT_ASK_OPEN = "project_ask_open";
  1475. void ProjectList::_bind_methods() {
  1476. ClassDB::bind_method("_panel_draw", &ProjectList::_panel_draw);
  1477. ClassDB::bind_method("_panel_input", &ProjectList::_panel_input);
  1478. ClassDB::bind_method("_favorite_pressed", &ProjectList::_favorite_pressed);
  1479. ClassDB::bind_method("_show_project", &ProjectList::_show_project);
  1480. ADD_SIGNAL(MethodInfo(SIGNAL_SELECTION_CHANGED));
  1481. ADD_SIGNAL(MethodInfo(SIGNAL_PROJECT_ASK_OPEN));
  1482. }
  1483. void ProjectManager::_notification(int p_what) {
  1484. switch (p_what) {
  1485. case NOTIFICATION_ENTER_TREE: {
  1486. Engine::get_singleton()->set_editor_hint(false);
  1487. } break;
  1488. case NOTIFICATION_RESIZED: {
  1489. if (open_templates && open_templates->is_visible()) {
  1490. open_templates->popup_centered_minsize();
  1491. }
  1492. if (asset_library) {
  1493. real_t size = get_size().x / EDSCALE;
  1494. // Adjust names of tabs to fit the new size.
  1495. if (size < 650) {
  1496. local_projects_hb->set_name(TTR("Local"));
  1497. asset_library->set_name(TTR("Asset Library"));
  1498. } else {
  1499. local_projects_hb->set_name(TTR("Local Projects"));
  1500. asset_library->set_name(TTR("Asset Library Projects"));
  1501. }
  1502. }
  1503. } break;
  1504. case NOTIFICATION_READY: {
  1505. if (_project_list->get_project_count() >= 1) {
  1506. // Focus on the search box immediately to allow the user
  1507. // to search without having to reach for their mouse
  1508. project_filter->search_box->grab_focus();
  1509. }
  1510. if (asset_library) {
  1511. // Suggest browsing asset library to get templates/demos.
  1512. if (open_templates && _project_list->get_project_count() == 0) {
  1513. open_templates->popup_centered_minsize();
  1514. }
  1515. }
  1516. } break;
  1517. case NOTIFICATION_VISIBILITY_CHANGED: {
  1518. set_process_unhandled_input(is_visible_in_tree());
  1519. } break;
  1520. case NOTIFICATION_WM_QUIT_REQUEST: {
  1521. _dim_window();
  1522. } break;
  1523. case NOTIFICATION_WM_ABOUT: {
  1524. _show_about();
  1525. } break;
  1526. }
  1527. }
  1528. void ProjectManager::_dim_window() {
  1529. // This method must be called before calling `get_tree()->quit()`.
  1530. // Otherwise, its effect won't be visible
  1531. // Dim the project manager window while it's quitting to make it clearer that it's busy.
  1532. // No transition is applied, as the effect needs to be visible immediately
  1533. float c = 0.5f;
  1534. Color dim_color = Color(c, c, c);
  1535. gui_base->set_modulate(dim_color);
  1536. }
  1537. void ProjectManager::_update_project_buttons() {
  1538. Vector<ProjectList::Item> selected_projects = _project_list->get_selected_projects();
  1539. bool empty_selection = selected_projects.empty();
  1540. bool is_missing_project_selected = false;
  1541. for (int i = 0; i < selected_projects.size(); ++i) {
  1542. if (selected_projects[i].missing) {
  1543. is_missing_project_selected = true;
  1544. break;
  1545. }
  1546. }
  1547. erase_btn->set_disabled(empty_selection);
  1548. open_btn->set_disabled(empty_selection || is_missing_project_selected);
  1549. rename_btn->set_disabled(empty_selection || is_missing_project_selected);
  1550. run_btn->set_disabled(empty_selection || is_missing_project_selected);
  1551. erase_missing_btn->set_disabled(!_project_list->is_any_project_missing());
  1552. }
  1553. void ProjectManager::_unhandled_input(const Ref<InputEvent> &p_ev) {
  1554. Ref<InputEventKey> k = p_ev;
  1555. if (k.is_valid()) {
  1556. if (!k->is_pressed()) {
  1557. return;
  1558. }
  1559. // Pressing Command + Q quits the Project Manager
  1560. // This is handled by the platform implementation on macOS,
  1561. // so only define the shortcut on other platforms
  1562. #ifndef OSX_ENABLED
  1563. if (k->get_scancode_with_modifiers() == (KEY_MASK_CMD | KEY_Q)) {
  1564. _dim_window();
  1565. get_tree()->quit();
  1566. }
  1567. #endif
  1568. if (tabs->get_current_tab() != 0) {
  1569. return;
  1570. }
  1571. bool scancode_handled = true;
  1572. switch (k->get_scancode()) {
  1573. case KEY_ENTER: {
  1574. _open_selected_projects_ask();
  1575. } break;
  1576. case KEY_HOME: {
  1577. if (_project_list->get_project_count() > 0) {
  1578. _project_list->select_project(0);
  1579. _update_project_buttons();
  1580. }
  1581. } break;
  1582. case KEY_END: {
  1583. if (_project_list->get_project_count() > 0) {
  1584. _project_list->select_project(_project_list->get_project_count() - 1);
  1585. _update_project_buttons();
  1586. }
  1587. } break;
  1588. case KEY_UP: {
  1589. if (k->get_shift()) {
  1590. break;
  1591. }
  1592. int index = _project_list->get_single_selected_index();
  1593. if (index > 0) {
  1594. _project_list->select_project(index - 1);
  1595. _project_list->ensure_project_visible(index - 1);
  1596. _update_project_buttons();
  1597. }
  1598. break;
  1599. }
  1600. case KEY_DOWN: {
  1601. if (k->get_shift()) {
  1602. break;
  1603. }
  1604. int index = _project_list->get_single_selected_index();
  1605. if (index + 1 < _project_list->get_project_count()) {
  1606. _project_list->select_project(index + 1);
  1607. _project_list->ensure_project_visible(index + 1);
  1608. _update_project_buttons();
  1609. }
  1610. } break;
  1611. case KEY_F: {
  1612. if (k->get_command()) {
  1613. this->project_filter->search_box->grab_focus();
  1614. } else {
  1615. scancode_handled = false;
  1616. }
  1617. } break;
  1618. default: {
  1619. scancode_handled = false;
  1620. } break;
  1621. }
  1622. if (scancode_handled) {
  1623. accept_event();
  1624. }
  1625. }
  1626. }
  1627. void ProjectManager::_load_recent_projects() {
  1628. _project_list->set_order_option(project_order_filter->get_filter_option());
  1629. _project_list->set_search_term(project_filter->get_search_term());
  1630. _project_list->load_projects();
  1631. _update_project_buttons();
  1632. tabs->set_current_tab(0);
  1633. }
  1634. void ProjectManager::_on_projects_updated() {
  1635. Vector<ProjectList::Item> selected_projects = _project_list->get_selected_projects();
  1636. int index = 0;
  1637. for (int i = 0; i < selected_projects.size(); ++i) {
  1638. index = _project_list->refresh_project(selected_projects[i].path);
  1639. }
  1640. if (index != -1) {
  1641. _project_list->ensure_project_visible(index);
  1642. }
  1643. _project_list->update_dock_menu();
  1644. }
  1645. void ProjectManager::_on_project_created(const String &dir) {
  1646. project_filter->clear();
  1647. int i = _project_list->refresh_project(dir);
  1648. _project_list->select_project(i);
  1649. _project_list->ensure_project_visible(i);
  1650. _open_selected_projects_ask();
  1651. _project_list->update_dock_menu();
  1652. }
  1653. void ProjectManager::_confirm_update_settings() {
  1654. _open_selected_projects();
  1655. }
  1656. void ProjectManager::_global_menu_action(const Variant &p_id, const Variant &p_meta) {
  1657. int id = (int)p_id;
  1658. if (id == ProjectList::GLOBAL_NEW_WINDOW) {
  1659. List<String> args;
  1660. args.push_back("-p");
  1661. String exec = OS::get_singleton()->get_executable_path();
  1662. OS::ProcessID pid = 0;
  1663. OS::get_singleton()->execute(exec, args, false, &pid);
  1664. } else if (id == ProjectList::GLOBAL_OPEN_PROJECT) {
  1665. String conf = (String)p_meta;
  1666. if (conf != String()) {
  1667. List<String> args;
  1668. args.push_back(conf);
  1669. String exec = OS::get_singleton()->get_executable_path();
  1670. OS::ProcessID pid = 0;
  1671. OS::get_singleton()->execute(exec, args, false, &pid);
  1672. }
  1673. }
  1674. }
  1675. void ProjectManager::_open_selected_projects() {
  1676. // Show loading text to tell the user that the project manager is busy loading.
  1677. // This is especially important for the HTML5 project manager.
  1678. loading_label->show();
  1679. const Set<String> &selected_list = _project_list->get_selected_project_keys();
  1680. for (const Set<String>::Element *E = selected_list.front(); E; E = E->next()) {
  1681. const String &selected = E->get();
  1682. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1683. String conf = path.plus_file("project.godot");
  1684. if (!FileAccess::exists(conf)) {
  1685. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1686. dialog_error->popup_centered_minsize();
  1687. return;
  1688. }
  1689. print_line("Editing project: " + path + " (" + selected + ")");
  1690. List<String> args;
  1691. args.push_back("--path");
  1692. args.push_back(path);
  1693. args.push_back("--editor");
  1694. if (OS::get_singleton()->is_stdout_debug_enabled()) {
  1695. args.push_back("--debug");
  1696. }
  1697. if (OS::get_singleton()->is_stdout_verbose()) {
  1698. args.push_back("--verbose");
  1699. }
  1700. if (OS::get_singleton()->is_disable_crash_handler()) {
  1701. args.push_back("--disable-crash-handler");
  1702. }
  1703. String exec = OS::get_singleton()->get_executable_path();
  1704. OS::ProcessID pid = 0;
  1705. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1706. ERR_FAIL_COND(err);
  1707. }
  1708. _project_list->project_opening_initiated = true;
  1709. _dim_window();
  1710. get_tree()->quit();
  1711. }
  1712. void ProjectManager::_open_selected_projects_ask() {
  1713. const Set<String> &selected_list = _project_list->get_selected_project_keys();
  1714. if (selected_list.size() < 1) {
  1715. return;
  1716. }
  1717. if (selected_list.size() > 1) {
  1718. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  1719. multi_open_ask->popup_centered_minsize();
  1720. return;
  1721. }
  1722. ProjectList::Item project = _project_list->get_selected_projects()[0];
  1723. if (project.missing) {
  1724. return;
  1725. }
  1726. // Update the project settings or don't open
  1727. String conf = project.path.plus_file("project.godot");
  1728. int config_version = project.version;
  1729. // Check if the config_version property was empty or 0
  1730. if (config_version == 0) {
  1731. 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));
  1732. ask_update_settings->popup_centered_minsize();
  1733. return;
  1734. }
  1735. // Check if we need to convert project settings from an earlier engine version
  1736. if (config_version < ProjectSettings::CONFIG_VERSION) {
  1737. 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));
  1738. ask_update_settings->popup_centered_minsize();
  1739. return;
  1740. }
  1741. // Check if the file was generated by a newer, incompatible engine version
  1742. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1743. 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."), project.path));
  1744. dialog_error->popup_centered_minsize();
  1745. return;
  1746. }
  1747. // Open if the project is up-to-date
  1748. _open_selected_projects();
  1749. }
  1750. void ProjectManager::_run_project_confirm() {
  1751. Vector<ProjectList::Item> selected_list = _project_list->get_selected_projects();
  1752. for (int i = 0; i < selected_list.size(); ++i) {
  1753. const String &selected_main = selected_list[i].main_scene;
  1754. if (selected_main == "") {
  1755. 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."));
  1756. run_error_diag->popup_centered();
  1757. continue;
  1758. }
  1759. const String &selected = selected_list[i].project_key;
  1760. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1761. String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
  1762. if (!DirAccess::exists(path + "/" + project_data_dir_name)) {
  1763. run_error_diag->set_text(TTR("Can't run project: Assets need to be imported.\nPlease edit the project to trigger the initial import."));
  1764. run_error_diag->popup_centered();
  1765. continue;
  1766. }
  1767. print_line("Running project: " + path + " (" + selected + ")");
  1768. List<String> args;
  1769. args.push_back("--path");
  1770. args.push_back(path);
  1771. if (OS::get_singleton()->is_disable_crash_handler()) {
  1772. args.push_back("--disable-crash-handler");
  1773. }
  1774. String exec = OS::get_singleton()->get_executable_path();
  1775. OS::ProcessID pid = 0;
  1776. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1777. ERR_FAIL_COND(err);
  1778. }
  1779. }
  1780. // When you press the "Run" button
  1781. void ProjectManager::_run_project() {
  1782. const Set<String> &selected_list = _project_list->get_selected_project_keys();
  1783. if (selected_list.size() < 1) {
  1784. return;
  1785. }
  1786. if (selected_list.size() > 1) {
  1787. multi_run_ask->set_text(vformat(TTR("Are you sure to run %d projects at once?"), selected_list.size()));
  1788. multi_run_ask->popup_centered_minsize();
  1789. } else {
  1790. _run_project_confirm();
  1791. }
  1792. }
  1793. void ProjectManager::_scan_dir(const String &path, List<String> *r_projects) {
  1794. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1795. Error error = da->change_dir(path);
  1796. ERR_FAIL_COND_MSG(error != OK, "Could not scan directory at: " + path);
  1797. da->list_dir_begin();
  1798. String n = da->get_next();
  1799. while (n != String()) {
  1800. if (da->current_is_dir() && !n.begins_with(".")) {
  1801. _scan_dir(da->get_current_dir().plus_file(n), r_projects);
  1802. } else if (n == "project.godot") {
  1803. r_projects->push_back(da->get_current_dir());
  1804. }
  1805. n = da->get_next();
  1806. }
  1807. da->list_dir_end();
  1808. }
  1809. void ProjectManager::_scan_begin(const String &p_base) {
  1810. print_line("Scanning projects at: " + p_base);
  1811. List<String> projects;
  1812. _scan_dir(p_base, &projects);
  1813. print_line("Found " + itos(projects.size()) + " projects.");
  1814. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  1815. String proj = get_project_key_from_path(E->get());
  1816. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  1817. }
  1818. EditorSettings::get_singleton()->save();
  1819. _load_recent_projects();
  1820. }
  1821. void ProjectManager::_scan_projects() {
  1822. scan_dir->popup_centered_ratio();
  1823. }
  1824. void ProjectManager::_new_project() {
  1825. npdialog->set_mode(ProjectDialog::MODE_NEW);
  1826. npdialog->show_dialog();
  1827. }
  1828. void ProjectManager::_import_project() {
  1829. npdialog->set_mode(ProjectDialog::MODE_IMPORT);
  1830. npdialog->show_dialog();
  1831. }
  1832. void ProjectManager::_rename_project() {
  1833. const Set<String> &selected_list = _project_list->get_selected_project_keys();
  1834. if (selected_list.size() == 0) {
  1835. return;
  1836. }
  1837. for (Set<String>::Element *E = selected_list.front(); E; E = E->next()) {
  1838. const String &selected = E->get();
  1839. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1840. npdialog->set_project_path(path);
  1841. npdialog->set_mode(ProjectDialog::MODE_RENAME);
  1842. npdialog->show_dialog();
  1843. }
  1844. }
  1845. void ProjectManager::_erase_project_confirm() {
  1846. _project_list->erase_selected_projects(delete_project_contents->is_pressed());
  1847. _update_project_buttons();
  1848. }
  1849. void ProjectManager::_erase_missing_projects_confirm() {
  1850. _project_list->erase_missing_projects();
  1851. _update_project_buttons();
  1852. }
  1853. void ProjectManager::_erase_project() {
  1854. const Set<String> &selected_list = _project_list->get_selected_project_keys();
  1855. if (selected_list.size() == 0) {
  1856. return;
  1857. }
  1858. String confirm_message;
  1859. if (selected_list.size() >= 2) {
  1860. confirm_message = vformat(TTR("Remove %d projects from the list?"), selected_list.size());
  1861. } else {
  1862. confirm_message = TTR("Remove this project from the list?");
  1863. }
  1864. erase_ask_label->set_text(confirm_message);
  1865. delete_project_contents->set_pressed(false);
  1866. erase_ask->popup_centered_minsize();
  1867. }
  1868. void ProjectManager::_erase_missing_projects() {
  1869. erase_missing_ask->set_text(TTR("Remove all missing projects from the list?\nThe project folders' contents won't be modified."));
  1870. erase_missing_ask->popup_centered_minsize();
  1871. }
  1872. void ProjectManager::_show_about() {
  1873. about->popup_centered(Size2(780, 500) * EDSCALE);
  1874. }
  1875. void ProjectManager::_language_selected(int p_id) {
  1876. String lang = language_btn->get_item_metadata(p_id);
  1877. EditorSettings::get_singleton()->set("interface/editor/editor_language", lang);
  1878. language_btn->set_text(lang);
  1879. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1880. language_restart_ask->set_text(TTR("Language changed.\nThe interface will update after restarting the editor or project manager."));
  1881. language_restart_ask->popup_centered();
  1882. }
  1883. void ProjectManager::_restart_confirm() {
  1884. List<String> args = OS::get_singleton()->get_cmdline_args();
  1885. String exec = OS::get_singleton()->get_executable_path();
  1886. OS::ProcessID pid = 0;
  1887. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1888. ERR_FAIL_COND(err);
  1889. _dim_window();
  1890. get_tree()->quit();
  1891. }
  1892. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  1893. npdialog->set_mode(ProjectDialog::MODE_INSTALL);
  1894. npdialog->set_zip_path(p_zip_path);
  1895. npdialog->set_zip_title(p_title);
  1896. npdialog->show_dialog();
  1897. }
  1898. void ProjectManager::_files_dropped(PoolStringArray p_files, int p_screen) {
  1899. if (p_files.size() == 1 && p_files[0].ends_with(".zip")) {
  1900. const String file = p_files[0].get_file();
  1901. _install_project(p_files[0], file.substr(0, file.length() - 4).capitalize());
  1902. return;
  1903. }
  1904. Set<String> folders_set;
  1905. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1906. for (int i = 0; i < p_files.size(); i++) {
  1907. String file = p_files[i];
  1908. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  1909. }
  1910. memdelete(da);
  1911. if (folders_set.size() > 0) {
  1912. PoolStringArray folders;
  1913. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  1914. folders.append(E->get());
  1915. }
  1916. bool confirm = true;
  1917. if (folders.size() == 1) {
  1918. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1919. if (dir->change_dir(folders[0]) == OK) {
  1920. dir->list_dir_begin();
  1921. String file = dir->get_next();
  1922. while (confirm && file != String()) {
  1923. if (!dir->current_is_dir() && file.ends_with("project.godot")) {
  1924. confirm = false;
  1925. }
  1926. file = dir->get_next();
  1927. }
  1928. dir->list_dir_end();
  1929. }
  1930. memdelete(dir);
  1931. }
  1932. if (confirm) {
  1933. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  1934. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  1935. multi_scan_ask->set_text(
  1936. vformat(TTR("Are you sure to scan %s folders for existing Godot projects?\nThis could take a while."), folders.size()));
  1937. multi_scan_ask->popup_centered_minsize();
  1938. } else {
  1939. _scan_multiple_folders(folders);
  1940. }
  1941. }
  1942. }
  1943. void ProjectManager::_scan_multiple_folders(PoolStringArray p_files) {
  1944. for (int i = 0; i < p_files.size(); i++) {
  1945. _scan_begin(p_files.get(i));
  1946. }
  1947. }
  1948. void ProjectManager::_on_order_option_changed() {
  1949. _project_list->set_order_option(project_order_filter->get_filter_option());
  1950. _project_list->sort_projects();
  1951. }
  1952. void ProjectManager::_on_filter_option_changed() {
  1953. _project_list->set_search_term(project_filter->get_search_term());
  1954. _project_list->sort_projects();
  1955. }
  1956. void ProjectManager::_on_tab_changed(int p_tab) {
  1957. if (p_tab == 0) { // Projects
  1958. // Automatically grab focus when the user moves from the Templates tab
  1959. // back to the Projects tab.
  1960. LineEdit *search_box = project_filter->get_search_box();
  1961. if (search_box) {
  1962. search_box->grab_focus();
  1963. }
  1964. }
  1965. // The Templates tab's search field is focused on display in the asset
  1966. // library editor plugin code.
  1967. }
  1968. void ProjectManager::_bind_methods() {
  1969. ClassDB::bind_method("_open_selected_projects_ask", &ProjectManager::_open_selected_projects_ask);
  1970. ClassDB::bind_method("_open_selected_projects", &ProjectManager::_open_selected_projects);
  1971. ClassDB::bind_method(D_METHOD("_global_menu_action"), &ProjectManager::_global_menu_action, DEFVAL(Variant()));
  1972. ClassDB::bind_method("_run_project", &ProjectManager::_run_project);
  1973. ClassDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  1974. ClassDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  1975. ClassDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  1976. ClassDB::bind_method("_import_project", &ProjectManager::_import_project);
  1977. ClassDB::bind_method("_new_project", &ProjectManager::_new_project);
  1978. ClassDB::bind_method("_rename_project", &ProjectManager::_rename_project);
  1979. ClassDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  1980. ClassDB::bind_method("_erase_missing_projects", &ProjectManager::_erase_missing_projects);
  1981. ClassDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  1982. ClassDB::bind_method("_erase_missing_projects_confirm", &ProjectManager::_erase_missing_projects_confirm);
  1983. ClassDB::bind_method("_show_about", &ProjectManager::_show_about);
  1984. ClassDB::bind_method("_version_button_pressed", &ProjectManager::_version_button_pressed);
  1985. ClassDB::bind_method("_language_selected", &ProjectManager::_language_selected);
  1986. ClassDB::bind_method("_restart_confirm", &ProjectManager::_restart_confirm);
  1987. ClassDB::bind_method("_on_order_option_changed", &ProjectManager::_on_order_option_changed);
  1988. ClassDB::bind_method("_on_filter_option_changed", &ProjectManager::_on_filter_option_changed);
  1989. ClassDB::bind_method("_on_tab_changed", &ProjectManager::_on_tab_changed);
  1990. ClassDB::bind_method("_on_projects_updated", &ProjectManager::_on_projects_updated);
  1991. ClassDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  1992. ClassDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  1993. ClassDB::bind_method("_install_project", &ProjectManager::_install_project);
  1994. ClassDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  1995. ClassDB::bind_method("_open_asset_library", &ProjectManager::_open_asset_library);
  1996. ClassDB::bind_method("_confirm_update_settings", &ProjectManager::_confirm_update_settings);
  1997. ClassDB::bind_method("_update_project_buttons", &ProjectManager::_update_project_buttons);
  1998. ClassDB::bind_method(D_METHOD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  1999. }
  2000. void ProjectManager::_open_asset_library() {
  2001. asset_library->disable_community_support();
  2002. tabs->set_current_tab(1);
  2003. }
  2004. void ProjectManager::_version_button_pressed() {
  2005. OS::get_singleton()->set_clipboard(version_btn->get_text());
  2006. }
  2007. ProjectManager::ProjectManager() {
  2008. // load settings
  2009. if (!EditorSettings::get_singleton()) {
  2010. EditorSettings::create();
  2011. }
  2012. // Turn off some servers we aren't going to be using in the Project Manager.
  2013. NavigationServer::get_singleton()->set_active(false);
  2014. PhysicsServer::get_singleton()->set_active(false);
  2015. Physics2DServer::get_singleton()->set_active(false);
  2016. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  2017. {
  2018. int display_scale = EditorSettings::get_singleton()->get("interface/editor/display_scale");
  2019. switch (display_scale) {
  2020. case 0:
  2021. // Try applying a suitable display scale automatically.
  2022. editor_set_scale(EditorSettings::get_singleton()->get_auto_display_scale());
  2023. break;
  2024. case 1:
  2025. editor_set_scale(0.75);
  2026. break;
  2027. case 2:
  2028. editor_set_scale(1.0);
  2029. break;
  2030. case 3:
  2031. editor_set_scale(1.25);
  2032. break;
  2033. case 4:
  2034. editor_set_scale(1.5);
  2035. break;
  2036. case 5:
  2037. editor_set_scale(1.75);
  2038. break;
  2039. case 6:
  2040. editor_set_scale(2.0);
  2041. break;
  2042. default:
  2043. editor_set_scale(EditorSettings::get_singleton()->get("interface/editor/custom_display_scale"));
  2044. break;
  2045. }
  2046. }
  2047. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  2048. set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2049. set_theme(create_custom_theme());
  2050. gui_base = memnew(Control);
  2051. add_child(gui_base);
  2052. gui_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2053. Panel *panel = memnew(Panel);
  2054. gui_base->add_child(panel);
  2055. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2056. panel->add_style_override("panel", gui_base->get_stylebox("Background", "EditorStyles"));
  2057. VBoxContainer *vb = memnew(VBoxContainer);
  2058. panel->add_child(vb);
  2059. vb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 8 * EDSCALE);
  2060. String cp;
  2061. cp += 0xA9;
  2062. // TRANSLATORS: This refers to the application where users manage their Godot projects.
  2063. OS::get_singleton()->set_window_title(VERSION_NAME + String(" - ") + TTR("Project Manager", "Application"));
  2064. Control *center_box = memnew(Control);
  2065. center_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2066. vb->add_child(center_box);
  2067. tabs = memnew(TabContainer);
  2068. center_box->add_child(tabs);
  2069. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2070. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  2071. tabs->connect("tab_changed", this, "_on_tab_changed");
  2072. local_projects_hb = memnew(HBoxContainer);
  2073. local_projects_hb->set_name(TTR("Local Projects"));
  2074. tabs->add_child(local_projects_hb);
  2075. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  2076. local_projects_hb->add_child(search_tree_vb);
  2077. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2078. HBoxContainer *sort_filters = memnew(HBoxContainer);
  2079. project_filter = memnew(ProjectListFilter);
  2080. project_filter->add_search_box();
  2081. project_filter->connect("filter_changed", this, "_on_filter_option_changed");
  2082. project_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2083. sort_filters->add_child(project_filter);
  2084. Label *sort_label = memnew(Label);
  2085. sort_label->set_text(TTR("Sort:"));
  2086. sort_filters->add_child(sort_label);
  2087. Vector<String> sort_filter_titles;
  2088. sort_filter_titles.push_back(TTR("Name"));
  2089. sort_filter_titles.push_back(TTR("Path"));
  2090. sort_filter_titles.push_back(TTR("Last Modified"));
  2091. project_order_filter = memnew(ProjectListFilter);
  2092. project_order_filter->add_filter_option();
  2093. project_order_filter->_setup_filters(sort_filter_titles);
  2094. project_order_filter->set_filter_size(180);
  2095. sort_filters->add_child(project_order_filter);
  2096. project_order_filter->connect("filter_changed", this, "_on_order_option_changed");
  2097. const int projects_sorting_order = (int)EditorSettings::get_singleton()->get("project_manager/sorting_order");
  2098. project_order_filter->set_filter_option((ProjectListFilter::FilterOption)projects_sorting_order);
  2099. search_tree_vb->add_child(sort_filters);
  2100. loading_label = memnew(Label(TTR("Loading, please wait...")));
  2101. loading_label->add_font_override("font", get_font("bold", "EditorFonts"));
  2102. loading_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2103. sort_filters->add_child(loading_label);
  2104. // The loading label is shown later.
  2105. loading_label->hide();
  2106. PanelContainer *pc = memnew(PanelContainer);
  2107. pc->add_style_override("panel", gui_base->get_stylebox("bg", "Tree"));
  2108. search_tree_vb->add_child(pc);
  2109. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  2110. _project_list = memnew(ProjectList);
  2111. _project_list->connect(ProjectList::SIGNAL_SELECTION_CHANGED, this, "_update_project_buttons");
  2112. _project_list->connect(ProjectList::SIGNAL_PROJECT_ASK_OPEN, this, "_open_selected_projects_ask");
  2113. pc->add_child(_project_list);
  2114. _project_list->set_enable_h_scroll(false);
  2115. VBoxContainer *tree_vb = memnew(VBoxContainer);
  2116. tree_vb->set_custom_minimum_size(Size2(120, 120));
  2117. local_projects_hb->add_child(tree_vb);
  2118. Button *open = memnew(Button);
  2119. open->set_text(TTR("Edit"));
  2120. open->set_shortcut(ED_SHORTCUT("project_manager/edit_project", TTR("Edit Project"), KEY_MASK_CMD | KEY_E));
  2121. tree_vb->add_child(open);
  2122. open->connect("pressed", this, "_open_selected_projects_ask");
  2123. open_btn = open;
  2124. Button *run = memnew(Button);
  2125. run->set_text(TTR("Run"));
  2126. run->set_shortcut(ED_SHORTCUT("project_manager/run_project", TTR("Run Project"), KEY_MASK_CMD | KEY_R));
  2127. tree_vb->add_child(run);
  2128. run->connect("pressed", this, "_run_project");
  2129. run_btn = run;
  2130. tree_vb->add_child(memnew(HSeparator));
  2131. Button *scan = memnew(Button);
  2132. scan->set_text(TTR("Scan"));
  2133. scan->set_shortcut(ED_SHORTCUT("project_manager/scan_projects", TTR("Scan Projects"), KEY_MASK_CMD | KEY_S));
  2134. tree_vb->add_child(scan);
  2135. scan->connect("pressed", this, "_scan_projects");
  2136. tree_vb->add_child(memnew(HSeparator));
  2137. scan_dir = memnew(FileDialog);
  2138. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  2139. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  2140. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  2141. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  2142. gui_base->add_child(scan_dir);
  2143. scan_dir->connect("dir_selected", this, "_scan_begin");
  2144. Button *create = memnew(Button);
  2145. create->set_text(TTR("New Project"));
  2146. create->set_shortcut(ED_SHORTCUT("project_manager/new_project", TTR("New Project"), KEY_MASK_CMD | KEY_N));
  2147. tree_vb->add_child(create);
  2148. create->connect("pressed", this, "_new_project");
  2149. Button *import = memnew(Button);
  2150. import->set_text(TTR("Import"));
  2151. import->set_shortcut(ED_SHORTCUT("project_manager/import_project", TTR("Import Project"), KEY_MASK_CMD | KEY_I));
  2152. tree_vb->add_child(import);
  2153. import->connect("pressed", this, "_import_project");
  2154. Button *rename = memnew(Button);
  2155. rename->set_text(TTR("Rename"));
  2156. // The F2 shortcut isn't overridden with Enter on macOS as Enter is already used to edit a project.
  2157. rename->set_shortcut(ED_SHORTCUT("project_manager/rename_project", TTR("Rename Project"), KEY_F2));
  2158. tree_vb->add_child(rename);
  2159. rename->connect("pressed", this, "_rename_project");
  2160. rename_btn = rename;
  2161. Button *erase = memnew(Button);
  2162. erase->set_text(TTR("Remove"));
  2163. erase->set_shortcut(ED_SHORTCUT("project_manager/remove_project", TTR("Remove Project"), KEY_DELETE));
  2164. tree_vb->add_child(erase);
  2165. erase->connect("pressed", this, "_erase_project");
  2166. erase_btn = erase;
  2167. Button *erase_missing = memnew(Button);
  2168. erase_missing->set_text(TTR("Remove Missing"));
  2169. tree_vb->add_child(erase_missing);
  2170. erase_missing->connect("pressed", this, "_erase_missing_projects");
  2171. erase_missing_btn = erase_missing;
  2172. tree_vb->add_spacer();
  2173. about_btn = memnew(Button);
  2174. about_btn->set_text(TTR("About"));
  2175. about_btn->connect("pressed", this, "_show_about");
  2176. tree_vb->add_child(about_btn);
  2177. if (AssetLibraryEditorPlugin::is_available()) {
  2178. asset_library = memnew(EditorAssetLibrary(true));
  2179. asset_library->set_name(TTR("Asset Library Projects"));
  2180. tabs->add_child(asset_library);
  2181. asset_library->connect("install_asset", this, "_install_project");
  2182. } else {
  2183. print_verbose("Asset Library not available (due to using Web editor, or SSL support disabled).");
  2184. }
  2185. HBoxContainer *settings_hb = memnew(HBoxContainer);
  2186. settings_hb->set_alignment(BoxContainer::ALIGN_END);
  2187. settings_hb->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  2188. // A VBoxContainer that contains a dummy Control node to adjust the LinkButton's vertical position.
  2189. VBoxContainer *spacer_vb = memnew(VBoxContainer);
  2190. settings_hb->add_child(spacer_vb);
  2191. Control *v_spacer = memnew(Control);
  2192. spacer_vb->add_child(v_spacer);
  2193. version_btn = memnew(LinkButton);
  2194. String hash = String(VERSION_HASH);
  2195. if (hash.length() != 0) {
  2196. hash = " " + vformat("[%s]", hash.left(9));
  2197. }
  2198. version_btn->set_text("v" VERSION_FULL_BUILD + hash);
  2199. // Fade the version label to be less prominent, but still readable.
  2200. version_btn->set_self_modulate(Color(1, 1, 1, 0.6));
  2201. version_btn->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  2202. version_btn->set_tooltip(TTR("Click to copy."));
  2203. version_btn->connect("pressed", this, "_version_button_pressed");
  2204. spacer_vb->add_child(version_btn);
  2205. // Add a small horizontal spacer between the version and language buttons
  2206. // to distinguish them.
  2207. Control *h_spacer = memnew(Control);
  2208. settings_hb->add_child(h_spacer);
  2209. language_btn = memnew(OptionButton);
  2210. language_btn->set_flat(true);
  2211. language_btn->set_focus_mode(Control::FOCUS_NONE);
  2212. #ifdef ANDROID_ENABLED
  2213. // The language selection dropdown doesn't work on Android (as the setting isn't saved), see GH-60353.
  2214. // Also, the dropdown it spawns is very tall and can't be scrolled without a hardware mouse.
  2215. // Hiding the language selection dropdown also leaves more space for the version label to display.
  2216. language_btn->hide();
  2217. #endif
  2218. Vector<String> editor_languages;
  2219. List<PropertyInfo> editor_settings_properties;
  2220. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  2221. for (List<PropertyInfo>::Element *E = editor_settings_properties.front(); E; E = E->next()) {
  2222. PropertyInfo &pi = E->get();
  2223. if (pi.name == "interface/editor/editor_language") {
  2224. editor_languages = pi.hint_string.split(",");
  2225. }
  2226. }
  2227. String current_lang = EditorSettings::get_singleton()->get("interface/editor/editor_language");
  2228. for (int i = 0; i < editor_languages.size(); i++) {
  2229. String lang = editor_languages[i];
  2230. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang);
  2231. language_btn->add_item(vformat("[%s] %s", lang, lang_name), i);
  2232. language_btn->set_item_metadata(i, lang);
  2233. if (current_lang == lang) {
  2234. language_btn->select(i);
  2235. language_btn->set_text(lang);
  2236. }
  2237. }
  2238. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  2239. settings_hb->add_child(language_btn);
  2240. language_btn->connect("item_selected", this, "_language_selected");
  2241. center_box->add_child(settings_hb);
  2242. settings_hb->set_anchors_and_margins_preset(Control::PRESET_TOP_RIGHT);
  2243. //////////////////////////////////////////////////////////////
  2244. language_restart_ask = memnew(ConfirmationDialog);
  2245. language_restart_ask->get_ok()->set_text(TTR("Restart Now"));
  2246. language_restart_ask->get_ok()->connect("pressed", this, "_restart_confirm");
  2247. language_restart_ask->get_cancel()->set_text(TTR("Continue"));
  2248. gui_base->add_child(language_restart_ask);
  2249. erase_missing_ask = memnew(ConfirmationDialog);
  2250. erase_missing_ask->get_ok()->set_text(TTR("Remove All"));
  2251. erase_missing_ask->get_ok()->connect("pressed", this, "_erase_missing_projects_confirm");
  2252. gui_base->add_child(erase_missing_ask);
  2253. erase_ask = memnew(ConfirmationDialog);
  2254. erase_ask->get_ok()->set_text(TTR("Remove"));
  2255. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  2256. gui_base->add_child(erase_ask);
  2257. VBoxContainer *erase_ask_vb = memnew(VBoxContainer);
  2258. erase_ask->add_child(erase_ask_vb);
  2259. erase_ask_label = memnew(Label);
  2260. erase_ask_vb->add_child(erase_ask_label);
  2261. delete_project_contents = memnew(CheckBox);
  2262. delete_project_contents->set_text(TTR("Also delete project contents (no undo!)"));
  2263. erase_ask_vb->add_child(delete_project_contents);
  2264. multi_open_ask = memnew(ConfirmationDialog);
  2265. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  2266. multi_open_ask->get_ok()->connect("pressed", this, "_open_selected_projects");
  2267. gui_base->add_child(multi_open_ask);
  2268. multi_run_ask = memnew(ConfirmationDialog);
  2269. multi_run_ask->get_ok()->set_text(TTR("Run"));
  2270. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  2271. gui_base->add_child(multi_run_ask);
  2272. multi_scan_ask = memnew(ConfirmationDialog);
  2273. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  2274. gui_base->add_child(multi_scan_ask);
  2275. ask_update_settings = memnew(ConfirmationDialog);
  2276. ask_update_settings->get_ok()->connect("pressed", this, "_confirm_update_settings");
  2277. gui_base->add_child(ask_update_settings);
  2278. // Define a minimum window size to prevent UI elements from overlapping or being cut off.
  2279. OS::get_singleton()->set_min_window_size(Size2(520, 350) * EDSCALE);
  2280. // Resize the bootsplash window based on editor display scale.
  2281. const float scale_factor = MAX(1, EDSCALE);
  2282. if (scale_factor > 1.0 + CMP_EPSILON) {
  2283. const Vector2 window_size = OS::get_singleton()->get_window_size() * scale_factor;
  2284. const Vector2 screen_size = OS::get_singleton()->get_screen_size();
  2285. const Vector2 window_position = Vector2(screen_size.x - window_size.x, screen_size.y - window_size.y) * 0.5;
  2286. // Handle multi-monitor setups correctly by moving the window relative to the current screen.
  2287. OS::get_singleton()->set_window_position(OS::get_singleton()->get_screen_position() + window_position);
  2288. OS::get_singleton()->set_window_size(window_size);
  2289. }
  2290. OS::get_singleton()->set_low_processor_usage_mode(true);
  2291. npdialog = memnew(ProjectDialog);
  2292. gui_base->add_child(npdialog);
  2293. npdialog->connect("projects_updated", this, "_on_projects_updated");
  2294. npdialog->connect("project_created", this, "_on_project_created");
  2295. _load_recent_projects();
  2296. DirAccessRef dir_access = DirAccess::create(DirAccess::AccessType::ACCESS_FILESYSTEM);
  2297. String default_project_path = EditorSettings::get_singleton()->get("filesystem/directories/default_project_path");
  2298. if (!dir_access->dir_exists(default_project_path)) {
  2299. Error error = dir_access->make_dir_recursive(default_project_path);
  2300. if (error != OK) {
  2301. ERR_PRINT("Could not create default project directory at: " + default_project_path);
  2302. }
  2303. }
  2304. String autoscan_path = EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path");
  2305. if (autoscan_path != "") {
  2306. if (dir_access->dir_exists(autoscan_path)) {
  2307. _scan_begin(autoscan_path);
  2308. } else {
  2309. Error error = dir_access->make_dir_recursive(autoscan_path);
  2310. if (error != OK) {
  2311. ERR_PRINT("Could not create project autoscan directory at: " + autoscan_path);
  2312. }
  2313. }
  2314. }
  2315. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  2316. SceneTree::get_singleton()->connect("global_menu_action", this, "_global_menu_action");
  2317. run_error_diag = memnew(AcceptDialog);
  2318. gui_base->add_child(run_error_diag);
  2319. run_error_diag->set_title(TTR("Can't run project"));
  2320. dialog_error = memnew(AcceptDialog);
  2321. gui_base->add_child(dialog_error);
  2322. if (asset_library) {
  2323. open_templates = memnew(ConfirmationDialog);
  2324. open_templates->set_text(TTR("You currently don't have any projects.\nWould you like to explore official example projects in the Asset Library?"));
  2325. open_templates->get_ok()->set_text(TTR("Open Asset Library"));
  2326. open_templates->connect("confirmed", this, "_open_asset_library");
  2327. add_child(open_templates);
  2328. }
  2329. about = memnew(EditorAbout);
  2330. add_child(about);
  2331. }
  2332. ProjectManager::~ProjectManager() {
  2333. if (EditorSettings::get_singleton()) {
  2334. EditorSettings::destroy();
  2335. }
  2336. }
  2337. void ProjectListFilter::_setup_filters(Vector<String> options) {
  2338. filter_option->clear();
  2339. for (int i = 0; i < options.size(); i++) {
  2340. filter_option->add_item(options[i]);
  2341. }
  2342. }
  2343. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  2344. emit_signal("filter_changed");
  2345. }
  2346. String ProjectListFilter::get_search_term() {
  2347. return search_box->get_text().strip_edges();
  2348. }
  2349. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  2350. return _current_filter;
  2351. }
  2352. void ProjectListFilter::set_filter_option(FilterOption option) {
  2353. filter_option->select((int)option);
  2354. _filter_option_selected(0);
  2355. }
  2356. void ProjectListFilter::_filter_option_selected(int p_idx) {
  2357. FilterOption selected = (FilterOption)(filter_option->get_selected());
  2358. if (_current_filter != selected) {
  2359. _current_filter = selected;
  2360. emit_signal("filter_changed");
  2361. }
  2362. }
  2363. void ProjectListFilter::_notification(int p_what) {
  2364. if (p_what == NOTIFICATION_ENTER_TREE && has_search_box) {
  2365. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  2366. search_box->set_clear_button_enabled(true);
  2367. }
  2368. }
  2369. void ProjectListFilter::_bind_methods() {
  2370. ClassDB::bind_method(D_METHOD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  2371. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  2372. ADD_SIGNAL(MethodInfo("filter_changed"));
  2373. }
  2374. void ProjectListFilter::add_filter_option() {
  2375. filter_option = memnew(OptionButton);
  2376. filter_option->set_clip_text(true);
  2377. filter_option->connect("item_selected", this, "_filter_option_selected");
  2378. add_child(filter_option);
  2379. }
  2380. void ProjectListFilter::add_search_box() {
  2381. search_box = memnew(LineEdit);
  2382. search_box->set_placeholder(TTR("Filter projects"));
  2383. search_box->set_tooltip(TTR("This field filters projects by name and last path component.\nTo filter projects by name and full path, the query must contain at least one `/` character."));
  2384. search_box->connect("text_changed", this, "_search_text_changed");
  2385. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2386. add_child(search_box);
  2387. has_search_box = true;
  2388. }
  2389. LineEdit *ProjectListFilter::get_search_box() const {
  2390. return search_box;
  2391. }
  2392. void ProjectListFilter::set_filter_size(int h_size) {
  2393. filter_option->set_custom_minimum_size(Size2(h_size * EDSCALE, 10 * EDSCALE));
  2394. }
  2395. ProjectListFilter::ProjectListFilter() {
  2396. _current_filter = FILTER_NAME;
  2397. has_search_box = false;
  2398. }
  2399. void ProjectListFilter::clear() {
  2400. if (has_search_box) {
  2401. search_box->clear();
  2402. }
  2403. }