project_manager.cpp 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082
  1. /**************************************************************************/
  2. /* project_manager.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/project_settings.h"
  32. #include "core/io/config_file.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/io/stream_peer_tls.h"
  37. #include "core/io/zip_io.h"
  38. #include "core/os/keyboard.h"
  39. #include "core/os/os.h"
  40. #include "core/string/translation.h"
  41. #include "core/version.h"
  42. #include "editor/editor_file_dialog.h"
  43. #include "editor/editor_paths.h"
  44. #include "editor/editor_scale.h"
  45. #include "editor/editor_settings.h"
  46. #include "editor/editor_themes.h"
  47. #include "editor/editor_vcs_interface.h"
  48. #include "main/main.h"
  49. #include "scene/gui/center_container.h"
  50. #include "scene/gui/check_box.h"
  51. #include "scene/gui/line_edit.h"
  52. #include "scene/gui/margin_container.h"
  53. #include "scene/gui/panel_container.h"
  54. #include "scene/gui/separator.h"
  55. #include "scene/gui/texture_rect.h"
  56. #include "scene/main/window.h"
  57. #include "servers/display_server.h"
  58. #include "servers/navigation_server_3d.h"
  59. #include "servers/physics_server_2d.h"
  60. constexpr int GODOT4_CONFIG_VERSION = 5;
  61. class ProjectDialog : public ConfirmationDialog {
  62. GDCLASS(ProjectDialog, ConfirmationDialog);
  63. public:
  64. bool is_folder_empty = true;
  65. enum Mode {
  66. MODE_NEW,
  67. MODE_IMPORT,
  68. MODE_INSTALL,
  69. MODE_RENAME,
  70. };
  71. private:
  72. enum MessageType {
  73. MESSAGE_ERROR,
  74. MESSAGE_WARNING,
  75. MESSAGE_SUCCESS,
  76. };
  77. enum InputType {
  78. PROJECT_PATH,
  79. INSTALL_PATH,
  80. };
  81. Mode mode;
  82. Button *browse;
  83. Button *install_browse;
  84. Button *create_dir;
  85. Container *name_container;
  86. Container *path_container;
  87. Container *install_path_container;
  88. Container *renderer_container;
  89. Label *renderer_info;
  90. HBoxContainer *default_files_container;
  91. Ref<ButtonGroup> renderer_button_group;
  92. Label *msg;
  93. LineEdit *project_path;
  94. LineEdit *project_name;
  95. LineEdit *install_path;
  96. TextureRect *status_rect;
  97. TextureRect *install_status_rect;
  98. EditorFileDialog *fdialog;
  99. EditorFileDialog *fdialog_install;
  100. OptionButton *vcs_metadata_selection;
  101. String zip_path;
  102. String zip_title;
  103. AcceptDialog *dialog_error;
  104. String fav_dir;
  105. String created_folder_path;
  106. void set_message(const String &p_msg, MessageType p_type = MESSAGE_SUCCESS, InputType input_type = PROJECT_PATH) {
  107. msg->set_text(p_msg);
  108. Ref<Texture2D> current_path_icon = status_rect->get_texture();
  109. Ref<Texture2D> current_install_icon = install_status_rect->get_texture();
  110. Ref<Texture2D> new_icon;
  111. switch (p_type) {
  112. case MESSAGE_ERROR: {
  113. msg->add_theme_color_override("font_color", msg->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  114. msg->set_modulate(Color(1, 1, 1, 1));
  115. new_icon = msg->get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  116. } break;
  117. case MESSAGE_WARNING: {
  118. msg->add_theme_color_override("font_color", msg->get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  119. msg->set_modulate(Color(1, 1, 1, 1));
  120. new_icon = msg->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  121. } break;
  122. case MESSAGE_SUCCESS: {
  123. msg->set_modulate(Color(1, 1, 1, 0));
  124. new_icon = msg->get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons"));
  125. } break;
  126. }
  127. if (current_path_icon != new_icon && input_type == PROJECT_PATH) {
  128. status_rect->set_texture(new_icon);
  129. } else if (current_install_icon != new_icon && input_type == INSTALL_PATH) {
  130. install_status_rect->set_texture(new_icon);
  131. }
  132. Size2i window_size = get_size();
  133. Size2 contents_min_size = get_contents_minimum_size();
  134. if (window_size.x < contents_min_size.x || window_size.y < contents_min_size.y) {
  135. set_size(window_size.max(contents_min_size));
  136. }
  137. }
  138. String _test_path() {
  139. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  140. String valid_path, valid_install_path;
  141. if (d->change_dir(project_path->get_text()) == OK) {
  142. valid_path = project_path->get_text();
  143. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  144. valid_path = project_path->get_text().strip_edges();
  145. } else if (project_path->get_text().ends_with(".zip")) {
  146. if (d->file_exists(project_path->get_text())) {
  147. valid_path = project_path->get_text();
  148. }
  149. } else if (project_path->get_text().strip_edges().ends_with(".zip")) {
  150. if (d->file_exists(project_path->get_text().strip_edges())) {
  151. valid_path = project_path->get_text().strip_edges();
  152. }
  153. }
  154. if (valid_path.is_empty()) {
  155. set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR);
  156. get_ok_button()->set_disabled(true);
  157. return "";
  158. }
  159. if (mode == MODE_IMPORT && valid_path.ends_with(".zip")) {
  160. if (d->change_dir(install_path->get_text()) == OK) {
  161. valid_install_path = install_path->get_text();
  162. } else if (d->change_dir(install_path->get_text().strip_edges()) == OK) {
  163. valid_install_path = install_path->get_text().strip_edges();
  164. }
  165. if (valid_install_path.is_empty()) {
  166. set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR, INSTALL_PATH);
  167. get_ok_button()->set_disabled(true);
  168. return "";
  169. }
  170. }
  171. if (mode == MODE_IMPORT || mode == MODE_RENAME) {
  172. if (!valid_path.is_empty() && !d->file_exists("project.godot")) {
  173. if (valid_path.ends_with(".zip")) {
  174. Ref<FileAccess> io_fa;
  175. zlib_filefunc_def io = zipio_create_io(&io_fa);
  176. unzFile pkg = unzOpen2(valid_path.utf8().get_data(), &io);
  177. if (!pkg) {
  178. set_message(TTR("Error opening package file (it's not in ZIP format)."), MESSAGE_ERROR);
  179. get_ok_button()->set_disabled(true);
  180. unzClose(pkg);
  181. return "";
  182. }
  183. int ret = unzGoToFirstFile(pkg);
  184. while (ret == UNZ_OK) {
  185. unz_file_info info;
  186. char fname[16384];
  187. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  188. if (ret != UNZ_OK) {
  189. break;
  190. }
  191. if (String::utf8(fname).ends_with("project.godot")) {
  192. break;
  193. }
  194. ret = unzGoToNextFile(pkg);
  195. }
  196. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  197. set_message(TTR("Invalid \".zip\" project file; it doesn't contain a \"project.godot\" file."), MESSAGE_ERROR);
  198. get_ok_button()->set_disabled(true);
  199. unzClose(pkg);
  200. return "";
  201. }
  202. unzClose(pkg);
  203. // check if the specified install folder is empty, even though this is not an error, it is good to check here
  204. d->list_dir_begin();
  205. is_folder_empty = true;
  206. String n = d->get_next();
  207. while (!n.is_empty()) {
  208. if (!n.begins_with(".")) {
  209. // Allow `.`, `..` (reserved current/parent folder names)
  210. // and hidden files/folders to be present.
  211. // For instance, this lets users initialize a Git repository
  212. // and still be able to create a project in the directory afterwards.
  213. is_folder_empty = false;
  214. break;
  215. }
  216. n = d->get_next();
  217. }
  218. d->list_dir_end();
  219. if (!is_folder_empty) {
  220. set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH);
  221. get_ok_button()->set_disabled(true);
  222. return "";
  223. }
  224. } else {
  225. set_message(TTR("Please choose a \"project.godot\" or \".zip\" file."), MESSAGE_ERROR);
  226. install_path_container->hide();
  227. get_ok_button()->set_disabled(true);
  228. return "";
  229. }
  230. } else if (valid_path.ends_with("zip")) {
  231. set_message(TTR("This directory already contains a Godot project."), MESSAGE_ERROR, INSTALL_PATH);
  232. get_ok_button()->set_disabled(true);
  233. return "";
  234. }
  235. } else {
  236. // Check if the specified folder is empty, even though this is not an error, it is good to check here.
  237. d->list_dir_begin();
  238. is_folder_empty = true;
  239. String n = d->get_next();
  240. while (!n.is_empty()) {
  241. if (!n.begins_with(".")) {
  242. // Allow `.`, `..` (reserved current/parent folder names)
  243. // and hidden files/folders to be present.
  244. // For instance, this lets users initialize a Git repository
  245. // and still be able to create a project in the directory afterwards.
  246. is_folder_empty = false;
  247. break;
  248. }
  249. n = d->get_next();
  250. }
  251. d->list_dir_end();
  252. if (!is_folder_empty) {
  253. if (valid_path == OS::get_singleton()->get_environment("HOME") || valid_path == OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DOCUMENTS) || valid_path == OS::get_singleton()->get_executable_path().get_base_dir()) {
  254. set_message(TTR("You cannot save a project in the selected path. Please make a new folder or choose a new path."), MESSAGE_ERROR);
  255. get_ok_button()->set_disabled(true);
  256. return "";
  257. }
  258. set_message(TTR("The selected path is not empty. Choosing an empty folder is highly recommended."), MESSAGE_WARNING);
  259. get_ok_button()->set_disabled(false);
  260. return valid_path;
  261. }
  262. }
  263. set_message("");
  264. set_message("", MESSAGE_SUCCESS, INSTALL_PATH);
  265. get_ok_button()->set_disabled(false);
  266. return valid_path;
  267. }
  268. void _path_text_changed(const String &p_path) {
  269. String sp = _test_path();
  270. if (!sp.is_empty()) {
  271. // If the project name is empty or default, infer the project name from the selected folder name
  272. if (project_name->get_text().strip_edges().is_empty() || project_name->get_text().strip_edges() == TTR("New Game Project")) {
  273. sp = sp.replace("\\", "/");
  274. int lidx = sp.rfind("/");
  275. if (lidx != -1) {
  276. sp = sp.substr(lidx + 1, sp.length()).capitalize();
  277. }
  278. if (sp.is_empty() && mode == MODE_IMPORT) {
  279. sp = TTR("Imported Project");
  280. }
  281. project_name->set_text(sp);
  282. _text_changed(sp);
  283. }
  284. }
  285. if (!created_folder_path.is_empty() && created_folder_path != p_path) {
  286. _remove_created_folder();
  287. }
  288. }
  289. void _file_selected(const String &p_path) {
  290. String p = p_path;
  291. if (mode == MODE_IMPORT) {
  292. if (p.ends_with("project.godot")) {
  293. p = p.get_base_dir();
  294. install_path_container->hide();
  295. get_ok_button()->set_disabled(false);
  296. } else if (p.ends_with(".zip")) {
  297. install_path->set_text(p.get_base_dir());
  298. install_path_container->show();
  299. get_ok_button()->set_disabled(false);
  300. } else {
  301. set_message(TTR("Please choose a \"project.godot\" or \".zip\" file."), MESSAGE_ERROR);
  302. get_ok_button()->set_disabled(true);
  303. return;
  304. }
  305. }
  306. String sp = p.simplify_path();
  307. project_path->set_text(sp);
  308. _path_text_changed(sp);
  309. if (p.ends_with(".zip")) {
  310. install_path->call_deferred(SNAME("grab_focus"));
  311. } else {
  312. get_ok_button()->call_deferred(SNAME("grab_focus"));
  313. }
  314. }
  315. void _path_selected(const String &p_path) {
  316. String sp = p_path.simplify_path();
  317. project_path->set_text(sp);
  318. _path_text_changed(sp);
  319. get_ok_button()->call_deferred(SNAME("grab_focus"));
  320. }
  321. void _install_path_selected(const String &p_path) {
  322. String sp = p_path.simplify_path();
  323. install_path->set_text(sp);
  324. _path_text_changed(sp);
  325. get_ok_button()->call_deferred(SNAME("grab_focus"));
  326. }
  327. void _browse_path() {
  328. fdialog->set_current_dir(project_path->get_text());
  329. if (mode == MODE_IMPORT) {
  330. fdialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  331. fdialog->clear_filters();
  332. fdialog->add_filter("project.godot", vformat("%s %s", VERSION_NAME, TTR("Project")));
  333. fdialog->add_filter("*.zip", TTR("ZIP File"));
  334. } else {
  335. fdialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  336. }
  337. fdialog->popup_file_dialog();
  338. }
  339. void _browse_install_path() {
  340. fdialog_install->set_current_dir(install_path->get_text());
  341. fdialog_install->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  342. fdialog_install->popup_file_dialog();
  343. }
  344. void _create_folder() {
  345. const String project_name_no_edges = project_name->get_text().strip_edges();
  346. if (project_name_no_edges.is_empty() || !created_folder_path.is_empty() || project_name_no_edges.ends_with(".")) {
  347. set_message(TTR("Invalid project name."), MESSAGE_WARNING);
  348. return;
  349. }
  350. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  351. if (d->change_dir(project_path->get_text()) == OK) {
  352. if (!d->dir_exists(project_name_no_edges)) {
  353. if (d->make_dir(project_name_no_edges) == OK) {
  354. d->change_dir(project_name_no_edges);
  355. String dir_str = d->get_current_dir();
  356. project_path->set_text(dir_str);
  357. _path_text_changed(dir_str);
  358. created_folder_path = d->get_current_dir();
  359. create_dir->set_disabled(true);
  360. } else {
  361. dialog_error->set_text(TTR("Couldn't create folder."));
  362. dialog_error->popup_centered();
  363. }
  364. } else {
  365. dialog_error->set_text(TTR("There is already a folder in this path with the specified name."));
  366. dialog_error->popup_centered();
  367. }
  368. }
  369. }
  370. void _text_changed(const String &p_text) {
  371. if (mode != MODE_NEW) {
  372. return;
  373. }
  374. _test_path();
  375. if (p_text.strip_edges().is_empty()) {
  376. set_message(TTR("It would be a good idea to name your project."), MESSAGE_ERROR);
  377. }
  378. }
  379. void _nonempty_confirmation_ok_pressed() {
  380. is_folder_empty = true;
  381. ok_pressed();
  382. }
  383. void _renderer_selected() {
  384. String renderer_type = renderer_button_group->get_pressed_button()->get_meta(SNAME("rendering_method"));
  385. if (renderer_type == "forward_plus") {
  386. renderer_info->set_text(
  387. String::utf8("• ") + TTR("Supports desktop platforms only.") +
  388. String::utf8("\n• ") + TTR("Advanced 3D graphics available.") +
  389. String::utf8("\n• ") + TTR("Can scale to large complex scenes.") +
  390. String::utf8("\n• ") + TTR("Uses RenderingDevice backend.") +
  391. String::utf8("\n• ") + TTR("Slower rendering of simple scenes."));
  392. } else if (renderer_type == "mobile") {
  393. renderer_info->set_text(
  394. String::utf8("• ") + TTR("Supports desktop + mobile platforms.") +
  395. String::utf8("\n• ") + TTR("Less advanced 3D graphics.") +
  396. String::utf8("\n• ") + TTR("Less scalable for complex scenes.") +
  397. String::utf8("\n• ") + TTR("Uses RenderingDevice backend.") +
  398. String::utf8("\n• ") + TTR("Fast rendering of simple scenes."));
  399. } else if (renderer_type == "gl_compatibility") {
  400. renderer_info->set_text(
  401. String::utf8("• ") + TTR("Supports desktop, mobile + web platforms.") +
  402. String::utf8("\n• ") + TTR("Least advanced 3D graphics (currently work-in-progress).") +
  403. String::utf8("\n• ") + TTR("Intended for low-end/older devices.") +
  404. String::utf8("\n• ") + TTR("Uses OpenGL 3 backend (OpenGL 3.3/ES 3.0/WebGL2).") +
  405. String::utf8("\n• ") + TTR("Fastest rendering of simple scenes."));
  406. } else {
  407. WARN_PRINT("Unknown renderer type. Please report this as a bug on GitHub.");
  408. }
  409. }
  410. void ok_pressed() override {
  411. String dir = project_path->get_text();
  412. if (mode == MODE_RENAME) {
  413. String dir2 = _test_path();
  414. if (dir2.is_empty()) {
  415. set_message(TTR("Invalid project path (changed anything?)."), MESSAGE_ERROR);
  416. return;
  417. }
  418. // Load project.godot as ConfigFile to set the new name.
  419. ConfigFile cfg;
  420. String project_godot = dir2.path_join("project.godot");
  421. Error err = cfg.load(project_godot);
  422. if (err != OK) {
  423. set_message(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err), MESSAGE_ERROR);
  424. } else {
  425. cfg.set_value("application", "config/name", project_name->get_text().strip_edges());
  426. err = cfg.save(project_godot);
  427. if (err != OK) {
  428. set_message(vformat(TTR("Couldn't save project at '%s' (error %d)."), project_godot, err), MESSAGE_ERROR);
  429. }
  430. }
  431. hide();
  432. emit_signal(SNAME("projects_updated"));
  433. } else {
  434. if (mode == MODE_IMPORT) {
  435. if (project_path->get_text().ends_with(".zip")) {
  436. mode = MODE_INSTALL;
  437. ok_pressed();
  438. return;
  439. }
  440. } else {
  441. if (mode == MODE_NEW) {
  442. // Before we create a project, check that the target folder is empty.
  443. // If not, we need to ask the user if they're sure they want to do this.
  444. if (!is_folder_empty) {
  445. ConfirmationDialog *cd = memnew(ConfirmationDialog);
  446. cd->set_title(TTR("Warning: This folder is not empty"));
  447. cd->set_text(TTR("You are about to create a Godot project in a non-empty folder.\nThe entire contents of this folder will be imported as project resources!\n\nAre you sure you wish to continue?"));
  448. cd->get_ok_button()->connect("pressed", callable_mp(this, &ProjectDialog::_nonempty_confirmation_ok_pressed));
  449. get_parent()->add_child(cd);
  450. cd->popup_centered();
  451. cd->grab_focus();
  452. return;
  453. }
  454. PackedStringArray project_features = ProjectSettings::get_required_features();
  455. ProjectSettings::CustomMap initial_settings;
  456. // Be sure to change this code if/when renderers are changed.
  457. // Default values are "forward_plus" for the main setting, "mobile" for the mobile override,
  458. // and "gl_compatibility" for the web override.
  459. String renderer_type = renderer_button_group->get_pressed_button()->get_meta(SNAME("rendering_method"));
  460. initial_settings["rendering/renderer/rendering_method"] = renderer_type;
  461. EditorSettings::get_singleton()->set("project_manager/default_renderer", renderer_type);
  462. EditorSettings::get_singleton()->save();
  463. if (renderer_type == "forward_plus") {
  464. project_features.push_back("Forward Plus");
  465. } else if (renderer_type == "mobile") {
  466. project_features.push_back("Mobile");
  467. } else if (renderer_type == "gl_compatibility") {
  468. project_features.push_back("GL Compatibility");
  469. // Also change the default rendering method for the mobile override.
  470. initial_settings["rendering/renderer/rendering_method.mobile"] = "gl_compatibility";
  471. } else {
  472. WARN_PRINT("Unknown renderer type. Please report this as a bug on GitHub.");
  473. }
  474. project_features.sort();
  475. initial_settings["application/config/features"] = project_features;
  476. initial_settings["application/config/name"] = project_name->get_text().strip_edges();
  477. initial_settings["application/config/icon"] = "res://icon.svg";
  478. if (ProjectSettings::get_singleton()->save_custom(dir.path_join("project.godot"), initial_settings, Vector<String>(), false) != OK) {
  479. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  480. } else {
  481. // Store default project icon in SVG format.
  482. Error err;
  483. Ref<FileAccess> fa_icon = FileAccess::open(dir.path_join("icon.svg"), FileAccess::WRITE, &err);
  484. fa_icon->store_string(get_default_project_icon());
  485. if (err != OK) {
  486. set_message(TTR("Couldn't create icon.svg in project path."), MESSAGE_ERROR);
  487. }
  488. EditorVCSInterface::create_vcs_metadata_files(EditorVCSInterface::VCSMetadata(vcs_metadata_selection->get_selected()), dir);
  489. }
  490. } else if (mode == MODE_INSTALL) {
  491. if (project_path->get_text().ends_with(".zip")) {
  492. dir = install_path->get_text();
  493. zip_path = project_path->get_text();
  494. }
  495. Ref<FileAccess> io_fa;
  496. zlib_filefunc_def io = zipio_create_io(&io_fa);
  497. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  498. if (!pkg) {
  499. dialog_error->set_text(TTR("Error opening package file, not in ZIP format."));
  500. dialog_error->popup_centered();
  501. return;
  502. }
  503. // Find the zip_root
  504. String zip_root;
  505. int ret = unzGoToFirstFile(pkg);
  506. while (ret == UNZ_OK) {
  507. unz_file_info info;
  508. char fname[16384];
  509. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  510. String name = String::utf8(fname);
  511. if (name.ends_with("project.godot")) {
  512. zip_root = name.substr(0, name.rfind("project.godot"));
  513. break;
  514. }
  515. ret = unzGoToNextFile(pkg);
  516. }
  517. ret = unzGoToFirstFile(pkg);
  518. Vector<String> failed_files;
  519. while (ret == UNZ_OK) {
  520. //get filename
  521. unz_file_info info;
  522. char fname[16384];
  523. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  524. if (ret != UNZ_OK) {
  525. break;
  526. }
  527. String path = String::utf8(fname);
  528. if (path.is_empty() || path == zip_root || !zip_root.is_subsequence_of(path)) {
  529. //
  530. } else if (path.ends_with("/")) { // a dir
  531. path = path.substr(0, path.length() - 1);
  532. String rel_path = path.substr(zip_root.length());
  533. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  534. da->make_dir(dir.path_join(rel_path));
  535. } else {
  536. Vector<uint8_t> uncomp_data;
  537. uncomp_data.resize(info.uncompressed_size);
  538. String rel_path = path.substr(zip_root.length());
  539. //read
  540. unzOpenCurrentFile(pkg);
  541. ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
  542. ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", rel_path));
  543. unzCloseCurrentFile(pkg);
  544. Ref<FileAccess> f = FileAccess::open(dir.path_join(rel_path), FileAccess::WRITE);
  545. if (f.is_valid()) {
  546. f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
  547. } else {
  548. failed_files.push_back(rel_path);
  549. }
  550. }
  551. ret = unzGoToNextFile(pkg);
  552. }
  553. unzClose(pkg);
  554. if (failed_files.size()) {
  555. String err_msg = TTR("The following files failed extraction from package:") + "\n\n";
  556. for (int i = 0; i < failed_files.size(); i++) {
  557. if (i > 15) {
  558. err_msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  559. break;
  560. }
  561. err_msg += failed_files[i] + "\n";
  562. }
  563. dialog_error->set_text(err_msg);
  564. dialog_error->popup_centered();
  565. } else if (!project_path->get_text().ends_with(".zip")) {
  566. dialog_error->set_text(TTR("Package installed successfully!"));
  567. dialog_error->popup_centered();
  568. }
  569. }
  570. }
  571. dir = dir.replace("\\", "/");
  572. if (dir.ends_with("/")) {
  573. dir = dir.substr(0, dir.length() - 1);
  574. }
  575. hide();
  576. emit_signal(SNAME("project_created"), dir);
  577. }
  578. }
  579. void _remove_created_folder() {
  580. if (!created_folder_path.is_empty()) {
  581. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  582. d->remove(created_folder_path);
  583. create_dir->set_disabled(false);
  584. created_folder_path = "";
  585. }
  586. }
  587. void cancel_pressed() override {
  588. _remove_created_folder();
  589. project_path->clear();
  590. _path_text_changed("");
  591. project_name->clear();
  592. _text_changed("");
  593. if (status_rect->get_texture() == msg->get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"))) {
  594. msg->show();
  595. }
  596. if (install_status_rect->get_texture() == msg->get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"))) {
  597. msg->show();
  598. }
  599. }
  600. void _notification(int p_what) {
  601. switch (p_what) {
  602. case NOTIFICATION_WM_CLOSE_REQUEST: {
  603. _remove_created_folder();
  604. } break;
  605. }
  606. }
  607. protected:
  608. static void _bind_methods() {
  609. ADD_SIGNAL(MethodInfo("project_created"));
  610. ADD_SIGNAL(MethodInfo("projects_updated"));
  611. }
  612. public:
  613. void set_zip_path(const String &p_path) {
  614. zip_path = p_path;
  615. }
  616. void set_zip_title(const String &p_title) {
  617. zip_title = p_title;
  618. }
  619. void set_mode(Mode p_mode) {
  620. mode = p_mode;
  621. }
  622. void set_project_path(const String &p_path) {
  623. project_path->set_text(p_path);
  624. }
  625. void show_dialog() {
  626. if (mode == MODE_RENAME) {
  627. project_path->set_editable(false);
  628. browse->hide();
  629. install_browse->hide();
  630. set_title(TTR("Rename Project"));
  631. set_ok_button_text(TTR("Rename"));
  632. name_container->show();
  633. status_rect->hide();
  634. msg->hide();
  635. install_path_container->hide();
  636. install_status_rect->hide();
  637. renderer_container->hide();
  638. default_files_container->hide();
  639. get_ok_button()->set_disabled(false);
  640. // Fetch current name from project.godot to prefill the text input.
  641. ConfigFile cfg;
  642. String project_godot = project_path->get_text().path_join("project.godot");
  643. Error err = cfg.load(project_godot);
  644. if (err != OK) {
  645. set_message(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err), MESSAGE_ERROR);
  646. status_rect->show();
  647. msg->show();
  648. get_ok_button()->set_disabled(true);
  649. } else {
  650. String cur_name = cfg.get_value("application", "config/name", "");
  651. project_name->set_text(cur_name);
  652. _text_changed(cur_name);
  653. }
  654. project_name->call_deferred(SNAME("grab_focus"));
  655. create_dir->hide();
  656. } else {
  657. fav_dir = EDITOR_GET("filesystem/directories/default_project_path");
  658. if (!fav_dir.is_empty()) {
  659. project_path->set_text(fav_dir);
  660. fdialog->set_current_dir(fav_dir);
  661. } else {
  662. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  663. project_path->set_text(d->get_current_dir());
  664. fdialog->set_current_dir(d->get_current_dir());
  665. }
  666. String proj = TTR("New Game Project");
  667. project_name->set_text(proj);
  668. _text_changed(proj);
  669. project_path->set_editable(true);
  670. browse->set_disabled(false);
  671. browse->show();
  672. install_browse->set_disabled(false);
  673. install_browse->show();
  674. create_dir->show();
  675. status_rect->show();
  676. install_status_rect->show();
  677. msg->show();
  678. if (mode == MODE_IMPORT) {
  679. set_title(TTR("Import Existing Project"));
  680. set_ok_button_text(TTR("Import & Edit"));
  681. name_container->hide();
  682. install_path_container->hide();
  683. renderer_container->hide();
  684. default_files_container->hide();
  685. project_path->grab_focus();
  686. } else if (mode == MODE_NEW) {
  687. set_title(TTR("Create New Project"));
  688. set_ok_button_text(TTR("Create & Edit"));
  689. name_container->show();
  690. install_path_container->hide();
  691. renderer_container->show();
  692. default_files_container->show();
  693. project_name->call_deferred(SNAME("grab_focus"));
  694. project_name->call_deferred(SNAME("select_all"));
  695. } else if (mode == MODE_INSTALL) {
  696. set_title(TTR("Install Project:") + " " + zip_title);
  697. set_ok_button_text(TTR("Install & Edit"));
  698. project_name->set_text(zip_title);
  699. name_container->show();
  700. install_path_container->hide();
  701. renderer_container->hide();
  702. default_files_container->hide();
  703. project_path->grab_focus();
  704. }
  705. _test_path();
  706. }
  707. popup_centered(Size2(500, 0) * EDSCALE);
  708. }
  709. ProjectDialog() {
  710. VBoxContainer *vb = memnew(VBoxContainer);
  711. add_child(vb);
  712. name_container = memnew(VBoxContainer);
  713. vb->add_child(name_container);
  714. Label *l = memnew(Label);
  715. l->set_text(TTR("Project Name:"));
  716. name_container->add_child(l);
  717. HBoxContainer *pnhb = memnew(HBoxContainer);
  718. name_container->add_child(pnhb);
  719. project_name = memnew(LineEdit);
  720. project_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  721. pnhb->add_child(project_name);
  722. create_dir = memnew(Button);
  723. pnhb->add_child(create_dir);
  724. create_dir->set_text(TTR("Create Folder"));
  725. create_dir->connect("pressed", callable_mp(this, &ProjectDialog::_create_folder));
  726. path_container = memnew(VBoxContainer);
  727. vb->add_child(path_container);
  728. l = memnew(Label);
  729. l->set_text(TTR("Project Path:"));
  730. path_container->add_child(l);
  731. HBoxContainer *pphb = memnew(HBoxContainer);
  732. path_container->add_child(pphb);
  733. project_path = memnew(LineEdit);
  734. project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  735. project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  736. pphb->add_child(project_path);
  737. install_path_container = memnew(VBoxContainer);
  738. vb->add_child(install_path_container);
  739. l = memnew(Label);
  740. l->set_text(TTR("Project Installation Path:"));
  741. install_path_container->add_child(l);
  742. HBoxContainer *iphb = memnew(HBoxContainer);
  743. install_path_container->add_child(iphb);
  744. install_path = memnew(LineEdit);
  745. install_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  746. install_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  747. iphb->add_child(install_path);
  748. // status icon
  749. status_rect = memnew(TextureRect);
  750. status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  751. pphb->add_child(status_rect);
  752. browse = memnew(Button);
  753. browse->set_text(TTR("Browse"));
  754. browse->connect("pressed", callable_mp(this, &ProjectDialog::_browse_path));
  755. pphb->add_child(browse);
  756. // install status icon
  757. install_status_rect = memnew(TextureRect);
  758. install_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  759. iphb->add_child(install_status_rect);
  760. install_browse = memnew(Button);
  761. install_browse->set_text(TTR("Browse"));
  762. install_browse->connect("pressed", callable_mp(this, &ProjectDialog::_browse_install_path));
  763. iphb->add_child(install_browse);
  764. msg = memnew(Label);
  765. msg->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  766. vb->add_child(msg);
  767. // Renderer selection.
  768. renderer_container = memnew(VBoxContainer);
  769. vb->add_child(renderer_container);
  770. l = memnew(Label);
  771. l->set_text(TTR("Renderer:"));
  772. renderer_container->add_child(l);
  773. HBoxContainer *rshc = memnew(HBoxContainer);
  774. renderer_container->add_child(rshc);
  775. renderer_button_group.instantiate();
  776. // Left hand side, used for checkboxes to select renderer.
  777. Container *rvb = memnew(VBoxContainer);
  778. rshc->add_child(rvb);
  779. String default_renderer_type = "forward_plus";
  780. if (EditorSettings::get_singleton()->has_setting("project_manager/default_renderer")) {
  781. default_renderer_type = EditorSettings::get_singleton()->get_setting("project_manager/default_renderer");
  782. }
  783. Button *rs_button = memnew(CheckBox);
  784. rs_button->set_button_group(renderer_button_group);
  785. rs_button->set_text(TTR("Forward+"));
  786. #if defined(WEB_ENABLED)
  787. rs_button->set_disabled(true);
  788. #endif
  789. rs_button->set_meta(SNAME("rendering_method"), "forward_plus");
  790. rs_button->connect("pressed", callable_mp(this, &ProjectDialog::_renderer_selected));
  791. rvb->add_child(rs_button);
  792. if (default_renderer_type == "forward_plus") {
  793. rs_button->set_pressed(true);
  794. }
  795. rs_button = memnew(CheckBox);
  796. rs_button->set_button_group(renderer_button_group);
  797. rs_button->set_text(TTR("Mobile"));
  798. #if defined(WEB_ENABLED)
  799. rs_button->set_disabled(true);
  800. #endif
  801. rs_button->set_meta(SNAME("rendering_method"), "mobile");
  802. rs_button->connect("pressed", callable_mp(this, &ProjectDialog::_renderer_selected));
  803. rvb->add_child(rs_button);
  804. if (default_renderer_type == "mobile") {
  805. rs_button->set_pressed(true);
  806. }
  807. rs_button = memnew(CheckBox);
  808. rs_button->set_button_group(renderer_button_group);
  809. rs_button->set_text(TTR("Compatibility"));
  810. #if !defined(GLES3_ENABLED)
  811. rs_button->set_disabled(true);
  812. #endif
  813. rs_button->set_meta(SNAME("rendering_method"), "gl_compatibility");
  814. rs_button->connect("pressed", callable_mp(this, &ProjectDialog::_renderer_selected));
  815. rvb->add_child(rs_button);
  816. #if defined(GLES3_ENABLED)
  817. if (default_renderer_type == "gl_compatibility") {
  818. rs_button->set_pressed(true);
  819. }
  820. #endif
  821. rshc->add_child(memnew(VSeparator));
  822. // Right hand side, used for text explaining each choice.
  823. rvb = memnew(VBoxContainer);
  824. rvb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  825. rshc->add_child(rvb);
  826. renderer_info = memnew(Label);
  827. renderer_info->set_modulate(Color(1, 1, 1, 0.7));
  828. rvb->add_child(renderer_info);
  829. _renderer_selected();
  830. l = memnew(Label);
  831. l->set_text(TTR("The renderer can be changed later, but scenes may need to be adjusted."));
  832. // Add some extra spacing to separate it from the list above and the buttons below.
  833. l->set_custom_minimum_size(Size2(0, 40) * EDSCALE);
  834. l->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  835. l->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  836. l->set_modulate(Color(1, 1, 1, 0.7));
  837. renderer_container->add_child(l);
  838. default_files_container = memnew(HBoxContainer);
  839. vb->add_child(default_files_container);
  840. l = memnew(Label);
  841. l->set_text(TTR("Version Control Metadata:"));
  842. default_files_container->add_child(l);
  843. vcs_metadata_selection = memnew(OptionButton);
  844. vcs_metadata_selection->set_custom_minimum_size(Size2(100, 20));
  845. vcs_metadata_selection->add_item(TTR("None"), (int)EditorVCSInterface::VCSMetadata::NONE);
  846. vcs_metadata_selection->add_item(TTR("Git"), (int)EditorVCSInterface::VCSMetadata::GIT);
  847. vcs_metadata_selection->select((int)EditorVCSInterface::VCSMetadata::GIT);
  848. default_files_container->add_child(vcs_metadata_selection);
  849. Control *spacer = memnew(Control);
  850. spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  851. default_files_container->add_child(spacer);
  852. fdialog = memnew(EditorFileDialog);
  853. fdialog->set_previews_enabled(false); //Crucial, otherwise the engine crashes.
  854. fdialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  855. fdialog_install = memnew(EditorFileDialog);
  856. fdialog_install->set_previews_enabled(false); //Crucial, otherwise the engine crashes.
  857. fdialog_install->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  858. add_child(fdialog);
  859. add_child(fdialog_install);
  860. project_name->connect("text_changed", callable_mp(this, &ProjectDialog::_text_changed));
  861. project_path->connect("text_changed", callable_mp(this, &ProjectDialog::_path_text_changed));
  862. install_path->connect("text_changed", callable_mp(this, &ProjectDialog::_path_text_changed));
  863. fdialog->connect("dir_selected", callable_mp(this, &ProjectDialog::_path_selected));
  864. fdialog->connect("file_selected", callable_mp(this, &ProjectDialog::_file_selected));
  865. fdialog_install->connect("dir_selected", callable_mp(this, &ProjectDialog::_install_path_selected));
  866. fdialog_install->connect("file_selected", callable_mp(this, &ProjectDialog::_install_path_selected));
  867. set_hide_on_ok(false);
  868. mode = MODE_NEW;
  869. dialog_error = memnew(AcceptDialog);
  870. add_child(dialog_error);
  871. }
  872. };
  873. class ProjectListItemControl : public HBoxContainer {
  874. GDCLASS(ProjectListItemControl, HBoxContainer)
  875. public:
  876. TextureButton *favorite_button;
  877. TextureRect *icon;
  878. bool icon_needs_reload;
  879. bool hover;
  880. ProjectListItemControl() {
  881. favorite_button = nullptr;
  882. icon = nullptr;
  883. icon_needs_reload = true;
  884. hover = false;
  885. set_focus_mode(FocusMode::FOCUS_ALL);
  886. }
  887. void set_is_favorite(bool fav) {
  888. favorite_button->set_modulate(fav ? Color(1, 1, 1, 1) : Color(1, 1, 1, 0.2));
  889. }
  890. void _notification(int p_what) {
  891. switch (p_what) {
  892. case NOTIFICATION_MOUSE_ENTER: {
  893. hover = true;
  894. queue_redraw();
  895. } break;
  896. case NOTIFICATION_MOUSE_EXIT: {
  897. hover = false;
  898. queue_redraw();
  899. } break;
  900. case NOTIFICATION_DRAW: {
  901. if (hover) {
  902. draw_style_box(get_theme_stylebox(SNAME("hover"), SNAME("Tree")), Rect2(Point2(), get_size()));
  903. }
  904. } break;
  905. }
  906. }
  907. };
  908. class ProjectList : public ScrollContainer {
  909. GDCLASS(ProjectList, ScrollContainer)
  910. public:
  911. static const char *SIGNAL_SELECTION_CHANGED;
  912. static const char *SIGNAL_PROJECT_ASK_OPEN;
  913. enum MenuOptions {
  914. GLOBAL_NEW_WINDOW,
  915. GLOBAL_OPEN_PROJECT
  916. };
  917. // Can often be passed by copy
  918. struct Item {
  919. String project_name;
  920. String description;
  921. String path;
  922. String icon;
  923. String main_scene;
  924. PackedStringArray unsupported_features;
  925. uint64_t last_edited = 0;
  926. bool favorite = false;
  927. bool grayed = false;
  928. bool missing = false;
  929. int version = 0;
  930. ProjectListItemControl *control = nullptr;
  931. Item() {}
  932. Item(const String &p_name,
  933. const String &p_description,
  934. const String &p_path,
  935. const String &p_icon,
  936. const String &p_main_scene,
  937. const PackedStringArray &p_unsupported_features,
  938. uint64_t p_last_edited,
  939. bool p_favorite,
  940. bool p_grayed,
  941. bool p_missing,
  942. int p_version) {
  943. project_name = p_name;
  944. description = p_description;
  945. path = p_path;
  946. icon = p_icon;
  947. main_scene = p_main_scene;
  948. unsupported_features = p_unsupported_features;
  949. last_edited = p_last_edited;
  950. favorite = p_favorite;
  951. grayed = p_grayed;
  952. missing = p_missing;
  953. version = p_version;
  954. control = nullptr;
  955. }
  956. _FORCE_INLINE_ bool operator==(const Item &l) const {
  957. return path == l.path;
  958. }
  959. };
  960. bool project_opening_initiated;
  961. ProjectList();
  962. ~ProjectList();
  963. void _global_menu_new_window(const Variant &p_tag);
  964. void _global_menu_open_project(const Variant &p_tag);
  965. void update_dock_menu();
  966. void migrate_config();
  967. void load_projects();
  968. void set_search_term(String p_search_term);
  969. void set_order_option(int p_option);
  970. void sort_projects();
  971. int get_project_count() const;
  972. void select_project(int p_index);
  973. void select_first_visible_project();
  974. void erase_selected_projects(bool p_delete_project_contents);
  975. Vector<Item> get_selected_projects() const;
  976. const HashSet<String> &get_selected_project_keys() const;
  977. void ensure_project_visible(int p_index);
  978. int get_single_selected_index() const;
  979. bool is_any_project_missing() const;
  980. void erase_missing_projects();
  981. int refresh_project(const String &dir_path);
  982. void add_project(const String &dir_path, bool favorite);
  983. void save_config();
  984. void set_project_version(const String &p_project_path, int version);
  985. private:
  986. static void _bind_methods();
  987. void _notification(int p_what);
  988. void _panel_draw(Node *p_hb);
  989. void _panel_input(const Ref<InputEvent> &p_ev, Node *p_hb);
  990. void _favorite_pressed(Node *p_hb);
  991. void _show_project(const String &p_path);
  992. void select_range(int p_begin, int p_end);
  993. void toggle_select(int p_index);
  994. void create_project_item_control(int p_index);
  995. void remove_project(int p_index, bool p_update_settings);
  996. void update_icons_async();
  997. void load_project_icon(int p_index);
  998. static Item load_project_data(const String &p_property_key, bool p_favorite);
  999. String _search_term;
  1000. FilterOption _order_option;
  1001. HashSet<String> _selected_project_paths;
  1002. String _last_clicked; // Project key
  1003. VBoxContainer *_scroll_children;
  1004. int _icon_load_index;
  1005. Vector<Item> _projects;
  1006. ConfigFile _config;
  1007. String _config_path;
  1008. };
  1009. struct ProjectListComparator {
  1010. FilterOption order_option = FilterOption::EDIT_DATE;
  1011. // operator<
  1012. _FORCE_INLINE_ bool operator()(const ProjectList::Item &a, const ProjectList::Item &b) const {
  1013. if (a.favorite && !b.favorite) {
  1014. return true;
  1015. }
  1016. if (b.favorite && !a.favorite) {
  1017. return false;
  1018. }
  1019. switch (order_option) {
  1020. case PATH:
  1021. return a.path < b.path;
  1022. case EDIT_DATE:
  1023. return a.last_edited > b.last_edited;
  1024. default:
  1025. return a.project_name < b.project_name;
  1026. }
  1027. }
  1028. };
  1029. ProjectList::ProjectList() {
  1030. _order_option = FilterOption::EDIT_DATE;
  1031. _scroll_children = memnew(VBoxContainer);
  1032. _scroll_children->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1033. add_child(_scroll_children);
  1034. _icon_load_index = 0;
  1035. project_opening_initiated = false;
  1036. _config_path = EditorPaths::get_singleton()->get_data_dir().path_join("projects.cfg");
  1037. }
  1038. ProjectList::~ProjectList() {
  1039. }
  1040. void ProjectList::update_icons_async() {
  1041. _icon_load_index = 0;
  1042. set_process(true);
  1043. }
  1044. void ProjectList::_notification(int p_what) {
  1045. switch (p_what) {
  1046. case NOTIFICATION_PROCESS: {
  1047. // Load icons as a coroutine to speed up launch when you have hundreds of projects
  1048. if (_icon_load_index < _projects.size()) {
  1049. Item &item = _projects.write[_icon_load_index];
  1050. if (item.control->icon_needs_reload) {
  1051. load_project_icon(_icon_load_index);
  1052. }
  1053. _icon_load_index++;
  1054. } else {
  1055. set_process(false);
  1056. }
  1057. } break;
  1058. }
  1059. }
  1060. void ProjectList::load_project_icon(int p_index) {
  1061. Item &item = _projects.write[p_index];
  1062. Ref<Texture2D> default_icon = get_theme_icon(SNAME("DefaultProjectIcon"), SNAME("EditorIcons"));
  1063. Ref<Texture2D> icon;
  1064. if (!item.icon.is_empty()) {
  1065. Ref<Image> img;
  1066. img.instantiate();
  1067. Error err = img->load(item.icon.replace_first("res://", item.path + "/"));
  1068. if (err == OK) {
  1069. img->resize(default_icon->get_width(), default_icon->get_height(), Image::INTERPOLATE_LANCZOS);
  1070. icon = ImageTexture::create_from_image(img);
  1071. }
  1072. }
  1073. if (icon.is_null()) {
  1074. icon = default_icon;
  1075. }
  1076. // The default project icon is 128×128 to look crisp on hiDPI displays,
  1077. // but we want the actual displayed size to be 64×64 on loDPI displays.
  1078. item.control->icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1079. item.control->icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  1080. item.control->icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1081. item.control->icon->set_texture(icon);
  1082. item.control->icon_needs_reload = false;
  1083. }
  1084. // Load project data from p_property_key and return it in a ProjectList::Item. p_favorite is passed directly into the Item.
  1085. ProjectList::Item ProjectList::load_project_data(const String &p_path, bool p_favorite) {
  1086. String conf = p_path.path_join("project.godot");
  1087. bool grayed = false;
  1088. bool missing = false;
  1089. Ref<ConfigFile> cf = memnew(ConfigFile);
  1090. Error cf_err = cf->load(conf);
  1091. int config_version = 0;
  1092. String project_name = TTR("Unnamed Project");
  1093. if (cf_err == OK) {
  1094. String cf_project_name = static_cast<String>(cf->get_value("application", "config/name", ""));
  1095. if (!cf_project_name.is_empty()) {
  1096. project_name = cf_project_name.xml_unescape();
  1097. }
  1098. config_version = (int)cf->get_value("", "config_version", 0);
  1099. }
  1100. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1101. // Comes from an incompatible (more recent) Godot version, gray it out.
  1102. grayed = true;
  1103. }
  1104. const String description = cf->get_value("application", "config/description", "");
  1105. const String icon = cf->get_value("application", "config/icon", "");
  1106. const String main_scene = cf->get_value("application", "run/main_scene", "");
  1107. PackedStringArray project_features = cf->get_value("application", "config/features", PackedStringArray());
  1108. PackedStringArray unsupported_features = ProjectSettings::get_unsupported_features(project_features);
  1109. uint64_t last_edited = 0;
  1110. if (cf_err == OK) {
  1111. // The modification date marks the date the project was last edited.
  1112. // This is because the `project.godot` file will always be modified
  1113. // when editing a project (but not when running it).
  1114. last_edited = FileAccess::get_modified_time(conf);
  1115. String fscache = p_path.path_join(".fscache");
  1116. if (FileAccess::exists(fscache)) {
  1117. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  1118. if (cache_modified > last_edited) {
  1119. last_edited = cache_modified;
  1120. }
  1121. }
  1122. } else {
  1123. grayed = true;
  1124. missing = true;
  1125. print_line("Project is missing: " + conf);
  1126. }
  1127. return Item(project_name, description, p_path, icon, main_scene, unsupported_features, last_edited, p_favorite, grayed, missing, config_version);
  1128. }
  1129. void ProjectList::migrate_config() {
  1130. // Proposal #1637 moved the project list from editor settings to a separate config file
  1131. // If the new config file doesn't exist, populate it from EditorSettings
  1132. if (FileAccess::exists(_config_path)) {
  1133. return;
  1134. }
  1135. List<PropertyInfo> properties;
  1136. EditorSettings::get_singleton()->get_property_list(&properties);
  1137. for (const PropertyInfo &E : properties) {
  1138. // This is actually something like "projects/C:::Documents::Godot::Projects::MyGame"
  1139. String property_key = E.name;
  1140. if (!property_key.begins_with("projects/")) {
  1141. continue;
  1142. }
  1143. String path = EDITOR_GET(property_key);
  1144. print_line("Migrating legacy project '" + path + "'.");
  1145. String favoriteKey = "favorite_projects/" + property_key.get_slice("/", 1);
  1146. bool favorite = EditorSettings::get_singleton()->has_setting(favoriteKey);
  1147. add_project(path, favorite);
  1148. if (favorite) {
  1149. EditorSettings::get_singleton()->erase(favoriteKey);
  1150. }
  1151. EditorSettings::get_singleton()->erase(property_key);
  1152. }
  1153. save_config();
  1154. }
  1155. void ProjectList::load_projects() {
  1156. // This is a full, hard reload of the list. Don't call this unless really required, it's expensive.
  1157. // If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
  1158. // Clear whole list
  1159. for (int i = 0; i < _projects.size(); ++i) {
  1160. Item &project = _projects.write[i];
  1161. CRASH_COND(project.control == nullptr);
  1162. memdelete(project.control); // Why not queue_free()?
  1163. }
  1164. _projects.clear();
  1165. _last_clicked = "";
  1166. _selected_project_paths.clear();
  1167. List<String> sections;
  1168. _config.load(_config_path);
  1169. _config.get_sections(&sections);
  1170. for (const String &path : sections) {
  1171. bool favorite = _config.get_value(path, "favorite", false);
  1172. _projects.push_back(load_project_data(path, favorite));
  1173. }
  1174. // Create controls
  1175. for (int i = 0; i < _projects.size(); ++i) {
  1176. create_project_item_control(i);
  1177. }
  1178. sort_projects();
  1179. set_v_scroll(0);
  1180. update_icons_async();
  1181. update_dock_menu();
  1182. }
  1183. void ProjectList::update_dock_menu() {
  1184. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  1185. return;
  1186. }
  1187. DisplayServer::get_singleton()->global_menu_clear("_dock");
  1188. int favs_added = 0;
  1189. int total_added = 0;
  1190. for (int i = 0; i < _projects.size(); ++i) {
  1191. if (!_projects[i].grayed && !_projects[i].missing) {
  1192. if (_projects[i].favorite) {
  1193. favs_added++;
  1194. } else {
  1195. if (favs_added != 0) {
  1196. DisplayServer::get_singleton()->global_menu_add_separator("_dock");
  1197. }
  1198. favs_added = 0;
  1199. }
  1200. DisplayServer::get_singleton()->global_menu_add_item("_dock", _projects[i].project_name + " ( " + _projects[i].path + " )", callable_mp(this, &ProjectList::_global_menu_open_project), Callable(), i);
  1201. total_added++;
  1202. }
  1203. }
  1204. if (total_added != 0) {
  1205. DisplayServer::get_singleton()->global_menu_add_separator("_dock");
  1206. }
  1207. DisplayServer::get_singleton()->global_menu_add_item("_dock", TTR("New Window"), callable_mp(this, &ProjectList::_global_menu_new_window));
  1208. }
  1209. void ProjectList::_global_menu_new_window(const Variant &p_tag) {
  1210. List<String> args;
  1211. args.push_back("-p");
  1212. OS::get_singleton()->create_instance(args);
  1213. }
  1214. void ProjectList::_global_menu_open_project(const Variant &p_tag) {
  1215. int idx = (int)p_tag;
  1216. if (idx >= 0 && idx < _projects.size()) {
  1217. String conf = _projects[idx].path.path_join("project.godot");
  1218. List<String> args;
  1219. args.push_back(conf);
  1220. OS::get_singleton()->create_instance(args);
  1221. }
  1222. }
  1223. void ProjectList::create_project_item_control(int p_index) {
  1224. // Will be added last in the list, so make sure indexes match
  1225. ERR_FAIL_COND(p_index != _scroll_children->get_child_count());
  1226. Item &item = _projects.write[p_index];
  1227. ERR_FAIL_COND(item.control != nullptr); // Already created
  1228. Ref<Texture2D> favorite_icon = get_theme_icon(SNAME("Favorites"), SNAME("EditorIcons"));
  1229. Color font_color = get_theme_color(SNAME("font_color"), SNAME("Tree"));
  1230. ProjectListItemControl *hb = memnew(ProjectListItemControl);
  1231. hb->connect("draw", callable_mp(this, &ProjectList::_panel_draw).bind(hb));
  1232. hb->connect("gui_input", callable_mp(this, &ProjectList::_panel_input).bind(hb));
  1233. hb->add_theme_constant_override("separation", 10 * EDSCALE);
  1234. hb->set_tooltip_text(item.description);
  1235. VBoxContainer *favorite_box = memnew(VBoxContainer);
  1236. favorite_box->set_name("FavoriteBox");
  1237. TextureButton *favorite = memnew(TextureButton);
  1238. favorite->set_name("FavoriteButton");
  1239. favorite->set_texture_normal(favorite_icon);
  1240. // This makes the project's "hover" style display correctly when hovering the favorite icon.
  1241. favorite->set_mouse_filter(MOUSE_FILTER_PASS);
  1242. favorite->connect("pressed", callable_mp(this, &ProjectList::_favorite_pressed).bind(hb));
  1243. favorite_box->add_child(favorite);
  1244. favorite_box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  1245. hb->add_child(favorite_box);
  1246. hb->favorite_button = favorite;
  1247. hb->set_is_favorite(item.favorite);
  1248. TextureRect *tf = memnew(TextureRect);
  1249. // The project icon may not be loaded by the time the control is displayed,
  1250. // so use a loading placeholder.
  1251. tf->set_texture(get_theme_icon(SNAME("ProjectIconLoading"), SNAME("EditorIcons")));
  1252. tf->set_v_size_flags(SIZE_SHRINK_CENTER);
  1253. if (item.missing) {
  1254. tf->set_modulate(Color(1, 1, 1, 0.5));
  1255. }
  1256. hb->add_child(tf);
  1257. hb->icon = tf;
  1258. VBoxContainer *vb = memnew(VBoxContainer);
  1259. if (item.grayed) {
  1260. vb->set_modulate(Color(1, 1, 1, 0.5));
  1261. }
  1262. vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1263. hb->add_child(vb);
  1264. Control *ec = memnew(Control);
  1265. ec->set_custom_minimum_size(Size2(0, 1));
  1266. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  1267. vb->add_child(ec);
  1268. { // Top half, title and unsupported features labels.
  1269. HBoxContainer *title_hb = memnew(HBoxContainer);
  1270. vb->add_child(title_hb);
  1271. Label *title = memnew(Label(!item.missing ? item.project_name : TTR("Missing Project")));
  1272. title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1273. title->add_theme_font_override("font", get_theme_font(SNAME("title"), SNAME("EditorFonts")));
  1274. title->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("title_size"), SNAME("EditorFonts")));
  1275. title->add_theme_color_override("font_color", font_color);
  1276. title->set_clip_text(true);
  1277. title_hb->add_child(title);
  1278. String unsupported_features_str = String(", ").join(item.unsupported_features);
  1279. int length = unsupported_features_str.length();
  1280. if (length > 0) {
  1281. Label *unsupported_label = memnew(Label(unsupported_features_str));
  1282. unsupported_label->set_custom_minimum_size(Size2(length * 15, 10) * EDSCALE);
  1283. unsupported_label->add_theme_font_override("font", get_theme_font(SNAME("title"), SNAME("EditorFonts")));
  1284. unsupported_label->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  1285. unsupported_label->set_clip_text(true);
  1286. unsupported_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  1287. title_hb->add_child(unsupported_label);
  1288. Control *spacer = memnew(Control());
  1289. spacer->set_custom_minimum_size(Size2(10, 10));
  1290. title_hb->add_child(spacer);
  1291. }
  1292. }
  1293. { // Bottom half, containing the path and view folder button.
  1294. HBoxContainer *path_hb = memnew(HBoxContainer);
  1295. path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1296. vb->add_child(path_hb);
  1297. Button *show = memnew(Button);
  1298. // Display a folder icon if the project directory can be opened, or a "broken file" icon if it can't.
  1299. show->set_icon(get_theme_icon(!item.missing ? SNAME("Load") : SNAME("FileBroken"), SNAME("EditorIcons")));
  1300. show->set_flat(true);
  1301. if (!item.grayed) {
  1302. // Don't make the icon less prominent if the parent is already grayed out.
  1303. show->set_modulate(Color(1, 1, 1, 0.5));
  1304. }
  1305. path_hb->add_child(show);
  1306. if (!item.missing) {
  1307. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  1308. show->connect("pressed", callable_mp(this, &ProjectList::_show_project).bind(item.path));
  1309. show->set_tooltip_text(TTR("Show in File Manager"));
  1310. #else
  1311. // Opening the system file manager is not supported on the Android and web editors.
  1312. show->hide();
  1313. #endif
  1314. } else {
  1315. show->set_tooltip_text(TTR("Error: Project is missing on the filesystem."));
  1316. }
  1317. Label *fpath = memnew(Label(item.path));
  1318. fpath->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1319. path_hb->add_child(fpath);
  1320. fpath->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1321. fpath->set_modulate(Color(1, 1, 1, 0.5));
  1322. fpath->add_theme_color_override("font_color", font_color);
  1323. fpath->set_clip_text(true);
  1324. }
  1325. _scroll_children->add_child(hb);
  1326. item.control = hb;
  1327. }
  1328. void ProjectList::set_search_term(String p_search_term) {
  1329. _search_term = p_search_term;
  1330. }
  1331. void ProjectList::set_order_option(int p_option) {
  1332. FilterOption selected = (FilterOption)p_option;
  1333. EditorSettings::get_singleton()->set("project_manager/sorting_order", p_option);
  1334. EditorSettings::get_singleton()->save();
  1335. _order_option = selected;
  1336. sort_projects();
  1337. }
  1338. void ProjectList::sort_projects() {
  1339. SortArray<Item, ProjectListComparator> sorter;
  1340. sorter.compare.order_option = _order_option;
  1341. sorter.sort(_projects.ptrw(), _projects.size());
  1342. for (int i = 0; i < _projects.size(); ++i) {
  1343. Item &item = _projects.write[i];
  1344. bool item_visible = true;
  1345. if (!_search_term.is_empty()) {
  1346. String search_path;
  1347. if (_search_term.contains("/")) {
  1348. // Search path will match the whole path
  1349. search_path = item.path;
  1350. } else {
  1351. // Search path will only match the last path component to make searching more strict
  1352. search_path = item.path.get_file();
  1353. }
  1354. // When searching, display projects whose name or path contain the search term
  1355. item_visible = item.project_name.findn(_search_term) != -1 || search_path.findn(_search_term) != -1;
  1356. }
  1357. item.control->set_visible(item_visible);
  1358. }
  1359. for (int i = 0; i < _projects.size(); ++i) {
  1360. Item &item = _projects.write[i];
  1361. item.control->get_parent()->move_child(item.control, i);
  1362. }
  1363. // Rewind the coroutine because order of projects changed
  1364. update_icons_async();
  1365. update_dock_menu();
  1366. }
  1367. const HashSet<String> &ProjectList::get_selected_project_keys() const {
  1368. // Faster if that's all you need
  1369. return _selected_project_paths;
  1370. }
  1371. Vector<ProjectList::Item> ProjectList::get_selected_projects() const {
  1372. Vector<Item> items;
  1373. if (_selected_project_paths.size() == 0) {
  1374. return items;
  1375. }
  1376. items.resize(_selected_project_paths.size());
  1377. int j = 0;
  1378. for (int i = 0; i < _projects.size(); ++i) {
  1379. const Item &item = _projects[i];
  1380. if (_selected_project_paths.has(item.path)) {
  1381. items.write[j++] = item;
  1382. }
  1383. }
  1384. ERR_FAIL_COND_V(j != items.size(), items);
  1385. return items;
  1386. }
  1387. void ProjectList::ensure_project_visible(int p_index) {
  1388. const Item &item = _projects[p_index];
  1389. ensure_control_visible(item.control);
  1390. }
  1391. int ProjectList::get_single_selected_index() const {
  1392. if (_selected_project_paths.size() == 0) {
  1393. // Default selection
  1394. return 0;
  1395. }
  1396. String key;
  1397. if (_selected_project_paths.size() == 1) {
  1398. // Only one selected
  1399. key = *_selected_project_paths.begin();
  1400. } else {
  1401. // Multiple selected, consider the last clicked one as "main"
  1402. key = _last_clicked;
  1403. }
  1404. for (int i = 0; i < _projects.size(); ++i) {
  1405. if (_projects[i].path == key) {
  1406. return i;
  1407. }
  1408. }
  1409. return 0;
  1410. }
  1411. void ProjectList::remove_project(int p_index, bool p_update_config) {
  1412. const Item item = _projects[p_index]; // Take a copy
  1413. _selected_project_paths.erase(item.path);
  1414. if (_last_clicked == item.path) {
  1415. _last_clicked = "";
  1416. }
  1417. memdelete(item.control);
  1418. _projects.remove_at(p_index);
  1419. if (p_update_config) {
  1420. _config.erase_section(item.path);
  1421. // Not actually saving the file, in case you are doing more changes to settings
  1422. }
  1423. update_dock_menu();
  1424. }
  1425. bool ProjectList::is_any_project_missing() const {
  1426. for (int i = 0; i < _projects.size(); ++i) {
  1427. if (_projects[i].missing) {
  1428. return true;
  1429. }
  1430. }
  1431. return false;
  1432. }
  1433. void ProjectList::erase_missing_projects() {
  1434. if (_projects.is_empty()) {
  1435. return;
  1436. }
  1437. int deleted_count = 0;
  1438. int remaining_count = 0;
  1439. for (int i = 0; i < _projects.size(); ++i) {
  1440. const Item &item = _projects[i];
  1441. if (item.missing) {
  1442. remove_project(i, true);
  1443. --i;
  1444. ++deleted_count;
  1445. } else {
  1446. ++remaining_count;
  1447. }
  1448. }
  1449. print_line("Removed " + itos(deleted_count) + " projects from the list, remaining " + itos(remaining_count) + " projects");
  1450. save_config();
  1451. }
  1452. int ProjectList::refresh_project(const String &dir_path) {
  1453. // Reloads information about a specific project.
  1454. // If it wasn't loaded and should be in the list, it is added (i.e new project).
  1455. // If it isn't in the list anymore, it is removed.
  1456. // If it is in the list but doesn't exist anymore, it is marked as missing.
  1457. bool should_be_in_list = _config.has_section(dir_path);
  1458. bool is_favourite = _config.get_value(dir_path, "favorite", false);
  1459. bool was_selected = _selected_project_paths.has(dir_path);
  1460. // Remove item in any case
  1461. for (int i = 0; i < _projects.size(); ++i) {
  1462. const Item &existing_item = _projects[i];
  1463. if (existing_item.path == dir_path) {
  1464. remove_project(i, false);
  1465. break;
  1466. }
  1467. }
  1468. int index = -1;
  1469. if (should_be_in_list) {
  1470. // Recreate it with updated info
  1471. Item item = load_project_data(dir_path, is_favourite);
  1472. _projects.push_back(item);
  1473. create_project_item_control(_projects.size() - 1);
  1474. sort_projects();
  1475. for (int i = 0; i < _projects.size(); ++i) {
  1476. if (_projects[i].path == dir_path) {
  1477. if (was_selected) {
  1478. select_project(i);
  1479. ensure_project_visible(i);
  1480. }
  1481. load_project_icon(i);
  1482. index = i;
  1483. break;
  1484. }
  1485. }
  1486. }
  1487. return index;
  1488. }
  1489. void ProjectList::add_project(const String &dir_path, bool favorite) {
  1490. if (!_config.has_section(dir_path)) {
  1491. _config.set_value(dir_path, "favorite", favorite);
  1492. }
  1493. }
  1494. void ProjectList::save_config() {
  1495. _config.save(_config_path);
  1496. }
  1497. void ProjectList::set_project_version(const String &p_project_path, int p_version) {
  1498. for (ProjectList::Item &E : _projects) {
  1499. if (E.path == p_project_path) {
  1500. E.version = p_version;
  1501. break;
  1502. }
  1503. }
  1504. }
  1505. int ProjectList::get_project_count() const {
  1506. return _projects.size();
  1507. }
  1508. void ProjectList::select_project(int p_index) {
  1509. Vector<Item> previous_selected_items = get_selected_projects();
  1510. _selected_project_paths.clear();
  1511. for (int i = 0; i < previous_selected_items.size(); ++i) {
  1512. previous_selected_items[i].control->queue_redraw();
  1513. }
  1514. toggle_select(p_index);
  1515. }
  1516. void ProjectList::select_first_visible_project() {
  1517. bool found = false;
  1518. for (int i = 0; i < _projects.size(); i++) {
  1519. if (_projects[i].control->is_visible()) {
  1520. select_project(i);
  1521. found = true;
  1522. break;
  1523. }
  1524. }
  1525. if (!found) {
  1526. // Deselect all projects if there are no visible projects in the list.
  1527. _selected_project_paths.clear();
  1528. }
  1529. }
  1530. inline void sort(int &a, int &b) {
  1531. if (a > b) {
  1532. int temp = a;
  1533. a = b;
  1534. b = temp;
  1535. }
  1536. }
  1537. void ProjectList::select_range(int p_begin, int p_end) {
  1538. sort(p_begin, p_end);
  1539. select_project(p_begin);
  1540. for (int i = p_begin + 1; i <= p_end; ++i) {
  1541. toggle_select(i);
  1542. }
  1543. }
  1544. void ProjectList::toggle_select(int p_index) {
  1545. Item &item = _projects.write[p_index];
  1546. if (_selected_project_paths.has(item.path)) {
  1547. _selected_project_paths.erase(item.path);
  1548. } else {
  1549. _selected_project_paths.insert(item.path);
  1550. }
  1551. item.control->queue_redraw();
  1552. }
  1553. void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
  1554. if (_selected_project_paths.size() == 0) {
  1555. return;
  1556. }
  1557. for (int i = 0; i < _projects.size(); ++i) {
  1558. Item &item = _projects.write[i];
  1559. if (_selected_project_paths.has(item.path) && item.control->is_visible()) {
  1560. _config.erase_section(item.path);
  1561. // Comment out for now until we have a better warning system to
  1562. // ensure users delete their project only.
  1563. //if (p_delete_project_contents) {
  1564. // OS::get_singleton()->move_to_trash(item.path);
  1565. //}
  1566. memdelete(item.control);
  1567. _projects.remove_at(i);
  1568. --i;
  1569. }
  1570. }
  1571. save_config();
  1572. _selected_project_paths.clear();
  1573. _last_clicked = "";
  1574. update_dock_menu();
  1575. }
  1576. // Draws selected project highlight
  1577. void ProjectList::_panel_draw(Node *p_hb) {
  1578. Control *hb = Object::cast_to<Control>(p_hb);
  1579. if (is_layout_rtl() && get_v_scroll_bar()->is_visible_in_tree()) {
  1580. hb->draw_line(Point2(get_v_scroll_bar()->get_minimum_size().x, hb->get_size().y + 1), Point2(hb->get_size().x, hb->get_size().y + 1), get_theme_color(SNAME("guide_color"), SNAME("Tree")));
  1581. } else {
  1582. hb->draw_line(Point2(0, hb->get_size().y + 1), Point2(hb->get_size().x, hb->get_size().y + 1), get_theme_color(SNAME("guide_color"), SNAME("Tree")));
  1583. }
  1584. String key = _projects[p_hb->get_index()].path;
  1585. if (_selected_project_paths.has(key)) {
  1586. hb->draw_style_box(get_theme_stylebox(SNAME("selected"), SNAME("Tree")), Rect2(Point2(), hb->get_size()));
  1587. }
  1588. }
  1589. // Input for each item in the list
  1590. void ProjectList::_panel_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  1591. Ref<InputEventMouseButton> mb = p_ev;
  1592. int clicked_index = p_hb->get_index();
  1593. const Item &clicked_project = _projects[clicked_index];
  1594. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1595. if (mb->is_shift_pressed() && _selected_project_paths.size() > 0 && !_last_clicked.is_empty() && clicked_project.path != _last_clicked) {
  1596. int anchor_index = -1;
  1597. for (int i = 0; i < _projects.size(); ++i) {
  1598. const Item &p = _projects[i];
  1599. if (p.path == _last_clicked) {
  1600. anchor_index = p.control->get_index();
  1601. break;
  1602. }
  1603. }
  1604. CRASH_COND(anchor_index == -1);
  1605. select_range(anchor_index, clicked_index);
  1606. } else if (mb->is_ctrl_pressed()) {
  1607. toggle_select(clicked_index);
  1608. } else {
  1609. _last_clicked = clicked_project.path;
  1610. select_project(clicked_index);
  1611. }
  1612. emit_signal(SNAME(SIGNAL_SELECTION_CHANGED));
  1613. // Do not allow opening a project more than once using a single project manager instance.
  1614. // Opening the same project in several editor instances at once can lead to various issues.
  1615. if (!mb->is_ctrl_pressed() && mb->is_double_click() && !project_opening_initiated) {
  1616. emit_signal(SNAME(SIGNAL_PROJECT_ASK_OPEN));
  1617. }
  1618. }
  1619. }
  1620. void ProjectList::_favorite_pressed(Node *p_hb) {
  1621. ProjectListItemControl *control = Object::cast_to<ProjectListItemControl>(p_hb);
  1622. int index = control->get_index();
  1623. Item item = _projects.write[index]; // Take copy
  1624. item.favorite = !item.favorite;
  1625. _config.set_value(item.path, "favorite", item.favorite);
  1626. save_config();
  1627. _projects.write[index] = item;
  1628. control->set_is_favorite(item.favorite);
  1629. sort_projects();
  1630. if (item.favorite) {
  1631. for (int i = 0; i < _projects.size(); ++i) {
  1632. if (_projects[i].path == item.path) {
  1633. ensure_project_visible(i);
  1634. break;
  1635. }
  1636. }
  1637. }
  1638. update_dock_menu();
  1639. }
  1640. void ProjectList::_show_project(const String &p_path) {
  1641. OS::get_singleton()->shell_open(String("file://") + p_path);
  1642. }
  1643. const char *ProjectList::SIGNAL_SELECTION_CHANGED = "selection_changed";
  1644. const char *ProjectList::SIGNAL_PROJECT_ASK_OPEN = "project_ask_open";
  1645. void ProjectList::_bind_methods() {
  1646. ADD_SIGNAL(MethodInfo(SIGNAL_SELECTION_CHANGED));
  1647. ADD_SIGNAL(MethodInfo(SIGNAL_PROJECT_ASK_OPEN));
  1648. }
  1649. ProjectManager *ProjectManager::singleton = nullptr;
  1650. void ProjectManager::_notification(int p_what) {
  1651. switch (p_what) {
  1652. case NOTIFICATION_TRANSLATION_CHANGED:
  1653. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  1654. settings_hb->set_anchors_and_offsets_preset(Control::PRESET_TOP_RIGHT);
  1655. queue_redraw();
  1656. } break;
  1657. case NOTIFICATION_ENTER_TREE: {
  1658. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  1659. search_box->set_clear_button_enabled(true);
  1660. create_btn->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1661. import_btn->set_icon(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")));
  1662. scan_btn->set_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  1663. open_btn->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  1664. run_btn->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  1665. rename_btn->set_icon(get_theme_icon(SNAME("Rename"), SNAME("EditorIcons")));
  1666. erase_btn->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  1667. erase_missing_btn->set_icon(get_theme_icon(SNAME("Clear"), SNAME("EditorIcons")));
  1668. Engine::get_singleton()->set_editor_hint(false);
  1669. } break;
  1670. case NOTIFICATION_RESIZED: {
  1671. if (open_templates && open_templates->is_visible()) {
  1672. open_templates->popup_centered();
  1673. }
  1674. if (asset_library) {
  1675. real_t size = get_size().x / EDSCALE;
  1676. // Adjust names of tabs to fit the new size.
  1677. if (size < 650) {
  1678. local_projects_hb->set_name(TTR("Local"));
  1679. asset_library->set_name(TTR("Asset Library"));
  1680. } else {
  1681. local_projects_hb->set_name(TTR("Local Projects"));
  1682. asset_library->set_name(TTR("Asset Library Projects"));
  1683. }
  1684. }
  1685. } break;
  1686. case NOTIFICATION_READY: {
  1687. int default_sorting = (int)EDITOR_GET("project_manager/sorting_order");
  1688. filter_option->select(default_sorting);
  1689. _project_list->set_order_option(default_sorting);
  1690. #ifndef ANDROID_ENABLED
  1691. if (_project_list->get_project_count() >= 1) {
  1692. // Focus on the search box immediately to allow the user
  1693. // to search without having to reach for their mouse
  1694. search_box->grab_focus();
  1695. }
  1696. #endif
  1697. if (asset_library) {
  1698. // Removes extra border margins.
  1699. asset_library->add_theme_style_override("panel", memnew(StyleBoxEmpty));
  1700. // Suggest browsing asset library to get templates/demos.
  1701. if (open_templates && _project_list->get_project_count() == 0) {
  1702. open_templates->popup_centered();
  1703. }
  1704. }
  1705. } break;
  1706. case NOTIFICATION_VISIBILITY_CHANGED: {
  1707. set_process_shortcut_input(is_visible_in_tree());
  1708. } break;
  1709. case NOTIFICATION_WM_CLOSE_REQUEST: {
  1710. _dim_window();
  1711. } break;
  1712. case NOTIFICATION_WM_ABOUT: {
  1713. _show_about();
  1714. } break;
  1715. }
  1716. }
  1717. Ref<Texture2D> ProjectManager::_file_dialog_get_icon(const String &p_path) {
  1718. if (p_path.get_extension().to_lower() == "godot") {
  1719. return singleton->icon_type_cache["GodotMonochrome"];
  1720. }
  1721. return singleton->icon_type_cache["Object"];
  1722. }
  1723. Ref<Texture2D> ProjectManager::_file_dialog_get_thumbnail(const String &p_path) {
  1724. if (p_path.get_extension().to_lower() == "godot") {
  1725. return singleton->icon_type_cache["GodotFile"];
  1726. }
  1727. return Ref<Texture2D>();
  1728. }
  1729. void ProjectManager::_build_icon_type_cache(Ref<Theme> p_theme) {
  1730. List<StringName> tl;
  1731. p_theme->get_icon_list(SNAME("EditorIcons"), &tl);
  1732. for (List<StringName>::Element *E = tl.front(); E; E = E->next()) {
  1733. icon_type_cache[E->get()] = p_theme->get_icon(E->get(), SNAME("EditorIcons"));
  1734. }
  1735. }
  1736. void ProjectManager::_dim_window() {
  1737. // This method must be called before calling `get_tree()->quit()`.
  1738. // Otherwise, its effect won't be visible
  1739. // Dim the project manager window while it's quitting to make it clearer that it's busy.
  1740. // No transition is applied, as the effect needs to be visible immediately
  1741. float c = 0.5f;
  1742. Color dim_color = Color(c, c, c);
  1743. set_modulate(dim_color);
  1744. }
  1745. void ProjectManager::_update_project_buttons() {
  1746. Vector<ProjectList::Item> selected_projects = _project_list->get_selected_projects();
  1747. bool empty_selection = selected_projects.is_empty();
  1748. bool is_missing_project_selected = false;
  1749. for (int i = 0; i < selected_projects.size(); ++i) {
  1750. if (selected_projects[i].missing) {
  1751. is_missing_project_selected = true;
  1752. break;
  1753. }
  1754. }
  1755. erase_btn->set_disabled(empty_selection);
  1756. open_btn->set_disabled(empty_selection || is_missing_project_selected);
  1757. rename_btn->set_disabled(empty_selection || is_missing_project_selected);
  1758. run_btn->set_disabled(empty_selection || is_missing_project_selected);
  1759. erase_missing_btn->set_disabled(!_project_list->is_any_project_missing());
  1760. }
  1761. void ProjectManager::shortcut_input(const Ref<InputEvent> &p_ev) {
  1762. ERR_FAIL_COND(p_ev.is_null());
  1763. Ref<InputEventKey> k = p_ev;
  1764. if (k.is_valid()) {
  1765. if (!k->is_pressed()) {
  1766. return;
  1767. }
  1768. // Pressing Command + Q quits the Project Manager
  1769. // This is handled by the platform implementation on macOS,
  1770. // so only define the shortcut on other platforms
  1771. #ifndef MACOS_ENABLED
  1772. if (k->get_keycode_with_modifiers() == (KeyModifierMask::META | Key::Q)) {
  1773. _dim_window();
  1774. get_tree()->quit();
  1775. }
  1776. #endif
  1777. if (tabs->get_current_tab() != 0) {
  1778. return;
  1779. }
  1780. bool keycode_handled = true;
  1781. switch (k->get_keycode()) {
  1782. case Key::ENTER: {
  1783. _open_selected_projects_ask();
  1784. } break;
  1785. case Key::HOME: {
  1786. if (_project_list->get_project_count() > 0) {
  1787. _project_list->select_project(0);
  1788. _update_project_buttons();
  1789. }
  1790. } break;
  1791. case Key::END: {
  1792. if (_project_list->get_project_count() > 0) {
  1793. _project_list->select_project(_project_list->get_project_count() - 1);
  1794. _update_project_buttons();
  1795. }
  1796. } break;
  1797. case Key::UP: {
  1798. if (k->is_shift_pressed()) {
  1799. break;
  1800. }
  1801. int index = _project_list->get_single_selected_index();
  1802. if (index > 0) {
  1803. _project_list->select_project(index - 1);
  1804. _project_list->ensure_project_visible(index - 1);
  1805. _update_project_buttons();
  1806. }
  1807. break;
  1808. }
  1809. case Key::DOWN: {
  1810. if (k->is_shift_pressed()) {
  1811. break;
  1812. }
  1813. int index = _project_list->get_single_selected_index();
  1814. if (index + 1 < _project_list->get_project_count()) {
  1815. _project_list->select_project(index + 1);
  1816. _project_list->ensure_project_visible(index + 1);
  1817. _update_project_buttons();
  1818. }
  1819. } break;
  1820. case Key::F: {
  1821. if (k->is_command_or_control_pressed()) {
  1822. this->search_box->grab_focus();
  1823. } else {
  1824. keycode_handled = false;
  1825. }
  1826. } break;
  1827. default: {
  1828. keycode_handled = false;
  1829. } break;
  1830. }
  1831. if (keycode_handled) {
  1832. accept_event();
  1833. }
  1834. }
  1835. }
  1836. void ProjectManager::_load_recent_projects() {
  1837. _project_list->set_search_term(search_box->get_text().strip_edges());
  1838. _project_list->load_projects();
  1839. _update_project_buttons();
  1840. tabs->set_current_tab(0);
  1841. }
  1842. void ProjectManager::_on_projects_updated() {
  1843. Vector<ProjectList::Item> selected_projects = _project_list->get_selected_projects();
  1844. int index = 0;
  1845. for (int i = 0; i < selected_projects.size(); ++i) {
  1846. index = _project_list->refresh_project(selected_projects[i].path);
  1847. }
  1848. if (index != -1) {
  1849. _project_list->ensure_project_visible(index);
  1850. }
  1851. _project_list->update_dock_menu();
  1852. }
  1853. void ProjectManager::_on_project_created(const String &dir) {
  1854. _project_list->add_project(dir, false);
  1855. _project_list->save_config();
  1856. search_box->clear();
  1857. int i = _project_list->refresh_project(dir);
  1858. _project_list->select_project(i);
  1859. _project_list->ensure_project_visible(i);
  1860. _open_selected_projects_ask();
  1861. _project_list->update_dock_menu();
  1862. }
  1863. void ProjectManager::_confirm_update_settings() {
  1864. _open_selected_projects();
  1865. }
  1866. void ProjectManager::_open_selected_projects() {
  1867. // Show loading text to tell the user that the project manager is busy loading.
  1868. // This is especially important for the Web project manager.
  1869. loading_label->show();
  1870. const HashSet<String> &selected_list = _project_list->get_selected_project_keys();
  1871. for (const String &path : selected_list) {
  1872. String conf = path.path_join("project.godot");
  1873. if (!FileAccess::exists(conf)) {
  1874. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1875. dialog_error->popup_centered();
  1876. return;
  1877. }
  1878. print_line("Editing project: " + path);
  1879. List<String> args;
  1880. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_TOOL)) {
  1881. args.push_back(a);
  1882. }
  1883. args.push_back("--path");
  1884. args.push_back(path);
  1885. args.push_back("--editor");
  1886. Error err = OS::get_singleton()->create_instance(args);
  1887. ERR_FAIL_COND(err);
  1888. }
  1889. _project_list->project_opening_initiated = true;
  1890. _dim_window();
  1891. get_tree()->quit();
  1892. }
  1893. void ProjectManager::_open_selected_projects_ask() {
  1894. const HashSet<String> &selected_list = _project_list->get_selected_project_keys();
  1895. if (selected_list.size() < 1) {
  1896. return;
  1897. }
  1898. const Size2i popup_min_size = Size2i(600.0 * EDSCALE, 400.0 * EDSCALE);
  1899. if (selected_list.size() > 1) {
  1900. multi_open_ask->set_text(vformat(TTR("You requested to open %d projects in parallel. Do you confirm?\nNote that usual checks for engine version compatibility will be bypassed."), selected_list.size()));
  1901. multi_open_ask->popup_centered(popup_min_size);
  1902. return;
  1903. }
  1904. ProjectList::Item project = _project_list->get_selected_projects()[0];
  1905. if (project.missing) {
  1906. return;
  1907. }
  1908. // Update the project settings or don't open.
  1909. const int config_version = project.version;
  1910. PackedStringArray unsupported_features = project.unsupported_features;
  1911. Label *ask_update_label = ask_update_settings->get_label();
  1912. ask_update_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT); // Reset in case of previous center align.
  1913. full_convert_button->hide();
  1914. ask_update_settings->get_ok_button()->set_text("OK");
  1915. // Check if the config_version property was empty or 0.
  1916. if (config_version == 0) {
  1917. ask_update_settings->set_text(vformat(TTR("The selected project \"%s\" does not specify its supported Godot version in its configuration file (\"project.godot\").\n\nProject path: %s\n\nIf you proceed with opening it, it will be converted to Godot's current configuration file format.\n\nWarning: You won't be able to open the project with previous versions of the engine anymore."), project.project_name, project.path));
  1918. ask_update_settings->popup_centered(popup_min_size);
  1919. return;
  1920. }
  1921. // Check if we need to convert project settings from an earlier engine version.
  1922. if (config_version < ProjectSettings::CONFIG_VERSION) {
  1923. if (config_version == GODOT4_CONFIG_VERSION - 1 && ProjectSettings::CONFIG_VERSION == GODOT4_CONFIG_VERSION) { // Conversion from Godot 3 to 4.
  1924. full_convert_button->show();
  1925. ask_update_settings->set_text(vformat(TTR("The selected project \"%s\" was generated by Godot 3.x, and needs to be converted for Godot 4.x.\n\nProject path: %s\n\nYou have three options:\n- Convert only the configuration file (\"project.godot\"). Use this to open the project without attempting to convert its scenes, resources and scripts.\n- Convert the entire project including its scenes, resources and scripts (recommended if you are upgrading).\n- Do nothing and go back.\n\nWarning: If you select a conversion option, you won't be able to open the project with previous versions of the engine anymore."), project.project_name, project.path));
  1926. ask_update_settings->get_ok_button()->set_text(TTR("Convert project.godot Only"));
  1927. } else {
  1928. ask_update_settings->set_text(vformat(TTR("The selected project \"%s\" was generated by an older engine version, and needs to be converted for this version.\n\nProject path: %s\n\nDo you want to convert it?\n\nWarning: You won't be able to open the project with previous versions of the engine anymore."), project.project_name, project.path));
  1929. ask_update_settings->get_ok_button()->set_text(TTR("Convert project.godot"));
  1930. }
  1931. ask_update_settings->popup_centered(popup_min_size);
  1932. ask_update_settings->get_cancel_button()->grab_focus(); // To prevent accidents.
  1933. return;
  1934. }
  1935. // Check if the file was generated by a newer, incompatible engine version.
  1936. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1937. dialog_error->set_text(vformat(TTR("Can't open project \"%s\" at the following path:\n\n%s\n\nThe project settings were created by a newer engine version, whose settings are not compatible with this version."), project.project_name, project.path));
  1938. dialog_error->popup_centered(popup_min_size);
  1939. return;
  1940. }
  1941. // Check if the project is using features not supported by this build of Godot.
  1942. if (!unsupported_features.is_empty()) {
  1943. String warning_message = "";
  1944. for (int i = 0; i < unsupported_features.size(); i++) {
  1945. String feature = unsupported_features[i];
  1946. if (feature == "Double Precision") {
  1947. warning_message += TTR("Warning: This project uses double precision floats, but this version of\nGodot uses single precision floats. Opening this project may cause data loss.\n\n");
  1948. unsupported_features.remove_at(i);
  1949. i--;
  1950. } else if (feature == "C#") {
  1951. warning_message += TTR("Warning: This project uses C#, but this build of Godot does not have\nthe Mono module. If you proceed you will not be able to use any C# scripts.\n\n");
  1952. unsupported_features.remove_at(i);
  1953. i--;
  1954. } else if (feature.substr(0, 3).is_numeric()) {
  1955. warning_message += vformat(TTR("Warning: This project was built in Godot %s.\nOpening will upgrade or downgrade the project to Godot %s.\n\n"), Variant(feature), Variant(VERSION_BRANCH));
  1956. unsupported_features.remove_at(i);
  1957. i--;
  1958. }
  1959. }
  1960. if (!unsupported_features.is_empty()) {
  1961. String unsupported_features_str = String(", ").join(unsupported_features);
  1962. warning_message += vformat(TTR("Warning: This project uses the following features not supported by this build of Godot:\n\n%s\n\n"), unsupported_features_str);
  1963. }
  1964. warning_message += TTR("Open anyway? Project will be modified.");
  1965. ask_update_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1966. ask_update_settings->set_text(warning_message);
  1967. ask_update_settings->popup_centered(popup_min_size);
  1968. return;
  1969. }
  1970. // Open if the project is up-to-date.
  1971. _open_selected_projects();
  1972. }
  1973. void ProjectManager::_full_convert_button_pressed() {
  1974. ask_update_settings->hide();
  1975. ask_full_convert_dialog->popup_centered(Size2i(600.0 * EDSCALE, 400.0 * EDSCALE));
  1976. ask_full_convert_dialog->get_cancel_button()->grab_focus();
  1977. }
  1978. void ProjectManager::_perform_full_project_conversion() {
  1979. Vector<ProjectList::Item> selected_list = _project_list->get_selected_projects();
  1980. if (selected_list.is_empty()) {
  1981. return;
  1982. }
  1983. const String &path = selected_list[0].path;
  1984. print_line("Converting project: " + path);
  1985. Ref<ConfigFile> cf;
  1986. cf.instantiate();
  1987. cf->load(path.path_join("project.godot"));
  1988. cf->set_value("", "config_version", GODOT4_CONFIG_VERSION);
  1989. cf->save(path.path_join("project.godot"));
  1990. _project_list->set_project_version(path, GODOT4_CONFIG_VERSION);
  1991. List<String> args;
  1992. args.push_back("--path");
  1993. args.push_back(path);
  1994. args.push_back("--convert-3to4");
  1995. Error err = OS::get_singleton()->create_instance(args);
  1996. ERR_FAIL_COND(err);
  1997. }
  1998. void ProjectManager::_run_project_confirm() {
  1999. Vector<ProjectList::Item> selected_list = _project_list->get_selected_projects();
  2000. for (int i = 0; i < selected_list.size(); ++i) {
  2001. const String &selected_main = selected_list[i].main_scene;
  2002. if (selected_main.is_empty()) {
  2003. 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."));
  2004. run_error_diag->popup_centered();
  2005. continue;
  2006. }
  2007. const String &path = selected_list[i].path;
  2008. // `.substr(6)` on `ProjectSettings::get_singleton()->get_imported_files_path()` strips away the leading "res://".
  2009. if (!DirAccess::exists(path.path_join(ProjectSettings::get_singleton()->get_imported_files_path().substr(6)))) {
  2010. run_error_diag->set_text(TTR("Can't run project: Assets need to be imported.\nPlease edit the project to trigger the initial import."));
  2011. run_error_diag->popup_centered();
  2012. continue;
  2013. }
  2014. print_line("Running project: " + path);
  2015. List<String> args;
  2016. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_PROJECT)) {
  2017. args.push_back(a);
  2018. }
  2019. args.push_back("--path");
  2020. args.push_back(path);
  2021. Error err = OS::get_singleton()->create_instance(args);
  2022. ERR_FAIL_COND(err);
  2023. }
  2024. }
  2025. void ProjectManager::_run_project() {
  2026. const HashSet<String> &selected_list = _project_list->get_selected_project_keys();
  2027. if (selected_list.size() < 1) {
  2028. return;
  2029. }
  2030. if (selected_list.size() > 1) {
  2031. multi_run_ask->set_text(vformat(TTR("Are you sure to run %d projects at once?"), selected_list.size()));
  2032. multi_run_ask->popup_centered();
  2033. } else {
  2034. _run_project_confirm();
  2035. }
  2036. }
  2037. void ProjectManager::_scan_dir(const String &path) {
  2038. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2039. Error error = da->change_dir(path);
  2040. ERR_FAIL_COND_MSG(error != OK, "Could not scan directory at: " + path);
  2041. da->list_dir_begin();
  2042. String n = da->get_next();
  2043. while (!n.is_empty()) {
  2044. if (da->current_is_dir() && !n.begins_with(".")) {
  2045. _scan_dir(da->get_current_dir().path_join(n));
  2046. } else if (n == "project.godot") {
  2047. _project_list->add_project(da->get_current_dir(), false);
  2048. }
  2049. n = da->get_next();
  2050. }
  2051. da->list_dir_end();
  2052. }
  2053. void ProjectManager::_scan_begin(const String &p_base) {
  2054. print_line("Scanning projects at: " + p_base);
  2055. _scan_dir(p_base);
  2056. _project_list->save_config();
  2057. _load_recent_projects();
  2058. }
  2059. void ProjectManager::_scan_projects() {
  2060. scan_dir->popup_file_dialog();
  2061. }
  2062. void ProjectManager::_new_project() {
  2063. npdialog->set_mode(ProjectDialog::MODE_NEW);
  2064. npdialog->show_dialog();
  2065. }
  2066. void ProjectManager::_import_project() {
  2067. npdialog->set_mode(ProjectDialog::MODE_IMPORT);
  2068. npdialog->show_dialog();
  2069. }
  2070. void ProjectManager::_rename_project() {
  2071. const HashSet<String> &selected_list = _project_list->get_selected_project_keys();
  2072. if (selected_list.size() == 0) {
  2073. return;
  2074. }
  2075. for (const String &E : selected_list) {
  2076. npdialog->set_project_path(E);
  2077. npdialog->set_mode(ProjectDialog::MODE_RENAME);
  2078. npdialog->show_dialog();
  2079. }
  2080. }
  2081. void ProjectManager::_erase_project_confirm() {
  2082. _project_list->erase_selected_projects(false);
  2083. _update_project_buttons();
  2084. }
  2085. void ProjectManager::_erase_missing_projects_confirm() {
  2086. _project_list->erase_missing_projects();
  2087. _update_project_buttons();
  2088. }
  2089. void ProjectManager::_erase_project() {
  2090. const HashSet<String> &selected_list = _project_list->get_selected_project_keys();
  2091. if (selected_list.size() == 0) {
  2092. return;
  2093. }
  2094. String confirm_message;
  2095. if (selected_list.size() >= 2) {
  2096. confirm_message = vformat(TTR("Remove %d projects from the list?"), selected_list.size());
  2097. } else {
  2098. confirm_message = TTR("Remove this project from the list?");
  2099. }
  2100. erase_ask_label->set_text(confirm_message);
  2101. //delete_project_contents->set_pressed(false);
  2102. erase_ask->popup_centered();
  2103. }
  2104. void ProjectManager::_erase_missing_projects() {
  2105. erase_missing_ask->set_text(TTR("Remove all missing projects from the list?\nThe project folders' contents won't be modified."));
  2106. erase_missing_ask->popup_centered();
  2107. }
  2108. void ProjectManager::_show_about() {
  2109. about->popup_centered(Size2(780, 500) * EDSCALE);
  2110. }
  2111. void ProjectManager::_language_selected(int p_id) {
  2112. String lang = language_btn->get_item_metadata(p_id);
  2113. EditorSettings::get_singleton()->set("interface/editor/editor_language", lang);
  2114. language_restart_ask->set_text(TTR("Language changed.\nThe interface will update after restarting the editor or project manager."));
  2115. language_restart_ask->popup_centered();
  2116. }
  2117. void ProjectManager::_restart_confirm() {
  2118. List<String> args = OS::get_singleton()->get_cmdline_args();
  2119. Error err = OS::get_singleton()->create_instance(args);
  2120. ERR_FAIL_COND(err);
  2121. _dim_window();
  2122. get_tree()->quit();
  2123. }
  2124. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  2125. npdialog->set_mode(ProjectDialog::MODE_INSTALL);
  2126. npdialog->set_zip_path(p_zip_path);
  2127. npdialog->set_zip_title(p_title);
  2128. npdialog->show_dialog();
  2129. }
  2130. void ProjectManager::_files_dropped(PackedStringArray p_files) {
  2131. if (p_files.size() == 1 && p_files[0].ends_with(".zip")) {
  2132. const String file = p_files[0].get_file();
  2133. _install_project(p_files[0], file.substr(0, file.length() - 4).capitalize());
  2134. return;
  2135. }
  2136. HashSet<String> folders_set;
  2137. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2138. for (int i = 0; i < p_files.size(); i++) {
  2139. String file = p_files[i];
  2140. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  2141. }
  2142. if (folders_set.size() > 0) {
  2143. PackedStringArray folders;
  2144. for (const String &E : folders_set) {
  2145. folders.push_back(E);
  2146. }
  2147. bool confirm = true;
  2148. if (folders.size() == 1) {
  2149. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2150. if (dir->change_dir(folders[0]) == OK) {
  2151. dir->list_dir_begin();
  2152. String file = dir->get_next();
  2153. while (confirm && !file.is_empty()) {
  2154. if (!dir->current_is_dir() && file.ends_with("project.godot")) {
  2155. confirm = false;
  2156. }
  2157. file = dir->get_next();
  2158. }
  2159. dir->list_dir_end();
  2160. }
  2161. }
  2162. if (confirm) {
  2163. multi_scan_ask->get_ok_button()->disconnect("pressed", callable_mp(this, &ProjectManager::_scan_multiple_folders));
  2164. multi_scan_ask->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_scan_multiple_folders).bind(folders));
  2165. multi_scan_ask->set_text(
  2166. vformat(TTR("Are you sure to scan %s folders for existing Godot projects?\nThis could take a while."), folders.size()));
  2167. multi_scan_ask->popup_centered();
  2168. } else {
  2169. _scan_multiple_folders(folders);
  2170. }
  2171. }
  2172. }
  2173. void ProjectManager::_scan_multiple_folders(PackedStringArray p_files) {
  2174. for (int i = 0; i < p_files.size(); i++) {
  2175. _scan_begin(p_files.get(i));
  2176. }
  2177. }
  2178. void ProjectManager::_on_order_option_changed(int p_idx) {
  2179. if (is_inside_tree()) {
  2180. _project_list->set_order_option(p_idx);
  2181. }
  2182. }
  2183. void ProjectManager::_on_tab_changed(int p_tab) {
  2184. #ifndef ANDROID_ENABLED
  2185. if (p_tab == 0) { // Projects
  2186. // Automatically grab focus when the user moves from the Templates tab
  2187. // back to the Projects tab.
  2188. search_box->grab_focus();
  2189. }
  2190. // The Templates tab's search field is focused on display in the asset
  2191. // library editor plugin code.
  2192. #endif
  2193. }
  2194. void ProjectManager::_on_search_term_changed(const String &p_term) {
  2195. _project_list->set_search_term(p_term);
  2196. _project_list->sort_projects();
  2197. // Select the first visible project in the list.
  2198. // This makes it possible to open a project without ever touching the mouse,
  2199. // as the search field is automatically focused on startup.
  2200. _project_list->select_first_visible_project();
  2201. _update_project_buttons();
  2202. }
  2203. void ProjectManager::_bind_methods() {
  2204. ClassDB::bind_method("_update_project_buttons", &ProjectManager::_update_project_buttons);
  2205. ClassDB::bind_method("_version_button_pressed", &ProjectManager::_version_button_pressed);
  2206. }
  2207. void ProjectManager::_open_asset_library() {
  2208. asset_library->disable_community_support();
  2209. tabs->set_current_tab(1);
  2210. }
  2211. void ProjectManager::_version_button_pressed() {
  2212. DisplayServer::get_singleton()->clipboard_set(version_btn->get_text());
  2213. }
  2214. ProjectManager::ProjectManager() {
  2215. singleton = this;
  2216. // load settings
  2217. if (!EditorSettings::get_singleton()) {
  2218. EditorSettings::create();
  2219. }
  2220. // Turn off some servers we aren't going to be using in the Project Manager.
  2221. NavigationServer3D::get_singleton()->set_active(false);
  2222. PhysicsServer3D::get_singleton()->set_active(false);
  2223. PhysicsServer2D::get_singleton()->set_active(false);
  2224. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  2225. {
  2226. int display_scale = EDITOR_GET("interface/editor/display_scale");
  2227. switch (display_scale) {
  2228. case 0:
  2229. // Try applying a suitable display scale automatically.
  2230. editor_set_scale(EditorSettings::get_singleton()->get_auto_display_scale());
  2231. break;
  2232. case 1:
  2233. editor_set_scale(0.75);
  2234. break;
  2235. case 2:
  2236. editor_set_scale(1.0);
  2237. break;
  2238. case 3:
  2239. editor_set_scale(1.25);
  2240. break;
  2241. case 4:
  2242. editor_set_scale(1.5);
  2243. break;
  2244. case 5:
  2245. editor_set_scale(1.75);
  2246. break;
  2247. case 6:
  2248. editor_set_scale(2.0);
  2249. break;
  2250. default:
  2251. editor_set_scale(EDITOR_GET("interface/editor/custom_display_scale"));
  2252. break;
  2253. }
  2254. EditorFileDialog::get_icon_func = &ProjectManager::_file_dialog_get_icon;
  2255. EditorFileDialog::get_thumbnail_func = &ProjectManager::_file_dialog_get_thumbnail;
  2256. }
  2257. // TRANSLATORS: This refers to the application where users manage their Godot projects.
  2258. DisplayServer::get_singleton()->window_set_title(VERSION_NAME + String(" - ") + TTR("Project Manager", "Application"));
  2259. EditorFileDialog::set_default_show_hidden_files(EDITOR_GET("filesystem/file_dialog/show_hidden_files"));
  2260. int swap_cancel_ok = EDITOR_GET("interface/editor/accept_dialog_cancel_ok_buttons");
  2261. if (swap_cancel_ok != 0) { // 0 is auto, set in register_scene based on DisplayServer.
  2262. // Swap on means OK first.
  2263. AcceptDialog::set_swap_cancel_ok(swap_cancel_ok == 2);
  2264. }
  2265. EditorColorMap::create();
  2266. Ref<Theme> theme = create_custom_theme();
  2267. DisplayServer::set_early_window_clear_color_override(true, theme->get_color(SNAME("background"), SNAME("Editor")));
  2268. set_theme(theme);
  2269. set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2270. Panel *panel = memnew(Panel);
  2271. add_child(panel);
  2272. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2273. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("Background"), SNAME("EditorStyles")));
  2274. VBoxContainer *vb = memnew(VBoxContainer);
  2275. panel->add_child(vb);
  2276. vb->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 8 * EDSCALE);
  2277. Control *center_box = memnew(Control);
  2278. center_box->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2279. vb->add_child(center_box);
  2280. tabs = memnew(TabContainer);
  2281. center_box->add_child(tabs);
  2282. tabs->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2283. tabs->connect("tab_changed", callable_mp(this, &ProjectManager::_on_tab_changed));
  2284. local_projects_hb = memnew(HBoxContainer);
  2285. local_projects_hb->set_name(TTR("Local Projects"));
  2286. tabs->add_child(local_projects_hb);
  2287. {
  2288. // Projects + search bar
  2289. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  2290. local_projects_hb->add_child(search_tree_vb);
  2291. search_tree_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2292. HBoxContainer *hb = memnew(HBoxContainer);
  2293. hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2294. search_tree_vb->add_child(hb);
  2295. search_box = memnew(LineEdit);
  2296. search_box->set_placeholder(TTR("Filter Projects"));
  2297. search_box->set_tooltip_text(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."));
  2298. search_box->connect("text_changed", callable_mp(this, &ProjectManager::_on_search_term_changed));
  2299. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2300. hb->add_child(search_box);
  2301. loading_label = memnew(Label(TTR("Loading, please wait...")));
  2302. loading_label->add_theme_font_override("font", get_theme_font(SNAME("bold"), SNAME("EditorFonts")));
  2303. loading_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2304. hb->add_child(loading_label);
  2305. // The loading label is shown later.
  2306. loading_label->hide();
  2307. Label *sort_label = memnew(Label);
  2308. sort_label->set_text(TTR("Sort:"));
  2309. hb->add_child(sort_label);
  2310. filter_option = memnew(OptionButton);
  2311. filter_option->set_clip_text(true);
  2312. filter_option->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2313. filter_option->connect("item_selected", callable_mp(this, &ProjectManager::_on_order_option_changed));
  2314. hb->add_child(filter_option);
  2315. Vector<String> sort_filter_titles;
  2316. sort_filter_titles.push_back(TTR("Last Edited"));
  2317. sort_filter_titles.push_back(TTR("Name"));
  2318. sort_filter_titles.push_back(TTR("Path"));
  2319. for (int i = 0; i < sort_filter_titles.size(); i++) {
  2320. filter_option->add_item(sort_filter_titles[i]);
  2321. }
  2322. PanelContainer *pc = memnew(PanelContainer);
  2323. pc->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  2324. pc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2325. search_tree_vb->add_child(pc);
  2326. _project_list = memnew(ProjectList);
  2327. _project_list->connect(ProjectList::SIGNAL_SELECTION_CHANGED, callable_mp(this, &ProjectManager::_update_project_buttons));
  2328. _project_list->connect(ProjectList::SIGNAL_PROJECT_ASK_OPEN, callable_mp(this, &ProjectManager::_open_selected_projects_ask));
  2329. _project_list->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  2330. pc->add_child(_project_list);
  2331. }
  2332. {
  2333. // Project tab side bar
  2334. VBoxContainer *tree_vb = memnew(VBoxContainer);
  2335. tree_vb->set_custom_minimum_size(Size2(120, 120));
  2336. local_projects_hb->add_child(tree_vb);
  2337. const int btn_h_separation = int(6 * EDSCALE);
  2338. create_btn = memnew(Button);
  2339. create_btn->set_text(TTR("New Project"));
  2340. create_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2341. create_btn->set_shortcut(ED_SHORTCUT("project_manager/new_project", TTR("New Project"), KeyModifierMask::CMD_OR_CTRL | Key::N));
  2342. create_btn->connect("pressed", callable_mp(this, &ProjectManager::_new_project));
  2343. tree_vb->add_child(create_btn);
  2344. import_btn = memnew(Button);
  2345. import_btn->set_text(TTR("Import"));
  2346. import_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2347. import_btn->set_shortcut(ED_SHORTCUT("project_manager/import_project", TTR("Import Project"), KeyModifierMask::CMD_OR_CTRL | Key::I));
  2348. import_btn->connect("pressed", callable_mp(this, &ProjectManager::_import_project));
  2349. tree_vb->add_child(import_btn);
  2350. scan_btn = memnew(Button);
  2351. scan_btn->set_text(TTR("Scan"));
  2352. scan_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2353. scan_btn->set_shortcut(ED_SHORTCUT("project_manager/scan_projects", TTR("Scan Projects"), KeyModifierMask::CMD_OR_CTRL | Key::S));
  2354. scan_btn->connect("pressed", callable_mp(this, &ProjectManager::_scan_projects));
  2355. tree_vb->add_child(scan_btn);
  2356. tree_vb->add_child(memnew(HSeparator));
  2357. open_btn = memnew(Button);
  2358. open_btn->set_text(TTR("Edit"));
  2359. open_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2360. open_btn->set_shortcut(ED_SHORTCUT("project_manager/edit_project", TTR("Edit Project"), KeyModifierMask::CMD_OR_CTRL | Key::E));
  2361. open_btn->connect("pressed", callable_mp(this, &ProjectManager::_open_selected_projects_ask));
  2362. tree_vb->add_child(open_btn);
  2363. run_btn = memnew(Button);
  2364. run_btn->set_text(TTR("Run"));
  2365. run_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2366. run_btn->set_shortcut(ED_SHORTCUT("project_manager/run_project", TTR("Run Project"), KeyModifierMask::CMD_OR_CTRL | Key::R));
  2367. run_btn->connect("pressed", callable_mp(this, &ProjectManager::_run_project));
  2368. tree_vb->add_child(run_btn);
  2369. rename_btn = memnew(Button);
  2370. rename_btn->set_text(TTR("Rename"));
  2371. rename_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2372. // The F2 shortcut isn't overridden with Enter on macOS as Enter is already used to edit a project.
  2373. rename_btn->set_shortcut(ED_SHORTCUT("project_manager/rename_project", TTR("Rename Project"), Key::F2));
  2374. rename_btn->connect("pressed", callable_mp(this, &ProjectManager::_rename_project));
  2375. tree_vb->add_child(rename_btn);
  2376. erase_btn = memnew(Button);
  2377. erase_btn->set_text(TTR("Remove"));
  2378. erase_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2379. erase_btn->set_shortcut(ED_SHORTCUT("project_manager/remove_project", TTR("Remove Project"), Key::KEY_DELETE));
  2380. erase_btn->connect("pressed", callable_mp(this, &ProjectManager::_erase_project));
  2381. tree_vb->add_child(erase_btn);
  2382. erase_missing_btn = memnew(Button);
  2383. erase_missing_btn->set_text(TTR("Remove Missing"));
  2384. erase_missing_btn->add_theme_constant_override("h_separation", btn_h_separation);
  2385. erase_missing_btn->connect("pressed", callable_mp(this, &ProjectManager::_erase_missing_projects));
  2386. tree_vb->add_child(erase_missing_btn);
  2387. tree_vb->add_spacer();
  2388. about_btn = memnew(Button);
  2389. about_btn->set_text(TTR("About"));
  2390. about_btn->connect("pressed", callable_mp(this, &ProjectManager::_show_about));
  2391. tree_vb->add_child(about_btn);
  2392. }
  2393. {
  2394. // Version info and language options
  2395. settings_hb = memnew(HBoxContainer);
  2396. settings_hb->set_alignment(BoxContainer::ALIGNMENT_END);
  2397. settings_hb->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  2398. settings_hb->set_anchors_and_offsets_preset(Control::PRESET_TOP_RIGHT);
  2399. // A VBoxContainer that contains a dummy Control node to adjust the LinkButton's vertical position.
  2400. VBoxContainer *spacer_vb = memnew(VBoxContainer);
  2401. settings_hb->add_child(spacer_vb);
  2402. Control *v_spacer = memnew(Control);
  2403. spacer_vb->add_child(v_spacer);
  2404. version_btn = memnew(LinkButton);
  2405. String hash = String(VERSION_HASH);
  2406. if (hash.length() != 0) {
  2407. hash = " " + vformat("[%s]", hash.left(9));
  2408. }
  2409. version_btn->set_text("v" VERSION_FULL_BUILD + hash);
  2410. // Fade the version label to be less prominent, but still readable.
  2411. version_btn->set_self_modulate(Color(1, 1, 1, 0.6));
  2412. version_btn->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  2413. version_btn->set_tooltip_text(TTR("Click to copy."));
  2414. version_btn->connect("pressed", callable_mp(this, &ProjectManager::_version_button_pressed));
  2415. spacer_vb->add_child(version_btn);
  2416. // Add a small horizontal spacer between the version and language buttons
  2417. // to distinguish them.
  2418. Control *h_spacer = memnew(Control);
  2419. settings_hb->add_child(h_spacer);
  2420. language_btn = memnew(OptionButton);
  2421. language_btn->set_icon(get_theme_icon(SNAME("Environment"), SNAME("EditorIcons")));
  2422. language_btn->set_focus_mode(Control::FOCUS_NONE);
  2423. language_btn->set_fit_to_longest_item(false);
  2424. language_btn->set_flat(true);
  2425. language_btn->connect("item_selected", callable_mp(this, &ProjectManager::_language_selected));
  2426. #ifdef ANDROID_ENABLED
  2427. // The language selection dropdown doesn't work on Android (as the setting isn't saved), see GH-60353.
  2428. // Also, the dropdown it spawns is very tall and can't be scrolled without a hardware mouse.
  2429. // Hiding the language selection dropdown also leaves more space for the version label to display.
  2430. language_btn->hide();
  2431. #endif
  2432. Vector<String> editor_languages;
  2433. List<PropertyInfo> editor_settings_properties;
  2434. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  2435. for (const PropertyInfo &pi : editor_settings_properties) {
  2436. if (pi.name == "interface/editor/editor_language") {
  2437. editor_languages = pi.hint_string.split(",");
  2438. break;
  2439. }
  2440. }
  2441. String current_lang = EDITOR_GET("interface/editor/editor_language");
  2442. language_btn->set_text(current_lang);
  2443. for (int i = 0; i < editor_languages.size(); i++) {
  2444. String lang = editor_languages[i];
  2445. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang);
  2446. language_btn->add_item(vformat("[%s] %s", lang, lang_name), i);
  2447. language_btn->set_item_metadata(i, lang);
  2448. if (current_lang == lang) {
  2449. language_btn->select(i);
  2450. }
  2451. }
  2452. settings_hb->add_child(language_btn);
  2453. center_box->add_child(settings_hb);
  2454. }
  2455. if (AssetLibraryEditorPlugin::is_available()) {
  2456. asset_library = memnew(EditorAssetLibrary(true));
  2457. asset_library->set_name(TTR("Asset Library Projects"));
  2458. tabs->add_child(asset_library);
  2459. asset_library->connect("install_asset", callable_mp(this, &ProjectManager::_install_project));
  2460. } else {
  2461. print_verbose("Asset Library not available (due to using Web editor, or SSL support disabled).");
  2462. }
  2463. {
  2464. // Dialogs
  2465. language_restart_ask = memnew(ConfirmationDialog);
  2466. language_restart_ask->set_ok_button_text(TTR("Restart Now"));
  2467. language_restart_ask->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_restart_confirm));
  2468. language_restart_ask->set_cancel_button_text(TTR("Continue"));
  2469. add_child(language_restart_ask);
  2470. scan_dir = memnew(EditorFileDialog);
  2471. scan_dir->set_previews_enabled(false);
  2472. scan_dir->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  2473. scan_dir->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  2474. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  2475. scan_dir->set_current_dir(EDITOR_GET("filesystem/directories/default_project_path"));
  2476. add_child(scan_dir);
  2477. scan_dir->connect("dir_selected", callable_mp(this, &ProjectManager::_scan_begin));
  2478. erase_missing_ask = memnew(ConfirmationDialog);
  2479. erase_missing_ask->set_ok_button_text(TTR("Remove All"));
  2480. erase_missing_ask->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_erase_missing_projects_confirm));
  2481. add_child(erase_missing_ask);
  2482. erase_ask = memnew(ConfirmationDialog);
  2483. erase_ask->set_ok_button_text(TTR("Remove"));
  2484. erase_ask->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_erase_project_confirm));
  2485. add_child(erase_ask);
  2486. VBoxContainer *erase_ask_vb = memnew(VBoxContainer);
  2487. erase_ask->add_child(erase_ask_vb);
  2488. erase_ask_label = memnew(Label);
  2489. erase_ask_vb->add_child(erase_ask_label);
  2490. // Comment out for now until we have a better warning system to
  2491. // ensure users delete their project only.
  2492. //delete_project_contents = memnew(CheckBox);
  2493. //delete_project_contents->set_text(TTR("Also delete project contents (no undo!)"));
  2494. //erase_ask_vb->add_child(delete_project_contents);
  2495. multi_open_ask = memnew(ConfirmationDialog);
  2496. multi_open_ask->set_ok_button_text(TTR("Edit"));
  2497. multi_open_ask->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_open_selected_projects));
  2498. add_child(multi_open_ask);
  2499. multi_run_ask = memnew(ConfirmationDialog);
  2500. multi_run_ask->set_ok_button_text(TTR("Run"));
  2501. multi_run_ask->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_run_project_confirm));
  2502. add_child(multi_run_ask);
  2503. multi_scan_ask = memnew(ConfirmationDialog);
  2504. multi_scan_ask->set_ok_button_text(TTR("Scan"));
  2505. add_child(multi_scan_ask);
  2506. ask_update_settings = memnew(ConfirmationDialog);
  2507. ask_update_settings->set_autowrap(true);
  2508. ask_update_settings->get_ok_button()->connect("pressed", callable_mp(this, &ProjectManager::_confirm_update_settings));
  2509. full_convert_button = ask_update_settings->add_button(TTR("Convert Full Project"), !GLOBAL_GET("gui/common/swap_cancel_ok"));
  2510. full_convert_button->connect("pressed", callable_mp(this, &ProjectManager::_full_convert_button_pressed));
  2511. add_child(ask_update_settings);
  2512. ask_full_convert_dialog = memnew(ConfirmationDialog);
  2513. ask_full_convert_dialog->set_autowrap(true);
  2514. ask_full_convert_dialog->set_text(TTR("This option will perform full project conversion, updating scenes, resources and scripts from Godot 3.x to work in Godot 4.0.\n\nNote that this is a best-effort conversion, i.e. it makes upgrading the project easier, but it will not open out-of-the-box and will still require manual adjustments.\n\nIMPORTANT: Make sure to backup your project before converting, as this operation makes it impossible to open it in older versions of Godot."));
  2515. ask_full_convert_dialog->connect("confirmed", callable_mp(this, &ProjectManager::_perform_full_project_conversion));
  2516. add_child(ask_full_convert_dialog);
  2517. npdialog = memnew(ProjectDialog);
  2518. npdialog->connect("projects_updated", callable_mp(this, &ProjectManager::_on_projects_updated));
  2519. npdialog->connect("project_created", callable_mp(this, &ProjectManager::_on_project_created));
  2520. add_child(npdialog);
  2521. run_error_diag = memnew(AcceptDialog);
  2522. run_error_diag->set_title(TTR("Can't run project"));
  2523. add_child(run_error_diag);
  2524. dialog_error = memnew(AcceptDialog);
  2525. add_child(dialog_error);
  2526. if (asset_library) {
  2527. open_templates = memnew(ConfirmationDialog);
  2528. open_templates->set_text(TTR("You currently don't have any projects.\nWould you like to explore official example projects in the Asset Library?"));
  2529. open_templates->set_ok_button_text(TTR("Open Asset Library"));
  2530. open_templates->connect("confirmed", callable_mp(this, &ProjectManager::_open_asset_library));
  2531. add_child(open_templates);
  2532. }
  2533. about = memnew(EditorAbout);
  2534. add_child(about);
  2535. _build_icon_type_cache(get_theme());
  2536. }
  2537. _project_list->migrate_config();
  2538. _load_recent_projects();
  2539. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::AccessType::ACCESS_FILESYSTEM);
  2540. String default_project_path = EDITOR_GET("filesystem/directories/default_project_path");
  2541. if (!dir_access->dir_exists(default_project_path)) {
  2542. Error error = dir_access->make_dir_recursive(default_project_path);
  2543. if (error != OK) {
  2544. ERR_PRINT("Could not create default project directory at: " + default_project_path);
  2545. }
  2546. }
  2547. String autoscan_path = EDITOR_GET("filesystem/directories/autoscan_project_path");
  2548. if (!autoscan_path.is_empty()) {
  2549. if (dir_access->dir_exists(autoscan_path)) {
  2550. _scan_begin(autoscan_path);
  2551. } else {
  2552. Error error = dir_access->make_dir_recursive(autoscan_path);
  2553. if (error != OK) {
  2554. ERR_PRINT("Could not create project autoscan directory at: " + autoscan_path);
  2555. }
  2556. }
  2557. }
  2558. SceneTree::get_singleton()->get_root()->connect("files_dropped", callable_mp(this, &ProjectManager::_files_dropped));
  2559. // Define a minimum window size to prevent UI elements from overlapping or being cut off.
  2560. Window *w = Object::cast_to<Window>(SceneTree::get_singleton()->get_root());
  2561. if (w) {
  2562. w->set_min_size(Size2(520, 350) * EDSCALE);
  2563. }
  2564. // Resize the bootsplash window based on Editor display scale EDSCALE.
  2565. float scale_factor = MAX(1, EDSCALE);
  2566. if (scale_factor > 1.0) {
  2567. Vector2i window_size = DisplayServer::get_singleton()->window_get_size();
  2568. Rect2i screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen());
  2569. window_size *= scale_factor;
  2570. DisplayServer::get_singleton()->window_set_size(window_size);
  2571. if (screen_rect.size != Vector2i()) {
  2572. Vector2i window_position;
  2573. window_position.x = screen_rect.position.x + (screen_rect.size.x - window_size.x) / 2;
  2574. window_position.y = screen_rect.position.y + (screen_rect.size.y - window_size.y) / 2;
  2575. DisplayServer::get_singleton()->window_set_position(window_position);
  2576. }
  2577. }
  2578. OS::get_singleton()->set_low_processor_usage_mode(true);
  2579. }
  2580. ProjectManager::~ProjectManager() {
  2581. singleton = nullptr;
  2582. if (EditorSettings::get_singleton()) {
  2583. EditorSettings::destroy();
  2584. }
  2585. }