123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Key Navigation")]
- public class UIKeyNavigation : MonoBehaviour
- {
- protected virtual void OnEnable()
- {
- UIKeyNavigation.list.Add(this);
- if (this.startsSelected && (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject)))
- {
- UICamera.currentScheme = UICamera.ControlScheme.Controller;
- UICamera.selectedObject = base.gameObject;
- }
- }
- protected virtual void OnDisable()
- {
- UIKeyNavigation.list.Remove(this);
- }
- protected GameObject GetLeft()
- {
- if (NGUITools.GetActive(this.onLeft))
- {
- return this.onLeft;
- }
- if (this.constraint == UIKeyNavigation.Constraint.Vertical || this.constraint == UIKeyNavigation.Constraint.Explicit)
- {
- return null;
- }
- return this.Get(Vector3.left, true);
- }
- private GameObject GetRight()
- {
- if (NGUITools.GetActive(this.onRight))
- {
- return this.onRight;
- }
- if (this.constraint == UIKeyNavigation.Constraint.Vertical || this.constraint == UIKeyNavigation.Constraint.Explicit)
- {
- return null;
- }
- return this.Get(Vector3.right, true);
- }
- protected GameObject GetUp()
- {
- if (NGUITools.GetActive(this.onUp))
- {
- return this.onUp;
- }
- if (this.constraint == UIKeyNavigation.Constraint.Horizontal || this.constraint == UIKeyNavigation.Constraint.Explicit)
- {
- return null;
- }
- return this.Get(Vector3.up, false);
- }
- protected GameObject GetDown()
- {
- if (NGUITools.GetActive(this.onDown))
- {
- return this.onDown;
- }
- if (this.constraint == UIKeyNavigation.Constraint.Horizontal || this.constraint == UIKeyNavigation.Constraint.Explicit)
- {
- return null;
- }
- return this.Get(Vector3.down, false);
- }
- protected GameObject Get(Vector3 myDir, bool horizontal)
- {
- Transform transform = base.transform;
- myDir = transform.TransformDirection(myDir);
- Vector3 center = UIKeyNavigation.GetCenter(base.gameObject);
- float num = float.MaxValue;
- GameObject result = null;
- for (int i = 0; i < UIKeyNavigation.list.size; i++)
- {
- UIKeyNavigation uikeyNavigation = UIKeyNavigation.list[i];
- if (!(uikeyNavigation == this))
- {
- UIButton component = uikeyNavigation.GetComponent<UIButton>();
- if (!(component != null) || component.isEnabled)
- {
- Vector3 direction = UIKeyNavigation.GetCenter(uikeyNavigation.gameObject) - center;
- float num2 = Vector3.Dot(myDir, direction.normalized);
- if (num2 >= 0.707f)
- {
- direction = transform.InverseTransformDirection(direction);
- if (horizontal)
- {
- direction.y *= 2f;
- }
- else
- {
- direction.x *= 2f;
- }
- float sqrMagnitude = direction.sqrMagnitude;
- if (sqrMagnitude <= num)
- {
- result = uikeyNavigation.gameObject;
- num = sqrMagnitude;
- }
- }
- }
- }
- }
- return result;
- }
- protected static Vector3 GetCenter(GameObject go)
- {
- UIWidget component = go.GetComponent<UIWidget>();
- if (component != null)
- {
- Vector3[] worldCorners = component.worldCorners;
- return (worldCorners[0] + worldCorners[2]) * 0.5f;
- }
- return go.transform.position;
- }
- protected virtual void OnKey(KeyCode key)
- {
- if (!NGUITools.GetActive(this))
- {
- return;
- }
- GameObject gameObject = null;
- switch (key)
- {
- case KeyCode.UpArrow:
- gameObject = this.GetUp();
- break;
- case KeyCode.DownArrow:
- gameObject = this.GetDown();
- break;
- case KeyCode.RightArrow:
- gameObject = this.GetRight();
- break;
- case KeyCode.LeftArrow:
- gameObject = this.GetLeft();
- break;
- default:
- if (key == KeyCode.Tab)
- {
- if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
- {
- gameObject = this.GetLeft();
- if (gameObject == null)
- {
- gameObject = this.GetUp();
- }
- if (gameObject == null)
- {
- gameObject = this.GetDown();
- }
- if (gameObject == null)
- {
- gameObject = this.GetRight();
- }
- }
- else
- {
- gameObject = this.GetRight();
- if (gameObject == null)
- {
- gameObject = this.GetDown();
- }
- if (gameObject == null)
- {
- gameObject = this.GetUp();
- }
- if (gameObject == null)
- {
- gameObject = this.GetLeft();
- }
- }
- }
- break;
- }
- if (gameObject != null)
- {
- UICamera.selectedObject = gameObject;
- }
- }
- protected virtual void OnClick()
- {
- if (NGUITools.GetActive(this) && NGUITools.GetActive(this.onClick))
- {
- UICamera.selectedObject = this.onClick;
- }
- }
- public static BetterList<UIKeyNavigation> list = new BetterList<UIKeyNavigation>();
- public UIKeyNavigation.Constraint constraint;
- public GameObject onUp;
- public GameObject onDown;
- public GameObject onLeft;
- public GameObject onRight;
- public GameObject onClick;
- public bool startsSelected;
- public enum Constraint
- {
- None,
- Vertical,
- Horizontal,
- Explicit
- }
- }
|