project_manager.cpp 86 KB

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