using System; using UnityEngine; [AddComponentMenu("NGUI/Interaction/Key Navigation4")] public class UIKeyNavigation4 : MonoBehaviour { protected virtual void OnEnable() { UIKeyNavigation4.listMenuItem.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() { UIKeyNavigation4.listMenuItem.Remove(this); } protected GameObject GetLeft() { if (NGUITools.GetActive(this.onLeft)) { return this.onLeft; } if (this.constraint == UIKeyNavigation4.Constraint.Vertical || this.constraint == UIKeyNavigation4.Constraint.Explicit) { return null; } return this.GetLeft(Vector3.left, true); } private GameObject GetRight() { if (NGUITools.GetActive(this.onRight)) { return this.onRight; } if (this.constraint == UIKeyNavigation4.Constraint.Vertical || this.constraint == UIKeyNavigation4.Constraint.Explicit) { return null; } return this.Get(Vector3.right, true); } protected GameObject GetUp() { if (NGUITools.GetActive(this.onUp)) { return this.onUp; } if (this.constraint == UIKeyNavigation4.Constraint.Horizontal || this.constraint == UIKeyNavigation4.Constraint.Explicit) { return null; } return this.Get(Vector3.up, false); } protected GameObject GetDown() { if (NGUITools.GetActive(this.onDown)) { return this.onDown; } if (this.constraint == UIKeyNavigation4.Constraint.Horizontal || this.constraint == UIKeyNavigation4.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 = UIKeyNavigation4.GetCenter(base.gameObject); float num = float.MaxValue; GameObject result = null; for (int i = 0; i < UIKeyNavigation4.listMenuItem.size; i++) { UIKeyNavigation4 uikeyNavigation = UIKeyNavigation4.listMenuItem[i]; if (!(uikeyNavigation == this)) { UIButton component = uikeyNavigation.GetComponent(); if (!(component != null) || component.isEnabled) { Vector3 direction = UIKeyNavigation4.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 GameObject GetLeft(Vector3 myDir, bool horizontal) { Transform transform = base.transform; myDir = transform.TransformDirection(myDir); Vector3 center = UIKeyNavigation4.GetCenter(base.gameObject); float num = float.MaxValue; GameObject gameObject = this.Get(myDir, horizontal); if (gameObject == null) { for (int i = 0; i < UIKeyNavigation3.listPartsType.size; i++) { UIKeyNavigation3 uikeyNavigation = UIKeyNavigation3.listPartsType[i]; if (!(uikeyNavigation == this)) { UIButton component = uikeyNavigation.GetComponent(); if (!(component != null) || component.isEnabled) { Vector3 direction = UIKeyNavigation4.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) { gameObject = uikeyNavigation.gameObject; num = sqrMagnitude; } } } } } } return gameObject; } protected static Vector3 GetCenter(GameObject go) { UIWidget component = go.GetComponent(); 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 listMenuItem = new BetterList(); public UIKeyNavigation4.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 } }