project_manager.cpp 96 KB

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