You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB

  1. using CustomUI.BeatSaber;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. using UnityEngine.UI;
  11. namespace BSIPA_ModList.UI
  12. {
  13. internal class ButtonUI : MonoBehaviour
  14. {
  15. private const string ControllerPanel = "MainMenuViewController/SmallButtons";
  16. private const string CopyButton = "CreditsButton";
  17. internal static ButtonUI Instance;
  18. public void Awake()
  19. {
  20. DontDestroyOnLoad(gameObject);
  21. SceneManager.activeSceneChanged += this.SceneManager_activeSceneChanged;
  22. }
  23. private void SceneManager_activeSceneChanged(Scene from, Scene to)
  24. {
  25. if (to.name == "EmptyTransition")
  26. {
  27. if (Instance != null)
  28. {
  29. Instance.StopAllCoroutines();
  30. Destroy(Instance.gameObject);
  31. menuFlow = null;
  32. }
  33. Instance = null;
  34. }
  35. }
  36. public void Init()
  37. {
  38. Instance = this;
  39. Logger.log.Debug("UI Awake");
  40. StartCoroutine(AddModListButton());
  41. }
  42. private static MainFlowCoordinator mainFlow;
  43. private static ModListFlowCoordinator menuFlow;
  44. private static readonly WaitUntil _bottomPanelExists = new WaitUntil(() => GameObject.Find(ControllerPanel) != null);
  45. private static RectTransform panel;
  46. private static HoverHint hintText;
  47. private static Button button;
  48. private static IEnumerator AddModListButton()
  49. {
  50. Logger.log.Debug("AddModListButton");
  51. yield return _bottomPanelExists;
  52. Logger.log.Debug("Adding button to main menu");
  53. lock (Instance)
  54. {
  55. if (mainFlow == null)
  56. mainFlow = Resources.FindObjectsOfTypeAll<MainFlowCoordinator>().First();
  57. if (menuFlow == null)
  58. menuFlow = new GameObject("BSIPA Mod List Flow Controller").AddComponent<ModListFlowCoordinator>();
  59. if (panel == null)
  60. panel = GameObject.Find(ControllerPanel).transform as RectTransform;
  61. if (button == null)
  62. {
  63. button = BeatSaberUI.CreateUIButton(panel, CopyButton, () =>
  64. {
  65. Logger.log.Debug("Presenting own flow controller");
  66. menuFlow.PresentOn(mainFlow);
  67. }, "Mod List");
  68. panel.Find(CopyButton).SetAsLastSibling();
  69. hintText = BeatSaberUI.AddHintText(button.transform as RectTransform, "View and control updates for installed mods");
  70. }
  71. yield break;
  72. }
  73. }
  74. }
  75. }