public void DisplayPieces() { // Iterate thorugh all the buttons and set the default content. Container.Children.Cast <Button>().ToList().ForEach(button => { var column = Grid.GetColumn(button); var row = Grid.GetRow(button); // Create a DisplayChecker. It's used to check what's in the tiles. Game.DisplayChecker displayChecker = new DisplayChecker(); // Display castle. if (displayChecker.CheckIfCastle(StupidDataGhost.GameEngine, column, row)) { Canvas canv = new Canvas(); Rectangle tower = new Rectangle(); Rectangle def1 = new Rectangle(); Rectangle def2 = new Rectangle(); Rectangle def3 = new Rectangle(); tower.Fill = Brushes.DarkGray; tower.Height = 30; tower.Width = 30; Canvas.SetTop(tower, 10); Canvas.SetLeft(tower, 5); def1.Fill = Brushes.DarkGray; def1.Height = 9; def1.Width = 5; Canvas.SetTop(def1, 5); Canvas.SetLeft(def1, 5); def2.Fill = Brushes.DarkGray; def2.Height = 9; def2.Width = 5; Canvas.SetTop(def2, 5); Canvas.SetLeft(def2, 18); def3.Fill = Brushes.DarkGray; def3.Height = 9; def3.Width = 5; Canvas.SetTop(def3, 5); Canvas.SetLeft(def3, 30); canv.Children.Add(tower); canv.Children.Add(def1); canv.Children.Add(def2); canv.Children.Add(def3); button.Content = canv; button.HorizontalContentAlignment = HorizontalAlignment.Left; button.VerticalContentAlignment = VerticalAlignment.Top; } // Display king on castle. if (displayChecker.CheckIfKingAndCastle(StupidDataGhost.GameEngine, column, row)) { Grid grd = new Grid(); Ellipse el = new Ellipse(); // Display selected. if (displayChecker.CheckIfSelected(StupidDataGhost.GameEngine, column, row)) { el.Fill = Brushes.Black; el.Height = 30; el.Width = 30; grd.Children.Add(el); button.Content = grd; } else { el.Fill = Brushes.Lavender; el.Height = 30; el.Width = 30; el.Stroke = Brushes.Black; el.StrokeThickness = 6; grd.Children.Add(el); button.Content = grd; } button.HorizontalContentAlignment = HorizontalAlignment.Center; button.VerticalContentAlignment = VerticalAlignment.Center; } // Display king. else if (displayChecker.CheckIfKing(StupidDataGhost.GameEngine, column, row)) { Grid grd = new Grid(); Ellipse el = new Ellipse(); // Display selected. if (displayChecker.CheckIfSelected(StupidDataGhost.GameEngine, column, row)) { el.Fill = Brushes.Black; el.Height = 30; el.Width = 30; grd.Children.Add(el); button.Content = grd; } else { el.Fill = Brushes.Lavender; el.Height = 30; el.Width = 30; el.Stroke = Brushes.Black; el.StrokeThickness = 6; grd.Children.Add(el); button.Content = grd; } } // Display soldiers of defender. if (displayChecker.CheckIfSoldierDef(StupidDataGhost.GameEngine, column, row)) { Grid grd = new Grid(); Ellipse el = new Ellipse(); // Display selected. if (displayChecker.CheckIfSelected(StupidDataGhost.GameEngine, column, row)) { el.Fill = Brushes.Black; el.Height = 30; el.Width = 30; grd.Children.Add(el); button.Content = grd; } else { el.Fill = Brushes.Tomato; el.Height = 30; el.Width = 30; el.Stroke = Brushes.Black; el.StrokeThickness = 6; grd.Children.Add(el); button.Content = grd; } } // Display soldiers of attacker. if (displayChecker.CheckIfSoldierAtt(StupidDataGhost.GameEngine, column, row)) { Grid grd = new Grid(); Ellipse el = new Ellipse(); // Display selected. if (displayChecker.CheckIfSelected(StupidDataGhost.GameEngine, column, row)) { el.Fill = Brushes.Black; el.Height = 30; el.Width = 30; grd.Children.Add(el); button.Content = grd; } else { el.Fill = Brushes.Gold; el.Height = 30; el.Width = 30; el.Stroke = Brushes.Black; el.StrokeThickness = 6; grd.Children.Add(el); button.Content = grd; } } // Display empty tiles. if (displayChecker.CheckIfEmpty(StupidDataGhost.GameEngine, column, row)) { button.Content = ""; } }); }
protected override void OnPreviewMouseMove(MouseEventArgs e) { base.OnPreviewMouseMove(e); // If no element is being dragged, there is nothing to do. if (m_DraggedElement == null || !m_IsDragInProgress) { return; } // Get the position of the mouse cursor, relative to the Canvas. Point cursorLocation = e.GetPosition(this); // These values will store the new offsets of the drag element. double _HorizontalOffset = Double.NaN; double _VerticalOffset = Double.NaN;; #region Calculate Offsets Size _DraggedElemenetSize = m_DraggedElement.RenderSize; if ((m_ModificationDirection & ModificationDirection.Left) != 0) { _HorizontalOffset = m_OriginalLefOffset + (cursorLocation.X - m_InitialCursorLocation.X); if (m_DraggingMode == DraggingModes.AllowDragInView) { if (_HorizontalOffset < 0) { _HorizontalOffset = 0; } else if ((_HorizontalOffset + _DraggedElemenetSize.Width) > this.ActualWidth) { _HorizontalOffset = this.ActualWidth - _DraggedElemenetSize.Width; } } m_OriginalLefOffset = _HorizontalOffset; m_OriginalRightOffset = this.ActualWidth + _DraggedElemenetSize.Width - m_OriginalLefOffset; } else if ((m_ModificationDirection & ModificationDirection.Right) != 0) { _HorizontalOffset = m_OriginalRightOffset - (cursorLocation.X - m_InitialCursorLocation.X); if (m_DraggingMode == DraggingModes.AllowDragInView) { if (_HorizontalOffset < 0) { _HorizontalOffset = 0; } else if ((_HorizontalOffset + _DraggedElemenetSize.Width) > this.ActualWidth) { _HorizontalOffset = this.ActualWidth - _DraggedElemenetSize.Width; } } m_OriginalRightOffset = _HorizontalOffset; m_OriginalLefOffset = this.ActualWidth + _DraggedElemenetSize.Width - m_OriginalRightOffset; } if ((m_ModificationDirection & ModificationDirection.Top) != 0) { _VerticalOffset = m_OriginalTopOffset + (cursorLocation.Y - m_InitialCursorLocation.Y); if (m_DraggingMode == DraggingModes.AllowDragInView) { if (_VerticalOffset < 0) { _VerticalOffset = 0; } else if ((_VerticalOffset + _DraggedElemenetSize.Height) > this.ActualHeight) { _VerticalOffset = this.ActualHeight - _DraggedElemenetSize.Height; } } m_OriginalTopOffset = _VerticalOffset; m_OriginalBottomOffset = this.ActualHeight + _DraggedElemenetSize.Height - m_OriginalTopOffset; } else if ((m_ModificationDirection & ModificationDirection.Bottom) != 0) { _VerticalOffset = m_OriginalBottomOffset - (cursorLocation.Y - m_InitialCursorLocation.Y); if (m_DraggingMode == DraggingModes.AllowDragInView) { if (_VerticalOffset < 0) { _VerticalOffset = 0; } else if ((_VerticalOffset + _DraggedElemenetSize.Height) > this.ActualHeight) { _VerticalOffset = this.ActualHeight - _DraggedElemenetSize.Height; } } m_OriginalBottomOffset = _VerticalOffset; m_OriginalTopOffset = this.ActualHeight + _DraggedElemenetSize.Height - m_OriginalBottomOffset; } m_InitialCursorLocation = cursorLocation; #endregion // Calculate Offsets #region Move Drag Element if ((m_ModificationDirection & ModificationDirection.Left) != 0) { Canvas.SetLeft(m_DraggedElement, _HorizontalOffset); } else if ((m_ModificationDirection & ModificationDirection.Right) != 0) { Canvas.SetRight(m_DraggedElement, _HorizontalOffset); } if ((m_ModificationDirection & ModificationDirection.Top) != 0) { Canvas.SetTop(m_DraggedElement, _VerticalOffset); } else if ((m_ModificationDirection & ModificationDirection.Bottom) != 0) { Canvas.SetBottom(m_DraggedElement, _VerticalOffset); } #endregion // Move Drag Element }