SongBrowserUI.cs 54 KB

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