project_manager.cpp 99 KB

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