// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using UltimateXR.UI.UnityInputModule.Controls;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UltimateXR.UI.UnityInputModule.Utils
{
///
/// Component that rotates a 3D object when a given UI control is being pressed.
/// This allows to model buttons that rotate depending on the point of pressure.
/// The axis of rotation will be computed automatically, the center will be given by
/// and the pressure applied will be on the transform of this component.
///
public class UxrButton3DRotate : UxrButton3D
{
#region Inspector Properties/Serialized Fields
[SerializeField] private Vector3 _buttonLocalUpAxis = Vector3.up;
[SerializeField] private float _pressedDegrees = 2.0f;
#endregion
#region Event Trigger Methods
///
/// Key down event. The object is rotated according to the pressing point.
///
/// Control that triggered the event
/// Input event data
protected override void OnKeyPressed(UxrControlInput controlInput, PointerEventData eventData)
{
if (Target)
{
Vector3 rotationAxis = Vector3.Cross(_buttonLocalUpAxis, Target.InverseTransformVector(Target.position - transform.position).normalized);
Target.Rotate(rotationAxis, -_pressedDegrees, Space.Self);
}
}
///
/// Key up event. The original object rotation is restored.
///
/// Control that triggered the event
/// Input event data
protected override void OnKeyReleased(UxrControlInput controlInput, PointerEventData eventData)
{
if (Target)
{
Target.localRotation = InitialTargetLocalRotation;
}
}
#endregion
}
}