Tilt Five™ Unity API  1.3.0
 
Loading...
Searching...
No Matches
TiltFive.Wand Class Reference

The Wand API and runtime. More...

Inheritance diagram for TiltFive.Wand:
TiltFive.Singleton< Wand >

Classes

class  WandCore
 Internal Wand core runtime. More...
 
struct  WandPair
 

Static Public Member Functions

static void Update (WandSettings wandSettings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings, PlayerIndex playerIndex=PlayerIndex.One)
 
static void ScanForWands ()
 
static Vector3 GetPosition (ControllerIndex controllerIndex=ControllerIndex.Right, ControllerPosition controllerPosition=ControllerPosition.Grip, PlayerIndex playerIndex=PlayerIndex.One)
 Gets the position of the wand in world space.
 
static Quaternion GetRotation (ControllerIndex controllerIndex=ControllerIndex.Right, PlayerIndex playerIndex=PlayerIndex.One)
 Gets the rotation of the wand in world space.
 
static bool IsTracked (ControllerIndex controllerIndex=ControllerIndex.Right, PlayerIndex playerIndex=PlayerIndex.One)
 
static bool TryCheckConnected (out bool connected, PlayerIndex playerIndex, ControllerIndex controllerIndex=ControllerIndex.Right)
 Gets the connection status of the indicated wand.
 

Private Member Functions

WandCore ObtainWandCore (GlassesHandle glassesHandle, ControllerIndex controllerIndex)
 

Static Private Member Functions

static bool TryScanForWands ()
 
static bool TryGetWandAvailability (out bool connected, GlassesHandle glassesHandle, ControllerIndex controllerIndex)
 

Private Attributes

Dictionary< GlassesHandle, WandPairwandCores = new Dictionary<GlassesHandle, WandPair>()
 The collection of WandCores. GlassesHandles are mapped to pairs of right/left WandCores.
 
HashSet< GlassesHandle > incomingHandles = new HashSet<GlassesHandle>()
 
HashSet< GlassesHandle > lostHandles = new HashSet<GlassesHandle>()
 
HashSet< WandCorelostWands = new HashSet<WandCore>()
 

Static Private Attributes

static readonly Vector3 DEFAULT_WAND_POSITION_GAME_BOARD_SPACE = new Vector3(0f, 0.25f, -0.25f)
 The default position of the wand relative to the board.
 
static readonly Vector3 DEFAULT_WAND_HANDEDNESS_OFFSET_GAME_BOARD_SPACE = new Vector3(0.125f, 0f, 0f)
 A left/right offset to the default wand position, depending on handedness.
 
static readonly Quaternion DEFAULT_WAND_ROTATION_GAME_BOARD_SPACE = Quaternion.Euler(new Vector3(-33f, 0f, 0f))
 The default rotation of the wand relative to the board.
 
static DateTime lastScanAttempt = System.DateTime.MinValue
 
static readonly double wandScanRate = 0.5d
 
static bool wandAvailabilityErroredOnce = false
 
static int currentFrame = -1
 

Additional Inherited Members

- Protected Member Functions inherited from TiltFive.Singleton< Wand >
 Singleton ()
 
- Properties inherited from TiltFive.Singleton< Wand >
static T Instance [get]
 

Detailed Description

The Wand API and runtime.

Definition at line 55 of file Wand.cs.

Member Function Documentation

◆ GetPosition()

static Vector3 TiltFive.Wand.GetPosition ( ControllerIndex  controllerIndex = ControllerIndex::Right,
ControllerPosition  controllerPosition = ControllerPosition::Grip,
PlayerIndex  playerIndex = PlayerIndex::One 
)
static

Gets the position of the wand in world space.

Parameters
controllerIndex
controllerPosition
glassesHandleThe specified glasses. If null is provided, this uses the default glasses.
Returns
If the indicated wand is not connected, this returns the zero vector.

Definition at line 304 of file Wand.cs.

308 {
309 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
310 || !Instance.wandCores.TryGetValue(glassesHandle, out var wandPair)
311 || !wandPair.TryGet(controllerIndex, out var wandCore))
312 {
313 return Vector3.zero;
314 }
315
316 switch (controllerPosition)
317 {
318 case ControllerPosition.Fingertips:
319 return wandCore.fingertipsPose_UnityWorldSpace.position;
320 case ControllerPosition.Aim:
321 return wandCore.aimPose_UnityWorldSpace.position;
322 case ControllerPosition.Grip:
323 return wandCore.Pose_UnityWorldSpace.position;
324 default:
325 return Vector3.zero;
326 }
327 }
$ Player
Definition: Player.cs:93
ControllerPosition
Points of interest along the wand controller, such as the handle position or wand tip.

References TiltFive.Singleton< Wand >.Instance.

◆ GetRotation()

static Quaternion TiltFive.Wand.GetRotation ( ControllerIndex  controllerIndex = ControllerIndex::Right,
PlayerIndex  playerIndex = PlayerIndex::One 
)
static

Gets the rotation of the wand in world space.

Parameters
controllerIndex
glassesHandleThe specified glasses. If null is provided, this uses the default glasses.
Returns
If the indicated wand is not connected, this returns the identity quaternion.

Definition at line 335 of file Wand.cs.

336 {
337 if (!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
338 || !Instance.wandCores.TryGetValue(glassesHandle, out var wandPair)
339 || !wandPair.TryGet(controllerIndex, out var wandCore))
340 {
341 return Quaternion.identity;
342 }
343
344 return wandCore.Pose_UnityWorldSpace.rotation;
345 }

References TiltFive.Singleton< Wand >.Instance.

◆ IsTracked()

static bool TiltFive.Wand.IsTracked ( ControllerIndex  controllerIndex = ControllerIndex::Right,
PlayerIndex  playerIndex = PlayerIndex::One 
)
static

Definition at line 347 of file Wand.cs.

348 {
349 if (!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
350 || !Instance.wandCores.TryGetValue(glassesHandle, out var wandPair)
351 || !wandPair.TryGet(controllerIndex, out var wandCore))
352 {
353 return false;
354 }
355
356 return wandCore.IsTracked;
357 }

References TiltFive.Singleton< Wand >.Instance.

◆ ObtainWandCore()

WandCore TiltFive.Wand.ObtainWandCore ( GlassesHandle  glassesHandle,
ControllerIndex  controllerIndex 
)
private

Definition at line 604 of file Wand.cs.

605 {
606 WandCore wandCore = null;
607 var glassesAlreadyMonitored = wandCores.TryGetValue(glassesHandle, out var wandPair);
608
609 // Ask the native plugin whether this wand is currently connected.
610 if (TryGetWandAvailability(out var wandConnected, glassesHandle, controllerIndex) && wandConnected)
611 {
612 // If we're not already monitoring this wand, go ahead and create a corresponding WandCore.
613 if (!glassesAlreadyMonitored || !wandPair.TryGet(controllerIndex, out wandCore))
614 {
615#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
616 wandCore = new WandDeviceCore(glassesHandle, controllerIndex);
617#else
618 wandCore = new WandCore(glassesHandle, controllerIndex);
619#endif
620 }
621 return wandCore;
622 }
623 // If this wand is disconnected and we were previously monitoring it, mark it as disconnected
624 else if(glassesAlreadyMonitored && wandPair.TryGet(controllerIndex, out var lostWandCore))
625 {
626 lostWands.Add(lostWandCore);
627 }
628
629 return wandCore;
630 }
static bool TryGetWandAvailability(out bool connected, GlassesHandle glassesHandle, ControllerIndex controllerIndex)
Definition: Wand.cs:395
Dictionary< GlassesHandle, WandPair > wandCores
The collection of WandCores. GlassesHandles are mapped to pairs of right/left WandCores.
Definition: Wand.cs:62
HashSet< WandCore > lostWands
Definition: Wand.cs:91

References TiltFive.Wand.lostWands, TiltFive.Wand.TryGetWandAvailability(), and TiltFive.Wand.wandCores.

◆ ScanForWands()

static void TiltFive.Wand.ScanForWands ( )
static

Definition at line 204 of file Wand.cs.

205 {
206 // Tell the native plugin to search for wands.
208
209 // Obtain the latest set of connected glasses
210 var connectedGlassesHandles = Glasses.GetAllConnectedGlassesHandles();
211
212 // Add/Remove entries from the wandCores dictionary depending on which glasses appeared/disappeared
213 // ---------------------------------------------------
214 var wandCores = Instance.wandCores;
215 var incomingHandles = Instance.incomingHandles;
216 var lostHandles = Instance.lostHandles;
217 var lostWands = Instance.lostWands;
218
219 incomingHandles.Clear();
220 lostHandles.Clear();
221 lostWands.Clear();
222
223 // Add newly connected wands
224 for (int i = 0; i < connectedGlassesHandles.Length; i++)
225 {
226 var glassesHandle = connectedGlassesHandles[i];
227 incomingHandles.Add(glassesHandle);
228
229 var rightWandCore = Instance.ObtainWandCore(glassesHandle, ControllerIndex.Right);
230 var leftWandCore = Instance.ObtainWandCore(glassesHandle, ControllerIndex.Left);
231
232 // Obtain and store a pair of WandCores to associate with this GlassesHandle.
233 // If either wand is unavailable, we'll just store null.
234 // This will also clear any WandCores that represented a now-disconnected wand.
235 wandCores[glassesHandle] = new WandPair(rightWandCore, leftWandCore);
236 }
237
238 // Prune disconnected wands
239 foreach(var glassesHandle in wandCores.Keys)
240 {
241 // If a pair of glasses disconnects, save its handle for the next foreach loop below
242 if(!incomingHandles.Contains(glassesHandle))
243 {
244 lostHandles.Add(glassesHandle);
245 }
246 // otherwise, check if its wands are still connected.
247 else
248 {
249 if(wandCores.TryGetValue(glassesHandle, out var wandPair))
250 {
251 if(wandPair.TryGet(ControllerIndex.Left, out var leftWandCore)
252 && (!TryGetWandAvailability(out bool leftWandConnected, glassesHandle, ControllerIndex.Left) || !leftWandConnected))
253 {
254 lostWands.Add(leftWandCore);
255 }
256 if (wandPair.TryGet(ControllerIndex.Right, out var rightWandCore)
257 && (!TryGetWandAvailability(out bool rightWandConnected, glassesHandle, ControllerIndex.Right) || !rightWandConnected))
258 {
259 lostWands.Add(rightWandCore);
260 }
261 }
262 }
263 }
264
265 foreach(var lostHandle in lostHandles)
266 {
267 var lostWandPair = wandCores[lostHandle];
268
269 if (lostWandPair.TryGet(ControllerIndex.Right, out var lostRightWand))
270 {
271 lostWands.Add(lostRightWand);
272 }
273
274 if (lostWandPair.TryGet(ControllerIndex.Left, out var lostLeftWand))
275 {
276 lostWands.Add(lostLeftWand);
277 }
278 wandCores.Remove(lostHandle);
279 }
280
281 foreach (var lostWand in lostWands)
282 {
283 lostWand.Dispose();
284 }
285 }
$ Glasses
Definition: Glasses.cs:862
HashSet< GlassesHandle > incomingHandles
Definition: Wand.cs:87
static bool TryScanForWands()
Definition: Wand.cs:170
HashSet< GlassesHandle > lostHandles
Definition: Wand.cs:89
ControllerIndex
Since wands are all physically identical (they have no "handedness"), it doesn't make sense to addres...

References TiltFive.Wand.incomingHandles, TiltFive.Singleton< Wand >.Instance, TiltFive.Wand.lostHandles, TiltFive.Wand.lostWands, TiltFive.Wand.TryGetWandAvailability(), TiltFive.Wand.TryScanForWands(), and TiltFive.Wand.wandCores.

Referenced by TiltFive.Input.Input().

◆ TryCheckConnected()

static bool TiltFive.Wand.TryCheckConnected ( out bool  connected,
PlayerIndex  playerIndex,
ControllerIndex  controllerIndex = ControllerIndex::Right 
)
static

Gets the connection status of the indicated wand.

Parameters
connected
playerIndex
controllerIndex
Returns
Returns false if something went wrong while attempting to check wand connectivity.

Definition at line 366 of file Wand.cs.

367 {
368 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
369 || !Instance.wandCores.TryGetValue(glassesHandle, out var wandPair)
370 || !wandPair.TryGet(controllerIndex, out var wandCore))
371 {
372 connected = false;
373 return false;
374 }
375
376 connected = wandCore.IsConnected;
377 return true;
378 }

References TiltFive.Singleton< Wand >.Instance.

Referenced by TiltFive.Input.GetWandAvailability().

◆ TryGetWandAvailability()

static bool TiltFive.Wand.TryGetWandAvailability ( out bool  connected,
GlassesHandle  glassesHandle,
ControllerIndex  controllerIndex 
)
staticprivate

Definition at line 395 of file Wand.cs.

396 {
398 {
399 try
400 {
401 T5_Bool wandAvailable = false;
402 int result = NativePlugin.GetWandAvailability(glassesHandle, ref wandAvailable, controllerIndex);
403
404 if (result == 0)
405 {
406 connected = wandAvailable;
407 return true;
408 }
409 }
410 catch (DllNotFoundException e)
411 {
412 Log.Info("Could not connect to Tilt Five plugin for wand: {0}", e.Message);
414 }
415 catch (Exception e)
416 {
417 Log.Error(
418 "Failed to connect to Tilt Five plugin for wand availability: {0}",
419 e.ToString());
421 }
422 }
423
424 connected = false;
425 return false;
426 }
The Logger.
Definition: Log.cs:42
static void Info(string m, params object[] list)
INFO logging function call.
Definition: Log.cs:140
static void Error(string m, params object[] list)
ERROR logging function call.
Definition: Log.cs:127
static bool wandAvailabilityErroredOnce
Definition: Wand.cs:100

References TiltFive.Logging.Log.Error(), TiltFive.NativePlugin.GetWandAvailability(), TiltFive.Logging.Log.Info(), and TiltFive.Wand.wandAvailabilityErroredOnce.

Referenced by TiltFive.Wand.ObtainWandCore(), and TiltFive.Wand.ScanForWands().

◆ TryScanForWands()

static bool TiltFive.Wand.TryScanForWands ( )
staticprivate

Definition at line 170 of file Wand.cs.

171 {
172 var currentTime = System.DateTime.Now;
173 var timeSinceLastScan = currentTime - lastScanAttempt;
174
175 // Scan for wands if necessary.
176 // TODO: Implement more robust disconnect detection, communicate wand availability events to users, offer user option to swap wands.
177 if (timeSinceLastScan.TotalSeconds >= wandScanRate
178 && (!Input.GetWandAvailability(ControllerIndex.Right) || !Input.GetWandAvailability(ControllerIndex.Left)))
179 {
180 int result = 1;
181
182 try
183 {
184 result = NativePlugin.ScanForWands();
185 }
186 catch (System.DllNotFoundException e)
187 {
188 Log.Info(
189 "Could not connect to Tilt Five plugin to scan for wands: {0}",
190 e);
191 }
192 catch (Exception e)
193 {
194 Log.Error(e.Message);
195 }
196
197 lastScanAttempt = currentTime;
198 return (0 == result);
199 }
200
201 return false;
202 }
static readonly double wandScanRate
Definition: Wand.cs:98
static DateTime lastScanAttempt
Definition: Wand.cs:95

References TiltFive.Logging.Log.Error(), TiltFive.Input.GetWandAvailability(), TiltFive.Logging.Log.Info(), TiltFive.Wand.lastScanAttempt, TiltFive.NativePlugin.ScanForWands(), and TiltFive.Wand.wandScanRate.

Referenced by TiltFive.Wand.ScanForWands().

◆ Update()

static void TiltFive.Wand.Update ( WandSettings  wandSettings,
ScaleSettings  scaleSettings,
GameBoardSettings  gameBoardSettings,
PlayerIndex  playerIndex = PlayerIndex::One 
)
static

Definition at line 152 of file Wand.cs.

153 {
154 // Update the relevant WandCore
155 if (Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
156 {
157 Update(glassesHandle, wandSettings, scaleSettings, gameBoardSettings);
158 }
159 }
static void Update(WandSettings wandSettings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings, PlayerIndex playerIndex=PlayerIndex.One)
Definition: Wand.cs:152

References TiltFive.Wand.Update().

Referenced by TiltFive.TiltFiveManager.GetLatestPoseData(), TiltFive.Player.PlayerCore.GetLatestPoseData(), and TiltFive.Wand.Update().

Member Data Documentation

◆ currentFrame

int TiltFive.Wand.currentFrame = -1
staticprivate

Definition at line 104 of file Wand.cs.

◆ DEFAULT_WAND_HANDEDNESS_OFFSET_GAME_BOARD_SPACE

readonly Vector3 TiltFive.Wand.DEFAULT_WAND_HANDEDNESS_OFFSET_GAME_BOARD_SPACE = new Vector3(0.125f, 0f, 0f)
staticprivate

A left/right offset to the default wand position, depending on handedness.

Definition at line 74 of file Wand.cs.

Referenced by TiltFive.Wand.WandCore.GetDefaultPoseGameboardSpace().

◆ DEFAULT_WAND_POSITION_GAME_BOARD_SPACE

readonly Vector3 TiltFive.Wand.DEFAULT_WAND_POSITION_GAME_BOARD_SPACE = new Vector3(0f, 0.25f, -0.25f)
staticprivate

The default position of the wand relative to the board.

The wand GameObject will snap back to this position if the glasses and/or wand are unavailable.

Definition at line 70 of file Wand.cs.

Referenced by TiltFive.Wand.WandCore.GetDefaultPoseGameboardSpace().

◆ DEFAULT_WAND_ROTATION_GAME_BOARD_SPACE

readonly Quaternion TiltFive.Wand.DEFAULT_WAND_ROTATION_GAME_BOARD_SPACE = Quaternion.Euler(new Vector3(-33f, 0f, 0f))
staticprivate

The default rotation of the wand relative to the board.

The wand GameObject will snap back to this rotation if the glasses are unavailable. If different behavior is desired in this scenario, a different camera should be used.

Definition at line 83 of file Wand.cs.

Referenced by TiltFive.Wand.WandCore.GetDefaultPoseGameboardSpace().

◆ incomingHandles

HashSet<GlassesHandle> TiltFive.Wand.incomingHandles = new HashSet<GlassesHandle>()
private

Definition at line 87 of file Wand.cs.

Referenced by TiltFive.Wand.ScanForWands().

◆ lastScanAttempt

DateTime TiltFive.Wand.lastScanAttempt = System.DateTime.MinValue
staticprivate

Definition at line 95 of file Wand.cs.

Referenced by TiltFive.Wand.TryScanForWands().

◆ lostHandles

HashSet<GlassesHandle> TiltFive.Wand.lostHandles = new HashSet<GlassesHandle>()
private

Definition at line 89 of file Wand.cs.

Referenced by TiltFive.Wand.ScanForWands().

◆ lostWands

HashSet<WandCore> TiltFive.Wand.lostWands = new HashSet<WandCore>()
private

Definition at line 91 of file Wand.cs.

Referenced by TiltFive.Wand.ObtainWandCore(), and TiltFive.Wand.ScanForWands().

◆ wandAvailabilityErroredOnce

bool TiltFive.Wand.wandAvailabilityErroredOnce = false
staticprivate

◆ wandCores

Dictionary<GlassesHandle, WandPair> TiltFive.Wand.wandCores = new Dictionary<GlassesHandle, WandPair>()
private

The collection of WandCores. GlassesHandles are mapped to pairs of right/left WandCores.

Definition at line 62 of file Wand.cs.

Referenced by TiltFive.Wand.ObtainWandCore(), and TiltFive.Wand.ScanForWands().

◆ wandScanRate

readonly double TiltFive.Wand.wandScanRate = 0.5d
staticprivate

Definition at line 98 of file Wand.cs.

Referenced by TiltFive.Wand.TryScanForWands().


The documentation for this class was generated from the following file: