コード例 #1
0
ファイル: App1.cs プロジェクト: JoeProgram/SurfaceToTUIO
        /// <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 <TouchPoint> addedContacts   = new List <TouchPoint>();
                List <TouchPoint> removedContacts = new List <TouchPoint>();
                List <TouchPoint> changedContacts = new List <TouchPoint>();
                List <TouchPoint> aliveContacts   = new List <TouchPoint>();

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

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

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

                ReadOnlyTouchPointCollection currentContacts = contactTarget.GetState();

                // Write all unactive previous Contacts into the according removed-Lists
                if (previousContacts != null)
                {
                    foreach (TouchPoint contact in previousContacts)
                    {
                        //Console.WriteLine("Touch: " + contact.X + ", " + contact.Y);
                        TouchPoint c = null;
                        currentContacts.TryGetTouchPointFromId(contact.Id, out c);
                        if (c == null)
                        {
                            removedContacts.Add(contact);
                            if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true))
                            {
                                Console.WriteLine("Removing Finger: " + contact.Id);
                                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 (TouchPoint 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);
                        }
                        TouchPoint c = null;
                        previousContacts.TryGetTouchPointFromId(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 ////////////////////////////
                    //Console.WriteLine("Sending...");
                    //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 (TouchPoint c in currentContacts)
                    {
                        addedContacts.Add(c);

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

                foreach (TouchPoint c in changedContacts)
                {
                    updateHistoryData(c.Id, c.FrameTimestamp);
                }
                foreach (TouchPoint c in removedContacts)
                {
                    cleanHistory(c.Id);
                }
            }
            base.Update(gameTime);
        }