/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using System;
using UnityEngine;
using System.Runtime.InteropServices;
using System.Collections.Generic;
/// A controller state parse utility.
public class ControllerStateParseUtility : MonoBehaviour
{
/// The controller state parsers.
private static IControllerStateParser[] m_ControllerStateParsers = new IControllerStateParser[NRInput.MAX_CONTROLLER_STATE_COUNT];
/// The default parser class type dictionary.
private static Dictionary m_DefaultParserClassTypeDict = new Dictionary()
{
{ControllerType.CONTROLLER_TYPE_PHONE, typeof(NRPhoneControllerStateParser)},
{ControllerType.CONTROLLER_TYPE_EDITOR, typeof(NRPhoneControllerStateParser)},
{ControllerType.CONTROLLER_TYPE_NREALLIGHT, typeof(NrealLightControllerStateParser)}
};
/// Creates controller state parser.
/// Type of the parser.
/// The new controller state parser.
private static IControllerStateParser CreateControllerStateParser(System.Type parserType)
{
if (parserType != null)
{
object parserObj = Activator.CreateInstance(parserType);
if (parserObj is IControllerStateParser)
return parserObj as IControllerStateParser;
}
return null;
}
/// Gets default controller state parser type.
/// Type of the controller.
/// The default controller state parser type.
private static System.Type GetDefaultControllerStateParserType(ControllerType controllerType)
{
if (m_DefaultParserClassTypeDict.ContainsKey(controllerType))
return m_DefaultParserClassTypeDict[controllerType];
return null;
}
/// Gets controller state parser.
/// Type of the controller.
/// Zero-based index of the.
/// The controller state parser.
public static IControllerStateParser GetControllerStateParser(ControllerType controllerType, int index)
{
System.Type parserType = GetDefaultControllerStateParserType(controllerType);
if (parserType == null)
m_ControllerStateParsers[index] = null;
else if (m_ControllerStateParsers[index] == null || parserType != m_ControllerStateParsers[index].GetType())
m_ControllerStateParsers[index] = CreateControllerStateParser(parserType);
return m_ControllerStateParsers[index];
}
/// Sets default controller state parser type.
/// Type of the controller.
/// Type of the parser.
public static void SetDefaultControllerStateParserType(ControllerType controllerType, System.Type parserType)
{
if (parserType == null && m_DefaultParserClassTypeDict.ContainsKey(controllerType))
{
m_DefaultParserClassTypeDict.Remove(controllerType);
return;
}
if (m_DefaultParserClassTypeDict.ContainsKey(controllerType))
m_DefaultParserClassTypeDict[controllerType] = parserType;
else
m_DefaultParserClassTypeDict.Add(controllerType, parserType);
}
}
}