public void ProcessTouch (bool pressed, bool unpressed)
{
// Whether we're using the mouse
bool isMouse = (currentTouch == mMouse[0]);
float drag = isMouse ? mouseDragThreshold : touchDragThreshold;
float click = isMouse ? mouseClickThreshold : touchClickThreshold;
// Send out the press message
if (pressed)
{
if (mTooltip != null) ShowTooltip(false);
currentTouch.pressStarted = true;
Notify(currentTouch.pressed, "OnPress", false);
currentTouch.pressed = currentTouch.current;
currentTouch.clickNotification = ClickNotification.Always;
currentTouch.totalDelta = Vector2.zero;
currentTouch.dragStarted = false;
Notify(currentTouch.pressed, "OnPress", true);
// Clear the selection
if (currentTouch.pressed != mSel)
{
if (mTooltip != null) ShowTooltip(false);
selectedObject = null;
}
}
else
{
// If the user is pressing down and has dragged the touch away from the original object,
// unpress the original object and notify the new object that it is now being pressed on.
if (!stickyPress && !unpressed && currentTouch.pressStarted && currentTouch.pressed != hoveredObject)
{
Notify(currentTouch.pressed, "OnPress", false);
currentTouch.pressed = hoveredObject;
Notify(currentTouch.pressed, "OnPress", true);
}
if (currentTouch.pressed != null)
{
float mag = currentTouch.delta.magnitude;
if (mag != 0f)
{
// Keep track of the total movement
currentTouch.totalDelta += currentTouch.delta;
mag = currentTouch.totalDelta.magnitude;
// If the drag event has not yet started, see if we've dragged the touch far enough to start it
if (!currentTouch.dragStarted && drag < mag)
{
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);
bool isDisabled = (currentTouch.clickNotification == ClickNotification.None);
Notify(currentTouch.pressed, "OnDrag", currentTouch.delta);
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;
}
}
}
}
}
// Send out the unpress message
if (unpressed)
{
currentTouch.pressStarted = false;
if (mTooltip != null) ShowTooltip(false);
if (currentTouch.pressed != null)
{
Notify(currentTouch.pressed, "OnPress", false);
// Send a hover message to the object, but don't add it to the list of hovered items
// as it's already present. This happens when the mouse is released over the same button
// it was pressed on, and since it already had its 'OnHover' event, it never got
// Highlight(false), so we simply re-notify it so it can update the visible state.
if (useMouse && currentTouch.pressed == mHover) Notify(currentTouch.pressed, "OnHover", true);
// If the button/touch was released on the same object, consider it a click and select it
if (currentTouch.pressed == currentTouch.current ||
(currentTouch.clickNotification != ClickNotification.None &&
currentTouch.totalDelta.magnitude < drag))
{
if (currentTouch.pressed != mSel)
{
mSel = currentTouch.pressed;
Notify(currentTouch.pressed, "OnSelect", true);
}
else
{
mSel = currentTouch.pressed;
}
// If the touch should consider clicks, send out an OnClick notification
if (currentTouch.clickNotification != ClickNotification.None)
{
float time = Time.realtimeSinceStartup;
Notify(currentTouch.pressed, "OnClick", null);
if (currentTouch.clickTime + 0.35f > time)
{
Notify(currentTouch.pressed, "OnDoubleClick", null);
}
currentTouch.clickTime = time;
}
}
else // The button/touch was released on a different object
{
// Send a drop notification (for drag & drop)
Notify(currentTouch.current, "OnDrop", currentTouch.pressed);
}
}
currentTouch.dragStarted = false;
currentTouch.pressed = null;
}
}