project_manager.cpp 82 KB

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