UIButtonNavigation.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using AnimationOrTween;
  3. using Core;
  4. using UnityEngine;
  5. [RequireComponent(typeof(UIButton))]
  6. public class UIButtonNavigation : MonoBehaviour
  7. {
  8. public UIButton button
  9. {
  10. get
  11. {
  12. UIButton result;
  13. if ((result = this._button) == null)
  14. {
  15. result = (this._button = base.GetComponent<UIButton>());
  16. }
  17. return result;
  18. }
  19. }
  20. public static UIButtonNavigation current
  21. {
  22. get
  23. {
  24. GameObject hoveredObject = UICamera.hoveredObject;
  25. if (hoveredObject == null)
  26. {
  27. return null;
  28. }
  29. return hoveredObject.GetComponent<UIButtonNavigation>();
  30. }
  31. }
  32. public bool isColliderEnabled
  33. {
  34. get
  35. {
  36. if (!base.enabled || !base.gameObject.activeInHierarchy)
  37. {
  38. return false;
  39. }
  40. Collider component = base.GetComponent<Collider>();
  41. if (component != null)
  42. {
  43. return component.enabled;
  44. }
  45. Collider2D component2 = base.GetComponent<Collider2D>();
  46. return component2 != null && component2.enabled;
  47. }
  48. }
  49. protected virtual void OnEnable()
  50. {
  51. UIButtonNavigation.list.Add(this);
  52. if (this.mStarted)
  53. {
  54. this.Start();
  55. }
  56. }
  57. private void Start()
  58. {
  59. this.mStarted = true;
  60. if (this.startsSelected && this.isColliderEnabled)
  61. {
  62. UICamera.hoveredObject = base.gameObject;
  63. }
  64. }
  65. protected virtual void OnDisable()
  66. {
  67. UIButtonNavigation.list.Remove(this);
  68. }
  69. private static bool IsActive(GameObject go)
  70. {
  71. if (!go || !go.activeInHierarchy)
  72. {
  73. return false;
  74. }
  75. Collider component = go.GetComponent<Collider>();
  76. if (component != null)
  77. {
  78. return component.enabled;
  79. }
  80. Collider2D component2 = go.GetComponent<Collider2D>();
  81. return component2 != null && component2.enabled;
  82. }
  83. public GameObject GetLeft()
  84. {
  85. if (UIButtonNavigation.IsActive(this.onLeft))
  86. {
  87. return this.onLeft;
  88. }
  89. if (this.constraint == UIButtonNavigation.Constraint.Vertical || this.constraint == UIButtonNavigation.Constraint.Explicit)
  90. {
  91. return null;
  92. }
  93. return this.Get(Vector3.left, 1f, 2f);
  94. }
  95. public GameObject GetRight()
  96. {
  97. if (UIButtonNavigation.IsActive(this.onRight))
  98. {
  99. return this.onRight;
  100. }
  101. if (this.constraint == UIButtonNavigation.Constraint.Vertical || this.constraint == UIButtonNavigation.Constraint.Explicit)
  102. {
  103. return null;
  104. }
  105. return this.Get(Vector3.right, 1f, 2f);
  106. }
  107. public GameObject GetUp()
  108. {
  109. if (UIButtonNavigation.IsActive(this.onUp))
  110. {
  111. return this.onUp;
  112. }
  113. if (this.constraint == UIButtonNavigation.Constraint.Horizontal || this.constraint == UIButtonNavigation.Constraint.Explicit)
  114. {
  115. return null;
  116. }
  117. return this.Get(Vector3.up, 2f, 1f);
  118. }
  119. public GameObject GetDown()
  120. {
  121. if (UIButtonNavigation.IsActive(this.onDown))
  122. {
  123. return this.onDown;
  124. }
  125. if (this.constraint == UIButtonNavigation.Constraint.Horizontal || this.constraint == UIButtonNavigation.Constraint.Explicit)
  126. {
  127. return null;
  128. }
  129. return this.Get(Vector3.down, 2f, 1f);
  130. }
  131. public GameObject Get(Vector3 myDir, float x = 1f, float y = 1f)
  132. {
  133. Transform transform = base.transform;
  134. myDir = transform.TransformDirection(myDir);
  135. Vector3 center = UIButtonNavigation.GetCenter(base.gameObject);
  136. float num = float.MaxValue;
  137. GameObject result = null;
  138. for (int i = 0; i < UIButtonNavigation.list.size; i++)
  139. {
  140. UIButtonNavigation uibuttonNavigation = UIButtonNavigation.list[i];
  141. if (!(uibuttonNavigation == this) && uibuttonNavigation.constraint != UIButtonNavigation.Constraint.Explicit && uibuttonNavigation.isColliderEnabled)
  142. {
  143. UIWidget component = uibuttonNavigation.GetComponent<UIWidget>();
  144. if (!(component != null) || component.alpha != 0f)
  145. {
  146. Vector3 direction = UIButtonNavigation.GetCenter(uibuttonNavigation.gameObject) - center;
  147. float num2 = Vector3.Dot(myDir, direction.normalized);
  148. if (num2 >= 0.707f)
  149. {
  150. direction = transform.InverseTransformDirection(direction);
  151. direction.x *= x;
  152. direction.y *= y;
  153. float sqrMagnitude = direction.sqrMagnitude;
  154. if (sqrMagnitude <= num)
  155. {
  156. result = uibuttonNavigation.gameObject;
  157. num = sqrMagnitude;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return result;
  164. }
  165. protected static Vector3 GetCenter(GameObject go)
  166. {
  167. UIWidget component = go.GetComponent<UIWidget>();
  168. UICamera uicamera = UICamera.FindCameraForLayer(go.layer);
  169. if (uicamera != null)
  170. {
  171. Vector3 vector = go.transform.position;
  172. if (component != null)
  173. {
  174. Vector3[] worldCorners = component.worldCorners;
  175. vector = (worldCorners[0] + worldCorners[2]) * 0.5f;
  176. }
  177. vector = uicamera.cachedCamera.WorldToScreenPoint(vector);
  178. vector.z = 0f;
  179. return vector;
  180. }
  181. if (component != null)
  182. {
  183. Vector3[] worldCorners2 = component.worldCorners;
  184. return (worldCorners2[0] + worldCorners2[2]) * 0.5f;
  185. }
  186. return go.transform.position;
  187. }
  188. public virtual void OnNavigate()
  189. {
  190. if (UIPopupList.isOpen)
  191. {
  192. return;
  193. }
  194. GameObject gameObject = null;
  195. if (Core.Input.UI.Left.OnPressedForSeveralSeconds(0.333333343f, true))
  196. {
  197. gameObject = this.GetLeft();
  198. }
  199. else if (Core.Input.UI.Right.OnPressedForSeveralSeconds(0.333333343f, true))
  200. {
  201. gameObject = this.GetRight();
  202. }
  203. else if (Core.Input.UI.Up.OnPressedForSeveralSeconds(0.333333343f, true))
  204. {
  205. gameObject = this.GetUp();
  206. }
  207. else if (Core.Input.UI.Down.OnPressedForSeveralSeconds(0.333333343f, true))
  208. {
  209. gameObject = this.GetDown();
  210. }
  211. if (gameObject != null)
  212. {
  213. R.Audio.PlayEffect(3, null);
  214. }
  215. if (Core.Input.UI.Confirm.OnClick)
  216. {
  217. gameObject = this.onClick;
  218. if (gameObject != null)
  219. {
  220. UIKeyInput.SaveHoveredObject();
  221. }
  222. this.button.SendMessage("OnClick");
  223. UIPlayAnimation[] components = base.GetComponents<UIPlayAnimation>();
  224. if (components != null)
  225. {
  226. for (int i = 0; i < components.Length; i++)
  227. {
  228. if (components[i].enabled && components[i].trigger == Trigger.OnClick)
  229. {
  230. Direction playDirection = components[i].playDirection;
  231. if (playDirection != Direction.Forward)
  232. {
  233. if (playDirection != Direction.Reverse)
  234. {
  235. if (playDirection == Direction.Toggle)
  236. {
  237. components[i].Play(true);
  238. }
  239. }
  240. else
  241. {
  242. components[i].Play(true, false);
  243. }
  244. }
  245. else
  246. {
  247. components[i].Play(true, false);
  248. }
  249. }
  250. }
  251. R.Audio.PlayEffect(139, null);
  252. }
  253. }
  254. if (gameObject != null)
  255. {
  256. this.button.state = UIButtonColor.State.Normal;
  257. UIKeyInput.HoveredObject = gameObject;
  258. }
  259. }
  260. public virtual void OnKey(KeyCode key)
  261. {
  262. }
  263. public static BetterList<UIButtonNavigation> list = new BetterList<UIButtonNavigation>();
  264. private UIButton _button;
  265. public UIButtonNavigation.Constraint constraint;
  266. public GameObject onUp;
  267. public GameObject onDown;
  268. public GameObject onLeft;
  269. public GameObject onRight;
  270. public GameObject onClick;
  271. public GameObject onTab;
  272. public bool startsSelected;
  273. [NonSerialized]
  274. private bool mStarted;
  275. public enum Constraint
  276. {
  277. None,
  278. Vertical,
  279. Horizontal,
  280. Explicit
  281. }
  282. }