Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Collections.Generic;
using System.Text;
namespace UnityEngine.EventSystems
{
public abstract class PointerInputModule : BaseInputModule
{
public const int kMouseId = -1;
public const int kFakeTouchesId = -2;
protected Dictionary<int, PointerEventData> m_PointerData = new Dictionary<int, PointerEventData> ();
private bool GetPointerData(int id, out PointerEventData data, bool create)
{
if (!m_PointerData.TryGetValue (id, out data) && create)
{
data = new PointerEventData (eventSystem)
{
pointerId = id,
};
m_PointerData.Add (id, data);
return true;
}
return false;
}
protected void RemovePointerData(PointerEventData data)
{
m_PointerData.Remove (data.pointerId);
}
protected PointerEventData GetTouchPointerEventData(Touch input, out bool pressed, out bool released)
{
PointerEventData pointerData;
var created = GetPointerData (input.fingerId, out pointerData, true);
pointerData.Reset ();
pressed = created || (input.phase == TouchPhase.Began);
released = (input.phase == TouchPhase.Canceled) || (input.phase == TouchPhase.Ended);
if (created)
pointerData.position = input.position;
if (pressed)
pointerData.delta = Vector2.zero;
else
pointerData.delta = input.position - pointerData.position;
pointerData.position = input.position;
eventSystem.RaycastAll (pointerData, m_RaycastResultCache);
var raycast = FindFirstRaycast (m_RaycastResultCache);
pointerData.pointerCurrentRaycast = raycast;
m_RaycastResultCache.Clear ();
return pointerData;
}
protected virtual PointerEventData GetMousePointerEventData()
{
PointerEventData pointerData;
var created = GetPointerData (kMouseId, out pointerData, true);
pointerData.Reset ();
if (created)
pointerData.position = Input.mousePosition;
Vector2 pos = Input.mousePosition;
pointerData.delta = pos - pointerData.position;
pointerData.position = pos;
eventSystem.RaycastAll (pointerData, m_RaycastResultCache);
var raycast = FindFirstRaycast (m_RaycastResultCache);
pointerData.pointerCurrentRaycast = raycast;
m_RaycastResultCache.Clear ();
return pointerData;
}
protected PointerEventData GetLastPointerEventData(int id)
{
PointerEventData data;
GetPointerData (id, out data, false);
return data;
}
protected virtual void ProcessMove(PointerEventData pointerEvent)
{
bool moving = pointerEvent.IsPointerMoving ();
// Drag notification
if (moving && pointerEvent.pointerDrag != null)
{
// Before doing drag we should cancel any pointer down state
// And clear selection!
if (pointerEvent.pointerPress != pointerEvent.pointerDrag)
{
ExecuteEvents.Execute (pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
pointerEvent.eligibleForClick = false;
pointerEvent.pointerPress = null;
pointerEvent.rawPointerPress = null;
}
ExecuteEvents.Execute (pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.dragHandler);
}
}
public override bool IsPointerOverEventSystemObject(int pointerId)
{
var lastPointer = GetLastPointerEventData (pointerId);
if (lastPointer != null)
return lastPointer.pointerEnter != null;
return false;
}
protected void ClearSelection()
{
var baseEventData = GetBaseEventData ();
foreach (var pointer in m_PointerData.Values)
{
// clear all selection
HandlePointerExitAndEnter (pointer, null);
}
m_PointerData.Clear ();
eventSystem.SetSelectedGameObject (null, baseEventData);
}
public override string ToString()
{
var sb = new StringBuilder("<b>Pointer Input Module of type: </b>" + GetType ());
foreach (var pointer in m_PointerData)
{
if (pointer.Value == null)
continue;
sb.AppendLine ("<B>Pointer:</b> " + pointer.Key);
sb.AppendLine (pointer.Value.ToString ());
}
return sb.ToString ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.