private TouchCollection GetState()
        {
            TouchCollection actualState = Microsoft.Xna.Framework.Input.Touch.TouchPanel.GetState();

            TouchLocation[] locations = new TouchLocation[1];
            if (actualState.Count > 0)
            {
                TouchLocation location = actualState[0];
                TouchLocation previousState;
                location.TryGetPreviousLocation(out previousState);

                if (previousState.State == TouchLocationState.Pressed)
                {
                    locations[0] = new TouchLocation(previousState.Id, TouchLocationState.Pressed, location.Position);
                }
                else
                {
                    locations[0] = location;
                }

                previousLocation = location;
                return(new TouchCollection(locations));
            }
            else if (previousLocation.State == TouchLocationState.Moved)
            {
                locations[0]     = new TouchLocation(previousLocation.Id, TouchLocationState.Released, previousLocation.Position);
                previousLocation = new TouchLocation();
                return(new TouchCollection(locations));
            }

            return(new TouchCollection(empty));
        }
Example #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            verboseTouches.Clear();
            TouchCollection touchs = TouchPanel.GetState();

            if (touchs.Count > 0)
            {
                for (int i = 0; i < touchs.Count; i++)
                {
                    TouchLocation tl = touchs[i];
                    verboseTouches.Add("Id: " + tl.Id);
                    TouchLocation ptl;
                    if (tl.TryGetPreviousLocation(out ptl))
                    {
                        verboseTouches.Add(String.Format("Prev: ({0}, {1})", ptl.Position.X, ptl.Position.Y));
                    }
                    verboseTouches.Add(String.Format("Position: ({0}, {1})", tl.Position.X, tl.Position.Y));
                    verboseTouches.Add("State: " + tl.State.ToString());
                    verboseTouches.Add("--------------");
                }
            }
            else
            {
                verboseTouches.Add("Push the screen");
                verboseTouches.Add("to view the position");
            }

            base.Update(gameTime);
        }
Example #3
0
        public override void Update(float elapsed)
        {
#if PLATFORM_WINDOWS
            this.touchCollection = new TouchCollection();
#else
            this.touchCollection = TouchPanel.GetState();
#endif
            MouseState         mouse = Mouse.GetState();
            Vector2            currentMousePosition  = new Vector2(mouse.X, mouse.Y);
            Vector2            previousMousePosition = new Vector2(previousMouse.X, previousMouse.Y);
            TouchLocationState mouseState            = TouchLocationState.Invalid;

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                if ((currentMousePosition - previousMousePosition).LengthSquared() > 0)
                {
                    mouseState = TouchLocationState.Moved;
                }
                if (previousMouse.LeftButton != ButtonState.Pressed)
                {
                    mouseID--;
                    mouseState = TouchLocationState.Pressed;
                }
            }
            else if (previousMouse.LeftButton == ButtonState.Pressed)
            {
                mouseState = TouchLocationState.Released;
            }

            TouchLocation[] result = new TouchLocation[this.touchCollection.Count + (mouseState != TouchLocationState.Invalid?1:0)];

            for (int i = 0; i < this.touchCollection.Count; i++)
            {
                TouchLocation p, l = this.touchCollection [i];
                if (l.TryGetPreviousLocation(out p))
                {
                    result [i] = new TouchLocation(l.Id, l.State, Vector2.Transform(l.Position, this.invertedWorld), p.State, Vector2.Transform(p.Position, this.invertedWorld));
                }
                else
                {
                    result [i] = new TouchLocation(l.Id, l.State, Vector2.Transform(l.Position, this.invertedWorld));
                }
            }

            if (mouseState != TouchLocationState.Invalid)
            {
                result [result.Length - 1] = new TouchLocation(mouseID, mouseState, Vector2.Transform(currentMousePosition, this.invertedWorld), mousePrevState, Vector2.Transform(previousMousePosition, this.invertedWorld));
            }

            this.touchCollection = new TouchCollection(result);

            mousePrevState = mouseState;
            previousMouse  = mouse;
            base.Update(elapsed);
        }
Example #4
0
        public bool ProcessTouch(TouchLocation touch)
        {
            if (Texture == null)
            {
                return(false);
            }

            bool touchHandled = false;

            switch (touch.State)
            {
            case TouchLocationState.Pressed:
                if ((touch.Position.X > Position.X - Origin.X) &&
                    (touch.Position.X < Position.X - Origin.X + Texture.Width) &&
                    (touch.Position.Y > Position.Y - Origin.Y) &&
                    (touch.Position.Y < Position.Y - Origin.Y + Texture.Height))
                {
                    touchId      = touch.Id;
                    touchHandled = true;
                }
                break;

            case TouchLocationState.Moved:
                if (touchId.HasValue && touchId.Value == touch.Id)
                {
                    TouchLocation previousTouch;
                    touch.TryGetPreviousLocation(out previousTouch);
                    Position += touch.Position - previousTouch.Position;

                    // Fire the event!
                    if (PositionChanged != null)
                    {
                        PositionChanged(this, EventArgs.Empty);
                    }

                    touchHandled = true;
                }
                break;

            case TouchLocationState.Released:
                if (touchId.HasValue && touchId.Value == touch.Id)
                {
                    touchId      = null;
                    touchHandled = true;
                }
                break;
            }
            return(touchHandled);
        }
        public void AgeStateUpdatesPreviousDetails([Values(true, false)] bool isSameFrameReleased)
        {
            var touch = new TouchLocation(1, TouchLocationState.Pressed, new Vector2(1, 2), TimeSpan.FromSeconds(1));

            if (isSameFrameReleased)
            {
                touch.SameFrameReleased = true;
            }

            touch.AgeState();

            TouchLocation previous;

            Assert.True(touch.TryGetPreviousLocation(out previous));

            Assert.AreEqual(TouchLocationState.Pressed, previous.State);
            Assert.AreEqual(touch.Id, previous.Id);
            Assert.AreEqual(touch.Position, previous.Position);
            Assert.AreEqual(touch.Timestamp, previous.Timestamp);
        }
Example #6
0
        public void Update(Camera cam)
        {
            camera = cam;

            // Clear all lists
            touchPoints.Clear();
            PressPoints.Clear();
            ClickPoints.Clear();

            // Update touch state
            TouchCollection touchCollection = TouchPanel.GetState();

            // Add touches to list
            foreach (TouchLocation touch in touchCollection)
            {
                touchPoints.Add(touch);
            }


            if (touchPoints.Count == 0)
            {
                // See if any release states were missed
                foreach (TouchLocation touch in prevTouchPoints)
                {
                    if (touch.State == TouchLocationState.Pressed)
                    {
                        ClickPoints.Add(touch.Position);
                    }
                    else if (touch.State == TouchLocationState.Moved)
                    {
                        TouchLocation prevTouch;
                        if (touch.TryGetPreviousLocation(out prevTouch))
                        {
                            var delta = touch.Position - prevTouch.Position;

                            // Allow some errors
                            if (delta.LengthSquared() < 2)
                            {
                                ClickPoints.Add(touch.Position);
                            }
                        }
                    }
                }
                swipeDirection = Vector2.Zero;
            }
            else if (touchPoints.Count == 1)
            {
                TouchLocation touch = touchPoints[0];

                if ((touch.State == TouchLocationState.Moved) || (touch.State == TouchLocationState.Pressed))
                {
                    TouchLocation prevTouch;

                    // Sometimes TryGetPreviousLocation can fail
                    if (touch.TryGetPreviousLocation(out prevTouch))
                    {
                        // Get swiping direction
                        swipeDirection = touch.Position - prevTouch.Position;
                    }
                    else
                    {
                        PressPoints.Add(touch.Position);
                    }
                }
                else if (touch.State == TouchLocationState.Released)
                {
                    TouchLocation prevTouch;
                    if (touch.TryGetPreviousLocation(out prevTouch))
                    {
                        var delta = touch.Position - prevTouch.Position;

                        // Allow some errors
                        if (delta.LengthSquared() < 2)
                        {
                            ClickPoints.Add(touch.Position);
                        }
                    }
                    else
                    {
                        ClickPoints.Add(touch.Position);
                    }
                }
            }
            else if (touchCollection.Count == 2)
            {
                // TODO
            }
            else
            {
                // TODO
            }

            // Update previous state
            prevTouchPoints.Clear();
            prevTouchPoints.AddRange(touchPoints);
        }