2
0

project_manager.cpp 89 KB

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