using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Base class for the visible, interactible transform controls in the ZED MR Calibration scene.
/// Handles the visuals, enabling/disabling objects when grabbed or let go, and knows what grabbed it.
/// Behaviors to actually move the ZED are in inheriting classes.
///
public abstract class TransformGrabbable : MonoBehaviour, IXRHoverable, IXRGrabbable
{
///
/// Calls SetActive(true) on all objects when a grab starts, and SetActive(false) when it ends.
/// Used to enable/disable the transform and limit indicators.
///
[Tooltip("Calls SetActive(true) on all objects when a grab starts, and SetActive(false) when it ends. " +
"Used to enable/disable the transform and limit indicators.")]
public List enableOnlyWhenGrabbed = new List();
///
/// Material applied when ZEDXRGrabber hovers over it. If empty, a default one is loaded from Resources.
///
[Tooltip("Material applied when ZEDXRGrabber hovers over it. If empty, a default one is loaded from Resources. ")]
public Material hoverMaterial;
private Dictionary baseMaterials = new Dictionary();
protected Transform grabbingTransform;
protected bool isGrabbed
{
get
{
return grabbingTransform != null;
}
}
///
/// Make sure we have a hovermat assigned.
///
protected virtual void Awake()
{
if (!hoverMaterial)
{
hoverMaterial = Resources.Load("HoverMat");
}
}
///
/// Returns the transform. Exists as the IXRGrabbable and IXRHoverable interfaces can't inherit from Monobehaviours.
///
///
public Transform GetTransform()
{
return transform;
}
///
/// Apply the hover material to all Renderers on and parented to this transform.
///
void IXRHoverable.OnHoverStart()
{
foreach (Renderer rend in GetComponentsInChildren())
{
if (!baseMaterials.ContainsKey(rend))
{
baseMaterials.Add(rend, rend.material);
}
rend.material = hoverMaterial;
}
}
///
/// Return the original materials to all the Renderers on and parented to this transform.
///
void IXRHoverable.OnHoverEnd()
{
if (this == null) return;
foreach (Renderer rend in GetComponentsInChildren())
{
if (baseMaterials.ContainsKey(rend))
{
rend.material = baseMaterials[rend];
}
else Debug.LogError("Starting material for " + rend.gameObject + " wasn't cached before hover started.");
}
}
///
/// What happens when ZEDXRGrabber first grabs it. From IXRGrabbable.
///
public virtual void OnGrabStart(Transform grabtransform)
{
grabbingTransform = grabtransform;
//Enable all indicators.
foreach (GameObject go in enableOnlyWhenGrabbed)
{
go.SetActive(true);
}
}
///
/// What happens when ZEDXRGrabber stops grabbing it. From IXRGrabbable.
///
public virtual void OnGrabEnd()
{
grabbingTransform = null;
//Disable all indicators.
foreach (GameObject go in enableOnlyWhenGrabbed)
{
go.SetActive(false);
}
}
}