// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using UltimateXR.Core.Components.Composite;
using UnityEngine;
namespace UltimateXR.Manipulation.Helpers
{
///
/// Component that allows an object be grabbed only if another object is being grabbed. For instance, it can
/// be added to a grenade pin to make sure the pin is never grabbed unless the grenade is being grabbed too.
/// Otherwise the pin could be removed by mistake when trying to grab the grenade.
///
public class UxrDependentGrabbable : UxrGrabbableObjectComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private UxrGrabbableObject _dependentOn;
[SerializeField] private bool _onlyOnce;
#endregion
#region Public Types & Data
///
/// Gets or sets the grabbable object the component depends on.
///
public UxrGrabbableObject DependentFrom
{
get => _dependentOn;
set => _dependentOn = value;
}
///
/// Whether to stop toggling the enabled state once the dependent object was grabbed. For instance, a grenade pin
/// should remain grabbable once it has been removed from the grenade, no matter if the grenade is being grabbed or
/// not at that point.
///
public bool OnlyOnce
{
get => _onlyOnce;
set => _onlyOnce = value;
}
#endregion
#region Unity
///
/// Initializes the grabbable object state.
///
protected override void Start()
{
base.Start();
GrabbableObject.enabled = false;
}
///
/// Updates the grabbable object state.
///
private void Update()
{
if (GrabbableObject && DependentFrom && _check)
{
GrabbableObject.enabled = UxrGrabManager.Instance.IsBeingGrabbed(DependentFrom);
if (GrabbableObject.enabled && OnlyOnce)
{
_check = false;
}
}
}
#endregion
#region Event Trigger Methods
///
/// Called whenever the object was grabbed.
///
/// Event parameters
protected override void OnObjectGrabbed(UxrManipulationEventArgs e)
{
base.OnObjectGrabbed(e);
_check = false;
}
#endregion
#region Private Types & Data
private bool _check = true;
#endregion
}
}