2using System.Collections.Generic;
6#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
7using UnityEngine.InputSystem;
17 #region Private Fields
19 private Dictionary<PlayerIndex, PlayerCore>
players =
new Dictionary<PlayerIndex, PlayerCore>();
20 internal static bool scanningForPlayers =
false;
25 #region Public Functions
34 return playerIndex != PlayerIndex.None &&
Instance.players.TryGetValue(playerIndex, out var playerCore);
46 if (playerIndex ==
PlayerIndex.None || !
Instance.players.TryGetValue(playerIndex, out var playerCore))
52 friendlyName = playerCore.FriendlyName;
59 #region Internal Functions
67 if (scanningForPlayers)
72 if (playerSettings !=
null &&
Instance.players.TryGetValue(playerSettings.
PlayerIndex, out var playerCore))
80 Log.
Info($
"Player {playerIndex} was removed due to their glasses (handle: {playerCore.GlassesHandle}) being disconnected.");
81 Instance.players.Remove(playerIndex);
85 playerCore.Update(playerSettings, spectatorSettings);
97 if (playerSettings !=
null &&
Instance.players.TryGetValue(playerIndex, out var playerCore))
101 playerCore.Reset(playerSettings, spectatorSettings);
105 Validate(playerSettings);
116 if (playerSettings ==
null)
131 internal static void ScanForNewPlayers()
133 if (scanningForPlayers)
137 scanningForPlayers =
true;
141 scanningForPlayers =
false;
144 internal static bool TryGetGlassesHandle(
PlayerIndex playerIndex, out GlassesHandle glassesHandle)
148 glassesHandle =
new GlassesHandle();
151 var playerAvailable =
Instance.players.TryGetValue(playerIndex, out var playerCore);
152 glassesHandle = playerCore?.GlassesHandle ??
new GlassesHandle();
153 return playerAvailable;
156 internal static bool TryGetPlayerIndex(GlassesHandle glassesHandle, out
PlayerIndex playerIndex)
158 foreach (var keyValuePair
in Instance.players)
160 var currentPlayerIndex = keyValuePair.Key;
161 var currentPlayerCore = keyValuePair.Value;
162 if (currentPlayerCore.GlassesHandle == glassesHandle)
164 playerIndex = currentPlayerIndex;
172 internal static bool AllSupportedPlayersConnected()
178 var supportedPlayers = sceneInfo.GetSupportedPlayerCount();
196 internal static bool TryAddPlayer(GlassesHandle glassesHandle, out
PlayerIndex playerIndex)
205 if(TryGetPlayerIndex(glassesHandle, out var existingPlayerIndex))
221 if (!
players.ContainsKey(currentPlayerIndex))
224 players[currentPlayerIndex] =
new PlayerCore(glassesHandle, friendlyName);
226 Log.
Info($
"Player {currentPlayerIndex} created. Glasses: {glassesHandle} (\"{friendlyName}\")");
233 bool lowestPlayerIndex =
false;
238 if (lowestPlayerIndex)
240 Glasses.SetDefaultGlassesHandle(glassesHandle);
243 playerIndex = currentPlayerIndex;
253 internal static void OnEnable(uint supportedPlayers)
255#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
256 var glassesDevices =
Input.glassesDevices;
257 var wandDevices =
Input.wandDevices;
263 var i = (int)playerIndex - 1;
264 if ((uint)playerIndex > supportedPlayers)
267 if (glassesDevices[i] !=
null)
269 InputSystem.RemoveDevice(glassesDevices[i]);
270 glassesDevices[i] =
null;
274 if (wandDevices[i, leftIndex] !=
null)
276 InputSystem.RemoveDevice(wandDevices[i, leftIndex]);
277 wandDevices[i, leftIndex] =
null;
279 if (wandDevices[i, rightIndex] !=
null)
281 InputSystem.RemoveDevice(wandDevices[i, rightIndex]);
282 wandDevices[i, rightIndex] =
null;
288 Input.AddGlassesDevice(playerIndex);
293 InputSystem.QueueConfigChangeEvent(wandDevices[i, leftIndex]);
294 InputSystem.QueueConfigChangeEvent(wandDevices[i, rightIndex]);
297 glassesDevices[i].LeftWand = wandDevices[i, leftIndex];
298 glassesDevices[i].RightWand = wandDevices[i, rightIndex];
301 if(Application.isPlaying)
305 if (glassesDevices[i] !=
null)
307 InputSystem.DisableDevice(glassesDevices[i]);
309 if (wandDevices[i, leftIndex] !=
null)
311 InputSystem.DisableDevice(wandDevices[i, leftIndex]);
313 if (wandDevices[i, rightIndex] !=
null)
315 InputSystem.DisableDevice(wandDevices[i, rightIndex]);
322 internal static void OnDisable()
328 internal static bool TryGetFirstConnectedPlayer(out
PlayerIndex playerIndex)
351 if (playerSettings ==
null)
358 if (gameBoardSettings.currentGameBoard !=
null)
368 #region Private Classes
GameBoard currentGameBoard
The game board is the window into the game world, as well as the origin about which the glasses/wand ...
The Glasses API and runtime.
static void Reset(GlassesSettings glassesSettings, SpectatorSettings spectatorSettings=null, PlayerIndex playerIndex=PlayerIndex.None)
Reset this T:TiltFive.Glasses.
static bool TryGetFriendlyName(PlayerIndex playerIndex, out string friendlyName)
static void Update(GlassesSettings glassesSettings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings)
Updates this T:TiltFive.Glasses.
static bool IsConnected(PlayerIndex playerIndex=PlayerIndex.One)
Indicate if the specified glasses are connected.
static bool Validate(GlassesSettings glassesSettings, SpectatorSettings spectatorSettings=null, PlayerIndex playerIndex=PlayerIndex.One)
Validates the specified glassesSettings with the specified glasses core.
static void ScanForGlasses()
GlassesSettings encapsulates all configuration data used by the Glasses' tracking runtime to compute ...
static void Info(string m, params object[] list)
INFO logging function call.
readonly string FriendlyName
PlayerCore(GlassesHandle glassesHandle, string friendlyName)
readonly GlassesHandle GlassesHandle
void GetLatestPoseData(PlayerSettings playerSettings, SpectatorSettings spectatorSettings)
Obtains the latest pose for all trackable objects.
Provides access to player settings and functionality.
Dictionary< PlayerIndex, PlayerCore > players
static bool IsConnected(PlayerIndex playerIndex)
Determines whether the specified player has an associated pair of glasses connected.
static bool TryGetFriendlyName(PlayerIndex playerIndex, out string friendlyName)
Attempts to get the friendly name assigned to the specified player's glasses.
ScaleSettings scaleSettings
GameBoardSettings gameboardSettings
WandSettings leftWandSettings
GlassesSettings glassesSettings
WandSettings rightWandSettings
ScaleSettings contains the scale data used to translate between Unity units and the user's physical s...
const float MIN_CONTENT_SCALE_RATIO
static bool TryGetISceneInfo(out ISceneInfo sceneInfo)
The Wand API and runtime.
static void ScanForWands()
static void Update(WandSettings wandSettings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings, PlayerIndex playerIndex=PlayerIndex.One)
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)