project_manager.cpp 95 KB

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