// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.MixedReality.Toolkit.Utilities; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Input { /// /// Animated cursor is a cursor driven using an animator to inject state information /// and animate accordingly /// [AddComponentMenu("Scripts/MRTK/SDK/AnimatedCursor")] public class AnimatedCursor : BaseCursor { [SerializeField] [Header("Animated Cursor State Data")] [Tooltip("Cursor state data to use for its various states.")] private AnimatedCursorStateData[] cursorStateData = null; [SerializeField] [Header("Animated Cursor Context Data")] [Tooltip("Cursor context data to use for its various contextual states.")] private AnimatedCursorContextData[] cursorContextData = null; [SerializeField] [Tooltip("Animator parameter to set when input is enabled.")] private AnimatorParameter inputEnabledParameter = default(AnimatorParameter); [SerializeField] [Tooltip("Animator parameter to set when input is disabled.")] private AnimatorParameter inputDisabledParameter = default(AnimatorParameter); [SerializeField] [Tooltip("Animator for the cursor")] private Animator cursorAnimator = null; /// /// Change animation state when enabling input. /// public override void OnInputEnabled() { base.OnInputEnabled(); SetAnimatorParameter(inputEnabledParameter); } /// /// Change animation state when disabling input. /// public override void OnInputDisabled() { base.OnInputDisabled(); SetAnimatorParameter(inputDisabledParameter); } /// /// Override to set the cursor animation trigger. /// public override void OnFocusChanged(FocusEventData eventData) { base.OnFocusChanged(eventData); if (((Pointer is UnityEngine.Object) ? ((Pointer as UnityEngine.Object) != null) : (Pointer != null)) && Pointer.CursorModifier != null) { if ((Pointer.CursorModifier.CursorParameters != null) && (Pointer.CursorModifier.CursorParameters.Length > 0)) { OnCursorStateChange(CursorStateEnum.Contextual); for (var i = 0; i < Pointer.CursorModifier.CursorParameters.Length; i++) { SetAnimatorParameter(Pointer.CursorModifier.CursorParameters[i]); } } } else { OnCursorStateChange(CursorStateEnum.None); } } /// /// Override OnCursorState change to set the correct animation state for the cursor. /// public override void OnCursorStateChange(CursorStateEnum state) { base.OnCursorStateChange(state); if (state == CursorStateEnum.Contextual) { return; } for (int i = 0; i < cursorStateData.Length; i++) { if (cursorStateData[i].CursorState == state) { SetAnimatorParameter(cursorStateData[i].Parameter); } } } /// /// Override OnCursorContext change to set the correct animation state for the cursor. /// public override void OnCursorContextChange(CursorContextEnum context) { base.OnCursorContextChange(context); if (context == CursorContextEnum.Contextual) { return; } for (int i = 0; i < cursorContextData.Length; i++) { if (cursorContextData[i].CursorState == context) { SetAnimatorParameter(cursorContextData[i].Parameter); } } } /// /// Based on the type of animator state info pass it through to the animator /// private void SetAnimatorParameter(AnimatorParameter animationParameter) { // Return if we do not have an animator if (cursorAnimator == null || !cursorAnimator.isInitialized) { return; } switch (animationParameter.ParameterType) { case AnimatorControllerParameterType.Bool: cursorAnimator.SetBool(animationParameter.NameHash, animationParameter.DefaultBool); break; case AnimatorControllerParameterType.Float: cursorAnimator.SetFloat(animationParameter.NameHash, animationParameter.DefaultFloat); break; case AnimatorControllerParameterType.Int: cursorAnimator.SetInteger(animationParameter.NameHash, animationParameter.DefaultInt); break; case AnimatorControllerParameterType.Trigger: cursorAnimator.SetTrigger(animationParameter.NameHash); break; } } } }