// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.MixedReality.Toolkit.Utilities; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.UI { /// /// Base class for all constraints /// public abstract class TransformConstraint : MonoBehaviour { #region Properties [SerializeField] [EnumFlags] [Tooltip("What type of manipulation this constraint applies to. Defaults to One Handed and Two Handed.")] private ManipulationHandFlags handType = ManipulationHandFlags.OneHanded | ManipulationHandFlags.TwoHanded; /// /// Whether this constraint applies to one hand manipulation, two hand manipulation or both /// public ManipulationHandFlags HandType { get => handType; set => handType = value; } [SerializeField] [EnumFlags] [Tooltip("What type of manipulation this constraint applies to. Defaults to Near and Far.")] private ManipulationProximityFlags proximityType = ManipulationProximityFlags.Near | ManipulationProximityFlags.Far; /// /// Whether this constraint applies to near manipulation, far manipulation or both /// public ManipulationProximityFlags ProximityType { get => proximityType; set => proximityType = value; } [SerializeField] [Tooltip("Execution order priority of this constraint. Lower numbers will be executed before higher numbers.")] private int executionOrder = 0; /// /// Execution order priority of this constraint. Lower numbers will be executed before higher numbers. /// public int ExecutionPriority { get => executionOrder; set { executionOrder = value; // Notify all ConstraintManagers to re-sort these priorities. foreach (var mgr in gameObject.GetComponents()) { mgr.RefreshPriorities(); } } } protected MixedRealityTransform worldPoseOnManipulationStart; public abstract TransformFlags ConstraintType { get; } #endregion Properties #region Public Methods /// /// Intended to be called on manipulation started /// public virtual void Initialize(MixedRealityTransform worldPose) { worldPoseOnManipulationStart = worldPose; } /// /// Abstract method for applying constraints to transforms during manipulation /// public abstract void ApplyConstraint(ref MixedRealityTransform transform); #endregion Public Methods #region MonoBeaviour protected void OnEnable() { var managers = gameObject.GetComponents(); foreach (var manager in managers) { manager.AutoRegisterConstraint(this); } } protected void OnDisable() { var managers = gameObject.GetComponents(); foreach (var manager in managers) { manager.AutoUnregisterConstraint(this); } } #endregion #region Deprecated /// /// Intended to be called on manipulation started /// [System.Obsolete("Deprecated: Pass MixedRealityTransform instead of MixedRealityPose.")] public virtual void Initialize(MixedRealityPose worldPose) { Initialize(new MixedRealityTransform(worldPose.Position, worldPose.Rotation, Vector3.one)); } /// /// Transform that we intend to apply constraints to /// [System.Obsolete("Deprecated: Get component transform instead.")] public Transform TargetTransform { get; set; } = null; #endregion } }