Tilt Five Unity API  1.3.0
Classes | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
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. More...
 
static Quaternion GetRotation (ControllerIndex controllerIndex=ControllerIndex.Right, PlayerIndex playerIndex=PlayerIndex.One)
 Gets the rotation of the wand in world space. More...
 
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. More...
 

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. More...
 
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. More...
 
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. More...
 
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. More...
 
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 54 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 303 of file Wand.cs.

307  {
308  if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
309  || !Instance.wandCores.TryGetValue(glassesHandle, out var wandPair)
310  || !wandPair.TryGet(controllerIndex, out var wandCore))
311  {
312  return Vector3.zero;
313  }
314 
315  switch (controllerPosition)
316  {
317  case ControllerPosition.Fingertips:
318  return wandCore.fingertipsPose_UnityWorldSpace.position;
319  case ControllerPosition.Aim:
320  return wandCore.aimPose_UnityWorldSpace.position;
321  case ControllerPosition.Grip:
322  return wandCore.Pose_UnityWorldSpace.position;
323  default:
324  return Vector3.zero;
325  }
326  }
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 334 of file Wand.cs.

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

References TiltFive.Singleton< Wand >.Instance.

◆ IsTracked()

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

Definition at line 346 of file Wand.cs.

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

References TiltFive.Singleton< Wand >.Instance.

◆ ObtainWandCore()

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

Definition at line 603 of file Wand.cs.

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

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

◆ ScanForWands()

static void TiltFive.Wand.ScanForWands ( )
static

Definition at line 203 of file Wand.cs.

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

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

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 394 of file Wand.cs.

395  {
397  {
398  try
399  {
400  T5_Bool wandAvailable = false;
401  int result = NativePlugin.GetWandAvailability(glassesHandle, ref wandAvailable, controllerIndex);
402 
403  if (result == 0)
404  {
405  connected = wandAvailable;
406  return true;
407  }
408  }
409  catch (DllNotFoundException e)
410  {
411  Log.Info("Could not connect to Tilt Five plugin for wand: {0}", e.Message);
413  }
414  catch (Exception e)
415  {
416  Log.Error(
417  "Failed to connect to Tilt Five plugin for wand availability: {0}",
418  e.ToString());
420  }
421  }
422 
423  connected = false;
424  return false;
425  }
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:99

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 169 of file Wand.cs.

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

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 151 of file Wand.cs.

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

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

Member Data Documentation

◆ currentFrame

int TiltFive.Wand.currentFrame = -1
staticprivate

Definition at line 103 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 73 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 69 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 82 of file Wand.cs.

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

◆ incomingHandles

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

Definition at line 86 of file Wand.cs.

Referenced by TiltFive.Wand.ScanForWands().

◆ lastScanAttempt

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

Definition at line 94 of file Wand.cs.

Referenced by TiltFive.Wand.TryScanForWands().

◆ lostHandles

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

Definition at line 88 of file Wand.cs.

Referenced by TiltFive.Wand.ScanForWands().

◆ lostWands

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

Definition at line 90 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 61 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 97 of file Wand.cs.

Referenced by TiltFive.Wand.TryScanForWands().


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