// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
#if WINDOWS_UWP
using Windows.Perception;
using Windows.UI.Input.Spatial;
#elif DOTNETWINRT_PRESENT
using Microsoft.Windows.Perception;
using Microsoft.Windows.UI.Input.Spatial;
#endif
#endif // (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
namespace Microsoft.MixedReality.Toolkit.WindowsMixedReality
{
///
/// Provides useful extensions for Windows-defined types.
///
public static class WindowsExtensions
{
#if (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
///
/// Converts a platform into
/// the equivalent value in MRTK's defined .
///
/// The handedness value to convert.
/// The converted value in the new type.
public static Handedness ToMRTKHandedness(this SpatialInteractionSourceHandedness handedness)
{
switch (handedness)
{
case SpatialInteractionSourceHandedness.Left:
return Handedness.Left;
case SpatialInteractionSourceHandedness.Right:
return Handedness.Right;
case SpatialInteractionSourceHandedness.Unspecified:
default:
return Handedness.None;
}
}
///
/// Tries to get an active SpatialInteractionSource with the corresponding handedness and input source type.
///
/// The handedness of the source to get.
/// The input source type of the source to get.
/// The input source or null if none could be found.
public static SpatialInteractionSource GetSpatialInteractionSource(Handedness handedness, InputSourceType inputSourceType)
{
SpatialInteractionSourceHandedness sourceHandedness;
switch (handedness)
{
default:
sourceHandedness = SpatialInteractionSourceHandedness.Unspecified;
break;
case Handedness.Left:
sourceHandedness = SpatialInteractionSourceHandedness.Left;
break;
case Handedness.Right:
sourceHandedness = SpatialInteractionSourceHandedness.Right;
break;
}
SpatialInteractionSourceKind sourceKind;
switch (inputSourceType)
{
default:
sourceKind = SpatialInteractionSourceKind.Other;
break;
case InputSourceType.Controller:
sourceKind = SpatialInteractionSourceKind.Controller;
break;
case InputSourceType.Hand:
sourceKind = SpatialInteractionSourceKind.Hand;
break;
}
System.Collections.Generic.IReadOnlyList sourceStates =
WindowsMixedRealityUtilities.SpatialInteractionManager?.GetDetectedSourcesAtTimestamp(PerceptionTimestampHelper.FromHistoricalTargetTime(System.DateTimeOffset.UtcNow));
if (sourceStates == null)
{
return null;
}
foreach (SpatialInteractionSourceState sourceState in sourceStates)
{
if (sourceState.Source.Handedness == sourceHandedness && sourceState.Source.Kind == sourceKind)
{
return sourceState.Source;
}
}
return null;
}
#endif // (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
}
}