UIKeyNavigation.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Key Navigation")]
  4. public class UIKeyNavigation : MonoBehaviour
  5. {
  6. protected virtual void OnEnable()
  7. {
  8. UIKeyNavigation.list.Add(this);
  9. if (this.startsSelected && (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject)))
  10. {
  11. UICamera.currentScheme = UICamera.ControlScheme.Controller;
  12. UICamera.selectedObject = base.gameObject;
  13. }
  14. }
  15. protected virtual void OnDisable()
  16. {
  17. UIKeyNavigation.list.Remove(this);
  18. }
  19. protected GameObject GetLeft()
  20. {
  21. if (NGUITools.GetActive(this.onLeft))
  22. {
  23. return this.onLeft;
  24. }
  25. if (this.constraint == UIKeyNavigation.Constraint.Vertical || this.constraint == UIKeyNavigation.Constraint.Explicit)
  26. {
  27. return null;
  28. }
  29. return this.Get(Vector3.left, true);
  30. }
  31. private GameObject GetRight()
  32. {
  33. if (NGUITools.GetActive(this.onRight))
  34. {
  35. return this.onRight;
  36. }
  37. if (this.constraint == UIKeyNavigation.Constraint.Vertical || this.constraint == UIKeyNavigation.Constraint.Explicit)
  38. {
  39. return null;
  40. }
  41. return this.Get(Vector3.right, true);
  42. }
  43. protected GameObject GetUp()
  44. {
  45. if (NGUITools.GetActive(this.onUp))
  46. {
  47. return this.onUp;
  48. }
  49. if (this.constraint == UIKeyNavigation.Constraint.Horizontal || this.constraint == UIKeyNavigation.Constraint.Explicit)
  50. {
  51. return null;
  52. }
  53. return this.Get(Vector3.up, false);
  54. }
  55. protected GameObject GetDown()
  56. {
  57. if (NGUITools.GetActive(this.onDown))
  58. {
  59. return this.onDown;
  60. }
  61. if (this.constraint == UIKeyNavigation.Constraint.Horizontal || this.constraint == UIKeyNavigation.Constraint.Explicit)
  62. {
  63. return null;
  64. }
  65. return this.Get(Vector3.down, false);
  66. }
  67. protected GameObject Get(Vector3 myDir, bool horizontal)
  68. {
  69. Transform transform = base.transform;
  70. myDir = transform.TransformDirection(myDir);
  71. Vector3 center = UIKeyNavigation.GetCenter(base.gameObject);
  72. float num = float.MaxValue;
  73. GameObject result = null;
  74. for (int i = 0; i < UIKeyNavigation.list.size; i++)
  75. {
  76. UIKeyNavigation uikeyNavigation = UIKeyNavigation.list[i];
  77. if (!(uikeyNavigation == this))
  78. {
  79. UIButton component = uikeyNavigation.GetComponent<UIButton>();
  80. if (!(component != null) || component.isEnabled)
  81. {
  82. Vector3 direction = UIKeyNavigation.GetCenter(uikeyNavigation.gameObject) - center;
  83. float num2 = Vector3.Dot(myDir, direction.normalized);
  84. if (num2 >= 0.707f)
  85. {
  86. direction = transform.InverseTransformDirection(direction);
  87. if (horizontal)
  88. {
  89. direction.y *= 2f;
  90. }
  91. else
  92. {
  93. direction.x *= 2f;
  94. }
  95. float sqrMagnitude = direction.sqrMagnitude;
  96. if (sqrMagnitude <= num)
  97. {
  98. result = uikeyNavigation.gameObject;
  99. num = sqrMagnitude;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. return result;
  106. }
  107. protected static Vector3 GetCenter(GameObject go)
  108. {
  109. UIWidget component = go.GetComponent<UIWidget>();
  110. if (component != null)
  111. {
  112. Vector3[] worldCorners = component.worldCorners;
  113. return (worldCorners[0] + worldCorners[2]) * 0.5f;
  114. }
  115. return go.transform.position;
  116. }
  117. protected virtual void OnKey(KeyCode key)
  118. {
  119. if (!NGUITools.GetActive(this))
  120. {
  121. return;
  122. }
  123. GameObject gameObject = null;
  124. switch (key)
  125. {
  126. case KeyCode.UpArrow:
  127. gameObject = this.GetUp();
  128. break;
  129. case KeyCode.DownArrow:
  130. gameObject = this.GetDown();
  131. break;
  132. case KeyCode.RightArrow:
  133. gameObject = this.GetRight();
  134. break;
  135. case KeyCode.LeftArrow:
  136. gameObject = this.GetLeft();
  137. break;
  138. default:
  139. if (key == KeyCode.Tab)
  140. {
  141. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  142. {
  143. gameObject = this.GetLeft();
  144. if (gameObject == null)
  145. {
  146. gameObject = this.GetUp();
  147. }
  148. if (gameObject == null)
  149. {
  150. gameObject = this.GetDown();
  151. }
  152. if (gameObject == null)
  153. {
  154. gameObject = this.GetRight();
  155. }
  156. }
  157. else
  158. {
  159. gameObject = this.GetRight();
  160. if (gameObject == null)
  161. {
  162. gameObject = this.GetDown();
  163. }
  164. if (gameObject == null)
  165. {
  166. gameObject = this.GetUp();
  167. }
  168. if (gameObject == null)
  169. {
  170. gameObject = this.GetLeft();
  171. }
  172. }
  173. }
  174. break;
  175. }
  176. if (gameObject != null)
  177. {
  178. UICamera.selectedObject = gameObject;
  179. }
  180. }
  181. protected virtual void OnClick()
  182. {
  183. if (NGUITools.GetActive(this) && NGUITools.GetActive(this.onClick))
  184. {
  185. UICamera.selectedObject = this.onClick;
  186. }
  187. }
  188. public static BetterList<UIKeyNavigation> list = new BetterList<UIKeyNavigation>();
  189. public UIKeyNavigation.Constraint constraint;
  190. public GameObject onUp;
  191. public GameObject onDown;
  192. public GameObject onLeft;
  193. public GameObject onRight;
  194. public GameObject onClick;
  195. public bool startsSelected;
  196. public enum Constraint
  197. {
  198. None,
  199. Vertical,
  200. Horizontal,
  201. Explicit
  202. }
  203. }