project_manager.cpp 112 KB

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