project_list.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /**************************************************************************/
  2. /* project_list.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_list.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/os/time.h"
  34. #include "core/version.h"
  35. #include "editor/editor_paths.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/project_manager.h"
  39. #include "editor/project_manager/project_tag.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/gui/button.h"
  42. #include "scene/gui/label.h"
  43. #include "scene/gui/line_edit.h"
  44. #include "scene/gui/texture_button.h"
  45. #include "scene/gui/texture_rect.h"
  46. #include "scene/resources/image_texture.h"
  47. void ProjectListItemControl::_notification(int p_what) {
  48. switch (p_what) {
  49. case NOTIFICATION_THEME_CHANGED: {
  50. if (icon_needs_reload) {
  51. // The project icon may not be loaded by the time the control is displayed,
  52. // so use a loading placeholder.
  53. project_icon->set_texture(get_editor_theme_icon(SNAME("ProjectIconLoading")));
  54. }
  55. project_title->begin_bulk_theme_override();
  56. project_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title"), EditorStringName(EditorFonts)));
  57. project_title->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("title_size"), EditorStringName(EditorFonts)));
  58. project_title->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Tree")));
  59. project_title->end_bulk_theme_override();
  60. project_path->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Tree")));
  61. project_unsupported_features->set_texture(get_editor_theme_icon(SNAME("NodeWarning")));
  62. favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Favorites")));
  63. if (project_is_missing) {
  64. explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
  65. } else {
  66. explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  67. }
  68. } break;
  69. case NOTIFICATION_MOUSE_ENTER: {
  70. is_hovering = true;
  71. queue_redraw();
  72. } break;
  73. case NOTIFICATION_MOUSE_EXIT: {
  74. is_hovering = false;
  75. queue_redraw();
  76. } break;
  77. case NOTIFICATION_DRAW: {
  78. if (is_selected) {
  79. draw_style_box(get_theme_stylebox(SNAME("selected"), SNAME("Tree")), Rect2(Point2(), get_size()));
  80. }
  81. if (is_hovering) {
  82. draw_style_box(get_theme_stylebox(SNAME("hovered"), SNAME("Tree")), Rect2(Point2(), get_size()));
  83. }
  84. draw_line(Point2(0, get_size().y + 1), Point2(get_size().x, get_size().y + 1), get_theme_color(SNAME("guide_color"), SNAME("Tree")));
  85. } break;
  86. }
  87. }
  88. void ProjectListItemControl::_favorite_button_pressed() {
  89. emit_signal(SNAME("favorite_pressed"));
  90. }
  91. void ProjectListItemControl::_explore_button_pressed() {
  92. emit_signal(SNAME("explore_pressed"));
  93. }
  94. void ProjectListItemControl::set_project_title(const String &p_title) {
  95. project_title->set_text(p_title);
  96. }
  97. void ProjectListItemControl::set_project_path(const String &p_path) {
  98. project_path->set_text(p_path);
  99. }
  100. void ProjectListItemControl::set_tags(const PackedStringArray &p_tags, ProjectList *p_parent_list) {
  101. for (const String &tag : p_tags) {
  102. ProjectTag *tag_control = memnew(ProjectTag(tag));
  103. tag_container->add_child(tag_control);
  104. tag_control->connect_button_to(callable_mp(p_parent_list, &ProjectList::add_search_tag).bind(tag));
  105. }
  106. }
  107. void ProjectListItemControl::set_project_icon(const Ref<Texture2D> &p_icon) {
  108. icon_needs_reload = false;
  109. // The default project icon is 128×128 to look crisp on hiDPI displays,
  110. // but we want the actual displayed size to be 64×64 on loDPI displays.
  111. project_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  112. project_icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  113. project_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  114. project_icon->set_texture(p_icon);
  115. }
  116. void ProjectListItemControl::set_last_edited_info(const String &p_info) {
  117. last_edited_info->set_text(p_info);
  118. }
  119. void ProjectListItemControl::set_project_version(const String &p_info) {
  120. project_version->set_text(p_info);
  121. }
  122. void ProjectListItemControl::set_unsupported_features(PackedStringArray p_features) {
  123. if (p_features.size() > 0) {
  124. String tooltip_text = "";
  125. for (int i = 0; i < p_features.size(); i++) {
  126. if (ProjectList::project_feature_looks_like_version(p_features[i])) {
  127. PackedStringArray project_version_split = p_features[i].split(".");
  128. int project_version_major = 0, project_version_minor = 0;
  129. if (project_version_split.size() >= 2) {
  130. project_version_major = project_version_split[0].to_int();
  131. project_version_minor = project_version_split[1].to_int();
  132. }
  133. if (VERSION_MAJOR != project_version_major || VERSION_MINOR <= project_version_minor) {
  134. // Don't show a warning if the project was last edited in a previous minor version.
  135. tooltip_text += TTR("This project was last edited in a different Godot version: ") + p_features[i] + "\n";
  136. }
  137. p_features.remove_at(i);
  138. i--;
  139. }
  140. }
  141. if (p_features.size() > 0) {
  142. String unsupported_features_str = String(", ").join(p_features);
  143. tooltip_text += TTR("This project uses features unsupported by the current build:") + "\n" + unsupported_features_str;
  144. }
  145. if (tooltip_text.is_empty()) {
  146. return;
  147. }
  148. project_version->set_tooltip_text(tooltip_text);
  149. project_unsupported_features->set_tooltip_text(tooltip_text);
  150. project_unsupported_features->show();
  151. } else {
  152. project_unsupported_features->hide();
  153. }
  154. }
  155. bool ProjectListItemControl::should_load_project_icon() const {
  156. return icon_needs_reload;
  157. }
  158. void ProjectListItemControl::set_selected(bool p_selected) {
  159. is_selected = p_selected;
  160. queue_redraw();
  161. }
  162. void ProjectListItemControl::set_is_favorite(bool p_favorite) {
  163. favorite_button->set_modulate(p_favorite ? Color(1, 1, 1, 1) : Color(1, 1, 1, 0.2));
  164. }
  165. void ProjectListItemControl::set_is_missing(bool p_missing) {
  166. if (project_is_missing == p_missing) {
  167. return;
  168. }
  169. project_is_missing = p_missing;
  170. if (project_is_missing) {
  171. project_icon->set_modulate(Color(1, 1, 1, 0.5));
  172. explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
  173. explore_button->set_tooltip_text(TTR("Error: Project is missing on the filesystem."));
  174. } else {
  175. project_icon->set_modulate(Color(1, 1, 1, 1.0));
  176. explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  177. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  178. explore_button->set_tooltip_text(TTR("Show in File Manager"));
  179. #else
  180. // Opening the system file manager is not supported on the Android and web editors.
  181. explore_button->hide();
  182. #endif
  183. }
  184. }
  185. void ProjectListItemControl::set_is_grayed(bool p_grayed) {
  186. if (p_grayed) {
  187. main_vbox->set_modulate(Color(1, 1, 1, 0.5));
  188. // Don't make the icon less prominent if the parent is already grayed out.
  189. explore_button->set_modulate(Color(1, 1, 1, 1.0));
  190. } else {
  191. main_vbox->set_modulate(Color(1, 1, 1, 1.0));
  192. explore_button->set_modulate(Color(1, 1, 1, 0.5));
  193. }
  194. }
  195. void ProjectListItemControl::_bind_methods() {
  196. ADD_SIGNAL(MethodInfo("favorite_pressed"));
  197. ADD_SIGNAL(MethodInfo("explore_pressed"));
  198. }
  199. ProjectListItemControl::ProjectListItemControl() {
  200. set_focus_mode(FocusMode::FOCUS_ALL);
  201. VBoxContainer *favorite_box = memnew(VBoxContainer);
  202. favorite_box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  203. add_child(favorite_box);
  204. favorite_button = memnew(TextureButton);
  205. favorite_button->set_name("FavoriteButton");
  206. // This makes the project's "hover" style display correctly when hovering the favorite icon.
  207. favorite_button->set_mouse_filter(MOUSE_FILTER_PASS);
  208. favorite_box->add_child(favorite_button);
  209. favorite_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_favorite_button_pressed));
  210. project_icon = memnew(TextureRect);
  211. project_icon->set_name("ProjectIcon");
  212. project_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
  213. add_child(project_icon);
  214. main_vbox = memnew(VBoxContainer);
  215. main_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  216. add_child(main_vbox);
  217. Control *ec = memnew(Control);
  218. ec->set_custom_minimum_size(Size2(0, 1));
  219. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  220. main_vbox->add_child(ec);
  221. // Top half, title, tags and unsupported features labels.
  222. {
  223. HBoxContainer *title_hb = memnew(HBoxContainer);
  224. main_vbox->add_child(title_hb);
  225. project_title = memnew(Label);
  226. project_title->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  227. project_title->set_name("ProjectName");
  228. project_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  229. project_title->set_clip_text(true);
  230. title_hb->add_child(project_title);
  231. tag_container = memnew(HBoxContainer);
  232. title_hb->add_child(tag_container);
  233. Control *spacer = memnew(Control);
  234. spacer->set_custom_minimum_size(Size2(10, 10));
  235. title_hb->add_child(spacer);
  236. }
  237. // Bottom half, containing the path and view folder button.
  238. {
  239. HBoxContainer *path_hb = memnew(HBoxContainer);
  240. path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  241. main_vbox->add_child(path_hb);
  242. explore_button = memnew(Button);
  243. explore_button->set_name("ExploreButton");
  244. explore_button->set_flat(true);
  245. path_hb->add_child(explore_button);
  246. explore_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_explore_button_pressed));
  247. project_path = memnew(Label);
  248. project_path->set_name("ProjectPath");
  249. project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  250. project_path->set_clip_text(true);
  251. project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  252. project_path->set_modulate(Color(1, 1, 1, 0.5));
  253. path_hb->add_child(project_path);
  254. project_unsupported_features = memnew(TextureRect);
  255. project_unsupported_features->set_name("ProjectUnsupportedFeatures");
  256. project_unsupported_features->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  257. path_hb->add_child(project_unsupported_features);
  258. project_unsupported_features->hide();
  259. project_version = memnew(Label);
  260. project_version->set_name("ProjectVersion");
  261. project_version->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  262. path_hb->add_child(project_version);
  263. last_edited_info = memnew(Label);
  264. last_edited_info->set_name("LastEditedInfo");
  265. last_edited_info->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  266. last_edited_info->set_tooltip_text(TTR("Last edited timestamp"));
  267. last_edited_info->set_modulate(Color(1, 1, 1, 0.5));
  268. path_hb->add_child(last_edited_info);
  269. Control *spacer = memnew(Control);
  270. spacer->set_custom_minimum_size(Size2(10, 10));
  271. path_hb->add_child(spacer);
  272. }
  273. }
  274. struct ProjectListComparator {
  275. ProjectList::FilterOption order_option = ProjectList::FilterOption::EDIT_DATE;
  276. // operator<
  277. _FORCE_INLINE_ bool operator()(const ProjectList::Item &a, const ProjectList::Item &b) const {
  278. if (a.favorite && !b.favorite) {
  279. return true;
  280. }
  281. if (b.favorite && !a.favorite) {
  282. return false;
  283. }
  284. switch (order_option) {
  285. case ProjectList::PATH:
  286. return a.path < b.path;
  287. case ProjectList::EDIT_DATE:
  288. return a.last_edited > b.last_edited;
  289. case ProjectList::TAGS:
  290. return a.tag_sort_string < b.tag_sort_string;
  291. default:
  292. return a.project_name < b.project_name;
  293. }
  294. }
  295. };
  296. const char *ProjectList::SIGNAL_LIST_CHANGED = "list_changed";
  297. const char *ProjectList::SIGNAL_SELECTION_CHANGED = "selection_changed";
  298. const char *ProjectList::SIGNAL_PROJECT_ASK_OPEN = "project_ask_open";
  299. // Helpers.
  300. bool ProjectList::project_feature_looks_like_version(const String &p_feature) {
  301. return p_feature.contains_char('.') && p_feature.substr(0, 3).is_numeric();
  302. }
  303. // Notifications.
  304. void ProjectList::_notification(int p_what) {
  305. switch (p_what) {
  306. case NOTIFICATION_PROCESS: {
  307. // Load icons as a coroutine to speed up launch when you have hundreds of projects
  308. if (_icon_load_index < _projects.size()) {
  309. Item &item = _projects.write[_icon_load_index];
  310. if (item.control->should_load_project_icon()) {
  311. _load_project_icon(_icon_load_index);
  312. }
  313. _icon_load_index++;
  314. } else {
  315. set_process(false);
  316. }
  317. } break;
  318. }
  319. }
  320. // Initialization & loading.
  321. void ProjectList::_migrate_config() {
  322. // Proposal #1637 moved the project list from editor settings to a separate config file
  323. // If the new config file doesn't exist, populate it from EditorSettings
  324. if (FileAccess::exists(_config_path)) {
  325. return;
  326. }
  327. List<PropertyInfo> properties;
  328. EditorSettings::get_singleton()->get_property_list(&properties);
  329. for (const PropertyInfo &E : properties) {
  330. // This is actually something like "projects/C:::Documents::Godot::Projects::MyGame"
  331. String property_key = E.name;
  332. if (!property_key.begins_with("projects/")) {
  333. continue;
  334. }
  335. String path = EDITOR_GET(property_key);
  336. print_line("Migrating legacy project '" + path + "'.");
  337. String favoriteKey = "favorite_projects/" + property_key.get_slice("/", 1);
  338. bool favorite = EditorSettings::get_singleton()->has_setting(favoriteKey);
  339. add_project(path, favorite);
  340. if (favorite) {
  341. EditorSettings::get_singleton()->erase(favoriteKey);
  342. }
  343. EditorSettings::get_singleton()->erase(property_key);
  344. }
  345. save_config();
  346. }
  347. void ProjectList::save_config() {
  348. _config.save(_config_path);
  349. }
  350. // Load project data from p_property_key and return it in a ProjectList::Item.
  351. // p_favorite is passed directly into the Item.
  352. ProjectList::Item ProjectList::load_project_data(const String &p_path, bool p_favorite) {
  353. String conf = p_path.path_join("project.godot");
  354. bool grayed = false;
  355. bool missing = false;
  356. Ref<ConfigFile> cf = memnew(ConfigFile);
  357. Error cf_err = cf->load(conf);
  358. int config_version = 0;
  359. String project_name = TTR("Unnamed Project");
  360. if (cf_err == OK) {
  361. String cf_project_name = cf->get_value("application", "config/name", "");
  362. if (!cf_project_name.is_empty()) {
  363. project_name = cf_project_name.xml_unescape();
  364. }
  365. config_version = (int)cf->get_value("", "config_version", 0);
  366. }
  367. if (config_version > ProjectSettings::CONFIG_VERSION) {
  368. // Comes from an incompatible (more recent) Godot version, gray it out.
  369. grayed = true;
  370. }
  371. const String description = cf->get_value("application", "config/description", "");
  372. const PackedStringArray tags = cf->get_value("application", "config/tags", PackedStringArray());
  373. const String icon = cf->get_value("application", "config/icon", "");
  374. const String main_scene = cf->get_value("application", "run/main_scene", "");
  375. PackedStringArray project_features = cf->get_value("application", "config/features", PackedStringArray());
  376. PackedStringArray unsupported_features = ProjectSettings::get_unsupported_features(project_features);
  377. String project_version = "?";
  378. for (int i = 0; i < project_features.size(); i++) {
  379. if (ProjectList::project_feature_looks_like_version(project_features[i])) {
  380. project_version = project_features[i];
  381. break;
  382. }
  383. }
  384. if (config_version < ProjectSettings::CONFIG_VERSION) {
  385. // Previous versions may not have unsupported features.
  386. if (config_version == 4) {
  387. unsupported_features.push_back("3.x");
  388. project_version = "3.x";
  389. } else {
  390. unsupported_features.push_back("Unknown version");
  391. }
  392. }
  393. uint64_t last_edited = 0;
  394. if (cf_err == OK) {
  395. // The modification date marks the date the project was last edited.
  396. // This is because the `project.godot` file will always be modified
  397. // when editing a project (but not when running it).
  398. last_edited = FileAccess::get_modified_time(conf);
  399. String fscache = p_path.path_join(".fscache");
  400. if (FileAccess::exists(fscache)) {
  401. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  402. if (cache_modified > last_edited) {
  403. last_edited = cache_modified;
  404. }
  405. }
  406. } else {
  407. grayed = true;
  408. missing = true;
  409. print_line("Project is missing: " + conf);
  410. }
  411. for (const String &tag : tags) {
  412. ProjectManager::get_singleton()->add_new_tag(tag);
  413. }
  414. return Item(project_name, description, project_version, tags, p_path, icon, main_scene, unsupported_features, last_edited, p_favorite, grayed, missing, config_version);
  415. }
  416. void ProjectList::_update_icons_async() {
  417. _icon_load_index = 0;
  418. set_process(true);
  419. }
  420. void ProjectList::_load_project_icon(int p_index) {
  421. Item &item = _projects.write[p_index];
  422. Ref<Texture2D> default_icon = get_editor_theme_icon(SNAME("DefaultProjectIcon"));
  423. Ref<Texture2D> icon;
  424. if (!item.icon.is_empty()) {
  425. Ref<Image> img;
  426. img.instantiate();
  427. Error err = img->load(item.icon.replace_first("res://", item.path + "/"));
  428. if (err == OK) {
  429. img->resize(default_icon->get_width(), default_icon->get_height(), Image::INTERPOLATE_LANCZOS);
  430. icon = ImageTexture::create_from_image(img);
  431. }
  432. }
  433. if (icon.is_null()) {
  434. icon = default_icon;
  435. }
  436. item.control->set_project_icon(icon);
  437. }
  438. // Project list updates.
  439. void ProjectList::update_project_list() {
  440. // This is a full, hard reload of the list. Don't call this unless really required, it's expensive.
  441. // If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
  442. // FIXME: Does it really have to be a full, hard reload? Runtime updates should be made much cheaper.
  443. if (ProjectManager::get_singleton()->is_initialized()) {
  444. // Clear whole list
  445. for (int i = 0; i < _projects.size(); ++i) {
  446. Item &project = _projects.write[i];
  447. CRASH_COND(project.control == nullptr);
  448. memdelete(project.control); // Why not queue_free()?
  449. }
  450. _projects.clear();
  451. _last_clicked = "";
  452. _selected_project_paths.clear();
  453. load_project_list();
  454. }
  455. // Create controls
  456. for (int i = 0; i < _projects.size(); ++i) {
  457. _create_project_item_control(i);
  458. }
  459. sort_projects();
  460. _update_icons_async();
  461. update_dock_menu();
  462. set_v_scroll(0);
  463. emit_signal(SNAME(SIGNAL_LIST_CHANGED));
  464. }
  465. void ProjectList::sort_projects() {
  466. SortArray<Item, ProjectListComparator> sorter;
  467. sorter.compare.order_option = _order_option;
  468. sorter.sort(_projects.ptrw(), _projects.size());
  469. String search_term;
  470. PackedStringArray tags;
  471. if (!_search_term.is_empty()) {
  472. PackedStringArray search_parts = _search_term.split(" ");
  473. if (search_parts.size() > 1 || search_parts[0].begins_with("tag:")) {
  474. PackedStringArray remaining;
  475. for (const String &part : search_parts) {
  476. if (part.begins_with("tag:")) {
  477. tags.push_back(part.get_slice(":", 1));
  478. } else {
  479. remaining.append(part);
  480. }
  481. }
  482. search_term = String(" ").join(remaining); // Search term without tags.
  483. } else {
  484. search_term = _search_term;
  485. }
  486. }
  487. for (int i = 0; i < _projects.size(); ++i) {
  488. Item &item = _projects.write[i];
  489. bool item_visible = true;
  490. if (!_search_term.is_empty()) {
  491. String search_path;
  492. if (search_term.contains_char('/')) {
  493. // Search path will match the whole path
  494. search_path = item.path;
  495. } else {
  496. // Search path will only match the last path component to make searching more strict
  497. search_path = item.path.get_file();
  498. }
  499. bool missing_tags = false;
  500. for (const String &tag : tags) {
  501. if (!item.tags.has(tag)) {
  502. missing_tags = true;
  503. break;
  504. }
  505. }
  506. // When searching, display projects whose name or path contain the search term and whose tags match the searched tags.
  507. item_visible = !missing_tags && (search_term.is_empty() || item.project_name.containsn(search_term) || search_path.containsn(search_term));
  508. }
  509. item.control->set_visible(item_visible);
  510. }
  511. for (int i = 0; i < _projects.size(); ++i) {
  512. Item &item = _projects.write[i];
  513. item.control->get_parent()->move_child(item.control, i);
  514. }
  515. // Rewind the coroutine because order of projects changed
  516. _update_icons_async();
  517. update_dock_menu();
  518. }
  519. int ProjectList::get_project_count() const {
  520. return _projects.size();
  521. }
  522. void ProjectList::find_projects(const String &p_path) {
  523. PackedStringArray paths = { p_path };
  524. find_projects_multiple(paths);
  525. }
  526. void ProjectList::find_projects_multiple(const PackedStringArray &p_paths) {
  527. List<String> projects;
  528. for (int i = 0; i < p_paths.size(); i++) {
  529. const String &base_path = p_paths.get(i);
  530. print_verbose(vformat("Scanning for projects in \"%s\".", base_path));
  531. _scan_folder_recursive(base_path, &projects);
  532. print_verbose(vformat("Found %d project(s).", projects.size()));
  533. }
  534. for (const String &E : projects) {
  535. add_project(E, false);
  536. }
  537. save_config();
  538. if (ProjectManager::get_singleton()->is_initialized()) {
  539. update_project_list();
  540. }
  541. }
  542. void ProjectList::load_project_list() {
  543. List<String> sections;
  544. _config.load(_config_path);
  545. _config.get_sections(&sections);
  546. for (const String &path : sections) {
  547. bool favorite = _config.get_value(path, "favorite", false);
  548. _projects.push_back(load_project_data(path, favorite));
  549. }
  550. }
  551. void ProjectList::_scan_folder_recursive(const String &p_path, List<String> *r_projects) {
  552. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  553. Error error = da->change_dir(p_path);
  554. ERR_FAIL_COND_MSG(error != OK, vformat("Failed to open the path \"%s\" for scanning (code %d).", p_path, error));
  555. da->list_dir_begin();
  556. String n = da->get_next();
  557. while (!n.is_empty()) {
  558. if (da->current_is_dir() && n[0] != '.') {
  559. _scan_folder_recursive(da->get_current_dir().path_join(n), r_projects);
  560. } else if (n == "project.godot") {
  561. r_projects->push_back(da->get_current_dir());
  562. }
  563. n = da->get_next();
  564. }
  565. da->list_dir_end();
  566. }
  567. // Project list items.
  568. void ProjectList::add_project(const String &dir_path, bool favorite) {
  569. if (!_config.has_section(dir_path)) {
  570. _config.set_value(dir_path, "favorite", favorite);
  571. }
  572. }
  573. void ProjectList::set_project_version(const String &p_project_path, int p_version) {
  574. for (ProjectList::Item &E : _projects) {
  575. if (E.path == p_project_path) {
  576. E.version = p_version;
  577. break;
  578. }
  579. }
  580. }
  581. int ProjectList::refresh_project(const String &dir_path) {
  582. // Reloads information about a specific project.
  583. // If it wasn't loaded and should be in the list, it is added (i.e new project).
  584. // If it isn't in the list anymore, it is removed.
  585. // If it is in the list but doesn't exist anymore, it is marked as missing.
  586. bool should_be_in_list = _config.has_section(dir_path);
  587. bool is_favourite = _config.get_value(dir_path, "favorite", false);
  588. bool was_selected = _selected_project_paths.has(dir_path);
  589. // Remove item in any case
  590. for (int i = 0; i < _projects.size(); ++i) {
  591. const Item &existing_item = _projects[i];
  592. if (existing_item.path == dir_path) {
  593. _remove_project(i, false);
  594. break;
  595. }
  596. }
  597. int index = -1;
  598. if (should_be_in_list) {
  599. // Recreate it with updated info
  600. Item item = load_project_data(dir_path, is_favourite);
  601. _projects.push_back(item);
  602. _create_project_item_control(_projects.size() - 1);
  603. sort_projects();
  604. for (int i = 0; i < _projects.size(); ++i) {
  605. if (_projects[i].path == dir_path) {
  606. if (was_selected) {
  607. select_project(i);
  608. ensure_project_visible(i);
  609. }
  610. _load_project_icon(i);
  611. index = i;
  612. break;
  613. }
  614. }
  615. }
  616. return index;
  617. }
  618. void ProjectList::ensure_project_visible(int p_index) {
  619. const Item &item = _projects[p_index];
  620. ensure_control_visible(item.control);
  621. }
  622. void ProjectList::_create_project_item_control(int p_index) {
  623. // Will be added last in the list, so make sure indexes match
  624. ERR_FAIL_COND(p_index != project_list_vbox->get_child_count());
  625. Item &item = _projects.write[p_index];
  626. ERR_FAIL_COND(item.control != nullptr); // Already created
  627. ProjectListItemControl *hb = memnew(ProjectListItemControl);
  628. hb->add_theme_constant_override("separation", 10 * EDSCALE);
  629. hb->set_project_title(!item.missing ? item.project_name : TTR("Missing Project"));
  630. hb->set_project_path(item.path);
  631. hb->set_tooltip_text(item.description);
  632. hb->set_tags(item.tags, this);
  633. hb->set_unsupported_features(item.unsupported_features.duplicate());
  634. hb->set_project_version(item.project_version);
  635. hb->set_last_edited_info(!item.missing ? Time::get_singleton()->get_datetime_string_from_unix_time(item.last_edited, true) : TTR("Missing Date"));
  636. hb->set_is_favorite(item.favorite);
  637. hb->set_is_missing(item.missing);
  638. hb->set_is_grayed(item.grayed);
  639. hb->connect(SceneStringName(gui_input), callable_mp(this, &ProjectList::_list_item_input).bind(hb));
  640. hb->connect("favorite_pressed", callable_mp(this, &ProjectList::_on_favorite_pressed).bind(hb));
  641. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  642. hb->connect("explore_pressed", callable_mp(this, &ProjectList::_on_explore_pressed).bind(item.path));
  643. #endif
  644. project_list_vbox->add_child(hb);
  645. item.control = hb;
  646. }
  647. void ProjectList::_toggle_project(int p_index) {
  648. // This methods adds to the selection or removes from the
  649. // selection.
  650. Item &item = _projects.write[p_index];
  651. if (_selected_project_paths.has(item.path)) {
  652. _deselect_project_nocheck(p_index);
  653. } else {
  654. _select_project_nocheck(p_index);
  655. }
  656. }
  657. void ProjectList::_remove_project(int p_index, bool p_update_config) {
  658. const Item item = _projects[p_index]; // Take a copy
  659. _selected_project_paths.erase(item.path);
  660. if (_last_clicked == item.path) {
  661. _last_clicked = "";
  662. }
  663. memdelete(item.control);
  664. _projects.remove_at(p_index);
  665. if (p_update_config) {
  666. _config.erase_section(item.path);
  667. // Not actually saving the file, in case you are doing more changes to settings
  668. }
  669. update_dock_menu();
  670. }
  671. void ProjectList::_list_item_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  672. Ref<InputEventMouseButton> mb = p_ev;
  673. int clicked_index = p_hb->get_index();
  674. const Item &clicked_project = _projects[clicked_index];
  675. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  676. if (mb->is_shift_pressed() && _selected_project_paths.size() > 0 && !_last_clicked.is_empty() && clicked_project.path != _last_clicked) {
  677. int anchor_index = -1;
  678. for (int i = 0; i < _projects.size(); ++i) {
  679. const Item &p = _projects[i];
  680. if (p.path == _last_clicked) {
  681. anchor_index = p.control->get_index();
  682. break;
  683. }
  684. }
  685. CRASH_COND(anchor_index == -1);
  686. _select_project_range(anchor_index, clicked_index);
  687. } else if (mb->is_command_or_control_pressed()) {
  688. _toggle_project(clicked_index);
  689. } else {
  690. _last_clicked = clicked_project.path;
  691. select_project(clicked_index);
  692. }
  693. emit_signal(SNAME(SIGNAL_SELECTION_CHANGED));
  694. // Do not allow opening a project more than once using a single project manager instance.
  695. // Opening the same project in several editor instances at once can lead to various issues.
  696. if (!mb->is_command_or_control_pressed() && mb->is_double_click() && !project_opening_initiated) {
  697. emit_signal(SNAME(SIGNAL_PROJECT_ASK_OPEN));
  698. }
  699. }
  700. }
  701. void ProjectList::_on_favorite_pressed(Node *p_hb) {
  702. ProjectListItemControl *control = Object::cast_to<ProjectListItemControl>(p_hb);
  703. int index = control->get_index();
  704. Item item = _projects.write[index]; // Take copy
  705. item.favorite = !item.favorite;
  706. _config.set_value(item.path, "favorite", item.favorite);
  707. save_config();
  708. _projects.write[index] = item;
  709. control->set_is_favorite(item.favorite);
  710. sort_projects();
  711. if (item.favorite) {
  712. for (int i = 0; i < _projects.size(); ++i) {
  713. if (_projects[i].path == item.path) {
  714. ensure_project_visible(i);
  715. break;
  716. }
  717. }
  718. }
  719. update_dock_menu();
  720. }
  721. void ProjectList::_on_explore_pressed(const String &p_path) {
  722. OS::get_singleton()->shell_show_in_file_manager(p_path, true);
  723. }
  724. // Project list selection.
  725. void ProjectList::_clear_project_selection() {
  726. Vector<Item> previous_selected_items = get_selected_projects();
  727. _selected_project_paths.clear();
  728. for (int i = 0; i < previous_selected_items.size(); ++i) {
  729. previous_selected_items[i].control->set_selected(false);
  730. }
  731. }
  732. void ProjectList::_select_project_nocheck(int p_index) {
  733. Item &item = _projects.write[p_index];
  734. _selected_project_paths.insert(item.path);
  735. item.control->set_selected(true);
  736. }
  737. void ProjectList::_deselect_project_nocheck(int p_index) {
  738. Item &item = _projects.write[p_index];
  739. _selected_project_paths.erase(item.path);
  740. item.control->set_selected(false);
  741. }
  742. inline void _sort_project_range(int &a, int &b) {
  743. if (a > b) {
  744. int temp = a;
  745. a = b;
  746. b = temp;
  747. }
  748. }
  749. void ProjectList::_select_project_range(int p_begin, int p_end) {
  750. _clear_project_selection();
  751. _sort_project_range(p_begin, p_end);
  752. for (int i = p_begin; i <= p_end; ++i) {
  753. _select_project_nocheck(i);
  754. }
  755. }
  756. void ProjectList::select_project(int p_index) {
  757. // This method keeps only one project selected.
  758. _clear_project_selection();
  759. _select_project_nocheck(p_index);
  760. }
  761. void ProjectList::select_first_visible_project() {
  762. _clear_project_selection();
  763. for (int i = 0; i < _projects.size(); i++) {
  764. if (_projects[i].control->is_visible()) {
  765. _select_project_nocheck(i);
  766. break;
  767. }
  768. }
  769. }
  770. Vector<ProjectList::Item> ProjectList::get_selected_projects() const {
  771. Vector<Item> items;
  772. if (_selected_project_paths.size() == 0) {
  773. return items;
  774. }
  775. items.resize(_selected_project_paths.size());
  776. int j = 0;
  777. for (int i = 0; i < _projects.size(); ++i) {
  778. const Item &item = _projects[i];
  779. if (_selected_project_paths.has(item.path)) {
  780. items.write[j++] = item;
  781. }
  782. }
  783. ERR_FAIL_COND_V(j != items.size(), items);
  784. return items;
  785. }
  786. const HashSet<String> &ProjectList::get_selected_project_keys() const {
  787. // Faster if that's all you need
  788. return _selected_project_paths;
  789. }
  790. int ProjectList::get_single_selected_index() const {
  791. if (_selected_project_paths.size() == 0) {
  792. // Default selection
  793. return 0;
  794. }
  795. String key;
  796. if (_selected_project_paths.size() == 1) {
  797. // Only one selected
  798. key = *_selected_project_paths.begin();
  799. } else {
  800. // Multiple selected, consider the last clicked one as "main"
  801. key = _last_clicked;
  802. }
  803. for (int i = 0; i < _projects.size(); ++i) {
  804. if (_projects[i].path == key) {
  805. return i;
  806. }
  807. }
  808. return 0;
  809. }
  810. void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
  811. if (_selected_project_paths.size() == 0) {
  812. return;
  813. }
  814. for (int i = 0; i < _projects.size(); ++i) {
  815. Item &item = _projects.write[i];
  816. if (_selected_project_paths.has(item.path) && item.control->is_visible()) {
  817. _config.erase_section(item.path);
  818. // Comment out for now until we have a better warning system to
  819. // ensure users delete their project only.
  820. //if (p_delete_project_contents) {
  821. // OS::get_singleton()->move_to_trash(item.path);
  822. //}
  823. memdelete(item.control);
  824. _projects.remove_at(i);
  825. --i;
  826. }
  827. }
  828. save_config();
  829. _selected_project_paths.clear();
  830. _last_clicked = "";
  831. update_dock_menu();
  832. }
  833. // Missing projects.
  834. bool ProjectList::is_any_project_missing() const {
  835. for (int i = 0; i < _projects.size(); ++i) {
  836. if (_projects[i].missing) {
  837. return true;
  838. }
  839. }
  840. return false;
  841. }
  842. void ProjectList::erase_missing_projects() {
  843. if (_projects.is_empty()) {
  844. return;
  845. }
  846. int deleted_count = 0;
  847. int remaining_count = 0;
  848. for (int i = 0; i < _projects.size(); ++i) {
  849. const Item &item = _projects[i];
  850. if (item.missing) {
  851. _remove_project(i, true);
  852. --i;
  853. ++deleted_count;
  854. } else {
  855. ++remaining_count;
  856. }
  857. }
  858. print_line("Removed " + itos(deleted_count) + " projects from the list, remaining " + itos(remaining_count) + " projects");
  859. save_config();
  860. }
  861. // Project list sorting and filtering.
  862. void ProjectList::set_search_term(String p_search_term) {
  863. _search_term = p_search_term;
  864. }
  865. void ProjectList::add_search_tag(const String &p_tag) {
  866. const String tag_string = "tag:" + p_tag;
  867. int exists = _search_term.find(tag_string);
  868. if (exists > -1) {
  869. _search_term = _search_term.erase(exists, tag_string.length() + 1);
  870. } else if (_search_term.is_empty() || _search_term.ends_with(" ")) {
  871. _search_term += tag_string;
  872. } else {
  873. _search_term += " " + tag_string;
  874. }
  875. ProjectManager::get_singleton()->get_search_box()->set_text(_search_term);
  876. sort_projects();
  877. }
  878. void ProjectList::set_order_option(int p_option) {
  879. FilterOption selected = (FilterOption)p_option;
  880. EditorSettings::get_singleton()->set("project_manager/sorting_order", p_option);
  881. EditorSettings::get_singleton()->save();
  882. _order_option = selected;
  883. sort_projects();
  884. }
  885. // Global menu integration.
  886. void ProjectList::update_dock_menu() {
  887. if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {
  888. return;
  889. }
  890. RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);
  891. NativeMenu::get_singleton()->clear(dock_rid);
  892. int favs_added = 0;
  893. int total_added = 0;
  894. for (int i = 0; i < _projects.size(); ++i) {
  895. if (!_projects[i].grayed && !_projects[i].missing) {
  896. if (_projects[i].favorite) {
  897. favs_added++;
  898. } else {
  899. if (favs_added != 0) {
  900. NativeMenu::get_singleton()->add_separator(dock_rid);
  901. }
  902. favs_added = 0;
  903. }
  904. NativeMenu::get_singleton()->add_item(dock_rid, _projects[i].project_name + " ( " + _projects[i].path + " )", callable_mp(this, &ProjectList::_global_menu_open_project), Callable(), i);
  905. total_added++;
  906. }
  907. }
  908. if (total_added != 0) {
  909. NativeMenu::get_singleton()->add_separator(dock_rid);
  910. }
  911. NativeMenu::get_singleton()->add_item(dock_rid, TTR("New Window"), callable_mp(this, &ProjectList::_global_menu_new_window));
  912. }
  913. void ProjectList::_global_menu_new_window(const Variant &p_tag) {
  914. List<String> args;
  915. args.push_back("-p");
  916. OS::get_singleton()->create_instance(args);
  917. }
  918. void ProjectList::_global_menu_open_project(const Variant &p_tag) {
  919. int idx = (int)p_tag;
  920. if (idx >= 0 && idx < _projects.size()) {
  921. String conf = _projects[idx].path.path_join("project.godot");
  922. List<String> args;
  923. args.push_back(conf);
  924. OS::get_singleton()->create_instance(args);
  925. }
  926. }
  927. // Object methods.
  928. void ProjectList::_bind_methods() {
  929. ADD_SIGNAL(MethodInfo(SIGNAL_LIST_CHANGED));
  930. ADD_SIGNAL(MethodInfo(SIGNAL_SELECTION_CHANGED));
  931. ADD_SIGNAL(MethodInfo(SIGNAL_PROJECT_ASK_OPEN));
  932. }
  933. ProjectList::ProjectList() {
  934. project_list_vbox = memnew(VBoxContainer);
  935. project_list_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  936. add_child(project_list_vbox);
  937. _config_path = EditorPaths::get_singleton()->get_data_dir().path_join("projects.cfg");
  938. _migrate_config();
  939. }