Advertisement
Guest User

PassThroughDragEvents

a guest
Jun 7th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7.  
  8. namespace UI.Pagination
  9. {
  10.     class PassThroughDragEvents : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
  11.     {
  12.         public List<GameObject> Targets = null;
  13.         public List<string> DesiredTargetTypes = null;
  14.         Dictionary<string, Dictionary<MonoBehaviour, MethodInfo>> m_Events = new Dictionary<string, Dictionary<MonoBehaviour, MethodInfo>>();        
  15.  
  16.         static List<string> eventTypes = new List<string>()
  17.         {
  18.             "OnBeginDrag",
  19.             "OnEndDrag",
  20.             "OnDrag"
  21.         };
  22.  
  23.         Vector2 m_dragStartPosition = new Vector2();
  24.         Vector2 m_dragEndPosition = new Vector2();
  25.         Vector2 m_delta = new Vector2();
  26.         bool m_dragging = false;
  27.  
  28.         public bool PassThroughHorizontalDragEvents = true;        
  29.         public bool PassThroughVerticalDragEvents = true;        
  30.  
  31.         void Start()
  32.         {
  33.             Initialise();
  34.         }
  35.  
  36.         public void Initialise()
  37.         {
  38.             m_Events.Clear();
  39.  
  40.             if (Targets == null || Targets.Count == 0 || DesiredTargetTypes == null || DesiredTargetTypes.Count == 0) return;
  41.  
  42.             foreach (var eventType in eventTypes)
  43.             {
  44.                 foreach (var target in Targets)
  45.                 {
  46.                     if (target == null) continue;                    
  47.  
  48.                     var components = target.GetComponents<MonoBehaviour>();
  49.                     foreach (var component in components)
  50.                     {
  51.                         var type = component.GetType();
  52.  
  53.                         if (!DesiredTargetTypes.Contains(type.Name)) continue;
  54.  
  55.                         var methodInfo = type.GetMethod(eventType, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  56.  
  57.                         if (methodInfo != null)
  58.                         {
  59.                             if (!m_Events.ContainsKey(eventType))
  60.                             {
  61.                                 m_Events.Add(eventType, new Dictionary<MonoBehaviour, MethodInfo>());
  62.                             }
  63.                            
  64.                             m_Events[eventType].Add(component, methodInfo);
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         void Update()
  72.         {
  73.             if (m_dragging)
  74.             {
  75.                 m_dragStartPosition = Input.mousePosition;
  76.             }
  77.         }
  78.  
  79.         public void OnEndDrag(PointerEventData data)
  80.         {
  81.             if (!m_dragging) return;
  82.  
  83.             m_dragging = false;
  84.  
  85.             // if this object is used on a scrollview, data.delta is always (0,0) for some reason, so we need to calculate it ourselves
  86.             m_dragEndPosition = Input.mousePosition;
  87.             m_delta = m_dragEndPosition - m_dragStartPosition;
  88.             data.delta = m_delta;
  89.  
  90.             if (!m_Events.ContainsKey("OnEndDrag")) return;            
  91.  
  92.             foreach (var kvp in m_Events["OnEndDrag"])
  93.             {                
  94.                 kvp.Value.Invoke(kvp.Key, new object[] { data });
  95.             }            
  96.         }
  97.  
  98.         public void OnBeginDrag(PointerEventData data)
  99.         {
  100.             var analysis = new DragEventAnalysis(data);            
  101.  
  102.             if (!(this.PassThroughHorizontalDragEvents && analysis.DragPlane == DragEventAnalysis.eDragPlane.Horizontal)
  103.              && !(this.PassThroughVerticalDragEvents && analysis.DragPlane == DragEventAnalysis.eDragPlane.Vertical))
  104.             {                
  105.                 return;
  106.             }
  107.  
  108.             m_dragging = true;
  109.  
  110.             if (!m_Events.ContainsKey("OnBeginDrag")) return;                      
  111.  
  112.             foreach (var kvp in m_Events["OnBeginDrag"])
  113.             {
  114.                 kvp.Value.Invoke(kvp.Key, new object[] { data });
  115.             }
  116.         }
  117.  
  118.         public void OnDrag(PointerEventData data)
  119.         {
  120.             if (!m_dragging) return;
  121.             if (!m_Events.ContainsKey("OnDrag")) return;      
  122.  
  123.             foreach (var kvp in m_Events["OnDrag"])
  124.             {
  125.                 kvp.Value.Invoke(kvp.Key, new object[] { data });
  126.             }
  127.         }              
  128.     }
  129.  
  130.     public class DragEventAnalysis
  131.     {
  132.         private PointerEventData data;
  133.  
  134.         public enum eDragPlane
  135.         {
  136.             Horizontal,
  137.             Vertical,
  138.             None
  139.         }
  140.  
  141.         public eDragPlane DragPlane
  142.         {
  143.             get
  144.             {
  145.                 if (Math.Abs(data.delta.x) > Math.Abs(data.delta.y))
  146.                 {
  147.                     return eDragPlane.Horizontal;
  148.                 }
  149.  
  150.                 if (Math.Abs(data.delta.y) > Math.Abs(data.delta.x))
  151.                 {
  152.                     return eDragPlane.Vertical;
  153.                 }
  154.  
  155.                 return eDragPlane.None;
  156.             }
  157.         }
  158.            
  159.         public DragEventAnalysis(PointerEventData data)
  160.         {
  161.             this.data = data;                
  162.         }            
  163.     }    
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement