project_manager.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. /*************************************************************************/
  2. /* project_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "project_manager.h"
  31. #include "core/io/config_file.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/io/stream_peer_ssl.h"
  34. #include "core/io/zip_io.h"
  35. #include "core/os/dir_access.h"
  36. #include "core/os/file_access.h"
  37. #include "core/os/keyboard.h"
  38. #include "core/os/os.h"
  39. #include "core/translation.h"
  40. #include "core/version.h"
  41. #include "core/version_hash.gen.h"
  42. #include "editor_scale.h"
  43. #include "editor_settings.h"
  44. #include "editor_themes.h"
  45. #include "scene/gui/center_container.h"
  46. #include "scene/gui/line_edit.h"
  47. #include "scene/gui/margin_container.h"
  48. #include "scene/gui/panel_container.h"
  49. #include "scene/gui/separator.h"
  50. #include "scene/gui/texture_rect.h"
  51. #include "scene/gui/tool_button.h"
  52. class ProjectDialog : public ConfirmationDialog {
  53. GDCLASS(ProjectDialog, ConfirmationDialog);
  54. public:
  55. enum Mode {
  56. MODE_NEW,
  57. MODE_IMPORT,
  58. MODE_INSTALL,
  59. MODE_RENAME
  60. };
  61. private:
  62. enum MessageType {
  63. MESSAGE_ERROR,
  64. MESSAGE_WARNING,
  65. MESSAGE_SUCCESS
  66. };
  67. enum InputType {
  68. PROJECT_PATH,
  69. INSTALL_PATH
  70. };
  71. Mode mode;
  72. Button *browse;
  73. Button *install_browse;
  74. Button *create_dir;
  75. Container *name_container;
  76. Container *path_container;
  77. Container *install_path_container;
  78. Label *msg;
  79. LineEdit *project_path;
  80. LineEdit *project_name;
  81. LineEdit *install_path;
  82. TextureRect *status_rect;
  83. TextureRect *install_status_rect;
  84. FileDialog *fdialog;
  85. FileDialog *fdialog_install;
  86. String zip_path;
  87. String zip_title;
  88. AcceptDialog *dialog_error;
  89. String fav_dir;
  90. String created_folder_path;
  91. void set_message(const String &p_msg, MessageType p_type = MESSAGE_SUCCESS, InputType input_type = PROJECT_PATH) {
  92. msg->set_text(p_msg);
  93. Ref<Texture> current_path_icon = status_rect->get_texture();
  94. Ref<Texture> current_install_icon = install_status_rect->get_texture();
  95. Ref<Texture> new_icon;
  96. switch (p_type) {
  97. case MESSAGE_ERROR: {
  98. msg->add_color_override("font_color", get_color("error_color", "Editor"));
  99. msg->set_modulate(Color(1, 1, 1, 1));
  100. new_icon = get_icon("StatusError", "EditorIcons");
  101. } break;
  102. case MESSAGE_WARNING: {
  103. msg->add_color_override("font_color", get_color("warning_color", "Editor"));
  104. msg->set_modulate(Color(1, 1, 1, 1));
  105. new_icon = get_icon("StatusWarning", "EditorIcons");
  106. } break;
  107. case MESSAGE_SUCCESS: {
  108. msg->set_modulate(Color(1, 1, 1, 0));
  109. new_icon = get_icon("StatusSuccess", "EditorIcons");
  110. } break;
  111. }
  112. if (current_path_icon != new_icon && input_type == PROJECT_PATH) {
  113. status_rect->set_texture(new_icon);
  114. } else if (current_install_icon != new_icon && input_type == INSTALL_PATH) {
  115. install_status_rect->set_texture(new_icon);
  116. }
  117. set_size(Size2(500, 0) * EDSCALE);
  118. }
  119. String _test_path() {
  120. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  121. String valid_path, valid_install_path;
  122. if (d->change_dir(project_path->get_text()) == OK) {
  123. valid_path = project_path->get_text();
  124. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  125. valid_path = project_path->get_text().strip_edges();
  126. } else if (project_path->get_text().ends_with(".zip")) {
  127. if (d->file_exists(project_path->get_text())) {
  128. valid_path = project_path->get_text();
  129. }
  130. } else if (project_path->get_text().strip_edges().ends_with(".zip")) {
  131. if (d->file_exists(project_path->get_text().strip_edges())) {
  132. valid_path = project_path->get_text().strip_edges();
  133. }
  134. }
  135. if (valid_path == "") {
  136. set_message(TTR("The path does not exist."), MESSAGE_ERROR);
  137. memdelete(d);
  138. get_ok()->set_disabled(true);
  139. return "";
  140. }
  141. if (mode == MODE_IMPORT && valid_path.ends_with(".zip")) {
  142. if (d->change_dir(install_path->get_text()) == OK) {
  143. valid_install_path = install_path->get_text();
  144. } else if (d->change_dir(install_path->get_text().strip_edges()) == OK) {
  145. valid_install_path = install_path->get_text().strip_edges();
  146. }
  147. if (valid_install_path == "") {
  148. set_message(TTR("The path does not exist."), MESSAGE_ERROR, INSTALL_PATH);
  149. memdelete(d);
  150. get_ok()->set_disabled(true);
  151. return "";
  152. }
  153. }
  154. if (mode == MODE_IMPORT || mode == MODE_RENAME) {
  155. if (valid_path != "" && !d->file_exists("project.godot")) {
  156. if (valid_path.ends_with(".zip")) {
  157. FileAccess *src_f = NULL;
  158. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  159. unzFile pkg = unzOpen2(valid_path.utf8().get_data(), &io);
  160. if (!pkg) {
  161. set_message(TTR("Error opening package file, not in zip format."), MESSAGE_ERROR);
  162. memdelete(d);
  163. get_ok()->set_disabled(true);
  164. unzClose(pkg);
  165. return "";
  166. }
  167. int ret = unzGoToFirstFile(pkg);
  168. while (ret == UNZ_OK) {
  169. unz_file_info info;
  170. char fname[16384];
  171. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  172. if (String(fname).ends_with("project.godot")) {
  173. break;
  174. }
  175. ret = unzGoToNextFile(pkg);
  176. }
  177. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  178. set_message(TTR("Invalid '.zip' project file, does not contain a 'project.godot' file."), MESSAGE_ERROR);
  179. memdelete(d);
  180. get_ok()->set_disabled(true);
  181. unzClose(pkg);
  182. return "";
  183. }
  184. unzClose(pkg);
  185. // check if the specified install folder is empty, even though this is not an error, it is good to check here
  186. d->list_dir_begin();
  187. bool is_empty = true;
  188. String n = d->get_next();
  189. while (n != String()) {
  190. if (n != "." && n != "..") {
  191. is_empty = false;
  192. break;
  193. }
  194. n = d->get_next();
  195. }
  196. d->list_dir_end();
  197. if (!is_empty) {
  198. set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH);
  199. memdelete(d);
  200. get_ok()->set_disabled(true);
  201. return "";
  202. }
  203. } else {
  204. set_message(TTR("Please choose a 'project.godot' or '.zip' file."), MESSAGE_ERROR);
  205. memdelete(d);
  206. install_path_container->hide();
  207. get_ok()->set_disabled(true);
  208. return "";
  209. }
  210. } else if (valid_path.ends_with("zip")) {
  211. set_message(TTR("Directory already contains a Godot project."), MESSAGE_ERROR, INSTALL_PATH);
  212. memdelete(d);
  213. get_ok()->set_disabled(true);
  214. return "";
  215. }
  216. } else {
  217. // check if the specified folder is empty, even though this is not an error, it is good to check here
  218. d->list_dir_begin();
  219. bool is_empty = true;
  220. String n = d->get_next();
  221. while (n != String()) {
  222. if (n != "." && n != "..") { // i don't know if this is enough to guarantee an empty dir
  223. is_empty = false;
  224. break;
  225. }
  226. n = d->get_next();
  227. }
  228. d->list_dir_end();
  229. if (!is_empty) {
  230. set_message(TTR("Please choose an empty folder."), MESSAGE_ERROR);
  231. memdelete(d);
  232. get_ok()->set_disabled(true);
  233. return "";
  234. }
  235. }
  236. set_message("");
  237. set_message("", MESSAGE_SUCCESS, INSTALL_PATH);
  238. memdelete(d);
  239. get_ok()->set_disabled(false);
  240. return valid_path;
  241. }
  242. void _path_text_changed(const String &p_path) {
  243. String sp = _test_path();
  244. if (sp != "") {
  245. // set the project name to the select folder name
  246. if (project_name->get_text() == "") {
  247. sp = sp.replace("\\", "/");
  248. int lidx = sp.find_last("/");
  249. if (lidx != -1) {
  250. sp = sp.substr(lidx + 1, sp.length());
  251. }
  252. if (sp == "" && mode == MODE_IMPORT)
  253. sp = TTR("Imported Project");
  254. project_name->set_text(sp);
  255. _text_changed(sp);
  256. }
  257. }
  258. if (created_folder_path != "" && created_folder_path != p_path) {
  259. _remove_created_folder();
  260. }
  261. }
  262. void _file_selected(const String &p_path) {
  263. String p = p_path;
  264. if (mode == MODE_IMPORT) {
  265. if (p.ends_with("project.godot")) {
  266. p = p.get_base_dir();
  267. install_path_container->hide();
  268. get_ok()->set_disabled(false);
  269. } else if (p.ends_with(".zip")) {
  270. install_path->set_text(p.get_base_dir());
  271. install_path_container->show();
  272. get_ok()->set_disabled(false);
  273. } else {
  274. set_message(TTR("Please choose a 'project.godot' or '.zip' file."), MESSAGE_ERROR);
  275. get_ok()->set_disabled(true);
  276. return;
  277. }
  278. }
  279. String sp = p.simplify_path();
  280. project_path->set_text(sp);
  281. _path_text_changed(sp);
  282. if (p.ends_with(".zip")) {
  283. install_path->call_deferred("grab_focus");
  284. } else {
  285. get_ok()->call_deferred("grab_focus");
  286. }
  287. }
  288. void _path_selected(const String &p_path) {
  289. String p = p_path;
  290. String sp = p.simplify_path();
  291. project_path->set_text(sp);
  292. _path_text_changed(sp);
  293. get_ok()->call_deferred("grab_focus");
  294. }
  295. void _install_path_selected(const String &p_path) {
  296. String p = p_path;
  297. String sp = p.simplify_path();
  298. install_path->set_text(sp);
  299. _path_text_changed(sp);
  300. get_ok()->call_deferred("grab_focus");
  301. }
  302. void _browse_path() {
  303. fdialog->set_current_dir(project_path->get_text());
  304. if (mode == MODE_IMPORT) {
  305. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  306. fdialog->clear_filters();
  307. fdialog->add_filter("project.godot ; " VERSION_NAME " Project");
  308. fdialog->add_filter("*.zip ; Zip File");
  309. } else {
  310. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  311. }
  312. fdialog->popup_centered_ratio();
  313. }
  314. void _browse_install_path() {
  315. fdialog_install->set_current_dir(install_path->get_text());
  316. fdialog_install->set_mode(FileDialog::MODE_OPEN_DIR);
  317. fdialog_install->popup_centered_ratio();
  318. }
  319. void _create_folder() {
  320. if (project_name->get_text() == "" || created_folder_path != "" || project_name->get_text().ends_with(".") || project_name->get_text().ends_with(" ")) {
  321. set_message(TTR("Invalid Project Name."), MESSAGE_WARNING);
  322. return;
  323. }
  324. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  325. if (d->change_dir(project_path->get_text()) == OK) {
  326. if (!d->dir_exists(project_name->get_text())) {
  327. if (d->make_dir(project_name->get_text()) == OK) {
  328. d->change_dir(project_name->get_text());
  329. String dir_str = d->get_current_dir();
  330. project_path->set_text(dir_str);
  331. _path_text_changed(dir_str);
  332. created_folder_path = d->get_current_dir();
  333. create_dir->set_disabled(true);
  334. } else {
  335. dialog_error->set_text(TTR("Couldn't create folder."));
  336. dialog_error->popup_centered_minsize();
  337. }
  338. } else {
  339. dialog_error->set_text(TTR("There is already a folder in this path with the specified name."));
  340. dialog_error->popup_centered_minsize();
  341. }
  342. }
  343. memdelete(d);
  344. }
  345. void _text_changed(const String &p_text) {
  346. if (mode != MODE_NEW)
  347. return;
  348. _test_path();
  349. if (p_text == "")
  350. set_message(TTR("It would be a good idea to name your project."), MESSAGE_WARNING);
  351. }
  352. void ok_pressed() {
  353. String dir = project_path->get_text();
  354. if (mode == MODE_RENAME) {
  355. String dir = _test_path();
  356. if (dir == "") {
  357. set_message(TTR("Invalid project path (changed anything?)."), MESSAGE_ERROR);
  358. return;
  359. }
  360. ProjectSettings *current = memnew(ProjectSettings);
  361. int err = current->setup(dir, "");
  362. if (err != OK) {
  363. set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR);
  364. } else {
  365. ProjectSettings::CustomMap edited_settings;
  366. edited_settings["application/config/name"] = project_name->get_text();
  367. if (current->save_custom(dir.plus_file("project.godot"), edited_settings, Vector<String>(), true) != OK) {
  368. set_message(TTR("Couldn't edit project.godot in project path."), MESSAGE_ERROR);
  369. }
  370. }
  371. hide();
  372. emit_signal("projects_updated");
  373. } else {
  374. if (mode == MODE_IMPORT) {
  375. if (project_path->get_text().ends_with(".zip")) {
  376. mode = MODE_INSTALL;
  377. ok_pressed();
  378. return;
  379. }
  380. } else {
  381. if (mode == MODE_NEW) {
  382. ProjectSettings::CustomMap initial_settings;
  383. initial_settings["application/config/name"] = project_name->get_text();
  384. initial_settings["application/config/icon"] = "res://icon.png";
  385. initial_settings["rendering/environment/default_environment"] = "res://default_env.tres";
  386. if (ProjectSettings::get_singleton()->save_custom(dir.plus_file("project.godot"), initial_settings, Vector<String>(), false) != OK) {
  387. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  388. } else {
  389. ResourceSaver::save(dir.plus_file("icon.png"), get_icon("DefaultProjectIcon", "EditorIcons"));
  390. FileAccess *f = FileAccess::open(dir.plus_file("default_env.tres"), FileAccess::WRITE);
  391. if (!f) {
  392. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  393. } else {
  394. f->store_line("[gd_resource type=\"Environment\" load_steps=2 format=2]");
  395. f->store_line("[sub_resource type=\"ProceduralSky\" id=1]");
  396. f->store_line("[resource]");
  397. f->store_line("background_mode = 2");
  398. f->store_line("background_sky = SubResource( 1 )");
  399. memdelete(f);
  400. }
  401. }
  402. } else if (mode == MODE_INSTALL) {
  403. if (project_path->get_text().ends_with(".zip")) {
  404. dir = install_path->get_text();
  405. zip_path = project_path->get_text();
  406. }
  407. FileAccess *src_f = NULL;
  408. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  409. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  410. if (!pkg) {
  411. dialog_error->set_text(TTR("Error opening package file, not in zip format."));
  412. dialog_error->popup_centered_minsize();
  413. return;
  414. }
  415. int ret = unzGoToFirstFile(pkg);
  416. Vector<String> failed_files;
  417. int idx = 0;
  418. while (ret == UNZ_OK) {
  419. //get filename
  420. unz_file_info info;
  421. char fname[16384];
  422. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  423. String path = fname;
  424. int depth = 1; //stuff from github comes with tag
  425. bool skip = false;
  426. while (depth > 0) {
  427. int pp = path.find("/");
  428. if (pp == -1) {
  429. skip = true;
  430. break;
  431. }
  432. path = path.substr(pp + 1, path.length());
  433. depth--;
  434. }
  435. if (skip || path == String()) {
  436. //
  437. } else if (path.ends_with("/")) { // a dir
  438. path = path.substr(0, path.length() - 1);
  439. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  440. da->make_dir(dir.plus_file(path));
  441. memdelete(da);
  442. } else {
  443. Vector<uint8_t> data;
  444. data.resize(info.uncompressed_size);
  445. //read
  446. unzOpenCurrentFile(pkg);
  447. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  448. unzCloseCurrentFile(pkg);
  449. FileAccess *f = FileAccess::open(dir.plus_file(path), FileAccess::WRITE);
  450. if (f) {
  451. f->store_buffer(data.ptr(), data.size());
  452. memdelete(f);
  453. } else {
  454. failed_files.push_back(path);
  455. }
  456. }
  457. idx++;
  458. ret = unzGoToNextFile(pkg);
  459. }
  460. unzClose(pkg);
  461. if (failed_files.size()) {
  462. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  463. for (int i = 0; i < failed_files.size(); i++) {
  464. if (i > 15) {
  465. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  466. break;
  467. }
  468. msg += failed_files[i] + "\n";
  469. }
  470. dialog_error->set_text(msg);
  471. dialog_error->popup_centered_minsize();
  472. } else if (!project_path->get_text().ends_with(".zip")) {
  473. dialog_error->set_text(TTR("Package installed successfully!"));
  474. dialog_error->popup_centered_minsize();
  475. }
  476. }
  477. }
  478. dir = dir.replace("\\", "/");
  479. if (dir.ends_with("/"))
  480. dir = dir.substr(0, dir.length() - 1);
  481. String proj = dir.replace("/", "::");
  482. EditorSettings::get_singleton()->set("projects/" + proj, dir);
  483. EditorSettings::get_singleton()->save();
  484. hide();
  485. emit_signal("project_created", dir);
  486. }
  487. }
  488. void _remove_created_folder() {
  489. if (created_folder_path != "") {
  490. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  491. d->remove(created_folder_path);
  492. memdelete(d);
  493. create_dir->set_disabled(false);
  494. created_folder_path = "";
  495. }
  496. }
  497. void cancel_pressed() {
  498. _remove_created_folder();
  499. project_path->clear();
  500. _path_text_changed("");
  501. project_name->clear();
  502. _text_changed("");
  503. if (status_rect->get_texture() == get_icon("StatusError", "EditorIcons"))
  504. msg->show();
  505. if (install_status_rect->get_texture() == get_icon("StatusError", "EditorIcons"))
  506. msg->show();
  507. }
  508. void _notification(int p_what) {
  509. if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST)
  510. _remove_created_folder();
  511. }
  512. protected:
  513. static void _bind_methods() {
  514. ClassDB::bind_method("_browse_path", &ProjectDialog::_browse_path);
  515. ClassDB::bind_method("_create_folder", &ProjectDialog::_create_folder);
  516. ClassDB::bind_method("_text_changed", &ProjectDialog::_text_changed);
  517. ClassDB::bind_method("_path_text_changed", &ProjectDialog::_path_text_changed);
  518. ClassDB::bind_method("_path_selected", &ProjectDialog::_path_selected);
  519. ClassDB::bind_method("_file_selected", &ProjectDialog::_file_selected);
  520. ClassDB::bind_method("_install_path_selected", &ProjectDialog::_install_path_selected);
  521. ClassDB::bind_method("_browse_install_path", &ProjectDialog::_browse_install_path);
  522. ADD_SIGNAL(MethodInfo("project_created"));
  523. ADD_SIGNAL(MethodInfo("projects_updated"));
  524. }
  525. public:
  526. void set_zip_path(const String &p_path) {
  527. zip_path = p_path;
  528. }
  529. void set_zip_title(const String &p_title) {
  530. zip_title = p_title;
  531. }
  532. void set_mode(Mode p_mode) {
  533. mode = p_mode;
  534. }
  535. void set_project_path(const String &p_path) {
  536. project_path->set_text(p_path);
  537. }
  538. void show_dialog() {
  539. if (mode == MODE_RENAME) {
  540. project_path->set_editable(false);
  541. browse->hide();
  542. install_browse->hide();
  543. set_title(TTR("Rename Project"));
  544. get_ok()->set_text(TTR("Rename"));
  545. name_container->show();
  546. status_rect->hide();
  547. msg->hide();
  548. install_path_container->hide();
  549. install_status_rect->hide();
  550. get_ok()->set_disabled(false);
  551. ProjectSettings *current = memnew(ProjectSettings);
  552. int err = current->setup(project_path->get_text(), "");
  553. if (err != OK) {
  554. set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR);
  555. status_rect->show();
  556. msg->show();
  557. get_ok()->set_disabled(true);
  558. } else if (current->has_setting("application/config/name")) {
  559. String proj = current->get("application/config/name");
  560. project_name->set_text(proj);
  561. _text_changed(proj);
  562. }
  563. project_name->call_deferred("grab_focus");
  564. create_dir->hide();
  565. } else {
  566. fav_dir = EditorSettings::get_singleton()->get("filesystem/directories/default_project_path");
  567. if (fav_dir != "") {
  568. project_path->set_text(fav_dir);
  569. fdialog->set_current_dir(fav_dir);
  570. } else {
  571. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  572. project_path->set_text(d->get_current_dir());
  573. fdialog->set_current_dir(d->get_current_dir());
  574. memdelete(d);
  575. }
  576. String proj = TTR("New Game Project");
  577. project_name->set_text(proj);
  578. _text_changed(proj);
  579. project_path->set_editable(true);
  580. browse->set_disabled(false);
  581. browse->show();
  582. install_browse->set_disabled(false);
  583. install_browse->show();
  584. create_dir->show();
  585. status_rect->show();
  586. install_status_rect->show();
  587. msg->show();
  588. if (mode == MODE_IMPORT) {
  589. set_title(TTR("Import Existing Project"));
  590. get_ok()->set_text(TTR("Import & Edit"));
  591. name_container->hide();
  592. install_path_container->hide();
  593. project_path->grab_focus();
  594. } else if (mode == MODE_NEW) {
  595. set_title(TTR("Create New Project"));
  596. get_ok()->set_text(TTR("Create & Edit"));
  597. name_container->show();
  598. install_path_container->hide();
  599. project_name->call_deferred("grab_focus");
  600. project_name->call_deferred("select_all");
  601. } else if (mode == MODE_INSTALL) {
  602. set_title(TTR("Install Project:") + " " + zip_title);
  603. get_ok()->set_text(TTR("Install & Edit"));
  604. name_container->hide();
  605. install_path_container->hide();
  606. project_path->grab_focus();
  607. }
  608. _test_path();
  609. }
  610. popup_centered(Size2(500, 0) * EDSCALE);
  611. }
  612. ProjectDialog() {
  613. VBoxContainer *vb = memnew(VBoxContainer);
  614. add_child(vb);
  615. name_container = memnew(VBoxContainer);
  616. vb->add_child(name_container);
  617. Label *l = memnew(Label);
  618. l->set_text(TTR("Project Name:"));
  619. name_container->add_child(l);
  620. HBoxContainer *pnhb = memnew(HBoxContainer);
  621. name_container->add_child(pnhb);
  622. project_name = memnew(LineEdit);
  623. project_name->set_h_size_flags(SIZE_EXPAND_FILL);
  624. pnhb->add_child(project_name);
  625. create_dir = memnew(Button);
  626. pnhb->add_child(create_dir);
  627. create_dir->set_text(TTR("Create folder"));
  628. create_dir->connect("pressed", this, "_create_folder");
  629. path_container = memnew(VBoxContainer);
  630. vb->add_child(path_container);
  631. l = memnew(Label);
  632. l->set_text(TTR("Project Path:"));
  633. path_container->add_child(l);
  634. HBoxContainer *pphb = memnew(HBoxContainer);
  635. path_container->add_child(pphb);
  636. project_path = memnew(LineEdit);
  637. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  638. pphb->add_child(project_path);
  639. install_path_container = memnew(VBoxContainer);
  640. vb->add_child(install_path_container);
  641. l = memnew(Label);
  642. l->set_text(TTR("Project Installation Path:"));
  643. install_path_container->add_child(l);
  644. HBoxContainer *iphb = memnew(HBoxContainer);
  645. install_path_container->add_child(iphb);
  646. install_path = memnew(LineEdit);
  647. install_path->set_h_size_flags(SIZE_EXPAND_FILL);
  648. iphb->add_child(install_path);
  649. // status icon
  650. status_rect = memnew(TextureRect);
  651. status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  652. pphb->add_child(status_rect);
  653. browse = memnew(Button);
  654. browse->set_text(TTR("Browse"));
  655. browse->connect("pressed", this, "_browse_path");
  656. pphb->add_child(browse);
  657. // install status icon
  658. install_status_rect = memnew(TextureRect);
  659. install_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  660. iphb->add_child(install_status_rect);
  661. install_browse = memnew(Button);
  662. install_browse->set_text(TTR("Browse"));
  663. install_browse->connect("pressed", this, "_browse_install_path");
  664. iphb->add_child(install_browse);
  665. msg = memnew(Label);
  666. msg->set_align(Label::ALIGN_CENTER);
  667. vb->add_child(msg);
  668. fdialog = memnew(FileDialog);
  669. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  670. fdialog_install = memnew(FileDialog);
  671. fdialog_install->set_access(FileDialog::ACCESS_FILESYSTEM);
  672. add_child(fdialog);
  673. add_child(fdialog_install);
  674. project_name->connect("text_changed", this, "_text_changed");
  675. project_path->connect("text_changed", this, "_path_text_changed");
  676. install_path->connect("text_changed", this, "_path_text_changed");
  677. fdialog->connect("dir_selected", this, "_path_selected");
  678. fdialog->connect("file_selected", this, "_file_selected");
  679. fdialog_install->connect("dir_selected", this, "_install_path_selected");
  680. fdialog_install->connect("file_selected", this, "_install_path_selected");
  681. set_hide_on_ok(false);
  682. mode = MODE_NEW;
  683. dialog_error = memnew(AcceptDialog);
  684. add_child(dialog_error);
  685. }
  686. };
  687. struct ProjectItem {
  688. String project;
  689. String path;
  690. String conf;
  691. int config_version;
  692. uint64_t last_modified;
  693. bool favorite;
  694. bool grayed;
  695. bool ordered_latest_modification;
  696. ProjectItem() {}
  697. ProjectItem(const String &p_project, const String &p_path, const String &p_conf, int p_config_version, uint64_t p_last_modified, bool p_favorite = false, bool p_grayed = false, const bool p_ordered_latest_modification = true) {
  698. project = p_project;
  699. path = p_path;
  700. conf = p_conf;
  701. config_version = p_config_version;
  702. last_modified = p_last_modified;
  703. favorite = p_favorite;
  704. grayed = p_grayed;
  705. ordered_latest_modification = p_ordered_latest_modification;
  706. }
  707. _FORCE_INLINE_ bool operator<(const ProjectItem &l) const {
  708. if (ordered_latest_modification)
  709. return last_modified > l.last_modified;
  710. return project < l.project;
  711. }
  712. _FORCE_INLINE_ bool operator==(const ProjectItem &l) const { return project == l.project; }
  713. };
  714. void ProjectManager::_notification(int p_what) {
  715. switch (p_what) {
  716. case NOTIFICATION_ENTER_TREE: {
  717. Engine::get_singleton()->set_editor_hint(false);
  718. } break;
  719. case NOTIFICATION_READY: {
  720. if (scroll_children->get_child_count() == 0 && StreamPeerSSL::is_available())
  721. open_templates->popup_centered_minsize();
  722. } break;
  723. case NOTIFICATION_VISIBILITY_CHANGED: {
  724. set_process_unhandled_input(is_visible_in_tree());
  725. } break;
  726. }
  727. }
  728. void ProjectManager::_panel_draw(Node *p_hb) {
  729. HBoxContainer *hb = Object::cast_to<HBoxContainer>(p_hb);
  730. hb->draw_line(Point2(0, hb->get_size().y + 1), Point2(hb->get_size().x - 10, hb->get_size().y + 1), get_color("guide_color", "Tree"));
  731. if (selected_list.has(hb->get_meta("name"))) {
  732. hb->draw_style_box(gui_base->get_stylebox("selected", "Tree"), Rect2(Point2(), hb->get_size() - Size2(10, 0) * EDSCALE));
  733. }
  734. }
  735. void ProjectManager::_update_project_buttons() {
  736. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  737. CanvasItem *item = Object::cast_to<CanvasItem>(scroll_children->get_child(i));
  738. item->update();
  739. }
  740. bool empty_selection = selected_list.empty();
  741. erase_btn->set_disabled(empty_selection);
  742. open_btn->set_disabled(empty_selection);
  743. rename_btn->set_disabled(empty_selection);
  744. run_btn->set_disabled(empty_selection);
  745. }
  746. void ProjectManager::_panel_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  747. Ref<InputEventMouseButton> mb = p_ev;
  748. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  749. String clicked = p_hb->get_meta("name");
  750. String clicked_main_scene = p_hb->get_meta("main_scene");
  751. if (mb->get_shift() && selected_list.size() > 0 && last_clicked != "" && clicked != last_clicked) {
  752. int clicked_id = -1;
  753. int last_clicked_id = -1;
  754. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  755. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  756. if (!hb) continue;
  757. if (hb->get_meta("name") == clicked) clicked_id = i;
  758. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  759. }
  760. if (last_clicked_id != -1 && clicked_id != -1) {
  761. int min = clicked_id < last_clicked_id ? clicked_id : last_clicked_id;
  762. int max = clicked_id > last_clicked_id ? clicked_id : last_clicked_id;
  763. for (int i = 0; i < scroll_children->get_child_count(); ++i) {
  764. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  765. if (!hb) continue;
  766. if (i != clicked_id && (i < min || i > max) && !mb->get_control()) {
  767. selected_list.erase(hb->get_meta("name"));
  768. } else if (i >= min && i <= max) {
  769. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  770. }
  771. }
  772. }
  773. } else if (selected_list.has(clicked) && mb->get_control()) {
  774. selected_list.erase(clicked);
  775. } else {
  776. last_clicked = clicked;
  777. if (mb->get_control() || selected_list.size() == 0) {
  778. selected_list.insert(clicked, clicked_main_scene);
  779. } else {
  780. selected_list.clear();
  781. selected_list.insert(clicked, clicked_main_scene);
  782. }
  783. }
  784. _update_project_buttons();
  785. if (mb->is_doubleclick())
  786. _open_selected_projects_ask(); //open if doubleclicked
  787. }
  788. }
  789. void ProjectManager::_unhandled_input(const Ref<InputEvent> &p_ev) {
  790. Ref<InputEventKey> k = p_ev;
  791. if (k.is_valid()) {
  792. if (!k->is_pressed())
  793. return;
  794. if (tabs->get_current_tab() != 0)
  795. return;
  796. bool scancode_handled = true;
  797. switch (k->get_scancode()) {
  798. case KEY_ENTER: {
  799. _open_selected_projects_ask();
  800. } break;
  801. case KEY_DELETE: {
  802. _erase_project();
  803. } break;
  804. case KEY_HOME: {
  805. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  806. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  807. if (hb) {
  808. selected_list.clear();
  809. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  810. scroll->set_v_scroll(0);
  811. _update_project_buttons();
  812. break;
  813. }
  814. }
  815. } break;
  816. case KEY_END: {
  817. for (int i = scroll_children->get_child_count() - 1; i >= 0; i--) {
  818. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  819. if (hb) {
  820. selected_list.clear();
  821. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  822. scroll->set_v_scroll(scroll_children->get_size().y);
  823. _update_project_buttons();
  824. break;
  825. }
  826. }
  827. } break;
  828. case KEY_UP: {
  829. if (k->get_shift())
  830. break;
  831. if (selected_list.size()) {
  832. bool found = false;
  833. for (int i = scroll_children->get_child_count() - 1; i >= 0; i--) {
  834. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  835. if (!hb) continue;
  836. String current = hb->get_meta("name");
  837. if (found) {
  838. selected_list.clear();
  839. selected_list.insert(current, hb->get_meta("main_scene"));
  840. int offset_diff = scroll->get_v_scroll() - hb->get_position().y;
  841. if (offset_diff > 0)
  842. scroll->set_v_scroll(scroll->get_v_scroll() - offset_diff);
  843. _update_project_buttons();
  844. break;
  845. } else if (current == selected_list.back()->key()) {
  846. found = true;
  847. }
  848. }
  849. break;
  850. }
  851. // else fallthrough to key_down
  852. }
  853. case KEY_DOWN: {
  854. if (k->get_shift())
  855. break;
  856. bool found = selected_list.empty();
  857. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  858. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  859. if (!hb) continue;
  860. String current = hb->get_meta("name");
  861. if (found) {
  862. selected_list.clear();
  863. selected_list.insert(current, hb->get_meta("main_scene"));
  864. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  865. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  866. if (offset_diff > 0)
  867. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  868. _update_project_buttons();
  869. break;
  870. } else if (current == selected_list.back()->key()) {
  871. found = true;
  872. }
  873. }
  874. } break;
  875. case KEY_F: {
  876. if (k->get_command())
  877. this->project_filter->search_box->grab_focus();
  878. else
  879. scancode_handled = false;
  880. } break;
  881. default: {
  882. scancode_handled = false;
  883. } break;
  884. }
  885. if (scancode_handled) {
  886. accept_event();
  887. }
  888. }
  889. }
  890. void ProjectManager::_favorite_pressed(Node *p_hb) {
  891. String clicked = p_hb->get_meta("name");
  892. bool favorite = !p_hb->get_meta("favorite");
  893. String proj = clicked.replace(":::", ":/");
  894. proj = proj.replace("::", "/");
  895. if (favorite) {
  896. EditorSettings::get_singleton()->set("favorite_projects/" + clicked, proj);
  897. } else {
  898. EditorSettings::get_singleton()->erase("favorite_projects/" + clicked);
  899. }
  900. EditorSettings::get_singleton()->save();
  901. call_deferred("_load_recent_projects");
  902. }
  903. void ProjectManager::_load_recent_projects() {
  904. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  905. String search_term = project_filter->get_search_term();
  906. while (scroll_children->get_child_count() > 0) {
  907. memdelete(scroll_children->get_child(0));
  908. }
  909. Map<String, String> selected_list_copy = selected_list;
  910. List<PropertyInfo> properties;
  911. EditorSettings::get_singleton()->get_property_list(&properties);
  912. Color font_color = gui_base->get_color("font_color", "Tree");
  913. bool set_ordered_latest_modification;
  914. ProjectListFilter::FilterOption filter_order_option = project_order_filter->get_filter_option();
  915. if (filter_order_option == ProjectListFilter::FILTER_NAME) {
  916. set_ordered_latest_modification = false;
  917. } else {
  918. set_ordered_latest_modification = true;
  919. }
  920. EditorSettings::get_singleton()->set("project_manager/sorting_order", (int)filter_order_option);
  921. List<ProjectItem> projects;
  922. List<ProjectItem> favorite_projects;
  923. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  924. String _name = E->get().name;
  925. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  926. continue;
  927. String path = EditorSettings::get_singleton()->get(_name);
  928. if (filter_option == ProjectListFilter::FILTER_PATH && search_term != "" && path.findn(search_term) == -1)
  929. continue;
  930. String project = _name.get_slice("/", 1);
  931. String conf = path.plus_file("project.godot");
  932. int config_version = 0; // Assume 0 until we know better
  933. bool favorite = (_name.begins_with("favorite_projects/")) ? true : false;
  934. bool grayed = false;
  935. uint64_t last_modified = 0;
  936. if (FileAccess::exists(conf)) {
  937. last_modified = FileAccess::get_modified_time(conf);
  938. String fscache = path.plus_file(".fscache");
  939. if (FileAccess::exists(fscache)) {
  940. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  941. if (cache_modified > last_modified)
  942. last_modified = cache_modified;
  943. }
  944. } else {
  945. grayed = true;
  946. }
  947. ProjectItem item(project, path, conf, config_version, last_modified, favorite, grayed, set_ordered_latest_modification);
  948. if (favorite)
  949. favorite_projects.push_back(item);
  950. else
  951. projects.push_back(item);
  952. }
  953. projects.sort();
  954. favorite_projects.sort();
  955. for (List<ProjectItem>::Element *E = projects.front(); E;) {
  956. List<ProjectItem>::Element *next = E->next();
  957. if (favorite_projects.find(E->get()) != NULL)
  958. projects.erase(E->get());
  959. E = next;
  960. }
  961. for (List<ProjectItem>::Element *E = favorite_projects.back(); E; E = E->prev()) {
  962. projects.push_front(E->get());
  963. }
  964. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  965. for (List<ProjectItem>::Element *E = projects.front(); E; E = E->next()) {
  966. ProjectItem &item = E->get();
  967. String project = item.project;
  968. String path = item.path;
  969. String conf = item.conf;
  970. Ref<ConfigFile> cf = memnew(ConfigFile);
  971. Error cf_err = cf->load(conf);
  972. String project_name = TTR("Unnamed Project");
  973. if (cf_err == OK && cf->has_section_key("application", "config/name")) {
  974. project_name = static_cast<String>(cf->get_value("application", "config/name")).xml_unescape();
  975. }
  976. if (filter_option == ProjectListFilter::FILTER_NAME && search_term != "" && project_name.findn(search_term) == -1)
  977. continue;
  978. Ref<Texture> icon;
  979. String main_scene;
  980. if (cf_err == OK) {
  981. item.config_version = (int)cf->get_value("", "config_version", 0);
  982. if (item.config_version > ProjectSettings::CONFIG_VERSION) {
  983. // Comes from an incompatible (more recent) Godot version, grey it out
  984. item.grayed = true;
  985. }
  986. String appicon = cf->get_value("application", "config/icon", "");
  987. if (appicon != "") {
  988. Ref<Image> img;
  989. img.instance();
  990. Error err = img->load(appicon.replace_first("res://", path + "/"));
  991. if (err == OK) {
  992. Ref<Texture> default_icon = get_icon("DefaultProjectIcon", "EditorIcons");
  993. img->resize(default_icon->get_width(), default_icon->get_height());
  994. Ref<ImageTexture> it = memnew(ImageTexture);
  995. it->create_from_image(img);
  996. icon = it;
  997. }
  998. }
  999. main_scene = cf->get_value("application", "run/main_scene", "");
  1000. }
  1001. if (icon.is_null()) {
  1002. icon = get_icon("DefaultProjectIcon", "EditorIcons");
  1003. }
  1004. selected_list_copy.erase(project);
  1005. bool is_favorite = item.favorite;
  1006. bool is_grayed = item.grayed;
  1007. HBoxContainer *hb = memnew(HBoxContainer);
  1008. hb->set_meta("name", project);
  1009. hb->set_meta("main_scene", main_scene);
  1010. hb->set_meta("favorite", is_favorite);
  1011. hb->connect("draw", this, "_panel_draw", varray(hb));
  1012. hb->connect("gui_input", this, "_panel_input", varray(hb));
  1013. hb->add_constant_override("separation", 10 * EDSCALE);
  1014. VBoxContainer *favorite_box = memnew(VBoxContainer);
  1015. TextureButton *favorite = memnew(TextureButton);
  1016. favorite->set_normal_texture(favorite_icon);
  1017. if (!is_favorite)
  1018. favorite->set_modulate(Color(1, 1, 1, 0.2));
  1019. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  1020. favorite_box->add_child(favorite);
  1021. favorite_box->set_alignment(BoxContainer::ALIGN_CENTER);
  1022. hb->add_child(favorite_box);
  1023. TextureRect *tf = memnew(TextureRect);
  1024. tf->set_texture(icon);
  1025. hb->add_child(tf);
  1026. VBoxContainer *vb = memnew(VBoxContainer);
  1027. if (is_grayed)
  1028. vb->set_modulate(Color(0.5, 0.5, 0.5));
  1029. vb->set_name("project");
  1030. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1031. hb->add_child(vb);
  1032. Control *ec = memnew(Control);
  1033. ec->set_custom_minimum_size(Size2(0, 1));
  1034. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  1035. vb->add_child(ec);
  1036. Label *title = memnew(Label(project_name));
  1037. title->add_font_override("font", gui_base->get_font("title", "EditorFonts"));
  1038. title->add_color_override("font_color", font_color);
  1039. title->set_clip_text(true);
  1040. vb->add_child(title);
  1041. HBoxContainer *path_hb = memnew(HBoxContainer);
  1042. path_hb->set_name("path_box");
  1043. path_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1044. vb->add_child(path_hb);
  1045. Button *show = memnew(Button);
  1046. show->set_name("show");
  1047. show->set_icon(get_icon("Filesystem", "EditorIcons"));
  1048. show->set_flat(true);
  1049. show->set_modulate(Color(1, 1, 1, 0.5));
  1050. path_hb->add_child(show);
  1051. show->connect("pressed", this, "_show_project", varray(path));
  1052. show->set_tooltip(TTR("Show in File Manager"));
  1053. Label *fpath = memnew(Label(path));
  1054. fpath->set_name("path");
  1055. path_hb->add_child(fpath);
  1056. fpath->set_h_size_flags(SIZE_EXPAND_FILL);
  1057. fpath->set_modulate(Color(1, 1, 1, 0.5));
  1058. fpath->add_color_override("font_color", font_color);
  1059. fpath->set_clip_text(true);
  1060. scroll_children->add_child(hb);
  1061. }
  1062. for (Map<String, String>::Element *E = selected_list_copy.front(); E; E = E->next()) {
  1063. String key = E->key();
  1064. selected_list.erase(key);
  1065. }
  1066. scroll->set_v_scroll(0);
  1067. _update_project_buttons();
  1068. EditorSettings::get_singleton()->save();
  1069. tabs->set_current_tab(0);
  1070. }
  1071. void ProjectManager::_on_projects_updated() {
  1072. _load_recent_projects();
  1073. }
  1074. void ProjectManager::_on_project_created(const String &dir) {
  1075. bool has_already = false;
  1076. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1077. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1078. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path_box/path")));
  1079. if (fpath->get_text() == dir) {
  1080. has_already = true;
  1081. break;
  1082. }
  1083. }
  1084. if (has_already) {
  1085. _update_scroll_position(dir);
  1086. } else {
  1087. _load_recent_projects();
  1088. _update_scroll_position(dir);
  1089. }
  1090. _open_selected_projects_ask();
  1091. }
  1092. void ProjectManager::_update_scroll_position(const String &dir) {
  1093. for (int i = 0; i < scroll_children->get_child_count(); i++) {
  1094. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_children->get_child(i));
  1095. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path_box/path")));
  1096. if (fpath->get_text() == dir) {
  1097. last_clicked = hb->get_meta("name");
  1098. selected_list.clear();
  1099. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  1100. _update_project_buttons();
  1101. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  1102. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  1103. if (offset_diff > 0)
  1104. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. void ProjectManager::_confirm_update_settings() {
  1110. _open_selected_projects();
  1111. }
  1112. void ProjectManager::_open_selected_projects() {
  1113. for (const Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1114. const String &selected = E->key();
  1115. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1116. String conf = path.plus_file("project.godot");
  1117. if (!FileAccess::exists(conf)) {
  1118. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1119. dialog_error->popup_centered_minsize();
  1120. return;
  1121. }
  1122. print_line("Editing project: " + path + " (" + selected + ")");
  1123. List<String> args;
  1124. args.push_back("--path");
  1125. args.push_back(path);
  1126. args.push_back("--editor");
  1127. if (OS::get_singleton()->is_disable_crash_handler()) {
  1128. args.push_back("--disable-crash-handler");
  1129. }
  1130. String exec = OS::get_singleton()->get_executable_path();
  1131. OS::ProcessID pid = 0;
  1132. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1133. ERR_FAIL_COND(err);
  1134. }
  1135. get_tree()->quit();
  1136. }
  1137. void ProjectManager::_open_selected_projects_ask() {
  1138. if (selected_list.size() < 1) {
  1139. return;
  1140. }
  1141. if (selected_list.size() > 1) {
  1142. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  1143. multi_open_ask->popup_centered_minsize();
  1144. return;
  1145. }
  1146. // Update the project settings or don't open
  1147. String path = EditorSettings::get_singleton()->get("projects/" + selected_list.front()->key());
  1148. String conf = path.plus_file("project.godot");
  1149. // FIXME: We already parse those in _load_recent_projects, we could instead make
  1150. // its `projects` list global and reuse its parsed metadata here.
  1151. Ref<ConfigFile> cf = memnew(ConfigFile);
  1152. Error cf_err = cf->load(conf);
  1153. if (cf_err != OK) {
  1154. dialog_error->set_text(vformat(TTR("Can't open project at '%s'."), path));
  1155. dialog_error->popup_centered_minsize();
  1156. return;
  1157. }
  1158. int config_version = (int)cf->get_value("", "config_version", 0);
  1159. // Check if we need to convert project settings from an earlier engine version
  1160. if (config_version < ProjectSettings::CONFIG_VERSION) {
  1161. 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 will not be able to open the project with previous versions of the engine anymore."), conf));
  1162. ask_update_settings->popup_centered_minsize();
  1163. return;
  1164. }
  1165. // Check if the file was generated by a newer, incompatible engine version
  1166. if (config_version > ProjectSettings::CONFIG_VERSION) {
  1167. 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."), path));
  1168. dialog_error->popup_centered_minsize();
  1169. return;
  1170. }
  1171. // Open if the project is up-to-date
  1172. _open_selected_projects();
  1173. }
  1174. void ProjectManager::_run_project_confirm() {
  1175. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1176. const String &selected_main = E->get();
  1177. if (selected_main == "") {
  1178. run_error_diag->set_text(TTR("Can't run project: no main scene defined.\nPlease edit the project and set the main scene in \"Project Settings\" under the \"Application\" category."));
  1179. run_error_diag->popup_centered();
  1180. return;
  1181. }
  1182. const String &selected = E->key();
  1183. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1184. if (!DirAccess::exists(path + "/.import")) {
  1185. run_error_diag->set_text(TTR("Can't run project: Assets need to be imported.\nPlease edit the project to trigger the initial import."));
  1186. run_error_diag->popup_centered();
  1187. return;
  1188. }
  1189. print_line("Running project: " + path + " (" + selected + ")");
  1190. List<String> args;
  1191. args.push_back("--path");
  1192. args.push_back(path);
  1193. if (OS::get_singleton()->is_disable_crash_handler()) {
  1194. args.push_back("--disable-crash-handler");
  1195. }
  1196. String exec = OS::get_singleton()->get_executable_path();
  1197. OS::ProcessID pid = 0;
  1198. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1199. ERR_FAIL_COND(err);
  1200. }
  1201. }
  1202. void ProjectManager::_run_project() {
  1203. if (selected_list.size() < 1) {
  1204. return;
  1205. }
  1206. if (selected_list.size() > 1) {
  1207. multi_run_ask->set_text(TTR("Are you sure to run more than one project?"));
  1208. multi_run_ask->popup_centered_minsize();
  1209. } else {
  1210. _run_project_confirm();
  1211. }
  1212. }
  1213. void ProjectManager::_show_project(const String &p_path) {
  1214. OS::get_singleton()->shell_open(String("file://") + p_path);
  1215. }
  1216. void ProjectManager::_scan_dir(DirAccess *da, float pos, float total, List<String> *r_projects) {
  1217. List<String> subdirs;
  1218. da->list_dir_begin();
  1219. String n = da->get_next();
  1220. while (n != String()) {
  1221. if (da->current_is_dir() && !n.begins_with(".")) {
  1222. subdirs.push_front(n);
  1223. } else if (n == "project.godot") {
  1224. r_projects->push_back(da->get_current_dir());
  1225. }
  1226. n = da->get_next();
  1227. }
  1228. da->list_dir_end();
  1229. int m = 0;
  1230. for (List<String>::Element *E = subdirs.front(); E; E = E->next()) {
  1231. da->change_dir(E->get());
  1232. float slice = total / subdirs.size();
  1233. _scan_dir(da, pos + slice * m, slice, r_projects);
  1234. da->change_dir("..");
  1235. m++;
  1236. }
  1237. }
  1238. void ProjectManager::_scan_begin(const String &p_base) {
  1239. print_line("Scanning projects at: " + p_base);
  1240. List<String> projects;
  1241. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1242. da->change_dir(p_base);
  1243. _scan_dir(da, 0, 1, &projects);
  1244. memdelete(da);
  1245. print_line("Found " + itos(projects.size()) + " projects.");
  1246. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  1247. String proj = E->get().replace("/", "::");
  1248. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  1249. }
  1250. EditorSettings::get_singleton()->save();
  1251. _load_recent_projects();
  1252. }
  1253. void ProjectManager::_scan_projects() {
  1254. scan_dir->popup_centered_ratio();
  1255. }
  1256. void ProjectManager::_new_project() {
  1257. npdialog->set_mode(ProjectDialog::MODE_NEW);
  1258. npdialog->show_dialog();
  1259. }
  1260. void ProjectManager::_import_project() {
  1261. npdialog->set_mode(ProjectDialog::MODE_IMPORT);
  1262. npdialog->show_dialog();
  1263. }
  1264. void ProjectManager::_rename_project() {
  1265. if (selected_list.size() == 0) {
  1266. return;
  1267. }
  1268. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1269. const String &selected = E->key();
  1270. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1271. npdialog->set_project_path(path);
  1272. npdialog->set_mode(ProjectDialog::MODE_RENAME);
  1273. npdialog->show_dialog();
  1274. }
  1275. }
  1276. void ProjectManager::_erase_project_confirm() {
  1277. if (selected_list.size() == 0) {
  1278. return;
  1279. }
  1280. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1281. EditorSettings::get_singleton()->erase("projects/" + E->key());
  1282. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  1283. }
  1284. EditorSettings::get_singleton()->save();
  1285. selected_list.clear();
  1286. last_clicked = "";
  1287. _load_recent_projects();
  1288. }
  1289. void ProjectManager::_erase_project() {
  1290. if (selected_list.size() == 0)
  1291. return;
  1292. erase_ask->set_text(TTR("Remove project from the list? (Folder contents will not be modified)"));
  1293. erase_ask->popup_centered_minsize();
  1294. }
  1295. void ProjectManager::_language_selected(int p_id) {
  1296. String lang = language_btn->get_item_metadata(p_id);
  1297. EditorSettings::get_singleton()->set("interface/editor/editor_language", lang);
  1298. language_btn->set_text(lang);
  1299. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1300. language_restart_ask->set_text(TTR("Language changed.\nThe UI will update next time the editor or project manager starts."));
  1301. language_restart_ask->popup_centered();
  1302. }
  1303. void ProjectManager::_restart_confirm() {
  1304. List<String> args = OS::get_singleton()->get_cmdline_args();
  1305. String exec = OS::get_singleton()->get_executable_path();
  1306. OS::ProcessID pid = 0;
  1307. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1308. ERR_FAIL_COND(err);
  1309. get_tree()->quit();
  1310. }
  1311. void ProjectManager::_exit_dialog() {
  1312. get_tree()->quit();
  1313. }
  1314. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  1315. npdialog->set_mode(ProjectDialog::MODE_INSTALL);
  1316. npdialog->set_zip_path(p_zip_path);
  1317. npdialog->set_zip_title(p_title);
  1318. npdialog->show_dialog();
  1319. }
  1320. void ProjectManager::_files_dropped(PoolStringArray p_files, int p_screen) {
  1321. Set<String> folders_set;
  1322. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1323. for (int i = 0; i < p_files.size(); i++) {
  1324. String file = p_files[i];
  1325. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  1326. }
  1327. memdelete(da);
  1328. if (folders_set.size() > 0) {
  1329. PoolStringArray folders;
  1330. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  1331. folders.append(E->get());
  1332. }
  1333. bool confirm = true;
  1334. if (folders.size() == 1) {
  1335. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1336. if (dir->change_dir(folders[0]) == OK) {
  1337. dir->list_dir_begin();
  1338. String file = dir->get_next();
  1339. while (confirm && file != String()) {
  1340. if (!dir->current_is_dir() && file.ends_with("project.godot")) {
  1341. confirm = false;
  1342. }
  1343. file = dir->get_next();
  1344. }
  1345. dir->list_dir_end();
  1346. }
  1347. memdelete(dir);
  1348. }
  1349. if (confirm) {
  1350. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  1351. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  1352. multi_scan_ask->set_text(vformat(TTR("You are about the scan %s folders for existing Godot projects. Do you confirm?"), folders.size()));
  1353. multi_scan_ask->popup_centered_minsize();
  1354. } else {
  1355. _scan_multiple_folders(folders);
  1356. }
  1357. }
  1358. }
  1359. void ProjectManager::_scan_multiple_folders(PoolStringArray p_files) {
  1360. for (int i = 0; i < p_files.size(); i++) {
  1361. _scan_begin(p_files.get(i));
  1362. }
  1363. }
  1364. void ProjectManager::_bind_methods() {
  1365. ClassDB::bind_method("_open_selected_projects_ask", &ProjectManager::_open_selected_projects_ask);
  1366. ClassDB::bind_method("_open_selected_projects", &ProjectManager::_open_selected_projects);
  1367. ClassDB::bind_method("_run_project", &ProjectManager::_run_project);
  1368. ClassDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  1369. ClassDB::bind_method("_show_project", &ProjectManager::_show_project);
  1370. ClassDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  1371. ClassDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  1372. ClassDB::bind_method("_import_project", &ProjectManager::_import_project);
  1373. ClassDB::bind_method("_new_project", &ProjectManager::_new_project);
  1374. ClassDB::bind_method("_rename_project", &ProjectManager::_rename_project);
  1375. ClassDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  1376. ClassDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  1377. ClassDB::bind_method("_language_selected", &ProjectManager::_language_selected);
  1378. ClassDB::bind_method("_restart_confirm", &ProjectManager::_restart_confirm);
  1379. ClassDB::bind_method("_exit_dialog", &ProjectManager::_exit_dialog);
  1380. ClassDB::bind_method("_load_recent_projects", &ProjectManager::_load_recent_projects);
  1381. ClassDB::bind_method("_on_projects_updated", &ProjectManager::_on_projects_updated);
  1382. ClassDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  1383. ClassDB::bind_method("_update_scroll_position", &ProjectManager::_update_scroll_position);
  1384. ClassDB::bind_method("_panel_draw", &ProjectManager::_panel_draw);
  1385. ClassDB::bind_method("_panel_input", &ProjectManager::_panel_input);
  1386. ClassDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  1387. ClassDB::bind_method("_favorite_pressed", &ProjectManager::_favorite_pressed);
  1388. ClassDB::bind_method("_install_project", &ProjectManager::_install_project);
  1389. ClassDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  1390. ClassDB::bind_method("_open_asset_library", &ProjectManager::_open_asset_library);
  1391. ClassDB::bind_method("_confirm_update_settings", &ProjectManager::_confirm_update_settings);
  1392. ClassDB::bind_method(D_METHOD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  1393. }
  1394. void ProjectManager::_open_asset_library() {
  1395. asset_library->disable_community_support();
  1396. tabs->set_current_tab(1);
  1397. }
  1398. ProjectManager::ProjectManager() {
  1399. // load settings
  1400. if (!EditorSettings::get_singleton())
  1401. EditorSettings::create();
  1402. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  1403. {
  1404. int display_scale = EditorSettings::get_singleton()->get("interface/editor/display_scale");
  1405. float custom_display_scale = EditorSettings::get_singleton()->get("interface/editor/custom_display_scale");
  1406. switch (display_scale) {
  1407. case 0: {
  1408. // Try applying a suitable display scale automatically
  1409. const int screen = OS::get_singleton()->get_current_screen();
  1410. editor_set_scale(OS::get_singleton()->get_screen_dpi(screen) >= 192 && OS::get_singleton()->get_screen_size(screen).x > 2000 ? 2.0 : 1.0);
  1411. } break;
  1412. case 1: {
  1413. editor_set_scale(0.75);
  1414. } break;
  1415. case 2: {
  1416. editor_set_scale(1.0);
  1417. } break;
  1418. case 3: {
  1419. editor_set_scale(1.25);
  1420. } break;
  1421. case 4: {
  1422. editor_set_scale(1.5);
  1423. } break;
  1424. case 5: {
  1425. editor_set_scale(1.75);
  1426. } break;
  1427. case 6: {
  1428. editor_set_scale(2.0);
  1429. } break;
  1430. default: {
  1431. editor_set_scale(custom_display_scale);
  1432. } break;
  1433. }
  1434. #ifndef OSX_ENABLED
  1435. // The macOS platform implementation uses its own hiDPI window resizing code
  1436. // TODO: Resize windows on hiDPI displays on Windows and Linux and remove the line below
  1437. OS::get_singleton()->set_window_size(OS::get_singleton()->get_window_size() * MAX(1, EDSCALE));
  1438. #endif
  1439. }
  1440. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  1441. set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1442. set_theme(create_editor_theme());
  1443. gui_base = memnew(Control);
  1444. add_child(gui_base);
  1445. gui_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1446. gui_base->set_theme(create_custom_theme());
  1447. Panel *panel = memnew(Panel);
  1448. gui_base->add_child(panel);
  1449. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1450. VBoxContainer *vb = memnew(VBoxContainer);
  1451. panel->add_child(vb);
  1452. vb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 20 * EDSCALE);
  1453. vb->set_margin(MARGIN_TOP, 4 * EDSCALE);
  1454. vb->set_margin(MARGIN_BOTTOM, -4 * EDSCALE);
  1455. vb->add_constant_override("separation", 15 * EDSCALE);
  1456. String cp;
  1457. cp += 0xA9;
  1458. OS::get_singleton()->set_window_title(VERSION_NAME + String(" - ") + TTR("Project Manager") + " - " + cp + " 2007-2019 Juan Linietsky, Ariel Manzur & Godot Contributors");
  1459. HBoxContainer *top_hb = memnew(HBoxContainer);
  1460. vb->add_child(top_hb);
  1461. CenterContainer *ccl = memnew(CenterContainer);
  1462. Label *l = memnew(Label);
  1463. l->set_text(VERSION_NAME + String(" - ") + TTR("Project Manager"));
  1464. ccl->add_child(l);
  1465. top_hb->add_child(ccl);
  1466. top_hb->add_spacer();
  1467. l = memnew(Label);
  1468. String hash = String(VERSION_HASH);
  1469. if (hash.length() != 0)
  1470. hash = "." + hash.left(7);
  1471. l->set_text("v" VERSION_FULL_BUILD "" + hash);
  1472. l->set_align(Label::ALIGN_CENTER);
  1473. top_hb->add_child(l);
  1474. Control *center_box = memnew(Control);
  1475. center_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1476. vb->add_child(center_box);
  1477. tabs = memnew(TabContainer);
  1478. center_box->add_child(tabs);
  1479. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1480. HBoxContainer *tree_hb = memnew(HBoxContainer);
  1481. projects_hb = tree_hb;
  1482. projects_hb->set_name(TTR("Project List"));
  1483. tabs->add_child(tree_hb);
  1484. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  1485. tree_hb->add_child(search_tree_vb);
  1486. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1487. HBoxContainer *sort_filters = memnew(HBoxContainer);
  1488. Label *sort_label = memnew(Label);
  1489. sort_label->set_text(TTR("Sort:"));
  1490. sort_filters->add_child(sort_label);
  1491. Vector<String> sort_filter_titles;
  1492. sort_filter_titles.push_back("Name");
  1493. sort_filter_titles.push_back("Last Modified");
  1494. project_order_filter = memnew(ProjectListFilter);
  1495. project_order_filter->_setup_filters(sort_filter_titles);
  1496. project_order_filter->set_filter_size(150);
  1497. sort_filters->add_child(project_order_filter);
  1498. project_order_filter->connect("filter_changed", this, "_load_recent_projects");
  1499. project_order_filter->set_custom_minimum_size(Size2(180, 10) * EDSCALE);
  1500. int projects_sorting_order = (int)EditorSettings::get_singleton()->get("project_manager/sorting_order");
  1501. project_order_filter->set_filter_option((ProjectListFilter::FilterOption)projects_sorting_order);
  1502. sort_filters->add_spacer(true);
  1503. Label *search_label = memnew(Label);
  1504. search_label->set_text(TTR("Search:"));
  1505. sort_filters->add_child(search_label);
  1506. HBoxContainer *search_filters = memnew(HBoxContainer);
  1507. Vector<String> vec2;
  1508. vec2.push_back("Name");
  1509. vec2.push_back("Path");
  1510. project_filter = memnew(ProjectListFilter);
  1511. project_filter->_setup_filters(vec2);
  1512. project_filter->add_search_box();
  1513. search_filters->add_child(project_filter);
  1514. project_filter->connect("filter_changed", this, "_load_recent_projects");
  1515. project_filter->set_custom_minimum_size(Size2(280, 10) * EDSCALE);
  1516. sort_filters->add_child(search_filters);
  1517. search_tree_vb->add_child(sort_filters);
  1518. PanelContainer *pc = memnew(PanelContainer);
  1519. pc->add_style_override("panel", gui_base->get_stylebox("bg", "Tree"));
  1520. search_tree_vb->add_child(pc);
  1521. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  1522. scroll = memnew(ScrollContainer);
  1523. pc->add_child(scroll);
  1524. scroll->set_enable_h_scroll(false);
  1525. VBoxContainer *tree_vb = memnew(VBoxContainer);
  1526. tree_hb->add_child(tree_vb);
  1527. scroll_children = memnew(VBoxContainer);
  1528. scroll_children->set_h_size_flags(SIZE_EXPAND_FILL);
  1529. scroll->add_child(scroll_children);
  1530. Button *open = memnew(Button);
  1531. open->set_text(TTR("Edit"));
  1532. tree_vb->add_child(open);
  1533. open->connect("pressed", this, "_open_selected_projects_ask");
  1534. open_btn = open;
  1535. Button *run = memnew(Button);
  1536. run->set_text(TTR("Run"));
  1537. tree_vb->add_child(run);
  1538. run->connect("pressed", this, "_run_project");
  1539. run_btn = run;
  1540. tree_vb->add_child(memnew(HSeparator));
  1541. Button *scan = memnew(Button);
  1542. scan->set_text(TTR("Scan"));
  1543. tree_vb->add_child(scan);
  1544. scan->connect("pressed", this, "_scan_projects");
  1545. tree_vb->add_child(memnew(HSeparator));
  1546. scan_dir = memnew(FileDialog);
  1547. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  1548. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  1549. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  1550. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  1551. gui_base->add_child(scan_dir);
  1552. scan_dir->connect("dir_selected", this, "_scan_begin");
  1553. Button *create = memnew(Button);
  1554. create->set_text(TTR("New Project"));
  1555. tree_vb->add_child(create);
  1556. create->connect("pressed", this, "_new_project");
  1557. Button *import = memnew(Button);
  1558. import->set_text(TTR("Import"));
  1559. tree_vb->add_child(import);
  1560. import->connect("pressed", this, "_import_project");
  1561. Button *rename = memnew(Button);
  1562. rename->set_text(TTR("Rename"));
  1563. tree_vb->add_child(rename);
  1564. rename->connect("pressed", this, "_rename_project");
  1565. rename_btn = rename;
  1566. Button *erase = memnew(Button);
  1567. erase->set_text(TTR("Remove"));
  1568. tree_vb->add_child(erase);
  1569. erase->connect("pressed", this, "_erase_project");
  1570. erase_btn = erase;
  1571. tree_vb->add_spacer();
  1572. if (StreamPeerSSL::is_available()) {
  1573. asset_library = memnew(EditorAssetLibrary(true));
  1574. asset_library->set_name(TTR("Templates"));
  1575. tabs->add_child(asset_library);
  1576. asset_library->connect("install_asset", this, "_install_project");
  1577. } else {
  1578. WARN_PRINT("Asset Library not available, as it requires SSL to work.");
  1579. }
  1580. HBoxContainer *settings_hb = memnew(HBoxContainer);
  1581. settings_hb->set_alignment(BoxContainer::ALIGN_END);
  1582. settings_hb->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  1583. language_btn = memnew(OptionButton);
  1584. Vector<String> editor_languages;
  1585. List<PropertyInfo> editor_settings_properties;
  1586. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  1587. for (List<PropertyInfo>::Element *E = editor_settings_properties.front(); E; E = E->next()) {
  1588. PropertyInfo &pi = E->get();
  1589. if (pi.name == "interface/editor/editor_language") {
  1590. editor_languages = pi.hint_string.split(",");
  1591. }
  1592. }
  1593. String current_lang = EditorSettings::get_singleton()->get("interface/editor/editor_language");
  1594. for (int i = 0; i < editor_languages.size(); i++) {
  1595. String lang = editor_languages[i];
  1596. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang);
  1597. language_btn->add_item(lang_name + " [" + lang + "]", i);
  1598. language_btn->set_item_metadata(i, lang);
  1599. if (current_lang == lang) {
  1600. language_btn->select(i);
  1601. language_btn->set_text(lang);
  1602. }
  1603. }
  1604. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1605. settings_hb->add_child(language_btn);
  1606. language_btn->connect("item_selected", this, "_language_selected");
  1607. center_box->add_child(settings_hb);
  1608. settings_hb->set_anchors_and_margins_preset(Control::PRESET_TOP_RIGHT);
  1609. CenterContainer *cc = memnew(CenterContainer);
  1610. Button *cancel = memnew(Button);
  1611. cancel->set_text(TTR("Exit"));
  1612. cancel->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1613. cc->add_child(cancel);
  1614. cancel->connect("pressed", this, "_exit_dialog");
  1615. vb->add_child(cc);
  1616. //////////////////////////////////////////////////////////////
  1617. language_restart_ask = memnew(ConfirmationDialog);
  1618. language_restart_ask->get_ok()->set_text(TTR("Restart Now"));
  1619. language_restart_ask->get_ok()->connect("pressed", this, "_restart_confirm");
  1620. language_restart_ask->get_cancel()->set_text(TTR("Continue"));
  1621. gui_base->add_child(language_restart_ask);
  1622. erase_ask = memnew(ConfirmationDialog);
  1623. erase_ask->get_ok()->set_text(TTR("Remove"));
  1624. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  1625. gui_base->add_child(erase_ask);
  1626. multi_open_ask = memnew(ConfirmationDialog);
  1627. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  1628. multi_open_ask->get_ok()->connect("pressed", this, "_open_selected_projects");
  1629. gui_base->add_child(multi_open_ask);
  1630. multi_run_ask = memnew(ConfirmationDialog);
  1631. multi_run_ask->get_ok()->set_text(TTR("Run"));
  1632. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  1633. gui_base->add_child(multi_run_ask);
  1634. multi_scan_ask = memnew(ConfirmationDialog);
  1635. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  1636. gui_base->add_child(multi_scan_ask);
  1637. ask_update_settings = memnew(ConfirmationDialog);
  1638. ask_update_settings->get_ok()->connect("pressed", this, "_confirm_update_settings");
  1639. gui_base->add_child(ask_update_settings);
  1640. OS::get_singleton()->set_low_processor_usage_mode(true);
  1641. npdialog = memnew(ProjectDialog);
  1642. gui_base->add_child(npdialog);
  1643. npdialog->connect("projects_updated", this, "_on_projects_updated");
  1644. npdialog->connect("project_created", this, "_on_project_created");
  1645. _load_recent_projects();
  1646. if (EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path")) {
  1647. _scan_begin(EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path"));
  1648. }
  1649. last_clicked = "";
  1650. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  1651. run_error_diag = memnew(AcceptDialog);
  1652. gui_base->add_child(run_error_diag);
  1653. run_error_diag->set_title(TTR("Can't run project"));
  1654. dialog_error = memnew(AcceptDialog);
  1655. gui_base->add_child(dialog_error);
  1656. open_templates = memnew(ConfirmationDialog);
  1657. open_templates->set_text(TTR("You don't currently have any projects.\nWould you like to explore the official example projects in the Asset Library?"));
  1658. open_templates->get_ok()->set_text(TTR("Open Asset Library"));
  1659. open_templates->connect("confirmed", this, "_open_asset_library");
  1660. add_child(open_templates);
  1661. }
  1662. ProjectManager::~ProjectManager() {
  1663. if (EditorSettings::get_singleton())
  1664. EditorSettings::destroy();
  1665. }
  1666. void ProjectListFilter::_setup_filters(Vector<String> options) {
  1667. filter_option->clear();
  1668. for (int i = 0; i < options.size(); i++)
  1669. filter_option->add_item(TTR(options[i]));
  1670. }
  1671. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  1672. emit_signal("filter_changed");
  1673. }
  1674. String ProjectListFilter::get_search_term() {
  1675. return search_box->get_text().strip_edges();
  1676. }
  1677. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  1678. return _current_filter;
  1679. }
  1680. void ProjectListFilter::set_filter_option(FilterOption option) {
  1681. filter_option->select((int)option);
  1682. _filter_option_selected(0);
  1683. }
  1684. void ProjectListFilter::_filter_option_selected(int p_idx) {
  1685. FilterOption selected = (FilterOption)(filter_option->get_selected());
  1686. if (_current_filter != selected) {
  1687. _current_filter = selected;
  1688. emit_signal("filter_changed");
  1689. }
  1690. }
  1691. void ProjectListFilter::_notification(int p_what) {
  1692. if (p_what == NOTIFICATION_ENTER_TREE && has_search_box) {
  1693. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  1694. search_box->set_clear_button_enabled(true);
  1695. }
  1696. }
  1697. void ProjectListFilter::_bind_methods() {
  1698. ClassDB::bind_method(D_METHOD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  1699. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  1700. ADD_SIGNAL(MethodInfo("filter_changed"));
  1701. }
  1702. void ProjectListFilter::add_search_box() {
  1703. search_box = memnew(LineEdit);
  1704. search_box->connect("text_changed", this, "_search_text_changed");
  1705. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1706. add_child(search_box);
  1707. has_search_box = true;
  1708. }
  1709. void ProjectListFilter::set_filter_size(int h_size) {
  1710. filter_option->set_custom_minimum_size(Size2(h_size * EDSCALE, 10 * EDSCALE));
  1711. }
  1712. ProjectListFilter::ProjectListFilter() {
  1713. _current_filter = FILTER_NAME;
  1714. filter_option = memnew(OptionButton);
  1715. set_filter_size(80);
  1716. filter_option->set_clip_text(true);
  1717. filter_option->connect("item_selected", this, "_filter_option_selected");
  1718. add_child(filter_option);
  1719. has_search_box = false;
  1720. }