Ejemplo n.º 1
0
 void getDataAndClose(paintmode mode)
 {
     this.stopSystem = false;
     getValuesFromFormControls();
     this.data.mode = mode;
     this.Close();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Method that is used as UpdateThread. Initializing image and then runs the grid turns over and over again
        /// </summary>
        private void UpdateThread()
        {
            //Make Image Black And White and set "bool[] cells" to living or dead
            initializeGridFromCells();

            //bool for While cycle
            bool running = true;

            //Delete selection
            isSelected           = false;
            mouseStartPosSelectX = mouseStartPosSelectY = -1;
            if (paintMode == paintmode.select)
            {
                paintMode = paintmode.draw;
            }

            //CYCLE
            while (running)
            {
                //Sleep for some ms because otherwise its too fast on small grids
                Thread.Sleep(cycleDelay);
                //Make a Turn
                processRun();

                //Set Generation number;
                MainForm.instance.SetGeneration(MainForm.instance.GetGeneration() + 1);

                //Display the image on the Grid Form
                syncImage();

                //If process should only be ran once then kill it after one turn
                if (selfKill)
                {
                    selfKill = false;
                    running  = false;
                }
            }
        }
Ejemplo n.º 3
0
        //Check if Mouse Down
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //If simulation is running then pause it
            if (updateThread != null && updateThread.IsAlive)
            {
                started = false;

                updateThread.Abort();

                //Update Start/Pause Button
                MainForm.instance.UpdateStartButton();

                //Set variable wasRunningBeforePaint so it restarts when stop painting
                wasRunningBeforePaint = true;
            }

            //Check if Left or Right Mousebutton
            switch (e.Button)
            {
            case MouseButtons.Left:
                isImageGrabbed = false;
                //Set Color Black
                paintColor = MainForm.livingcolor;

                if (paintMode == paintmode.grab)
                {
                    paintMode = paintmode.draw;
                }
                break;

            case MouseButtons.Right:
                isImageGrabbed = false;
                //Set Color White
                if (paintMode != paintmode.select)
                {
                    paintColor = MainForm.deadcolor;
                }

                if (paintMode == paintmode.grab)
                {
                    paintMode = paintmode.draw;
                }

                float widthMultiplicator  = (float)image.Width / (float)gridPictureBox.Width;
                float heightMultiplicator = (float)image.Height / (float)gridPictureBox.Height;
                int   mouseX = (int)(e.X * widthMultiplicator);
                int   mouseY = (int)(e.Y * heightMultiplicator);

                if ((mouseStartPosSelectX < mouseEndPosSelectX && mouseStartPosSelectX <= mouseX && mouseEndPosSelectX >= mouseX) && (mouseStartPosSelectY < mouseEndPosSelectY && mouseStartPosSelectY <= mouseY && mouseEndPosSelectY >= mouseY) ||
                    (mouseStartPosSelectX > mouseEndPosSelectX && mouseStartPosSelectX >= mouseX && mouseEndPosSelectX <= mouseX) && (mouseStartPosSelectY < mouseEndPosSelectY && mouseStartPosSelectY <= mouseY && mouseEndPosSelectY >= mouseY) ||
                    (mouseStartPosSelectX < mouseEndPosSelectX && mouseStartPosSelectX <= mouseX && mouseEndPosSelectX >= mouseX) && (mouseStartPosSelectY > mouseEndPosSelectY && mouseStartPosSelectY >= mouseY && mouseEndPosSelectY <= mouseY) ||
                    (mouseStartPosSelectX > mouseEndPosSelectX && mouseStartPosSelectX >= mouseX && mouseEndPosSelectX <= mouseX) && (mouseStartPosSelectY > mouseEndPosSelectY && mouseStartPosSelectY >= mouseY && mouseEndPosSelectY <= mouseY))
                {
                    keepSelectionOnLeftclick = true;
                    contextMenuElementSelected.Show(Cursor.Position.X, Cursor.Position.Y);
                }
                break;

            case MouseButtons.Middle:
                isImageGrabbed = true;
                paintColor     = MainForm.deadcolor;
                paintMode      = paintmode.grab;
                break;
            }

            //Check if user wants to draw by hand
            if (paintMode == paintmode.draw)
            {
                //Set startpoint for holding Control to make straight lines
                mouseDownX = e.X;
                mouseDownY = e.Y;

                //Set in Painting Mode
                isPainting = true;

                //Set a Dot at Mouse Position
                PaintAt(e.X, e.Y);
            }
            //Check if user wants to paint a ship and is using leftclick (color livingcolor = leftclick)
            else if ((paintMode == paintmode.ship || paintMode == paintmode.glider || paintMode == paintmode.custom) && paintColor == MainForm.livingcolor)
            {
                bool[,] pattern = Patterns.ship;

                //Check Paintmode
                if (paintMode == paintmode.glider)
                {
                    pattern = Patterns.glider;
                }
                else if (paintMode == paintmode.custom)
                {
                    pattern = currentCustomPattern;
                }

                for (int i = 0; i < currenRotationPattern; i++)
                {
                    pattern = Patterns.RotatePatternBy90(pattern);
                }

                //Calculate pattern size
                int he = pattern.GetLength(0);
                int wi = pattern.GetLength(1);

                //Calculate mousePosition on image
                float widthMultiplicator  = (float)image.Width / (float)gridPictureBox.Width;
                float heightMultiplicator = (float)image.Height / (float)gridPictureBox.Height;
                int   mouseX = (int)(e.X * widthMultiplicator);
                int   mouseY = (int)(e.Y * heightMultiplicator);

                //Every row of pattern
                for (int y = 0; y < he; y++)
                {
                    //Every collumn of pattern
                    for (int x = 0; x < wi; x++)
                    {
                        int yoffset = 0;
                        int xoffset = 0;

                        //Make infinity work (Check if out of bounds. If yes subtract/add height/width)
                        if (mouseY + y >= image.Height && y > 0)
                        {
                            yoffset = -image.Height;
                        }
                        if (mouseX + x >= image.Width && x > 0)
                        {
                            xoffset = -image.Width;
                        }


                        //If pattern has on position x and y a living cell then add it to the grid
                        if (pattern[y, x])
                        {
                            PaintAt(mouseX + x + xoffset, mouseY + y + yoffset, true);
                        }
                    }
                }
            }
            //Select something on the grid
            else if (paintMode == paintmode.select && paintColor == MainForm.livingcolor)
            {
                if (!keepSelectionOnLeftclick)
                {
                    float widthMultiplicator  = (float)image.Width / (float)gridPictureBox.Width;
                    float heightMultiplicator = (float)image.Height / (float)gridPictureBox.Height;
                    mouseStartPosSelectX = (int)(e.X * widthMultiplicator);
                    mouseStartPosSelectY = (int)(e.Y * heightMultiplicator);

                    isSelected = false;
                }
                else
                {
                    keepSelectionOnLeftclick = false;
                }
            }
            else if (paintMode == paintmode.grab && paintColor == MainForm.deadcolor)
            {
                float widthMultiplicator  = (float)image.Width / (float)gridPictureBox.Width;
                float heightMultiplicator = (float)image.Height / (float)gridPictureBox.Height;
                int   mouseX = (int)(e.X * widthMultiplicator);
                int   mouseY = (int)(e.Y * heightMultiplicator);

                grabPositionX = mouseX;
                grabPositionY = mouseY;
            }

            //Display the image on the Grid Form
            syncImage(true);

            //Reset paintmode to hand
            if (paintColor == MainForm.deadcolor && paintMode != paintmode.grab)
            {
                paintMode = paintmode.draw;
            }
        }
Ejemplo n.º 4
0
 void getDataAndClose(paintmode mode)
 {
     this.stopSystem = false;
     getValuesFromFormControls();
     this.data.mode = mode;
     this.Close();
 }