/// <summary> Checks for any new events that have come in from the SenseGloveCs library. </summary> /// <remarks> Events are queued and fired here so that they are Unity-Safe: They could be fired from asynchronous worker threads, /// and certain Unity functions are only accessible from this main thread. </remarks> private void CheckEventQueue() { //New gloves detected since the last Update if (this.queuedGloves.Count > 0) { for (int i = 0; i < this.queuedGloves.Count; i++) { SenseGlove detectedGlove = this.queuedGloves[i]; //keep a refrence here, since the RemoveAt may make it go out of scope. Debug.Log("Found a new unattended Glove: " + detectedGlove.DeviceID()); this.AssignNewGlove(detectedGlove); } this.queuedGloves.Clear(); //clear after we're done(?) } }
/// <summary> Attempt to link a new glove to one of our assigned SenseGlove_Objects. Fire a GloveDetected event afterwards.? </summary> /// <param name="glove"></param> private void AssignNewGlove(SenseGlove glove) { if (GetGloveIndex(glove.DeviceID()) < 0) //It is a new glove { SenseGlove_DeviceManager.detectedGloves.Add(glove); SenseGlove_DeviceManager.gloveLinked.Add(false); int index = detectedGloves.Count - 1; bool linked = false; bool isRight = glove.IsRight(); //attempt to assign it to existing SenseGlove_Objects? for (int i = 0; i < this.senseGloves.Count; i++) { if (this.senseGloves[i] != null) { if (!this.senseGloves[i].IsLinked && SenseGlove_Object.MatchesConnection(isRight, senseGloves[i].connectionMethod)) { //This Sense Glove is elligible for a connection and is not already connected. bool succesfullLink = this.senseGloves[i].LinkToGlove(index); if (succesfullLink) { //only when it is actually assigned do we break. SenseGlove_DeviceManager.gloveLinked[index] = true; //we linked the glove at Index. linked = true; break; } } } else { //Warn devs when their SenseGlove_Objects have not been assigned. Debug.LogError("NullRefrence exception occured in " + this.name + ". You likely haven't assigned it via the inspector."); } } if (!linked) { this.OnGloveDetected(glove, index); //only fire event if the glove was not assigned. } } else { Debug.LogWarning("GloveDetected event was fired twice for the same device. Most likely you have two instances of DeviceManager running. " + "We reccommend removing duplicate instances."); } }