UIKeyNavigation3.cs 7.1 KB

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