2
0

project_manager.cpp 102 KB

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