project_manager.cpp 83 KB

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