MobileMenuController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using SRDebugger.UI.Other;
  3. using SRF;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7. namespace SRDebugger.UI
  8. {
  9. public class MobileMenuController : SRMonoBehaviourEx
  10. {
  11. public float PeekAmount
  12. {
  13. get
  14. {
  15. return this._peekAmount;
  16. }
  17. }
  18. public float MaxMenuWidth
  19. {
  20. get
  21. {
  22. return this._maxMenuWidth;
  23. }
  24. }
  25. protected override void OnEnable()
  26. {
  27. base.OnEnable();
  28. RectTransform rectTransform = this.Menu.parent as RectTransform;
  29. LayoutElement component = this.Menu.GetComponent<LayoutElement>();
  30. component.ignoreLayout = true;
  31. this.Menu.pivot = new Vector2(1f, 1f);
  32. this.Menu.offsetMin = new Vector2(1f, 0f);
  33. this.Menu.offsetMax = new Vector2(1f, 1f);
  34. this.Menu.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Clamp(rectTransform.rect.width - this.PeekAmount, 0f, this.MaxMenuWidth));
  35. this.Menu.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rectTransform.rect.height);
  36. this.Menu.anchoredPosition = new Vector2(0f, 0f);
  37. if (this._closeButton == null)
  38. {
  39. this.CreateCloseButton();
  40. }
  41. this.OpenButton.gameObject.SetActive(true);
  42. this.TabController.ActiveTabChanged += this.TabControllerOnActiveTabChanged;
  43. }
  44. protected override void OnDisable()
  45. {
  46. base.OnDisable();
  47. LayoutElement component = this.Menu.GetComponent<LayoutElement>();
  48. component.ignoreLayout = false;
  49. this.Content.anchoredPosition = new Vector2(0f, 0f);
  50. this._closeButton.gameObject.SetActive(false);
  51. this.OpenButton.gameObject.SetActive(false);
  52. this.TabController.ActiveTabChanged -= this.TabControllerOnActiveTabChanged;
  53. }
  54. private void CreateCloseButton()
  55. {
  56. GameObject gameObject = new GameObject("SR_CloseButtonCanvas", new Type[]
  57. {
  58. typeof(RectTransform)
  59. });
  60. gameObject.transform.SetParent(this.Content, false);
  61. Canvas canvas = gameObject.AddComponent<Canvas>();
  62. gameObject.AddComponent<GraphicRaycaster>();
  63. RectTransform componentOrAdd = gameObject.GetComponentOrAdd<RectTransform>();
  64. canvas.overrideSorting = true;
  65. canvas.sortingOrder = 122;
  66. gameObject.AddComponent<LayoutElement>().ignoreLayout = true;
  67. this.SetRectSize(componentOrAdd);
  68. GameObject gameObject2 = new GameObject("SR_CloseButton", new Type[]
  69. {
  70. typeof(RectTransform)
  71. });
  72. gameObject2.transform.SetParent(componentOrAdd, false);
  73. RectTransform component = gameObject2.GetComponent<RectTransform>();
  74. this.SetRectSize(component);
  75. gameObject2.AddComponent<Image>().color = new Color(0f, 0f, 0f, 0f);
  76. this._closeButton = gameObject2.AddComponent<Button>();
  77. this._closeButton.transition = Selectable.Transition.None;
  78. this._closeButton.onClick.AddListener(new UnityAction(this.CloseButtonClicked));
  79. this._closeButton.gameObject.SetActive(false);
  80. }
  81. private void SetRectSize(RectTransform rect)
  82. {
  83. rect.anchorMin = new Vector2(0f, 0f);
  84. rect.anchorMax = new Vector2(1f, 1f);
  85. rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, this.Content.rect.width);
  86. rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, this.Content.rect.height);
  87. }
  88. private void CloseButtonClicked()
  89. {
  90. this.Close();
  91. }
  92. protected override void Update()
  93. {
  94. base.Update();
  95. float x = this.Content.anchoredPosition.x;
  96. if (Mathf.Abs(this._targetXPosition - x) < 2.5f)
  97. {
  98. this.Content.anchoredPosition = new Vector2(this._targetXPosition, this.Content.anchoredPosition.y);
  99. }
  100. else
  101. {
  102. this.Content.anchoredPosition = new Vector2(SRMath.SpringLerp(x, this._targetXPosition, 15f, Time.unscaledDeltaTime), this.Content.anchoredPosition.y);
  103. }
  104. }
  105. private void TabControllerOnActiveTabChanged(SRTabController srTabController, SRTab srTab)
  106. {
  107. this.Close();
  108. }
  109. [ContextMenu("Open")]
  110. public void Open()
  111. {
  112. this._targetXPosition = this.Menu.rect.width;
  113. this._closeButton.gameObject.SetActive(true);
  114. }
  115. [ContextMenu("Close")]
  116. public void Close()
  117. {
  118. this._targetXPosition = 0f;
  119. this._closeButton.gameObject.SetActive(false);
  120. }
  121. private Button _closeButton;
  122. [SerializeField]
  123. private float _maxMenuWidth = 185f;
  124. [SerializeField]
  125. private float _peekAmount = 45f;
  126. private float _targetXPosition;
  127. [RequiredField]
  128. public RectTransform Content;
  129. [RequiredField]
  130. public RectTransform Menu;
  131. [RequiredField]
  132. public Button OpenButton;
  133. [RequiredField]
  134. public SRTabController TabController;
  135. }
  136. }