| //-------------------------------------------------------- | 
| //    [Author]:           玩个游戏 | 
| //    [  Date ]:           Tuesday, October 31, 2017 | 
| //-------------------------------------------------------- | 
| using UnityEngine; | 
| using System.Collections; | 
| using UnityEngine.UI; | 
| using UnityEngine.Events; | 
| using UnityEngine.EventSystems; | 
|   | 
| using System; | 
|   | 
|   | 
| //关联游戏玩法功能按钮,如升星功能 | 
| public class FunctionButton : Button | 
| { | 
|     [SerializeField] int m_Order = 0; | 
|     public int order { | 
|         get { return m_Order; } | 
|         set { | 
|             m_Order = value; | 
|         } | 
|     } | 
|   | 
|     [SerializeField] int m_FunctionId = -1; | 
|     public int functionId { | 
|         get { return m_FunctionId; } | 
|         set { | 
|             m_FunctionId = value; | 
|             OnFunctionUnLockStateChange(); | 
|         } | 
|     } | 
|   | 
|     [SerializeField] TitleBtnState m_State = TitleBtnState.Normal; | 
|     public TitleBtnState state { | 
|         get { return m_State; } | 
|         set { | 
|             if (m_State != value) | 
|             { | 
|                 m_State = value; | 
|                 OnStateChange(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     [SerializeField] Button m_Button; | 
|     public Button button { | 
|         get { return m_Button; } | 
|         set { m_Button = value; } | 
|     } | 
|   | 
|     [SerializeField] ImageEx m_Icon; | 
|     public ImageEx icon { | 
|         get { return this.m_Icon; } | 
|         set { this.m_Icon = value; } | 
|     } | 
|   | 
|     [SerializeField] TextEx m_Title; | 
|     public TextEx title { | 
|         get { return this.m_Title; } | 
|         set { this.m_Title = value; } | 
|     } | 
|   | 
|     [SerializeField] RedpointBehaviour m_Redpoint; | 
|     public RedpointBehaviour redpoint { | 
|         get { return m_Redpoint; } | 
|         set { m_Redpoint = value; } | 
|     } | 
|   | 
|     [SerializeField] Shadow m_Shadow; | 
|     public Shadow shadow { | 
|         get { return this.m_Shadow; } | 
|         set { this.m_Shadow = value; } | 
|     } | 
|   | 
|     [SerializeField] Transform m_Locked; | 
|     public Transform locked { | 
|         get { return this.m_Locked; } | 
|         set { this.m_Locked = value; } | 
|     } | 
|   | 
|     [SerializeField] int m_Audio = 1; | 
|     public int clickAudio { | 
|         get { return this.m_Audio; } | 
|         set { this.m_Audio = value; } | 
|     } | 
|   | 
|     [SerializeField] bool m_UseDefaultConfig = true; | 
|     public bool useDefaultConfig { | 
|         get { return this.m_UseDefaultConfig; } | 
|         set { this.m_UseDefaultConfig = value; } | 
|     } | 
|   | 
|     [SerializeField] FunctionButtonConfig m_AlternativeConfig; | 
|     public FunctionButtonConfig alternativeConfig { get { return m_AlternativeConfig; } set { m_AlternativeConfig = value; } } | 
|   | 
|     [SerializeField] | 
|     FunctionButtonGroup m_Group; | 
|     public FunctionButtonGroup group { | 
|         get { return m_Group; } | 
|         set { | 
|             if (m_Group != null) | 
|             { | 
|                 m_Group.UnRegister(this); | 
|             } | 
|             m_Group = value; | 
|             if (m_Group != null) | 
|             { | 
|                 m_Group.Register(this); | 
|             } | 
|         } | 
|     } | 
|   | 
|     public event Action<string> OnPointClickLockFunc; | 
|     public event Action repeatClickFunc; | 
|   | 
|     protected override void Awake() | 
|     { | 
| #if UNITY_EDITOR | 
|         if (!Application.isPlaying) return; | 
| #endif | 
|         OnFunctionUnLockStateChange(); | 
|         FuncOpen.Instance.OnFuncStateChangeEvent += OnFunctionUnLockStateChange; | 
|     } | 
|   | 
|     protected override void OnEnable() | 
|     { | 
|         base.OnEnable(); | 
|   | 
|         if (group != null) | 
|         { | 
|             group.Register(this); | 
|         } | 
|   | 
|         OnStateChange(); | 
|     } | 
|   | 
|     protected override void OnDisable() | 
|     { | 
|         base.OnDisable(); | 
|   | 
|         if (group != null) | 
|         { | 
|             group.UnRegister(this); | 
|         } | 
|     } | 
|   | 
|     protected override void OnDestroy() | 
|     { | 
| #if UNITY_EDITOR | 
|         if (!Application.isPlaying) return; | 
| #endif | 
|         FuncOpen.Instance.OnFuncStateChangeEvent -= OnFunctionUnLockStateChange; | 
|     } | 
|   | 
|     bool invokeForce = false; | 
|   | 
|     public void Invoke(bool _force) | 
|     { | 
|         invokeForce = _force; | 
|         OnPointerClick(null); | 
|         invokeForce = false; | 
|     } | 
|   | 
|     public override void OnPointerClick(PointerEventData eventData) | 
|     { | 
|         if (state == TitleBtnState.Locked) | 
|         { | 
|             if (OnPointClickLockFunc != null) | 
|             { | 
|                 OnPointClickLockFunc(this.gameObject.name); | 
|             } | 
|             else | 
|             { | 
|                 FuncOpen.Instance.ProcessorFuncErrorTip(functionId); | 
|             } | 
|             return; | 
|         } | 
|   | 
|         if (!invokeForce && state == TitleBtnState.Click) | 
|         { | 
|             if (repeatClickFunc != null) | 
|             { | 
|                 repeatClickFunc(); | 
|             } | 
|             return; | 
|         } | 
|   | 
|         if (base.onClick != null) | 
|         { | 
|             base.onClick.Invoke(); | 
|             if (eventData != null) | 
|             { | 
|                 SoundPlayer.Instance.PlayUIAudio(clickAudio); | 
|             } | 
|         } | 
|   | 
|         state = TitleBtnState.Click; | 
|     } | 
|   | 
|     private void OnStateChange() | 
|     { | 
|         var config = useDefaultConfig ? FunctionButtonConfig.GetDefault() : m_AlternativeConfig; | 
|         if (Application.isPlaying) | 
|         { | 
|             icon.SetSprite(config.GetIconKey(state)); | 
|         } | 
|   | 
|         title.color = config.GetFontColor(state); | 
|         title.fontSize = config.GetFontSize(state); | 
|   | 
|         if (shadow != null) | 
|         { | 
|             shadow.enabled = state == TitleBtnState.Locked || state == TitleBtnState.Normal; | 
|         } | 
|   | 
|         if (locked != null) | 
|         { | 
|             locked.SetActive(state == TitleBtnState.Locked); | 
|         } | 
|   | 
|         if (group != null && state == TitleBtnState.Click) | 
|         { | 
|             group.NotifyToggleOn(this); | 
|         } | 
|     } | 
|   | 
|     private void OnFunctionUnLockStateChange(int _functionId) | 
|     { | 
|         if (m_FunctionId == _functionId) | 
|         { | 
|             OnFunctionUnLockStateChange(); | 
|         } | 
|     } | 
|   | 
|     private void OnFunctionUnLockStateChange() | 
|     { | 
|         if (m_FunctionId == 0) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         var isOpen = m_FunctionId == -1 || FuncOpen.Instance.IsFuncOpen(m_FunctionId); | 
|         state = isOpen ? state == TitleBtnState.Click ? TitleBtnState.Click : TitleBtnState.Normal : TitleBtnState.Locked; | 
|     } | 
|   | 
| #if UNITY_EDITOR | 
|     protected override void OnValidate() | 
|     { | 
|         base.OnValidate(); | 
|         OnStateChange(); | 
|     } | 
| #endif | 
|   | 
| } |