Пример #1
0
        // spustenie hry, nastavia sa vsetky potrebne nastavenia a zobrazi sa hracia
        // plocha s puzzle kuskami a oknom s instrukciami(len pri prvom spusteni programu)
        private void button2_Click(object sender, EventArgs e)
        {
            if (gameData.SourcePicture != null)
            {
                textBox1.Visible    = false;
                pictureBox1.Visible = false;
                button1.Visible     = false;
                button2.Visible     = false;
                panel5.Visible      = true; //obsahuje button3==Restart, button4==NewGame

                //
                //nastavime pociatocne data pre hru
                //
                gameData.SourcePicture          = PictureEditor.CropImage(gameData.SourcePicture, gridLayer.StartCutLocation, gridLayer.EndCutLocation);
                gameData.PiecesGridDimensions   = gridLayer.GridDimensions;
                gameData.PiecesCount            = gameData.PiecesGridDimensions.Width * gameData.PiecesGridDimensions.Height;
                gameData.PieceDimensions        = gridLayer.PieceDimensions;
                gameData.PieceSurroundingSize   = (int)Math.Ceiling(gameData.PieceDimensions.Width * 0.163);
                gameData.GameBoard              = this.panel4;
                gameData.GameBoardStartPosition = new Point(50, 50);

                PuzzleGameUtilities.CreatePieces(gameData);
                PuzzleGameUtilities.SetOriginalPiecesLocations(gameData);
                PuzzleGameUtilities.SetPiecesArrangement(gameData);
                PuzzleGameUtilities.SetPiecesImages(gameData);
                PuzzleGameUtilities.SetPiecesOriginalNeighbours(gameData);
                PuzzleGameUtilities.RandomizePiecesLocations(gameData);


                //
                //Pociatocne nastavenia gameboard-u
                //
                gameboard = new Gameboard(this, gameData, panel4.Size);
                //gameboard ako instancia Form nesmie byt nastavena na top level control !!
                gameboard.TopLevel = false;
                panel4.Controls.Add(gameboard);
                gameboard.Visible  = false;
                gameboard.Anchor   = AnchorStyles.None;
                gameboard.Anchor   = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
                gameboard.Location = new Point(0, 0); //pozicia vo vnutri panel4
                gameboard.Visible  = true;

                if (showGameInstructions)
                {
                    this.panel6.BringToFront();
                    this.panel6.Visible = true;
                    // zabezpecime aby sa to ukazalo iba pri prvom spusteni
                    showGameInstructions = false;
                }
            }
            else
            {
                noPictureLabel.Visible = true;
                // MessageBox.Show("Nebol vybraný obrazok!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        // vystrihne jednotlive kusku zo zdrojoveho obrazka za pomoci metody CreatePuzzleShapePath
        private static Bitmap[] CreatePuzzleShapeImagesFromSourceImage(PuzzleGameData data)
        {
            Bitmap[] pShapes = new Bitmap[data.PiecesCount];

            //Pre zjednodesenie vyrezavania pridame hranu hrubky SurroundingSize okolo zdrojoveho obrazka,
            //sourceImage == obrazok + hrany dookola
            int hrubkaHrany = data.PieceSurroundingSize;
            int gridWidth   = data.PiecesGridDimensions.Width;
            int gridheight  = data.PiecesGridDimensions.Height;
            int pieceWidth  = data.PieceDimensions.Width;
            int pieceHeight = data.PieceDimensions.Height;

            Bitmap sourceImage = new Bitmap(data.SourcePicture.Width + 2 * hrubkaHrany,
                                            data.SourcePicture.Height + 2 * hrubkaHrany);

            using (Graphics draw = Graphics.FromImage(sourceImage))
            {
                draw.DrawImage(data.SourcePicture, new Point(hrubkaHrany, hrubkaHrany));
            }

            //Vyrezavanie jednotlivych kuskov

            int x = 0, y = 0;
            //Point startP = new Point(x, y);
            //Point endP = new Point((x + pieceWidth + 2 * hrubkaHrany),
            //                       (y + pieceHeight + 2 * hrubkaHrany));
            GraphicsPath shapePath;

            int index;

            for (int j = 0; j < gridheight; j++)
            {
                for (int i = 0; i < gridWidth; i++)
                {
                    index          = j * gridWidth + i;
                    pShapes[index] = PictureEditor.CropImage(sourceImage,
                                                             new Point(x, y),
                                                             new Point((x + pieceWidth + 2 * hrubkaHrany),
                                                                       (y + pieceHeight + 2 * hrubkaHrany)));

                    shapePath      = CreatePuzzleShapePath(hrubkaHrany, data.PieceDimensions, data.Pieces[index].Arrangement);
                    pShapes[index] = PieceCutter.CutOut(pShapes[index], shapePath);

                    x += pieceWidth;
                }
                x  = 0;
                y += pieceHeight;
            }

            return(pShapes);
        }
Пример #3
0
        // sluzi na vykreslenie alebo prekreslenie mriezky pocat behu programu, pri zmene velkosti okna alebo rozmerov mriezky pod hodnot v textbox-och
        private void SetGridImage(double ratioR)  //nastavuje aj suradnice horneho laveho a dolneho praveho rohu gridu vzhladom k parent picturebox-u
        {
            this.Image = PictureEditor.DrawGridIntoImage(ratioR, pieceDimensions,
                                                         gridDimensions.Width,
                                                         gridDimensions.Height,
                                                         backGroundImageForGrid,
                                                         this.Size);

            //Urcime, kde konkretne sa nachadzaju rohy gridu(top left, bottom right) v parent picturebox-e vzdy,
            //ked dojde k zmene velkosti alebo pozicii gridu.
            pointTopLeftCorner_Grid     = TopLeftCornerPoint_GridLayer_TransformedToParentPicturebox;
            pointBottomRightCorner_Grid = BottomRightCornerPoint_GridLayer_TransformedToParentPicturebox;
            currImageSize = new Size((int)(ratioR * data.SourcePicture.Width), (int)(ratioR * data.SourcePicture.Height));
        }
Пример #4
0
        private void customclientSizeChanged_ParentPictureBox(object sender, EventArgs e)
        {
            // doslo k zmene velkosti panel-u a teda aj k zmene velkosti mriezky
            this.Size = this.parentPanel.Size;

            // vratime zmensenu / zvacsenu verziu povodneho obrazka, podla aktualneho GridRatioFromSourceImageResolution
            this.parentPictBox.Image = PictureEditor.ImageScale(GridRatioFromSourceImageResolution, data.SourcePicture);

            // prekreslime mriezky na nove rozmery okna
            this.SetGridImage(GridRatioFromSourceImageResolution);

            // upravime poziciu obrazka vzhladom k mriezke, aby mriezka ukazovala na rovnaku oblast obrazka aj po zmene velkosti okna
            ImageUnderGridLayer_Location = new Point(TopLeftCornerPoint_GridLayer_TransformedToParentPicturebox.X -
                                                     (int)((double)originalCornersDiff.X * (double)GridRatioFromSourceImageResolution),
                                                     TopLeftCornerPoint_GridLayer_TransformedToParentPicturebox.Y -
                                                     (int)((double)originalCornersDiff.Y * (double)GridRatioFromSourceImageResolution));


            this.TextBoxesInTheMiddle(MiddlePoint_Grid);
        }
Пример #5
0
        // pociatocne nastavenia
        public void Configure(PuzzleGameData data, Form1 form)
        {
            #region Pociatocne nastavenia - rozmiestnenia, velkosti, vztahy medzi controls, events handlers

            this.data                   = data;                               //Je potrebny zdrojovy obrazok v gamedata!!
            this.parentPanel            = (form.Controls["panel4"] as Panel); //Je potrebne nastavit pred zavolanim Configure(), kto je komu rodicom
            this.parentPictBox          = (this.Parent as PictureBox);
            this.parentPictBox.Location = new Point(0 - data.SourcePicture.Width, 0 - data.SourcePicture.Height);
            this.parentPictBox.Size     = new Size(data.SourcePicture.Width + this.parentPanel.Size.Width,
                                                   data.SourcePicture.Height + this.parentPanel.Size.Height);
            this.parentPictBox.BringToFront();
            //
            this.Location  = new Point(data.SourcePicture.Width, data.SourcePicture.Height);  //pozicia gridlayer kvoli posunu picturebox-u
            this.Size      = new Size(this.parentPanel.Size.Width, this.parentPanel.Height);  //velkost gridlayer podla velkosti zobrazovaniecho panelu(panel4)
            this.BackColor = Color.Transparent;
            this.BringToFront();
            this.Anchor = AnchorStyles.None;
            this.Anchor = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
            this.Controls.Add(widthBox);
            this.Controls.Add(heightBox);
            this.Controls.Add(dimensionBox);
            this.MouseDown += this.customGridLayer_MouseDown;
            this.MouseUp   += this.customGridLayer_MouseUp;
            this.MouseMove += this.customGridLayer_MouseMove;
            this.parentPictBox.ClientSizeChanged += customclientSizeChanged_ParentPictureBox;
            //
            this.parentPictBox.Image = PictureEditor.ImageScale(GridRatioFromSourceImageResolution, data.SourcePicture);


            this.parentPictBox.Padding = new Padding(data.SourcePicture.Width +
                                                     (int)((this.parentPanel.Width - this.parentPictBox.Image.Size.Width) / 2),
                                                     data.SourcePicture.Height +
                                                     (int)((this.parentPanel.Height - this.parentPictBox.Image.Size.Height) / 2), 0, 0);
            TextBoxesVisible = false;
            //====================================================================================================================================
            #endregion

            #region Nastavenie Grid
            //
            //Prednastavenia pred vytvorenim Grid okrazka
            //
            backGroundImageForGrid = PictureEditor.ColouredBackGroundImage(Color.Black, this.Size); //Color.RoyalBlue
            backGroundImageForGrid = PictureEditor.ImageOpacity(0.60F, backGroundImageForGrid);
            pieceDimensions        = new Size(100, 100);
            gridDimensions         = new Size((int)((data.SourcePicture.Width * 0.7) / pieceDimensions.Width),
                                              (int)((data.SourcePicture.Height * 0.7) / pieceDimensions.Height));
            //
            //Nastavenie vygenerovaneho grid obrazka
            //
            this.SetGridImage(GridRatioFromSourceImageResolution);
            ////this.Refresh();


            //
            //Nastavenie textboxov
            //
            widthBox.TextChanged += this.whenTextBox1_Changed;
            widthBox.MouseEnter  += this.whenTextBox1_MouseEnter;
            widthBox.MouseLeave  += this.whenTextBox1_MouseLeave;

            heightBox.TextChanged += this.whenTextBox2_Changed;
            heightBox.MouseEnter  += this.whenTextBox2_MouseEnter;
            heightBox.MouseLeave  += this.whenTextBox2_MouseLeave;

            dimensionBox.TextChanged += this.whenTextBox3_Changed;
            dimensionBox.MouseEnter  += this.whenTextBox3_MouseEnter;
            dimensionBox.MouseLeave  += this.whenTextBox3_MouseLeave;

            //widthBox.Text = "Width";
            //heightBox.Text = "Height";
            //dimensionBox.Text = "Size";

            var textBoxes = this.Controls.OfType <TextBox>().ToArray();
            foreach (var textBox in textBoxes)
            {
                textBox.BackColor   = Color.FromArgb(64, 64, 64);
                textBox.BorderStyle = BorderStyle.FixedSingle;
                textBox.ForeColor   = Color.White;
                textBox.Size        = new Size(50, 20);
                //textBox.Refresh();
            }
            //
            //do textboxov vyplnime pociatocne hodnoty rozmerov grid, a velkost kuska puzzle, zalohujeme text, ktory v nich bol
            //
            textbox1_BackupText = (gridDimensions.Width).ToString();
            widthBox.Text       = "Šírka";
            textbox2_BackupText = (gridDimensions.Height).ToString();
            heightBox.Text      = "Dĺžka";
            textbox3_BackupText = (pieceDimensions.Width).ToString();
            dimensionBox.Text   = "Rozmer";
            //
            //TextBox-y umiestnime do stredu
            //
            TextBoxesInTheMiddle(MiddlePoint_Grid);
            //
            //TextBox-y budu viditelne
            //
            TextBoxesVisible = true;
            this.Refresh();
            //
            // Vzhlad kurzora
            //
            //Pri vybere obrazka sa vzhlad kurzoru zmeni na posovny kurzor, sipky styrmi smermi
            this.Cursor = Cursors.SizeAll;

            //
            // Zistime ako su od seba posunuta mriezka a obrazok a prevedieme to do skutocneho rozdielu, ak by bol obrazok nescalovany
            // Pri zmene velkosti okna totiz nesmie dojst k posunu mriezky vzhladom k obrazku, mriezka by mohla vyjst mimo obrazok
            //
            originalCornersDiff = OriginalCornersDiff;

            #endregion
        }