UIKeyNavigation2.cs 6.9 KB

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