예제 #1
0
        async void OnSquareTapped(object parameter)
        {
            PuzzleSquare tappedSquare = (PuzzleSquare)parameter;

            await ShiftIntoEmpty(tappedSquare.Row, tappedSquare.Col);

            // Check for a "win".
            int index;

            for (index = 0; index < NUM * NUM - 1; index++)
            {
                int          row    = index / NUM;
                int          col    = index % NUM;
                PuzzleSquare square = squares[row, col];
                if (square == null || square.Index != index)
                {
                    break;
                }
            }

            // We have a winner!
            if (index == NUM * NUM - 1)
            {
                isPlaying = false;
                await DisplayAlert("CONGRATULATION", "YOU WON", "OK");
            }
        }
예제 #2
0
        void OnStackSizeChanged(object sender, EventArgs args)
        {
            double width  = stackLayout.Width;
            double height = stackLayout.Height;

            if (width <= 0 || height <= 0)
            {
                return;
            }



            // Orient StackLayout based on portrait/landscape mode.
            stackLayout.Orientation = (width < height) ? StackOrientation.Vertical :
                                      StackOrientation.Horizontal;

            // Calculate square size and position based on stack size.
            squareSize = Math.Min(width, height) / NUM;
            absoluteLayout.WidthRequest  = NUM * squareSize;
            absoluteLayout.HeightRequest = NUM * squareSize;

            foreach (View view in absoluteLayout.Children)
            {
                PuzzleSquare square = (PuzzleSquare)view;


                Xamarin.Forms.AbsoluteLayout.SetLayoutBounds(square,
                                                             new Rectangle(square.Col * squareSize,
                                                                           square.Row * squareSize,
                                                                           squareSize,
                                                                           squareSize));
            }
        }
예제 #3
0
        async Task AnimateSquare(int row, int col, int newRow, int newCol, uint length)
        {
            // The Square to be animated.
            PuzzleSquare animaSquare = squares[row, col];


            // The destination rectangle.
            Rectangle rect = new Rectangle(squareSize * emptyCol,
                                           squareSize * emptyRow,
                                           squareSize,
                                           squareSize);


            // This is the actual animation call.
            await animaSquare.LayoutTo(rect, length);

            // Set several variables and properties for new layout.
            squares[newRow, newCol] = animaSquare;
            animaSquare.Row         = newRow;
            animaSquare.Col         = newCol;
            squares[row, col]       = null;
            emptyRow = row;
            emptyCol = col;
        }
예제 #4
0
        public PuzzlePage()
        {
            // AbsoluteLayout to host the squares.
            absoluteLayout = new Xamarin.Forms.AbsoluteLayout()
            {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions   = LayoutOptions.Center
            };


            // Create XuzzleSquare's for all the rows and columns.


            Image[] normalImages = new Image[NUM * NUM];


            int index = 0;



            for (int row = 0; row < NUM; row++)
            {
                for (int col = 0; col < NUM; col++)
                {
                    // But skip the last one!
                    if (row == NUM - 1 && col == NUM - 1)
                    {
                        break;
                    }



                    // Instantiate the image reading it from the local resources.
                    normalImages[index] = new Image();

                    if (NUM == 2)
                    {
                        normalImages[index].Source = ImageSource
                                                     .FromResource(String.Format("Puzzles.Images.flower" + index + ".png"));
                    }
                    if (NUM == 4)
                    {
                        normalImages[index].Source = ImageSource
                                                     .FromResource(String.Format("Puzzles.ImagesMedium.insect" + index + ".png"));
                    }
                    if (NUM == 5)
                    {
                        normalImages[index].Source = ImageSource
                                                     .FromResource(String.Format("Puzzles.Images5X5.nature" + index + ".png"));
                    }



                    //Bitmap bmp = BitmapFactory.DecodeResourceAsync(String.Format("Puzzles.test.png"));
                    //BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
                    //bitmapDrawable.SetTileModeXY(Shader.TileMode.Repeat, Shader.TileMode.Repeat);



                    //getPuzzleBitmap(image);

                    //ImageView imageview = (ImageView)("Puzzles.test.png");

                    //Bitmap drawingCache = imageview.GetDrawingCache(true);
                    //Canvas canvas = new Canvas();
                    //Rect src = new Rect(23, 12, 23, 12);
                    //RectF dst = new RectF(0, 20, 0, 20); Paint paint = new Paint();
                    //canvas.DrawBitmap(drawingCache, src, dst, paint);



                    //Bitmap piece = Bitmap.CreateBitmap(normalImages, row, col, 30, 30);



                    //Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
                    //Bitmap myLogo = ((BitmapDrawable)myDrawable).getBitmap();



                    // Instantiate XuzzleSquare.
                    PuzzleSquare square = new PuzzleSquare(normalImages[index], index)
                    {
                        Row = row,
                        Col = col
                    };



                    // Add tap recognition
                    TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer
                    {
                        Command          = new Command(OnSquareTapped),
                        CommandParameter = square
                    };
                    square.GestureRecognizers.Add(tapGestureRecognizer);

                    //Bitmap bitmap = BitmapFactory.DecodeFile("Puzzles.test.png");
                    //cropBitmap(bitmap, Convert.ToInt32(square.WidthRequest), Convert.ToInt32(square.HeightRequest));
                    // Add it to the array and the AbsoluteLayout.
                    squares[row, col] = square;

                    absoluteLayout.Children.Add(square);
                    index++;
                }
            }


            // This is the "Randomize" button.
            randomizeButton = new Xamarin.Forms.Button
            {
                Text = "Randomize",
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions   = LayoutOptions.CenterAndExpand
            };
            randomizeButton.Clicked += OnRandomizeButtonClicked;

            // Label to display elapsed time.
            timeLabel = new Label
            {
                FontSize          = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                FontAttributes    = FontAttributes.Bold,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions   = LayoutOptions.CenterAndExpand
            };


            // Put everything in a StackLayout.
            stackLayout = new StackLayout
            {
                Children =
                {
                    new StackLayout {
                        VerticalOptions   = LayoutOptions.FillAndExpand,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        Children          =
                        {
                            randomizeButton,
                            timeLabel,
                        }
                    },
                    absoluteLayout
                }
            };
            stackLayout.SizeChanged += OnStackSizeChanged;

            // And set that to the content of the page.
            this.Padding =
                new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
            this.Content = stackLayout;
        }