project_list.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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_string_names.h"
  36. #include "editor/file_system/editor_paths.h"
  37. #include "editor/project_manager/project_manager.h"
  38. #include "editor/project_manager/project_tag.h"
  39. #include "editor/settings/editor_settings.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/gui/button.h"
  42. #include "scene/gui/dialogs.h"
  43. #include "scene/gui/label.h"
  44. #include "scene/gui/line_edit.h"
  45. #include "scene/gui/popup_menu.h"
  46. #include "scene/gui/progress_bar.h"
  47. #include "scene/gui/texture_button.h"
  48. #include "scene/gui/texture_rect.h"
  49. #include "scene/resources/image_texture.h"
  50. void ProjectListItemControl::_notification(int p_what) {
  51. switch (p_what) {
  52. case NOTIFICATION_THEME_CHANGED: {
  53. if (icon_needs_reload) {
  54. // The project icon may not be loaded by the time the control is displayed,
  55. // so use a loading placeholder.
  56. project_icon->set_texture(get_editor_theme_icon(SNAME("ProjectIconLoading")));
  57. }
  58. project_title->begin_bulk_theme_override();
  59. project_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title"), EditorStringName(EditorFonts)));
  60. project_title->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("title_size"), EditorStringName(EditorFonts)));
  61. project_title->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Tree")));
  62. project_title->end_bulk_theme_override();
  63. project_path->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Tree")));
  64. project_unsupported_features->set_texture(get_editor_theme_icon(SNAME("NodeWarning")));
  65. favorite_focus_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  66. _update_favorite_button_focus_color();
  67. if (is_favorite) {
  68. favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Favorites")));
  69. } else {
  70. favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Unfavorite")));
  71. }
  72. if (project_is_missing) {
  73. explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
  74. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  75. } else {
  76. explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  77. #endif
  78. }
  79. if (touch_menu_button) {
  80. touch_menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
  81. }
  82. } break;
  83. case NOTIFICATION_MOUSE_ENTER: {
  84. is_hovering = true;
  85. queue_redraw();
  86. queue_accessibility_update();
  87. } break;
  88. case NOTIFICATION_MOUSE_EXIT: {
  89. is_hovering = false;
  90. queue_redraw();
  91. queue_accessibility_update();
  92. } break;
  93. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  94. RID ae = get_accessibility_element();
  95. ERR_FAIL_COND(ae.is_null());
  96. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX_OPTION);
  97. DisplayServer::get_singleton()->accessibility_update_set_name(ae, TTR("Project") + " " + project_title->get_text());
  98. DisplayServer::get_singleton()->accessibility_update_set_value(ae, project_title->get_text());
  99. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &ProjectListItemControl::_accessibility_action_open));
  100. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &ProjectListItemControl::_accessibility_action_scroll_into_view));
  101. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &ProjectListItemControl::_accessibility_action_focus));
  102. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &ProjectListItemControl::_accessibility_action_blur));
  103. ProjectList *pl = get_list();
  104. if (pl) {
  105. DisplayServer::get_singleton()->accessibility_update_set_list_item_index(ae, pl->get_index(this));
  106. }
  107. DisplayServer::get_singleton()->accessibility_update_set_list_item_level(ae, 0);
  108. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(ae, is_selected);
  109. } break;
  110. case NOTIFICATION_FOCUS_ENTER: {
  111. ProjectList *pl = get_list();
  112. if (pl) {
  113. int idx = pl->get_index(this);
  114. if (idx >= 0) {
  115. pl->ensure_project_visible(idx);
  116. pl->select_project(idx);
  117. pl->emit_signal(SNAME(ProjectList::SIGNAL_SELECTION_CHANGED));
  118. }
  119. }
  120. } break;
  121. case NOTIFICATION_DRAW: {
  122. if (is_selected) {
  123. draw_style_box(get_theme_stylebox(SNAME("selected"), SNAME("Tree")), Rect2(Point2(), get_size()));
  124. }
  125. if (is_hovering) {
  126. draw_style_box(get_theme_stylebox(SNAME("hovered"), SNAME("Tree")), Rect2(Point2(), get_size()));
  127. }
  128. if (has_focus()) {
  129. draw_style_box(get_theme_stylebox(SNAME("focus"), SNAME("Tree")), Rect2(Point2(), get_size()));
  130. }
  131. draw_line(Point2(0, get_size().y + 1), Point2(get_size().x, get_size().y + 1), get_theme_color(SNAME("guide_color"), SNAME("Tree")));
  132. } break;
  133. }
  134. }
  135. ProjectList *ProjectListItemControl::get_list() const {
  136. if (!is_inside_tree()) {
  137. return nullptr;
  138. }
  139. ProjectList *pl = Object::cast_to<ProjectList>(get_parent()->get_parent());
  140. return pl;
  141. }
  142. void ProjectListItemControl::_accessibility_action_scroll_into_view(const Variant &p_data) {
  143. ProjectList *pl = get_list();
  144. if (pl) {
  145. int idx = pl->get_index(this);
  146. if (idx >= 0) {
  147. pl->ensure_project_visible(idx);
  148. }
  149. }
  150. }
  151. void ProjectListItemControl::_accessibility_action_open(const Variant &p_data) {
  152. ProjectList *pl = get_list();
  153. if (pl && !pl->project_opening_initiated) {
  154. pl->emit_signal(SNAME(ProjectList::SIGNAL_PROJECT_ASK_OPEN));
  155. }
  156. }
  157. void ProjectListItemControl::_accessibility_action_focus(const Variant &p_data) {
  158. ProjectList *pl = get_list();
  159. if (pl) {
  160. int idx = pl->get_index(this);
  161. if (idx >= 0) {
  162. pl->ensure_project_visible(idx);
  163. pl->select_project(idx);
  164. }
  165. }
  166. }
  167. void ProjectListItemControl::_accessibility_action_blur(const Variant &p_data) {
  168. ProjectList *pl = get_list();
  169. if (pl) {
  170. int idx = pl->get_index(this);
  171. if (idx >= 0) {
  172. pl->ensure_project_visible(idx);
  173. pl->deselect_project(idx);
  174. }
  175. }
  176. }
  177. void ProjectListItemControl::_update_favorite_button_focus_color() {
  178. if (favorite_button->has_focus()) {
  179. favorite_button->set_self_modulate(favorite_focus_color);
  180. } else {
  181. favorite_button->set_self_modulate(Color(1.0, 1.0, 1.0, 1.0));
  182. }
  183. }
  184. void ProjectListItemControl::_favorite_button_pressed() {
  185. emit_signal(SNAME("favorite_pressed"));
  186. }
  187. void ProjectListItemControl::_explore_button_pressed() {
  188. emit_signal(SNAME("explore_pressed"));
  189. }
  190. void ProjectListItemControl::_request_menu() {
  191. emit_signal(SNAME("request_menu"), Vector2(touch_menu_button->get_position()));
  192. }
  193. void ProjectListItemControl::set_project_title(const String &p_title) {
  194. project_title->set_text(p_title);
  195. project_title->set_accessibility_name(TTRC("Project Name"));
  196. queue_accessibility_update();
  197. }
  198. void ProjectListItemControl::set_project_path(const String &p_path) {
  199. project_path->set_text(p_path);
  200. project_path->set_accessibility_name(TTRC("Project Path"));
  201. queue_accessibility_update();
  202. }
  203. void ProjectListItemControl::set_tags(const PackedStringArray &p_tags, ProjectList *p_parent_list) {
  204. for (const String &tag : p_tags) {
  205. ProjectTag *tag_control = memnew(ProjectTag(tag));
  206. tag_container->add_child(tag_control);
  207. tag_control->connect_button_to(callable_mp(p_parent_list, &ProjectList::add_search_tag).bind(tag));
  208. }
  209. }
  210. void ProjectListItemControl::set_project_icon(const Ref<Texture2D> &p_icon) {
  211. icon_needs_reload = false;
  212. // The default project icon is 128×128 to look crisp on hiDPI displays,
  213. // but we want the actual displayed size to be 64×64 on loDPI displays.
  214. project_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  215. project_icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  216. project_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  217. project_icon->set_texture(p_icon);
  218. }
  219. void ProjectListItemControl::set_last_edited_info(const String &p_info) {
  220. last_edited_info->set_text(p_info);
  221. }
  222. void ProjectListItemControl::set_project_version(const String &p_info) {
  223. project_version->set_text(p_info);
  224. }
  225. void ProjectListItemControl::set_unsupported_features(PackedStringArray p_features) {
  226. if (p_features.size() > 0) {
  227. String tooltip_text = "";
  228. for (int i = 0; i < p_features.size(); i++) {
  229. if (ProjectList::project_feature_looks_like_version(p_features[i])) {
  230. PackedStringArray project_version_split = p_features[i].split(".");
  231. int project_version_major = 0, project_version_minor = 0;
  232. if (project_version_split.size() >= 2) {
  233. project_version_major = project_version_split[0].to_int();
  234. project_version_minor = project_version_split[1].to_int();
  235. }
  236. if (GODOT_VERSION_MAJOR != project_version_major || GODOT_VERSION_MINOR <= project_version_minor) {
  237. // Don't show a warning if the project was last edited in a previous minor version.
  238. tooltip_text += TTR("This project was last edited in a different Godot version: ") + p_features[i] + "\n";
  239. }
  240. p_features.remove_at(i);
  241. i--;
  242. }
  243. }
  244. if (p_features.size() > 0) {
  245. String unsupported_features_str = String(", ").join(p_features);
  246. tooltip_text += TTR("This project uses features unsupported by the current build:") + "\n" + unsupported_features_str;
  247. }
  248. if (tooltip_text.is_empty()) {
  249. return;
  250. }
  251. project_version->set_tooltip_text(tooltip_text);
  252. project_unsupported_features->set_focus_mode(FOCUS_ACCESSIBILITY);
  253. project_unsupported_features->set_tooltip_text(tooltip_text);
  254. project_unsupported_features->show();
  255. } else {
  256. project_unsupported_features->hide();
  257. }
  258. }
  259. bool ProjectListItemControl::should_load_project_icon() const {
  260. return icon_needs_reload;
  261. }
  262. void ProjectListItemControl::set_selected(bool p_selected) {
  263. is_selected = p_selected;
  264. queue_redraw();
  265. queue_accessibility_update();
  266. }
  267. void ProjectListItemControl::set_is_favorite(bool p_favorite) {
  268. is_favorite = p_favorite;
  269. if (p_favorite) {
  270. favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Favorites")));
  271. } else {
  272. favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Unfavorite")));
  273. }
  274. }
  275. void ProjectListItemControl::set_is_missing(bool p_missing) {
  276. project_is_missing = p_missing;
  277. if (project_is_missing) {
  278. project_icon->set_modulate(Color(1, 1, 1, 0.5));
  279. explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
  280. explore_button->set_tooltip_text(TTRC("Error: Project is missing on the filesystem."));
  281. } else {
  282. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  283. explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  284. explore_button->set_tooltip_text(TTRC("Show in File Manager"));
  285. #else
  286. // Opening the system file manager is not supported on the Android and web editors.
  287. explore_button->hide();
  288. #endif
  289. }
  290. }
  291. void ProjectListItemControl::set_is_grayed(bool p_grayed) {
  292. if (p_grayed) {
  293. main_vbox->set_modulate(Color(1, 1, 1, 0.5));
  294. // Don't make the icon less prominent if the parent is already grayed out.
  295. explore_button->set_modulate(Color(1, 1, 1, 1.0));
  296. } else {
  297. main_vbox->set_modulate(Color(1, 1, 1, 1.0));
  298. explore_button->set_modulate(Color(1, 1, 1, 0.5));
  299. }
  300. }
  301. void ProjectListItemControl::_bind_methods() {
  302. ADD_SIGNAL(MethodInfo("favorite_pressed"));
  303. ADD_SIGNAL(MethodInfo("explore_pressed"));
  304. ADD_SIGNAL(MethodInfo("request_menu"));
  305. }
  306. ProjectListItemControl::ProjectListItemControl() {
  307. set_focus_mode(FocusMode::FOCUS_ALL);
  308. set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  309. VBoxContainer *favorite_box = memnew(VBoxContainer);
  310. favorite_box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  311. add_child(favorite_box);
  312. favorite_button = memnew(TextureButton);
  313. favorite_button->set_name("FavoriteButton");
  314. favorite_button->set_tooltip_text(TTRC("Toggle Favorite"));
  315. favorite_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
  316. // This makes the project's "hover" style display correctly when hovering the favorite icon.
  317. favorite_button->set_mouse_filter(MOUSE_FILTER_PASS);
  318. favorite_box->add_child(favorite_button);
  319. favorite_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_favorite_button_pressed));
  320. favorite_button->connect(SceneStringName(focus_entered), callable_mp(this, &ProjectListItemControl::_update_favorite_button_focus_color));
  321. favorite_button->connect(SceneStringName(focus_exited), callable_mp(this, &ProjectListItemControl::_update_favorite_button_focus_color));
  322. project_icon = memnew(TextureRect);
  323. project_icon->set_name("ProjectIcon");
  324. project_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
  325. add_child(project_icon);
  326. main_vbox = memnew(VBoxContainer);
  327. main_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  328. add_child(main_vbox);
  329. Control *ec = memnew(Control);
  330. ec->set_custom_minimum_size(Size2(0, 1));
  331. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  332. main_vbox->add_child(ec);
  333. // Top half, title, tags and unsupported features labels.
  334. {
  335. HBoxContainer *title_hb = memnew(HBoxContainer);
  336. main_vbox->add_child(title_hb);
  337. project_title = memnew(Label);
  338. project_title->set_focus_mode(FOCUS_ACCESSIBILITY);
  339. project_title->set_name("ProjectName");
  340. project_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  341. project_title->set_clip_text(true);
  342. title_hb->add_child(project_title);
  343. tag_container = memnew(HBoxContainer);
  344. title_hb->add_child(tag_container);
  345. Control *spacer = memnew(Control);
  346. spacer->set_custom_minimum_size(Size2(10, 10));
  347. title_hb->add_child(spacer);
  348. }
  349. // Bottom half, containing the path and view folder button.
  350. {
  351. HBoxContainer *path_hb = memnew(HBoxContainer);
  352. path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  353. main_vbox->add_child(path_hb);
  354. explore_button = memnew(Button);
  355. explore_button->set_name("ExploreButton");
  356. explore_button->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
  357. explore_button->set_tooltip_text(TTRC("Open in file manager"));
  358. explore_button->set_flat(true);
  359. path_hb->add_child(explore_button);
  360. explore_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_explore_button_pressed));
  361. project_path = memnew(Label);
  362. project_path->set_name("ProjectPath");
  363. project_path->set_focus_mode(FOCUS_ACCESSIBILITY);
  364. project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  365. project_path->set_clip_text(true);
  366. project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  367. project_path->set_modulate(Color(1, 1, 1, 0.5));
  368. path_hb->add_child(project_path);
  369. project_unsupported_features = memnew(TextureRect);
  370. project_unsupported_features->set_name("ProjectUnsupportedFeatures");
  371. project_unsupported_features->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  372. path_hb->add_child(project_unsupported_features);
  373. project_unsupported_features->hide();
  374. project_version = memnew(Label);
  375. project_version->set_focus_mode(FOCUS_ACCESSIBILITY);
  376. project_version->set_name("ProjectVersion");
  377. project_version->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  378. path_hb->add_child(project_version);
  379. last_edited_info = memnew(Label);
  380. last_edited_info->set_focus_mode(FOCUS_ACCESSIBILITY);
  381. last_edited_info->set_name("LastEditedInfo");
  382. last_edited_info->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  383. last_edited_info->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
  384. last_edited_info->set_tooltip_text(TTRC("Last edited timestamp"));
  385. last_edited_info->set_modulate(Color(1, 1, 1, 0.5));
  386. path_hb->add_child(last_edited_info);
  387. Control *spacer = memnew(Control);
  388. spacer->set_custom_minimum_size(Size2(10, 10));
  389. path_hb->add_child(spacer);
  390. }
  391. if (DisplayServer::get_singleton()->is_touchscreen_available()) {
  392. touch_menu_button = memnew(Button);
  393. touch_menu_button->set_theme_type_variation(SceneStringName(FlatButton));
  394. touch_menu_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  395. add_child(touch_menu_button);
  396. touch_menu_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_request_menu));
  397. }
  398. }
  399. struct ProjectListComparator {
  400. ProjectList::FilterOption order_option = ProjectList::FilterOption::EDIT_DATE;
  401. // operator<
  402. _FORCE_INLINE_ bool operator()(const ProjectList::Item &a, const ProjectList::Item &b) const {
  403. if (a.favorite && !b.favorite) {
  404. return true;
  405. }
  406. if (b.favorite && !a.favorite) {
  407. return false;
  408. }
  409. switch (order_option) {
  410. case ProjectList::PATH:
  411. return a.path < b.path;
  412. case ProjectList::EDIT_DATE:
  413. return a.last_edited > b.last_edited;
  414. case ProjectList::TAGS:
  415. return a.tag_sort_string < b.tag_sort_string;
  416. default:
  417. return a.project_name < b.project_name;
  418. }
  419. }
  420. };
  421. // Helpers.
  422. bool ProjectList::project_feature_looks_like_version(const String &p_feature) {
  423. return p_feature.contains_char('.') && p_feature.substr(0, 3).is_numeric();
  424. }
  425. // Notifications.
  426. void ProjectList::_notification(int p_what) {
  427. switch (p_what) {
  428. case NOTIFICATION_TRANSLATION_CHANGED: {
  429. if (is_ready()) {
  430. // FIXME: Technically this only needs to update some dynamic texts, not the whole list.
  431. update_project_list();
  432. }
  433. } break;
  434. case NOTIFICATION_THEME_CHANGED: {
  435. if (project_context_menu) {
  436. _update_menu_icons();
  437. }
  438. } break;
  439. case NOTIFICATION_PROCESS: {
  440. // Load icons as a coroutine to speed up launch when you have hundreds of projects.
  441. if (_icon_load_index < _projects.size()) {
  442. Item &item = _projects.write[_icon_load_index];
  443. if (item.control->should_load_project_icon()) {
  444. _load_project_icon(_icon_load_index);
  445. }
  446. _icon_load_index++;
  447. // Scan directories in thread to avoid blocking the window.
  448. } else if (scan_data && scan_data->scan_in_progress.is_set()) {
  449. // Wait for the thread.
  450. } else {
  451. set_process(false);
  452. if (scan_data) {
  453. _scan_finished();
  454. }
  455. }
  456. } break;
  457. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  458. RID ae = get_accessibility_element();
  459. ERR_FAIL_COND(ae.is_null());
  460. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX);
  461. DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, _projects.size());
  462. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MULTISELECTABLE, false);
  463. }
  464. }
  465. }
  466. // Projects scan.
  467. void ProjectList::_scan_thread(void *p_scan_data) {
  468. ScanData *scan_data = static_cast<ScanData *>(p_scan_data);
  469. for (const String &base_path : scan_data->paths_to_scan) {
  470. print_verbose(vformat("Scanning for projects in \"%s\".", base_path));
  471. _scan_folder_recursive(base_path, &scan_data->found_projects, scan_data->scan_in_progress);
  472. if (!scan_data->scan_in_progress.is_set()) {
  473. print_verbose("Scan aborted.");
  474. break;
  475. }
  476. }
  477. print_verbose(vformat("Found %d project(s).", scan_data->found_projects.size()));
  478. scan_data->scan_in_progress.clear();
  479. }
  480. void ProjectList::_scan_finished() {
  481. if (scan_data->scan_in_progress.is_set()) {
  482. // Abort scanning.
  483. scan_data->scan_in_progress.clear();
  484. }
  485. scan_data->thread->wait_to_finish();
  486. memdelete(scan_data->thread);
  487. if (scan_progress) {
  488. scan_progress->hide();
  489. }
  490. for (const String &E : scan_data->found_projects) {
  491. add_project(E, false);
  492. }
  493. memdelete(scan_data);
  494. scan_data = nullptr;
  495. save_config();
  496. if (ProjectManager::get_singleton()->is_initialized()) {
  497. update_project_list();
  498. }
  499. }
  500. // Initialization & loading.
  501. void ProjectList::_migrate_config() {
  502. // Proposal #1637 moved the project list from editor settings to a separate config file
  503. // If the new config file doesn't exist, populate it from EditorSettings
  504. if (FileAccess::exists(_config_path)) {
  505. return;
  506. }
  507. List<PropertyInfo> properties;
  508. EditorSettings::get_singleton()->get_property_list(&properties);
  509. for (const PropertyInfo &E : properties) {
  510. // This is actually something like "projects/C:::Documents::Godot::Projects::MyGame"
  511. String property_key = E.name;
  512. if (!property_key.begins_with("projects/")) {
  513. continue;
  514. }
  515. String path = EDITOR_GET(property_key);
  516. print_line("Migrating legacy project '" + path + "'.");
  517. String favoriteKey = "favorite_projects/" + property_key.get_slicec('/', 1);
  518. bool favorite = EditorSettings::get_singleton()->has_setting(favoriteKey);
  519. add_project(path, favorite);
  520. if (favorite) {
  521. EditorSettings::get_singleton()->erase(favoriteKey);
  522. }
  523. EditorSettings::get_singleton()->erase(property_key);
  524. }
  525. save_config();
  526. }
  527. void ProjectList::save_config() {
  528. _config.save(_config_path);
  529. }
  530. // Load project data from p_property_key and return it in a ProjectList::Item.
  531. // p_favorite is passed directly into the Item.
  532. ProjectList::Item ProjectList::load_project_data(const String &p_path, bool p_favorite) {
  533. String conf = p_path.path_join("project.godot");
  534. bool grayed = false;
  535. bool missing = false;
  536. bool recovery_mode = false;
  537. Ref<ConfigFile> cf = memnew(ConfigFile);
  538. Error cf_err = cf->load(conf);
  539. int config_version = 0;
  540. String cf_project_name;
  541. String project_name = TTR("Unnamed Project");
  542. if (cf_err == OK) {
  543. cf_project_name = cf->get_value("application", "config/name", "");
  544. if (!cf_project_name.is_empty()) {
  545. project_name = cf_project_name.xml_unescape();
  546. }
  547. config_version = (int)cf->get_value("", "config_version", 0);
  548. }
  549. if (config_version > ProjectSettings::CONFIG_VERSION) {
  550. // Comes from an incompatible (more recent) Godot version, gray it out.
  551. grayed = true;
  552. }
  553. const String description = cf->get_value("application", "config/description", "");
  554. const PackedStringArray tags = cf->get_value("application", "config/tags", PackedStringArray());
  555. const String main_scene = cf->get_value("application", "run/main_scene", "");
  556. String icon = cf->get_value("application", "config/icon", "");
  557. if (icon.begins_with("uid://")) {
  558. Error err;
  559. Ref<FileAccess> file = FileAccess::open(p_path.path_join(".godot/uid_cache.bin"), FileAccess::READ, &err);
  560. if (err == OK) {
  561. icon = ResourceUID::get_path_from_cache(file, icon);
  562. if (icon.is_empty()) {
  563. WARN_PRINT(vformat("Could not load icon from UID for project at path \"%s\". Make sure UID cache exists.", p_path));
  564. }
  565. } else {
  566. // Cache does not exist yet, so ignore and fallback to default icon.
  567. icon = "";
  568. }
  569. }
  570. PackedStringArray project_features = cf->get_value("application", "config/features", PackedStringArray());
  571. PackedStringArray unsupported_features = ProjectSettings::get_unsupported_features(project_features);
  572. String project_version = "?";
  573. for (int i = 0; i < project_features.size(); i++) {
  574. if (ProjectList::project_feature_looks_like_version(project_features[i])) {
  575. project_version = project_features[i];
  576. break;
  577. }
  578. }
  579. if (config_version < ProjectSettings::CONFIG_VERSION) {
  580. // Previous versions may not have unsupported features.
  581. if (config_version == 4) {
  582. unsupported_features.push_back("3.x");
  583. project_version = "3.x";
  584. } else {
  585. unsupported_features.push_back(TTR("Unknown version"));
  586. }
  587. }
  588. uint64_t last_edited = 0;
  589. if (cf_err == OK) {
  590. // The modification date marks the date the project was last edited.
  591. // This is because the `project.godot` file will always be modified
  592. // when editing a project (but not when running it).
  593. last_edited = FileAccess::get_modified_time(conf);
  594. String fscache = p_path.path_join(".fscache");
  595. if (FileAccess::exists(fscache)) {
  596. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  597. if (cache_modified > last_edited) {
  598. last_edited = cache_modified;
  599. }
  600. }
  601. } else {
  602. grayed = true;
  603. missing = true;
  604. }
  605. for (const String &tag : tags) {
  606. ProjectManager::get_singleton()->add_new_tag(tag);
  607. }
  608. // We can't use OS::get_user_dir() because it attempts to load paths from the current loaded project through ProjectSettings,
  609. // while here we're parsing project files externally. Therefore, we have to replicate its behavior.
  610. String user_dir;
  611. if (!cf_project_name.is_empty()) {
  612. String appname = OS::get_singleton()->get_safe_dir_name(cf_project_name);
  613. bool use_custom_dir = cf->get_value("application", "config/use_custom_user_dir", false);
  614. if (use_custom_dir) {
  615. String custom_dir = OS::get_singleton()->get_safe_dir_name(cf->get_value("application", "config/custom_user_dir_name", ""), true);
  616. if (custom_dir.is_empty()) {
  617. custom_dir = appname;
  618. }
  619. user_dir = custom_dir;
  620. } else {
  621. user_dir = OS::get_singleton()->get_godot_dir_name().path_join("app_userdata").path_join(appname);
  622. }
  623. } else {
  624. user_dir = OS::get_singleton()->get_godot_dir_name().path_join("app_userdata").path_join("[unnamed project]");
  625. }
  626. String recovery_mode_lock_file = OS::get_singleton()->get_user_data_dir(user_dir).path_join(".recovery_mode_lock");
  627. recovery_mode = FileAccess::exists(recovery_mode_lock_file);
  628. return Item(project_name, description, project_version, tags, p_path, icon, main_scene, unsupported_features, last_edited, p_favorite, grayed, missing, recovery_mode, config_version);
  629. }
  630. void ProjectList::_update_icons_async() {
  631. _icon_load_index = 0;
  632. set_process(true);
  633. }
  634. void ProjectList::_load_project_icon(int p_index) {
  635. Item &item = _projects.write[p_index];
  636. Ref<Texture2D> default_icon = get_editor_theme_icon(SNAME("DefaultProjectIcon"));
  637. Ref<Texture2D> icon;
  638. if (!item.icon.is_empty()) {
  639. Ref<Image> img;
  640. img.instantiate();
  641. Error err = img->load(item.icon.replace_first("res://", item.path + "/"));
  642. if (err == OK) {
  643. img->resize(default_icon->get_width(), default_icon->get_height(), Image::INTERPOLATE_LANCZOS);
  644. icon = ImageTexture::create_from_image(img);
  645. }
  646. }
  647. if (icon.is_null()) {
  648. icon = default_icon;
  649. }
  650. item.control->set_project_icon(icon);
  651. }
  652. // Project list updates.
  653. void ProjectList::update_project_list() {
  654. // This is a full, hard reload of the list. Don't call this unless really required, it's expensive.
  655. // If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
  656. // FIXME: Does it really have to be a full, hard reload? Runtime updates should be made much cheaper.
  657. if (ProjectManager::get_singleton()->is_initialized()) {
  658. // Clear whole list
  659. for (int i = 0; i < _projects.size(); ++i) {
  660. Item &project = _projects.write[i];
  661. CRASH_COND(project.control == nullptr);
  662. memdelete(project.control); // Why not queue_free()?
  663. }
  664. _projects.clear();
  665. _last_clicked = "";
  666. _selected_project_paths.clear();
  667. load_project_list();
  668. }
  669. // Create controls
  670. for (int i = 0; i < _projects.size(); ++i) {
  671. _create_project_item_control(i);
  672. }
  673. sort_projects();
  674. _update_icons_async();
  675. update_dock_menu();
  676. set_v_scroll(0);
  677. emit_signal(SNAME(SIGNAL_LIST_CHANGED));
  678. queue_accessibility_update();
  679. }
  680. void ProjectList::sort_projects() {
  681. SortArray<Item, ProjectListComparator> sorter;
  682. sorter.compare.order_option = _order_option;
  683. sorter.sort(_projects.ptrw(), _projects.size());
  684. String search_term;
  685. PackedStringArray tags;
  686. if (!_search_term.is_empty()) {
  687. PackedStringArray search_parts = _search_term.split(" ");
  688. if (search_parts.size() > 1 || search_parts[0].begins_with("tag:")) {
  689. PackedStringArray remaining;
  690. for (const String &part : search_parts) {
  691. if (part.begins_with("tag:")) {
  692. tags.push_back(part.get_slicec(':', 1));
  693. } else {
  694. remaining.append(part);
  695. }
  696. }
  697. search_term = String(" ").join(remaining); // Search term without tags.
  698. } else {
  699. search_term = _search_term;
  700. }
  701. }
  702. for (int i = 0; i < _projects.size(); ++i) {
  703. Item &item = _projects.write[i];
  704. bool item_visible = true;
  705. if (!_search_term.is_empty()) {
  706. String search_path;
  707. if (search_term.contains_char('/')) {
  708. // Search path will match the whole path
  709. search_path = item.path;
  710. } else {
  711. // Search path will only match the last path component to make searching more strict
  712. search_path = item.path.get_file();
  713. }
  714. bool missing_tags = false;
  715. for (const String &tag : tags) {
  716. if (!item.tags.has(tag)) {
  717. missing_tags = true;
  718. break;
  719. }
  720. }
  721. // When searching, display projects whose name or path contain the search term and whose tags match the searched tags.
  722. item_visible = !missing_tags && (search_term.is_empty() || item.project_name.containsn(search_term) || search_path.containsn(search_term));
  723. }
  724. item.control->set_visible(item_visible);
  725. }
  726. for (int i = 0; i < _projects.size(); ++i) {
  727. Item &item = _projects.write[i];
  728. item.control->get_parent()->move_child(item.control, i);
  729. }
  730. // Rewind the coroutine because order of projects changed
  731. _update_icons_async();
  732. update_dock_menu();
  733. queue_accessibility_update();
  734. }
  735. int ProjectList::get_project_count() const {
  736. return _projects.size();
  737. }
  738. void ProjectList::find_projects(const String &p_path) {
  739. PackedStringArray paths = { p_path };
  740. find_projects_multiple(paths);
  741. }
  742. void ProjectList::find_projects_multiple(const PackedStringArray &p_paths) {
  743. if (!scan_progress && is_inside_tree()) {
  744. scan_progress = memnew(AcceptDialog);
  745. scan_progress->set_title(TTRC("Scanning"));
  746. scan_progress->set_ok_button_text(TTRC("Cancel"));
  747. VBoxContainer *vb = memnew(VBoxContainer);
  748. scan_progress->add_child(vb);
  749. Label *label = memnew(Label);
  750. label->set_text(TTRC("Scanning for projects..."));
  751. vb->add_child(label);
  752. ProgressBar *progress = memnew(ProgressBar);
  753. progress->set_indeterminate(true);
  754. vb->add_child(progress);
  755. add_child(scan_progress);
  756. scan_progress->connect(SceneStringName(confirmed), callable_mp(this, &ProjectList::_scan_finished));
  757. scan_progress->connect("canceled", callable_mp(this, &ProjectList::_scan_finished));
  758. }
  759. scan_data = memnew(ScanData);
  760. scan_data->paths_to_scan = p_paths;
  761. scan_data->scan_in_progress.set();
  762. scan_data->thread = memnew(Thread);
  763. scan_data->thread->start(_scan_thread, scan_data);
  764. if (scan_progress) {
  765. scan_progress->reset_size();
  766. scan_progress->popup_centered();
  767. }
  768. set_process(true);
  769. }
  770. void ProjectList::load_project_list() {
  771. _config.load(_config_path);
  772. Vector<String> sections = _config.get_sections();
  773. for (const String &path : sections) {
  774. bool favorite = _config.get_value(path, "favorite", false);
  775. _projects.push_back(load_project_data(path, favorite));
  776. }
  777. }
  778. void ProjectList::_scan_folder_recursive(const String &p_path, List<String> *r_projects, const SafeFlag &p_scan_active) {
  779. if (!p_scan_active.is_set()) {
  780. return;
  781. }
  782. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  783. Error error = da->change_dir(p_path);
  784. ERR_FAIL_COND_MSG(error != OK, vformat("Failed to open the path \"%s\" for scanning (code %d).", p_path, error));
  785. da->list_dir_begin();
  786. String n = da->get_next();
  787. while (!n.is_empty()) {
  788. if (!p_scan_active.is_set()) {
  789. return;
  790. }
  791. if (da->current_is_dir() && n[0] != '.') {
  792. _scan_folder_recursive(da->get_current_dir().path_join(n), r_projects, p_scan_active);
  793. } else if (n == "project.godot") {
  794. r_projects->push_back(da->get_current_dir());
  795. }
  796. n = da->get_next();
  797. }
  798. da->list_dir_end();
  799. }
  800. // Project list items.
  801. void ProjectList::add_project(const String &dir_path, bool favorite) {
  802. if (!_config.has_section(dir_path)) {
  803. _config.set_value(dir_path, "favorite", favorite);
  804. }
  805. queue_accessibility_update();
  806. }
  807. void ProjectList::set_project_version(const String &p_project_path, int p_version) {
  808. for (ProjectList::Item &E : _projects) {
  809. if (E.path == p_project_path) {
  810. E.version = p_version;
  811. break;
  812. }
  813. }
  814. }
  815. int ProjectList::refresh_project(const String &dir_path) {
  816. // Reloads information about a specific project.
  817. // If it wasn't loaded and should be in the list, it is added (i.e new project).
  818. // If it isn't in the list anymore, it is removed.
  819. // If it is in the list but doesn't exist anymore, it is marked as missing.
  820. bool should_be_in_list = _config.has_section(dir_path);
  821. bool is_favorite = _config.get_value(dir_path, "favorite", false);
  822. bool was_selected = _selected_project_paths.has(dir_path);
  823. // Remove item in any case
  824. for (int i = 0; i < _projects.size(); ++i) {
  825. const Item &existing_item = _projects[i];
  826. if (existing_item.path == dir_path) {
  827. _remove_project(i, false);
  828. break;
  829. }
  830. }
  831. int index = -1;
  832. if (should_be_in_list) {
  833. // Recreate it with updated info
  834. Item item = load_project_data(dir_path, is_favorite);
  835. _projects.push_back(item);
  836. _create_project_item_control(_projects.size() - 1);
  837. sort_projects();
  838. for (int i = 0; i < _projects.size(); ++i) {
  839. if (_projects[i].path == dir_path) {
  840. if (was_selected) {
  841. ensure_project_visible(i);
  842. }
  843. _load_project_icon(i);
  844. index = i;
  845. break;
  846. }
  847. }
  848. }
  849. return index;
  850. }
  851. int ProjectList::get_index(const ProjectListItemControl *p_control) const {
  852. for (int i = 0; i < _projects.size(); ++i) {
  853. if (_projects[i].control == p_control) {
  854. return i;
  855. }
  856. }
  857. return -1;
  858. }
  859. void ProjectList::ensure_project_visible(int p_index) {
  860. const Item &item = _projects[p_index];
  861. // Since follow focus is enabled.
  862. item.control->grab_focus();
  863. }
  864. void ProjectList::_create_project_item_control(int p_index) {
  865. // Will be added last in the list, so make sure indexes match
  866. ERR_FAIL_COND(p_index != project_list_vbox->get_child_count());
  867. Item &item = _projects.write[p_index];
  868. ERR_FAIL_COND(item.control != nullptr); // Already created
  869. ProjectListItemControl *hb = memnew(ProjectListItemControl);
  870. hb->add_theme_constant_override("separation", 10 * EDSCALE);
  871. hb->set_project_title(!item.missing ? item.project_name : TTR("Missing Project"));
  872. hb->set_project_path(item.path);
  873. hb->set_tooltip_text(item.description);
  874. hb->set_tags(item.tags, this);
  875. hb->set_unsupported_features(item.unsupported_features.duplicate());
  876. hb->set_project_version(item.project_version);
  877. hb->set_last_edited_info(item.get_last_edited_string());
  878. hb->set_is_favorite(item.favorite);
  879. hb->set_is_missing(item.missing);
  880. hb->set_is_grayed(item.grayed);
  881. hb->connect(SceneStringName(gui_input), callable_mp(this, &ProjectList::_list_item_input).bind(hb));
  882. hb->connect("favorite_pressed", callable_mp(this, &ProjectList::_on_favorite_pressed).bind(hb));
  883. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  884. hb->connect("explore_pressed", callable_mp(this, &ProjectList::_on_explore_pressed).bind(item.path));
  885. #endif
  886. hb->connect("request_menu", callable_mp(this, &ProjectList::_open_menu).bind(hb));
  887. project_list_vbox->add_child(hb);
  888. item.control = hb;
  889. }
  890. void ProjectList::_toggle_project(int p_index) {
  891. // This methods adds to the selection or removes from the
  892. // selection.
  893. Item &item = _projects.write[p_index];
  894. if (_selected_project_paths.has(item.path)) {
  895. _deselect_project_nocheck(p_index);
  896. } else {
  897. _select_project_nocheck(p_index);
  898. }
  899. }
  900. void ProjectList::_remove_project(int p_index, bool p_update_config) {
  901. const Item item = _projects[p_index]; // Take a copy
  902. _selected_project_paths.erase(item.path);
  903. if (_last_clicked == item.path) {
  904. _last_clicked = "";
  905. }
  906. memdelete(item.control);
  907. _projects.remove_at(p_index);
  908. if (p_update_config) {
  909. _config.erase_section(item.path);
  910. // Not actually saving the file, in case you are doing more changes to settings
  911. }
  912. queue_accessibility_update();
  913. update_dock_menu();
  914. }
  915. void ProjectList::_list_item_input(const Ref<InputEvent> &p_ev, Control *p_hb) {
  916. Ref<InputEventMouseButton> mb = p_ev;
  917. int clicked_index = p_hb->get_index();
  918. const Item &clicked_project = _projects[clicked_index];
  919. if (mb.is_valid() && mb->is_pressed()) {
  920. if (mb->get_button_index() == MouseButton::LEFT) {
  921. if (mb->is_shift_pressed() && _selected_project_paths.size() > 0 && !_last_clicked.is_empty() && clicked_project.path != _last_clicked) {
  922. int anchor_index = -1;
  923. for (int i = 0; i < _projects.size(); ++i) {
  924. const Item &p = _projects[i];
  925. if (p.path == _last_clicked) {
  926. anchor_index = p.control->get_index();
  927. break;
  928. }
  929. }
  930. CRASH_COND(anchor_index == -1);
  931. _select_project_range(anchor_index, clicked_index);
  932. } else if (mb->is_command_or_control_pressed()) {
  933. _toggle_project(clicked_index);
  934. } else {
  935. _last_clicked = clicked_project.path;
  936. select_project(clicked_index);
  937. }
  938. emit_signal(SNAME(SIGNAL_SELECTION_CHANGED));
  939. // Do not allow opening a project more than once using a single project manager instance.
  940. // Opening the same project in several editor instances at once can lead to various issues.
  941. if (!mb->is_command_or_control_pressed() && mb->is_double_click() && !project_opening_initiated) {
  942. emit_signal(SNAME(SIGNAL_PROJECT_ASK_OPEN));
  943. }
  944. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  945. _open_menu(mb->get_position(), p_hb);
  946. }
  947. }
  948. Ref<InputEventKey> kev = p_ev;
  949. if (kev.is_valid() && kev->is_pressed()) {
  950. switch (kev->get_keycode()) {
  951. case Key::E: {
  952. _on_explore_pressed(clicked_project.path);
  953. accept_event();
  954. } break;
  955. case Key::F: {
  956. if (kev->is_command_or_control_pressed()) {
  957. return; // Focus the search box by the ProjectManager.
  958. }
  959. _on_favorite_pressed(p_hb);
  960. accept_event();
  961. } break;
  962. default: {
  963. } break;
  964. }
  965. }
  966. }
  967. void ProjectList::_on_favorite_pressed(Node *p_hb) {
  968. ProjectListItemControl *control = Object::cast_to<ProjectListItemControl>(p_hb);
  969. int index = control->get_index();
  970. Item item = _projects.write[index]; // Take copy
  971. item.favorite = !item.favorite;
  972. _config.set_value(item.path, "favorite", item.favorite);
  973. save_config();
  974. _projects.write[index] = item;
  975. control->set_is_favorite(item.favorite);
  976. sort_projects();
  977. // As controls are sorted, the calls are delayed in case follow focus does not take effect.
  978. if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
  979. callable_mp((ScrollContainer *)this, &ScrollContainer::ensure_control_visible).call_deferred(control);
  980. } else {
  981. // Do not follow the control when toggling.
  982. callable_mp(this, &ProjectList::ensure_project_visible).call_deferred(index);
  983. }
  984. update_dock_menu();
  985. }
  986. void ProjectList::_on_explore_pressed(const String &p_path) {
  987. OS::get_singleton()->shell_show_in_file_manager(p_path, true);
  988. }
  989. void ProjectList::_open_menu(const Vector2 &p_at, Control *p_hb) {
  990. int clicked_index = p_hb->get_index();
  991. const Item &clicked_project = _projects[clicked_index];
  992. if (!project_context_menu) {
  993. project_context_menu = memnew(PopupMenu);
  994. project_context_menu->add_item(TTRC("Open in Editor"), MENU_EDIT);
  995. project_context_menu->add_item(TTRC("Open in Editor (Verbose Mode)"), MENU_EDIT_VERBOSE);
  996. project_context_menu->add_item(TTRC("Open in Editor (Recovery Mode)"), MENU_EDIT_RECOVERY);
  997. project_context_menu->add_item(TTRC("Run Project"), MENU_RUN);
  998. project_context_menu->add_separator();
  999. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  1000. project_context_menu->add_item(TTRC("Show in File Manager"), MENU_SHOW_IN_FILE_MANAGER);
  1001. #endif
  1002. project_context_menu->add_item(TTRC("Copy Path"), MENU_COPY_PATH);
  1003. project_context_menu->add_separator();
  1004. project_context_menu->add_item(TTRC("Rename"), MENU_RENAME);
  1005. project_context_menu->add_item(TTRC("Manage Tags"), MENU_MANAGE_TAGS);
  1006. project_context_menu->add_item(TTRC("Duplicate"), MENU_DUPLICATE);
  1007. project_context_menu->add_item(TTRC("Remove from Project List"), MENU_REMOVE);
  1008. add_child(project_context_menu);
  1009. project_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ProjectList::_menu_option));
  1010. _update_menu_icons();
  1011. }
  1012. select_project(clicked_index);
  1013. for (int id : Vector<int>{
  1014. MENU_EDIT,
  1015. MENU_EDIT_VERBOSE,
  1016. MENU_EDIT_RECOVERY,
  1017. MENU_RUN,
  1018. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  1019. MENU_SHOW_IN_FILE_MANAGER,
  1020. #endif
  1021. MENU_RENAME,
  1022. MENU_MANAGE_TAGS,
  1023. MENU_DUPLICATE }) {
  1024. project_context_menu->set_item_disabled(project_context_menu->get_item_index(id), clicked_project.missing);
  1025. }
  1026. project_context_menu->set_position(p_hb->get_screen_position() + p_at);
  1027. project_context_menu->reset_size();
  1028. project_context_menu->popup();
  1029. }
  1030. void ProjectList::_menu_option(int p_option) {
  1031. emit_signal(SIGNAL_MENU_OPTION_SELECTED, p_option);
  1032. }
  1033. void ProjectList::_update_menu_icons() {
  1034. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_EDIT), get_editor_theme_icon("Edit"));
  1035. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_EDIT_VERBOSE), get_editor_theme_icon("Notification"));
  1036. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_EDIT_RECOVERY), get_editor_theme_icon("NodeWarning"));
  1037. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_RUN), get_editor_theme_icon("Play"));
  1038. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  1039. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_SHOW_IN_FILE_MANAGER), get_editor_theme_icon("Load"));
  1040. #endif
  1041. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_COPY_PATH), get_editor_theme_icon("ActionCopy"));
  1042. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_RENAME), get_editor_theme_icon("Rename"));
  1043. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_MANAGE_TAGS), get_editor_theme_icon("Script"));
  1044. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_DUPLICATE), get_editor_theme_icon("Duplicate"));
  1045. project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_REMOVE), get_editor_theme_icon("Remove"));
  1046. }
  1047. // Project list selection.
  1048. void ProjectList::_clear_project_selection() {
  1049. Vector<Item> previous_selected_items = get_selected_projects();
  1050. _selected_project_paths.clear();
  1051. for (int i = 0; i < previous_selected_items.size(); ++i) {
  1052. previous_selected_items[i].control->set_selected(false);
  1053. }
  1054. queue_accessibility_update();
  1055. }
  1056. void ProjectList::_select_project_nocheck(int p_index) {
  1057. Item &item = _projects.write[p_index];
  1058. _selected_project_paths.insert(item.path);
  1059. item.control->set_selected(true);
  1060. queue_accessibility_update();
  1061. }
  1062. void ProjectList::_deselect_project_nocheck(int p_index) {
  1063. Item &item = _projects.write[p_index];
  1064. _selected_project_paths.erase(item.path);
  1065. item.control->set_selected(false);
  1066. queue_accessibility_update();
  1067. }
  1068. inline void _sort_project_range(int &a, int &b) {
  1069. if (a > b) {
  1070. int temp = a;
  1071. a = b;
  1072. b = temp;
  1073. }
  1074. }
  1075. void ProjectList::_select_project_range(int p_begin, int p_end) {
  1076. _clear_project_selection();
  1077. _sort_project_range(p_begin, p_end);
  1078. for (int i = p_begin; i <= p_end; ++i) {
  1079. _select_project_nocheck(i);
  1080. }
  1081. }
  1082. void ProjectList::select_project(int p_index) {
  1083. // This method keeps only one project selected.
  1084. _clear_project_selection();
  1085. _select_project_nocheck(p_index);
  1086. }
  1087. void ProjectList::deselect_project(int p_index) {
  1088. _deselect_project_nocheck(p_index);
  1089. }
  1090. void ProjectList::select_first_visible_project() {
  1091. _clear_project_selection();
  1092. for (int i = 0; i < _projects.size(); i++) {
  1093. if (_projects[i].control->is_visible()) {
  1094. _select_project_nocheck(i);
  1095. break;
  1096. }
  1097. }
  1098. }
  1099. void ProjectList::deselect_all_visible_projects() {
  1100. for (int i = 0; i < _projects.size(); i++) {
  1101. if (_projects[i].control->is_visible()) {
  1102. _deselect_project_nocheck(i);
  1103. }
  1104. }
  1105. }
  1106. void ProjectList::select_all_visible_projects() {
  1107. for (int i = 0; i < _projects.size(); i++) {
  1108. if (_projects[i].control->is_visible()) {
  1109. _select_project_nocheck(i);
  1110. }
  1111. }
  1112. }
  1113. Vector<ProjectList::Item> ProjectList::get_selected_projects() const {
  1114. Vector<Item> items;
  1115. if (_selected_project_paths.is_empty()) {
  1116. return items;
  1117. }
  1118. items.resize(_selected_project_paths.size());
  1119. int j = 0;
  1120. for (int i = 0; i < _projects.size(); ++i) {
  1121. const Item &item = _projects[i];
  1122. if (_selected_project_paths.has(item.path)) {
  1123. items.write[j++] = item;
  1124. }
  1125. }
  1126. ERR_FAIL_COND_V(j != items.size(), items);
  1127. return items;
  1128. }
  1129. const HashSet<String> &ProjectList::get_selected_project_keys() const {
  1130. // Faster if that's all you need
  1131. return _selected_project_paths;
  1132. }
  1133. int ProjectList::get_single_selected_index() const {
  1134. if (_selected_project_paths.is_empty()) {
  1135. // Default selection
  1136. return 0;
  1137. }
  1138. String key;
  1139. if (_selected_project_paths.size() == 1) {
  1140. // Only one selected
  1141. key = *_selected_project_paths.begin();
  1142. } else {
  1143. // Multiple selected, consider the last clicked one as "main"
  1144. key = _last_clicked;
  1145. }
  1146. for (int i = 0; i < _projects.size(); ++i) {
  1147. if (_projects[i].path == key) {
  1148. return i;
  1149. }
  1150. }
  1151. return 0;
  1152. }
  1153. void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
  1154. if (_selected_project_paths.is_empty()) {
  1155. return;
  1156. }
  1157. for (int i = 0; i < _projects.size(); ++i) {
  1158. Item &item = _projects.write[i];
  1159. if (_selected_project_paths.has(item.path) && item.control->is_visible()) {
  1160. _config.erase_section(item.path);
  1161. // Comment out for now until we have a better warning system to
  1162. // ensure users delete their project only.
  1163. //if (p_delete_project_contents) {
  1164. // OS::get_singleton()->move_to_trash(item.path);
  1165. //}
  1166. memdelete(item.control);
  1167. _projects.remove_at(i);
  1168. --i;
  1169. }
  1170. }
  1171. save_config();
  1172. _selected_project_paths.clear();
  1173. _last_clicked = "";
  1174. update_dock_menu();
  1175. }
  1176. // Missing projects.
  1177. bool ProjectList::is_any_project_missing() const {
  1178. for (int i = 0; i < _projects.size(); ++i) {
  1179. if (_projects[i].missing) {
  1180. return true;
  1181. }
  1182. }
  1183. return false;
  1184. }
  1185. void ProjectList::erase_missing_projects() {
  1186. if (_projects.is_empty()) {
  1187. return;
  1188. }
  1189. int deleted_count = 0;
  1190. int remaining_count = 0;
  1191. for (int i = 0; i < _projects.size(); ++i) {
  1192. const Item &item = _projects[i];
  1193. if (item.missing) {
  1194. _remove_project(i, true);
  1195. --i;
  1196. ++deleted_count;
  1197. } else {
  1198. ++remaining_count;
  1199. }
  1200. }
  1201. print_line("Removed " + itos(deleted_count) + " projects from the list, remaining " + itos(remaining_count) + " projects");
  1202. save_config();
  1203. }
  1204. // Project list sorting and filtering.
  1205. void ProjectList::set_search_term(String p_search_term) {
  1206. _search_term = p_search_term;
  1207. }
  1208. void ProjectList::add_search_tag(const String &p_tag) {
  1209. const String tag_string = "tag:" + p_tag;
  1210. int exists = _search_term.find(tag_string);
  1211. if (exists > -1) {
  1212. _search_term = _search_term.erase(exists, tag_string.length() + 1);
  1213. } else if (_search_term.is_empty() || _search_term.ends_with(" ")) {
  1214. _search_term += tag_string;
  1215. } else {
  1216. _search_term += " " + tag_string;
  1217. }
  1218. ProjectManager::get_singleton()->get_search_box()->set_text(_search_term);
  1219. sort_projects();
  1220. }
  1221. void ProjectList::set_order_option(int p_option, bool p_save) {
  1222. FilterOption selected = (FilterOption)p_option;
  1223. if (p_save) {
  1224. EditorSettings::get_singleton()->set("project_manager/sorting_order", p_option);
  1225. EditorSettings::get_singleton()->save();
  1226. }
  1227. _order_option = selected;
  1228. sort_projects();
  1229. }
  1230. // Global menu integration.
  1231. void ProjectList::update_dock_menu() {
  1232. if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {
  1233. return;
  1234. }
  1235. RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);
  1236. NativeMenu::get_singleton()->clear(dock_rid);
  1237. int favs_added = 0;
  1238. int total_added = 0;
  1239. for (int i = 0; i < _projects.size(); ++i) {
  1240. if (!_projects[i].grayed && !_projects[i].missing) {
  1241. if (_projects[i].favorite) {
  1242. favs_added++;
  1243. } else {
  1244. if (favs_added != 0) {
  1245. NativeMenu::get_singleton()->add_separator(dock_rid);
  1246. }
  1247. favs_added = 0;
  1248. }
  1249. NativeMenu::get_singleton()->add_item(dock_rid, _projects[i].project_name + " ( " + _projects[i].path + " )", callable_mp(this, &ProjectList::_global_menu_open_project), Callable(), i);
  1250. total_added++;
  1251. }
  1252. }
  1253. if (total_added != 0) {
  1254. NativeMenu::get_singleton()->add_separator(dock_rid);
  1255. }
  1256. NativeMenu::get_singleton()->add_item(dock_rid, TTR("New Window"), callable_mp(this, &ProjectList::_global_menu_new_window));
  1257. }
  1258. void ProjectList::_global_menu_new_window(const Variant &p_tag) {
  1259. List<String> args;
  1260. args.push_back("-p");
  1261. OS::get_singleton()->create_instance(args);
  1262. }
  1263. void ProjectList::_global_menu_open_project(const Variant &p_tag) {
  1264. int idx = (int)p_tag;
  1265. if (idx >= 0 && idx < _projects.size()) {
  1266. String conf = _projects[idx].path.path_join("project.godot");
  1267. List<String> args;
  1268. args.push_back(conf);
  1269. OS::get_singleton()->create_instance(args);
  1270. }
  1271. }
  1272. // Object methods.
  1273. void ProjectList::_bind_methods() {
  1274. ADD_SIGNAL(MethodInfo(SIGNAL_LIST_CHANGED));
  1275. ADD_SIGNAL(MethodInfo(SIGNAL_SELECTION_CHANGED));
  1276. ADD_SIGNAL(MethodInfo(SIGNAL_PROJECT_ASK_OPEN));
  1277. ADD_SIGNAL(MethodInfo(SIGNAL_MENU_OPTION_SELECTED));
  1278. }
  1279. ProjectList::ProjectList() {
  1280. set_follow_focus(true);
  1281. project_list_vbox = memnew(VBoxContainer);
  1282. project_list_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1283. add_child(project_list_vbox);
  1284. _config_path = EditorPaths::get_singleton()->get_data_dir().path_join("projects.cfg");
  1285. _migrate_config();
  1286. }