SteamVR_Behaviour_Single.cs
5.98 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
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
namespace Valve.VR
{
/// <summary>
/// SteamVR_Behaviour_Single simplifies the use of single actions. It gives an event to subscribe to for when the action has changed.
/// </summary>
public class SteamVR_Behaviour_Single : MonoBehaviour
{
/// <summary>The single action to get data from.</summary>
public SteamVR_Action_Single singleAction;
/// <summary>The device this action applies to. Any if the action is not device specific.</summary>
[Tooltip("The device this action should apply to. Any if the action is not device specific.")]
public SteamVR_Input_Sources inputSource;
/// <summary>Unity event that Fires whenever the action's value has changed since the last update.</summary>
[Tooltip("Fires whenever the action's value has changed since the last update.")]
public SteamVR_Behaviour_SingleEvent onChange;
/// <summary>Unity event that Fires whenever the action's value has been updated</summary>
[Tooltip("Fires whenever the action's value has been updated.")]
public SteamVR_Behaviour_SingleEvent onUpdate;
/// <summary>Unity event that Fires whenever the action's value has been updated and is non-zero</summary>
[Tooltip("Fires whenever the action's value has been updated and is non-zero.")]
public SteamVR_Behaviour_SingleEvent onAxis;
/// <summary>C# event that fires whenever the action's value has changed since the last update.</summary>
public ChangeHandler onChangeEvent;
/// <summary>C# event that fires whenever the action's value has been updated</summary>
public UpdateHandler onUpdateEvent;
/// <summary>C# event that fires whenever the action's value has been updated and is non-zero</summary>
public AxisHandler onAxisEvent;
/// <summary>Returns whether this action is bound and the action set is active</summary>
public bool isActive { get { return singleAction.GetActive(inputSource); } }
protected virtual void OnEnable()
{
if (singleAction == null)
{
Debug.LogError("[SteamVR] Single action not set.", this);
return;
}
AddHandlers();
}
protected virtual void OnDisable()
{
RemoveHandlers();
}
protected void AddHandlers()
{
singleAction[inputSource].onUpdate += SteamVR_Behaviour_Single_OnUpdate;
singleAction[inputSource].onChange += SteamVR_Behaviour_Single_OnChange;
singleAction[inputSource].onAxis += SteamVR_Behaviour_Single_OnAxis;
}
protected void RemoveHandlers()
{
if (singleAction != null)
{
singleAction[inputSource].onUpdate -= SteamVR_Behaviour_Single_OnUpdate;
singleAction[inputSource].onChange -= SteamVR_Behaviour_Single_OnChange;
singleAction[inputSource].onAxis -= SteamVR_Behaviour_Single_OnAxis;
}
}
private void SteamVR_Behaviour_Single_OnUpdate(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta)
{
if (onUpdate != null)
{
onUpdate.Invoke(this, fromSource, newAxis, newDelta);
}
if (onUpdateEvent != null)
{
onUpdateEvent.Invoke(this, fromSource, newAxis, newDelta);
}
}
private void SteamVR_Behaviour_Single_OnChange(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta)
{
if (onChange != null)
{
onChange.Invoke(this, fromSource, newAxis, newDelta);
}
if (onChangeEvent != null)
{
onChangeEvent.Invoke(this, fromSource, newAxis, newDelta);
}
}
private void SteamVR_Behaviour_Single_OnAxis(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta)
{
if (onAxis != null)
{
onAxis.Invoke(this, fromSource, newAxis, newDelta);
}
if (onAxisEvent != null)
{
onAxisEvent.Invoke(this, fromSource, newAxis, newDelta);
}
}
/// <summary>
/// Gets the localized name of the device that the action corresponds to.
/// </summary>
/// <param name="localizedParts">
/// <list type="bullet">
/// <item><description>VRInputString_Hand - Which hand the origin is in. E.g. "Left Hand"</description></item>
/// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand.E.g. "Vive Controller"</description></item>
/// <item><description>VRInputString_InputSource - What part of that controller is the origin. E.g. "Trackpad"</description></item>
/// <item><description>VRInputString_All - All of the above. E.g. "Left Hand Vive Controller Trackpad"</description></item>
/// </list>
/// </param>
public string GetLocalizedName(params EVRInputStringBits[] localizedParts)
{
if (singleAction != null)
return singleAction.GetLocalizedOriginPart(inputSource, localizedParts);
return null;
}
public delegate void AxisHandler(SteamVR_Behaviour_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
public delegate void ChangeHandler(SteamVR_Behaviour_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
public delegate void UpdateHandler(SteamVR_Behaviour_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
}
}