Ejemplo n.º 1
0
        public static void Update(float dt)
        {
            totalTime += dt;

            justTouched  = false;
            justTapped   = false;
            touchStartId = null;

            //while (TouchPanel.IsGestureAvailable)
            //{
            //    GestureSample gesture = TouchPanel.ReadGesture();
            //    if (gesture.GestureType == GestureType.Tap)
            //    {
            //        justTapped = true;
            //        tapPosition = gesture.Position;
            //    }
            //}

            //stateLast = state;
            state = TouchPanel.GetState();
            TouchLocation?leftTouch = null, rightTouch = null;

            if (tapStartCount > state.Count)
            {
                tapStartCount = state.Count;

                // Workaround. Sometimes very fast taps won't be registered as TouchLocations with state of Released
                // meaning the algorithm in the for loop below falls down :(
                // Here we assume that only one tap was missed
                if (state.Count == 0)
                {
                    justTapped  = true;
                    tapPosition = tapStarts[0].Pos;
                }
            }

            foreach (TouchLocation loc in state)
            {
                if (loc.State == TouchLocationState.Released)
                {
                    int tapStartId = -1;
                    for (int i = 0; i < tapStartCount; ++i)
                    {
                        if (tapStarts[i].Id == loc.Id)
                        {
                            // This touch was released. Check if it was a tap
                            tapStartId = i;

                            // COMMENTED CODE WAS TO ENSURE TAPS ARE NOT REGISTERED FOR HOLDS (LONG TAPS)
                            //if ((Engine.Instance.TimeTotal - tapStarts[i].Time) < 1.0f)
                            //{
                            justTapped  = true;
                            tapPosition = loc.Position;
                            //}
                            //else
                            //{
                            //  System.Diagnostics.Debug.WriteLine("Rejected touch: Held too long");
                            //}

                            break;
                        }
                    }
                    if (tapStartId >= 0)
                    {
                        // Remove the tap start as it has been released
                        for (int i = tapStartId; i < tapStartCount - 1; ++i)
                        {
                            tapStarts[i] = tapStarts[i + 1];
                        }

                        tapStartCount--;
                    }
                    continue;
                }
                else if (loc.State == TouchLocationState.Pressed && tapStartCount < tapStarts.Length)
                {
                    // Started new touch
                    tapStarts[tapStartCount] = new TapStart(loc.Id, totalTime, loc.Position);
                    tapStartCount++;
                    justTouched   = true;
                    touchStartId  = loc.Id;
                    touchStartPos = loc.Position;
                }
                // COMMENTED CODE WAS TO REMOVE TAPS THAT DEVIATE TOO FAR FROM THEIR ORIGINAL POSITION
                //else
                //{
                //    int removeTapId = -1;
                //    for (int i = 0; i < tapStartCount; ++i)
                //    {
                //        if (tapStarts[i].Id == loc.Id)
                //        {
                //            // Remove any tap that deviates too far from it's original position
                //            float distSqr = Vector2.DistanceSquared(tapStarts[i].Pos, loc.Position);
                //            if (distSqr > 3600.0f)
                //            {
                //                //System.Diagnostics.Debug.WriteLine("Rejected touch: Deviated too far");
                //                removeTapId = i;
                //            }
                //            break;
                //        }
                //    }
                //    if (removeTapId >= 0)
                //    {
                //        // Remove the tap start as it has moved further than is valid
                //        for (int i = removeTapId; i < tapStartCount - 1; ++i)
                //            tapStarts[i] = tapStarts[i + 1];

                //        tapStartCount--;
                //    }
                //}

                if (leftStick.HasValue && loc.Id == leftStick.Value.Id)
                {
                    // Continue left touch
                    leftTouch = loc;
                    continue;
                }
                if (rightStick.HasValue && loc.Id == rightStick.Value.Id)
                {
                    // Continue right touch
                    rightTouch = loc;
                    continue;
                }

                TouchLocation locPrev;
                if (!loc.TryGetPreviousLocation(out locPrev))
                {
                    locPrev = loc;
                }

                if (!leftStick.HasValue)
                {
                    // if we are not currently tracking a left thumbstick and this touch is on the left
                    // half of the screen, start tracking this touch as our left stick
                    if (leftStickStartRegion.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                    {
                        if (leftStickStyle == TouchStickStyle.Fixed)
                        {
                            if (Vector2.Distance(locPrev.Position, leftStartLocation) < aliveZoneSize)
                            {
                                leftTouch = locPrev;
                            }
                        }
                        else
                        {
                            leftTouch         = locPrev;
                            leftStartLocation = leftTouch.Value.Position;

                            if (leftStartLocation.X < leftStickStartRegion.Left + DistFromScreenEdge)
                            {
                                leftStartLocation.X = leftStickStartRegion.Left + DistFromScreenEdge;
                            }
                            if (leftStartLocation.Y > leftStickStartRegion.Bottom - DistFromScreenEdge)
                            {
                                leftStartLocation.Y = leftStickStartRegion.Bottom - DistFromScreenEdge;
                            }
                        }
                        continue;
                    }
                }

                if (!rightStick.HasValue)
                {
                    // if we are not currently tracking a right thumbstick and this touch is on the right
                    // half of the screen, start tracking this touch as our right stick
                    if (rightStickStartRegion.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                    {
                        if (rightStickStyle == TouchStickStyle.Fixed)
                        {
                            if (Vector2.Distance(locPrev.Position, rightStartLocation) < aliveZoneSize)
                            {
                                rightTouch = locPrev;
                            }
                        }
                        else
                        {
                            rightTouch         = locPrev;
                            rightStartLocation = rightTouch.Value.Position;

                            // Ensure touch is not too close to screen edge
                            if (rightStartLocation.X > rightStickStartRegion.Right - DistFromScreenEdge)
                            {
                                rightStartLocation.X = rightStickStartRegion.Right - DistFromScreenEdge;
                            }
                            if (rightStartLocation.Y > rightStickStartRegion.Bottom - DistFromScreenEdge)
                            {
                                rightStartLocation.Y = rightStickStartRegion.Bottom - DistFromScreenEdge;
                            }
                        }
                        continue;
                    }
                }
            }

            if (leftTouch.HasValue)
            {
                leftStick    = leftTouch;
                leftStickPos = leftTouch.Value.Position;
                EvaluateLeftPoint(leftStickPos, dt);
            }
            else
            {
                bool foundNew = false;
                if (leftStick.HasValue)
                {
                    // No left touch now but previously there was. Check to see if the TouchPanel decided
                    // to reset our touch id. Search for any touch within 10 pixel radius.
                    foreach (TouchLocation loc in state)
                    {
                        Vector2 pos = loc.Position;
                        float   distSqr; Vector2.DistanceSquared(ref pos, ref leftStickPos, out distSqr);
                        if (distSqr < 100f)
                        {
                            foundNew     = true;
                            leftStick    = loc;
                            leftStickPos = loc.Position;
                            EvaluateLeftPoint(leftStickPos, dt);
                        }
                    }
                }

                if (!foundNew)
                {
                    leftStick          = null;
                    leftStickDirection = Vector2.Zero;
                    leftStickMagnitude = 0.0f;
                }
            }

            if (rightTouch.HasValue)
            {
                rightStick    = rightTouch;
                rightStickPos = rightTouch.Value.Position;
                EvaluateRightPoint(rightStickPos, dt);
            }
            else
            {
                bool foundNew = false;
                if (rightStick.HasValue)
                {
                    // No right touch now but previously there was. Check to see if the TouchPanel decided
                    // to reset our touch id. Search for any touch within 10 pixel radius.
                    foreach (TouchLocation loc in state)
                    {
                        Vector2 pos = loc.Position;
                        float   distSqr; Vector2.DistanceSquared(ref pos, ref rightStickPos, out distSqr);
                        if (distSqr < 100f)
                        {
                            foundNew      = true;
                            rightStick    = loc;
                            rightStickPos = loc.Position;
                            EvaluateRightPoint(rightStickPos, dt);
                        }
                    }
                }

                //
                if (!foundNew)
                {
                    rightStick          = null;
                    rightStickDirection = Vector2.Zero;
                    rightStickMagnitude = 0.0f;
                }
            }
        }
Ejemplo n.º 2
0
        public static void Update(float dt)
        {
            totalTime += dt;

            justTouched = false;
            justTapped = false;
            touchStartId = null;

            //while (TouchPanel.IsGestureAvailable)
            //{
            //    GestureSample gesture = TouchPanel.ReadGesture();
            //    if (gesture.GestureType == GestureType.Tap)
            //    {
            //        justTapped = true;
            //        tapPosition = gesture.Position;
            //    }
            //}

            //stateLast = state;
            state = TouchPanel.GetState();
            TouchLocation? leftTouch = null, rightTouch = null;

            if (tapStartCount > state.Count)
            {
                tapStartCount = state.Count;

                // Workaround. Sometimes very fast taps won't be registered as TouchLocations with state of Released
                // meaning the algorithm in the for loop below falls down :(
                // Here we assume that only one tap was missed
                if (state.Count == 0)
                {
                    justTapped = true;
                    tapPosition = tapStarts[0].Pos;
                }
            }

            foreach (TouchLocation loc in state)
            {
                if (loc.State == TouchLocationState.Released)
                {
                    int tapStartId = -1;
                    for (int i = 0; i < tapStartCount; ++i)
                    {
                        if (tapStarts[i].Id == loc.Id)
                        {
                            // This touch was released. Check if it was a tap
                            tapStartId = i;

                            // COMMENTED CODE WAS TO ENSURE TAPS ARE NOT REGISTERED FOR HOLDS (LONG TAPS)
                            //if ((Engine.Instance.TimeTotal - tapStarts[i].Time) < 1.0f)
                            //{
                                justTapped = true;
                                tapPosition = loc.Position;
                            //}
                            //else
                            //{
                            //  System.Diagnostics.Debug.WriteLine("Rejected touch: Held too long");
                            //}

                            break;
                        }
                    }
                    if (tapStartId >= 0)
                    {
                        // Remove the tap start as it has been released
                        for (int i = tapStartId; i < tapStartCount - 1; ++i)
                            tapStarts[i] = tapStarts[i + 1];

                        tapStartCount--;
                    }
                    continue;
                }
                else if (loc.State == TouchLocationState.Pressed && tapStartCount < tapStarts.Length)
                {
                    // Started new touch
                    tapStarts[tapStartCount] = new TapStart(loc.Id, totalTime, loc.Position);
                    tapStartCount++;
                    justTouched = true;
                    touchStartId = loc.Id;
                    touchStartPos = loc.Position;
                }
                // COMMENTED CODE WAS TO REMOVE TAPS THAT DEVIATE TOO FAR FROM THEIR ORIGINAL POSITION
                //else
                //{
                //    int removeTapId = -1;
                //    for (int i = 0; i < tapStartCount; ++i)
                //    {
                //        if (tapStarts[i].Id == loc.Id)
                //        {
                //            // Remove any tap that deviates too far from it's original position
                //            float distSqr = Vector2.DistanceSquared(tapStarts[i].Pos, loc.Position);
                //            if (distSqr > 3600.0f)
                //            {
                //                //System.Diagnostics.Debug.WriteLine("Rejected touch: Deviated too far");
                //                removeTapId = i;
                //            }
                //            break;
                //        }
                //    }
                //    if (removeTapId >= 0)
                //    {
                //        // Remove the tap start as it has moved further than is valid
                //        for (int i = removeTapId; i < tapStartCount - 1; ++i)
                //            tapStarts[i] = tapStarts[i + 1];

                //        tapStartCount--;
                //    }
                //}

                if (leftStick.HasValue && loc.Id == leftStick.Value.Id)
                {
                    // Continue left touch
                    leftTouch = loc;
                    continue;
                }
                if (rightStick.HasValue && loc.Id == rightStick.Value.Id)
                {
                    // Continue right touch
                    rightTouch = loc;
                    continue;
                }

                TouchLocation locPrev;
                if (!loc.TryGetPreviousLocation(out locPrev))
                    locPrev = loc;

                if (!leftStick.HasValue)
                {
                    // if we are not currently tracking a left thumbstick and this touch is on the left
                    // half of the screen, start tracking this touch as our left stick
                    if (leftStickStartRegion.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                    {
                        if (leftStickStyle == TouchStickStyle.Fixed)
                        {
                            if (Vector2.Distance(locPrev.Position, leftStartLocation) < aliveZoneSize)
                            {
                                leftTouch = locPrev;
                            }
                        }
                        else
                        {
                            leftTouch = locPrev;
                            leftStartLocation = leftTouch.Value.Position;

                            if (leftStartLocation.X < leftStickStartRegion.Left + DistFromScreenEdge)
                                leftStartLocation.X = leftStickStartRegion.Left + DistFromScreenEdge;
                            if (leftStartLocation.Y > leftStickStartRegion.Bottom - DistFromScreenEdge)
                                leftStartLocation.Y = leftStickStartRegion.Bottom - DistFromScreenEdge;
                        }
                        continue;
                    }
                }

                if (!rightStick.HasValue)
                {
                    // if we are not currently tracking a right thumbstick and this touch is on the right
                    // half of the screen, start tracking this touch as our right stick
                    if (rightStickStartRegion.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                    {
                        if (rightStickStyle == TouchStickStyle.Fixed)
                        {
                            if (Vector2.Distance(locPrev.Position, rightStartLocation) < aliveZoneSize)
                            {
                                rightTouch = locPrev;
                            }
                        }
                        else
                        {
                            rightTouch = locPrev;
                            rightStartLocation = rightTouch.Value.Position;

                            // Ensure touch is not too close to screen edge
                            if (rightStartLocation.X > rightStickStartRegion.Right - DistFromScreenEdge)
                                rightStartLocation.X = rightStickStartRegion.Right - DistFromScreenEdge;
                            if (rightStartLocation.Y > rightStickStartRegion.Bottom - DistFromScreenEdge)
                                rightStartLocation.Y = rightStickStartRegion.Bottom - DistFromScreenEdge;
                        }
                        continue;
                    }
                }
            }

            if (leftTouch.HasValue)
            {
                leftStick = leftTouch;
                leftStickPos = leftTouch.Value.Position;
                EvaluateLeftPoint(leftStickPos, dt);
            }
            else
            {
                bool foundNew = false;
                if (leftStick.HasValue)
                {
                    // No left touch now but previously there was. Check to see if the TouchPanel decided
                    // to reset our touch id. Search for any touch within 10 pixel radius.
                    foreach (TouchLocation loc in state)
                    {
                        Vector2 pos = loc.Position;
                        float distSqr; Vector2.DistanceSquared(ref pos, ref leftStickPos, out distSqr);
                        if (distSqr < 100f)
                        {
                            foundNew = true;
                            leftStick = loc;
                            leftStickPos = loc.Position;
                            EvaluateLeftPoint(leftStickPos, dt);
                        }
                    }
                }

                if (!foundNew)
                {
                    leftStick = null;
                    leftStickDirection = Vector2.Zero;
                    leftStickMagnitude = 0.0f;
                }
            }

            if (rightTouch.HasValue)
            {
                rightStick = rightTouch;
                rightStickPos = rightTouch.Value.Position;
                EvaluateRightPoint(rightStickPos, dt);
            }
            else
            {
                bool foundNew = false;
                if (rightStick.HasValue)
                {
                    // No right touch now but previously there was. Check to see if the TouchPanel decided
                    // to reset our touch id. Search for any touch within 10 pixel radius.
                    foreach (TouchLocation loc in state)
                    {
                        Vector2 pos = loc.Position;
                        float distSqr; Vector2.DistanceSquared(ref pos, ref rightStickPos, out distSqr);
                        if (distSqr < 100f)
                        {
                            foundNew = true;
                            rightStick = loc;
                            rightStickPos = loc.Position;
                            EvaluateRightPoint(rightStickPos, dt);
                        }
                    }
                }

                //
                if (!foundNew)
                {
                    rightStick = null;
                    rightStickDirection = Vector2.Zero;
                    rightStickMagnitude = 0.0f;
                }
            }
        }
Ejemplo n.º 3
0
        public void Update(GameTime gameTime)
        {
            var dt = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            totalTime += dt;

            var           state = TouchPanel.GetState();
            TouchLocation?leftTouch = null, rightTouch = null;

            if (tapStartCount > state.Count)
            {
                tapStartCount = state.Count;
            }

            foreach (TouchLocation loc in state)
            {
                if (loc.State == TouchLocationState.Released)
                {
                    int tapStartId = -1;
                    for (int i = 0; i < tapStartCount; ++i)
                    {
                        if (tapStarts[i].Id == loc.Id)
                        {
                            tapStartId = i;
                            break;
                        }
                    }
                    if (tapStartId >= 0)
                    {
                        for (int i = tapStartId; i < tapStartCount - 1; ++i)
                        {
                            tapStarts[i] = tapStarts[i + 1];
                        }
                        tapStartCount--;
                    }
                    continue;
                }
                else if (loc.State == TouchLocationState.Pressed && tapStartCount < tapStarts.Length)
                {
                    tapStarts[tapStartCount] = new TapStart(loc.Id, totalTime, loc.Position);
                    tapStartCount++;
                }

                if (LeftStick.touchLocation.HasValue && loc.Id == LeftStick.touchLocation.Value.Id)
                {
                    leftTouch = loc;
                    continue;
                }
                if (RightStick.touchLocation.HasValue && loc.Id == RightStick.touchLocation.Value.Id)
                {
                    rightTouch = loc;
                    continue;
                }

                if (!loc.TryGetPreviousLocation(out TouchLocation locPrev))
                {
                    locPrev = loc;
                }

                if (!LeftStick.touchLocation.HasValue)
                {
                    if (LeftStick.StartRegion.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                    {
                        if (LeftStick.Style == TouchStickStyle.Fixed)
                        {
                            if (Vector2.Distance(locPrev.Position, LeftStick.StartLocation) < aliveZoneSize)
                            {
                                leftTouch = locPrev;
                            }
                        }
                        else
                        {
                            leftTouch = locPrev;
                            LeftStick.StartLocation = leftTouch.Value.Position;
                            if (LeftStick.StartLocation.X < LeftStick.StartRegion.Left + edgeSpacing)
                            {
                                LeftStick.StartLocation.X = LeftStick.StartRegion.Left + edgeSpacing;
                            }
                            if (LeftStick.StartLocation.Y > LeftStick.StartRegion.Bottom - edgeSpacing)
                            {
                                LeftStick.StartLocation.Y = LeftStick.StartRegion.Bottom - edgeSpacing;
                            }
                        }
                        continue;
                    }
                }

                if (!RightStick.touchLocation.HasValue && locPrev.Id != RightStick.lastExcludedRightTouchId)
                {
                    if (RightStick.StartRegion.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                    {
                        bool excluded = false;
                        foreach (Rectangle r in RightStick.startExcludeRegions)
                        {
                            if (r.Contains((int)locPrev.Position.X, (int)locPrev.Position.Y))
                            {
                                excluded = true;
                                RightStick.lastExcludedRightTouchId = locPrev.Id;
                                continue;
                            }
                        }
                        if (excluded)
                        {
                            continue;
                        }
                        RightStick.lastExcludedRightTouchId = -1;
                        if (RightStick.Style == TouchStickStyle.Fixed)
                        {
                            if (Vector2.Distance(locPrev.Position, RightStick.StartLocation) < aliveZoneSize)
                            {
                                rightTouch = locPrev;
                            }
                        }
                        else
                        {
                            rightTouch = locPrev;
                            RightStick.StartLocation = rightTouch.Value.Position;
                            if (RightStick.StartLocation.X > RightStick.StartRegion.Right - edgeSpacing)
                            {
                                RightStick.StartLocation.X = RightStick.StartRegion.Right - edgeSpacing;
                            }
                            if (RightStick.StartLocation.Y > RightStick.StartRegion.Bottom - edgeSpacing)
                            {
                                RightStick.StartLocation.Y = RightStick.StartRegion.Bottom - edgeSpacing;
                            }
                        }
                        continue;
                    }
                }
            }

            LeftStick.Update(state, leftTouch, dt);
            RightStick.Update(state, rightTouch, dt);
        }