// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using UltimateXR.Core.Components; using UltimateXR.UI.UnityInputModule.Controls; using UnityEngine; using UnityEngine.EventSystems; namespace UltimateXR.UI.UnityInputModule.Utils { /// /// Base class to simplify interacting with 3D button objects by programming 2D UI elements. /// A 2D Unity UI Canvas is placed on top of the 3D buttons. The Canvas will contain invisible /// UI components by using instead of images. /// The components will get the user input and through child implementations of /// the 3D objects will be "pushed", "rotated" creating 3D behaviour using 2D logic. /// [RequireComponent(typeof(UxrControlInput))] public class UxrButton3D : UxrComponent { #region Inspector Properties/Serialized Fields [SerializeField] private Transform _targetTransform; #endregion #region Public Types & Data /// /// Gets the UI input component. /// public UxrControlInput ControlInput => GetCachedComponent(); /// /// Gets the of the 3D object that is going to move, rotate, scale... /// public Transform Target => _targetTransform; /// /// Gets 's local position during Awake(). /// public Vector3 InitialTargetLocalPosition { get; private set; } /// /// Gets 's local rotation during Awake(). /// public Quaternion InitialTargetLocalRotation { get; private set; } /// /// Gets 's world position during Awake(). /// public Vector3 InitialTargetPosition { get; private set; } /// /// Gets 's world rotation during Awake(). /// public Quaternion InitialTargetRotation { get; private set; } #endregion #region Unity /// /// Initializes the component. /// protected override void Awake() { base.Awake(); if (Target != null) { InitialTargetLocalPosition = Target.localPosition; InitialTargetLocalRotation = Target.localRotation; InitialTargetPosition = Target.position; InitialTargetRotation = Target.rotation; } } /// /// Subscribes to the input control events. /// protected override void OnEnable() { base.OnEnable(); if (ControlInput) { ControlInput.Pressed += ControlInput_Pressed; ControlInput.Released += ControlInput_Released; } } /// /// Unsubscribes from the input control events. /// protected override void OnDisable() { base.OnDisable(); if (ControlInput) { ControlInput.Pressed -= ControlInput_Pressed; ControlInput.Released -= ControlInput_Released; } } #endregion #region Event Handling Methods /// /// Receives the key down event. /// /// Control that triggered the event /// Input event data private void ControlInput_Pressed(UxrControlInput controlInput, PointerEventData eventData) { OnKeyPressed(controlInput, eventData); } /// /// Receives the key up event. /// /// Control that triggered the event /// Input event data private void ControlInput_Released(UxrControlInput controlInput, PointerEventData eventData) { OnKeyReleased(controlInput, eventData); } #endregion #region Event Trigger Methods /// /// Event trigger for the key pressed event. It can be overridden in child classes to handle key presses without /// subscribing to events. /// /// Control that triggered the event /// Input event data protected virtual void OnKeyPressed(UxrControlInput controlInput, PointerEventData eventData) { } /// /// Event trigger for the key released event. It can be overridden in child classes to handle key releases without /// subscribing to events. /// /// Control that triggered the event /// Input event data protected virtual void OnKeyReleased(UxrControlInput controlInput, PointerEventData eventData) { } #endregion } }