public Point getSmoothedPosition(Point relativePosition)
 {
     smoothingBuffer.addValue(new System.Windows.Vector(relativePosition.X, relativePosition.Y));
     System.Windows.Vector smoothedVec = smoothingBuffer.getSmoothedValue();
     return(new Point(smoothedVec.X, smoothedVec.Y));
 }
Exemple #2
0
        public Queue <WiiContact> getFrame()
        {
            Queue <WiiContact> newFrame = new Queue <WiiContact>(1);

            //master
            if (masterPosition != null)
            {
                ContactType contactType;

                if (!this.masterReleased)
                {
                    if (this.masterHovering)
                    {
                        contactType         = ContactType.Start;
                        this.masterHovering = false;
                    }
                    else
                    {
                        contactType = ContactType.Move;
                    }



                    if (this.isFirstMasterContact)
                    {
                        this.firstMasterContact = this.masterPosition;
                    }
                    else
                    {
                        if (this.masterHoldPosition)
                        {
                            if (Math.Abs(this.firstMasterContact.X - this.masterPosition.X) < TouchHoldThreshold && Math.Abs(this.firstMasterContact.Y - this.masterPosition.Y) < TouchHoldThreshold)
                            {
                                /*Console.WriteLine("DiffX: " + Math.Abs(this.firstMasterContact.X - this.masterPosition.X) + " DiffY: " + Math.Abs(this.firstMasterContact.Y - this.masterPosition.Y));*/
                                this.masterPosition     = this.firstMasterContact;
                                this.masterHoldPosition = true;
                            }
                            else
                            {
                                this.masterHoldPosition = false;
                            }
                        }

                        //Helps to perform "edge swipe" guestures
                        if (this.firstMasterContact.X < EdgeHelperMargins && this.masterPosition.X < EdgeHelperRelease) //Left
                        {
                            this.masterPosition.Y = (this.firstMasterContact.Y + this.firstMasterContact.Y + this.masterPosition.Y) / 3;
                        }
                        if (this.firstMasterContact.X > (this.screenBounds.Width - EdgeHelperMargins) && this.masterPosition.X > (this.screenBounds.Width - EdgeHelperRelease)) //Right
                        {
                            this.masterPosition.Y = (this.firstMasterContact.Y + this.firstMasterContact.Y + this.masterPosition.Y) / 3;
                        }
                        if (this.firstMasterContact.Y < EdgeHelperMargins && this.masterPosition.Y < EdgeHelperRelease) //Top
                        {
                            this.masterPosition.X = (this.firstMasterContact.X + this.firstMasterContact.X + this.masterPosition.X) / 3;
                        }
                        if (this.firstMasterContact.Y > (this.screenBounds.Height - EdgeHelperMargins) && this.masterPosition.Y > (this.screenBounds.Height - EdgeHelperRelease)) //Bottom
                        {
                            this.masterPosition.X = (this.firstMasterContact.X + this.firstMasterContact.X + this.masterPosition.X) / 3;
                        }
                    }

                    smoothingBuffer.addValue(new System.Windows.Vector(masterPosition.X, masterPosition.Y));
                    System.Windows.Vector smoothedVec = smoothingBuffer.getSmoothedValue();
                    this.masterPosition.X = smoothedVec.X;
                    this.masterPosition.Y = smoothedVec.Y;

                    this.isFirstMasterContact = false;
                }
                else //Released = hovering
                {
                    if (!this.masterHovering) //End the touch first
                    {
                        if (this.hoverDisabled)
                        {
                            contactType = ContactType.End;
                        }
                        else
                        {
                            contactType = ContactType.EndToHover;
                        }
                        this.masterPosition = lastMasterContact.Position;
                        this.masterHovering = true;
                    }
                    else
                    {
                        contactType = ContactType.Hover;
                        smoothingBuffer.addValue(new Vector(masterPosition.X, masterPosition.Y));
                        Vector smoothedVec = smoothingBuffer.getSmoothedValue();
                        this.masterPosition.X = smoothedVec.X;
                        this.masterPosition.Y = smoothedVec.Y;
                    }

                    this.isFirstMasterContact = true;
                    this.masterHoldPosition   = true;
                }

                if (!(contactType == ContactType.Hover && this.hoverDisabled))
                {
                    if (this.stepIDs && contactType == ContactType.EndToHover) //If we release slave touch before we release master touch we want to make sure Windows treats master as the main touch point again
                    {
                        this.lastMasterContact = new WiiContact(this.masterID, ContactType.End, this.masterPosition, this.masterPriority, new Vector(this.screenBounds.Width, this.screenBounds.Height));
                        this.masterID          = (this.masterID - this.startID + 2) % 4 + this.startID;
                        this.slaveID           = (this.slaveID - this.startID + 2) % 4 + this.startID;
                        this.stepIDs           = false;
                    }
                    else
                    {
                        this.lastMasterContact = new WiiContact(this.masterID, contactType, this.masterPosition, this.masterPriority, new Vector(this.screenBounds.Width, this.screenBounds.Height));
                    }
                    newFrame.Enqueue(this.lastMasterContact);
                }
            }

            //slave
            if (slavePosition != null)
            {
                ContactType contactType;

                if (!this.slaveReleased)
                {
                    if (this.slaveHovering)
                    {
                        contactType        = ContactType.Start;
                        this.slaveHovering = false;
                    }
                    else
                    {
                        contactType = ContactType.Move;
                    }

                    if (!this.masterReleased)
                    {
                        if (!this.usingMidpoint)
                        {
                            this.midpoint      = calculateMidpoint(this.masterPosition, this.slavePosition);
                            this.usingMidpoint = true;
                        }

                        this.slavePosition = reflectThroughMidpoint(this.masterPosition, this.midpoint);

                        if (this.slavePosition.X < 0)
                        {
                            this.slavePosition.X = 0;
                        }
                        if (this.slavePosition.Y < 0)
                        {
                            this.slavePosition.Y = 0;
                        }

                        if (this.slavePosition.X > this.screenBounds.Width)
                        {
                            this.slavePosition.X = this.screenBounds.Width - 1;
                        }
                        if (this.slavePosition.Y > this.screenBounds.Height)
                        {
                            this.slavePosition.Y = this.screenBounds.Height - 1;
                        }
                    }
                    else
                    {
                        this.usingMidpoint = false;
                    }

                    this.slaveEnded = false;
                    this.stepIDs    = false;
                }
                else
                {
                    if (!this.slaveHovering)
                    {
                        contactType        = ContactType.EndToHover;
                        this.slavePosition = lastSlaveContact.Position;
                        this.slaveHovering = true;
                    }
                    else
                    {
                        contactType = ContactType.EndFromHover;
                    }
                }

                if (!this.slaveEnded)
                {
                    this.lastSlaveContact = new WiiContact(this.slaveID, contactType, this.slavePosition, this.slavePriority, new Vector(this.screenBounds.Width, this.screenBounds.Height));
                    newFrame.Enqueue(this.lastSlaveContact);

                    if (contactType == ContactType.EndFromHover)
                    {
                        this.slaveEnded = true;
                        if (!this.masterReleased) //If we release slave before master
                        {
                            this.stepIDs = true;
                        }
                        else
                        {
                            this.masterID = (this.masterID - this.startID + 2) % 4 + this.startID;
                            this.slaveID  = (this.slaveID - this.startID + 2) % 4 + this.startID;
                            this.stepIDs  = false;
                        }
                    }
                }
            }

            return(newFrame);
        }