project_manager.cpp 91 KB

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