Esempio n. 1
0
 public static OSCMessage aliveMessage(ReadOnlyContactCollection contacts)
 {
     OSCMessage message = new OSCMessage("/tuio/2Dblb");
     message.Append("alive");
     for (int i = 0; i < contacts.Count; i++)
     {
         message.Append(contacts[i].Id);
     }
     return message;
 }
    public void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts)
    {
      TimeSpan now = gameTime.TotalRealTime;

      // First process tags that are leaving.
      Queue<ContactData> q = leavingTags;
      ContactData current;
      leavingTags = new Queue<ContactData>();
      while (q.Count > 0)
      {
        current = q.Dequeue();
        if (current.LastSeen + new TimeSpan(0, 0, 0, 1) < now)
        {
          if (ContactRemoved != null) ContactRemoved(this, current);
        }
        else
        {
          Contact returned = FindReturned(contacts, current);
          if (returned != null)
          {
            current.Update(returned, now);
            activeContacts.Add(current);
          }
          else
          {
            leavingTags.Enqueue(current);
          }
        }
      }

      // Now get current contacts.
      foreach (Contact c in contacts)
      {
        bool found = false;
        // Search the collection of active contacts to see if we already know about this one.
        foreach (ContactData d in activeContacts)
        {
          if (c.Id == d.Contact.Id)
          {
            d.Update(c, now);
            found = true;
            break;
          }
        }

        // Not found? It's a new contact, add it and notify event handlers.
        if (!found)
        {
          ContactData d = new ContactData(c, now);
          activeContacts.Add(d);
          if (ContactAdded != null) ContactAdded(this, d);
        }
      }
      // Now check to see if there are any that need removing - these will have an outdated LastSeen.
      int length = activeContacts.Count;
      for (int i = 0; i < length; i++)
      {
        ContactData d = activeContacts[i];
        if (!d.LastSeen.Equals(now))
        {
          activeContacts.RemoveAt(i);
          i--;
          length--;
          if (d.Contact.IsTagRecognized)
          {
            leavingTags.Enqueue(d);
          }
          else
          {
            if (ContactRemoved != null) ContactRemoved(this, d);
          }
        }
      }

      // Finally, check to see if anything in pendingPressed can be moved to pressed.
      length = pendingPressed.Count;
      for (int i = 0; i < length; i++)
      {
        TouchableContactPair p = pendingPressed[i];
        if ((p.Contact.LastSeen - p.Contact.TimeAdded).TotalMilliseconds > 100 &&
          p.Touchable.InRegion(p.Contact.Contact))
        {
          pressed.Add(p);
          pendingPressed.RemoveAt(i);
          i--;
          length--;
          int player = WhichPlayer(p.Contact);
          if (player != -1)
          {
            p.Touchable.Controller.Press(p.Contact, player);
          }
        }
      }
    }
Esempio n. 3
0
 /// <summary>
 /// Called just before DoUpdate if contacts need to be processed.
 /// </summary>
 /// <param name="gameTime">Provides a snapshot of timing values.</param>
 /// <param name="contacts">The collection of contacts that are currently active.</param>
 protected abstract void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts);
 internal static Exception ContactIsNotInCollection(Contact contact, ReadOnlyContactCollection collection)
 {
     return new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ContactIsNotInCollectionException, contact.Id, collection));
 }
    private Contact FindReturned(ReadOnlyContactCollection contacts, ContactData gone)
    {
      foreach (Contact c in contacts)
      {
        // Not a tag, or is already associated with a player.
        if (!c.IsTagRecognized || GetPlayerIdByContact(c) != -1)
        {
          continue;
        }

        for (int i = 0; i < 4; i++)
        {
          if (playerTags[i] == gone.Contact.Id)
          {
            if (Vector2.Distance(new Vector2(c.CenterX, c.CenterY), gone.LastLocation) < 50)
            {
              playerTags[i] = c.Id;
              return c;
            }
            return null;
          }
        }
      }
      return null;
    }
Esempio n. 6
0
        /// <summary>
        /// Sends a /tuio/2Dobj message
        /// The remote Host is specified in the appConfig
        /// The message is built according to TUIO 1.1 specifications (see http://www.tuio.org/?specification)
        /// </summary>
        /// <param name="contacts">Current acitve Contacts</param>
        /// <param name="previousContacts">Contacts from the previous frame</param>
        public void sendTUIO_2DObj(List<Contact> contacts, ReadOnlyContactCollection previousContacts)
        {
            //if (contacts.Count == 0) return;
            InteractiveSurface surface = Microsoft.Surface.Core.InteractiveSurface.DefaultInteractiveSurface;
            double width = surface.Right - surface.Left;
            double height = surface.Bottom - surface.Top;

            OSCBundle objectBundle = new OSCBundle();
            OSCMessage sourceMessage = TUIO_2DObj.sourceMessage();
            objectBundle.Append(sourceMessage);

            OSCMessage aliveMessage = TUIO_2DObj.aliveMessage(contacts);
            objectBundle.Append(aliveMessage);

            for (int i = 0; i < contacts.Count; i++)
            {
                Contact c = contacts[i];
                double x = (c.CenterX-surface.Left)/width;
                double y = (c.CenterY-surface.Top)/height;

                float angularVelocity = 0.0f;
                float angularAcceleration = 0.0f;

                if( previousContacts.Contains( c.Id ))
                {
                    computeAngularVelocity(c, previousContacts.GetContactFromId(c.Id), out angularVelocity, out angularAcceleration);
                }

                float X = 0.0f;
                float Y = 0.0f;
                getVelocity(c.Id, out X, out Y, surface);
                float motionAcceleration = 0.0f;
                getMotionAcceleration(c.Id, out motionAcceleration, c.FrameTimestamp);

                if (c.Tag.Type == TagType.Byte)
                {
                    OSCMessage setMessage = TUIO_2DObj.setMessage(c.Id, (int)c.Tag.Byte.Value, (float)x, (float)y, (float)c.Orientation, X, Y, angularVelocity, motionAcceleration, angularAcceleration);
                    objectBundle.Append(setMessage);
                }
                else if (c.Tag.Type == TagType.Identity)
                {
                    OSCMessage setMessage = TUIO_2DObj.setMessage(c.Id, (int)c.Tag.Identity.Value, (float)x, (float)y, (float)c.Orientation, X, Y, angularVelocity, motionAcceleration, angularAcceleration);
                    objectBundle.Append(setMessage);
                }

            }
            OSCMessage frameMessage = TUIO_2DObj.frameMessage(_Frame);
            objectBundle.Append(frameMessage);
            _Frame++;
            _OSCSender.Send(objectBundle);
        }
Esempio n. 7
0
        /// <summary>
        /// Main application Logic
        /// We retrieve the current Contacts and send TUIO messages accordingly
        /// You can specify via appConfig if you want a Finger Contact to be sent as /tuio/2Dcur or /tuio/2Dblb
        /// Sending it as /tuio/2Dblb conserves the Contact size, rotation, angular velocity and angular acceleration
        /// </summary>
        protected override void Update(GameTime gameTime)
        {
            if (isApplicationActivated || isApplicationPreviewed)
            {

                _contactManipulationData.Clear();

                // Want to identify all the contacts added or removed since the last update.
                List<Contact> addedContacts = new List<Contact>();
                List<Contact> removedContacts = new List<Contact>();
                List<Contact> changedContacts = new List<Contact>();
                List<Contact> aliveContacts = new List<Contact>();

                List<Contact> addedFingers = new List<Contact>();
                List<Contact> removedFingers = new List<Contact>();
                List<Contact> changedFingers = new List<Contact>();
                List<Contact> aliveFingers = new List<Contact>();

                List<Contact> addedTags = new List<Contact>();
                List<Contact> removedTags = new List<Contact>();
                List<Contact> changedTags = new List<Contact>();
                List<Contact> aliveTags = new List<Contact>();

                List<Contact> addedBlobs = new List<Contact>();
                List<Contact> removedBlobs = new List<Contact>();
                List<Contact> changedBlobs= new List<Contact>();
                List<Contact> aliveBlobs = new List<Contact>();

                ReadOnlyContactCollection currentContacts = contactTarget.GetState();

                // Write all unactive previous Contacts into the according removed-Lists
                if (previousContacts != null) {
                    foreach (Contact contact in previousContacts) {
                        Contact c = null;
                        currentContacts.TryGetContactFromId(contact.Id, out c);
                        if (c == null) {
                            removedContacts.Add(contact);
                            if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                                removedFingers.Add(contact);
                            }
                            if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                                removedBlobs.Add(contact);
                            } else if (contact.IsTagRecognized) {
                                removedTags.Add(contact);
                            } else {
                                removedBlobs.Add(contact);
                            }
                        }
                    }

                    // Throw away unused Manipulation Processors
                    cleanManipulationProcessorList(removedContacts);

                    foreach (Contact contact in currentContacts) {
                        aliveContacts.Add(contact);

                        // Put the Contact into the according List
                        if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                            aliveFingers.Add(contact);
                        }
                        if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                            aliveBlobs.Add(contact);
                        } else if (contact.IsTagRecognized) {
                            aliveTags.Add(contact);
                        } else {
                            aliveBlobs.Add(contact);
                        }
                        Contact c = null;
                        previousContacts.TryGetContactFromId(contact.Id, out c);
                        if (c != null) {
                            if (c.ToString() != contact.ToString()) {
                                changedContacts.Add(contact);

                                // Invoke the processing of a Contact's Manipulation Processor
                                processContactManipulator(contact, currentContacts, previousContacts);

                                // Put the Contact into the according List
                                if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                                    changedFingers.Add(contact);
                                }
                                if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                                    changedBlobs.Add(contact);
                                } else if (contact.IsTagRecognized) {
                                    changedTags.Add(contact);
                                } else {
                                    changedBlobs.Add(contact);
                                }
                            }
                        } else {
                            addedContacts.Add(contact);
                            // Add a Manipulation Processor to each contact
                            // This is done for extracting the contact velocity directly from Surface SDK
                            addManipulationProcessor(contact);
                            if (contact.IsFingerRecognized &&  (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                                addedFingers.Add(contact);
                            }
                            if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) {
                                addedBlobs.Add(contact);
                            } else if (contact.IsTagRecognized) {
                                addedTags.Add(contact);
                            } else {
                                addedBlobs.Add(contact);
                            }
                        }
                    }

                    // Send the TUIO messages
                    //sendTUIO_2DCur(changedFingers, previousContacts);
                    //sendTUIO_2DObj(changedTags, previousContacts);
                    //sendTUIO_2DBlb(changedBlobs, previousContacts);

                    if (addedFingers.Count != 0 || changedFingers.Count != 0 || removedFingers.Count != 0) {
                        sendTUIO_2DCur(aliveFingers, previousContacts);
                    }
                    if (addedTags.Count != 0 || changedTags.Count != 0 || removedTags.Count != 0) {
                        sendTUIO_2DObj(aliveTags, previousContacts);
                    }
                } else {
                    foreach (Contact c in currentContacts) {
                        addedContacts.Add(c);

                        if (c.IsFingerRecognized) {
                            addedFingers.Add(c);
                        }
                        if (c.IsTagRecognized) {
                            addedTags.Add(c);
                        }
                    }
                }
                previousContacts = currentContacts;

                foreach (Contact c in changedContacts) {
                    updateHistoryData(c.Id, c.FrameTimestamp);
                }
                foreach (Contact c in removedContacts) {
                    cleanHistory(c.Id);
                }
            }
            base.Update(gameTime);
        }
Esempio n. 8
0
        /// <summary>
        /// Sends a /tuio/2Dcur message
        /// The remote Host is specified in the appConfig
        /// The message is built according to TUIO 1.1 specifications (see http://www.tuio.org/?specification)
        /// </summary>
        /// <param name="contacts">Current active contacts</param>
        /// <param name="previousContacts">Contacts from the previous frame</param>
        public void sendTUIO_2DCur(List<Contact> contacts, ReadOnlyContactCollection previousContacts)
        {
            //if (contacts.Count == 0) return;
            InteractiveSurface surface = Microsoft.Surface.Core.InteractiveSurface.DefaultInteractiveSurface;
            double width = surface.Right - surface.Left;
            double height = surface.Bottom - surface.Top;

            OSCBundle cursorBundle = new OSCBundle();
            OSCMessage sourceMessage = TUIO_2DCur.sourceMessage();
            cursorBundle.Append(sourceMessage);

            OSCMessage aliveMessage = TUIO_2DCur.aliveMessage(contacts);
            cursorBundle.Append(aliveMessage);

            for (int i = 0; i < contacts.Count; i++)
            {
                Contact c = contacts[i];

                double x = (c.CenterX - surface.Left) / width;
                double y = (c.CenterY - surface.Top) / height;

                float X = 0.0f;
                float Y = 0.0f;
                getVelocity(c.Id, out X, out Y, surface);
                float motionAcceleration = 0.0f;
                getMotionAcceleration(c.Id, out motionAcceleration, c.FrameTimestamp);

                OSCMessage setMessage = TUIO_2DCur.setMessage(c.Id, (float)x, (float)y, X, Y, motionAcceleration);
                cursorBundle.Append(setMessage);

            }
            OSCMessage frameMessage = TUIO_2DCur.frameMessage(_Frame);
            cursorBundle.Append(frameMessage);
            _Frame++;
            _OSCSender.Send(cursorBundle);
        }
Esempio n. 9
0
        /// <summary>
        /// Sends a /tuio/2Dblb message
        /// The remote Host is specified in the appConfig
        /// The message is built according to TUIO 1.1 specifications (see http://www.tuio.org/?specification)
        /// </summary>
        /// <param name="contacts">Current active contacts</param>
        /// <param name="previousContacts">Contacts from the previous frame</param>
        public void sendTUIO_2DBlb(List<Contact> contacts, ReadOnlyContactCollection previousContacts)
        {
            //if (contacts.Count == 0) return;
            InteractiveSurface surface = Microsoft.Surface.Core.InteractiveSurface.DefaultInteractiveSurface;
            double width = surface.Right - surface.Left;
            double height = surface.Bottom - surface.Top;

            OSCBundle blobBundle = new OSCBundle();
            OSCMessage sourceMessage = TUIO_2DBlb.sourceMessage();
            blobBundle.Append(sourceMessage);

            OSCMessage aliveMessage = TUIO_2DBlb.aliveMessage(contacts);
            blobBundle.Append(aliveMessage);

            for (int i = 0; i < contacts.Count; i++)
            {
                Contact c = contacts[i];
                double x = (c.CenterX - surface.Left) / width;
                double y = (c.CenterY - surface.Top) / height;
                double w = c.MajorAxis/surface.Width;
                double h = c.MinorAxis/surface.Height;
                double f = c.PhysicalArea / (surface.Width * surface.Height);

                float angularVelocity = 0.0f;
                float angularAcceleration = 0.0f;

                if (previousContacts.Contains(c.Id))
                {
                    computeAngularVelocity(c, previousContacts.GetContactFromId(c.Id), out angularVelocity, out angularAcceleration);
                }

                float X = 0.0f;
                float Y = 0.0f;
                getVelocity(c.Id, out X, out Y, surface);

                float motionAcceleration = 0.0f;
                getMotionAcceleration(c.Id, out motionAcceleration, c.FrameTimestamp);

                OSCMessage setMessage = TUIO_2DBlb.setMessage(c.Id, (float)x, (float)y, (float)c.Orientation, (float)w, (float)h, (float)(w * h), X, Y, angularVelocity, motionAcceleration, angularAcceleration);
                blobBundle.Append(setMessage);
            }
            OSCMessage frameMessage = TUIO_2DBlb.frameMessage(_Frame);
            blobBundle.Append(frameMessage);
            _Frame++;
            _OSCSender.Send(blobBundle);
        }
Esempio n. 10
0
        public void processContactManipulator(Contact c, ReadOnlyContactCollection currentContacts, ReadOnlyContactCollection previousContacts)
        {
            List<Manipulator> removedManipulators = new List<Manipulator>();

            if (previousContacts != null)
            {
                // Find all the contacts that were removed since the last check.
                foreach (Contact contact in previousContacts)
                {
                    // Create a temporary variable for the following method call.
                    Contact tempContact;

                    // Determine if the Contact object from the previous list is in the current list.
                    if (!currentContacts.TryGetContactFromId(contact.Id, out tempContact))
                    {
                        // The contact was not found in the list of current contacts, so it has been removed.

                        // Copy the Contact object information to a new Manipulator object and add
                        // the Manipulator object to the removedManipulators list.
                        removedManipulators.Add(new Manipulator(contact.Id, contact.X, contact.Y));
                    }
                }
            }

            List<Manipulator> currentManipulator = new List<Manipulator>();
            currentManipulator.Add(new Manipulator(c.Id, c.X, c.Y));
            if (_contactProcessors.ContainsKey(c.Id))
            {
                _contactProcessors[c.Id].ProcessManipulators(currentManipulator, removedManipulators);
            }
        }
Esempio n. 11
0
            public void Frame()
            {
                if (openCOVER != null)
                {
                    // Want to identify all the contacts added or removed since the last update.
                    List<Contact> addedContacts = new List<Contact>();
                    List<Contact> removedContacts = new List<Contact>();
                    List<Contact> changedContacts = new List<Contact>();

                    // Get a list of the current contacts
                    ReadOnlyContactCollection currentContacts = contactTarget.GetState();

                    // Compare the contacts in the current list to the list saved from the last update
                    if (previousContacts != null)
                    {
                        foreach (Contact contact in previousContacts)
                        {
                            Contact c = null;
                            currentContacts.TryGetContactFromId(contact.Id, out c);
                            if (c == null)
                            {
                                removedContacts.Add(contact);
                                if(contact.IsFingerRecognized)
                                removedManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY));
                            }
                        }
                        foreach (Contact contact in currentContacts)
                        {
                            Contact c = null;
                            previousContacts.TryGetContactFromId(contact.Id, out c);
                            if (c != null)
                            {
                                changedContacts.Add(contact);
                                if (contact.IsFingerRecognized)
                                currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY));
                            }
                            else
                            {
                                addedContacts.Add(contact);
                                if (contact.IsFingerRecognized)
                                currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY));
                            }
                        }
                    }
                    else
                    {
                        foreach (Contact c in currentContacts)
                        {
                            addedContacts.Add(c);
                            if (c.IsFingerRecognized)
                            currentManipulators.Add(new Manipulator(c.Id, c.CenterX, c.CenterY));
                        }
                    }

                    manipulationProcessor.ProcessManipulators(currentManipulators, removedManipulators);

                    currentManipulators.Clear();
                    removedManipulators.Clear();

                    previousContacts = currentContacts;

                    // Hit test and assign all new contacts
                    foreach (Contact c in addedContacts)
                    {
                        openCOVER.addedContact(c);
                    }

                    // Update the captors of all the pre-existing contacts
                    foreach (Contact c in changedContacts)
                    {
                        openCOVER.changedContact(c);
                    }

                    // Clean up all old contacts
                    foreach (Contact co in removedContacts)
                    {
                        openCOVER.removedContact(co);
                    }
                    openCOVER.frame();

                }
            }
Esempio n. 12
0
 protected override void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts)
 {
   contactParser.ProcessContacts(gameTime, contacts);
 }