Tilt Five™ Unity API  1.3.0
 
Loading...
Searching...
No Matches
WandDevice.cs
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 Tilt Five, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
18using UnityEditor;
19using UnityEngine;
20using UnityEngine.InputSystem;
21using UnityEngine.InputSystem.Controls;
22using UnityEngine.InputSystem.Layouts;
23using UnityEngine.InputSystem.LowLevel;
24using UnityEngine.InputSystem.Utilities;
25
26namespace TiltFive
27{
28 public struct WandState : IInputStateTypeInfo
29 {
30 public FourCC format => new FourCC('T', '5');
31
32 [InputControl(name = "TiltFive", layout = "Button", bit = 0)]
33 public bool TiltFive;
34
35 [InputControl(name = "One", layout = "Button", bit = 0)]
36 public bool One;
37
38 [InputControl(name = "Two", layout = "Button", bit = 0)]
39 public bool Two;
40
41 [InputControl(name = "A", layout = "Button", bit = 0)]
42 public bool A;
43
44 [InputControl(name = "B", layout = "Button", bit = 0)]
45 public bool B;
46
47 [InputControl(name = "X", layout = "Button", bit = 0)]
48 public bool X;
49
50 [InputControl(name = "Y", layout = "Button", bit = 0)]
51 public bool Y;
52
53 [InputControl(name = "Three", aliases = new string[]
54 { "thumbstickClick", "JoystickOrPadPressed", "thumbstickClicked", "thumbstickpressed", "primary2DAxisClick" },
55 layout = "Button", bit = 0)]
56 public bool Three;
57
58 [InputControl(name = "Stick", aliases = new string[] { "Joystick", "Thumbstick"}, layout = "Stick")]
59 [InputControl(name = "Primary2DAxis", synthetic = true, useStateFrom = "Stick", layout = "Vector2")]
60 public Vector2 Stick;
61
62 [InputControl(name = "Trigger", layout = "Axis")]
63 public float Trigger;
64
65 [InputControl(name = "devicePosition", alias = "Position_Grip", layout = "Vector3")]
66 public Vector3 devicePosition;
67
68 [InputControl(name = "Position_Fingertips", layout = "Vector3")]
69 public Vector3 FingertipsPosition;
70
71 [InputControl(name = "Position_Aim", layout = "Vector3")]
72 public Vector3 AimPosition;
73
74 [InputControl(name = "Position_Grip/Raw", layout = "Vector3")]
75 public Vector3 RawGripPosition;
76
77 [InputControl(name = "Position_Fingertips/Raw", layout = "Vector3")]
78 public Vector3 RawFingertipsPosition;
79
80 [InputControl(name = "Position_Aim/Raw", layout = "Vector3")]
81 public Vector3 RawAimPosition;
82
83 [InputControl(name = "deviceRotation", alias = "Rotation", layout = "Quaternion")]
84 public Quaternion deviceRotation;
85
86 [InputControl(name = "Rotation/Raw", layout = "Quaternion")]
87 public Quaternion RawRotation;
88
89 [InputControl(name = "isTracked", layout = "Button")]
90 public bool isTracked;
91
92 [InputControl(name = "trackingState", layout = "Integer")]
93 public int trackingState;
94 }
95
96 [InputControlLayout(stateType = typeof(WandState), displayName = "Tilt Five Wand",
97 commonUsages = new string[] { "LeftHand", "RightHand" }, updateBeforeRender = true)]
98#if UNITY_EDITOR
99 [InitializeOnLoad]
100#endif
101 public class WandDevice : UnityEngine.InputSystem.XR.XRControllerWithRumble
102 {
103 #region Public Fields
104
105 public PlayerIndex playerIndex { get; internal set; }
106 public ControllerIndex ControllerIndex { get; internal set; }
107
108 #endregion
109
110
111 #region Controls
112
113 public ButtonControl TiltFive { get; private set; }
114 public ButtonControl One { get; private set; }
115 public ButtonControl Two { get; private set; }
116 public ButtonControl Three { get; private set; }
117 public ButtonControl A { get; private set; }
118 public ButtonControl B { get; private set; }
119 public ButtonControl X { get; private set; }
120 public ButtonControl Y { get; private set; }
121
122 public StickControl Stick { get; private set; }
123
124 public AxisControl Trigger { get; private set; }
125
126 public new Vector3Control devicePosition { get; private set; }
127 public Vector3Control FingertipsPosition { get; private set; }
128 public Vector3Control AimPosition { get; private set; }
129
130 public Vector3Control RawGripPosition { get; private set; }
131 public Vector3Control RawFingertipsPosition { get; private set; }
132 public Vector3Control RawAimPosition { get; private set; }
133
134 public new QuaternionControl deviceRotation { get; private set; }
135 public QuaternionControl RawRotation { get; private set; }
136
137 public new ButtonControl isTracked { get; private set; }
138 public new IntegerControl trackingState { get; private set; }
139
140 #endregion
141
142
143 #region Overrides
144
145 protected override void FinishSetup()
146 {
147 base.FinishSetup();
148
149 TiltFive = GetChildControl<ButtonControl>("TiltFive");
150 One = GetChildControl<ButtonControl>("One");
151 Two = GetChildControl<ButtonControl>("Two");
152 A = GetChildControl<ButtonControl>("A");
153 B = GetChildControl<ButtonControl>("B");
154 X = GetChildControl<ButtonControl>("X");
155 Y = GetChildControl<ButtonControl>("Y");
156 Three = GetChildControl<ButtonControl>("Three");
157
158 Stick = GetChildControl<StickControl>("Stick");
159
160 Trigger = GetChildControl<AxisControl>("Trigger");
161
162 devicePosition = GetChildControl<Vector3Control>("Position_Grip");
163 FingertipsPosition = GetChildControl<Vector3Control>("Position_Fingertips");
164 AimPosition = GetChildControl<Vector3Control>("Position_Aim");
165
166 RawGripPosition = GetChildControl<Vector3Control>("Position_Grip/Raw");
167 RawFingertipsPosition = GetChildControl<Vector3Control>("Position_Fingertips/Raw");
168 RawAimPosition = GetChildControl<Vector3Control>("Position_Aim/Raw");
169
170 deviceRotation = GetChildControl<QuaternionControl>("Rotation");
171 RawRotation = GetChildControl<QuaternionControl>("Rotation/Raw");
172
173 isTracked = GetChildControl<ButtonControl>("isTracked");
174 trackingState = GetChildControl<IntegerControl>("trackingState");
175 }
176
177 protected override void RefreshConfiguration()
178 {
179 switch (ControllerIndex)
180 {
181 case ControllerIndex.Left:
182 if (usages.Contains(CommonUsages.RightHand))
183 {
184 InputSystem.RemoveDeviceUsage(this, CommonUsages.RightHand);
185 }
186 InputSystem.AddDeviceUsage(this, CommonUsages.LeftHand);
187 break;
188 case ControllerIndex.Right:
189 if(usages.Contains(CommonUsages.LeftHand))
190 {
191 InputSystem.RemoveDeviceUsage(this, CommonUsages.LeftHand);
192 }
193 InputSystem.AddDeviceUsage(this, CommonUsages.RightHand);
194 break;
195 default:
196 break;
197 }
198
199 base.RefreshConfiguration();
200 }
201
202 #endregion
203
204 static WandDevice()
205 {
206 InputSystem.RegisterLayout<WandDevice>(matches: new InputDeviceMatcher()
207 .WithInterface("WandDevice"));
208 }
209 }
210}
211#endif
Definition: Log.cs:21
ControllerIndex
Since wands are all physically identical (they have no "handedness"), it doesn't make sense to addres...
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)