SongBrowserUI.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. using BeatSaberMarkupLanguage.Components;
  2. using HMUI;
  3. using SongBrowser.DataAccess;
  4. using SongBrowser.Internals;
  5. using SongCore.Utilities;
  6. using SongDataCore.BeatStar;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. using Logger = SongBrowser.Logging.Logger;
  14. namespace SongBrowser.UI
  15. {
  16. public enum UIState
  17. {
  18. Disabled,
  19. Main,
  20. SortBy,
  21. FilterBy
  22. }
  23. public class SongBrowserViewController : ViewController
  24. {
  25. // Named instance
  26. }
  27. /// <summary>
  28. /// Hijack the flow coordinator. Have access to all StandardLevel easily.
  29. /// </summary>
  30. public class SongBrowserUI : MonoBehaviour
  31. {
  32. // Logging
  33. public const String Name = "SongBrowserUI";
  34. private const float SEGMENT_PERCENT = 0.1f;
  35. private const int LIST_ITEMS_VISIBLE_AT_ONCE = 6;
  36. private const float CLEAR_BUTTON_Y = -32.5f;
  37. private const float BUTTON_ROW_Y = -32.5f;
  38. // BeatSaber Internal UI structures
  39. DataAccess.BeatSaberUIController _beatUi;
  40. // New UI Elements
  41. private SongBrowserViewController _viewController;
  42. private List<SongSortButton> _sortButtonGroup;
  43. private List<SongFilterButton> _filterButtonGroup;
  44. private Button _sortByButton;
  45. private Button _sortByDisplay;
  46. private Button _filterByButton;
  47. private Button _filterByDisplay;
  48. private Button _randomButton;
  49. private Button _clearSortFilterButton;
  50. private SimpleDialogPromptViewController _deleteDialog;
  51. private Button _deleteButton;
  52. private Button _pageUpFastButton;
  53. private Button _pageDownFastButton;
  54. private RectTransform _ppStatButton;
  55. private RectTransform _starStatButton;
  56. private RectTransform _njsStatButton;
  57. private RectTransform _noteJumpStartBeatOffsetLabel;
  58. private IAnnotatedBeatmapLevelCollection _lastLevelCollection;
  59. private SongBrowserModel _model;
  60. public SongBrowserModel Model
  61. {
  62. set
  63. {
  64. _model = value;
  65. }
  66. get
  67. {
  68. return _model;
  69. }
  70. }
  71. private bool _uiCreated = false;
  72. private UIState _currentUiState = UIState.Disabled;
  73. private bool _asyncUpdating = false;
  74. /// <summary>
  75. /// Builds the UI for this plugin.
  76. /// </summary>
  77. public void CreateUI(MainMenuViewController.MenuButton mode)
  78. {
  79. Logger.Trace("CreateUI()");
  80. // Determine the flow controller to use
  81. FlowCoordinator flowCoordinator;
  82. if (mode == MainMenuViewController.MenuButton.SoloFreePlay)
  83. {
  84. Logger.Debug("Entering SOLO mode...");
  85. flowCoordinator = Resources.FindObjectsOfTypeAll<SoloFreePlayFlowCoordinator>().First();
  86. }
  87. else if (mode == MainMenuViewController.MenuButton.Party)
  88. {
  89. Logger.Debug("Entering PARTY mode...");
  90. flowCoordinator = Resources.FindObjectsOfTypeAll<PartyFreePlayFlowCoordinator>().First();
  91. }
  92. else
  93. {
  94. Logger.Info("Entering Unsupported mode...");
  95. return;
  96. }
  97. Logger.Debug("Done fetching Flow Coordinator for the appropriate mode...");
  98. _beatUi = new DataAccess.BeatSaberUIController(flowCoordinator);
  99. _lastLevelCollection = null;
  100. // returning to the menu and switching modes could trigger this.
  101. if (_uiCreated)
  102. {
  103. return;
  104. }
  105. try
  106. {
  107. // Create a view controller to store all SongBrowser elements
  108. if (_viewController)
  109. {
  110. UnityEngine.GameObject.Destroy(_viewController);
  111. }
  112. _viewController = BeatSaberUI.CreateCurvedViewController<SongBrowserViewController>("SongBrowserViewController", 125.0f);
  113. _viewController.rectTransform.SetParent(_beatUi.LevelCollectionNavigationController.rectTransform, false);
  114. _viewController.rectTransform.anchorMin = new Vector2(0f, 0f);
  115. _viewController.rectTransform.anchorMax = new Vector2(1f, 1f);
  116. _viewController.rectTransform.anchoredPosition = new Vector2(0, 0);
  117. _viewController.rectTransform.sizeDelta = new Vector2(125, 25);
  118. _viewController.gameObject.SetActive(true);
  119. // delete dialog
  120. this._deleteDialog = UnityEngine.Object.Instantiate<SimpleDialogPromptViewController>(_beatUi.SimpleDialogPromptViewControllerPrefab);
  121. this._deleteDialog.name = "DeleteDialogPromptViewController";
  122. this._deleteDialog.gameObject.SetActive(false);
  123. // create song browser main ui
  124. CreateOuterUi();
  125. CreateSortButtons();
  126. CreateFilterButtons();
  127. CreateDeleteButton();
  128. CreateFastPageButtons();
  129. this.InstallHandlers();
  130. this.ModifySongStatsPanel();
  131. this.ResizeSongUI();
  132. _uiCreated = true;
  133. RefreshSortButtonUI();
  134. Logger.Debug("Done Creating UI...");
  135. }
  136. catch (Exception e)
  137. {
  138. Logger.Exception("Exception during CreateUI: ", e);
  139. }
  140. }
  141. /// <summary>
  142. /// Create the outer ui.
  143. /// </summary>
  144. private void CreateOuterUi()
  145. {
  146. Logger.Debug("Creating outer UI...");
  147. float clearButtonX = -72.5f;
  148. float clearButtonY = CLEAR_BUTTON_Y;
  149. float buttonY = BUTTON_ROW_Y;
  150. float buttonHeight = 5.0f;
  151. float sortByButtonX = -62.5f + buttonHeight;
  152. float outerButtonFontSize = 3.0f;
  153. float displayButtonFontSize = 2.5f;
  154. float outerButtonWidth = 24.0f;
  155. float randomButtonWidth = 10.0f;
  156. // clear button
  157. _clearSortFilterButton = _viewController.CreateIconButton(
  158. "ClearSortAndFilterButton",
  159. "PracticeButton",
  160. new Vector2(clearButtonX, clearButtonY),
  161. new Vector2(randomButtonWidth, randomButtonWidth),
  162. () =>
  163. {
  164. if (_currentUiState == UIState.FilterBy || _currentUiState == UIState.SortBy)
  165. {
  166. RefreshOuterUIState(UIState.Main);
  167. }
  168. else
  169. {
  170. OnClearButtonClickEvent();
  171. }
  172. },
  173. Base64Sprites.XIcon);
  174. _clearSortFilterButton.SetButtonBackgroundActive(false);
  175. // create SortBy button and its display
  176. float curX = sortByButtonX;
  177. Logger.Debug("Creating Sort By...");
  178. _sortByButton = _viewController.CreateUIButton("sortBy", "PracticeButton", new Vector2(curX, buttonY), new Vector2(outerButtonWidth, buttonHeight), () =>
  179. {
  180. RefreshOuterUIState(UIState.SortBy);
  181. }, "Sort By");
  182. _sortByButton.SetButtonTextSize(outerButtonFontSize);
  183. _sortByButton.ToggleWordWrapping(false);
  184. curX += outerButtonWidth;
  185. Logger.Debug("Creating Sort By Display...");
  186. _sortByDisplay = _viewController.CreateUIButton("sortByValue", "PracticeButton", new Vector2(curX, buttonY), new Vector2(outerButtonWidth, buttonHeight), () =>
  187. {
  188. OnSortButtonClickEvent(_model.Settings.sortMode);
  189. }, "");
  190. _sortByDisplay.SetButtonTextSize(displayButtonFontSize);
  191. _sortByDisplay.ToggleWordWrapping(false);
  192. curX += outerButtonWidth;
  193. // create FilterBy button and its display
  194. Logger.Debug("Creating Filter By...");
  195. _filterByButton = _viewController.CreateUIButton("filterBy", "PracticeButton", new Vector2(curX, buttonY), new Vector2(outerButtonWidth, buttonHeight), () =>
  196. {
  197. RefreshOuterUIState(UIState.FilterBy);
  198. }, "Filter By");
  199. _filterByButton.SetButtonTextSize(outerButtonFontSize);
  200. _filterByButton.ToggleWordWrapping(false);
  201. curX += outerButtonWidth;
  202. Logger.Debug("Creating Filter By Display...");
  203. _filterByDisplay = _viewController.CreateUIButton("filterValue", "PracticeButton", new Vector2(curX, buttonY), new Vector2(outerButtonWidth, buttonHeight), () =>
  204. {
  205. _model.Settings.filterMode = SongFilterMode.None;
  206. CancelFilter();
  207. ProcessSongList();
  208. RefreshSongUI();
  209. }, "");
  210. _filterByDisplay.SetButtonTextSize(displayButtonFontSize);
  211. _filterByDisplay.ToggleWordWrapping(false);
  212. // random button
  213. Logger.Debug("Creating Random Button...");
  214. _randomButton = _viewController.CreateIconButton("randomButton", "PracticeButton", new Vector2(curX + (outerButtonWidth / 2.0f) + (randomButtonWidth / 4.0f), clearButtonY), new Vector2(randomButtonWidth, randomButtonWidth), () =>
  215. {
  216. OnSortButtonClickEvent(SongSortMode.Random);
  217. }, Base64Sprites.RandomIcon);
  218. _randomButton.SetButtonBackgroundActive(false);
  219. }
  220. /// <summary>
  221. /// Create the sort button ribbon
  222. /// </summary>
  223. private void CreateSortButtons()
  224. {
  225. Logger.Debug("Create sort buttons...");
  226. float sortButtonFontSize = 2.0f;
  227. float sortButtonX = -63.0f;
  228. float sortButtonWidth = 12.0f;
  229. float buttonSpacing = 0.25f;
  230. float buttonY = BUTTON_ROW_Y;
  231. float buttonHeight = 5.0f;
  232. string[] sortButtonNames = new string[]
  233. {
  234. "Title", "Author", "Newest", "#Plays", "PP", "Stars", "UpVotes", "Rating", "Heat"
  235. };
  236. SongSortMode[] sortModes = new SongSortMode[]
  237. {
  238. SongSortMode.Default, SongSortMode.Author, SongSortMode.Newest, SongSortMode.YourPlayCount, SongSortMode.PP, SongSortMode.Stars, SongSortMode.UpVotes, SongSortMode.Rating, SongSortMode.Heat
  239. };
  240. _sortButtonGroup = new List<SongSortButton>();
  241. for (int i = 0; i < sortButtonNames.Length; i++)
  242. {
  243. float curButtonX = sortButtonX + (sortButtonWidth * i) + (buttonSpacing * i);
  244. SongSortButton sortButton = new SongSortButton();
  245. sortButton.SortMode = sortModes[i];
  246. sortButton.Button = _viewController.CreateUIButton(String.Format("Sort{0}Button", sortButton.SortMode), "PracticeButton",
  247. new Vector2(curButtonX, buttonY), new Vector2(sortButtonWidth, buttonHeight),
  248. () =>
  249. {
  250. OnSortButtonClickEvent(sortButton.SortMode);
  251. RefreshOuterUIState(UIState.Main);
  252. },
  253. sortButtonNames[i]);
  254. sortButton.Button.SetButtonTextSize(sortButtonFontSize);
  255. sortButton.Button.ToggleWordWrapping(false);
  256. _sortButtonGroup.Add(sortButton);
  257. }
  258. }
  259. /// <summary>
  260. /// Create the filter by buttons
  261. /// </summary>
  262. private void CreateFilterButtons()
  263. {
  264. Logger.Debug("Creating filter buttons...");
  265. float filterButtonFontSize = 2.25f;
  266. float filterButtonX = -63.0f;
  267. float filterButtonWidth = 14.25f;
  268. float buttonSpacing = 0.5f;
  269. float buttonY = BUTTON_ROW_Y;
  270. float buttonHeight = 5.0f;
  271. string[] filterButtonNames = new string[]
  272. {
  273. "Search", "Ranked", "Unranked"
  274. };
  275. SongFilterMode[] filterModes = new SongFilterMode[]
  276. {
  277. SongFilterMode.Search, SongFilterMode.Ranked, SongFilterMode.Unranked
  278. };
  279. _filterButtonGroup = new List<SongFilterButton>();
  280. for (int i = 0; i < filterButtonNames.Length; i++)
  281. {
  282. float curButtonX = filterButtonX + (filterButtonWidth * i) + (buttonSpacing * i);
  283. SongFilterButton filterButton = new SongFilterButton();
  284. filterButton.FilterMode = filterModes[i];
  285. filterButton.Button = _viewController.CreateUIButton(String.Format("Filter{0}Button", filterButton.FilterMode), "PracticeButton",
  286. new Vector2(curButtonX, buttonY), new Vector2(filterButtonWidth, buttonHeight),
  287. () =>
  288. {
  289. OnFilterButtonClickEvent(filterButton.FilterMode);
  290. RefreshOuterUIState(UIState.Main);
  291. },
  292. filterButtonNames[i]);
  293. filterButton.Button.SetButtonTextSize(filterButtonFontSize);
  294. filterButton.Button.ToggleWordWrapping(false);
  295. _filterButtonGroup.Add(filterButton);
  296. }
  297. }
  298. /// <summary>
  299. /// Create the fast page up and down buttons
  300. /// </summary>
  301. private void CreateFastPageButtons()
  302. {
  303. Logger.Debug("Creating fast scroll button...");
  304. _pageUpFastButton = BeatSaberUI.CreateIconButton("PageUpFast",
  305. _beatUi.LevelCollectionNavigationController.transform as RectTransform, "PracticeButton",
  306. new Vector2(0.75f, 24f),
  307. new Vector2(10f, 10f),
  308. delegate ()
  309. {
  310. this.JumpSongList(-1, SEGMENT_PERCENT);
  311. }, Base64Sprites.DoubleArrow);
  312. _pageUpFastButton.SetButtonBackgroundActive(false);
  313. (_pageUpFastButton.transform as RectTransform).Rotate(new Vector3(0, 0, 180));
  314. _pageDownFastButton = BeatSaberUI.CreateIconButton("PageDownFast",
  315. _beatUi.LevelCollectionNavigationController.transform as RectTransform, "PracticeButton",
  316. new Vector2(0.75f, -24f),
  317. new Vector2(10f, 10f),
  318. delegate ()
  319. {
  320. this.JumpSongList(1, SEGMENT_PERCENT);
  321. }, Base64Sprites.DoubleArrow);
  322. _pageDownFastButton.SetButtonBackgroundActive(false);
  323. }
  324. /// <summary>
  325. /// Create the delete button in the play button container
  326. /// </summary>
  327. private void CreateDeleteButton()
  328. {
  329. // Create delete button
  330. /*Logger.Debug("Creating delete button...");
  331. _deleteButton = BeatSaberUI.CreateIconButton(_beatUi.PlayButtons, _beatUi.PracticeButton, Base64Sprites.DeleteIcon);
  332. _deleteButton.onClick.AddListener(delegate () {
  333. HandleDeleteSelectedLevel();
  334. });
  335. BeatSaberUI.DestroyHoverHint(_deleteButton.transform as RectTransform);*/
  336. }
  337. /// <summary>
  338. /// Resize the stats panel to fit more stats.
  339. /// </summary>
  340. private void ModifySongStatsPanel()
  341. {
  342. // modify stat panel, inject extra row of stats
  343. Logger.Debug("Resizing Stats Panel...");
  344. var statsPanel = _beatUi.StandardLevelDetailView.GetPrivateField<LevelParamsPanel>("_levelParamsPanel");
  345. (statsPanel.transform as RectTransform).Translate(0, 0.05f, 0);
  346. _ppStatButton = BeatSaberUI.CreateStatIcon("PPStatLabel",
  347. statsPanel.GetComponentsInChildren<RectTransform>().First(x => x.name == "NPS"),
  348. statsPanel.transform,
  349. Base64Sprites.GraphIcon,
  350. "PP Value");
  351. _starStatButton = BeatSaberUI.CreateStatIcon("StarStatLabel",
  352. statsPanel.GetComponentsInChildren<RectTransform>().First(x => x.name == "NotesCount"),
  353. statsPanel.transform,
  354. Base64Sprites.StarFullIcon,
  355. "Star Difficulty Rating");
  356. _njsStatButton = BeatSaberUI.CreateStatIcon("NoteJumpSpeedLabel",
  357. statsPanel.GetComponentsInChildren<RectTransform>().First(x => x.name == "ObstaclesCount"),
  358. statsPanel.transform,
  359. Base64Sprites.SpeedIcon,
  360. "Note Jump Speed");
  361. _noteJumpStartBeatOffsetLabel = BeatSaberUI.CreateStatIcon("NoteJumpStartBeatOffsetLabel",
  362. statsPanel.GetComponentsInChildren<RectTransform>().First(x => x.name == "BombsCount"),
  363. statsPanel.transform,
  364. Base64Sprites.NoteStartOffsetIcon,
  365. "Note Jump Start Beat Offset");
  366. }
  367. /// <summary>
  368. /// Resize some of the song table elements.
  369. /// </summary>
  370. public void ResizeSongUI()
  371. {
  372. // shrink play button container
  373. //RectTransform playButtonsRect = Resources.FindObjectsOfTypeAll<RectTransform>().First(x => x.name == "ActionButtons");
  374. //playButtonsRect.localScale = new Vector3(0.825f, 0.825f, 0.825f);
  375. }
  376. /// <summary>
  377. /// Add our handlers into BeatSaber.
  378. /// </summary>
  379. private void InstallHandlers()
  380. {
  381. // level collection, level, difficulty handlers, characteristics
  382. TableView tableView = ReflectionUtil.GetPrivateField<TableView>(_beatUi.LevelCollectionTableView, "_tableView");
  383. // update stats
  384. _beatUi.LevelCollectionViewController.didSelectLevelEvent -= OnDidSelectLevelEvent;
  385. _beatUi.LevelCollectionViewController.didSelectLevelEvent += OnDidSelectLevelEvent;
  386. _beatUi.LevelDetailViewController.didChangeContentEvent -= OnDidPresentContentEvent;
  387. _beatUi.LevelDetailViewController.didChangeContentEvent += OnDidPresentContentEvent;
  388. _beatUi.LevelDetailViewController.didChangeDifficultyBeatmapEvent -= OnDidChangeDifficultyEvent;
  389. _beatUi.LevelDetailViewController.didChangeDifficultyBeatmapEvent += OnDidChangeDifficultyEvent;
  390. // update our view of the game state
  391. _beatUi.LevelFilteringNavigationController.didSelectAnnotatedBeatmapLevelCollectionEvent -= _levelFilteringNavController_didSelectAnnotatedBeatmapLevelCollectionEvent;
  392. _beatUi.LevelFilteringNavigationController.didSelectAnnotatedBeatmapLevelCollectionEvent += _levelFilteringNavController_didSelectAnnotatedBeatmapLevelCollectionEvent;
  393. _beatUi.AnnotatedBeatmapLevelCollectionsViewController.didSelectAnnotatedBeatmapLevelCollectionEvent -= handleDidSelectAnnotatedBeatmapLevelCollection;
  394. _beatUi.AnnotatedBeatmapLevelCollectionsViewController.didSelectAnnotatedBeatmapLevelCollectionEvent += handleDidSelectAnnotatedBeatmapLevelCollection;
  395. // Respond to characteristics changes
  396. _beatUi.BeatmapCharacteristicSelectionViewController.didSelectBeatmapCharacteristicEvent -= OnDidSelectBeatmapCharacteristic;
  397. _beatUi.BeatmapCharacteristicSelectionViewController.didSelectBeatmapCharacteristicEvent += OnDidSelectBeatmapCharacteristic;
  398. // make sure the quick scroll buttons don't desync with regular scrolling
  399. _beatUi.TableViewPageDownButton.onClick.AddListener(delegate ()
  400. {
  401. StartCoroutine(RefreshQuickScrollButtonsAsync());
  402. });
  403. _beatUi.TableViewPageUpButton.onClick.AddListener(delegate ()
  404. {
  405. StartCoroutine(RefreshQuickScrollButtonsAsync());
  406. });
  407. }
  408. /// <summary>
  409. /// Waits for the song UI to be available before trying to update.
  410. /// </summary>
  411. /// <returns></returns>
  412. public IEnumerator AsyncWaitForSongUIUpdate()
  413. {
  414. if (_asyncUpdating)
  415. {
  416. yield break;
  417. }
  418. if (!_uiCreated)
  419. {
  420. yield break;
  421. }
  422. if (!_model.SortWasMissingData)
  423. {
  424. yield break;
  425. }
  426. _asyncUpdating = true;
  427. while (_beatUi.LevelSelectionNavigationController.GetPrivateField<bool>("_isInTransition") ||
  428. _beatUi.LevelDetailViewController.GetPrivateField<bool>("_isInTransition") ||
  429. !_beatUi.LevelSelectionNavigationController.isInViewControllerHierarchy ||
  430. !_beatUi.LevelDetailViewController.isInViewControllerHierarchy ||
  431. !_beatUi.LevelSelectionNavigationController.isActiveAndEnabled ||
  432. !_beatUi.LevelDetailViewController.isActiveAndEnabled)
  433. {
  434. yield return null;
  435. }
  436. //yield return new WaitForEndOfFrame();
  437. if (_model.Settings.sortMode.NeedsScoreSaberData() && SongDataCore.Plugin.Songs.IsDataAvailable())
  438. {
  439. ProcessSongList();
  440. RefreshSongUI();
  441. }
  442. _asyncUpdating = false;
  443. }
  444. /// <summary>
  445. /// Helper to reduce code duplication...
  446. /// </summary>
  447. public void RefreshSongUI(bool scrollToLevel = true)
  448. {
  449. if (!_uiCreated)
  450. {
  451. return;
  452. }
  453. RefreshSongList();
  454. RefreshSortButtonUI();
  455. if (!scrollToLevel)
  456. {
  457. _beatUi.ScrollToLevelByRow(0);
  458. }
  459. RefreshQuickScrollButtons();
  460. RefreshCurrentSelectionDisplay();
  461. }
  462. /// <summary>
  463. /// External helper.
  464. /// </summary>
  465. public void ProcessSongList()
  466. {
  467. if (!_uiCreated)
  468. {
  469. return;
  470. }
  471. this._model.ProcessSongList(_lastLevelCollection, _beatUi.LevelSelectionNavigationController);
  472. }
  473. /// <summary>
  474. /// Helper for common filter cancellation logic.
  475. /// </summary>
  476. public void CancelFilter()
  477. {
  478. Logger.Debug($"Cancelling filter, levelCollection {_lastLevelCollection}");
  479. _model.Settings.filterMode = SongFilterMode.None;
  480. GameObject _noDataGO = _beatUi.LevelCollectionViewController.GetPrivateField<GameObject>("_noDataInfoGO");
  481. string _headerText = _beatUi.LevelCollectionTableView.GetPrivateField<string>("_headerText");
  482. Sprite _headerSprite = _beatUi.LevelCollectionTableView.GetPrivateField<Sprite>("_headerSprite");
  483. IBeatmapLevelCollection levelCollection = _beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection().beatmapLevelCollection;
  484. _beatUi.LevelCollectionViewController.SetData(levelCollection, _headerText, _headerSprite, false, _noDataGO);
  485. }
  486. /// <summary>
  487. /// Playlists (fancy name for AnnotatedBeatmapLevelCollection)
  488. /// </summary>
  489. /// <param name="annotatedBeatmapLevelCollection"></param>
  490. public virtual void handleDidSelectAnnotatedBeatmapLevelCollection(IAnnotatedBeatmapLevelCollection annotatedBeatmapLevelCollection)
  491. {
  492. Logger.Trace("handleDidSelectAnnotatedBeatmapLevelCollection()");
  493. _lastLevelCollection = annotatedBeatmapLevelCollection;
  494. Model.Settings.currentLevelCategoryName = _beatUi.LevelFilteringNavigationController.selectedLevelCategory.ToString();
  495. Model.Settings.Save();
  496. Logger.Debug("Selected Level Collection={0}", _lastLevelCollection);
  497. }
  498. /// <summary>
  499. /// Handler for level collection selection, controller.
  500. /// Sets the current level collection into the model and updates.
  501. /// </summary>
  502. /// <param name="arg1"></param>
  503. /// <param name="arg2"></param>
  504. /// <param name="arg3"></param>
  505. /// <param name="arg4"></param>
  506. private void _levelFilteringNavController_didSelectAnnotatedBeatmapLevelCollectionEvent(LevelFilteringNavigationController arg1, IAnnotatedBeatmapLevelCollection arg2,
  507. GameObject arg3, BeatmapCharacteristicSO arg4)
  508. {
  509. Logger.Trace("_levelFilteringNavController_didSelectAnnotatedBeatmapLevelCollectionEvent(levelCollection={0})", arg2);
  510. if (arg2 == null)
  511. {
  512. // Probably means we transitioned between Music Packs and Playlists
  513. arg2 = _beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection();
  514. if (arg2 == null)
  515. {
  516. Logger.Warning("Nothing selected. This is likely an error.");
  517. return;
  518. }
  519. }
  520. Logger.Debug("Selected Level Collection={0}", arg2);
  521. // Do something about preview level packs, they can't be used past this point
  522. if (arg2 as PreviewBeatmapLevelPackSO)
  523. {
  524. Logger.Info("Hiding SongBrowser, previewing a song pack.");
  525. //CancelFilter();
  526. Hide();
  527. return;
  528. }
  529. else
  530. {
  531. Show();
  532. }
  533. // Skip the first time - Effectively ignores BeatSaber forcing OST1 on us on first load.
  534. // Skip when we have a playlist
  535. if (_lastLevelCollection == null)
  536. {
  537. return;
  538. }
  539. SelectLevelCollection(arg2);
  540. }
  541. /// <summary>
  542. /// Logic for selecting a level collection.
  543. /// </summary>
  544. /// <param name="levelPack"></param>
  545. public void SelectLevelCollection(IAnnotatedBeatmapLevelCollection levelCollection)
  546. {
  547. try
  548. {
  549. if (levelCollection == null)
  550. {
  551. Logger.Debug("No level collection selected...");
  552. return;
  553. }
  554. // store the real level collection
  555. if (levelCollection.collectionName != SongBrowserModel.FilteredSongsCollectionName && _lastLevelCollection != null)
  556. {
  557. Logger.Debug("Recording levelCollection: {0}", levelCollection.collectionName);
  558. _lastLevelCollection = levelCollection;
  559. Model.Settings.currentLevelCategoryName = _beatUi.LevelFilteringNavigationController.selectedLevelCategory.ToString();
  560. }
  561. // reset level selection
  562. _model.LastSelectedLevelId = null;
  563. // save level collection
  564. this._model.Settings.currentLevelCollectionName = levelCollection.collectionName;
  565. this._model.Settings.Save();
  566. StartCoroutine(ProcessSongListEndOfFrame());
  567. }
  568. catch (Exception e)
  569. {
  570. Logger.Exception("Exception handling SelectLevelCollection...", e);
  571. }
  572. }
  573. /// <summary>
  574. /// End of frame update the song list, the game seems to stomp on us sometimes otherwise
  575. /// TODO - Might not be nice to other plugins
  576. /// </summary>
  577. /// <returns></returns>
  578. public IEnumerator ProcessSongListEndOfFrame()
  579. {
  580. yield return new WaitForEndOfFrame();
  581. ProcessSongList();
  582. RefreshSongUI();
  583. }
  584. /// <summary>
  585. /// Remove all filters, update song list, save.
  586. /// </summary>
  587. private void OnClearButtonClickEvent()
  588. {
  589. Logger.Debug("Clearing all sorts and filters.");
  590. _model.Settings.sortMode = SongSortMode.Original;
  591. _model.Settings.invertSortResults = false;
  592. _model.Settings.filterMode = SongFilterMode.None;
  593. _model.Settings.Save();
  594. CancelFilter();
  595. ProcessSongList();
  596. RefreshSongUI();
  597. }
  598. /// <summary>
  599. /// Sort button clicked.
  600. /// </summary>
  601. private void OnSortButtonClickEvent(SongSortMode sortMode)
  602. {
  603. Logger.Debug("Sort button - {0} - pressed.", sortMode.ToString());
  604. if ((sortMode.NeedsScoreSaberData() && !SongDataCore.Plugin.Songs.IsDataAvailable()))
  605. {
  606. Logger.Info("Data for sort type is not available.");
  607. return;
  608. }
  609. // Clear current selected level id so our song list jumps to the start
  610. _model.LastSelectedLevelId = null;
  611. if (_model.Settings.sortMode == sortMode)
  612. {
  613. _model.ToggleInverting();
  614. }
  615. _model.Settings.sortMode = sortMode;
  616. // update the seed
  617. if (_model.Settings.sortMode == SongSortMode.Random)
  618. {
  619. _model.Settings.randomSongSeed = Guid.NewGuid().GetHashCode();
  620. }
  621. _model.Settings.Save();
  622. ProcessSongList();
  623. RefreshSongUI();
  624. }
  625. /// <summary>
  626. /// Handle filter button logic. Some filters have sub menus that need special logic.
  627. /// </summary>
  628. /// <param name="mode"></param>
  629. private void OnFilterButtonClickEvent(SongFilterMode mode)
  630. {
  631. Logger.Debug($"FilterButton {mode} clicked.");
  632. var curCollection = _beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection();
  633. if (_lastLevelCollection == null ||
  634. (curCollection != null &&
  635. curCollection.collectionName != SongBrowserModel.FilteredSongsCollectionName &&
  636. curCollection.collectionName != SongBrowserModel.PlaylistSongsCollectionName))
  637. {
  638. _lastLevelCollection = _beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection();
  639. }
  640. if (mode == SongFilterMode.Favorites)
  641. {
  642. _beatUi.SelectLevelCollection(SelectLevelCategoryViewController.LevelCategory.Favorites.ToString(), SongBrowserSettings.CUSTOM_SONGS_LEVEL_COLLECTION_NAME);
  643. }
  644. else
  645. {
  646. GameObject _noDataGO = _beatUi.LevelCollectionViewController.GetPrivateField<GameObject>("_noDataInfoGO");
  647. string _headerText = _beatUi.LevelCollectionTableView.GetPrivateField<string>("_headerText");
  648. Sprite _headerSprite = _beatUi.LevelCollectionTableView.GetPrivateField<Sprite>("_headerSprite");
  649. IBeatmapLevelCollection levelCollection = _beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection().beatmapLevelCollection;
  650. _beatUi.LevelCollectionViewController.SetData(levelCollection, _headerText, _headerSprite, false, _noDataGO);
  651. }
  652. // If selecting the same filter, cancel
  653. if (_model.Settings.filterMode == mode)
  654. {
  655. _model.Settings.filterMode = SongFilterMode.None;
  656. }
  657. else
  658. {
  659. _model.Settings.filterMode = mode;
  660. }
  661. switch (mode)
  662. {
  663. case SongFilterMode.Search:
  664. OnSearchButtonClickEvent();
  665. break;
  666. default:
  667. _model.Settings.Save();
  668. ProcessSongList();
  669. RefreshSongUI();
  670. break;
  671. }
  672. }
  673. /// <summary>
  674. /// Display the keyboard.
  675. /// </summary>
  676. /// <param name="sortMode"></param>
  677. private void OnSearchButtonClickEvent()
  678. {
  679. Logger.Debug("Filter button - {0} - pressed.", SongFilterMode.Search.ToString());
  680. this.ShowSearchKeyboard();
  681. }
  682. /// <summary>
  683. /// Adjust UI based on level selected.
  684. /// Various ways of detecting if a level is not properly selected. Seems most hit the first one.
  685. /// </summary>
  686. private void OnDidSelectLevelEvent(LevelCollectionViewController view, IPreviewBeatmapLevel level)
  687. {
  688. try
  689. {
  690. Logger.Trace("OnDidSelectLevelEvent()");
  691. if (level == null)
  692. {
  693. Logger.Debug("No level selected?");
  694. return;
  695. }
  696. if (_model.Settings == null)
  697. {
  698. Logger.Debug("Settings not instantiated yet?");
  699. return;
  700. }
  701. _model.LastSelectedLevelId = level.levelID;
  702. HandleDidSelectLevelRow(level);
  703. }
  704. catch (Exception e)
  705. {
  706. Logger.Exception("Exception selecting song:", e);
  707. }
  708. }
  709. /// <summary>
  710. /// Switching one-saber modes for example.
  711. /// </summary>
  712. /// <param name="view"></param>
  713. /// <param name="bc"></param>
  714. private void OnDidSelectBeatmapCharacteristic(BeatmapCharacteristicSegmentedControlController view, BeatmapCharacteristicSO bc)
  715. {
  716. try
  717. {
  718. Logger.Trace("OnDidSelectBeatmapCharacteristic({0})", bc.compoundIdPartName);
  719. _model.CurrentBeatmapCharacteristicSO = bc;
  720. if (_beatUi.StandardLevelDetailView != null)
  721. {
  722. RefreshScoreSaberData(_beatUi.StandardLevelDetailView.selectedDifficultyBeatmap.level);
  723. RefreshNoteJumpSpeed(_beatUi.StandardLevelDetailView.selectedDifficultyBeatmap.noteJumpMovementSpeed,
  724. _beatUi.StandardLevelDetailView.selectedDifficultyBeatmap.noteJumpStartBeatOffset);
  725. }
  726. }
  727. catch (Exception e)
  728. {
  729. Logger.Exception(e);
  730. }
  731. }
  732. /// <summary>
  733. /// Handle difficulty level selection.
  734. /// </summary>
  735. private void OnDidChangeDifficultyEvent(StandardLevelDetailViewController view, IDifficultyBeatmap beatmap)
  736. {
  737. Logger.Trace("OnDidChangeDifficultyEvent({0})", beatmap);
  738. if (view.selectedDifficultyBeatmap == null)
  739. {
  740. return;
  741. }
  742. if (_deleteButton != null)
  743. {
  744. _deleteButton.interactable = (view.selectedDifficultyBeatmap.level.levelID.Length >= 32);
  745. }
  746. RefreshScoreSaberData(view.selectedDifficultyBeatmap.level);
  747. RefreshNoteJumpSpeed(beatmap.noteJumpMovementSpeed, beatmap.noteJumpStartBeatOffset);
  748. }
  749. /// <summary>
  750. /// BeatSaber finished loading content. This is when the difficulty is finally updated.
  751. /// </summary>
  752. /// <param name="view"></param>
  753. /// <param name="type"></param>
  754. private void OnDidPresentContentEvent(StandardLevelDetailViewController view, StandardLevelDetailViewController.ContentType type)
  755. {
  756. Logger.Trace("OnDidPresentContentEvent()");
  757. // v1.12.2 - TODO - is this safe to prevent us from trying to lookup empty/dead content?
  758. if (type != StandardLevelDetailViewController.ContentType.OwnedAndReady)
  759. {
  760. return;
  761. }
  762. if (view.selectedDifficultyBeatmap == null)
  763. {
  764. return;
  765. }
  766. if (_deleteButton != null)
  767. {
  768. _deleteButton.interactable = (_beatUi.LevelDetailViewController.selectedDifficultyBeatmap.level.levelID.Length >= 32);
  769. }
  770. RefreshScoreSaberData(view.selectedDifficultyBeatmap.level);
  771. RefreshNoteJumpSpeed(view.selectedDifficultyBeatmap.noteJumpMovementSpeed, view.selectedDifficultyBeatmap.noteJumpStartBeatOffset);
  772. }
  773. /// <summary>
  774. /// Refresh stats panel.
  775. /// </summary>
  776. /// <param name="level"></param>
  777. private void HandleDidSelectLevelRow(IPreviewBeatmapLevel level)
  778. {
  779. Logger.Trace("HandleDidSelectLevelRow({0})", level);
  780. if (_deleteButton != null)
  781. {
  782. _deleteButton.interactable = (level.levelID.Length >= 32);
  783. }
  784. RefreshQuickScrollButtons();
  785. }
  786. /// <summary>
  787. /// Pop up a delete dialog.
  788. /// </summary>
  789. private void HandleDeleteSelectedLevel()
  790. {
  791. IBeatmapLevel level = _beatUi.LevelDetailViewController.selectedDifficultyBeatmap.level;
  792. _deleteDialog.Init("Delete song", $"Do you really want to delete \"{ level.songName} {level.songSubName}\"?", "Delete", "Cancel",
  793. (selectedButton) =>
  794. {
  795. _beatUi.LevelSelectionFlowCoordinator.InvokePrivateMethod("DismissViewController", new object[] { _deleteDialog, null, false });
  796. if (selectedButton == 0)
  797. {
  798. try
  799. {
  800. // determine the index we are deleting so we can keep the cursor near the same spot after
  801. // the header counts as an index, so if the index came from the level array we have to add 1.
  802. var levelsTableView = _beatUi.LevelCollectionTableView;
  803. List<IPreviewBeatmapLevel> levels = _beatUi.GetCurrentLevelCollectionLevels().ToList();
  804. int selectedIndex = levels.FindIndex(x => x.levelID == _beatUi.StandardLevelDetailView.selectedDifficultyBeatmap.level.levelID);
  805. if (selectedIndex > -1)
  806. {
  807. var song = SongCore.Loader.CustomLevels.First(x => x.Value.levelID == _beatUi.LevelDetailViewController.selectedDifficultyBeatmap.level.levelID).Value;
  808. Logger.Info($"Deleting song: {song.customLevelPath}");
  809. SongCore.Loader.Instance.DeleteSong(song.customLevelPath);
  810. this._model.RemoveSongFromLevelCollection(_beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection(), _beatUi.LevelDetailViewController.selectedDifficultyBeatmap.level.levelID);
  811. int removedLevels = levels.RemoveAll(x => x.levelID == _beatUi.StandardLevelDetailView.selectedDifficultyBeatmap.level.levelID);
  812. Logger.Info("Removed " + removedLevels + " level(s) from song list!");
  813. this.UpdateLevelDataModel();
  814. // if we have a song to select at the same index, set the last selected level id, UI updates takes care of the rest.
  815. if (selectedIndex < levels.Count)
  816. {
  817. if (levels[selectedIndex].levelID != null)
  818. {
  819. _model.LastSelectedLevelId = levels[selectedIndex].levelID;
  820. }
  821. }
  822. this.RefreshSongList();
  823. }
  824. }
  825. catch (Exception e)
  826. {
  827. Logger.Error("Unable to delete song! Exception: " + e);
  828. }
  829. }
  830. });
  831. _beatUi.LevelSelectionFlowCoordinator.InvokePrivateMethod("PresentViewController", new object[] { _deleteDialog, null, false });
  832. }
  833. /// <summary>
  834. /// Display the search keyboard
  835. /// </summary>
  836. void ShowSearchKeyboard()
  837. {
  838. var modalKbTag = new BeatSaberMarkupLanguage.Tags.ModalKeyboardTag();
  839. var modalKbView = modalKbTag.CreateObject(_beatUi.LevelSelectionNavigationController.rectTransform);
  840. modalKbView.gameObject.SetActive(true);
  841. var modalKb = modalKbView.GetComponent<ModalKeyboard>();
  842. modalKb.gameObject.SetActive(true);
  843. modalKb.keyboard.EnterPressed += SearchViewControllerSearchButtonPressed;
  844. modalKb.modalView.Show(true, true);
  845. }
  846. /// <summary>
  847. /// Handle search.
  848. /// </summary>
  849. /// <param name="searchFor"></param>
  850. private void SearchViewControllerSearchButtonPressed(string searchFor)
  851. {
  852. Logger.Debug("Searching for \"{0}\"...", searchFor);
  853. _model.Settings.filterMode = SongFilterMode.Search;
  854. _model.Settings.searchTerms.Insert(0, searchFor);
  855. _model.Settings.Save();
  856. _model.LastSelectedLevelId = null;
  857. ProcessSongList();
  858. RefreshSongUI();
  859. }
  860. /// <summary>
  861. /// Make big jumps in the song list.
  862. /// </summary>
  863. /// <param name="numJumps"></param>
  864. private void JumpSongList(int numJumps, float segmentPercent)
  865. {
  866. var levels = _beatUi.GetCurrentLevelCollectionLevels();
  867. if (levels == null)
  868. {
  869. return;
  870. }
  871. int totalSize = levels.Count();
  872. int segmentSize = (int)(totalSize * segmentPercent);
  873. // Jump at least one scree size.
  874. if (segmentSize < LIST_ITEMS_VISIBLE_AT_ONCE)
  875. {
  876. segmentSize = LIST_ITEMS_VISIBLE_AT_ONCE;
  877. }
  878. int currentRow = _beatUi.LevelCollectionTableView.GetPrivateField<int>("_selectedRow");
  879. int jumpDirection = Math.Sign(numJumps);
  880. int newRow = currentRow + (jumpDirection * segmentSize);
  881. if (newRow <= 0)
  882. {
  883. newRow = 0;
  884. }
  885. else if (newRow >= totalSize)
  886. {
  887. newRow = totalSize - 1;
  888. }
  889. Logger.Debug("jumpDirection: {0}, newRow: {1}", jumpDirection, newRow);
  890. _beatUi.SelectAndScrollToLevel(levels[newRow].levelID);
  891. RefreshQuickScrollButtons();
  892. }
  893. /// <summary>
  894. /// Update GUI elements that show score saber data.
  895. /// </summary>
  896. public void RefreshScoreSaberData(IPreviewBeatmapLevel level)
  897. {
  898. Logger.Trace("RefreshScoreSaberData({0})", level.levelID);
  899. if (!SongDataCore.Plugin.Songs.IsDataAvailable())
  900. {
  901. return;
  902. }
  903. BeatmapDifficulty difficulty = _beatUi.LevelDifficultyViewController.selectedDifficulty;
  904. string difficultyString = difficulty.ToString();
  905. if (difficultyString.Equals("ExpertPlus"))
  906. {
  907. difficultyString = "Expert+";
  908. }
  909. Logger.Debug(difficultyString);
  910. // Check if we have data for this song
  911. Logger.Debug("Checking if have info for song {0}", level.songName);
  912. var hash = SongBrowserModel.GetSongHash(level.levelID);
  913. if (SongDataCore.Plugin.Songs.Data.Songs.ContainsKey(hash))
  914. {
  915. Logger.Debug("Checking if have difficulty for song {0} difficulty {1}", level.songName, difficultyString);
  916. BeatStarSong scoreSaberSong = SongDataCore.Plugin.Songs.Data.Songs[hash];
  917. BeatStarSongDifficultyStats scoreSaberSongDifficulty = scoreSaberSong.diffs.FirstOrDefault(x => String.Equals(x.diff, difficultyString));
  918. if (scoreSaberSongDifficulty != null)
  919. {
  920. Logger.Debug("Display pp for song.");
  921. double pp = scoreSaberSongDifficulty.pp;
  922. double star = scoreSaberSongDifficulty.star;
  923. BeatSaberUI.SetStatButtonText(_ppStatButton, String.Format("{0:0.#}", pp));
  924. BeatSaberUI.SetStatButtonText(_starStatButton, String.Format("{0:0.#}", star));
  925. }
  926. else
  927. {
  928. BeatSaberUI.SetStatButtonText(_ppStatButton, "NA");
  929. BeatSaberUI.SetStatButtonText(_starStatButton, "NA");
  930. }
  931. }
  932. else
  933. {
  934. BeatSaberUI.SetStatButtonText(_ppStatButton, "NA");
  935. BeatSaberUI.SetStatButtonText(_starStatButton, "NA");
  936. }
  937. Logger.Debug("Done refreshing score saber stats.");
  938. }
  939. /// <summary>
  940. /// Helper to refresh the NJS widget.
  941. /// </summary>
  942. /// <param name="noteJumpMovementSpeed"></param>
  943. private void RefreshNoteJumpSpeed(float noteJumpMovementSpeed, float noteJumpStartBeatOffset)
  944. {
  945. BeatSaberUI.SetStatButtonText(_njsStatButton, String.Format("{0}", noteJumpMovementSpeed));
  946. BeatSaberUI.SetStatButtonText(_noteJumpStartBeatOffsetLabel, String.Format("{0}", noteJumpStartBeatOffset));
  947. }
  948. /// <summary>
  949. /// Update interactive state of the quick scroll buttons.
  950. /// </summary>
  951. private void RefreshQuickScrollButtons()
  952. {
  953. if (!_uiCreated)
  954. {
  955. return;
  956. }
  957. _pageUpFastButton.interactable = _beatUi.TableViewPageUpButton.interactable;
  958. _pageUpFastButton.gameObject.SetActive(_beatUi.TableViewPageUpButton.IsActive());
  959. _pageDownFastButton.interactable = _beatUi.TableViewPageDownButton.interactable;
  960. _pageDownFastButton.gameObject.SetActive(_beatUi.TableViewPageDownButton.IsActive());
  961. }
  962. /// <summary>
  963. /// TODO - evaluate this sillyness...
  964. /// </summary>
  965. /// <returns></returns>
  966. public IEnumerator RefreshQuickScrollButtonsAsync()
  967. {
  968. yield return new WaitForEndOfFrame();
  969. RefreshQuickScrollButtons();
  970. }
  971. /// <summary>
  972. /// Show the UI.
  973. /// </summary>
  974. public void Show()
  975. {
  976. Logger.Trace("Show SongBrowserUI()");
  977. this.SetVisibility(true);
  978. }
  979. /// <summary>
  980. /// Hide the UI.
  981. /// </summary>
  982. public void Hide()
  983. {
  984. Logger.Trace("Hide SongBrowserUI()");
  985. this.SetVisibility(false);
  986. }
  987. /// <summary>
  988. /// Handle showing or hiding UI logic.
  989. /// </summary>
  990. /// <param name="visible"></param>
  991. private void SetVisibility(bool visible)
  992. {
  993. // UI not created, nothing visible to hide...
  994. if (!_uiCreated)
  995. {
  996. return;
  997. }
  998. _ppStatButton?.gameObject.SetActive(visible);
  999. _starStatButton?.gameObject.SetActive(visible);
  1000. _njsStatButton?.gameObject.SetActive(visible);
  1001. RefreshOuterUIState(visible == true ? UIState.Main : UIState.Disabled);
  1002. _deleteButton?.gameObject.SetActive(visible);
  1003. _pageUpFastButton?.gameObject.SetActive(visible);
  1004. _pageDownFastButton?.gameObject.SetActive(visible);
  1005. }
  1006. /// <summary>
  1007. /// Update the top UI state.
  1008. /// Hides the outer ui, sort, and filter buttons depending on the state.
  1009. /// </summary>
  1010. private void RefreshOuterUIState(UIState state)
  1011. {
  1012. bool sortButtons = false;
  1013. bool filterButtons = false;
  1014. bool outerButtons = false;
  1015. bool clearButton = true;
  1016. if (state == UIState.SortBy)
  1017. {
  1018. sortButtons = true;
  1019. }
  1020. else if (state == UIState.FilterBy)
  1021. {
  1022. filterButtons = true;
  1023. }
  1024. else if (state == UIState.Main)
  1025. {
  1026. outerButtons = true;
  1027. }
  1028. else
  1029. {
  1030. clearButton = false;
  1031. }
  1032. _sortButtonGroup.ForEach(x => x.Button.gameObject.SetActive(sortButtons));
  1033. _filterButtonGroup.ForEach(x => x.Button.gameObject.SetActive(filterButtons));
  1034. _sortByButton?.gameObject.SetActive(outerButtons);
  1035. _sortByDisplay?.gameObject.SetActive(outerButtons);
  1036. _filterByButton?.gameObject.SetActive(outerButtons);
  1037. _filterByDisplay?.gameObject.SetActive(outerButtons);
  1038. _clearSortFilterButton?.gameObject.SetActive(clearButton);
  1039. _randomButton?.gameObject.SetActive(outerButtons);
  1040. RefreshCurrentSelectionDisplay();
  1041. _currentUiState = state;
  1042. }
  1043. /// <summary>
  1044. /// Adjust the text field of the sort by and filter by displays.
  1045. /// </summary>
  1046. private void RefreshCurrentSelectionDisplay()
  1047. {
  1048. string sortByDisplay;
  1049. if (_model.Settings.sortMode == SongSortMode.Default)
  1050. {
  1051. sortByDisplay = "Title";
  1052. }
  1053. else
  1054. {
  1055. sortByDisplay = _model.Settings.sortMode.ToString();
  1056. }
  1057. _sortByDisplay.SetButtonText(sortByDisplay);
  1058. if (_model.Settings.filterMode != SongFilterMode.Custom)
  1059. {
  1060. // Custom SongFilterMod implies that another mod has modified the text of this button (do not overwrite)
  1061. _filterByDisplay.SetButtonText(_model.Settings.filterMode.ToString());
  1062. }
  1063. }
  1064. /// <summary>
  1065. /// Adjust the UI colors.
  1066. /// </summary>
  1067. public void RefreshSortButtonUI()
  1068. {
  1069. if (!_uiCreated)
  1070. {
  1071. return;
  1072. }
  1073. foreach (SongSortButton sortButton in _sortButtonGroup)
  1074. {
  1075. if (sortButton.SortMode.NeedsScoreSaberData() && !SongDataCore.Plugin.Songs.IsDataAvailable())
  1076. {
  1077. sortButton.Button.SetButtonUnderlineColor(Color.gray);
  1078. }
  1079. else
  1080. {
  1081. sortButton.Button.SetButtonUnderlineColor(Color.white);
  1082. }
  1083. if (sortButton.SortMode == _model.Settings.sortMode)
  1084. {
  1085. if (this._model.Settings.invertSortResults)
  1086. {
  1087. sortButton.Button.SetButtonUnderlineColor(Color.red);
  1088. }
  1089. else
  1090. {
  1091. sortButton.Button.SetButtonUnderlineColor(Color.green);
  1092. }
  1093. }
  1094. }
  1095. foreach (SongFilterButton filterButton in _filterButtonGroup)
  1096. {
  1097. filterButton.Button.SetButtonUnderlineColor(Color.white);
  1098. if (filterButton.FilterMode == _model.Settings.filterMode)
  1099. {
  1100. filterButton.Button.SetButtonUnderlineColor(Color.green);
  1101. }
  1102. }
  1103. if (this._model.Settings.invertSortResults)
  1104. {
  1105. _sortByDisplay.SetButtonUnderlineColor(Color.red);
  1106. }
  1107. else
  1108. {
  1109. _sortByDisplay.SetButtonUnderlineColor(Color.green);
  1110. }
  1111. if (this._model.Settings.filterMode != SongFilterMode.None)
  1112. {
  1113. _filterByDisplay.SetButtonUnderlineColor(Color.green);
  1114. }
  1115. else
  1116. {
  1117. _filterByDisplay.SetButtonUnderlineColor(Color.white);
  1118. }
  1119. }
  1120. /// <summary>
  1121. ///
  1122. /// </summary>
  1123. public void RefreshSongList()
  1124. {
  1125. if (!_uiCreated)
  1126. {
  1127. return;
  1128. }
  1129. _beatUi.RefreshSongList(_model.LastSelectedLevelId);
  1130. }
  1131. /// <summary>
  1132. /// Helper for updating the model (which updates the song list)
  1133. /// </summary>
  1134. public void UpdateLevelDataModel()
  1135. {
  1136. try
  1137. {
  1138. Logger.Trace("UpdateLevelDataModel()");
  1139. // get a current beatmap characteristic...
  1140. if (_model.CurrentBeatmapCharacteristicSO == null && _uiCreated)
  1141. {
  1142. _model.CurrentBeatmapCharacteristicSO = _beatUi.BeatmapCharacteristicSelectionViewController.GetPrivateField<BeatmapCharacteristicSO>("_selectedBeatmapCharacteristic");
  1143. }
  1144. _model.UpdateLevelRecords();
  1145. }
  1146. catch (Exception e)
  1147. {
  1148. Logger.Exception("SongBrowser UI crashed trying to update the internal song lists: ", e);
  1149. }
  1150. }
  1151. /// <summary>
  1152. /// Logic for fixing BeatSaber's level pack selection bugs.
  1153. /// </summary>
  1154. public bool UpdateLevelCollectionSelection()
  1155. {
  1156. if (_uiCreated)
  1157. {
  1158. IAnnotatedBeatmapLevelCollection currentSelected = _beatUi.GetCurrentSelectedAnnotatedBeatmapLevelCollection();
  1159. Logger.Debug("Current selected level collection: {0}", currentSelected);
  1160. if (String.IsNullOrEmpty(_model.Settings.currentLevelCollectionName))
  1161. {
  1162. if (currentSelected == null)
  1163. {
  1164. Logger.Debug("No level collection selected, acquiring the first available, likely OST1...");
  1165. currentSelected = _beatUi.BeatmapLevelsModel.allLoadedBeatmapLevelPackCollection.beatmapLevelPacks[0];
  1166. }
  1167. }
  1168. else if (currentSelected == null || (currentSelected.collectionName != _model.Settings.currentLevelCollectionName))
  1169. {
  1170. Logger.Debug("Automatically selecting level collection: {0}", _model.Settings.currentLevelCollectionName);
  1171. _beatUi.LevelFilteringNavigationController.didSelectAnnotatedBeatmapLevelCollectionEvent -= _levelFilteringNavController_didSelectAnnotatedBeatmapLevelCollectionEvent;
  1172. _lastLevelCollection = _beatUi.GetLevelCollectionByName(_model.Settings.currentLevelCollectionName);
  1173. if (_lastLevelCollection as PreviewBeatmapLevelPackSO)
  1174. {
  1175. Hide();
  1176. }
  1177. _beatUi.SelectLevelCollection(_model.Settings.currentLevelCategoryName, _model.Settings.currentLevelCollectionName);
  1178. _beatUi.LevelFilteringNavigationController.didSelectAnnotatedBeatmapLevelCollectionEvent += _levelFilteringNavController_didSelectAnnotatedBeatmapLevelCollectionEvent;
  1179. }
  1180. if (_lastLevelCollection == null)
  1181. {
  1182. if (currentSelected.collectionName != SongBrowserModel.FilteredSongsCollectionName && currentSelected.collectionName != SongBrowserModel.PlaylistSongsCollectionName)
  1183. {
  1184. _lastLevelCollection = currentSelected;
  1185. }
  1186. }
  1187. Logger.Debug("Current Level Collection is: {0}", _lastLevelCollection);
  1188. ProcessSongList();
  1189. }
  1190. return false;
  1191. }
  1192. }
  1193. }