project_manager.cpp 86 KB

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