123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- using System.Collections.Generic;
- using System;
- using UnityEngine.EventSystems;
- using UnityEngine;
- public class ClickEvent : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler
- {
- private List<IClickListener> listenerList = new List<IClickListener>();
- private Dictionary<IClickListener, Action> callBackDict = new Dictionary<IClickListener, Action>();
- public void OnPointerDown(PointerEventData eventData)
- {
- for (int i = listenerList.Count - 1; i >= 0; i--)
- {
- listenerList[i]?.OnPointerDown(eventData);
- }
- }
- public void OnPointerUp(PointerEventData eventData)
- {
- for (int i = listenerList.Count - 1; i >= 0; i--)
- {
- listenerList[i]?.OnPointerUp(eventData);
- }
- }
- public void OnPointerClick(PointerEventData eventData)
- {
- for (int i = listenerList.Count - 1; i >= 0; i--)
- {
- listenerList[i]?.OnPointerClick(eventData);
- }
- }
- private void Update()
- {
- for (int i = listenerList.Count - 1; i >= 0; i--)
- {
- listenerList[i]?.Update();
- }
- }
- public T Add<T>() where T : ClickListener, new()
- {
- T t = Get<T>();
- if (t == null)
- {
- t = new T();
- (t as IClickListener).OnCallBack((success) => OnReceive(success, t));
- if (listenerList.Count == 0 || t is ClickButton) listenerList.Add(t);
- else listenerList.Insert(listenerList.Count - 1, t);
- }
- return t;
- }
- public void Remove<T>() where T : ClickListener
- {
- if (!Contains<T>()) return;
- T t = Get<T>();
- if (callBackDict.ContainsKey(t)) callBackDict.Remove(t);
- listenerList.Remove(t);
- }
- public void RemoveAll()
- {
- listenerList.Clear();
- callBackDict.Clear();
- }
- public T Get<T>() where T : ClickListener
- {
- if (listenerList.Count == 0) return default;
- IClickListener listener = default;
- for (int i = listenerList.Count - 1; i >= 0; i--)
- {
- listener = listenerList[i];
- if (listener is T) return (T)listener;
- }
- return default;
- }
- public bool Contains<T>() where T : ClickListener
- {
- if (listenerList.Count == 0) return false;
- IClickListener listener = default;
- for (int i = listenerList.Count - 1; i >= 0; i--)
- {
- listener = listenerList[i];
- if (listener is T) return true;
- }
- return false;
- }
- public bool ContainsCallBack<T>() where T : ClickListener
- {
- T t = Get<T>();
- if (t == null) return false;
- if (callBackDict.ContainsKey(t))
- {
- if (callBackDict[t] == null)
- {
- callBackDict.Remove(t);
- return false;
- }
- return true;
- }
- else return false;
- }
- public Action GetCallBack<T>() where T : ClickListener
- {
- T t = Get<T>();
- if (t == null) return null;
- if (callBackDict.ContainsKey(t))
- {
- Action callBack = callBackDict[t];
- if (callBack == null)
- {
- callBackDict.Remove(t);
- return null;
- }
- return callBack;
- }
- else return null;
- }
- public bool HasRuning()
- {
- bool isRuning = false;
- foreach (var lis in listenerList)
- {
- if (lis.isRuning)
- {
- isRuning = true;
- break;
- }
- }
- return isRuning;
- }
- public T OnRegister<T>(Action callBack) where T : ClickListener, new()
- {
- T t = Add<T>();
- if (callBackDict.ContainsKey(t)) callBackDict[t] = callBack;
- else callBackDict.Add(t, callBack);
- return t;
- }
- private Action clickCallBack = null;
- void OnReceive(bool success, IClickListener listener)
- {
- if (success)
- {
- if (listener is ClickButton)
- {
- Action cbk = GetCallBack<ClickButton>();
- if (cbk == null) return;
- if (HasRuning())
- {
- clickCallBack = cbk;
- }
- else
- {
- clickCallBack = null;
- ResetOther();
- cbk.Invoke();
- }
- }
- else
- {
- clickCallBack = null;
- ResetOther();
- if (callBackDict.ContainsKey(listener)) callBackDict[listener]?.Invoke();
- }
- }
- else
- {
- if (!HasRuning())
- {
- clickCallBack?.Invoke();
- clickCallBack = null;
- }
- }
- void ResetOther()
- {
- foreach (var btn in listenerList)
- {
- if (btn != listener) btn?.Reset();
- }
- }
- }
- #if UNITY_EDITOR
- [UnityEditor.CustomEditor(typeof(ClickEvent))]
- class ClickEventInspector : UnityEditor.Editor
- {
- private ClickEvent clickEvent;
- private void OnEnable()
- {
- clickEvent = target as ClickEvent;
- }
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- GUILayout.Label("ClickListeners: ");
- IClickListener listener;
- for (int i = clickEvent.listenerList.Count - 1; i >= 0; i--)
- {
- listener = clickEvent.listenerList[i];
- GUILayout.Label(" " + listener);
- }
- }
- }
- #endif
- private interface IClickListener
- {
- bool isRuning { get; set; }
- void OnPointerDown(PointerEventData eventData);
- void OnPointerUp(PointerEventData eventData);
- void OnPointerClick(PointerEventData eventData);
- void Update();
- void OnCallBack(Action<bool> callBack);
- void Reset();
- void Dispose();
- }
- public abstract class ClickListener : IClickListener
- {
- bool IClickListener.isRuning
- {
- get => isRuning;
- set { isRuning = value; }
- }
- protected bool isRuning = false;
- protected Action<bool> callBack { get; private set; }
- void IClickListener.OnPointerDown(PointerEventData eventData) => OnPointerDown(eventData);
- void IClickListener.OnPointerUp(PointerEventData eventData) => OnPointerUp(eventData);
- void IClickListener.OnPointerClick(PointerEventData eventData) => OnPointerClick(eventData);
- void IClickListener.Update() => Update();
- void IClickListener.Reset() => Reset();
- void IClickListener.Dispose() => Dispose();
- void IClickListener.OnCallBack(Action<bool> callBack)
- {
- this.callBack = callBack;
- }
- protected virtual void OnPointerDown(PointerEventData eventData)
- {
- }
- protected virtual void OnPointerUp(PointerEventData eventData)
- {
- }
- protected virtual void OnPointerClick(PointerEventData eventData)
- {
- }
- protected virtual void Update()
- {
- }
- protected abstract void Reset();
- protected virtual void Dispose()
- {
- callBack = null;
- }
- }
- }
|