project_manager.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. /*************************************************************************/
  2. /* project_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 "version.h"
  51. static String _find_project_file(DirAccess *p_da) {
  52. p_da->list_dir_begin();
  53. while (true) {
  54. String f = p_da->get_next();
  55. if (f == "")
  56. break;
  57. if (f.get_extension() == "godot")
  58. return p_da->get_current_dir() + "/" + f;
  59. }
  60. p_da->list_dir_end();
  61. return "";
  62. }
  63. class NewProjectDialog : public ConfirmationDialog {
  64. GDCLASS(NewProjectDialog, ConfirmationDialog);
  65. public:
  66. enum Mode {
  67. MODE_NEW,
  68. MODE_IMPORT,
  69. MODE_INSTALL
  70. };
  71. private:
  72. Mode mode;
  73. Label *pp, *pn;
  74. Label *error;
  75. LineEdit *project_path;
  76. LineEdit *project_name;
  77. FileDialog *fdialog;
  78. String zip_path;
  79. String zip_title;
  80. AcceptDialog *dialog_error;
  81. String _test_path() {
  82. error->set_text("");
  83. get_ok()->set_disabled(true);
  84. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  85. String valid_path;
  86. if (d->change_dir(project_path->get_text()) == OK) {
  87. valid_path = project_path->get_text();
  88. } else if (d->change_dir(project_path->get_text().strip_edges()) == OK) {
  89. valid_path = project_path->get_text().strip_edges();
  90. }
  91. if (valid_path == "") {
  92. error->set_text(TTR("Invalid project path, the path must exist!"));
  93. memdelete(d);
  94. return "";
  95. }
  96. if (mode != MODE_IMPORT) {
  97. if (_find_project_file(d) != "") {
  98. error->set_text(TTR("Invalid project path, *.godot must not exist."));
  99. memdelete(d);
  100. return "";
  101. }
  102. } else {
  103. if (valid_path != "" && _find_project_file(d) == "") {
  104. error->set_text(TTR("Invalid project path, *.godot must exist."));
  105. memdelete(d);
  106. return "";
  107. }
  108. }
  109. memdelete(d);
  110. get_ok()->set_disabled(false);
  111. return valid_path;
  112. }
  113. void _path_text_changed(const String &p_path) {
  114. String sp = _test_path();
  115. if (sp != "") {
  116. sp = sp.replace("\\", "/");
  117. int lidx = sp.find_last("/");
  118. if (lidx != -1) {
  119. sp = sp.substr(lidx + 1, sp.length());
  120. }
  121. if (sp == "" && mode == MODE_IMPORT)
  122. sp = TTR("Imported Project");
  123. project_name->set_text(sp);
  124. }
  125. }
  126. void _file_selected(const String &p_path) {
  127. String p = p_path;
  128. if (mode == MODE_IMPORT) {
  129. if (p.get_extension() == "godot") {
  130. p = p.get_base_dir();
  131. }
  132. }
  133. String sp = p.simplify_path();
  134. project_path->set_text(sp);
  135. _path_text_changed(sp);
  136. get_ok()->call_deferred("grab_focus");
  137. }
  138. void _path_selected(const String &p_path) {
  139. String p = p_path;
  140. String sp = p.simplify_path();
  141. project_path->set_text(sp);
  142. _path_text_changed(sp);
  143. get_ok()->call_deferred("grab_focus");
  144. }
  145. void _browse_path() {
  146. if (mode == MODE_IMPORT) {
  147. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  148. fdialog->clear_filters();
  149. fdialog->add_filter("*.godot ; " _MKSTR(VERSION_NAME) " Project");
  150. } else {
  151. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  152. }
  153. fdialog->popup_centered_ratio();
  154. }
  155. void _text_changed(const String &p_text) {
  156. _test_path();
  157. }
  158. void ok_pressed() {
  159. String dir = _test_path();
  160. if (dir == "") {
  161. error->set_text(TTR("Invalid project path (changed anything?)."));
  162. return;
  163. }
  164. if (mode == MODE_IMPORT) {
  165. // nothing to do
  166. } else {
  167. if (mode == MODE_NEW) {
  168. FileAccess *f = FileAccess::open(dir.plus_file("/" + project_name->get_text().replace(" ", "_") + ".godot"), FileAccess::WRITE);
  169. if (!f) {
  170. error->set_text(TTR("Couldn't create *.godot project file in project path."));
  171. } else {
  172. f->store_line("; Engine configuration file.");
  173. f->store_line("; It's best edited using the editor UI and not directly,");
  174. f->store_line("; since the parameters that go here are not all obvious.");
  175. f->store_line("; ");
  176. f->store_line("; Format: ");
  177. f->store_line("; [section] ; section goes between []");
  178. f->store_line("; param=value ; assign values to parameters");
  179. f->store_line("\n");
  180. f->store_line("[application]");
  181. f->store_line("\n");
  182. f->store_line("name=\"" + project_name->get_text() + "\"");
  183. f->store_line("icon=\"res://icon.png\"");
  184. memdelete(f);
  185. ResourceSaver::save(dir.plus_file("/icon.png"), get_icon("DefaultProjectIcon", "EditorIcons"));
  186. }
  187. } else if (mode == MODE_INSTALL) {
  188. FileAccess *src_f = NULL;
  189. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  190. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  191. if (!pkg) {
  192. dialog_error->set_text("Error opening package file, not in zip format.");
  193. return;
  194. }
  195. int ret = unzGoToFirstFile(pkg);
  196. Vector<String> failed_files;
  197. int idx = 0;
  198. while (ret == UNZ_OK) {
  199. //get filename
  200. unz_file_info info;
  201. char fname[16384];
  202. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  203. String path = fname;
  204. int depth = 1; //stuff from github comes with tag
  205. bool skip = false;
  206. while (depth > 0) {
  207. int pp = path.find("/");
  208. if (pp == -1) {
  209. skip = true;
  210. break;
  211. }
  212. path = path.substr(pp + 1, path.length());
  213. depth--;
  214. }
  215. if (skip || path == String()) {
  216. //
  217. } else if (path.ends_with("/")) { // a dir
  218. path = path.substr(0, path.length() - 1);
  219. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  220. da->make_dir(dir.plus_file(path));
  221. memdelete(da);
  222. } else {
  223. Vector<uint8_t> data;
  224. data.resize(info.uncompressed_size);
  225. //read
  226. unzOpenCurrentFile(pkg);
  227. unzReadCurrentFile(pkg, data.ptr(), data.size());
  228. unzCloseCurrentFile(pkg);
  229. FileAccess *f = FileAccess::open(dir.plus_file(path), FileAccess::WRITE);
  230. if (f) {
  231. f->store_buffer(data.ptr(), data.size());
  232. memdelete(f);
  233. } else {
  234. failed_files.push_back(path);
  235. }
  236. }
  237. idx++;
  238. ret = unzGoToNextFile(pkg);
  239. }
  240. unzClose(pkg);
  241. if (failed_files.size()) {
  242. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  243. for (int i = 0; i < failed_files.size(); i++) {
  244. if (i > 15) {
  245. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  246. break;
  247. }
  248. msg += failed_files[i] + "\n";
  249. }
  250. dialog_error->set_text(msg);
  251. dialog_error->popup_centered_minsize();
  252. } else {
  253. dialog_error->set_text(TTR("Package Installed Successfully!"));
  254. dialog_error->popup_centered_minsize();
  255. }
  256. }
  257. }
  258. dir = dir.replace("\\", "/");
  259. if (dir.ends_with("/"))
  260. dir = dir.substr(0, dir.length() - 1);
  261. String proj = dir.replace("/", "::");
  262. EditorSettings::get_singleton()->set("projects/" + proj, dir);
  263. EditorSettings::get_singleton()->save();
  264. hide();
  265. emit_signal("project_created", dir);
  266. }
  267. protected:
  268. static void _bind_methods() {
  269. ClassDB::bind_method("_browse_path", &NewProjectDialog::_browse_path);
  270. ClassDB::bind_method("_text_changed", &NewProjectDialog::_text_changed);
  271. ClassDB::bind_method("_path_text_changed", &NewProjectDialog::_path_text_changed);
  272. ClassDB::bind_method("_path_selected", &NewProjectDialog::_path_selected);
  273. ClassDB::bind_method("_file_selected", &NewProjectDialog::_file_selected);
  274. ADD_SIGNAL(MethodInfo("project_created"));
  275. }
  276. public:
  277. void set_zip_path(const String &p_path) {
  278. zip_path = p_path;
  279. }
  280. void set_zip_title(const String &p_title) {
  281. zip_title = p_title;
  282. }
  283. void set_mode(Mode p_mode) {
  284. mode = p_mode;
  285. }
  286. void show_dialog() {
  287. project_path->clear();
  288. project_name->clear();
  289. if (mode == MODE_IMPORT) {
  290. set_title(TTR("Import Existing Project"));
  291. get_ok()->set_text(TTR("Import"));
  292. pp->set_text(TTR("Project Path (Must Exist):"));
  293. pn->set_text(TTR("Project Name:"));
  294. pn->hide();
  295. project_name->hide();
  296. popup_centered(Size2(500, 125) * EDSCALE);
  297. } else if (mode == MODE_NEW) {
  298. set_title(TTR("Create New Project"));
  299. get_ok()->set_text(TTR("Create"));
  300. pp->set_text(TTR("Project Path:"));
  301. pn->set_text(TTR("Project Name:"));
  302. pn->show();
  303. project_name->show();
  304. popup_centered(Size2(500, 145) * EDSCALE);
  305. } else if (mode == MODE_INSTALL) {
  306. set_title(TTR("Install Project:") + " " + zip_title);
  307. get_ok()->set_text(TTR("Install"));
  308. pp->set_text(TTR("Project Path:"));
  309. pn->hide();
  310. project_name->hide();
  311. popup_centered(Size2(500, 125) * EDSCALE);
  312. }
  313. project_path->grab_focus();
  314. _test_path();
  315. }
  316. NewProjectDialog() {
  317. VBoxContainer *vb = memnew(VBoxContainer);
  318. add_child(vb);
  319. //set_child_rect(vb);
  320. Label *l = memnew(Label);
  321. l->set_text(TTR("Project Path:"));
  322. vb->add_child(l);
  323. pp = l;
  324. project_path = memnew(LineEdit);
  325. MarginContainer *mc = memnew(MarginContainer);
  326. vb->add_child(mc);
  327. HBoxContainer *pphb = memnew(HBoxContainer);
  328. mc->add_child(pphb);
  329. pphb->add_child(project_path);
  330. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  331. Button *browse = memnew(Button);
  332. pphb->add_child(browse);
  333. browse->set_text(TTR("Browse"));
  334. browse->connect("pressed", this, "_browse_path");
  335. l = memnew(Label);
  336. l->set_text(TTR("Project Name:"));
  337. l->set_position(Point2(5, 50));
  338. vb->add_child(l);
  339. pn = l;
  340. project_name = memnew(LineEdit);
  341. mc = memnew(MarginContainer);
  342. vb->add_child(mc);
  343. mc->add_child(project_name);
  344. project_name->set_text(TTR("New Game Project"));
  345. l = memnew(Label);
  346. l->set_text(TTR("That's a BINGO!"));
  347. vb->add_child(l);
  348. error = l;
  349. l->add_color_override("font_color", Color(1, 0.4, 0.3, 0.8));
  350. l->set_align(Label::ALIGN_CENTER);
  351. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  352. project_path->set_text(d->get_current_dir());
  353. memdelete(d);
  354. fdialog = memnew(FileDialog);
  355. add_child(fdialog);
  356. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  357. fdialog->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  358. project_name->connect("text_changed", this, "_text_changed");
  359. project_path->connect("text_changed", this, "_path_text_changed");
  360. fdialog->connect("dir_selected", this, "_path_selected");
  361. fdialog->connect("file_selected", this, "_file_selected");
  362. set_hide_on_ok(false);
  363. mode = MODE_NEW;
  364. dialog_error = memnew(AcceptDialog);
  365. add_child(dialog_error);
  366. }
  367. };
  368. struct ProjectItem {
  369. String project;
  370. String path;
  371. String conf;
  372. uint64_t last_modified;
  373. bool favorite;
  374. ProjectItem() {}
  375. ProjectItem(const String &p_project, const String &p_path, const String &p_conf, uint64_t p_last_modified, bool p_favorite = false) {
  376. project = p_project;
  377. path = p_path;
  378. conf = p_conf;
  379. last_modified = p_last_modified;
  380. favorite = p_favorite;
  381. }
  382. _FORCE_INLINE_ bool operator<(const ProjectItem &l) const { return last_modified > l.last_modified; }
  383. _FORCE_INLINE_ bool operator==(const ProjectItem &l) const { return project == l.project; }
  384. };
  385. void ProjectManager::_notification(int p_what) {
  386. if (p_what == NOTIFICATION_ENTER_TREE) {
  387. get_tree()->set_editor_hint(true);
  388. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  389. set_process_unhandled_input(is_visible_in_tree());
  390. }
  391. }
  392. void ProjectManager::_panel_draw(Node *p_hb) {
  393. HBoxContainer *hb = p_hb->cast_to<HBoxContainer>();
  394. 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"));
  395. if (selected_list.has(hb->get_meta("name"))) {
  396. hb->draw_style_box(gui_base->get_stylebox("selected", "Tree"), Rect2(Point2(), hb->get_size() - Size2(10, 0)));
  397. }
  398. }
  399. void ProjectManager::_update_project_buttons() {
  400. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  401. CanvasItem *item = scroll_childs->get_child(i)->cast_to<CanvasItem>();
  402. item->update();
  403. }
  404. bool has_runnable_scene = false;
  405. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  406. const String &selected_main = E->get();
  407. if (selected_main == "") continue;
  408. has_runnable_scene = true;
  409. break;
  410. }
  411. erase_btn->set_disabled(selected_list.size() < 1);
  412. open_btn->set_disabled(selected_list.size() < 1);
  413. run_btn->set_disabled(!has_runnable_scene);
  414. }
  415. void ProjectManager::_panel_input(const InputEvent &p_ev, Node *p_hb) {
  416. if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.pressed && p_ev.mouse_button.button_index == BUTTON_LEFT) {
  417. String clicked = p_hb->get_meta("name");
  418. String clicked_main_scene = p_hb->get_meta("main_scene");
  419. if (p_ev.key.mod.shift && selected_list.size() > 0 && last_clicked != "" && clicked != last_clicked) {
  420. int clicked_id = -1;
  421. int last_clicked_id = -1;
  422. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  423. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  424. if (!hb) continue;
  425. if (hb->get_meta("name") == clicked) clicked_id = i;
  426. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  427. }
  428. if (last_clicked_id != -1 && clicked_id != -1) {
  429. int min = clicked_id < last_clicked_id ? clicked_id : last_clicked_id;
  430. int max = clicked_id > last_clicked_id ? clicked_id : last_clicked_id;
  431. for (int i = 0; i < scroll_childs->get_child_count(); ++i) {
  432. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  433. if (!hb) continue;
  434. if (i != clicked_id && (i < min || i > max) && !p_ev.key.mod.control) {
  435. selected_list.erase(hb->get_meta("name"));
  436. } else if (i >= min && i <= max) {
  437. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  438. }
  439. }
  440. }
  441. } else if (selected_list.has(clicked) && p_ev.key.mod.control) {
  442. selected_list.erase(clicked);
  443. } else {
  444. last_clicked = clicked;
  445. if (p_ev.key.mod.control || selected_list.size() == 0) {
  446. selected_list.insert(clicked, clicked_main_scene);
  447. } else {
  448. selected_list.clear();
  449. selected_list.insert(clicked, clicked_main_scene);
  450. }
  451. }
  452. _update_project_buttons();
  453. if (p_ev.mouse_button.doubleclick)
  454. _open_project(); //open if doubleclicked
  455. }
  456. }
  457. void ProjectManager::_unhandled_input(const InputEvent &p_ev) {
  458. if (p_ev.type == InputEvent::KEY) {
  459. const InputEventKey &k = p_ev.key;
  460. if (!k.pressed)
  461. return;
  462. bool scancode_handled = true;
  463. switch (k.scancode) {
  464. case KEY_RETURN: {
  465. _open_project();
  466. } break;
  467. case KEY_HOME: {
  468. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  469. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  470. if (hb) {
  471. selected_list.clear();
  472. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  473. scroll->set_v_scroll(0);
  474. _update_project_buttons();
  475. break;
  476. }
  477. }
  478. } break;
  479. case KEY_END: {
  480. for (int i = scroll_childs->get_child_count() - 1; i >= 0; i--) {
  481. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  482. if (hb) {
  483. selected_list.clear();
  484. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  485. scroll->set_v_scroll(scroll_childs->get_size().y);
  486. _update_project_buttons();
  487. break;
  488. }
  489. }
  490. } break;
  491. case KEY_UP: {
  492. if (k.mod.shift)
  493. break;
  494. if (selected_list.size()) {
  495. bool found = false;
  496. for (int i = scroll_childs->get_child_count() - 1; i >= 0; i--) {
  497. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  498. if (!hb) continue;
  499. String current = hb->get_meta("name");
  500. if (found) {
  501. selected_list.clear();
  502. selected_list.insert(current, hb->get_meta("main_scene"));
  503. int offset_diff = scroll->get_v_scroll() - hb->get_position().y;
  504. if (offset_diff > 0)
  505. scroll->set_v_scroll(scroll->get_v_scroll() - offset_diff);
  506. _update_project_buttons();
  507. break;
  508. } else if (current == selected_list.back()->key()) {
  509. found = true;
  510. }
  511. }
  512. break;
  513. }
  514. // else fallthrough to key_down
  515. }
  516. case KEY_DOWN: {
  517. if (k.mod.shift)
  518. break;
  519. bool found = selected_list.empty();
  520. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  521. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  522. if (!hb) continue;
  523. String current = hb->get_meta("name");
  524. if (found) {
  525. selected_list.clear();
  526. selected_list.insert(current, hb->get_meta("main_scene"));
  527. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  528. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  529. if (offset_diff > 0)
  530. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  531. _update_project_buttons();
  532. break;
  533. } else if (current == selected_list.back()->key()) {
  534. found = true;
  535. }
  536. }
  537. } break;
  538. case KEY_F: {
  539. if (k.mod.command)
  540. this->project_filter->search_box->grab_focus();
  541. else
  542. scancode_handled = false;
  543. } break;
  544. default: {
  545. scancode_handled = false;
  546. } break;
  547. }
  548. if (scancode_handled) {
  549. accept_event();
  550. }
  551. }
  552. }
  553. void ProjectManager::_favorite_pressed(Node *p_hb) {
  554. String clicked = p_hb->get_meta("name");
  555. bool favorite = !p_hb->get_meta("favorite");
  556. String proj = clicked.replace(":::", ":/");
  557. proj = proj.replace("::", "/");
  558. if (favorite) {
  559. EditorSettings::get_singleton()->set("favorite_projects/" + clicked, proj);
  560. } else {
  561. EditorSettings::get_singleton()->erase("favorite_projects/" + clicked);
  562. }
  563. EditorSettings::get_singleton()->save();
  564. call_deferred("_load_recent_projects");
  565. }
  566. void ProjectManager::_load_recent_projects() {
  567. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  568. String search_term = project_filter->get_search_term();
  569. while (scroll_childs->get_child_count() > 0) {
  570. memdelete(scroll_childs->get_child(0));
  571. }
  572. Map<String, String> selected_list_copy = selected_list;
  573. List<PropertyInfo> properties;
  574. EditorSettings::get_singleton()->get_property_list(&properties);
  575. Color font_color = gui_base->get_color("font_color", "Tree");
  576. List<ProjectItem> projects;
  577. List<ProjectItem> favorite_projects;
  578. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  579. String _name = E->get().name;
  580. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  581. continue;
  582. String path = EditorSettings::get_singleton()->get(_name);
  583. if (filter_option == ProjectListFilter::FILTER_PATH && search_term != "" && path.findn(search_term) == -1)
  584. continue;
  585. String project = _name.get_slice("/", 1);
  586. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  587. if (dir_access->change_dir(path) != OK) {
  588. EditorSettings::get_singleton()->erase(_name);
  589. continue;
  590. }
  591. String conf = _find_project_file(dir_access);
  592. memdelete(dir_access);
  593. bool favorite = (_name.begins_with("favorite_projects/")) ? true : false;
  594. uint64_t last_modified = 0;
  595. if (FileAccess::exists(conf)) {
  596. last_modified = FileAccess::get_modified_time(conf);
  597. String fscache = path.plus_file(".fscache");
  598. if (FileAccess::exists(fscache)) {
  599. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  600. if (cache_modified > last_modified)
  601. last_modified = cache_modified;
  602. }
  603. ProjectItem item(project, path, conf, last_modified, favorite);
  604. if (favorite)
  605. favorite_projects.push_back(item);
  606. else
  607. projects.push_back(item);
  608. } else {
  609. //project doesn't exist on disk but it's in the XML settings file
  610. EditorSettings::get_singleton()->erase(_name); //remove it
  611. }
  612. }
  613. projects.sort();
  614. favorite_projects.sort();
  615. for (List<ProjectItem>::Element *E = projects.front(); E;) {
  616. List<ProjectItem>::Element *next = E->next();
  617. if (favorite_projects.find(E->get()) != NULL)
  618. projects.erase(E->get());
  619. E = next;
  620. }
  621. for (List<ProjectItem>::Element *E = favorite_projects.back(); E; E = E->prev()) {
  622. projects.push_front(E->get());
  623. }
  624. Ref<Texture> favorite_icon = get_icon("Favorites", "EditorIcons");
  625. for (List<ProjectItem>::Element *E = projects.front(); E; E = E->next()) {
  626. ProjectItem &item = E->get();
  627. String project = item.project;
  628. String path = item.path;
  629. String conf = item.conf;
  630. bool is_favorite = item.favorite;
  631. Ref<ConfigFile> cf = memnew(ConfigFile);
  632. Error err = cf->load(conf);
  633. ERR_CONTINUE(err != OK);
  634. String project_name = TTR("Unnamed Project");
  635. if (cf->has_section_key("application", "name")) {
  636. project_name = static_cast<String>(cf->get_value("application", "name")).xml_unescape();
  637. }
  638. if (filter_option == ProjectListFilter::FILTER_NAME && search_term != "" && project_name.findn(search_term) == -1)
  639. continue;
  640. Ref<Texture> icon;
  641. if (cf->has_section_key("application", "icon")) {
  642. String appicon = cf->get_value("application", "icon");
  643. if (appicon != "") {
  644. Image img;
  645. Error err = img.load(appicon.replace_first("res://", path + "/"));
  646. if (err == OK) {
  647. img.resize(64, 64);
  648. Ref<ImageTexture> it = memnew(ImageTexture);
  649. it->create_from_image(img);
  650. icon = it;
  651. }
  652. }
  653. }
  654. if (icon.is_null()) {
  655. icon = get_icon("DefaultProjectIcon", "EditorIcons");
  656. }
  657. String main_scene;
  658. if (cf->has_section_key("application", "main_scene")) {
  659. main_scene = cf->get_value("application", "main_scene");
  660. }
  661. selected_list_copy.erase(project);
  662. HBoxContainer *hb = memnew(HBoxContainer);
  663. hb->set_meta("name", project);
  664. hb->set_meta("main_scene", main_scene);
  665. hb->set_meta("favorite", is_favorite);
  666. hb->connect("draw", this, "_panel_draw", varray(hb));
  667. hb->connect("gui_input", this, "_panel_input", varray(hb));
  668. hb->add_constant_override("separation", 10 * EDSCALE);
  669. VBoxContainer *favorite_box = memnew(VBoxContainer);
  670. TextureButton *favorite = memnew(TextureButton);
  671. favorite->set_normal_texture(favorite_icon);
  672. if (!is_favorite)
  673. favorite->set_modulate(Color(1, 1, 1, 0.2));
  674. favorite->set_v_size_flags(SIZE_EXPAND);
  675. favorite->connect("pressed", this, "_favorite_pressed", varray(hb));
  676. favorite_box->add_child(favorite);
  677. hb->add_child(favorite_box);
  678. TextureRect *tf = memnew(TextureRect);
  679. tf->set_texture(icon);
  680. hb->add_child(tf);
  681. VBoxContainer *vb = memnew(VBoxContainer);
  682. vb->set_name("project");
  683. hb->add_child(vb);
  684. Control *ec = memnew(Control);
  685. ec->set_custom_minimum_size(Size2(0, 1));
  686. vb->add_child(ec);
  687. Label *title = memnew(Label(project_name));
  688. title->add_font_override("font", gui_base->get_font("large", "Fonts"));
  689. title->add_color_override("font_color", font_color);
  690. vb->add_child(title);
  691. Label *fpath = memnew(Label(path));
  692. fpath->set_name("path");
  693. vb->add_child(fpath);
  694. fpath->set_modulate(Color(1, 1, 1, 0.5));
  695. fpath->add_color_override("font_color", font_color);
  696. scroll_childs->add_child(hb);
  697. }
  698. for (Map<String, String>::Element *E = selected_list_copy.front(); E; E = E->next()) {
  699. String key = E->key();
  700. selected_list.erase(key);
  701. }
  702. scroll->set_v_scroll(0);
  703. _update_project_buttons();
  704. EditorSettings::get_singleton()->save();
  705. tabs->set_current_tab(0);
  706. }
  707. void ProjectManager::_on_project_created(const String &dir) {
  708. bool has_already = false;
  709. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  710. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  711. Label *fpath = hb->get_node(NodePath("project/path"))->cast_to<Label>();
  712. if (fpath->get_text() == dir) {
  713. has_already = true;
  714. break;
  715. }
  716. }
  717. if (has_already) {
  718. _update_scroll_pos(dir);
  719. } else {
  720. _load_recent_projects();
  721. scroll->connect("draw", this, "_update_scroll_pos", varray(dir), CONNECT_ONESHOT);
  722. }
  723. }
  724. void ProjectManager::_update_scroll_pos(const String &dir) {
  725. for (int i = 0; i < scroll_childs->get_child_count(); i++) {
  726. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  727. Label *fpath = hb->get_node(NodePath("project/path"))->cast_to<Label>();
  728. if (fpath->get_text() == dir) {
  729. last_clicked = hb->get_meta("name");
  730. selected_list.clear();
  731. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  732. _update_project_buttons();
  733. int last_y_visible = scroll->get_v_scroll() + scroll->get_size().y;
  734. int offset_diff = (hb->get_position().y + hb->get_size().y) - last_y_visible;
  735. if (offset_diff > 0)
  736. scroll->set_v_scroll(scroll->get_v_scroll() + offset_diff);
  737. break;
  738. }
  739. }
  740. }
  741. void ProjectManager::_open_project_confirm() {
  742. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  743. const String &selected = E->key();
  744. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  745. print_line("OPENING: " + path + " (" + selected + ")");
  746. List<String> args;
  747. args.push_back("-path");
  748. args.push_back(path);
  749. args.push_back("-editor");
  750. String exec = OS::get_singleton()->get_executable_path();
  751. OS::ProcessID pid = 0;
  752. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  753. ERR_FAIL_COND(err);
  754. }
  755. get_tree()->quit();
  756. }
  757. void ProjectManager::_open_project() {
  758. if (selected_list.size() < 1) {
  759. return;
  760. }
  761. if (selected_list.size() > 1) {
  762. multi_open_ask->set_text(TTR("Are you sure to open more than one project?"));
  763. multi_open_ask->popup_centered_minsize();
  764. } else {
  765. _open_project_confirm();
  766. }
  767. }
  768. void ProjectManager::_run_project_confirm() {
  769. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  770. const String &selected_main = E->get();
  771. if (selected_main == "") continue;
  772. const String &selected = E->key();
  773. String path = EditorSettings::get_singleton()->get("projects/" + selected);
  774. print_line("OPENING: " + path + " (" + selected + ")");
  775. List<String> args;
  776. args.push_back("-path");
  777. args.push_back(path);
  778. String exec = OS::get_singleton()->get_executable_path();
  779. OS::ProcessID pid = 0;
  780. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  781. ERR_FAIL_COND(err);
  782. }
  783. //get_scene()->quit(); do not quit
  784. }
  785. void ProjectManager::_run_project() {
  786. if (selected_list.size() < 1) {
  787. return;
  788. }
  789. if (selected_list.size() > 1) {
  790. multi_run_ask->set_text(TTR("Are you sure to run more than one project?"));
  791. multi_run_ask->popup_centered_minsize();
  792. } else {
  793. _run_project_confirm();
  794. }
  795. }
  796. void ProjectManager::_scan_dir(DirAccess *da, float pos, float total, List<String> *r_projects) {
  797. List<String> subdirs;
  798. da->list_dir_begin();
  799. String n = da->get_next();
  800. while (n != String()) {
  801. if (da->current_is_dir() && !n.begins_with(".")) {
  802. subdirs.push_front(n);
  803. } else if (n.get_extension() == "godot") {
  804. r_projects->push_back(da->get_current_dir());
  805. }
  806. n = da->get_next();
  807. }
  808. da->list_dir_end();
  809. int m = 0;
  810. for (List<String>::Element *E = subdirs.front(); E; E = E->next()) {
  811. da->change_dir(E->get());
  812. float slice = total / subdirs.size();
  813. _scan_dir(da, pos + slice * m, slice, r_projects);
  814. da->change_dir("..");
  815. m++;
  816. }
  817. }
  818. void ProjectManager::_scan_begin(const String &p_base) {
  819. print_line("SCAN PROJECTS AT: " + p_base);
  820. List<String> projects;
  821. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  822. da->change_dir(p_base);
  823. _scan_dir(da, 0, 1, &projects);
  824. memdelete(da);
  825. print_line("found: " + itos(projects.size()) + " projects.");
  826. for (List<String>::Element *E = projects.front(); E; E = E->next()) {
  827. String proj = E->get().replace("/", "::");
  828. EditorSettings::get_singleton()->set("projects/" + proj, E->get());
  829. }
  830. EditorSettings::get_singleton()->save();
  831. _load_recent_projects();
  832. }
  833. void ProjectManager::_scan_projects() {
  834. scan_dir->popup_centered_ratio();
  835. }
  836. void ProjectManager::_new_project() {
  837. npdialog->set_mode(NewProjectDialog::MODE_NEW);
  838. npdialog->show_dialog();
  839. }
  840. void ProjectManager::_import_project() {
  841. npdialog->set_mode(NewProjectDialog::MODE_IMPORT);
  842. npdialog->show_dialog();
  843. }
  844. void ProjectManager::_erase_project_confirm() {
  845. if (selected_list.size() == 0) {
  846. return;
  847. }
  848. for (Map<String, String>::Element *E = selected_list.front(); E; E = E->next()) {
  849. EditorSettings::get_singleton()->erase("projects/" + E->key());
  850. EditorSettings::get_singleton()->erase("favorite_projects/" + E->key());
  851. }
  852. EditorSettings::get_singleton()->save();
  853. selected_list.clear();
  854. last_clicked = "";
  855. _load_recent_projects();
  856. }
  857. void ProjectManager::_erase_project() {
  858. if (selected_list.size() == 0)
  859. return;
  860. erase_ask->set_text(TTR("Remove project from the list? (Folder contents will not be modified)"));
  861. erase_ask->popup_centered_minsize();
  862. }
  863. void ProjectManager::_exit_dialog() {
  864. get_tree()->quit();
  865. }
  866. void ProjectManager::_install_project(const String &p_zip_path, const String &p_title) {
  867. npdialog->set_mode(NewProjectDialog::MODE_INSTALL);
  868. npdialog->set_zip_path(p_zip_path);
  869. npdialog->set_zip_title(p_title);
  870. npdialog->show_dialog();
  871. }
  872. void ProjectManager::_files_dropped(PoolStringArray p_files, int p_screen) {
  873. Set<String> folders_set;
  874. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  875. for (int i = 0; i < p_files.size(); i++) {
  876. String file = p_files[i];
  877. folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir());
  878. }
  879. memdelete(da);
  880. if (folders_set.size() > 0) {
  881. PoolStringArray folders;
  882. for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) {
  883. folders.append(E->get());
  884. }
  885. bool confirm = true;
  886. if (folders.size() == 1) {
  887. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  888. if (dir->change_dir(folders[0]) == OK) {
  889. dir->list_dir_begin();
  890. String file = dir->get_next();
  891. while (confirm && file != String()) {
  892. if (!dir->current_is_dir() && file.get_extension() == "godot") {
  893. confirm = false;
  894. }
  895. file = dir->get_next();
  896. }
  897. dir->list_dir_end();
  898. }
  899. memdelete(dir);
  900. }
  901. if (confirm) {
  902. multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders");
  903. multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders));
  904. multi_scan_ask->set_text(vformat(TTR("You are about the scan %s folders for existing Godot projects. Do you confirm?"), folders.size()));
  905. multi_scan_ask->popup_centered_minsize();
  906. } else {
  907. _scan_multiple_folders(folders);
  908. }
  909. }
  910. }
  911. void ProjectManager::_scan_multiple_folders(PoolStringArray p_files) {
  912. for (int i = 0; i < p_files.size(); i++) {
  913. _scan_begin(p_files.get(i));
  914. }
  915. }
  916. void ProjectManager::_bind_methods() {
  917. ClassDB::bind_method("_open_project", &ProjectManager::_open_project);
  918. ClassDB::bind_method("_open_project_confirm", &ProjectManager::_open_project_confirm);
  919. ClassDB::bind_method("_run_project", &ProjectManager::_run_project);
  920. ClassDB::bind_method("_run_project_confirm", &ProjectManager::_run_project_confirm);
  921. ClassDB::bind_method("_scan_projects", &ProjectManager::_scan_projects);
  922. ClassDB::bind_method("_scan_begin", &ProjectManager::_scan_begin);
  923. ClassDB::bind_method("_import_project", &ProjectManager::_import_project);
  924. ClassDB::bind_method("_new_project", &ProjectManager::_new_project);
  925. ClassDB::bind_method("_erase_project", &ProjectManager::_erase_project);
  926. ClassDB::bind_method("_erase_project_confirm", &ProjectManager::_erase_project_confirm);
  927. ClassDB::bind_method("_exit_dialog", &ProjectManager::_exit_dialog);
  928. ClassDB::bind_method("_load_recent_projects", &ProjectManager::_load_recent_projects);
  929. ClassDB::bind_method("_on_project_created", &ProjectManager::_on_project_created);
  930. ClassDB::bind_method("_update_scroll_pos", &ProjectManager::_update_scroll_pos);
  931. ClassDB::bind_method("_panel_draw", &ProjectManager::_panel_draw);
  932. ClassDB::bind_method("_panel_input", &ProjectManager::_panel_input);
  933. ClassDB::bind_method("_unhandled_input", &ProjectManager::_unhandled_input);
  934. ClassDB::bind_method("_favorite_pressed", &ProjectManager::_favorite_pressed);
  935. ClassDB::bind_method("_install_project", &ProjectManager::_install_project);
  936. ClassDB::bind_method("_files_dropped", &ProjectManager::_files_dropped);
  937. ClassDB::bind_method(D_METHOD("_scan_multiple_folders", "files"), &ProjectManager::_scan_multiple_folders);
  938. }
  939. ProjectManager::ProjectManager() {
  940. // load settings
  941. if (!EditorSettings::get_singleton())
  942. EditorSettings::create();
  943. EditorSettings::get_singleton()->set_optimize_save(false); //just write settings as they came
  944. {
  945. int dpi_mode = EditorSettings::get_singleton()->get("interface/hidpi_mode");
  946. if (dpi_mode == 0) {
  947. editor_set_scale(OS::get_singleton()->get_screen_dpi(0) >= 192 && OS::get_singleton()->get_screen_size(OS::get_singleton()->get_current_screen()).x > 2000 ? 2.0 : 1.0);
  948. } else if (dpi_mode == 1) {
  949. editor_set_scale(0.75);
  950. } else if (dpi_mode == 2) {
  951. editor_set_scale(1.0);
  952. } else if (dpi_mode == 3) {
  953. editor_set_scale(1.5);
  954. } else if (dpi_mode == 4) {
  955. editor_set_scale(2.0);
  956. }
  957. }
  958. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  959. set_area_as_parent_rect();
  960. set_theme(create_editor_theme());
  961. gui_base = memnew(Control);
  962. add_child(gui_base);
  963. gui_base->set_area_as_parent_rect();
  964. gui_base->set_theme(create_custom_theme());
  965. Panel *panel = memnew(Panel);
  966. gui_base->add_child(panel);
  967. panel->set_area_as_parent_rect();
  968. VBoxContainer *vb = memnew(VBoxContainer);
  969. panel->add_child(vb);
  970. vb->set_area_as_parent_rect(20 * EDSCALE);
  971. vb->set_margin(MARGIN_TOP, 4 * EDSCALE);
  972. vb->set_margin(MARGIN_BOTTOM, 4 * EDSCALE);
  973. vb->add_constant_override("separation", 15 * EDSCALE);
  974. String cp;
  975. cp.push_back(0xA9);
  976. cp.push_back(0);
  977. OS::get_singleton()->set_window_title(_MKSTR(VERSION_NAME) + String(" - ") + TTR("Project Manager") + " - " + cp + " 2008-2017 Juan Linietsky, Ariel Manzur.");
  978. HBoxContainer *top_hb = memnew(HBoxContainer);
  979. vb->add_child(top_hb);
  980. CenterContainer *ccl = memnew(CenterContainer);
  981. Label *l = memnew(Label);
  982. l->set_text(_MKSTR(VERSION_NAME) + String(" - ") + TTR("Project Manager"));
  983. l->add_font_override("font", gui_base->get_font("doc", "EditorFonts"));
  984. ccl->add_child(l);
  985. top_hb->add_child(ccl);
  986. top_hb->add_spacer();
  987. l = memnew(Label);
  988. l->set_text("v" VERSION_MKSTRING);
  989. //l->add_font_override("font",get_font("bold","Fonts"));
  990. l->set_align(Label::ALIGN_CENTER);
  991. top_hb->add_child(l);
  992. //vb->add_child(memnew(HSeparator));
  993. //vb->add_margin_child("\n",memnew(Control));
  994. tabs = memnew(TabContainer);
  995. vb->add_child(tabs);
  996. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  997. HBoxContainer *tree_hb = memnew(HBoxContainer);
  998. projects_hb = tree_hb;
  999. projects_hb->set_name(TTR("Project List"));
  1000. tabs->add_child(tree_hb);
  1001. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  1002. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1003. tree_hb->add_child(search_tree_vb);
  1004. HBoxContainer *search_box = memnew(HBoxContainer);
  1005. search_box->add_spacer(true);
  1006. project_filter = memnew(ProjectListFilter);
  1007. search_box->add_child(project_filter);
  1008. project_filter->connect("filter_changed", this, "_load_recent_projects");
  1009. project_filter->set_custom_minimum_size(Size2(250, 10));
  1010. search_tree_vb->add_child(search_box);
  1011. PanelContainer *pc = memnew(PanelContainer);
  1012. pc->add_style_override("panel", gui_base->get_stylebox("bg", "Tree"));
  1013. search_tree_vb->add_child(pc);
  1014. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  1015. scroll = memnew(ScrollContainer);
  1016. pc->add_child(scroll);
  1017. scroll->set_enable_h_scroll(false);
  1018. VBoxContainer *tree_vb = memnew(VBoxContainer);
  1019. tree_hb->add_child(tree_vb);
  1020. scroll_childs = memnew(VBoxContainer);
  1021. scroll_childs->set_h_size_flags(SIZE_EXPAND_FILL);
  1022. scroll->add_child(scroll_childs);
  1023. //HBoxContainer *hb = memnew( HBoxContainer );
  1024. //vb->add_child(hb);
  1025. Button *open = memnew(Button);
  1026. open->set_text(TTR("Edit"));
  1027. tree_vb->add_child(open);
  1028. open->connect("pressed", this, "_open_project");
  1029. open_btn = open;
  1030. Button *run = memnew(Button);
  1031. run->set_text(TTR("Run"));
  1032. tree_vb->add_child(run);
  1033. run->connect("pressed", this, "_run_project");
  1034. run_btn = run;
  1035. tree_vb->add_child(memnew(HSeparator));
  1036. Button *scan = memnew(Button);
  1037. scan->set_text(TTR("Scan"));
  1038. tree_vb->add_child(scan);
  1039. scan->connect("pressed", this, "_scan_projects");
  1040. tree_vb->add_child(memnew(HSeparator));
  1041. scan_dir = memnew(FileDialog);
  1042. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  1043. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  1044. scan_dir->set_title(TTR("Select a Folder to Scan")); // must be after mode or it's overridden
  1045. scan_dir->set_current_dir(EditorSettings::get_singleton()->get("filesystem/directories/default_project_path"));
  1046. gui_base->add_child(scan_dir);
  1047. scan_dir->connect("dir_selected", this, "_scan_begin");
  1048. Button *create = memnew(Button);
  1049. create->set_text(TTR("New Project"));
  1050. tree_vb->add_child(create);
  1051. create->connect("pressed", this, "_new_project");
  1052. Button *import = memnew(Button);
  1053. import->set_text(TTR("Import"));
  1054. tree_vb->add_child(import);
  1055. import->connect("pressed", this, "_import_project");
  1056. Button *erase = memnew(Button);
  1057. erase->set_text(TTR("Remove"));
  1058. tree_vb->add_child(erase);
  1059. erase->connect("pressed", this, "_erase_project");
  1060. erase_btn = erase;
  1061. tree_vb->add_spacer();
  1062. if (StreamPeerSSL::is_available()) {
  1063. asset_library = memnew(EditorAssetLibrary(true));
  1064. asset_library->set_name(TTR("Templates"));
  1065. tabs->add_child(asset_library);
  1066. asset_library->connect("install_asset", this, "_install_project");
  1067. } else {
  1068. WARN_PRINT("Asset Library not available, as it requires SSL to work.");
  1069. }
  1070. CenterContainer *cc = memnew(CenterContainer);
  1071. Button *cancel = memnew(Button);
  1072. cancel->set_text(TTR("Exit"));
  1073. cancel->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1074. cc->add_child(cancel);
  1075. cancel->connect("pressed", this, "_exit_dialog");
  1076. vb->add_child(cc);
  1077. //
  1078. erase_ask = memnew(ConfirmationDialog);
  1079. erase_ask->get_ok()->set_text(TTR("Remove"));
  1080. erase_ask->get_ok()->connect("pressed", this, "_erase_project_confirm");
  1081. gui_base->add_child(erase_ask);
  1082. multi_open_ask = memnew(ConfirmationDialog);
  1083. multi_open_ask->get_ok()->set_text(TTR("Edit"));
  1084. multi_open_ask->get_ok()->connect("pressed", this, "_open_project_confirm");
  1085. gui_base->add_child(multi_open_ask);
  1086. multi_run_ask = memnew(ConfirmationDialog);
  1087. multi_run_ask->get_ok()->set_text(TTR("Run"));
  1088. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  1089. gui_base->add_child(multi_run_ask);
  1090. multi_scan_ask = memnew(ConfirmationDialog);
  1091. multi_scan_ask->get_ok()->set_text(TTR("Scan"));
  1092. gui_base->add_child(multi_scan_ask);
  1093. OS::get_singleton()->set_low_processor_usage_mode(true);
  1094. npdialog = memnew(NewProjectDialog);
  1095. gui_base->add_child(npdialog);
  1096. npdialog->connect("project_created", this, "_on_project_created");
  1097. _load_recent_projects();
  1098. if (EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path")) {
  1099. _scan_begin(EditorSettings::get_singleton()->get("filesystem/directories/autoscan_project_path"));
  1100. }
  1101. //get_ok()->set_text("Open");
  1102. //get_ok()->set_text("Exit");
  1103. last_clicked = "";
  1104. SceneTree::get_singleton()->connect("files_dropped", this, "_files_dropped");
  1105. }
  1106. ProjectManager::~ProjectManager() {
  1107. if (EditorSettings::get_singleton())
  1108. EditorSettings::destroy();
  1109. }
  1110. void ProjectListFilter::_setup_filters() {
  1111. filter_option->clear();
  1112. filter_option->add_item(TTR("Name"));
  1113. filter_option->add_item(TTR("Path"));
  1114. }
  1115. void ProjectListFilter::_command(int p_command) {
  1116. switch (p_command) {
  1117. case CMD_CLEAR_FILTER: {
  1118. if (search_box->get_text() != "") {
  1119. search_box->clear();
  1120. emit_signal("filter_changed");
  1121. }
  1122. } break;
  1123. }
  1124. }
  1125. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  1126. emit_signal("filter_changed");
  1127. }
  1128. String ProjectListFilter::get_search_term() {
  1129. return search_box->get_text().strip_edges();
  1130. }
  1131. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  1132. return _current_filter;
  1133. }
  1134. void ProjectListFilter::_filter_option_selected(int p_idx) {
  1135. FilterOption selected = (FilterOption)(filter_option->get_selected());
  1136. if (_current_filter != selected) {
  1137. _current_filter = selected;
  1138. emit_signal("filter_changed");
  1139. }
  1140. }
  1141. void ProjectListFilter::_notification(int p_what) {
  1142. switch (p_what) {
  1143. case NOTIFICATION_ENTER_TREE: {
  1144. clear_search_button->set_icon(get_icon("CloseHover", "EditorIcons"));
  1145. } break;
  1146. }
  1147. }
  1148. void ProjectListFilter::_bind_methods() {
  1149. ClassDB::bind_method(D_METHOD("_command"), &ProjectListFilter::_command);
  1150. ClassDB::bind_method(D_METHOD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  1151. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  1152. ADD_SIGNAL(MethodInfo("filter_changed"));
  1153. }
  1154. ProjectListFilter::ProjectListFilter() {
  1155. editor_initialize_certificates(); //for asset sharing
  1156. _current_filter = FILTER_NAME;
  1157. filter_option = memnew(OptionButton);
  1158. filter_option->set_custom_minimum_size(Size2(80, 10));
  1159. filter_option->set_clip_text(true);
  1160. filter_option->connect("item_selected", this, "_filter_option_selected");
  1161. add_child(filter_option);
  1162. _setup_filters();
  1163. search_box = memnew(LineEdit);
  1164. search_box->connect("text_changed", this, "_search_text_changed");
  1165. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1166. add_child(search_box);
  1167. clear_search_button = memnew(ToolButton);
  1168. clear_search_button->connect("pressed", this, "_command", make_binds(CMD_CLEAR_FILTER));
  1169. add_child(clear_search_button);
  1170. }