// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.MixedReality.Toolkit.Utilities.Editor;
using UnityEngine.Events;
namespace Microsoft.MixedReality.Toolkit.UI
{
///
/// A basic focus event receiver
///
public class InteractableOnFocusReceiver : ReceiverBase
{
///
/// Creates receiver that raises focus enter and exit unity events
///
public InteractableOnFocusReceiver() : this(new UnityEvent()) { }
///
/// Creates receiver that raises focus enter and exit unity events
///
public InteractableOnFocusReceiver(UnityEvent ev) : base(ev, "OnFocusOn") { }
///
/// Raised when focus has left the object
///
[InspectorField(Type = InspectorField.FieldTypes.Event, Label = "On Focus Off", Tooltip = "Focus has left the object")]
public UnityEvent OnFocusOff = new UnityEvent();
///
/// Raised when focus has entered the object
///
public UnityEvent OnFocusOn => uEvent;
private bool hadFocus;
///
public override void OnUpdate(InteractableStates state, Interactable source)
{
bool hasFocus = state.GetState(InteractableStates.InteractableStateEnum.Focus).Value > 0;
if (hadFocus != hasFocus)
{
if (hasFocus)
{
uEvent.Invoke();
}
else
{
OnFocusOff.Invoke();
}
}
hadFocus = hasFocus;
}
}
}