namespace Zinnia.Pattern
{
using UnityEngine;
using UnityEngine.XR;
using Zinnia.Extension;
using Zinnia.Utility;
///
/// Matches the name of the selected property.
///
///
/// `InputDevices.GetDeviceAtXRNode(XRNode.Head)` is used in Unity 2020.2 and above instead of .
///
public class XRDevicePatternMatcher : PatternMatcher
{
///
/// The property source.
///
public enum Source
{
///
/// The device battery level.
///
BatteryLevel,
///
/// The number of devices found at a given node.
///
DeviceCount,
///
/// The device positional tracking state.
///
HasPositionalTracking,
///
/// The device rotational tracking state.
///
HasRotationalTracking,
///
/// The device presence state.
///
IsPresent,
///
/// The device tracked state.
///
IsTracked,
///
/// The device validity.
///
IsValid,
///
/// The device manufacturer.
///
Manufacturer,
///
/// The device model.
///
Model,
///
/// The device refresh rate.
///
RefreshRate,
///
/// The user presence state.
///
UserPresence
}
[Tooltip("The source property to match against.")]
[SerializeField]
private Source propertySource;
///
/// The source property to match against.
///
public Source PropertySource
{
get
{
return propertySource;
}
set
{
propertySource = value;
if (this.IsMemberChangeAllowed())
{
OnAfterPropertySourceChange();
}
}
}
#if UNITY_2019_3_OR_NEWER
[Tooltip("The source node to consider as the device to check.")]
[SerializeField]
private XRNode deviceSource = XRNode.Head;
///
/// The source node to consider as the device to check.
///
public XRNode DeviceSource
{
get
{
return deviceSource;
}
set
{
deviceSource = value;
if (this.IsMemberChangeAllowed())
{
OnAfterDeviceSourceChange();
}
}
}
#else
protected XRNode DeviceSource { get; set; } = XRNode.Head;
#endif
///
/// Sets the .
///
/// The index of the .
public virtual void SetPropertySource(int index)
{
PropertySource = EnumExtensions.GetByIndex(index);
}
///
/// Sets the .
///
/// The index of the .
public virtual void SetDeviceSource(int index)
{
#if UNITY_2019_3_OR_NEWER
DeviceSource = EnumExtensions.GetByIndex(index);
#else
DeviceSource = XRNode.Head;
#endif
}
///
protected override string DefineSourceString()
{
switch (PropertySource)
{
case Source.BatteryLevel:
return XRDeviceProperties.BatteryLevel(DeviceSource).ToString();
case Source.DeviceCount:
return XRDeviceProperties.DeviceCount(DeviceSource).ToString();
case Source.HasPositionalTracking:
return XRDeviceProperties.HasPositionalTracking().ToString();
case Source.HasRotationalTracking:
return XRDeviceProperties.HasRotationalTracking().ToString();
case Source.IsPresent:
return XRDeviceProperties.IsPresent().ToString();
case Source.IsTracked:
return XRDeviceProperties.IsTracked(DeviceSource).ToString();
case Source.IsValid:
return XRDeviceProperties.IsValid(DeviceSource).ToString();
case Source.Manufacturer:
return XRDeviceProperties.Manufacturer(DeviceSource);
case Source.Model:
return XRDeviceProperties.Model(DeviceSource);
case Source.RefreshRate:
return XRDevice.refreshRate.ToString();
case Source.UserPresence:
return XRDeviceProperties.UserPresence(DeviceSource);
}
return null;
}
///
/// Called after has been changed.
///
protected virtual void OnAfterPropertySourceChange()
{
ProcessSourceString();
}
///
/// Called after has been changed.
///
protected virtual void OnAfterDeviceSourceChange()
{
ProcessSourceString();
}
}
}