project_manager.cpp 101 KB

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