2
0

project_manager.cpp 87 KB

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