project_manager.cpp 84 KB

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