| //-------------------------------------------------------- | 
| //    [Author]:           玩个游戏 | 
| //    [  Date ]:           Tuesday, October 31, 2017 | 
| //-------------------------------------------------------- | 
| using UnityEngine; | 
| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine.UI; | 
| using UnityEngine.Events; | 
| using System; | 
|   | 
|     public class FunctionButtonGroup : MonoBehaviour | 
|     { | 
|         [SerializeField] ScrollRect m_ScrollRect; | 
|         [SerializeField] RectTransform m_Container; | 
|   | 
|         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<int> orders = new List<int>(); | 
|         Dictionary<int, FunctionButton> functionButtons = new Dictionary<int, FunctionButton>(); | 
|   | 
|         public void Register(FunctionButton button) | 
|         { | 
|             functionButtons[button.order] = button; | 
|             if (!orders.Contains(button.order)) | 
|             { | 
|                 orders.Add(button.order); | 
|                 orders.Sort(OrderCompare); | 
|             } | 
|   | 
|             if (m_ScrollRect != null) | 
|             { | 
|                 m_ScrollRect.horizontal = functionButtons.Count > 5; | 
|             } | 
|         } | 
|   | 
|         public void ChangeHorizontal(bool move) | 
|         { | 
|             if (m_ScrollRect != null) | 
|             { | 
|                 m_ScrollRect.horizontal = move; | 
|             } | 
|         } | 
|   | 
|         public void ChangeHorizontalPos(float index) | 
|         { | 
|             if (m_ScrollRect != null) | 
|             { | 
|                 m_ScrollRect.horizontalNormalizedPosition = index; | 
|             } | 
|         } | 
|         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); | 
|             } | 
|   | 
|             if (m_ScrollRect != null) | 
|             { | 
|                 m_ScrollRect.horizontal = functionButtons.Count > 5; | 
|             } | 
|         } | 
|   | 
|         public void NotifyToggleOn(FunctionButton button) | 
|         { | 
|   | 
|             if (button.state == TitleBtnState.Click) | 
|             { | 
|                 currentOrder = button.order; | 
|                 for (int i = 0; i < orders.Count; i++) | 
|                 { | 
|                     if (!functionButtons.ContainsKey(orders[i])) | 
|                     { | 
|                         Debug.LogFormat("<color=#ff0000ff>{0} {1}</color>", orders[i], button.name); | 
|                     } | 
|                     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 if (m_ScrollRect.vertical) | 
|                 { | 
|                     m_ScrollRect.verticalNormalizedPosition = (float)index / orders.Count; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         private int OrderCompare(int a, int b) | 
|         { | 
|             return a < b ? -1 : 1; | 
|         } | 
|   | 
|         public FunctionButton AddFunction(string pattern, int order, int functionId, string title, int redpointId) | 
|         { | 
|             if (m_Container == null) | 
|             { | 
|                 return null; | 
|             } | 
|   | 
|             var instance = UIUtility.CreateWidget(pattern, string.Concat("Function_", order)); | 
|             instance.transform.SetParentEx(this.m_Container, Vector3.zero, Quaternion.identity, Vector3.one); | 
|   | 
|             var functionButton = instance.GetComponent<FunctionButton>(); | 
|             functionButton.functionId = functionId; | 
|             functionButton.order = order; | 
|             functionButton.title.text = title; | 
|   | 
|             functionButton.group = this; | 
|             functionButton.redpoint.redpointId = redpointId; | 
|             functionButtons[order] = functionButton; | 
|   | 
|             functionButton.SetActive(functionButton.functionId != -1); | 
|             return functionButton; | 
|         } | 
|   | 
|         public void SetFunctionListener(int order, Action callBack) | 
|         { | 
|             if (functionButtons.ContainsKey(order)) | 
|             { | 
|                 functionButtons[order].SetListener(() => callBack?.Invoke()); | 
|             } | 
|         } | 
|   | 
|         public void SetFunctionButtonActive(int order, bool isShow) | 
|         { | 
|             if (functionButtons.ContainsKey(order)) | 
|             { | 
|                 functionButtons[order].SetActive(isShow); | 
|             } | 
|         } | 
|   | 
|         public RedPointState GetFunctionButtonRedPointState(int order) | 
|         { | 
|             if (functionButtons.ContainsKey(order)) | 
|             { | 
|                 return RedpointCenter.Instance.GetRedpointState(functionButtons[order].redpoint.redpointId); | 
|             } | 
|   | 
|             return RedPointState.None; | 
|         } | 
|   | 
|         public FunctionButton GetFunctionBtn(int order) | 
|         { | 
|             if (functionButtons.ContainsKey(order)) | 
|             { | 
|                 return functionButtons[order]; | 
|             } | 
|             return null; | 
|         } | 
|     } |