1
0

SteamVR_Input_Source.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. using Valve.VR;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.ComponentModel;
  9. using System.Collections.Generic;
  10. namespace Valve.VR
  11. {
  12. public static class SteamVR_Input_Source
  13. {
  14. public static int numSources = System.Enum.GetValues(typeof(SteamVR_Input_Sources)).Length;
  15. private static ulong[] inputSourceHandlesBySource;
  16. private static Dictionary<ulong, SteamVR_Input_Sources> inputSourceSourcesByHandle = new Dictionary<ulong, SteamVR_Input_Sources>();
  17. private static Type enumType = typeof(SteamVR_Input_Sources);
  18. private static Type descriptionType = typeof(DescriptionAttribute);
  19. private static SteamVR_Input_Sources[] allSources;
  20. public static ulong GetHandle(SteamVR_Input_Sources inputSource)
  21. {
  22. int index = (int)inputSource;
  23. if (index < inputSourceHandlesBySource.Length)
  24. return inputSourceHandlesBySource[index];
  25. return 0;
  26. }
  27. public static SteamVR_Input_Sources GetSource(ulong handle)
  28. {
  29. if (inputSourceSourcesByHandle.ContainsKey(handle))
  30. return inputSourceSourcesByHandle[handle];
  31. return SteamVR_Input_Sources.Any;
  32. }
  33. public static SteamVR_Input_Sources[] GetAllSources()
  34. {
  35. if (allSources == null)
  36. allSources = (SteamVR_Input_Sources[])System.Enum.GetValues(typeof(SteamVR_Input_Sources));
  37. return allSources;
  38. }
  39. private static string GetPath(string inputSourceEnumName)
  40. {
  41. return ((DescriptionAttribute)enumType.GetMember(inputSourceEnumName)[0].GetCustomAttributes(descriptionType, false)[0]).Description;
  42. }
  43. public static void Initialize()
  44. {
  45. List<SteamVR_Input_Sources> allSourcesList = new List<SteamVR_Input_Sources>();
  46. string[] enumNames = System.Enum.GetNames(enumType);
  47. inputSourceHandlesBySource = new ulong[enumNames.Length];
  48. inputSourceSourcesByHandle = new Dictionary<ulong, SteamVR_Input_Sources>();
  49. for (int enumIndex = 0; enumIndex < enumNames.Length; enumIndex++)
  50. {
  51. string path = GetPath(enumNames[enumIndex]);
  52. ulong handle = 0;
  53. EVRInputError err = OpenVR.Input.GetInputSourceHandle(path, ref handle);
  54. if (err != EVRInputError.None)
  55. Debug.LogError("<b>[SteamVR]</b> GetInputSourceHandle (" + path + ") error: " + err.ToString());
  56. if (enumNames[enumIndex] == SteamVR_Input_Sources.Any.ToString()) //todo: temporary hack
  57. {
  58. inputSourceHandlesBySource[enumIndex] = 0;
  59. inputSourceSourcesByHandle.Add(0, (SteamVR_Input_Sources)enumIndex);
  60. }
  61. else
  62. {
  63. inputSourceHandlesBySource[enumIndex] = handle;
  64. inputSourceSourcesByHandle.Add(handle, (SteamVR_Input_Sources)enumIndex);
  65. }
  66. allSourcesList.Add((SteamVR_Input_Sources)enumIndex);
  67. }
  68. allSources = allSourcesList.ToArray();
  69. }
  70. }
  71. }