// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Input
{
///
/// Base Component for handling Focus on GameObjects.
///
[RequireComponent(typeof(Collider))]
public abstract class BaseFocusHandler : MonoBehaviour, IMixedRealityFocusHandler, IMixedRealityFocusChangedHandler
{
[SerializeField]
[Tooltip("Is focus enabled for this component?")]
private bool focusEnabled = true;
///
/// Is focus enabled for this Component?
///
public virtual bool FocusEnabled
{
get { return focusEnabled; }
set { focusEnabled = value; }
}
///
/// Does this object currently have focus by any ?
///
public virtual bool HasFocus => FocusEnabled && Focusers.Count > 0;
///
/// The list of s that are currently focused on this GameObject
///
public List Focusers { get; } = new List(0);
///
public virtual void OnFocusEnter(FocusEventData eventData) { }
///
public virtual void OnFocusExit(FocusEventData eventData) { }
///
public virtual void OnBeforeFocusChange(FocusEventData eventData)
{
// If we're the new target object,
// add the pointer to the list of focusers.
if (eventData.NewFocusedObject == gameObject)
{
eventData.Pointer.FocusTarget = this;
Focusers.Add(eventData.Pointer);
}
// If we're the old focused target object,
// remove the pointer from our list.
else if (eventData.OldFocusedObject == gameObject)
{
Focusers.Remove(eventData.Pointer);
// If there is no new focused target
// clear the FocusTarget field from the Pointer.
if (eventData.NewFocusedObject == null)
{
eventData.Pointer.FocusTarget = null;
}
}
}
///
public virtual void OnFocusChanged(FocusEventData eventData) { }
}
}