SteamVR_Input_Source.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. private static Dictionary<SteamVR_Input_Sources, ulong> inputSourceHandlesBySource = new Dictionary<SteamVR_Input_Sources, ulong>(new SteamVR_Input_Sources_Comparer());
  15. private static Dictionary<ulong, SteamVR_Input_Sources> inputSourceSourcesByHandle = new Dictionary<ulong, SteamVR_Input_Sources>();
  16. private static Type enumType = typeof(SteamVR_Input_Sources);
  17. private static Type descriptionType = typeof(DescriptionAttribute);
  18. private static SteamVR_Input_Sources[] allSources;
  19. public static ulong GetHandle(SteamVR_Input_Sources inputSource)
  20. {
  21. if (inputSourceHandlesBySource.ContainsKey(inputSource))
  22. return inputSourceHandlesBySource[inputSource];
  23. return 0;
  24. }
  25. public static SteamVR_Input_Sources GetSource(ulong handle)
  26. {
  27. if (inputSourceSourcesByHandle.ContainsKey(handle))
  28. return inputSourceSourcesByHandle[handle];
  29. return SteamVR_Input_Sources.Any;
  30. }
  31. public static SteamVR_Input_Sources[] GetAllSources()
  32. {
  33. if (allSources == null)
  34. allSources = (SteamVR_Input_Sources[])System.Enum.GetValues(typeof(SteamVR_Input_Sources));
  35. return allSources;
  36. }
  37. private static string GetPath(string inputSourceEnumName)
  38. {
  39. return ((DescriptionAttribute)enumType.GetMember(inputSourceEnumName)[0].GetCustomAttributes(descriptionType, false)[0]).Description;
  40. }
  41. public static void Initialize()
  42. {
  43. List<SteamVR_Input_Sources> allSourcesList = new List<SteamVR_Input_Sources>();
  44. string[] enumNames = System.Enum.GetNames(enumType);
  45. inputSourceHandlesBySource = new Dictionary<SteamVR_Input_Sources, ulong>(new SteamVR_Input_Sources_Comparer());
  46. inputSourceSourcesByHandle = new Dictionary<ulong, SteamVR_Input_Sources>();
  47. for (int enumIndex = 0; enumIndex < enumNames.Length; enumIndex++)
  48. {
  49. string path = GetPath(enumNames[enumIndex]);
  50. ulong handle = 0;
  51. EVRInputError err = OpenVR.Input.GetInputSourceHandle(path, ref handle);
  52. if (err != EVRInputError.None)
  53. Debug.LogError("<b>[SteamVR]</b> GetInputSourceHandle (" + path + ") error: " + err.ToString());
  54. if (enumNames[enumIndex] == SteamVR_Input_Sources.Any.ToString()) //todo: temporary hack
  55. {
  56. inputSourceHandlesBySource.Add((SteamVR_Input_Sources)enumIndex, 0);
  57. inputSourceSourcesByHandle.Add(0, (SteamVR_Input_Sources)enumIndex);
  58. }
  59. else
  60. {
  61. inputSourceHandlesBySource.Add((SteamVR_Input_Sources)enumIndex, handle);
  62. inputSourceSourcesByHandle.Add(handle, (SteamVR_Input_Sources)enumIndex);
  63. }
  64. allSourcesList.Add((SteamVR_Input_Sources)enumIndex);
  65. }
  66. allSources = allSourcesList.ToArray();
  67. }
  68. }
  69. }