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.

88 lines
2.7 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 ModListFlowCoordinator menuFlow;
  43. private static readonly WaitUntil _bottomPanelExists = new WaitUntil(() => GameObject.Find(ControllerPanel) != null);
  44. private static RectTransform panel;
  45. private static HoverHint hintText;
  46. private static Button button;
  47. private static IEnumerator AddModListButton()
  48. {
  49. Logger.log.Debug("AddModListButton");
  50. yield return _bottomPanelExists;
  51. Logger.log.Debug("Adding button to main menu");
  52. lock (Instance)
  53. {
  54. if (menuFlow == null)
  55. menuFlow = new GameObject("BSIPA Mod List Flow Controller").AddComponent<ModListFlowCoordinator>();
  56. if (panel == null)
  57. panel = GameObject.Find(ControllerPanel).transform as RectTransform;
  58. if (button == null)
  59. {
  60. button = BeatSaberUI.CreateUIButton(panel, CopyButton, () =>
  61. {
  62. Logger.log.Debug("Presenting own flow controller");
  63. menuFlow.Present();
  64. }, "Mod List");
  65. panel.Find(CopyButton).SetAsLastSibling();
  66. hintText = BeatSaberUI.AddHintText(button.transform as RectTransform, "View and control updates for installed mods");
  67. }
  68. yield break;
  69. }
  70. }
  71. }
  72. }