Пример #1
0
        private Point SetCurrentPiecePosition(Piece currentPiece, double newX, double newY)
        {
            double cellX = (int)((newX) / 100);
            double cellY = (int)((newY) / 100);

            var firstPiece = currentSelection[0];

            var relativeCellX = currentPiece.X - firstPiece.X;
            var relativeCellY = currentPiece.Y - firstPiece.Y;

            double rotatedCellX = relativeCellX;
            double rotatedCellY = relativeCellY;

            currentPiece.X = cellX + rotatedCellX;
            currentPiece.Y = cellY + rotatedCellY;

            currentPiece.SetValue(Canvas.LeftProperty, currentPiece.X * 100);
            currentPiece.SetValue(Canvas.TopProperty, currentPiece.Y * 100);

            shadowPieces[currentPiece.Index].SetValue(Canvas.LeftProperty, currentPiece.X * 100);
            shadowPieces[currentPiece.Index].SetValue(Canvas.TopProperty, currentPiece.Y * 100);

            return(new Point(cellX, cellY));
        }
Пример #2
0
        private void CreatePuzzle(Stream streamSource)
        {
            Random rnd         = new Random();
            var    connections = new int[] { (int)ConnectionType.Tab, (int)ConnectionType.Blank };

            png = null;

            imageSource = null;
            var uri = new Uri(destFileName);

            //We do this to avoid memory leaks
            using (WrappingStream wrapper = new WrappingStream(streamSource))
                using (BinaryReader reader = new BinaryReader(wrapper))
                {
                    imageSource = new BitmapImage();
                    imageSource.BeginInit();
                    imageSource.CacheOption  = BitmapCacheOption.OnLoad;
                    imageSource.StreamSource = reader.BaseStream; // streamSource;
                    imageSource.EndInit();
                    imageSource.Freeze();
                }

            imgShowImage.Source = imageSource;

            scvImage.Visibility  = Visibility.Hidden;
            cnvPuzzle.Visibility = Visibility.Visible;

            var angles = new int[] { 0, 90, 180, 270 };



            int index = 0;

            for (var y = 0; y < rows; y++)
            {
                for (var x = 0; x < columns; x++)
                {
                    if (x != 1000)
                    {
                        int upperConnection  = (int)ConnectionType.None;
                        int rightConnection  = (int)ConnectionType.None;
                        int bottomConnection = (int)ConnectionType.None;
                        int leftConnection   = (int)ConnectionType.None;

                        if (y != 0)
                        {
                            upperConnection = -1 * pieces[(y - 1) * columns + x].BottomConnection;
                        }

                        if (x != columns - 1)
                        {
                            rightConnection = connections[rnd.Next(2)];
                        }

                        if (y != rows - 1)
                        {
                            bottomConnection = connections[rnd.Next(2)];
                        }

                        if (x != 0)
                        {
                            leftConnection = -1 * pieces[y * columns + x - 1].RightConnection;
                        }

                        int angle = 0;

                        var piece = new Piece(imageSource, x, y, 0.1, 0.1, (int)upperConnection, (int)rightConnection, (int)bottomConnection, (int)leftConnection, false, index, scale);
                        piece.SetValue(Canvas.ZIndexProperty, 1000 + x * rows + y);
                        piece.MouseLeftButtonUp  += new MouseButtonEventHandler(piece_MouseLeftButtonUp);
                        piece.MouseRightButtonUp += new MouseButtonEventHandler(piece_MouseRightButtonUp);
                        piece.Rotate(piece, angle);

                        var shadowPiece = new Piece(imageSource, x, y, 0.1, 0.1, (int)upperConnection, (int)rightConnection, (int)bottomConnection, (int)leftConnection, true, shadowPieces.Count(), scale);
                        shadowPiece.SetValue(Canvas.ZIndexProperty, x * rows + y);
                        shadowPiece.Rotate(piece, angle);

                        pieces.Add(piece);
                        shadowPieces.Add(shadowPiece);
                        index++;
                    }
                }
            }

            var tt = new TranslateTransform()
            {
                X = 20, Y = 20
            };

            foreach (var p in pieces)
            {
                Random random = new Random();
                int    i      = random.Next(0, pnlPickUp.Children.Count);

                p.ScaleTransform.ScaleX = 1.0;
                p.ScaleTransform.ScaleY = 1.0;
                p.RenderTransform       = tt;
                p.X          = -1;
                p.Y          = -1;
                p.IsSelected = false;

                pnlPickUp.Children.Insert(i, p);

                double angle = angles[rnd.Next(0, 4)];
                p.Rotate(p, angle);
                shadowPieces[p.Index].Rotate(p, angle);
            }


            rectSelection.SetValue(Canvas.ZIndexProperty, 5000);

            rectSelection.StrokeDashArray = new DoubleCollection(new double[] { 4, 4, 4, 4 });
            cnvPuzzle.Children.Add(rectSelection);
        }