コード例 #1
0
        /// <summary>
        /// Handles OnTouch event for picBoxes imageViews
        /// </summary>
        /// <param name="v"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public bool OnTouch(View v, MotionEvent e)
        {
            if (!SuccessChecker.IsSuccessful(mCurrentLvl, mPicBoxes))
            {
                int x = (int)v.GetX(); // Get X position of picBox imageView
                int y = (int)v.GetY(); // Get Y position of picBox imageView

                if (e.Action == MotionEventActions.Down)
                {
                    // Touch down event
                    mFirstBox = (MyImageView)v; // Get the imageView which is touched
                    return(true);
                }
                if (e.Action == MotionEventActions.Up)
                {
                    // Touch up event
                    int positionX = (int)e.GetX() + x; // Get X position where the touch released relative to the original X position
                    int positionY = (int)e.GetY() + y; // Get Y position where the touch released relative to the original Y position
                    int picBox    = 0;

                    for (int i = 0; i < mCurrentLvl; i++)
                    {
                        // Get the bounds of each imageView
                        int xStart = (int)mPicBoxes[i].GetX();                       // Get the X position of the imageView top left bound
                        int xEnd   = (int)mPicBoxes[i].GetX() + mPicBoxes[i].Width;  // Get the X position of the imageView top right bound
                        int yStart = (int)mPicBoxes[i].GetY();                       // Get the Y position of the imageView bottom left bound
                        int yEnd   = (int)mPicBoxes[i].GetY() + mPicBoxes[i].Height; // Get the Y position of the imageView bottom right bound

                        // Check if the touch released position is within the imageView bounds
                        if (positionX > xStart && positionX < xEnd && positionY > yStart && positionY < yEnd)
                        {
                            picBox = i; // If true, get the index of the imageView
                        }
                    }
                    if (mFirstBox != mPicBoxes[picBox])
                    {
                        // Call ImageSwitcher if a swap is triggered
                        ImageSwitcher.SwitchImages(mFirstBox, (MyImageView)mPicBoxes[picBox], mImages, mCurrentLvl, mPicBoxes);
                    }
                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Method for switching images between two imageViews
        /// </summary>
        /// <param name="box1"></param>
        /// <param name="box2"></param>
        /// <param name="images"></param>
        /// <param name="currentLvl"></param>
        /// <param name="picBoxes"></param>
        public static void SwitchImages(MyImageView box1, MyImageView box2, Bitmap[] images, int currentLvl, ImageView[] picBoxes)
        {
            int tmp = box2.ImageIndex;                    // Temporary holder for box2 imageIndex

            box2.SetImageBitmap(images[box1.ImageIndex]); // Change the box2 image to box1 image
            box2.ImageIndex = box1.ImageIndex;            // Change the box2 imageIndex to box1 imageIndex
            box1.SetImageBitmap(images[tmp]);             // Change the box1 image to box2 image
            box1.ImageIndex = tmp;                        // Change the box1 imageIndex to box2 imageIndex

            // Send the result each time when a puzzle piece is switched
            OnSwapComplete.Invoke(Application.Context, new EventArgs());

            // Check if the puzzle is fixed
            if (SuccessChecker.IsSuccessful(currentLvl, picBoxes))
            {
                // If true, send the result
                OnPuzzleComplete.Invoke(Application.Context, new EventArgs());
            }
        }
コード例 #3
0
ファイル: PuzzlePlayer.cs プロジェクト: haZya/Enigma-Rampage
        /// <summary>
        /// Method for breaking down the image into puzzle pieces
        /// </summary>
        /// <param name="puzzlePanel"></param>
        /// <param name="Lvl"></param>
        /// <param name="image"></param>
        /// <param name="context"></param>
        public void Play(Bitmap image, Context context)
        {
            // Clear the arrays
            sImages   = null;
            sPicBoxes = null;

            // Calling the Garbage Collector to clean up the heap
            GC.Collect();

            sImages   = new Bitmap[mCurrentLvl];
            sPicBoxes = new ImageView[mCurrentLvl];

            int numRow = (int)Math.Sqrt(mCurrentLvl);  // # of rows
            int numCol = numRow;                       // # of columns
            int unitX  = mPuzzlePanel.Width / numRow;  // Width of a single puzzle piece
            int unitY  = mPuzzlePanel.Height / numCol; // Height of a single puzzle piece

            int[] indice = new int[mCurrentLvl];       // Helper array for shuffle
            mPuzzlePanel.RemoveAllViews();             // Remove all views from puzzlePanel layout

            // Draw all the puzzle pieces depending on the current level
            for (int i = 0; i < mCurrentLvl; i++)
            {
                indice[i] = i;
                if (sPicBoxes[i] == null)
                {
                    sPicBoxes[i] = new MyImageView(context, null);                                         // Create a new instance of imageView if doesn't exists
                    sPicBoxes[i].SetOnTouchListener(new MyTouchListener(sImages, mCurrentLvl, sPicBoxes)); // Add Touch Listner for each imageView
                }

                ViewGroup parent = (ViewGroup)sPicBoxes[i].Parent; // Get the parent view into a ViewGroup
                if (parent != null)
                {
                    // Remove from the parent
                    parent.RemoveView(sPicBoxes[i]);
                }

                mPuzzlePanel.AddView(sPicBoxes[i]); // Add the imageView to the puzzlePanel layout

                // Set the picBox imageView bounds
                sPicBoxes[i].LayoutParameters.Width  = unitX;                                   // Set the Width of a imageView
                sPicBoxes[i].LayoutParameters.Height = unitY;                                   // Set the Height of a imageView

                ((MyImageView)sPicBoxes[i]).Index = i;                                          // Set the index for the picBox imageView
                BitmapMaker.CreateBitmapImage(image, sImages, i, numRow, numCol, unitX, unitY); // Draw the image piece into imageView

                // Set location of the picBox imageView
                sPicBoxes[i].SetX(unitX * (i % numCol)); // Set X position of imageView
                sPicBoxes[i].SetY(unitY * (i / numCol)); // Set Y position of imageView

                // Set a boader to the picBox imageView
                sPicBoxes[i].SetPaddingRelative(2, 1, 2, 1);     // Set padding to image pieces
                sPicBoxes[i].SetBackgroundColor(Color.DarkGray); // Set background color for the imageView
            }

            Shuffle(indice); // Shuffle image pieces and populate picBoxes

            // Check if the puzzle is still the same order after shuffling
            while (SuccessChecker.IsSuccessful(mCurrentLvl, sPicBoxes))
            {
                // If true, shuffle again
                Shuffle(indice);
            }
        }