コード例 #1
0
 private void SizeChangedEventHandler(object sender, SizeChangedEventArgs e)
 {
     foreach (var piece in _pieces)
     {
         var ttv = SourceImage.TransformToVisual(Window.Current.Content).TransformPoint(new Point(0, 0));
         piece.TargetPosition = new Position
         {
             X = ttv.X + piece.ColId * _pieceWidth,
             Y = ttv.Y + piece.RowId * _pieceHeight
         };
     }
     foreach (var piece in _pieces.Where(p => p.IsPlaced))
     {
         piece.Position = piece.TargetPosition;
         Canvas.SetLeft(piece.Path, piece.Position.X);
         Canvas.SetTop(piece.Path, piece.Position.Y);
     }
     Bindings.Update();
 }
コード例 #2
0
        private void ConstructJigsawPieces()
        {
            _currentPiece = null;
            _pieces       = new List <Piece>();

            var pieceId = 0;

            var rand = new Random();

            var boardSize    = GameSettings.BoardSizes[GameSettings.SelectedBoardSizeIndex];
            var boardSizeCol = boardSize.Item1;
            var boardSizeRow = boardSize.Item2;

            for (var row = 0; row < boardSizeRow; row++)
            {
                for (var col = 0; col < boardSizeCol; col++)
                {
                    var connections = new[] { (int)ConnectionType.Tab, (int)ConnectionType.Blank };

                    var upperConnection  = (int)ConnectionType.None;
                    var rightConnection  = (int)ConnectionType.None;
                    var bottomConnection = (int)ConnectionType.None;
                    var leftConnection   = (int)ConnectionType.None;

                    if (row > 0)
                    {
                        upperConnection = -1 * _pieces[(row - 1) * boardSizeCol + col].BottomConnection;
                    }

                    if (col < boardSizeCol - 1)
                    {
                        rightConnection = connections[rand.Next(2)];
                    }

                    if (row < boardSizeRow - 1)
                    {
                        bottomConnection = connections[rand.Next(2)];
                    }

                    if (col > 0)
                    {
                        leftConnection = -1 * _pieces.Last().RightConnection;
                    }

                    var ttv   = SourceImage.TransformToVisual(Window.Current.Content).TransformPoint(new Point(0, 0));
                    var piece = new Piece
                    {
                        Id       = pieceId,
                        Width    = _pieceWidth,
                        Height   = _pieceHeight,
                        RowId    = row,
                        ColId    = col,
                        Position = new Position
                        {
                            X = rand.Next(200),
                            Y = 100 + rand.Next(500)
                        },
                        TargetPosition = new Position
                        {
                            X = ttv.X + col * _pieceWidth,
                            Y = ttv.Y + row * _pieceHeight
                        },
                        UpperConnection  = upperConnection,
                        RightConnection  = rightConnection,
                        BottomConnection = bottomConnection,
                        LeftConnection   = leftConnection
                    };

                    var path = piece.CreatePathGeometry();

                    var shiftX = 0;
                    if (leftConnection == 1)
                    {
                        shiftX = 16;
                    }
                    else if (leftConnection == -1)
                    {
                        shiftX = 8;
                    }

                    var shiftY = 0;
                    if (upperConnection == 1)
                    {
                        shiftY = 16;
                    }
                    else if (upperConnection == -1)
                    {
                        shiftY = 8;
                    }

                    path.Fill = new ImageBrush
                    {
                        AlignmentX  = AlignmentX.Left,
                        AlignmentY  = AlignmentY.Top,
                        ImageSource = _sourcePicture,
                        Stretch     = Stretch.None,
                        Transform   = new TranslateTransform {
                            X = -col * _pieceWidth + shiftX,
                            Y = -row * _pieceHeight + shiftY
                        }
                    };

                    path.StrokeThickness = 3;
                    path.Stroke          = new SolidColorBrush(Colors.Black);

                    path.ManipulationMode       = ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
                    path.ManipulationDelta     += UIElement_OnManipulationDelta;
                    path.ManipulationStarted   += UIElement_OnManipulationStarted;
                    path.ManipulationCompleted += UIElement_OnManipulationCompleted;

                    _pieces.Add(piece);

                    GameBoard.Children.Add(piece.Path);
                    Canvas.SetLeft(piece.Path, piece.Position.X);
                    Canvas.SetTop(piece.Path, piece.Position.Y);

                    pieceId++;
                }
            }
        }