Browse Source

Register DragCompleted handler to Draggable2D instead of finding all instances in the scene.

master
fjruizpo 8 years ago
parent
commit
77cd77d154
  1. 41
      Assets/Fungus/Scripts/Components/Draggable2D.cs
  2. 12
      Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs

41
Assets/Fungus/Scripts/Components/Draggable2D.cs

@ -4,6 +4,7 @@
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.Serialization; using UnityEngine.Serialization;
using System.Collections.Generic;
namespace Fungus namespace Fungus
{ {
@ -40,6 +41,23 @@ namespace Fungus
protected Vector3 newPosition; protected Vector3 newPosition;
protected Vector3 delta = Vector3.zero; protected Vector3 delta = Vector3.zero;
#region DragCompleted handlers
protected List<DragCompleted> dragCompletedHandlers = new List<DragCompleted>();
public void RegisterHandler(DragCompleted handler)
{
dragCompletedHandlers.Add(handler);
}
public void UnregisterHandler(DragCompleted handler)
{
if(dragCompletedHandlers.Contains(handler))
{
dragCompletedHandlers.Remove(handler);
}
}
#endregion
protected virtual void LateUpdate() protected virtual void LateUpdate()
{ {
// iTween will sometimes override the object position even if it should only be affecting the scale, rotation, etc. // iTween will sometimes override the object position even if it should only be affecting the scale, rotation, etc.
@ -69,12 +87,6 @@ namespace Fungus
EventDispatcher.Raise(new DragExited.DragExitedEvent(this, other)); EventDispatcher.Raise(new DragExited.DragExitedEvent(this, other));
} }
protected virtual T[] GetHandlers<T>() where T : EventHandler
{
// TODO: Cache these object for faster lookup
return GameObject.FindObjectsOfType<T>();
}
protected virtual void DoBeginDrag() protected virtual void DoBeginDrag()
{ {
// Offset the object so that the drag is anchored to the exact point where the user clicked it // Offset the object so that the drag is anchored to the exact point where the user clicked it
@ -113,20 +125,15 @@ namespace Fungus
bool dragCompleted = false; bool dragCompleted = false;
var handlers = GetHandlers<DragCompleted>(); for (int i = 0; i < dragCompletedHandlers.Count; i++)
for (int i = 0; i < handlers.Length; i++)
{ {
var handler = handlers[i]; var handler = dragCompletedHandlers[i];
if (handler.DraggableObject == this) if (handler != null && handler.DraggableObject == this)
{ {
if (handler.IsOverTarget()) if (handler.IsOverTarget())
{ {
EventDispatcher.Raise(new DragCompleted.DragCompletedEvent(this));
dragCompleted = true; dragCompleted = true;
if (returnOnCompleted) EventDispatcher.Raise(new DragCompleted.DragCompletedEvent(this));
{
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo);
}
} }
} }
} }
@ -140,6 +147,10 @@ namespace Fungus
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo);
} }
} }
else if(returnOnCompleted)
{
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo);
}
} }
protected virtual void DoPointerEnter() protected virtual void DoPointerEnter()

12
Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs

@ -38,6 +38,11 @@ namespace Fungus
EventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent); EventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent);
EventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); EventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
EventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent); EventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
if(draggableObject != null)
{
draggableObject.RegisterHandler(this);
}
} }
protected override void UnityOnDisable() protected override void UnityOnDisable()
@ -45,7 +50,12 @@ namespace Fungus
base.UnityOnDisable(); base.UnityOnDisable();
EventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent); EventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent);
EventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); EventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
EventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent); EventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
if(draggableObject != null)
{
draggableObject.UnregisterHandler(this);
}
} }
void OnDragCompletedEvent(DragCompletedEvent evt) void OnDragCompletedEvent(DragCompletedEvent evt)

Loading…
Cancel
Save