project_manager.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /*************************************************************************/
  2. /* project_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_initialize_ssl.h"
  32. #include "editor_scale.h"
  33. #include "editor_settings.h"
  34. #include "editor_themes.h"
  35. #include "io/config_file.h"
  36. #include "io/resource_saver.h"
  37. #include "io/stream_peer_ssl.h"
  38. #include "io/zip_io.h"
  39. #include "os/dir_access.h"
  40. #include "os/file_access.h"
  41. #include "os/keyboard.h"
  42. #include "os/os.h"
  43. #include "scene/gui/center_container.h"
  44. #include "scene/gui/line_edit.h"
  45. #include "scene/gui/margin_container.h"
  46. #include "scene/gui/panel_container.h"
  47. #include "scene/gui/separator.h"
  48. #include "scene/gui/texture_rect.h"
  49. #include "scene/gui/tool_button.h"
  50. #include "translation.h"
  51. #include "version.h"
  52. #include "version_hash.gen.h"
  53. class ProjectDialog : public ConfirmationDialog {
  54. GDCLASS(ProjectDialog, ConfirmationDialog);
  55. public:
  56. enum Mode {
  57. MODE_NEW,
  58. MODE_IMPORT,
  59. MODE_INSTALL,
  60. MODE_RENAME
  61. };
  62. private:
  63. enum MessageType {
  64. MESSAGE_ERROR,
  65. MESSAGE_WARNING,
  66. MESSAGE_SUCCESS
  67. };
  68. Mode mode;
  69. Button *browse;
  70. Button *create_dir;
  71. Container *name_container;
  72. Container *path_container;
  73. Label *msg;
  74. LineEdit *project_path;
  75. LineEdit *project_name;
  76. ToolButton *status_btn;
  77. FileDialog *fdialog;
  78. String zip_path;
  79. String zip_title;
  80. AcceptDialog *dialog_error;
  81. String fav_dir;
  82. String created_folder_path;
  83. void set_message(const String &p_msg, MessageType p_type = MESSAGE_SUCCESS) {
  84. msg->set_text(p_msg);
  85. if (p_msg == "") {
  86. status_btn->set_icon(get_icon("StatusSuccess", "EditorIcons"));
  87. return;
  88. }
  89. msg->hide();
  90. switch (p_type) {
  91. case MESSAGE_ERROR:
  92. msg->add_color_override("font_color", get_color("error_color", "Editor"));
  93. status_btn->set_icon(get_icon("StatusError", "EditorIcons"));
  94. msg->show();
  95. break;
  96. case MESSAGE_WARNING:
  97. msg->add_color_override("font_color", get_color("warning_color", "Editor"));
  98. status_btn->set_icon(get_icon("StatusWarning", "EditorIcons"));
  99. break;
  100. case MESSAGE_SUCCESS:
  101. msg->add_color_override("font_color", get_color("success_color", "Editor"));
  102. status_btn->set_icon(get_icon("StatusSuccess", "EditorIcons"));
  103. break;
  104. }
  105. }
  106. String _test_path() {
  107. set_message(" ");
  108. get_ok()->set_disabled(true);
  109. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  110. String valid_path;
  111. if (d->change_dir(project_path->get_text()) == OK) {
  112. valid_path = project_path->get_text();
  113. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  114. valid_path = project_path->get_text().strip_edges();
  115. }
  116. if (valid_path == "") {
  117. set_message(TTR("The path does not exist."), MESSAGE_ERROR);
  118. memdelete(d);
  119. return "";
  120. }
  121. if (mode == MODE_IMPORT || mode == MODE_RENAME) {
  122. if (valid_path != "" && !d->file_exists("project.godot")) {
  123. set_message(TTR("Please choose a 'project.godot' file."), MESSAGE_ERROR);
  124. memdelete(d);
  125. return "";
  126. }
  127. } else if (mode == MODE_NEW) {
  128. // check if the specified folder is empty, even though this is not an error, it is good to check here
  129. d->list_dir_begin();
  130. bool is_empty = true;
  131. String n = d->get_next();
  132. while (n != String()) {
  133. if (!n.begins_with(".")) { // i dont know if this is enough to guarantee an empty dir
  134. is_empty = false;
  135. break;
  136. }
  137. n = d->get_next();
  138. }
  139. d->list_dir_end();
  140. if (!is_empty) {
  141. set_message(TTR("Your project will be created in a non empty folder (you might want to create a new folder)."), MESSAGE_WARNING);
  142. }
  143. } else {
  144. if (d->file_exists("project.godot")) {
  145. set_message(TTR("Please choose a folder that does not contain a 'project.godot' file."), MESSAGE_ERROR);
  146. memdelete(d);
  147. return "";
  148. }
  149. }
  150. memdelete(d);
  151. get_ok()->set_disabled(false);
  152. return valid_path;
  153. }
  154. void _path_text_changed(const String &p_path) {
  155. String sp = _test_path();
  156. if (sp != "") {
  157. // set the project name to the select folder name
  158. if (project_name->get_text() == "") {
  159. sp = sp.replace("\\", "/");
  160. int lidx = sp.find_last("/");
  161. if (lidx != -1) {
  162. sp = sp.substr(lidx + 1, sp.length());
  163. }
  164. if (sp == "" && mode == MODE_IMPORT)
  165. sp = TTR("Imported Project");
  166. project_name->set_text(sp);
  167. }
  168. }
  169. if (created_folder_path != "" && created_folder_path != p_path) {
  170. _remove_created_folder();
  171. }
  172. }
  173. void _file_selected(const String &p_path) {
  174. String p = p_path;
  175. if (mode == MODE_IMPORT) {
  176. if (p.ends_with("project.godot")) {
  177. p = p.get_base_dir();
  178. get_ok()->set_disabled(false);
  179. } else {
  180. set_message(TTR("Please choose a 'project.godot' file."), MESSAGE_ERROR);
  181. get_ok()->set_disabled(true);
  182. return;
  183. }
  184. }
  185. String sp = p.simplify_path();
  186. project_path->set_text(sp);
  187. set_message(TTR(" ")); // just so it does not disappear
  188. get_ok()->call_deferred("grab_focus");
  189. }
  190. void _path_selected(const String &p_path) {
  191. String p = p_path;
  192. String sp = p.simplify_path();
  193. project_path->set_text(sp);
  194. get_ok()->call_deferred("grab_focus");
  195. }
  196. void _browse_path() {
  197. fdialog->set_current_dir(project_path->get_text());
  198. if (mode == MODE_IMPORT) {
  199. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  200. fdialog->clear_filters();
  201. fdialog->add_filter("project.godot ; " _MKSTR(VERSION_NAME) " Project");
  202. } else {
  203. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  204. }
  205. fdialog->popup_centered_ratio();
  206. }
  207. void _create_folder() {
  208. if (project_name->get_text() == "" || created_folder_path != "") {
  209. return;
  210. }
  211. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  212. if (d->change_dir(project_path->get_text()) == OK) {
  213. if (!d->dir_exists(project_name->get_text())) {
  214. if (d->make_dir(project_name->get_text()) == OK) {
  215. d->change_dir(project_name->get_text());
  216. project_path->set_text(d->get_current_dir());
  217. created_folder_path = d->get_current_dir();
  218. create_dir->set_disabled(true);
  219. }
  220. }
  221. }
  222. memdelete(d);
  223. }
  224. void _text_changed(const String &p_text) {
  225. if (mode != MODE_NEW)
  226. return;
  227. _test_path();
  228. if (p_text == "")
  229. set_message(TTR("It would be a good idea to name your project."), MESSAGE_WARNING);
  230. }
  231. void ok_pressed() {
  232. String dir = project_path->get_text();
  233. if (mode == MODE_RENAME) {
  234. String dir = _test_path();
  235. if (dir == "") {
  236. set_message(TTR("Invalid project path (changed anything?)."), MESSAGE_ERROR);
  237. return;
  238. }
  239. ProjectSettings *current = memnew(ProjectSettings);
  240. current->add_singleton(ProjectSettings::Singleton("Current"));
  241. if (current->setup(dir, "")) {
  242. set_message(TTR("Couldn't get project.godot in project path."), MESSAGE_ERROR);
  243. } else {
  244. ProjectSettings::CustomMap edited_settings;
  245. edited_settings["application/config/name"] = project_name->get_text();
  246. if (current->save_custom(dir.plus_file("/project.godot"), edited_settings, Vector<String>(), true)) {
  247. set_message(TTR("Couldn't edit project.godot in project path."), MESSAGE_ERROR);
  248. }
  249. }
  250. hide();
  251. emit_signal("project_renamed");
  252. } else {
  253. if (mode == MODE_IMPORT) {
  254. // nothing to do
  255. } else {
  256. if (mode == MODE_NEW) {
  257. ProjectSettings::CustomMap initial_settings;
  258. initial_settings["application/config/name"] = project_name->get_text();
  259. initial_settings["application/config/icon"] = "res://icon.png";
  260. initial_settings["rendering/environment/default_environment"] = "res://default_env.tres";
  261. if (ProjectSettings::get_singleton()->save_custom(dir.plus_file("/project.godot"), initial_settings, Vector<String>(), false)) {
  262. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  263. } else {
  264. ResourceSaver::save(dir.plus_file("/icon.png"), get_icon("DefaultProjectIcon", "EditorIcons"));
  265. FileAccess *f = FileAccess::open(dir.plus_file("/default_env.tres"), FileAccess::WRITE);
  266. if (!f) {
  267. set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  268. } else {
  269. f->store_line("[gd_resource type=\"Environment\" load_steps=2 format=2]");
  270. f->store_line("[sub_resource type=\"ProceduralSky\" id=1]");
  271. f->store_line("[resource]");
  272. f->store_line("background_mode = 2");
  273. f->store_line("background_sky = SubResource( 1 )");
  274. memdelete(f);
  275. }
  276. }
  277. } else if (mode == MODE_INSTALL) {
  278. FileAccess *src_f = NULL;
  279. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  280. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  281. if (!pkg) {
  282. dialog_error->set_text(TTR("Error opening package file, not in zip format."));
  283. return;
  284. }
  285. int ret = unzGoToFirstFile(pkg);
  286. Vector<String> failed_files;
  287. int idx = 0;
  288. while (ret == UNZ_OK) {
  289. //get filename
  290. unz_file_info info;
  291. char fname[16384];
  292. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  293. String path = fname;
  294. int depth = 1; //stuff from github comes with tag
  295. bool skip = false;
  296. while (depth > 0) {
  297. int pp = path.find("/");
  298. if (pp == -1) {
  299. skip = true;
  300. break;
  301. }
  302. path = path.substr(pp + 1, path.length());
  303. depth--;
  304. }
  305. if (skip || path == String()) {
  306. //
  307. } else if (path.ends_with("/")) { // a dir
  308. path = path.substr(0, path.length() - 1);
  309. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  310. da->make_dir(dir.plus_file(path));
  311. memdelete(da);
  312. } else {
  313. Vector<uint8_t> data;
  314. data.resize(info.uncompressed_size);
  315. //read
  316. unzOpenCurrentFile(pkg);
  317. unzReadCurrentFile(pkg, data.ptr(), data.size());
  318. unzCloseCurrentFile(pkg);
  319. FileAccess *f = FileAccess::open(dir.plus_file(path), FileAccess::WRITE);
  320. if (f) {
  321. f->store_buffer(data.ptr(), data.size());
  322. memdelete(f);
  323. } else {
  324. failed_files.push_back(path);
  325. }
  326. }
  327. idx++;
  328. ret = unzGoToNextFile(pkg);
  329. }
  330. unzClose(pkg);
  331. if (failed_files.size()) {
  332. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  333. for (int i = 0; i < failed_files.size(); i++) {
  334. if (i > 15) {
  335. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  336. break;
  337. }
  338. msg += failed_files[i] + "\n";
  339. }
  340. dialog_error->set_text(msg);
  341. dialog_error->popup_centered_minsize();
  342. } else {
  343. dialog_error->set_text(TTR("Package Installed Successfully!"));
  344. dialog_error->popup_centered_minsize();
  345. }
  346. }
  347. }
  348. dir = dir.replace("\\", "/");
  349. if (dir.ends_with("/"))
  350. dir = dir.substr(0, dir.length() - 1);
  351. String proj = dir.replace("/", "::");
  352. EditorSettings::get_singleton()->set("projects/" + proj, dir);
  353. EditorSettings::get_singleton()->save();
  354. hide();
  355. emit_signal("project_created", dir);
  356. }
  357. }
  358. void _remove_created_folder() {
  359. if (created_folder_path != "") {
  360. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  361. d->remove(created_folder_path);
  362. memdelete(d);
  363. create_dir->set_disabled(false);
  364. created_folder_path = "";
  365. }
  366. }
  367. void _toggle_message() {
  368. msg->set_visible(!msg->is_visible());
  369. }
  370. void cancel_pressed() {
  371. _remove_created_folder();
  372. project_path->clear();
  373. project_name->clear();
  374. }
  375. protected:
  376. static void _bind_methods() {
  377. ClassDB::bind_method("_browse_path", &ProjectDialog::_browse_path);
  378. ClassDB::bind_method("_create_folder", &ProjectDialog::_create_folder);
  379. ClassDB::bind_method("_text_changed", &ProjectDialog::_text_changed);
  380. ClassDB::bind_method("_path_text_changed", &ProjectDialog::_path_text_changed);
  381. ClassDB::bind_method("_path_selected", &ProjectDialog::_path_selected);
  382. ClassDB::bind_method("_file_selected", &ProjectDialog::_file_selected);
  383. ClassDB::bind_method("_toggle_message", &ProjectDialog::_toggle_message);
  384. ADD_SIGNAL(MethodInfo("project_created"));
  385. ADD_SIGNAL(MethodInfo("project_renamed"));
  386. }
  387. public:
  388. void set_zip_path(const String &p_path) {
  389. zip_path = p_path;
  390. }
  391. void set_zip_title(const String &p_title) {
  392. zip_title = p_title;
  393. }
  394. void set_mode(Mode p_mode) {
  395. mode = p_mode;
  396. }
  397. void set_project_path(const String &p_path) {
  398. project_path->set_text(p_path);
  399. }
  400. void show_dialog() {
  401. if (mode == MODE_RENAME) {
  402. project_path->set_editable(false);
  403. browse->hide();
  404. set_title(TTR("Rename Project"));
  405. get_ok()->set_text(TTR("Rename"));
  406. name_container->show();
  407. ProjectSettings *current = memnew(ProjectSettings);
  408. current->add_singleton(ProjectSettings::Singleton("Current"));
  409. if (current->setup(project_path->get_text(), "")) {
  410. set_message(TTR("Couldn't get project.godot in the project path."), MESSAGE_ERROR);
  411. } else if (current->has_setting("application/config/name")) {
  412. project_name->set_text(current->get("application/config/name"));
  413. }
  414. project_name->grab_focus();
  415. create_dir->hide();
  416. status_btn->hide();
  417. } else {
  418. fav_dir = EditorSettings::get_singleton()->get("filesystem/directories/default_project_path");
  419. if (fav_dir != "") {
  420. project_path->set_text(fav_dir);
  421. fdialog->set_current_dir(fav_dir);
  422. } else {
  423. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  424. project_path->set_text(d->get_current_dir());
  425. fdialog->set_current_dir(d->get_current_dir());
  426. memdelete(d);
  427. }
  428. project_name->set_text(TTR("New Game Project"));
  429. project_path->set_editable(true);
  430. browse->set_disabled(false);
  431. browse->show();
  432. create_dir->show();
  433. status_btn->show();
  434. if (mode == MODE_IMPORT) {
  435. set_title(TTR("Import Existing Project"));
  436. get_ok()->set_text(TTR("Import"));
  437. name_container->hide();
  438. project_path->grab_focus();
  439. } else if (mode == MODE_NEW) {
  440. set_title(TTR("Create New Project"));
  441. get_ok()->set_text(TTR("Create"));
  442. name_container->show();
  443. project_name->grab_focus();
  444. } else if (mode == MODE_INSTALL) {
  445. set_title(TTR("Install Project:") + " " + zip_title);
  446. get_ok()->set_text(TTR("Install"));
  447. name_container->hide();
  448. project_path->grab_focus();
  449. }
  450. _test_path();
  451. }
  452. popup_centered(Size2(500, 125) * EDSCALE);
  453. }
  454. ProjectDialog() {
  455. VBoxContainer *vb = memnew(VBoxContainer);
  456. add_child(vb);
  457. name_container = memnew(VBoxContainer);
  458. vb->add_child(name_container);
  459. Label *l = memnew(Label);
  460. l->set_text(TTR("Project Name:"));
  461. name_container->add_child(l);
  462. HBoxContainer *pnhb = memnew(HBoxContainer);
  463. name_container->add_child(pnhb);
  464. project_name = memnew(LineEdit);
  465. project_name->set_h_size_flags(SIZE_EXPAND_FILL);
  466. pnhb->add_child(project_name);
  467. create_dir = memnew(Button);
  468. pnhb->add_child(create_dir);
  469. create_dir->set_text(TTR("Create folder"));
  470. create_dir->connect("pressed", this, "_create_folder");
  471. path_container = memnew(VBoxContainer);
  472. vb->add_child(path_container);
  473. l = memnew(Label);
  474. l->set_text(TTR("Project Path:"));
  475. path_container->add_child(l);
  476. HBoxContainer *pphb = memnew(HBoxContainer);
  477. path_container->add_child(pphb);
  478. project_path = memnew(LineEdit);
  479. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  480. pphb->add_child(project_path);
  481. // status button
  482. status_btn = memnew(ToolButton);
  483. status_btn->connect("pressed", this, "_toggle_message");
  484. pphb->add_child(status_btn);
  485. browse = memnew(Button);
  486. browse->set_text(TTR("Browse"));
  487. browse->connect("pressed", this, "_browse_path");
  488. pphb->add_child(browse);
  489. msg = memnew(Label);
  490. msg->set_text(TTR("That's a BINGO!"));
  491. msg->set_align(Label::ALIGN_CENTER);
  492. msg->hide();
  493. vb->add_child(msg);
  494. fdialog = memnew(FileDialog);
  495. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  496. add_child(fdialog);
  497. project_name->connect("text_changed", this, "_text_changed");
  498. project_path->connect("text_changed", this, "_path_text_changed");
  499. fdialog->connect("dir_selected", this, "_path_selected");
  500. fdialog->connect("file_selected", this, "_file_selected");
  501. set_hide_on_ok(false);
  502. mode = MODE_NEW;
  503. dialog_error = memnew(AcceptDialog);
  504. add_child(dialog_error);
  505. }
  506. };
  507. struct ProjectItem {
  508. String project;
  509. String path;
  510. String conf;
  511. uint64_t last_modified;
  512. bool favorite;
  513. bool grayed;
  514. ProjectItem() {}
  515. ProjectItem(const String &p_project, const String &p_path, const String &p_conf, uint64_t p_last_modified, bool p_favorite = false, bool p_grayed = false) {
  516. project = p_project;
  517. path = p_path;
  518. conf = p_conf;
  519. last_modified = p_last_modified;
  520. favorite = p_favorite;
  521. grayed = p_grayed;
  522. }
  523. _FORCE_INLINE_ bool operator<(const ProjectItem &l) const { return last_modified > l.last_modified; }
  524. _FORCE_INLINE_ bool operator==(const ProjectItem &l) const { return project == l.project; }
  525. };
  526. void ProjectManager::_notification(int p_what) {
  527. if (p_what == NOTIFICATION_ENTER_TREE) {
  528. Engine::get_singleton()->set_editor_hint(false);
  529. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  530. set_process_unhandled_input(is_visible_in_tree());
  531. }
  532. }
  533. void ProjectManager::_panel_draw(Node *p_hb) {
  534. HBoxContainer *hb = Object::cast_to<HBoxContainer>(p_hb);
  535. 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"));
  536. if (selected_list.has(hb->get_meta("name"))) {
  537. hb->draw_style_box(gui_base->get_stylebox("selected", "Tree"), Rect2(Point2(), hb->get_size() - Size2(10, 0) * EDSCALE));
  538. }
  539. }
  540. void ProjectManager::_update_project_buttons() {
  541. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  542. CanvasItem *item = Object::cast_to<CanvasItem>(scroll_childs->get_child(i));
  543. item->update();
  544. }
  545. erase_btn->set_disabled(selected_list.size() < 1);
  546. open_btn->set_disabled(selected_list.size() < 1);
  547. rename_btn->set_disabled(selected_list.size() < 1);
  548. }
  549. void ProjectManager::_panel_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  550. Ref<InputEventMouseButton> mb = p_ev;
  551. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  552. String clicked = p_hb->get_meta("name");
  553. String clicked_main_scene = p_hb->get_meta("main_scene");
  554. if (mb->get_shift() && selected_list.size() > 0 && last_clicked != "" && clicked != last_clicked) {
  555. int clicked_id = -1;
  556. int last_clicked_id = -1;
  557. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  558. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  559. if (!hb) continue;
  560. if (hb->get_meta("name") == clicked) clicked_id = i;
  561. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  562. }
  563. if (last_clicked_id != -1 && clicked_id != -1) {
  564. int min = clicked_id < last_clicked_id ? clicked_id : last_clicked_id;
  565. int max = clicked_id > last_clicked_id ? clicked_id : last_clicked_id;
  566. for (int i = 0; i < scroll_childs->get_child_count(); ++i) {
  567. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  568. if (!hb) continue;
  569. if (i != clicked_id && (i < min || i > max) && !mb->get_control()) {
  570. selected_list.erase(hb->get_meta("name"));
  571. } else if (i >= min && i <= max) {
  572. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  573. }
  574. }
  575. }
  576. } else if (selected_list.has(clicked) && mb->get_control()) {
  577. selected_list.erase(clicked);
  578. } else {
  579. last_clicked = clicked;
  580. if (mb->get_control() || selected_list.size() == 0) {
  581. selected_list.insert(clicked, clicked_main_scene);
  582. } else {
  583. selected_list.clear();
  584. selected_list.insert(clicked, clicked_main_scene);
  585. }
  586. }
  587. _update_project_buttons();
  588. if (mb->is_doubleclick())
  589. _open_project(); //open if doubleclicked
  590. }
  591. }
  592. void ProjectManager::_unhandled_input(const Ref<InputEvent> &p_ev) {
  593. Ref<InputEventKey> k = p_ev;
  594. if (k.is_valid()) {
  595. if (!k->is_pressed())
  596. return;
  597. if (tabs->get_current_tab() != 0)
  598. return;
  599. bool scancode_handled = true;
  600. switch (k->get_scancode()) {
  601. case KEY_ENTER: {
  602. _open_project();
  603. } break;
  604. case KEY_HOME: {
  605. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  606. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  607. if (hb) {
  608. selected_list.clear();
  609. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  610. scroll->set_v_scroll(0);
  611. _update_project_buttons();
  612. break;
  613. }
  614. }
  615. } break;
  616. case KEY_END: {
  617. for (int i = scroll_childs->get_child_count() - 1; i >= 0; i--) {
  618. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  619. if (hb) {
  620. selected_list.clear();
  621. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  622. scroll->set_v_scroll(scroll_childs->get_size().y);
  623. _update_project_buttons();
  624. break;
  625. }
  626. }
  627. } break;
  628. case KEY_UP: {
  629. if (k->get_shift())
  630. break;
  631. if (selected_list.size()) {
  632. bool found = false;
  633. for (int i = scroll_childs->get_child_count() - 1; i >= 0; i--) {
  634. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  635. if (!hb) continue;
  636. String current = hb->get_meta("name");
  637. if (found) {
  638. selected_list.clear();
  639. selected_list.insert(current, hb->get_meta("main_scene"));
  640. int offset_diff = scroll->get_v_scroll() - hb->get_position().y;
  641. if (offset_diff > 0)
  642. scroll->set_v_scroll(scroll->get_v_scroll() - offset_diff);
  643. _update_project_buttons();
  644. break;
  645. } else if (current == selected_list.back()->key()) {
  646. found = true;
  647. }
  648. }
  649. break;
  650. }
  651. // else fallthrough to key_down
  652. }
  653. case KEY_DOWN: {
  654. if (k->get_shift())
  655. break;
  656. bool found = selected_list.empty();
  657. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  658. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  659. if (!hb) continue;
  660. String current = hb->get_meta("name");
  661. if (found) {
  662. selected_list.clear();
  663. selected_list.insert(current, hb->get_meta("main_scene"));
  664. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  665. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  666. if (offset_diff > 0)
  667. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  668. _update_project_buttons();
  669. break;
  670. } else if (current == selected_list.back()->key()) {
  671. found = true;
  672. }
  673. }
  674. } break;
  675. case KEY_F: {
  676. if (k->get_command())
  677. this->project_filter->search_box->grab_focus();
  678. else
  679. scancode_handled = false;
  680. } break;
  681. default: {
  682. scancode_handled = false;
  683. } break;
  684. }
  685. if (scancode_handled) {
  686. accept_event();
  687. }
  688. }
  689. }
  690. void ProjectManager::_favorite_pressed(Node *p_hb) {
  691. String clicked = p_hb->get_meta("name");
  692. bool favorite = !p_hb->get_meta("favorite");
  693. String proj = clicked.replace(":::", ":/");
  694. proj = proj.replace("::", "/");
  695. if (favorite) {
  696. EditorSettings::get_singleton()->set("favorite_projects/" + clicked, proj);
  697. } else {
  698. EditorSettings::get_singleton()->erase("favorite_projects/" + clicked);
  699. }
  700. EditorSettings::get_singleton()->save();
  701. call_deferred("_load_recent_projects");
  702. }
  703. void ProjectManager::_load_recent_projects() {
  704. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  705. String search_term = project_filter->get_search_term();
  706. while (scroll_childs->get_child_count() > 0) {
  707. memdelete(scroll_childs->get_child(0));
  708. }
  709. Map<String, String> selected_list_copy = selected_list;
  710. List<PropertyInfo> properties;
  711. EditorSettings::get_singleton()->get_property_list(&properties);
  712. Color font_color = gui_base->get_color("font_color", "Tree");
  713. List<ProjectItem> projects;
  714. List<ProjectItem> favorite_projects;
  715. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  716. String _name = E->get().name;
  717. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  718. continue;
  719. String path = EditorSettings::get_singleton()->get(_name);
  720. if (filter_option == ProjectListFilter::FILTER_PATH && search_term != "" && path.findn(search_term) == -1)
  721. continue;
  722. String project = _name.get_slice("/", 1);
  723. String conf = path.plus_file("project.godot");
  724. bool favorite = (_name.begins_with("favorite_projects/")) ? true : false;
  725. bool grayed = false;
  726. uint64_t last_modified = 0;
  727. if (FileAccess::exists(conf)) {
  728. last_modified = FileAccess::get_modified_time(conf);
  729. String fscache = path.plus_file(".fscache");
  730. if (FileAccess::exists(fscache)) {
  731. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  732. if (cache_modified > last_modified)
  733. last_modified = cache_modified;
  734. }
  735. } else {
  736. grayed = true;
  737. }
  738. ProjectItem item(project, path, conf, last_modified, favorite, grayed);
  739. if (favorite)
  740. favorite_projects.push_back(item);
  741. else
  742. projects.push_back(item);
  743. }
  744. projects.sort();
  745. favorite_projects.sort();
  746. for (List<ProjectItem>::Element *E = projects.front(); E;) {
  747. List<ProjectItem>::Element *next = E->next();
  748. if (favorite_projects.find(E->get()) != NULL)
  749. projects.erase(E->get());
  750. E = next;
  751. }
  752. for (List<ProjectItem>::Element *E = favorite_projects.back(); E; E = E->prev()) {
  753. projects.push_front(E->get());
  754. }
  755. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  756. for (List<ProjectItem>::Element *E = projects.front(); E; E = E->next()) {
  757. ProjectItem &item = E->get();
  758. String project = item.project;
  759. String path = item.path;
  760. String conf = item.conf;
  761. bool is_favorite = item.favorite;
  762. bool is_grayed = item.grayed;
  763. Ref<ConfigFile> cf = memnew(ConfigFile);
  764. Error cf_err = cf->load(conf);
  765. String project_name = TTR("Unnamed Project");
  766. if (cf_err == OK && cf->has_section_key("application", "config/name")) {
  767. project_name = static_cast<String>(cf->get_value("application", "config/name")).xml_unescape();
  768. }
  769. if (filter_option == ProjectListFilter::FILTER_NAME && search_term != "" && project_name.findn(search_term) == -1)
  770. continue;
  771. Ref<Texture> icon;
  772. if (cf_err == OK && cf->has_section_key("application", "config/icon")) {
  773. String appicon = cf->get_value("application", "config/icon");
  774. if (appicon != "") {
  775. Ref<Image> img;
  776. img.instance();
  777. Error err = img->load(appicon.replace_first("res://", path + "/"));
  778. if (err == OK) {
  779. Ref<Texture> default_icon = get_icon("DefaultProjectIcon", "EditorIcons");
  780. img->resize(default_icon->get_width(), default_icon->get_height());
  781. Ref<ImageTexture> it = memnew(ImageTexture);
  782. it->create_from_image(img);
  783. icon = it;
  784. }
  785. }
  786. }
  787. if (icon.is_null()) {
  788. icon = get_icon("DefaultProjectIcon", "EditorIcons");
  789. }
  790. String main_scene;
  791. if (cf_err == OK && cf->has_section_key("application", "run/main_scene")) {
  792. main_scene = cf->get_value("application", "run/main_scene");
  793. } else {
  794. main_scene = "";
  795. }
  796. selected_list_copy.erase(project);
  797. HBoxContainer *hb = memnew(HBoxContainer);
  798. hb->set_meta("name", project);
  799. hb->set_meta("main_scene", main_scene);
  800. hb->set_meta("favorite", is_favorite);
  801. hb->connect("draw", this, "_panel_draw", varray(hb));
  802. hb->connect("gui_input", this, "_panel_input", varray(hb));
  803. hb->add_constant_override("separation", 10 * EDSCALE);
  804. VBoxContainer *favorite_box = memnew(VBoxContainer);
  805. TextureButton *favorite = memnew(TextureButton);
  806. favorite->set_normal_texture(favorite_icon);
  807. if (!is_favorite)
  808. favorite->set_modulate(Color(1, 1, 1, 0.2));
  809. favorite->set_v_size_flags(SIZE_EXPAND);
  810. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  811. favorite_box->add_child(favorite);
  812. hb->add_child(favorite_box);
  813. TextureRect *tf = memnew(TextureRect);
  814. tf->set_texture(icon);
  815. hb->add_child(tf);
  816. VBoxContainer *vb = memnew(VBoxContainer);
  817. if (is_grayed)
  818. vb->set_modulate(Color(0.5, 0.5, 0.5));
  819. vb->set_name("project");
  820. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  821. hb->add_child(vb);
  822. Control *ec = memnew(Control);
  823. ec->set_custom_minimum_size(Size2(0, 1));
  824. vb->add_child(ec);
  825. Label *title = memnew(Label(project_name));
  826. title->add_font_override("font", gui_base->get_font("large", "Fonts"));
  827. title->add_color_override("font_color", font_color);
  828. title->set_clip_text(true);
  829. vb->add_child(title);
  830. Label *fpath = memnew(Label(path));
  831. fpath->set_name("path");
  832. vb->add_child(fpath);
  833. fpath->set_modulate(Color(1, 1, 1, 0.5));
  834. fpath->add_color_override("font_color", font_color);
  835. fpath->set_clip_text(true);
  836. scroll_childs->add_child(hb);
  837. }
  838. for (Map<String, String>::Element *E = selected_list_copy.front(); E; E = E->next()) {
  839. String key = E->key();
  840. selected_list.erase(key);
  841. }
  842. scroll->set_v_scroll(0);
  843. _update_project_buttons();
  844. EditorSettings::get_singleton()->save();
  845. tabs->set_current_tab(0);
  846. }
  847. void ProjectManager::_on_project_renamed() {
  848. _load_recent_projects();
  849. }
  850. void ProjectManager::_on_project_created(const String &dir) {
  851. bool has_already = false;
  852. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  853. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  854. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path")));
  855. if (fpath->get_text() == dir) {
  856. has_already = true;
  857. break;
  858. }
  859. }
  860. if (has_already) {
  861. _update_scroll_position(dir);
  862. } else {
  863. _load_recent_projects();
  864. _update_scroll_position(dir);
  865. }
  866. _open_project();
  867. }
  868. void ProjectManager::_update_scroll_position(const String &dir) {
  869. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  870. HBoxContainer *hb = Object::cast_to<HBoxContainer>(scroll_childs->get_child(i));
  871. Label *fpath = Object::cast_to<Label>(hb->get_node(NodePath("project/path")));
  872. if (fpath->get_text() == dir) {
  873. last_clicked = hb->get_meta("name");
  874. selected_list.clear();
  875. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  876. _update_project_buttons();
  877. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  878. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  879. if (offset_diff > 0)
  880. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  881. break;
  882. }
  883. }
  884. }
  885. void ProjectManager::_open_project_confirm() {
  886. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  887. const String &selected = E->key();
  888. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  889. String conf = path + "/project.godot";
  890. if (!FileAccess::exists(conf)) {
  891. dialog_error->set_text(TTR("Can't open project"));
  892. dialog_error->popup_centered_minsize();
  893. return;
  894. }
  895. print_line("OPENING: " + path + " (" + selected + ")");
  896. List<String> args;
  897. args.push_back("--path");
  898. args.push_back(path);
  899. args.push_back("--editor");
  900. if (OS::get_singleton()->is_disable_crash_handler()) {
  901. args.push_back("--disable-crash-handler");
  902. }
  903. String exec = OS::get_singleton()->get_executable_path();
  904. OS::ProcessID pid = 0;
  905. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  906. ERR_FAIL_COND(err);
  907. }
  908. get_tree()->quit();
  909. }
  910. void ProjectManager::_open_project() {
  911. if (selected_list.size() < 1) {
  912. return;
  913. }
  914. if (selected_list.size() > 1) {
  915. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  916. multi_open_ask->popup_centered_minsize();
  917. } else {
  918. _open_project_confirm();
  919. }
  920. }
  921. void ProjectManager::_run_project_confirm() {
  922. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  923. const String &selected_main = E->get();
  924. if (selected_main == "") {
  925. 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."));
  926. run_error_diag->popup_centered();
  927. return;
  928. }
  929. const String &selected = E->key();
  930. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  931. if (!DirAccess::exists(path + "/.import")) {
  932. run_error_diag->set_text(TTR("Can't run project: Assets need to be imported.\nPlease edit the project to trigger the initial import."));
  933. run_error_diag->popup_centered();
  934. return;
  935. }
  936. print_line("OPENING: " + path + " (" + selected + ")");
  937. List<String> args;
  938. args.push_back("--path");
  939. args.push_back(path);
  940. if (OS::get_singleton()->is_disable_crash_handler()) {
  941. args.push_back("--disable-crash-handler");
  942. }
  943. String exec = OS::get_singleton()->get_executable_path();
  944. OS::ProcessID pid = 0;
  945. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  946. ERR_FAIL_COND(err);
  947. }
  948. //get_scene()->quit(); do not quit
  949. }
  950. void ProjectManager::_run_project() {
  951. if (selected_list.size() < 1) {
  952. return;
  953. }
  954. if (selected_list.size() > 1) {
  955. multi_run_ask->set_text(TTR("Are you sure to run more than one project?"));
  956. multi_run_ask->popup_centered_minsize();
  957. } else {
  958. _run_project_confirm();
  959. }
  960. }
  961. void ProjectManager::_scan_dir(DirAccess *da, float pos, float total, List<String> *r_projects) {
  962. List<String> subdirs;
  963. da->list_dir_begin();
  964. String n = da->get_next();
  965. while (n != String()) {
  966. if (da->current_is_dir() && !n.begins_with(".")) {
  967. subdirs.push_front(n);
  968. } else if (n == "project.godot") {
  969. r_projects->push_back(da->get_current_dir());
  970. }
  971. n = da->get_next();
  972. }
  973. da->list_dir_end();
  974. int m = 0;
  975. for (List<String>::Element *E = subdirs.front(); E; E = E->next()) {
  976. da->change_dir(E->get());
  977. float slice = total / subdirs.size();
  978. _scan_dir(da, pos + slice * m, slice, r_projects);
  979. da->change_dir("..");
  980. m++;
  981. }
  982. }
  983. void ProjectManager::_scan_begin(const String &p_base) {
  984. print_line("SCAN PROJECTS AT: " + p_base);
  985. List<String> projects;
  986. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  987. da->change_dir(p_base);
  988. _scan_dir(da, 0, 1, &projects);
  989. memdelete(da);
  990. print_line("found: " + itos(projects.size()) + " projects.");
  991. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  992. String proj = E->get().replace("/", "::");
  993. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  994. }
  995. EditorSettings::get_singleton()->save();
  996. _load_recent_projects();
  997. }
  998. void ProjectManager::_scan_projects() {
  999. scan_dir->popup_centered_ratio();
  1000. }
  1001. void ProjectManager::_new_project() {
  1002. npdialog->set_mode(ProjectDialog::MODE_NEW);
  1003. npdialog->show_dialog();
  1004. }
  1005. void ProjectManager::_import_project() {
  1006. npdialog->set_mode(ProjectDialog::MODE_IMPORT);
  1007. npdialog->show_dialog();
  1008. }
  1009. void ProjectManager::_rename_project() {
  1010. if (selected_list.size() == 0) {
  1011. return;
  1012. }
  1013. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1014. const String &selected = E->key();
  1015. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  1016. npdialog->set_project_path(path);
  1017. npdialog->set_mode(ProjectDialog::MODE_RENAME);
  1018. npdialog->show_dialog();
  1019. }
  1020. }
  1021. void ProjectManager::_erase_project_confirm() {
  1022. if (selected_list.size() == 0) {
  1023. return;
  1024. }
  1025. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  1026. EditorSettings::get_singleton()->erase("projects/" + E->key());
  1027. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  1028. }
  1029. EditorSettings::get_singleton()->save();
  1030. selected_list.clear();
  1031. last_clicked = "";
  1032. _load_recent_projects();
  1033. }
  1034. void ProjectManager::_erase_project() {
  1035. if (selected_list.size() == 0)
  1036. return;
  1037. erase_ask->set_text(TTR("Remove project from the list? (Folder contents will not be modified)"));
  1038. erase_ask->popup_centered_minsize();
  1039. }
  1040. void ProjectManager::_language_selected(int p_id) {
  1041. String lang = language_btn->get_item_metadata(p_id);
  1042. EditorSettings::get_singleton()->set("interface/editor/editor_language", lang);
  1043. language_btn->set_text(lang);
  1044. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1045. language_restart_ask->set_text(TTR("Language changed.\nThe UI will update next time the editor or project manager starts."));
  1046. language_restart_ask->popup_centered();
  1047. }
  1048. void ProjectManager::_restart_confirm() {
  1049. List<String> args = OS::get_singleton()->get_cmdline_args();
  1050. String exec = OS::get_singleton()->get_executable_path();
  1051. OS::ProcessID pid = 0;
  1052. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  1053. ERR_FAIL_COND(err);
  1054. get_tree()->quit();
  1055. }
  1056. void ProjectManager::_exit_dialog() {
  1057. get_tree()->quit();
  1058. }
  1059. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  1060. npdialog->set_mode(ProjectDialog::MODE_INSTALL);
  1061. npdialog->set_zip_path(p_zip_path);
  1062. npdialog->set_zip_title(p_title);
  1063. npdialog->show_dialog();
  1064. }
  1065. void ProjectManager::_files_dropped(PoolStringArray p_files, int p_screen) {
  1066. Set<String> folders_set;
  1067. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1068. for (int i = 0; i < p_files.size(); i++) {
  1069. String file = p_files[i];
  1070. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  1071. }
  1072. memdelete(da);
  1073. if (folders_set.size() > 0) {
  1074. PoolStringArray folders;
  1075. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  1076. folders.append(E->get());
  1077. }
  1078. bool confirm = true;
  1079. if (folders.size() == 1) {
  1080. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1081. if (dir->change_dir(folders[0]) == OK) {
  1082. dir->list_dir_begin();
  1083. String file = dir->get_next();
  1084. while (confirm && file != String()) {
  1085. if (!dir->current_is_dir() && file.ends_with("project.godot")) {
  1086. confirm = false;
  1087. }
  1088. file = dir->get_next();
  1089. }
  1090. dir->list_dir_end();
  1091. }
  1092. memdelete(dir);
  1093. }
  1094. if (confirm) {
  1095. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  1096. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  1097. multi_scan_ask->set_text(vformat(TTR("You are about the scan %s folders for existing Godot projects. Do you confirm?"), folders.size()));
  1098. multi_scan_ask->popup_centered_minsize();
  1099. } else {
  1100. _scan_multiple_folders(folders);
  1101. }
  1102. }
  1103. }
  1104. void ProjectManager::_scan_multiple_folders(PoolStringArray p_files) {
  1105. for (int i = 0; i < p_files.size(); i++) {
  1106. _scan_begin(p_files.get(i));
  1107. }
  1108. }
  1109. void ProjectManager::_bind_methods() {
  1110. ClassDB::bind_method("_open_project", &ProjectManager::_open_project);
  1111. ClassDB::bind_method("_open_project_confirm", &ProjectManager::_open_project_confirm);
  1112. ClassDB::bind_method("_run_project", &ProjectManager::_run_project);
  1113. ClassDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  1114. ClassDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  1115. ClassDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  1116. ClassDB::bind_method("_import_project", &ProjectManager::_import_project);
  1117. ClassDB::bind_method("_new_project", &ProjectManager::_new_project);
  1118. ClassDB::bind_method("_rename_project", &ProjectManager::_rename_project);
  1119. ClassDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  1120. ClassDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  1121. ClassDB::bind_method("_language_selected", &ProjectManager::_language_selected);
  1122. ClassDB::bind_method("_restart_confirm", &ProjectManager::_restart_confirm);
  1123. ClassDB::bind_method("_exit_dialog", &ProjectManager::_exit_dialog);
  1124. ClassDB::bind_method("_load_recent_projects", &ProjectManager::_load_recent_projects);
  1125. ClassDB::bind_method("_on_project_renamed", &ProjectManager::_on_project_renamed);
  1126. ClassDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  1127. ClassDB::bind_method("_update_scroll_position", &ProjectManager::_update_scroll_position);
  1128. ClassDB::bind_method("_panel_draw", &ProjectManager::_panel_draw);
  1129. ClassDB::bind_method("_panel_input", &ProjectManager::_panel_input);
  1130. ClassDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  1131. ClassDB::bind_method("_favorite_pressed", &ProjectManager::_favorite_pressed);
  1132. ClassDB::bind_method("_install_project", &ProjectManager::_install_project);
  1133. ClassDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  1134. ClassDB::bind_method(D_METHOD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  1135. }
  1136. ProjectManager::ProjectManager() {
  1137. // load settings
  1138. if (!EditorSettings::get_singleton())
  1139. EditorSettings::create();
  1140. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  1141. {
  1142. int dpi_mode = EditorSettings::get_singleton()->get("interface/editor/hidpi_mode");
  1143. if (dpi_mode == 0) {
  1144. const int screen = OS::get_singleton()->get_current_screen();
  1145. editor_set_scale(OS::get_singleton()->get_screen_dpi(screen) >= 192 && OS::get_singleton()->get_screen_size(screen).x > 2000 ? 2.0 : 1.0);
  1146. } else if (dpi_mode == 1) {
  1147. editor_set_scale(0.75);
  1148. } else if (dpi_mode == 2) {
  1149. editor_set_scale(1.0);
  1150. } else if (dpi_mode == 3) {
  1151. editor_set_scale(1.5);
  1152. } else if (dpi_mode == 4) {
  1153. editor_set_scale(2.0);
  1154. }
  1155. }
  1156. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  1157. set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1158. set_theme(create_editor_theme());
  1159. gui_base = memnew(Control);
  1160. add_child(gui_base);
  1161. gui_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1162. gui_base->set_theme(create_custom_theme());
  1163. Panel *panel = memnew(Panel);
  1164. gui_base->add_child(panel);
  1165. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1166. VBoxContainer *vb = memnew(VBoxContainer);
  1167. panel->add_child(vb);
  1168. vb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 20 * EDSCALE);
  1169. vb->set_margin(MARGIN_TOP, 4 * EDSCALE);
  1170. vb->set_margin(MARGIN_BOTTOM, -4 * EDSCALE);
  1171. vb->add_constant_override("separation", 15 * EDSCALE);
  1172. String cp;
  1173. cp.push_back(0xA9);
  1174. cp.push_back(0);
  1175. OS::get_singleton()->set_window_title(_MKSTR(VERSION_NAME) + String(" - ") + TTR("Project Manager") + " - " + cp + " 2008-2017 Juan Linietsky, Ariel Manzur & Godot Contributors");
  1176. HBoxContainer *top_hb = memnew(HBoxContainer);
  1177. vb->add_child(top_hb);
  1178. CenterContainer *ccl = memnew(CenterContainer);
  1179. Label *l = memnew(Label);
  1180. l->set_text(_MKSTR(VERSION_NAME) + String(" - ") + TTR("Project Manager"));
  1181. l->add_font_override("font", gui_base->get_font("doc", "EditorFonts"));
  1182. ccl->add_child(l);
  1183. top_hb->add_child(ccl);
  1184. top_hb->add_spacer();
  1185. l = memnew(Label);
  1186. String hash = String(VERSION_HASH);
  1187. if (hash.length() != 0)
  1188. hash = "." + hash.left(7);
  1189. l->set_text("v" VERSION_MKSTRING "" + hash);
  1190. //l->add_font_override("font",get_font("bold","Fonts"));
  1191. l->set_align(Label::ALIGN_CENTER);
  1192. top_hb->add_child(l);
  1193. //vb->add_child(memnew(HSeparator));
  1194. //vb->add_margin_child("\n",memnew(Control));
  1195. Control *center_box = memnew(Control);
  1196. center_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1197. vb->add_child(center_box);
  1198. tabs = memnew(TabContainer);
  1199. center_box->add_child(tabs);
  1200. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1201. HBoxContainer *tree_hb = memnew(HBoxContainer);
  1202. projects_hb = tree_hb;
  1203. projects_hb->set_name(TTR("Project List"));
  1204. tabs->add_child(tree_hb);
  1205. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  1206. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1207. tree_hb->add_child(search_tree_vb);
  1208. HBoxContainer *search_box = memnew(HBoxContainer);
  1209. search_box->add_spacer(true);
  1210. project_filter = memnew(ProjectListFilter);
  1211. search_box->add_child(project_filter);
  1212. project_filter->connect("filter_changed", this, "_load_recent_projects");
  1213. project_filter->set_custom_minimum_size(Size2(250, 10));
  1214. search_tree_vb->add_child(search_box);
  1215. PanelContainer *pc = memnew(PanelContainer);
  1216. pc->add_style_override("panel", gui_base->get_stylebox("bg", "Tree"));
  1217. search_tree_vb->add_child(pc);
  1218. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  1219. scroll = memnew(ScrollContainer);
  1220. pc->add_child(scroll);
  1221. scroll->set_enable_h_scroll(false);
  1222. VBoxContainer *tree_vb = memnew(VBoxContainer);
  1223. tree_hb->add_child(tree_vb);
  1224. scroll_childs = memnew(VBoxContainer);
  1225. scroll_childs->set_h_size_flags(SIZE_EXPAND_FILL);
  1226. scroll->add_child(scroll_childs);
  1227. //HBoxContainer *hb = memnew( HBoxContainer );
  1228. //vb->add_child(hb);
  1229. Button *open = memnew(Button);
  1230. open->set_text(TTR("Edit"));
  1231. tree_vb->add_child(open);
  1232. open->connect("pressed", this, "_open_project");
  1233. open_btn = open;
  1234. Button *run = memnew(Button);
  1235. run->set_text(TTR("Run"));
  1236. tree_vb->add_child(run);
  1237. run->connect("pressed", this, "_run_project");
  1238. run_btn = run;
  1239. tree_vb->add_child(memnew(HSeparator));
  1240. Button *scan = memnew(Button);
  1241. scan->set_text(TTR("Scan"));
  1242. tree_vb->add_child(scan);
  1243. scan->connect("pressed", this, "_scan_projects");
  1244. tree_vb->add_child(memnew(HSeparator));
  1245. scan_dir = memnew(FileDialog);
  1246. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  1247. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  1248. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  1249. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  1250. gui_base->add_child(scan_dir);
  1251. scan_dir->connect("dir_selected", this, "_scan_begin");
  1252. Button *create = memnew(Button);
  1253. create->set_text(TTR("New Project"));
  1254. tree_vb->add_child(create);
  1255. create->connect("pressed", this, "_new_project");
  1256. Button *import = memnew(Button);
  1257. import->set_text(TTR("Import"));
  1258. tree_vb->add_child(import);
  1259. import->connect("pressed", this, "_import_project");
  1260. Button *rename = memnew(Button);
  1261. rename->set_text(TTR("Rename"));
  1262. tree_vb->add_child(rename);
  1263. rename->connect("pressed", this, "_rename_project");
  1264. rename_btn = rename;
  1265. Button *erase = memnew(Button);
  1266. erase->set_text(TTR("Remove"));
  1267. tree_vb->add_child(erase);
  1268. erase->connect("pressed", this, "_erase_project");
  1269. erase_btn = erase;
  1270. tree_vb->add_spacer();
  1271. if (StreamPeerSSL::is_available()) {
  1272. asset_library = memnew(EditorAssetLibrary(true));
  1273. asset_library->set_name(TTR("Templates"));
  1274. tabs->add_child(asset_library);
  1275. asset_library->connect("install_asset", this, "_install_project");
  1276. } else {
  1277. WARN_PRINT("Asset Library not available, as it requires SSL to work.");
  1278. }
  1279. HBoxContainer *settings_hb = memnew(HBoxContainer);
  1280. settings_hb->set_alignment(BoxContainer::ALIGN_END);
  1281. settings_hb->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  1282. language_btn = memnew(OptionButton);
  1283. Vector<String> editor_languages;
  1284. List<PropertyInfo> editor_settings_properties;
  1285. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  1286. for (List<PropertyInfo>::Element *E = editor_settings_properties.front(); E; E = E->next()) {
  1287. PropertyInfo &pi = E->get();
  1288. if (pi.name == "interface/editor/editor_language") {
  1289. editor_languages = pi.hint_string.split(",");
  1290. }
  1291. }
  1292. String current_lang = EditorSettings::get_singleton()->get("interface/editor/editor_language");
  1293. for (int i = 0; i < editor_languages.size(); i++) {
  1294. String lang = editor_languages[i];
  1295. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang);
  1296. language_btn->add_item(lang_name + " [" + lang + "]", i);
  1297. language_btn->set_item_metadata(i, lang);
  1298. if (current_lang == lang) {
  1299. language_btn->select(i);
  1300. language_btn->set_text(lang);
  1301. }
  1302. }
  1303. language_btn->set_icon(get_icon("Environment", "EditorIcons"));
  1304. settings_hb->add_child(language_btn);
  1305. language_btn->connect("item_selected", this, "_language_selected");
  1306. center_box->add_child(settings_hb);
  1307. settings_hb->set_anchors_and_margins_preset(Control::PRESET_TOP_RIGHT);
  1308. CenterContainer *cc = memnew(CenterContainer);
  1309. Button *cancel = memnew(Button);
  1310. cancel->set_text(TTR("Exit"));
  1311. cancel->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1312. cc->add_child(cancel);
  1313. cancel->connect("pressed", this, "_exit_dialog");
  1314. vb->add_child(cc);
  1315. //
  1316. language_restart_ask = memnew(ConfirmationDialog);
  1317. language_restart_ask->get_ok()->set_text(TTR("Restart Now"));
  1318. language_restart_ask->get_ok()->connect("pressed", this, "_restart_confirm");
  1319. language_restart_ask->get_cancel()->set_text(TTR("Continue"));
  1320. gui_base->add_child(language_restart_ask);
  1321. erase_ask = memnew(ConfirmationDialog);
  1322. erase_ask->get_ok()->set_text(TTR("Remove"));
  1323. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  1324. gui_base->add_child(erase_ask);
  1325. multi_open_ask = memnew(ConfirmationDialog);
  1326. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  1327. multi_open_ask->get_ok()->connect("pressed", this, "_open_project_confirm");
  1328. gui_base->add_child(multi_open_ask);
  1329. multi_run_ask = memnew(ConfirmationDialog);
  1330. multi_run_ask->get_ok()->set_text(TTR("Run"));
  1331. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  1332. gui_base->add_child(multi_run_ask);
  1333. multi_scan_ask = memnew(ConfirmationDialog);
  1334. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  1335. gui_base->add_child(multi_scan_ask);
  1336. OS::get_singleton()->set_low_processor_usage_mode(true);
  1337. npdialog = memnew(ProjectDialog);
  1338. gui_base->add_child(npdialog);
  1339. npdialog->connect("project_renamed", this, "_on_project_renamed");
  1340. npdialog->connect("project_created", this, "_on_project_created");
  1341. _load_recent_projects();
  1342. if (EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path")) {
  1343. _scan_begin(EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path"));
  1344. }
  1345. last_clicked = "";
  1346. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  1347. run_error_diag = memnew(AcceptDialog);
  1348. gui_base->add_child(run_error_diag);
  1349. run_error_diag->set_title(TTR("Can't run project"));
  1350. dialog_error = memnew(AcceptDialog);
  1351. gui_base->add_child(dialog_error);
  1352. }
  1353. ProjectManager::~ProjectManager() {
  1354. if (EditorSettings::get_singleton())
  1355. EditorSettings::destroy();
  1356. }
  1357. void ProjectListFilter::_setup_filters() {
  1358. filter_option->clear();
  1359. filter_option->add_item(TTR("Name"));
  1360. filter_option->add_item(TTR("Path"));
  1361. }
  1362. void ProjectListFilter::_command(int p_command) {
  1363. switch (p_command) {
  1364. case CMD_CLEAR_FILTER: {
  1365. if (search_box->get_text() != "") {
  1366. search_box->clear();
  1367. emit_signal("filter_changed");
  1368. }
  1369. } break;
  1370. }
  1371. }
  1372. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  1373. emit_signal("filter_changed");
  1374. }
  1375. String ProjectListFilter::get_search_term() {
  1376. return search_box->get_text().strip_edges();
  1377. }
  1378. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  1379. return _current_filter;
  1380. }
  1381. void ProjectListFilter::_filter_option_selected(int p_idx) {
  1382. FilterOption selected = (FilterOption)(filter_option->get_selected());
  1383. if (_current_filter != selected) {
  1384. _current_filter = selected;
  1385. emit_signal("filter_changed");
  1386. }
  1387. }
  1388. void ProjectListFilter::_notification(int p_what) {
  1389. switch (p_what) {
  1390. case NOTIFICATION_ENTER_TREE: {
  1391. clear_search_button->set_icon(get_icon("Close", "EditorIcons"));
  1392. } break;
  1393. }
  1394. }
  1395. void ProjectListFilter::_bind_methods() {
  1396. ClassDB::bind_method(D_METHOD("_command"), &ProjectListFilter::_command);
  1397. ClassDB::bind_method(D_METHOD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  1398. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  1399. ADD_SIGNAL(MethodInfo("filter_changed"));
  1400. }
  1401. ProjectListFilter::ProjectListFilter() {
  1402. editor_initialize_certificates(); //for asset sharing
  1403. _current_filter = FILTER_NAME;
  1404. filter_option = memnew(OptionButton);
  1405. filter_option->set_custom_minimum_size(Size2(80 * EDSCALE, 10 * EDSCALE));
  1406. filter_option->set_clip_text(true);
  1407. filter_option->connect("item_selected", this, "_filter_option_selected");
  1408. add_child(filter_option);
  1409. _setup_filters();
  1410. search_box = memnew(LineEdit);
  1411. search_box->connect("text_changed", this, "_search_text_changed");
  1412. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1413. add_child(search_box);
  1414. clear_search_button = memnew(ToolButton);
  1415. clear_search_button->connect("pressed", this, "_command", make_binds(CMD_CLEAR_FILTER));
  1416. add_child(clear_search_button);
  1417. }