//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Tuesday, October 31, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; namespace Snxxz.UI { public class FunctionButtonGroup : MonoBehaviour { [SerializeField] ScrollRect m_ScrollRect; public int unLockedCount { get { var count = 0; foreach (var button in functionButtons.Values) { if (button.state != TitleBtnState.Locked) { count++; } } return count; } } int currentOrder = 0; List orders = new List(); Dictionary functionButtons = new Dictionary(); public void Register(FunctionButton button) { functionButtons[button.order] = button; if (!orders.Contains(button.order)) { orders.Add(button.order); orders.Sort(OrderCompare); } } public void UnRegister(FunctionButton button) { if (functionButtons.ContainsKey(button.order)) { functionButtons.Remove(button.order); } if (orders.Contains(button.order)) { orders.Remove(button.order); orders.Sort(OrderCompare); } } public void NotifyToggleOn(FunctionButton button) { if (button.state == TitleBtnState.Click) { currentOrder = button.order; for (int i = 0; i < orders.Count; i++) { var functionButton = functionButtons[orders[i]]; if (functionButton != button && functionButton.state != TitleBtnState.Locked) { functionButton.state = TitleBtnState.Normal; } } } } public void TriggerByOrder(int targetOrder) { for (int i = 0; i < orders.Count; i++) { var order = orders[i]; if (order == targetOrder) { functionButtons[order].Invoke(true); break; } } } public void TriggerLast() { var index = orders.IndexOf(currentOrder); if (index < 0) { return; } var loopTimes = 0; while (loopTimes < 2) { index--; if (index < 0) { index = orders.Count - 1; loopTimes++; } var next = orders[index]; if (functionButtons[next].state != TitleBtnState.Locked) { functionButtons[next].Invoke(false); break; } } } public void TriggerNext() { var index = orders.IndexOf(currentOrder); if (index < 0) { return; } var loopTimes = 0; while (loopTimes < 2) { index++; if (index > orders.Count - 1) { index = 0; loopTimes++; } var next = orders[index]; if (functionButtons[next].state != TitleBtnState.Locked) { functionButtons[next].Invoke(false); break; } } } public bool IsFirst() { return orders.Count > 0 && currentOrder == orders[0]; } public bool IsLast() { return orders.Count > 0 && currentOrder == orders[orders.Count - 1]; } public void GotoOrder(int order) { if (m_ScrollRect != null) { var index = orders.IndexOf(order); if (m_ScrollRect.horizontal) { m_ScrollRect.horizontalNormalizedPosition = (float)index / orders.Count; } else { m_ScrollRect.verticalNormalizedPosition = (float)index / orders.Count; } } } private int OrderCompare(int a, int b) { return a < b ? -1 : 1; } } }