private void repaintPictureBoxFromList(PointF?mouseClickPoint = null) { // Pen handle for wall painting Pen wallPen; // ImageAttributes for transparent image drawing float[][] matrixItems = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 0.5f, 0 }, new float[] { 0, 0, 0, 0, 1 } }; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imageAtt = new ImageAttributes(); imageAtt.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(blueprintPictureBox.Image); g.FillRectangle(Brushes.White, 0, 0, blueprintPictureBox.Image.Width, blueprintPictureBox.Image.Height); for (int i = 0; i < createdFurnitureListBox.Items.Count; ++i) { if (!(createdFurnitureListBox.Items[i] is FurnitureListBoxItem)) { continue; } FurnitureListBoxItem item = createdFurnitureListBox.Items[i] as FurnitureListBoxItem; if (item.type == "wall.png") { if (item == createdFurnitureListBox.SelectedItem) { wallPen = new Pen(Color.FromArgb(128, 0, 0, 0), 10); // semi transparent } else { wallPen = new Pen(Color.Black, 10); // opaque } wallPen.LineJoin = LineJoin.Round; //Matrix for rotation if (item.path != null) { Matrix rotateMatrix = null; if (item.path.PointCount > 1) { rotateMatrix = new Matrix(); rotateMatrix.RotateAt(item.rotation, item.path.PathPoints[0]); } if (item.path.PointCount > 0) { GraphicsPath graphicsPath = (GraphicsPath)item.path.Clone(); if (wallPath == item.path && mouseClickPoint != null) { graphicsPath.AddLine(graphicsPath.GetLastPoint(), (PointF)mouseClickPoint); } if (rotateMatrix != null) { graphicsPath.Transform(rotateMatrix); } g.DrawPath(wallPen, graphicsPath); } else if (item.path.PointCount == 0 && mouseClickPoint != null) { g.DrawLine(wallPen, pathStart, (PointF)mouseClickPoint); } } } else { if (item.rotation != 0) { g.TranslateTransform(item.displayPoint.X, item.displayPoint.Y); g.RotateTransform(item.rotation); g.TranslateTransform(-item.displayPoint.X, -item.displayPoint.Y); } PointF point = new PointF(item.displayPoint.X - item.displayImage.Width / 2, item.displayPoint.Y - item.displayImage.Height / 2); if (item == createdFurnitureListBox.SelectedItem) { g.DrawImage(item.displayImage, new Rectangle((int)point.X, (int)point.Y, item.displayImage.Width, item.displayImage.Height), 0.0f, 0.0f, item.displayImage.Width, item.displayImage.Height, GraphicsUnit.Pixel, imageAtt); } else { g.DrawImage(item.displayImage, point); } } g.ResetTransform(); } g.Dispose(); blueprintPictureBox.Refresh(); }
private void openBlueprintToolStripMenuItem_Click(object sender, EventArgs e) { ResourceManager resources = new ResourceManager(typeof(RoomPlanner)); using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = resources.GetString("FileDialog.Filter"); openFileDialog.FilterIndex = 2; if (openFileDialog.ShowDialog() == DialogResult.OK) { string[] fileName = openFileDialog.FileName.Split('.'); if (fileName[fileName.Length - 1] != "bp") { MessageBox.Show(resources.GetString("OpenFileDialog.Fail")); return; } //Read the contents of the file into a stream Stream fileStream = null; try { fileStream = openFileDialog.OpenFile(); if (fileStream != null) { BinaryFormatter formatter = new BinaryFormatter(); List <FurnitureListBoxItem> listBoxItems = new List <FurnitureListBoxItem>(); listBoxItems = (List <FurnitureListBoxItem>)formatter.Deserialize(fileStream); newBitmapInPictureBox(splitContainer.Panel1.Width, splitContainer.Panel1.Height); createdFurnitureListBox.Items.Clear(); if (selectedButton != null) { selectedButton.BackColor = Color.White; selectedButton = null; } wallPainting = false; wallPath = new GraphicsPath(); for (int i = 0; i < listBoxItems.Count; ++i) { createdFurnitureListBox.Items.Add(listBoxItems[i]); } if (listBoxItems[0].pictureBoxSize.HasValue) { newBitmapInPictureBox(listBoxItems[0].pictureBoxSize.Value.X, listBoxItems[0].pictureBoxSize.Value.Y); FurnitureListBoxItem tmp = createdFurnitureListBox.Items[0] as FurnitureListBoxItem; tmp.pictureBoxSize = null; } } } catch (System.Runtime.Serialization.SerializationException) { MessageBox.Show(resources.GetString("OpenFileDialog.Fail")); return; } finally { if (fileStream != null) { fileStream.Close(); } } repaintPictureBoxFromList(); createdFurnitureListBox.RefreshItems(); MessageBox.Show(resources.GetString("OpenFileDialog.Success")); } } }
private void blueprintPictureBox_MouseDown(object sender, MouseEventArgs e) { if (selectedButton == null /*|| e.X > blueprintPictureBox.Image.Width || e.Y > blueprintPictureBox.Image.Height*/) { if (createdFurnitureListBox.SelectedItem != null && createdFurnitureListBox.SelectedItem is FurnitureListBoxItem selectedItem) { if ((string)selectedItem.displayImage.Tag == "wall.png") { Pen wallPen = new Pen(Color.FromArgb(128, 0, 0, 0), 10); wallPen.LineJoin = LineJoin.Round; if (selectedItem.path != null && selectedItem.path.IsOutlineVisible(e.Location, wallPen)) { movingSelectedItem = true; selectedItem.anchorPoint = new PointF(selectedItem.displayPoint.X - e.X, selectedItem.displayPoint.Y - e.Y); return; } } else if (Math.Abs(selectedItem.displayPoint.X - e.X) < selectedItem.displayImage.Width / 2 && (Math.Abs(selectedItem.displayPoint.Y - e.Y) < selectedItem.displayImage.Height / 2)) { movingSelectedItem = true; selectedItem.anchorPoint = new PointF(selectedItem.displayPoint.X - e.X, selectedItem.displayPoint.Y - e.Y); return; } } for (int i = 0; i < createdFurnitureListBox.Items.Count; ++i) { if (!(createdFurnitureListBox.Items[i] is FurnitureListBoxItem)) { continue; } FurnitureListBoxItem item = createdFurnitureListBox.Items[i] as FurnitureListBoxItem; if (item.type == "wall.png" && item.path != null) { Pen wallPen = new Pen(Color.Black, 10); wallPen.LineJoin = LineJoin.Round; Matrix rotateMatrix = null; if (item.path.PointCount > 1) { rotateMatrix = new Matrix(); rotateMatrix.RotateAt(item.rotation, item.path.PathPoints[0]); } GraphicsPath graphicsPath = (GraphicsPath)item.path.Clone(); if (wallPath == item.path) { graphicsPath.AddLine(graphicsPath.GetLastPoint(), e.Location); } if (rotateMatrix != null) { graphicsPath.Transform(rotateMatrix); } if (graphicsPath.IsOutlineVisible(e.Location, wallPen)) { createdFurnitureListBox.ClearSelected(); createdFurnitureListBox.SetSelected(i, true); movingSelectedItem = true; item.anchorPoint = new PointF(item.displayPoint.X - e.X, item.displayPoint.Y - e.Y); } } else { if (Math.Abs(item.displayPoint.X - e.X) < item.displayImage.Width / 2 && (Math.Abs(item.displayPoint.Y - e.Y) < item.displayImage.Height / 2)) { createdFurnitureListBox.ClearSelected(); createdFurnitureListBox.SetSelected(i, true); movingSelectedItem = true; item.anchorPoint = new PointF(item.displayPoint.X - e.X, item.displayPoint.Y - e.Y); } } } if (!movingSelectedItem) { createdFurnitureListBox.ClearSelected(); } return; } if (selectedButton == wallButton) { if (e.Button == MouseButtons.Right) { resetWallPainting(); selectedButton.BackColor = Color.White; selectedButton = null; repaintPictureBoxFromList(); return; } if (wallPainting == false) { pathStart = new PointF(e.X, e.Y); wallPainting = true; createdFurnitureListBox.Items.Add(new FurnitureListBoxItem(selectedButton.BackgroundImage, pathStart, wallPath)); return; } if (wallPath.PointCount == 0) { wallPath.AddLine(pathStart, new PointF(e.X, e.Y)); } else { wallPath.AddLine(wallPath.GetLastPoint(), new PointF(e.X, e.Y)); } repaintPictureBoxFromList(e.Location); } else if (e.Button != MouseButtons.Right) { PointF point = new PointF(e.X - selectedButton.BackgroundImage.Width / 2, e.Y - selectedButton.BackgroundImage.Height / 2); Graphics g = Graphics.FromImage(blueprintPictureBox.Image); g.DrawImage(selectedButton.BackgroundImage, point); blueprintPictureBox.Refresh(); g.Dispose(); createdFurnitureListBox.Items.Add(new FurnitureListBoxItem(selectedButton.BackgroundImage, e.Location)); selectedButton.BackColor = Color.White; selectedButton = null; } }