/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus SDK
* 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.
*/
using System;
using UnityEngine;
namespace Oculus.Interaction.Input
{
///
/// The Interaction SDK's default concrete implementation of . Instances of this type represent and
/// expose the capabilities of tracked XR controllers in the physical world. Controllers are leveraged in many different ways
/// throughout the Interaction SDK (as s directly, as s, as sources for
/// data, etc.) and, along with s, is one of the main interaction modalities
/// supported by the SDK.
///
public class Controller :
DataModifier,
IController
{
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual Handedness Handedness => GetData().Config.Handedness;
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual bool IsConnected
{
get
{
var currentData = GetData();
return currentData.IsDataValid && currentData.IsConnected;
}
}
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual bool IsPoseValid
{
get
{
var currentData = GetData();
return currentData.IsDataValid &&
currentData.RootPoseOrigin != PoseOrigin.None;
}
}
///
/// Checks whether a valid "pointer pose" is currently available. See
/// for details on what a pointer pose is.
///
public virtual bool IsPointerPoseValid
{
get
{
var currentData = GetData();
return currentData.IsDataValid &&
currentData.PointerPoseOrigin != PoseOrigin.None;
}
}
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual ControllerInput ControllerInput
{
get
{
var currentData = GetData();
return currentData.Input;
}
}
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual event Action WhenUpdated = delegate { };
private ITrackingToWorldTransformer TrackingToWorldTransformer =>
GetData().Config.TrackingToWorldTransformer;
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual float Scale => TrackingToWorldTransformer != null
? TrackingToWorldTransformer.Transform.lossyScale.x
: 1;
///
/// Implementation of ; for details, please refer to
/// the related documentation provided for that interface.
///
public virtual bool IsButtonUsageAnyActive(ControllerButtonUsage buttonUsage)
{
var currentData = GetData();
return
currentData.IsDataValid &&
(buttonUsage & currentData.Input.ButtonUsageMask) != 0;
}
///
/// Implementation of ; for details, please refer to
/// the related documentation provided for that interface.
///
public virtual bool IsButtonUsageAllActive(ControllerButtonUsage buttonUsage)
{
var currentData = GetData();
return currentData.IsDataValid &&
(buttonUsage & currentData.Input.ButtonUsageMask) == buttonUsage;
}
///
/// Implementation of ; for details, please refer to the related documentation
/// provided for that interface.
///
public virtual bool TryGetPose(out Pose pose)
{
if (!IsPoseValid)
{
pose = Pose.identity;
return false;
}
pose = GetData().Config.TrackingToWorldTransformer.ToWorldPose(GetData().RootPose);
return true;
}
///
/// Implementation of ; for details, please refer to the related
/// documentation provided for that interface.
///
public virtual bool TryGetPointerPose(out Pose pose)
{
if (!IsPointerPoseValid)
{
pose = Pose.identity;
return false;
}
pose = GetData().Config.TrackingToWorldTransformer.ToWorldPose(GetData().PointerPose);
return true;
}
///
/// Overrides and encapsulates , augmenting the behavior of the
/// base type's method by invoking to notify observers of the occurrence.
///
public override void MarkInputDataRequiresUpdate()
{
base.MarkInputDataRequiresUpdate();
if (Started)
{
WhenUpdated.Invoke();
}
}
protected override void Apply(ControllerDataAsset data)
{
// Default implementation does nothing, to allow instantiation of this modifier directly
}
#region Inject
///
/// Wrapper for
/// ,
/// for injecting the required dependencies to a dynamically-allocated Controller instance. This method exists to support
/// Interaction SDK's dependency injection pattern and is not needed for typical Unity Editor-based usage.
///
public void InjectAllController(UpdateModeFlags updateMode, IDataSource updateAfter,
IDataSource modifyDataFromSource, bool applyModifier)
{
base.InjectAllDataModifier(updateMode, updateAfter, modifyDataFromSource, applyModifier);
}
#endregion
}
}