SteamVR_ActionSet_Manager.cs
5.47 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
using UnityEngine;
using System.Collections;
using System;
using Valve.VR;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
namespace Valve.VR
{
/// <summary>
/// Action sets are logical groupings of actions. Multiple sets can be active at one time.
/// </summary>
public static class SteamVR_ActionSet_Manager
{
public static VRActiveActionSet_t[] rawActiveActionSetArray;
[NonSerialized]
private static uint activeActionSetSize;
private static bool changed = false;
public static void Initialize()
{
activeActionSetSize = (uint)(Marshal.SizeOf(typeof(VRActiveActionSet_t)));
}
/// <summary>
/// Disable all known action sets.
/// </summary>
public static void DisableAllActionSets()
{
for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
{
SteamVR_Input.actionSets[actionSetIndex].Deactivate(SteamVR_Input_Sources.Any);
SteamVR_Input.actionSets[actionSetIndex].Deactivate(SteamVR_Input_Sources.LeftHand);
SteamVR_Input.actionSets[actionSetIndex].Deactivate(SteamVR_Input_Sources.RightHand);
}
}
private static int lastFrameUpdated;
public static void UpdateActionStates(bool force = false)
{
if (force || Time.frameCount != lastFrameUpdated)
{
lastFrameUpdated = Time.frameCount;
if (changed)
{
UpdateActionSetsArray();
}
if (rawActiveActionSetArray != null && rawActiveActionSetArray.Length > 0)
{
if (OpenVR.Input != null)
{
EVRInputError err = OpenVR.Input.UpdateActionState(rawActiveActionSetArray, activeActionSetSize);
if (err != EVRInputError.None)
Debug.LogError("<b>[SteamVR]</b> UpdateActionState error: " + err.ToString());
//else Debug.Log("Action sets activated: " + activeActionSets.Length);
}
}
else
{
//Debug.LogWarning("No sets active");
}
}
}
public static void SetChanged()
{
changed = true;
}
private static void UpdateActionSetsArray()
{
List<VRActiveActionSet_t> activeActionSetsList = new List<VRActiveActionSet_t>();
SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();
for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
{
SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];
for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
{
SteamVR_Input_Sources source = sources[sourceIndex];
if (set.ReadRawSetActive(source))
{
VRActiveActionSet_t activeSet = new VRActiveActionSet_t();
activeSet.ulActionSet = set.handle;
activeSet.nPriority = set.ReadRawSetPriority(source);
activeSet.ulRestrictedToDevice = SteamVR_Input_Source.GetHandle(source);
int insertionIndex = 0;
for (insertionIndex = 0; insertionIndex < activeActionSetsList.Count; insertionIndex++)
{
if (activeActionSetsList[insertionIndex].nPriority > activeSet.nPriority)
break;
}
activeActionSetsList.Insert(insertionIndex, activeSet);
}
}
}
changed = false;
rawActiveActionSetArray = activeActionSetsList.ToArray();
if (Application.isEditor || updateDebugTextInBuilds)
UpdateDebugText();
}
public static SteamVR_ActionSet GetSetFromHandle(ulong handle)
{
for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
{
SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];
if (set.handle == handle)
return set;
}
return null;
}
public static string debugActiveSetListText;
public static bool updateDebugTextInBuilds = false;
private static void UpdateDebugText()
{
StringBuilder stringBuilder = new StringBuilder();
for (int activeIndex = 0; activeIndex < rawActiveActionSetArray.Length; activeIndex++)
{
VRActiveActionSet_t set = rawActiveActionSetArray[activeIndex];
stringBuilder.Append(set.nPriority);
stringBuilder.Append("\t");
stringBuilder.Append(SteamVR_Input_Source.GetSource(set.ulRestrictedToDevice));
stringBuilder.Append("\t");
stringBuilder.Append(GetSetFromHandle(set.ulActionSet).GetShortName());
stringBuilder.Append("\n");
}
debugActiveSetListText = stringBuilder.ToString();
}
}
}