/// <summary>
/// Process the press part of a touch.
/// </summary>
void ProcessPress (bool pressed, float click, float drag)
{
// Send out the press message
if (pressed)
{
if (mTooltip != null) ShowTooltip(false);
currentTouch.pressStarted = true;
if (onPress != null && currentTouch.pressed)
onPress(currentTouch.pressed, false);
Notify(currentTouch.pressed, "OnPress", false);
currentTouch.pressed = currentTouch.current;
currentTouch.dragged = currentTouch.current;
currentTouch.clickNotification = ClickNotification.BasedOnDelta;
currentTouch.totalDelta = Vector2.zero;
currentTouch.dragStarted = false;
if (onPress != null && currentTouch.pressed)
onPress(currentTouch.pressed, true);
Notify(currentTouch.pressed, "OnPress", true);
// Update the selection
if (currentTouch.pressed != mCurrentSelection)
{
if (mTooltip != null) ShowTooltip(false);
currentScheme = ControlScheme.Touch;
selectedObject = currentTouch.pressed;
}
}
else if (currentTouch.pressed != null && (currentTouch.delta.sqrMagnitude != 0f || currentTouch.current != currentTouch.last))
{
// Keep track of the total movement
currentTouch.totalDelta += currentTouch.delta;
float mag = currentTouch.totalDelta.sqrMagnitude;
bool justStarted = false;
// If the drag process hasn't started yet but we've already moved off the object, start it immediately
if (!currentTouch.dragStarted && currentTouch.last != currentTouch.current)
{
currentTouch.dragStarted = true;
currentTouch.delta = currentTouch.totalDelta;
// OnDragOver is sent for consistency, so that OnDragOut is always preceded by OnDragOver
isDragging = true;
if (onDragStart != null) onDragStart(currentTouch.dragged);
Notify(currentTouch.dragged, "OnDragStart", null);
if (onDragOver != null) onDragOver(currentTouch.last, currentTouch.dragged);
Notify(currentTouch.last, "OnDragOver", currentTouch.dragged);
isDragging = false;
}
else if (!currentTouch.dragStarted && drag < mag)
{
// If the drag event has not yet started, see if we've dragged the touch far enough to start it
justStarted = true;
currentTouch.dragStarted = true;
currentTouch.delta = currentTouch.totalDelta;
}
// If we're dragging the touch, send out drag events
if (currentTouch.dragStarted)
{
if (mTooltip != null) ShowTooltip(false);
isDragging = true;
bool isDisabled = (currentTouch.clickNotification == ClickNotification.None);
if (justStarted)
{
if (onDragStart != null) onDragStart(currentTouch.dragged);
Notify(currentTouch.dragged, "OnDragStart", null);
if (onDragOver != null) onDragOver(currentTouch.last, currentTouch.dragged);
Notify(currentTouch.current, "OnDragOver", currentTouch.dragged);
}
else if (currentTouch.last != currentTouch.current)
{
if (onDragStart != null) onDragStart(currentTouch.dragged);
Notify(currentTouch.last, "OnDragOut", currentTouch.dragged);
if (onDragOver != null) onDragOver(currentTouch.last, currentTouch.dragged);
Notify(currentTouch.current, "OnDragOver", currentTouch.dragged);
}
if (onDrag != null) onDrag(currentTouch.dragged, currentTouch.delta);
Notify(currentTouch.dragged, "OnDrag", currentTouch.delta);
currentTouch.last = currentTouch.current;
isDragging = false;
if (isDisabled)
{
// If the notification status has already been disabled, keep it as such
currentTouch.clickNotification = ClickNotification.None;
}
else if (currentTouch.clickNotification == ClickNotification.BasedOnDelta && click < mag)
{
// We've dragged far enough to cancel the click
currentTouch.clickNotification = ClickNotification.None;
}
}
}
}
/// <summary>
/// Process the release part of a touch.
/// </summary>
void ProcessRelease (bool isMouse, float drag)
{
// Send out the unpress message
currentTouch.pressStarted = false;
if (mTooltip != null) ShowTooltip(false);
if (currentTouch.pressed != null)
{
// If there was a drag event in progress, make sure OnDragOut gets sent
if (currentTouch.dragStarted)
{
if (onDragOut != null) onDragOut(currentTouch.last, currentTouch.dragged);
Notify(currentTouch.last, "OnDragOut", currentTouch.dragged);
if (onDragEnd != null) onDragEnd(currentTouch.dragged);
Notify(currentTouch.dragged, "OnDragEnd", null);
}
// Send the notification of a touch ending
if (onPress != null) onPress(currentTouch.pressed, false);
Notify(currentTouch.pressed, "OnPress", false);
// Send a hover message to the object
if (isMouse)
{
if (onHover != null) onHover(currentTouch.current, true);
Notify(currentTouch.current, "OnHover", true);
}
mHover = currentTouch.current;
// If the button/touch was released on the same object, consider it a click and select it
if (currentTouch.dragged == currentTouch.current ||
(currentScheme != ControlScheme.Controller &&
currentTouch.clickNotification != ClickNotification.None &&
currentTouch.totalDelta.sqrMagnitude < drag))
{
if (currentTouch.pressed != mCurrentSelection)
{
mNextSelection = null;
mCurrentSelection = currentTouch.pressed;
if (onSelect != null) onSelect(currentTouch.pressed, true);
Notify(currentTouch.pressed, "OnSelect", true);
}
else
{
mNextSelection = null;
mCurrentSelection = currentTouch.pressed;
}
// If the touch should consider clicks, send out an OnClick notification
if (currentTouch.clickNotification != ClickNotification.None && currentTouch.pressed == currentTouch.current)
{
float time = RealTime.time;
if (onClick != null) onClick(currentTouch.pressed);
Notify(currentTouch.pressed, "OnClick", null);
if (currentTouch.clickTime + 0.35f > time)
{
if (onDoubleClick != null) onDoubleClick(currentTouch.pressed);
Notify(currentTouch.pressed, "OnDoubleClick", null);
}
currentTouch.clickTime = time;
}
}
else if (currentTouch.dragStarted) // The button/touch was released on a different object
{
// Send a drop notification (for drag & drop)
if (onDrop != null) onDrop(currentTouch.current, currentTouch.dragged);
Notify(currentTouch.current, "OnDrop", currentTouch.dragged);
}
}
currentTouch.dragStarted = false;
currentTouch.pressed = null;
currentTouch.dragged = null;
}
/// <summary>
/// Process the events of the specified touch.
/// </summary>
public void ProcessTouch (bool pressed, bool released)
{
// Whether we're using the mouse
bool isMouse = (currentScheme == ControlScheme.Mouse);
float drag = isMouse ? mouseDragThreshold : touchDragThreshold;
float click = isMouse ? mouseClickThreshold : touchClickThreshold;
// So we can use sqrMagnitude below
drag *= drag;
click *= click;
if (currentTouch.pressed != null)
{
if (released) ProcessRelease(isMouse, drag);
ProcessPress(pressed, click, drag);
}
else
{
ProcessPress(pressed, click, drag);
if (released) ProcessRelease(isMouse, drag);
}
}