project_manager.cpp 88 KB

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