//Set Paintmode to custom private void paintmode_custom_Click(object sender, EventArgs e) { string senderName = ((Button)sender).Name.Split('_')[0]; gf.currentCustomPattern = Patterns.GetPattern(senderName); gf.paintMode = GridForm.paintmode.custom; }
//Add new Paintmode private void paintmode_add_Click(object sender, EventArgs e) { //Open File Dialog to select pattern you want to import using (OpenFileDialog dlg = new OpenFileDialog()) { //Set Dialog Title dlg.Title = "Load Pattern"; //Set Filters dlg.Filter = "Image files (*.jpg, *.png, *.bmp) | *.jpg; *.png; *.bmp"; //If User presses okay if (dlg.ShowDialog() == DialogResult.OK) { string namePattern = Microsoft.VisualBasic.Interaction.InputBox("Name your Pattern", "New Pattern", "Patternname");; namePattern.Replace("_", " "); if (Patterns.HasName(namePattern)) { return; } if (namePattern != "" && namePattern != null) { Patterns.AddPattern(dlg.FileName, namePattern); AddPatternButton(namePattern, Patterns.GenerateIcon(namePattern)); } } } }
public void UpdateHoverPositionAndRotation(int eX, int eY) { 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)(eX * widthMultiplicator); int mouseY = (int)(eY * 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]) { PaintHoverAt(mouseX + x + xoffset, mouseY + y + yoffset, true); } } } }
public void InitializePatternButtons() { foreach (Button btn in patternButtons) { this.Controls.Remove(btn); } patternButtons.Clear(); foreach (string name in Patterns.GetAllPatternNames()) { Debug.WriteLine(name); Bitmap bmp = Patterns.GetBitmap(name); if (bmp != null) { AddPatternButton(name, Patterns.GenerateIcon(name)); } } UpdateAddPatternButton(); }
private void selectionAsPatternToolStripMenuItem_Click(object sender, EventArgs e) { string namePattern = Microsoft.VisualBasic.Interaction.InputBox("Name your Pattern", "New Pattern", "Patternname");; namePattern.Replace("_", " "); if (Patterns.HasName(namePattern)) { return; } if (namePattern != "" && namePattern != null) { Rectangle rectangle = new Rectangle(mouseStartPosSelectX, mouseStartPosSelectY, mouseEndPosSelectX - mouseStartPosSelectX + 1, mouseEndPosSelectY - mouseStartPosSelectY + 1); if (mouseStartPosSelectX > mouseEndPosSelectX && mouseStartPosSelectY < mouseEndPosSelectY) { rectangle = new Rectangle(mouseEndPosSelectX, mouseStartPosSelectY, mouseStartPosSelectX - mouseEndPosSelectX + 1, mouseEndPosSelectY - mouseStartPosSelectY + 1); } else if (mouseStartPosSelectX > mouseEndPosSelectX && mouseStartPosSelectY > mouseEndPosSelectY) { rectangle = new Rectangle(mouseEndPosSelectX, mouseEndPosSelectY, mouseStartPosSelectX - mouseEndPosSelectX + 1, mouseStartPosSelectY - mouseEndPosSelectY + 1); } else if (mouseStartPosSelectX < mouseEndPosSelectX && mouseStartPosSelectY > mouseEndPosSelectY) { rectangle = new Rectangle(mouseStartPosSelectX, mouseEndPosSelectY, mouseEndPosSelectX - mouseStartPosSelectX + 1, mouseStartPosSelectY - mouseEndPosSelectY + 1); } //Crop useless areas Bitmap bmp = image.Clone(rectangle, System.Drawing.Imaging.PixelFormat.Format32bppRgb); Patterns.AddPattern(bmp, namePattern); MainForm.instance.InitializePatternButtons(); } }
//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; } }
//Removes a Pattern private void removeToolStripMenuItem_Click(object sender, EventArgs e) { Patterns.RemovePattern(buttonNameRightClicked); InitializePatternButtons(); }