// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.MixedReality.Toolkit.Physics; using Unity.Profiling; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Input { /// /// Touch Pointer Implementation. /// [AddComponentMenu("Scripts/MRTK/SDK/TouchPointer")] public class TouchPointer : BaseControllerPointer, IMixedRealityTouchPointer { private bool isInteractionEnabled = false; /// public override bool IsInteractionEnabled => isInteractionEnabled; private int fingerId = -1; /// public int FingerId { get { return fingerId; } set { if (fingerId < 0) { fingerId = value; } } } /// public Ray TouchRay { get; set; } = default(Ray); private static readonly ProfilerMarker OnPreSceneQueryPerfMarker = new ProfilerMarker("[MRTK] TouchPointer.OnPreSceneQuery"); /// public override void OnPreSceneQuery() { using (OnPreSceneQueryPerfMarker.Auto()) { if (!IsInteractionEnabled) { return; } Rays[0].CopyRay(TouchRay, PointerExtent); if (RayStabilizer != null) { RayStabilizer.UpdateStability(Rays[0].Origin, Rays[0].Direction); Rays[0].CopyRay(RayStabilizer.StableRay, PointerExtent); if (MixedRealityRaycaster.DebugEnabled) { Debug.DrawRay(RayStabilizer.StableRay.origin, RayStabilizer.StableRay.direction * PointerExtent, Color.green); } } else if (MixedRealityRaycaster.DebugEnabled) { Debug.DrawRay(TouchRay.origin, TouchRay.direction * PointerExtent, Color.yellow); } } } /// public override Vector3 Position => TouchRay.origin; /// public override Quaternion Rotation => Quaternion.LookRotation(TouchRay.direction); private static readonly ProfilerMarker OnSourceDetectedPerfMarker = new ProfilerMarker("[MRTK] TouchPointer.OnSourceDetected"); /// public override void OnSourceDetected(SourceStateEventData eventData) { using (OnSourceDetectedPerfMarker.Auto()) { base.OnSourceDetected(eventData); if (eventData.InputSource.SourceId == Controller.InputSource.SourceId) { isInteractionEnabled = true; } } } private static readonly ProfilerMarker OnSourceLostPerfMarker = new ProfilerMarker("[MRTK] TouchPointer.OnSourceLost"); /// public override void OnSourceLost(SourceStateEventData eventData) { using (OnSourceLostPerfMarker.Auto()) { base.OnSourceLost(eventData); if (Controller != null && eventData.Controller != null && eventData.Controller.InputSource.SourceId == Controller.InputSource.SourceId) { isInteractionEnabled = false; } } } } }