//-----------------------------------------------------------------------
//
//
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
//-----------------------------------------------------------------------
// Modifications copyright © 2020 Unity Technologies ApS
#if AR_FOUNDATION_PRESENT || PACKAGE_DOCS_GENERATION
using System;
namespace UnityEngine.XR.Interaction.Toolkit.AR
{
///
/// A Gesture represents a sequence of touch events that are detected to
/// represent a particular type of motion (e.g. Dragging, Pinching).
///
/// The actual gesture.
///
/// Gestures are created and updated by instances of .
///
public abstract partial class Gesture where T : Gesture
{
///
/// Initializes and returns an instance of with a given recognizer.
///
/// The gesture recognizer.
internal Gesture(GestureRecognizer recognizer)
{
this.recognizer = recognizer;
}
///
/// Calls the methods in its invocation list when a gesture is started.
///
public event Action onStart;
///
/// Calls the methods in its invocation list when a gesture is successfully updated.
///
public event Action onUpdated;
///
/// Calls the methods in its invocation list when a gesture is finished.
///
public event Action onFinished;
///
/// (Read Only) A boolean value indicating whether the gesture was canceled.
///
public bool isCanceled { get; private set; }
///
/// (Read Only) The GameObject this gesture is targeting.
///
public GameObject targetObject { get; protected set; }
///
/// (Read Only) The gesture recognizer.
///
protected internal GestureRecognizer recognizer { get; }
bool m_HasStarted;
///
/// Updates this gesture.
///
internal void Update()
{
if (!m_HasStarted && CanStart())
{
Start();
return;
}
if (m_HasStarted)
{
if (UpdateGesture())
{
onUpdated?.Invoke(this as T);
}
}
}
///
/// Derived types should call this during their Reinitialize step. Gesture is getting
/// reinitialized, so this call resets data to construction-time defaults.
///
internal void Reinitialize()
{
onStart = null;
onUpdated = null;
onFinished = null;
isCanceled = false;
targetObject = null;
m_HasStarted = false;
}
///
/// Cancels this gesture.
///
internal void Cancel()
{
isCanceled = true;
OnCancel();
Complete();
}
///
/// Completes this gesture.
///
protected internal void Complete()
{
OnFinish();
onFinished?.Invoke(this as T);
}
///
/// Starts this gesture.
///
void Start()
{
m_HasStarted = true;
OnStart();
onStart?.Invoke(this as T);
}
///
/// Returns true if this gesture can start.
///
/// Returns if the gesture can start. Otherwise, returns .
protected internal abstract bool CanStart();
///
/// This method is called automatically when this gesture is started.
///
protected internal abstract void OnStart();
///
/// Updates this gesture.
///
/// Returns if the update was successful. Otherwise, returns .
protected internal abstract bool UpdateGesture();
///
/// This method is called automatically when this gesture is canceled.
///
///
/// When canceled, this method is called right before , which is still invoked.
///
protected internal abstract void OnCancel();
///
/// This method is called automatically when this gesture is finished.
///
protected internal abstract void OnFinish();
}
}
#endif