ControllerStateParseUtility.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using UnityEngine;
  13. using System.Runtime.InteropServices;
  14. using System.Collections.Generic;
  15. /// <summary> A controller state parse utility. </summary>
  16. public class ControllerStateParseUtility : MonoBehaviour
  17. {
  18. /// <summary> The controller state parsers. </summary>
  19. private static IControllerStateParser[] m_ControllerStateParsers = new IControllerStateParser[NRInput.MAX_CONTROLLER_STATE_COUNT];
  20. /// <summary> The default parser class type dictionary. </summary>
  21. private static Dictionary<ControllerType, System.Type> m_DefaultParserClassTypeDict = new Dictionary<ControllerType, System.Type>()
  22. {
  23. {ControllerType.CONTROLLER_TYPE_PHONE, typeof(NRPhoneControllerStateParser)},
  24. {ControllerType.CONTROLLER_TYPE_EDITOR, typeof(NRPhoneControllerStateParser)},
  25. {ControllerType.CONTROLLER_TYPE_NREALLIGHT, typeof(NrealLightControllerStateParser)}
  26. };
  27. /// <summary> Creates controller state parser. </summary>
  28. /// <param name="parserType"> Type of the parser.</param>
  29. /// <returns> The new controller state parser. </returns>
  30. private static IControllerStateParser CreateControllerStateParser(System.Type parserType)
  31. {
  32. if (parserType != null)
  33. {
  34. object parserObj = Activator.CreateInstance(parserType);
  35. if (parserObj is IControllerStateParser)
  36. return parserObj as IControllerStateParser;
  37. }
  38. return null;
  39. }
  40. /// <summary> Gets default controller state parser type. </summary>
  41. /// <param name="controllerType"> Type of the controller.</param>
  42. /// <returns> The default controller state parser type. </returns>
  43. private static System.Type GetDefaultControllerStateParserType(ControllerType controllerType)
  44. {
  45. if (m_DefaultParserClassTypeDict.ContainsKey(controllerType))
  46. return m_DefaultParserClassTypeDict[controllerType];
  47. return null;
  48. }
  49. /// <summary> Gets controller state parser. </summary>
  50. /// <param name="controllerType"> Type of the controller.</param>
  51. /// <param name="index"> Zero-based index of the.</param>
  52. /// <returns> The controller state parser. </returns>
  53. public static IControllerStateParser GetControllerStateParser(ControllerType controllerType, int index)
  54. {
  55. System.Type parserType = GetDefaultControllerStateParserType(controllerType);
  56. if (parserType == null)
  57. m_ControllerStateParsers[index] = null;
  58. else if (m_ControllerStateParsers[index] == null || parserType != m_ControllerStateParsers[index].GetType())
  59. m_ControllerStateParsers[index] = CreateControllerStateParser(parserType);
  60. return m_ControllerStateParsers[index];
  61. }
  62. /// <summary> Sets default controller state parser type. </summary>
  63. /// <param name="controllerType"> Type of the controller.</param>
  64. /// <param name="parserType"> Type of the parser.</param>
  65. public static void SetDefaultControllerStateParserType(ControllerType controllerType, System.Type parserType)
  66. {
  67. if (parserType == null && m_DefaultParserClassTypeDict.ContainsKey(controllerType))
  68. {
  69. m_DefaultParserClassTypeDict.Remove(controllerType);
  70. return;
  71. }
  72. if (m_DefaultParserClassTypeDict.ContainsKey(controllerType))
  73. m_DefaultParserClassTypeDict[controllerType] = parserType;
  74. else
  75. m_DefaultParserClassTypeDict.Add(controllerType, parserType);
  76. }
  77. }
  78. }