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