Esempio n. 1
0
        /*
         * Initiate the drop event and if successful, move image from menu to board
         * @author Stanislav Yurchenko
         * @version 03/12/2017
         */
        private void DragAndDrop(object sender, DragEventArgs e)
        {
            Label lbl = sender as Label;
            // Find where the event was fired
            int   row    = Grid.GetRow(lbl);
            int   column = Grid.GetColumn(lbl);
            Image img    = new Image();
            // Get name of ship from the dragged content
            Ship s = GetShipReference(e.Data.GetData(typeof(String)) as String);
            // Get the image reference
            BitmapImage bitImage = new BitmapImage(new Uri(s.GetPath(), UriKind.Relative));

            img.Source = bitImage;
            if (s.Vertical)
            {
                img.SetValue(Grid.RowSpanProperty, s.GetLength());
            }
            else
            {
                img.SetValue(Grid.ColumnSpanProperty, s.GetLength());
            }
            try
            {
                user.GameGrid.AddShip(s, row, column);
            }
            catch (IndexOutOfRangeException)
            {
                return;
            }
            // Bind the image to the specific row and column
            Grid.SetRow(img, row);
            Grid.SetColumn(img, column);
            // Add the image to the board
            PlayerGrid.Children.Add(img);
            // Render the label inactive
            lbl.AllowDrop = false;
            // Remove ship image from menu, check all images in the grid
            foreach (Image menuShip in SeparationGrd.Children.OfType <Image>())
            {
                if (menuShip.Name.Equals(s.GetName()))
                {
                    SeparationGrd.Children.Remove(menuShip);
                    // Will complain about changing list otherwise
                    break;
                }
            }
            if (user.GameGrid.ShipsPlaced())
            {
                Mittelspiel();
            }
        }
Esempio n. 2
0
        /*
         * Draw the ship sent to this method
         * @author Stanislav Yurchenko
         * @version 06/12/2017
         */
        private void DrawShip(Ship s)
        {
            bool neverDrawn = true;

            for (int i = 0; i < drawnElements.Length; i++)
            {
                if (drawnElements[i] == null)
                {
                    drawnElements[i] = s.GetName();
                }
                else if (s.GetName().Equals(drawnElements[i]))
                {
                    neverDrawn = false;
                }
            }
            if (neverDrawn)
            {
                int         row;
                int         column;
                BitmapImage img;
                column = s.Coordinates[0] % 10;
                row    = s.Coordinates[0] / 10;
                img    = new BitmapImage(new Uri(s.GetPath(), UriKind.Relative));
                Image image = new Image()
                {
                    Source = img
                };
                Grid.SetRow(image, row);
                Grid.SetColumn(image, column);
                Grid.SetZIndex(image, 98);
                EnemyGrd.Children.Add(image);
                if (s.Vertical)
                {
                    image.SetValue(Grid.RowSpanProperty, s.GetLength());
                }
                else
                {
                    image.SetValue(Grid.ColumnSpanProperty, s.GetLength());
                }
            }
        }