Exemple #1
0
        /// <summary>
        /// Handle the input of the sample.
        /// </summary>
        private void HandleInput()
        {
            // Read all gesture
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample sample = TouchPanel.ReadGesture();

                if (switchViewButton.HandleInput(sample))
                {
                    return;
                }

                if (sample.GestureType == GestureType.Tap)
                {
                    if (!Guide.IsVisible)
                    {
                        Guide.BeginShowKeyboardInput(PlayerIndex.One, "Select location", "Type in a location to focus on.",
                                                     String.Empty, LocationSelected, null);
                    }
                }
                else if (sample.GestureType == GestureType.FreeDrag)
                {
                    // Move the map when dragging
                    bingMapsViewer.MoveByOffset(sample.Delta);
                }
            }
        }
        /// <summary>
        /// Handle the input of the sample.
        /// </summary>
        private void HandleInput()
        {
            TouchCollection touchCollection = TouchPanel.GetState();

            if (touchCollection.Count > 0)
            {
                TouchLocation touch = touchCollection[0];

                // If the tank is moving he can not be relocated
                if (!tank.IsMoving)
                {
                    if (touch.State == TouchLocationState.Pressed && !isDragingTank)
                    {
                        Rectangle toucRect = new Rectangle((int)touch.Position.X - 5,
                                                           (int)touch.Position.Y - 5, 10, 10);

                        if (tank.Bounds.Intersects(toucRect))
                        {
                            isDragingTank = true;
                        }
                    }
                    else if (touch.State == TouchLocationState.Moved && isDragingTank)
                    {
                        tank.Move(touch.Position);
                    }
                    else if (touch.State == TouchLocationState.Released && isDragingTank)
                    {
                        isDragingTank = false;
                    }
                }
            }

            // Read all gesture
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample sample = TouchPanel.ReadGesture();

                // If the search button is clicked, the button will handle it.
                if (serachButton.HandleInput(sample))
                {
                    return;
                }

                // If the switch button is clicked, the button will handle it.
                if (switchViewButton.HandleInput(sample))
                {
                    return;
                }

                // If the change mode button is clicked, the button will handle it.
                if (changeRotuingModeButton.HandleInput(sample))
                {
                    return;
                }

                if (tank.IsMoving)
                {
                    // If the draw route button is clicked, the button will handle it.
                    if (drawRouteButton.HandleInput(sample))
                    {
                        return;
                    }

                    // If the clear all pushpins button is clicked, the button will handle it.
                    if (clearPushPinsButton.HandleInput(sample))
                    {
                        return;
                    }
                }

                if (sample.GestureType == GestureType.Tap)
                {
                    // Gets the position on the displayed map
                    Vector2 positionOnMap = TileSystem.LatLongToPixelXY(bingMapsViewer.CenterGeoCoordinate, ZoomLevel)
                                            - bingMapsViewer.Offset;

                    // Gets the distance between the gesture and the center of the screen
                    Vector2 distance = sample.Position - BingMapsViewer.ScreenCenter;
                    // Calculate the distance between the center of the screen and the position on the map
                    Vector2 positionOfGesture = positionOnMap + distance;

                    PushPin pushPin = new PushPin(bingMapsViewer,
                                                  TileSystem.PixelXYToLatLong(positionOfGesture, ZoomLevel), pushPinTexture2D);
                    pushPins.AddLast(pushPin);

                    // Indicates to the Tank that new push pin is added
                    tank.AddPushPin(pushPin);
                }
                else if (sample.GestureType == GestureType.FreeDrag)
                {
                    if (!isDragingTank)
                    {
                        // Move the map when dragging
                        bingMapsViewer.MoveByOffset(sample.Delta);
                    }
                }
                // When an hold gesture is caught on push pin ask if wants to delete
                else if (sample.GestureType == GestureType.Hold)
                {
                    // Creates a rectangle around the gesture
                    Rectangle touchRectangle = new Rectangle(
                        (int)sample.Position.X - 20,
                        (int)sample.Position.Y - 40, 60, 100);


                    foreach (PushPin pushPin in pushPins)
                    {
                        // Gets the center of the map displayed
                        Vector2 center = TileSystem.LatLongToPixelXY(bingMapsViewer.CenterGeoCoordinate,
                                                                     bingMapsViewer.ZoomLevel);

                        // Gets the position of the push pin on the map
                        Vector2 position = TileSystem.LatLongToPixelXY(pushPin.Location, bingMapsViewer.ZoomLevel);

                        // Calculate the distance between them in screen scale
                        Vector2 targetPosition = (position - center) + BingMapsViewer.ScreenCenter +
                                                 bingMapsViewer.Offset;

                        // Checks if the push pin is in side the gesture rectangle
                        if (touchRectangle.Contains(new Rectangle((int)targetPosition.X, (int)targetPosition.Y, 1, 1)))
                        {
                            Guide.BeginShowMessageBox("Warning!", "Are you sure you want to delete this push pin?",
                                                      new List <string>()
                            {
                                "OK", "Cancel"
                            }, 0, MessageBoxIcon.Alert,
                                                      DeletePushPinCallBack, pushPin);
                            return;
                        }
                    }
                }
            }
        }