UIKeyNavigation5.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Key Navigation4")]
  4. public class UIKeyNavigation5 : MonoBehaviour
  5. {
  6. protected virtual void OnEnable()
  7. {
  8. UIKeyNavigation5.listSliderItem.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. UIKeyNavigation5.listSliderItem.Remove(this);
  18. }
  19. protected GameObject GetLeft()
  20. {
  21. if (NGUITools.GetActive(this.onLeft))
  22. {
  23. return this.onLeft;
  24. }
  25. if (this.constraint == UIKeyNavigation5.Constraint.Vertical || this.constraint == UIKeyNavigation5.Constraint.Explicit)
  26. {
  27. return null;
  28. }
  29. return this.GetLeft(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 == UIKeyNavigation5.Constraint.Vertical || this.constraint == UIKeyNavigation5.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 == UIKeyNavigation5.Constraint.Horizontal || this.constraint == UIKeyNavigation5.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 == UIKeyNavigation5.Constraint.Horizontal || this.constraint == UIKeyNavigation5.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 = UIKeyNavigation5.GetCenter(base.gameObject);
  72. float num = float.MaxValue;
  73. GameObject result = null;
  74. for (int i = 0; i < UIKeyNavigation5.listSliderItem.size; i++)
  75. {
  76. UIKeyNavigation5 uikeyNavigation = UIKeyNavigation5.listSliderItem[i];
  77. if (!(uikeyNavigation == this))
  78. {
  79. UIButton component = uikeyNavigation.GetComponent<UIButton>();
  80. if (!(component != null) || component.isEnabled)
  81. {
  82. Vector3 direction = UIKeyNavigation5.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 GameObject GetLeft(Vector3 myDir, bool horizontal)
  108. {
  109. Transform transform = base.transform;
  110. myDir = transform.TransformDirection(myDir);
  111. Vector3 center = UIKeyNavigation5.GetCenter(base.gameObject);
  112. float num = float.MaxValue;
  113. GameObject gameObject = this.Get(myDir, horizontal);
  114. if (gameObject == null)
  115. {
  116. for (int i = 0; i < UIKeyNavigation2.listCate.size; i++)
  117. {
  118. UIKeyNavigation2 uikeyNavigation = UIKeyNavigation2.listCate[i];
  119. if (!(uikeyNavigation == this))
  120. {
  121. UIButton component = uikeyNavigation.GetComponent<UIButton>();
  122. if (!(component != null) || component.isEnabled)
  123. {
  124. Vector3 direction = UIKeyNavigation5.GetCenter(uikeyNavigation.gameObject) - center;
  125. float num2 = Vector3.Dot(myDir, direction.normalized);
  126. if (num2 >= 0.707f)
  127. {
  128. direction = transform.InverseTransformDirection(direction);
  129. if (horizontal)
  130. {
  131. direction.y *= 2f;
  132. }
  133. else
  134. {
  135. direction.x *= 2f;
  136. }
  137. float sqrMagnitude = direction.sqrMagnitude;
  138. if (sqrMagnitude <= num)
  139. {
  140. gameObject = uikeyNavigation.gameObject;
  141. num = sqrMagnitude;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. return gameObject;
  149. }
  150. protected static Vector3 GetCenter(GameObject go)
  151. {
  152. UIWidget component = go.GetComponent<UIWidget>();
  153. if (component != null)
  154. {
  155. Vector3[] worldCorners = component.worldCorners;
  156. return (worldCorners[0] + worldCorners[2]) * 0.5f;
  157. }
  158. return go.transform.position;
  159. }
  160. protected virtual void OnKey(KeyCode key)
  161. {
  162. if (!NGUITools.GetActive(this))
  163. {
  164. return;
  165. }
  166. GameObject gameObject = null;
  167. if (key != KeyCode.UpArrow)
  168. {
  169. if (key != KeyCode.DownArrow)
  170. {
  171. if (key != KeyCode.Joystick1Button0)
  172. {
  173. if (key != KeyCode.Joystick1Button1)
  174. {
  175. if (key == KeyCode.Tab)
  176. {
  177. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  178. {
  179. gameObject = this.GetLeft();
  180. if (gameObject == null)
  181. {
  182. gameObject = this.GetUp();
  183. }
  184. if (gameObject == null)
  185. {
  186. gameObject = this.GetDown();
  187. }
  188. if (gameObject == null)
  189. {
  190. gameObject = this.GetRight();
  191. }
  192. }
  193. else
  194. {
  195. gameObject = this.GetRight();
  196. if (gameObject == null)
  197. {
  198. gameObject = this.GetDown();
  199. }
  200. if (gameObject == null)
  201. {
  202. gameObject = this.GetUp();
  203. }
  204. if (gameObject == null)
  205. {
  206. gameObject = this.GetLeft();
  207. }
  208. }
  209. }
  210. }
  211. else
  212. {
  213. gameObject = this.GetLeft();
  214. }
  215. }
  216. else
  217. {
  218. gameObject = this.GetLeft();
  219. }
  220. }
  221. else
  222. {
  223. gameObject = this.GetDown();
  224. }
  225. }
  226. else
  227. {
  228. gameObject = this.GetUp();
  229. }
  230. if (gameObject != null)
  231. {
  232. UICamera.selectedObject = gameObject;
  233. }
  234. }
  235. protected virtual void OnClick()
  236. {
  237. if (NGUITools.GetActive(this) && NGUITools.GetActive(this.onClick))
  238. {
  239. UICamera.selectedObject = this.onClick;
  240. }
  241. }
  242. public static BetterList<UIKeyNavigation5> listSliderItem = new BetterList<UIKeyNavigation5>();
  243. public UIKeyNavigation5.Constraint constraint;
  244. public GameObject onUp;
  245. public GameObject onDown;
  246. public GameObject onLeft;
  247. public GameObject onRight;
  248. public GameObject onClick;
  249. public bool startsSelected;
  250. public enum Constraint
  251. {
  252. None,
  253. Vertical,
  254. Horizontal,
  255. Explicit
  256. }
  257. }