diff options
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler')
14 files changed, 1076 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventHandle.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventHandle.cs new file mode 100644 index 0000000..977514f --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventHandle.cs @@ -0,0 +1,11 @@ +using System; + +namespace UnityEngine.EventSystems +{ + [Flags] + public enum EventHandle + { + Unused = 0, + Used = 1 + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventHandle.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventHandle.cs.meta new file mode 100644 index 0000000..1e2c52f --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventHandle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0c5751e0f52a2d9459e76555a888ad06 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventInterfaces.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventInterfaces.cs new file mode 100644 index 0000000..b808e6c --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventInterfaces.cs @@ -0,0 +1,91 @@ +namespace UnityEngine.EventSystems +{ + public interface IEventSystemHandler + { + } + + public interface IPointerEnterHandler : IEventSystemHandler + { + void OnPointerEnter(PointerEventData eventData); + } + + public interface IPointerExitHandler : IEventSystemHandler + { + void OnPointerExit(PointerEventData eventData); + } + + public interface IPointerDownHandler : IEventSystemHandler + { + void OnPointerDown(PointerEventData eventData); + } + + public interface IPointerUpHandler : IEventSystemHandler + { + void OnPointerUp(PointerEventData eventData); + } + + public interface IPointerClickHandler : IEventSystemHandler + { + void OnPointerClick(PointerEventData eventData); + } + + public interface IBeginDragHandler : IEventSystemHandler + { + void OnBeginDrag(PointerEventData eventData); + } + + public interface IInitializePotentialDragHandler : IEventSystemHandler + { + void OnInitializePotentialDrag(PointerEventData eventData); + } + + public interface IDragHandler : IEventSystemHandler + { + void OnDrag(PointerEventData eventData); + } + + public interface IEndDragHandler : IEventSystemHandler + { + void OnEndDrag(PointerEventData eventData); + } + + public interface IDropHandler : IEventSystemHandler + { + void OnDrop(PointerEventData eventData); + } + + public interface IScrollHandler : IEventSystemHandler + { + void OnScroll(PointerEventData eventData); + } + + public interface IUpdateSelectedHandler : IEventSystemHandler + { + void OnUpdateSelected(BaseEventData eventData); + } + + public interface ISelectHandler : IEventSystemHandler + { + void OnSelect(BaseEventData eventData); + } + + public interface IDeselectHandler : IEventSystemHandler + { + void OnDeselect(BaseEventData eventData); + } + + public interface IMoveHandler : IEventSystemHandler + { + void OnMove(AxisEventData eventData); + } + + public interface ISubmitHandler : IEventSystemHandler + { + void OnSubmit(BaseEventData eventData); + } + + public interface ICancelHandler : IEventSystemHandler + { + void OnCancel(BaseEventData eventData); + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventInterfaces.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventInterfaces.cs.meta new file mode 100644 index 0000000..2a369ae --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventInterfaces.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7bb73ff56f2f85b4ca62f8cb5ea59840 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventSystem.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventSystem.cs new file mode 100644 index 0000000..1440547 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventSystem.cs @@ -0,0 +1,328 @@ +using System; +using System.Collections.Generic; +using System.Text; +using UnityEngine; +using UnityEngine.Serialization; + +namespace UnityEngine.EventSystems +{ + [AddComponentMenu("Event/Event System")] + public class EventSystem : UIBehaviour + { + // 获得当前EventSystem挂着的BaseInputModule的派生类,一般来说只会挂一个StandableInputModule + private List<BaseInputModule> m_SystemInputModules = new List<BaseInputModule>(); + + private BaseInputModule m_CurrentInputModule; + + public static EventSystem current { get; set; } + + [SerializeField] + [FormerlySerializedAs("m_Selected")] + private GameObject m_FirstSelected; + + [SerializeField] + private bool m_sendNavigationEvents = true; // Should the EventSystem allow navigation events (move / submit / cancel). + + public bool sendNavigationEvents + { + get { return m_sendNavigationEvents; } + set { m_sendNavigationEvents = value; } + } + + [SerializeField] + private int m_DragThreshold = 5; + public int pixelDragThreshold + { + get { return m_DragThreshold; } + set { m_DragThreshold = value; } + } + + private GameObject m_CurrentSelected; + + public BaseInputModule currentInputModule + { + get { return m_CurrentInputModule; } + } + + /// <summary> + /// Only one object can be selected at a time. Think: controller-selected button. + /// </summary> + public GameObject firstSelectedGameObject + { + get { return m_FirstSelected; } + set { m_FirstSelected = value; } + } + + public GameObject currentSelectedGameObject + { + get { return m_CurrentSelected; } + } + + [Obsolete("lastSelectedGameObject is no longer supported")] + public GameObject lastSelectedGameObject + { + get { return null; } + } + + private bool m_HasFocus = true; + + public bool isFocused + { + get { return m_HasFocus; } + } + + protected EventSystem() + {} + + //c 把挂着的input modules加入m_SystemInputModules + public void UpdateModules() + { + // 拿到当前EventSystem下面挂着的inputmodule,通常来说只会挂一个standaloneInputModule + GetComponents(m_SystemInputModules); + for (int i = m_SystemInputModules.Count - 1; i >= 0; i--) + { + if (m_SystemInputModules[i] && m_SystemInputModules[i].IsActive()) + continue; + m_SystemInputModules.RemoveAt(i); + } + } + + private bool m_SelectionGuard; + public bool alreadySelecting + { + get { return m_SelectionGuard; } + } + + public void SetSelectedGameObject(GameObject selected, BaseEventData pointer) + { + if (m_SelectionGuard) + { + Debug.LogError("Attempting to select " + selected + "while already selecting an object."); + return; + } + + m_SelectionGuard = true; + if (selected == m_CurrentSelected) + { + m_SelectionGuard = false; + return; + } + + // Debug.Log("Selection: new (" + selected + ") old (" + m_CurrentSelected + ")"); + ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.deselectHandler); + m_CurrentSelected = selected; + ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.selectHandler); + m_SelectionGuard = false; + } + + private BaseEventData m_DummyData; + private BaseEventData baseEventDataCache + { + get + { + if (m_DummyData == null) + m_DummyData = new BaseEventData(this); + + return m_DummyData; + } + } + + public void SetSelectedGameObject(GameObject selected) + { + SetSelectedGameObject(selected, baseEventDataCache); + } + + private static int RaycastComparer(RaycastResult lhs, RaycastResult rhs) + { + if (lhs.module != rhs.module) + { + var lhsEventCamera = lhs.module.eventCamera; + var rhsEventCamera = rhs.module.eventCamera; + if (lhsEventCamera != null && rhsEventCamera != null && lhsEventCamera.depth != rhsEventCamera.depth) + { + // need to reverse the standard compareTo + if (lhsEventCamera.depth < rhsEventCamera.depth) + return 1; + if (lhsEventCamera.depth == rhsEventCamera.depth) + return 0; + + return -1; + } + + if (lhs.module.sortOrderPriority != rhs.module.sortOrderPriority) + return rhs.module.sortOrderPriority.CompareTo(lhs.module.sortOrderPriority); + + if (lhs.module.renderOrderPriority != rhs.module.renderOrderPriority) + return rhs.module.renderOrderPriority.CompareTo(lhs.module.renderOrderPriority); + } + + if (lhs.sortingLayer != rhs.sortingLayer) + { + // Uses the layer value to properly compare the relative order of the layers. + var rid = SortingLayer.GetLayerValueFromID(rhs.sortingLayer); + var lid = SortingLayer.GetLayerValueFromID(lhs.sortingLayer); + return rid.CompareTo(lid); + } + + + if (lhs.sortingOrder != rhs.sortingOrder) + return rhs.sortingOrder.CompareTo(lhs.sortingOrder); + + if (lhs.depth != rhs.depth) + return rhs.depth.CompareTo(lhs.depth); + + if (lhs.distance != rhs.distance) + return lhs.distance.CompareTo(rhs.distance); + + return lhs.index.CompareTo(rhs.index); + }
+
+ #region 射线检测 +
+ private static readonly Comparison<RaycastResult> s_RaycastComparer = RaycastComparer; + + // 从PointerInputModule.GetTouchPointerEventData()来的触摸数据 + public void RaycastAll(PointerEventData eventData, List<RaycastResult> raycastResults) + { + raycastResults.Clear(); + //包括场景中所有的raycaster + var modules = RaycasterManager.GetRaycasters(); + for (int i = 0; i < modules.Count; ++i) + { + var module = modules[i]; + if (module == null || !module.IsActive()) + continue; + + module.Raycast(eventData, raycastResults); + } + + raycastResults.Sort(s_RaycastComparer); + }
+
+ #endregion +
+ //c 是否点击到某个物体
+ public bool IsPointerOverGameObject() + { + return IsPointerOverGameObject(PointerInputModule.kMouseLeftId); + } + + //c 是否点击到某个物体 + public bool IsPointerOverGameObject(int pointerId) + { + if (m_CurrentInputModule == null) + return false; + + return m_CurrentInputModule.IsPointerOverGameObject(pointerId); + } + + protected override void OnEnable() + { + base.OnEnable(); + if (EventSystem.current == null) + EventSystem.current = this; +#if UNITY_EDITOR + else + { + Debug.LogWarning("Multiple EventSystems in scene... this is not supported"); + } +#endif + } + + protected override void OnDisable() + { + if (m_CurrentInputModule != null) + { + m_CurrentInputModule.DeactivateModule(); + m_CurrentInputModule = null; + } + + if (EventSystem.current == this) + EventSystem.current = null; + + base.OnDisable(); + } + + protected virtual void OnApplicationFocus(bool hasFocus) + { + m_HasFocus = hasFocus; + } + + private void TickModules() + { + // 只有standaloneInputModule一个 + for (var i = 0; i < m_SystemInputModules.Count; i++) + { + if (m_SystemInputModules[i] != null) + m_SystemInputModules[i].UpdateModule(); + } + } + + //c 事件处理跑在一个update里,以轮训的方式检测输入 + protected virtual void Update() + { + if (current != this) + return; + + // 只更新鼠标位置 + TickModules(); + + // 将m_CurrentInputModule设置为m_SystemInputModules第一个可用的module + bool changedModule = false; + for (var i = 0; i < m_SystemInputModules.Count; i++) + { + var module = m_SystemInputModules[i]; + if (module.IsModuleSupported() && module.ShouldActivateModule()) + { + if (m_CurrentInputModule != module) + { + ChangeEventModule(module); + changedModule = true; + } + break; + } + } + // no event module set... set the first valid one... + if (m_CurrentInputModule == null) + { + for (var i = 0; i < m_SystemInputModules.Count; i++) + { + var module = m_SystemInputModules[i]; + if (module.IsModuleSupported()) + { + ChangeEventModule(module); + changedModule = true; + break; + } + } + } + + //c! 执行事件处理主入口,通常情况下,如果没有自定义的inputModule,就是走StandaloneInputModule的Process + if (!changedModule && m_CurrentInputModule != null) + m_CurrentInputModule.Process(); + } + + private void ChangeEventModule(BaseInputModule module) + { + if (m_CurrentInputModule == module) + return; + + if (m_CurrentInputModule != null) + m_CurrentInputModule.DeactivateModule(); + + if (module != null) + module.ActivateModule(); + m_CurrentInputModule = module; + } + + public override string ToString() + { + var sb = new StringBuilder(); + sb.AppendLine("<b>Selected:</b>" + currentSelectedGameObject); + sb.AppendLine(); + sb.AppendLine(); + sb.AppendLine(m_CurrentInputModule != null ? m_CurrentInputModule.ToString() : "No module"); + return sb.ToString(); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventSystem.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventSystem.cs.meta new file mode 100644 index 0000000..8ff114e --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 153642a2f08376d4daebdf63af9e161a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTrigger.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTrigger.cs new file mode 100644 index 0000000..11d1c1c --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTrigger.cs @@ -0,0 +1,159 @@ +using System; +using System.Collections.Generic; +using UnityEngine.Events; +using UnityEngine.Serialization; + +namespace UnityEngine.EventSystems +{ + //c 一个辅助组件,给一个事件添加自定义的回调函数 + //继承了所有接口,效率不高,慎用。只是比较方便 + + [AddComponentMenu("Event/Event Trigger")] + public class EventTrigger : + MonoBehaviour, + IPointerEnterHandler, + IPointerExitHandler, + IPointerDownHandler, + IPointerUpHandler, + IPointerClickHandler, + IInitializePotentialDragHandler, + IBeginDragHandler, + IDragHandler, + IEndDragHandler, + IDropHandler, + IScrollHandler, + IUpdateSelectedHandler, + ISelectHandler, + IDeselectHandler, + IMoveHandler, + ISubmitHandler, + ICancelHandler + { + [Serializable] + public class TriggerEvent : UnityEvent<BaseEventData> + {} + + [Serializable] + public class Entry + { + public EventTriggerType eventID = EventTriggerType.PointerClick; + public TriggerEvent callback = new TriggerEvent(); + } + + [FormerlySerializedAs("delegates")] + [SerializeField] + private List<Entry> m_Delegates; + + [Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)] + public List<Entry> delegates; + + protected EventTrigger() + {} + + public List<Entry> triggers + { + get + { + if (m_Delegates == null) + m_Delegates = new List<Entry>(); + return m_Delegates; + } + set { m_Delegates = value; } + } + + private void Execute(EventTriggerType id, BaseEventData eventData) + { + for (int i = 0, imax = triggers.Count; i < imax; ++i) + { + var ent = triggers[i]; + if (ent.eventID == id && ent.callback != null) + ent.callback.Invoke(eventData); + } + } + + public virtual void OnPointerEnter(PointerEventData eventData) + { + Execute(EventTriggerType.PointerEnter, eventData); + } + + public virtual void OnPointerExit(PointerEventData eventData) + { + Execute(EventTriggerType.PointerExit, eventData); + } + + public virtual void OnDrag(PointerEventData eventData) + { + Execute(EventTriggerType.Drag, eventData); + } + + public virtual void OnDrop(PointerEventData eventData) + { + Execute(EventTriggerType.Drop, eventData); + } + + public virtual void OnPointerDown(PointerEventData eventData) + { + Execute(EventTriggerType.PointerDown, eventData); + } + + public virtual void OnPointerUp(PointerEventData eventData) + { + Execute(EventTriggerType.PointerUp, eventData); + } + + public virtual void OnPointerClick(PointerEventData eventData) + { + Execute(EventTriggerType.PointerClick, eventData); + } + + public virtual void OnSelect(BaseEventData eventData) + { + Execute(EventTriggerType.Select, eventData); + } + + public virtual void OnDeselect(BaseEventData eventData) + { + Execute(EventTriggerType.Deselect, eventData); + } + + public virtual void OnScroll(PointerEventData eventData) + { + Execute(EventTriggerType.Scroll, eventData); + } + + public virtual void OnMove(AxisEventData eventData) + { + Execute(EventTriggerType.Move, eventData); + } + + public virtual void OnUpdateSelected(BaseEventData eventData) + { + Execute(EventTriggerType.UpdateSelected, eventData); + } + + public virtual void OnInitializePotentialDrag(PointerEventData eventData) + { + Execute(EventTriggerType.InitializePotentialDrag, eventData); + } + + public virtual void OnBeginDrag(PointerEventData eventData) + { + Execute(EventTriggerType.BeginDrag, eventData); + } + + public virtual void OnEndDrag(PointerEventData eventData) + { + Execute(EventTriggerType.EndDrag, eventData); + } + + public virtual void OnSubmit(BaseEventData eventData) + { + Execute(EventTriggerType.Submit, eventData); + } + + public virtual void OnCancel(BaseEventData eventData) + { + Execute(EventTriggerType.Cancel, eventData); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTrigger.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTrigger.cs.meta new file mode 100644 index 0000000..af5beba --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bacfe5cf756f51b4f8c77a4addae254e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTriggerType.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTriggerType.cs new file mode 100644 index 0000000..11c232c --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTriggerType.cs @@ -0,0 +1,29 @@ +namespace UnityEngine.EventSystems +{ + /// <summary> + /// This class is capable of triggering one or more remote functions from a specified event. + /// Usage: Attach it to an object with a collider, or to a GUI Graphic of your choice. + /// NOTE: Doing this will make this object intercept ALL events, and no event bubbling will occur from this object! + /// </summary> + + public enum EventTriggerType + { + PointerEnter = 0, + PointerExit = 1, + PointerDown = 2, + PointerUp = 3, + PointerClick = 4, + Drag = 5, + Drop = 6, + Scroll = 7, + UpdateSelected = 8, + Select = 9, + Deselect = 10, + Move = 11, + InitializePotentialDrag = 12, + BeginDrag = 13, + EndDrag = 14, + Submit = 15, + Cancel = 16 + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTriggerType.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTriggerType.cs.meta new file mode 100644 index 0000000..4647857 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/EventTriggerType.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ec8af6a988832aa46844f9df796dfc4c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/ExecuteEvents.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/ExecuteEvents.cs new file mode 100644 index 0000000..a749ce8 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/ExecuteEvents.cs @@ -0,0 +1,368 @@ +using System; +using System.Collections.Generic; +using UnityEngine.UI; + +namespace UnityEngine.EventSystems +{ + // 最关键的类,调用gameobject对应的回调函数 + public static class ExecuteEvents + { + public delegate void EventFunction<T1>(T1 handler, BaseEventData eventData); + + public static T ValidateEventData<T>(BaseEventData data) where T : class + { + if ((data as T) == null) + throw new ArgumentException(String.Format("Invalid type: {0} passed to event expecting {1}", data.GetType(), typeof(T))); + return data as T; + } + + private static readonly EventFunction<IPointerEnterHandler> s_PointerEnterHandler = Execute; + + private static void Execute(IPointerEnterHandler handler, BaseEventData eventData) + { + handler.OnPointerEnter(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IPointerExitHandler> s_PointerExitHandler = Execute; + + private static void Execute(IPointerExitHandler handler, BaseEventData eventData) + { + handler.OnPointerExit(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IPointerDownHandler> s_PointerDownHandler = Execute; + + private static void Execute(IPointerDownHandler handler, BaseEventData eventData) + { + handler.OnPointerDown(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IPointerUpHandler> s_PointerUpHandler = Execute; + + private static void Execute(IPointerUpHandler handler, BaseEventData eventData) + { + handler.OnPointerUp(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IPointerClickHandler> s_PointerClickHandler = Execute; + + private static void Execute(IPointerClickHandler handler, BaseEventData eventData) + { + handler.OnPointerClick(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IInitializePotentialDragHandler> s_InitializePotentialDragHandler = Execute; + + private static void Execute(IInitializePotentialDragHandler handler, BaseEventData eventData) + { + handler.OnInitializePotentialDrag(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IBeginDragHandler> s_BeginDragHandler = Execute; + + private static void Execute(IBeginDragHandler handler, BaseEventData eventData) + { + handler.OnBeginDrag(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IDragHandler> s_DragHandler = Execute; + + private static void Execute(IDragHandler handler, BaseEventData eventData) + { + handler.OnDrag(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IEndDragHandler> s_EndDragHandler = Execute; + + private static void Execute(IEndDragHandler handler, BaseEventData eventData) + { + handler.OnEndDrag(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IDropHandler> s_DropHandler = Execute; + + private static void Execute(IDropHandler handler, BaseEventData eventData) + { + handler.OnDrop(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IScrollHandler> s_ScrollHandler = Execute; + + private static void Execute(IScrollHandler handler, BaseEventData eventData) + { + handler.OnScroll(ValidateEventData<PointerEventData>(eventData)); + } + + private static readonly EventFunction<IUpdateSelectedHandler> s_UpdateSelectedHandler = Execute; + + private static void Execute(IUpdateSelectedHandler handler, BaseEventData eventData) + { + handler.OnUpdateSelected(eventData); + } + + private static readonly EventFunction<ISelectHandler> s_SelectHandler = Execute; + + private static void Execute(ISelectHandler handler, BaseEventData eventData) + { + handler.OnSelect(eventData); + } + + private static readonly EventFunction<IDeselectHandler> s_DeselectHandler = Execute; + + private static void Execute(IDeselectHandler handler, BaseEventData eventData) + { + handler.OnDeselect(eventData); + } + + private static readonly EventFunction<IMoveHandler> s_MoveHandler = Execute; + + private static void Execute(IMoveHandler handler, BaseEventData eventData) + { + handler.OnMove(ValidateEventData<AxisEventData>(eventData)); + } + + private static readonly EventFunction<ISubmitHandler> s_SubmitHandler = Execute; + + private static void Execute(ISubmitHandler handler, BaseEventData eventData) + { + handler.OnSubmit(eventData); + } + + private static readonly EventFunction<ICancelHandler> s_CancelHandler = Execute; + + private static void Execute(ICancelHandler handler, BaseEventData eventData) + { + handler.OnCancel(eventData); + } + + public static EventFunction<IPointerEnterHandler> pointerEnterHandler + { + get { return s_PointerEnterHandler; } + } + + public static EventFunction<IPointerExitHandler> pointerExitHandler + { + get { return s_PointerExitHandler; } + } + + public static EventFunction<IPointerDownHandler> pointerDownHandler + { + get { return s_PointerDownHandler; } + } + + public static EventFunction<IPointerUpHandler> pointerUpHandler + { + get { return s_PointerUpHandler; } + } + + public static EventFunction<IPointerClickHandler> pointerClickHandler + { + get { return s_PointerClickHandler; } + } + + public static EventFunction<IInitializePotentialDragHandler> initializePotentialDrag + { + get { return s_InitializePotentialDragHandler; } + } + + public static EventFunction<IBeginDragHandler> beginDragHandler + { + get { return s_BeginDragHandler; } + } + + public static EventFunction<IDragHandler> dragHandler + { + get { return s_DragHandler; } + } + + public static EventFunction<IEndDragHandler> endDragHandler + { + get { return s_EndDragHandler; } + } + + public static EventFunction<IDropHandler> dropHandler + { + get { return s_DropHandler; } + } + + public static EventFunction<IScrollHandler> scrollHandler + { + get { return s_ScrollHandler; } + } + + public static EventFunction<IUpdateSelectedHandler> updateSelectedHandler + { + get { return s_UpdateSelectedHandler; } + } + + public static EventFunction<ISelectHandler> selectHandler + { + get { return s_SelectHandler; } + } + + public static EventFunction<IDeselectHandler> deselectHandler + { + get { return s_DeselectHandler; } + } + + public static EventFunction<IMoveHandler> moveHandler + { + get { return s_MoveHandler; } + } + + public static EventFunction<ISubmitHandler> submitHandler + { + get { return s_SubmitHandler; } + } + + public static EventFunction<ICancelHandler> cancelHandler + { + get { return s_CancelHandler; } + } + + private static void GetEventChain(GameObject root, IList<Transform> eventChain) + { + eventChain.Clear(); + if (root == null) + return; + + var t = root.transform; + while (t != null) + { + eventChain.Add(t); + t = t.parent; + } + } + + //IEventSystemHandler是所有接口的基类 + private static readonly ObjectPool<List<IEventSystemHandler>> s_HandlerListPool = new ObjectPool<List<IEventSystemHandler>>(null, l => l.Clear()); + + // 向target gameobject发送一个事件,对每个实现了T接口的类执行functor方法(就是接口中定义的回调) + public static bool Execute<T>(GameObject target, BaseEventData eventData, EventFunction<T> functor) where T : IEventSystemHandler + { + var internalHandlers = s_HandlerListPool.Get(); + GetEventList<T>(target, internalHandlers); + // if (s_InternalHandlers.Count > 0) + // Debug.Log("Executinng " + typeof (T) + " on " + target); + + // 拿到实现了T接口的组件,调用对应的方法 + + for (var i = 0; i < internalHandlers.Count; i++) + { + T arg; + try + { + arg = (T)internalHandlers[i]; + } + catch (Exception e) + { + var temp = internalHandlers[i]; + Debug.LogException(new Exception(string.Format("Type {0} expected {1} received.", typeof(T).Name, temp.GetType().Name), e)); + continue; + } + + try + { + functor(arg, eventData); + } + catch (Exception e) + { + Debug.LogException(e); + } + } + + var handlerCount = internalHandlers.Count; + s_HandlerListPool.Release(internalHandlers); + return handlerCount > 0; + } + + /// <summary> + /// Execute the specified event on the first game object underneath the current touch. + /// </summary> + private static readonly List<Transform> s_InternalTransformList = new List<Transform>(30); + + public static GameObject ExecuteHierarchy<T>(GameObject root, BaseEventData eventData, EventFunction<T> callbackFunction) where T : IEventSystemHandler + { + GetEventChain(root, s_InternalTransformList); + + for (var i = 0; i < s_InternalTransformList.Count; i++) + { + var transform = s_InternalTransformList[i]; + if (Execute(transform.gameObject, eventData, callbackFunction)) + return transform.gameObject; + } + return null; + } + + //c component是否匹配类型T且是否被激活 + private static bool ShouldSendToComponent<T>(Component component) where T : IEventSystemHandler + { + var valid = component is T; + if (!valid) + return false; + + var behaviour = component as Behaviour; + if (behaviour != null) + return behaviour.isActiveAndEnabled; + return true; + } + + /// <summary> + /// Get the specified object's event event. + /// </summary> + private static void GetEventList<T>(GameObject go, IList<IEventSystemHandler> results) where T : IEventSystemHandler + { + // Debug.LogWarning("GetEventList<" + typeof(T).Name + ">"); + if (results == null) + throw new ArgumentException("Results array is null", "results"); + + if (go == null || !go.activeInHierarchy) + return; + + var components = ListPool<Component>.Get(); + go.GetComponents(components); + // 遍历所有的组件,找到匹配类型T的 + for (var i = 0; i < components.Count; i++) + { + // 如果不匹配类型T,跳过这个 + if (!ShouldSendToComponent<T>(components[i])) + continue; + + // Debug.Log(string.Format("{2} found! On {0}.{1}", go, s_GetComponentsScratch[i].GetType(), typeof(T))); + results.Add(components[i] as IEventSystemHandler); + } + ListPool<Component>.Release(components); + // Debug.LogWarning("end GetEventList<" + typeof(T).Name + ">"); + } + + /// <summary> + /// Whether the specified game object will be able to handle the specified event. + /// </summary> + public static bool CanHandleEvent<T>(GameObject go) where T : IEventSystemHandler + { + var internalHandlers = s_HandlerListPool.Get(); + GetEventList<T>(go, internalHandlers); + var handlerCount = internalHandlers.Count; + s_HandlerListPool.Release(internalHandlers); + return handlerCount != 0; + } + + /// <summary> + /// Bubble the specified event on the game object, figuring out which object will actually receive the event. + /// </summary> + public static GameObject GetEventHandler<T>(GameObject root) where T : IEventSystemHandler + { + if (root == null) + return null; + + Transform t = root.transform; + while (t != null) + { + if (CanHandleEvent<T>(t.gameObject)) + return t.gameObject; + t = t.parent; + } + return null; + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/ExecuteEvents.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/ExecuteEvents.cs.meta new file mode 100644 index 0000000..6317afb --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/ExecuteEvents.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 06bc2e6f5036fa848bb09004f4ab4e54 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/MoveDirection.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/MoveDirection.cs new file mode 100644 index 0000000..4f4147c --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/MoveDirection.cs @@ -0,0 +1,11 @@ +namespace UnityEngine.EventSystems +{ + public enum MoveDirection + { + Left, + Up, + Right, + Down, + None + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/MoveDirection.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/MoveDirection.cs.meta new file mode 100644 index 0000000..ba67175 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/EventHandler/MoveDirection.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: a1de2f5af1cc8aa4aa68afe78b9c671e +timeCreated: 1602119379 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |