/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// Manager for controller visuals.
[DisallowMultipleComponent]
public class ControllerVisualManager : MonoBehaviour
{
/// The states.
private ControllerState[] m_States;
/// The controller visuals.
private IControllerVisual[] m_ControllerVisuals;
/// Executes the 'enable' action.
private void OnEnable()
{
NRInput.OnControllerStatesUpdated += OnControllerStatesUpdated;
}
/// Executes the 'disable' action.
private void OnDisable()
{
NRInput.OnControllerStatesUpdated -= OnControllerStatesUpdated;
}
/// Initializes this object.
/// The states.
public void Init(ControllerState[] states)
{
this.m_States = states;
m_ControllerVisuals = new IControllerVisual[states.Length];
}
/// Change controller visual.
/// Zero-based index of the.
/// Type of the visual.
public void ChangeControllerVisual(int index, ControllerVisualType visualType)
{
if (m_ControllerVisuals[index] != null)
{
DestroyVisual(index);
}
CreateControllerVisual(index, visualType);
}
/// Executes the 'controller states updated' action.
private void OnControllerStatesUpdated()
{
UpdateAllVisuals();
}
/// Updates all visuals.
private void UpdateAllVisuals()
{
int availableCount = NRInput.GetAvailableControllersCount();
if (availableCount > 1)
{
UpdateVisual(0, m_States[0]);
UpdateVisual(1, m_States[1]);
}
else
{
int activeVisual = NRInput.DomainHand == ControllerHandEnum.Right ? 0 : 1;
int deactiveVisual = NRInput.DomainHand == ControllerHandEnum.Right ? 1 : 0;
UpdateVisual(activeVisual, m_States[0]);
DestroyVisual(deactiveVisual);
}
}
/// Updates the visual.
/// Zero-based index of the.
/// The state.
private void UpdateVisual(int index, ControllerState state)
{
ControllerVisualType visualType = ControllerVisualFactory.GetDefaultVisualType(state.controllerType);
if (m_ControllerVisuals[index] != null && visualType == ControllerVisualType.None)
{
DestroyVisual(index);
}
else if (m_ControllerVisuals[index] == null && visualType != ControllerVisualType.None)
{
CreateControllerVisual(index, visualType);
}
if (m_ControllerVisuals[index] != null)
{
m_ControllerVisuals[index].SetActive(NRInput.ControllerVisualActive);
m_ControllerVisuals[index].UpdateVisual(state);
}
}
/// Creates controller visual.
/// Zero-based index of the.
/// Type of the visual.
private void CreateControllerVisual(int index, ControllerVisualType visualType)
{
GameObject visualGo = ControllerVisualFactory.CreateControllerVisualObject(visualType);
if (visualGo == null)
return;
m_ControllerVisuals[index] = visualGo.GetComponent();
if (m_ControllerVisuals[index] != null)
{
ControllerAnchorEnum ancherEnum = (index == 0 ? ControllerAnchorEnum.RightModelAnchor : ControllerAnchorEnum.LeftModelAnchor);
visualGo.transform.parent = NRInput.AnchorsHelper.GetAnchor(ancherEnum);
visualGo.transform.localPosition = Vector3.zero;
visualGo.transform.localRotation = Quaternion.identity;
visualGo.transform.localScale = Vector3.one;
}
else
{
NRDebugger.Error("The ControllerVisual prefab:" + visualGo.name + " does not contain IControllerVisual interface");
Destroy(visualGo);
}
}
/// Destroys the visual described by index.
/// Zero-based index of the.
private void DestroyVisual(int index)
{
if (m_ControllerVisuals[index] != null)
{
m_ControllerVisuals[index].DestroySelf();
m_ControllerVisuals[index] = null;
}
}
}
}