project_manager.cpp 99 KB

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