using System; using System.Collections.Generic; using UnityEngine; public class PassiveButtonManager : DestroyableSingleton { public List Buttons = new List(); private List FocusHolders = new List(); private PassiveButton currentOver; public Controller Controller = new Controller(); private PassiveButton currentDown; private Collider2D[] results = new Collider2D[40]; private class DepthComparer : IComparer { public static readonly PassiveButtonManager.DepthComparer Instance = new PassiveButtonManager.DepthComparer(); public int Compare(PassiveButton x, PassiveButton y) { if (x == null) { return 1; } if (y == null) { return -1; } return x.transform.position.z.CompareTo(y.transform.position.z); } } public void RegisterOne(PassiveButton button) { this.Buttons.Add(button); } public void RemoveOne(PassiveButton passiveButton) { this.Buttons.Remove(passiveButton); } public void RegisterOne(IFocusHolder focusHolder) { this.FocusHolders.Add(focusHolder); } public void RemoveOne(IFocusHolder focusHolder) { this.FocusHolders.Remove(focusHolder); } public void Update() { this.Controller.Update(); for (int i = 1; i < this.Buttons.Count; i++) { if (PassiveButtonManager.DepthComparer.Instance.Compare(this.Buttons[i - 1], this.Buttons[i]) > 0) { this.Buttons.Sort(PassiveButtonManager.DepthComparer.Instance); break; } } Vector2 position = this.Controller.Touches[0].Position; int num = Physics2D.OverlapPointNonAlloc(position, this.results); bool flag = false; for (int j = 0; j < this.Buttons.Count; j++) { PassiveButton passiveButton = this.Buttons[j]; if (!passiveButton) { this.Buttons.RemoveAt(j); j--; } else if (passiveButton.isActiveAndEnabled) { bool flag2 = false; for (int k = 0; k < num; k++) { if (this.results[k].gameObject == passiveButton.gameObject) { flag2 = true; break; } } if (flag2) { flag = true; if (passiveButton != this.currentOver) { if (this.currentOver) { this.currentOver.OnMouseOut.Invoke(); } this.currentOver = passiveButton; this.currentDown = null; this.currentOver.OnMouseOver.Invoke(); break; } break; } } } if (!flag && this.currentOver) { this.currentOver.OnMouseOut.Invoke(); this.currentOver = null; this.currentDown = null; } if (this.Controller.AnyTouchDown) { if (this.currentOver) { this.currentDown = this.currentOver; if (this.currentOver.OnDown) { this.currentOver.DoClick(); } } this.HandleFocus(position); return; } if (this.Controller.AnyTouchUp && this.currentDown) { if (this.currentDown.OnUp) { this.currentDown.DoClick(); } this.currentDown = null; } } private void CheckForDown() { Vector2 touch = this.GetTouch(true); for (int i = 0; i < this.Buttons.Count; i++) { PassiveButton passiveButton = this.Buttons[i]; if (!passiveButton) { this.Buttons.RemoveAt(i); i--; } else if (passiveButton.isActiveAndEnabled) { for (int j = 0; j < passiveButton.Colliders.Length; j++) { Collider2D collider2D = passiveButton.Colliders[j]; if (collider2D && collider2D.OverlapPoint(touch)) { this.currentDown = passiveButton; if (passiveButton.OnDown) { passiveButton.DoClick(); } return; } } } } this.HandleFocus(touch); } private void HandleFocus(Vector2 pt) { bool flag = false; for (int i = 0; i < this.FocusHolders.Count; i++) { IFocusHolder focusHolder = this.FocusHolders[i]; if (!(focusHolder as MonoBehaviour)) { this.FocusHolders.RemoveAt(i); i--; } else if (focusHolder.CheckCollision(pt)) { flag = true; focusHolder.GiveFocus(); for (int j = 0; j < this.FocusHolders.Count; j++) { if (j != i) { this.FocusHolders[j].LoseFocus(); } } break; } } if (!flag) { for (int k = 0; k < this.FocusHolders.Count; k++) { this.FocusHolders[k].LoseFocus(); } } } private void HandleMouseOut(PassiveButton button) { if (this.currentOver == button) { button.OnMouseOut.Invoke(); this.currentOver = null; } } private void CheckForUp() { if (!this.currentDown) { return; } PassiveButton passiveButton = this.currentDown; this.currentDown = null; if (!passiveButton.OnUp) { return; } Vector2 touch = this.GetTouch(false); for (int i = 0; i < passiveButton.Colliders.Length; i++) { if (passiveButton.Colliders[i].OverlapPoint(touch)) { if (passiveButton.OnUp) { passiveButton.DoClick(); } return; } } } private Vector2 GetTouch(bool downOrUp) { if (downOrUp) { if (this.Controller.Touches[0].TouchStart) { return this.Controller.Touches[0].Position; } return this.Controller.Touches[1].Position; } else { if (this.Controller.Touches[0].TouchEnd) { return this.Controller.Touches[0].Position; } return this.Controller.Touches[1].Position; } } }