|
@@ -21,19 +21,43 @@ namespace SketchAssistant
|
|
|
InitializeComponent();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public enum ProgramState
|
|
|
+ {
|
|
|
+ Idle,
|
|
|
+ Draw
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProgramState currentState;
|
|
|
|
|
|
OpenFileDialog openFileDialogLeft = new OpenFileDialog();
|
|
|
|
|
|
Image leftImage = null;
|
|
|
|
|
|
Image rightImage = null;
|
|
|
+
|
|
|
+ List<Point> currentLine;
|
|
|
+
|
|
|
+ List<Tuple<bool,Line>> lineList = new List<Tuple<bool, Line>>();
|
|
|
+
|
|
|
+ bool mousePressed = false;
|
|
|
+
|
|
|
+ Point currentCursorPosition;
|
|
|
+
|
|
|
+ Graphics graph = null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
|
{
|
|
|
+ currentState = ProgramState.Idle;
|
|
|
this.DoubleBuffered = true;
|
|
|
-
|
|
|
- pictureBoxLeft.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxLeft_Paint);
|
|
|
- pictureBoxRight.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxRight_Paint);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -41,92 +65,194 @@ namespace SketchAssistant
|
|
|
{
|
|
|
this.Refresh();
|
|
|
}
|
|
|
-
|
|
|
- private void pictureBoxLeft_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
|
|
|
+
|
|
|
+
|
|
|
+ private void loadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
-
|
|
|
- if(leftImage != null)
|
|
|
+ openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
|
|
|
+ if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
|
|
|
{
|
|
|
+ toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
|
|
|
+ leftImage = Image.FromFile(openFileDialogLeft.FileName);
|
|
|
pictureBoxLeft.Image = leftImage;
|
|
|
+
|
|
|
+ this.Refresh();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void pictureBoxRight_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
|
|
|
+
|
|
|
+ private void drawButton_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
-
|
|
|
- if (rightImage != null)
|
|
|
+ if(rightImage != null)
|
|
|
{
|
|
|
- pictureBoxRight.Image = rightImage;
|
|
|
+ if (currentState.Equals(ProgramState.Draw))
|
|
|
+ {
|
|
|
+ ChangeState(ProgramState.Idle);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ChangeState(ProgramState.Draw);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
|
|
|
+
|
|
|
+ private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
-
|
|
|
+ currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
|
|
|
}
|
|
|
|
|
|
- private void fileToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
+
|
|
|
+ private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void loadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
- {
|
|
|
- openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
|
|
|
- if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
|
|
|
+ mousePressed = true;
|
|
|
+ if (currentState.Equals(ProgramState.Draw))
|
|
|
{
|
|
|
- toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
|
|
|
- leftImage = Image.FromFile(openFileDialogLeft.FileName);
|
|
|
-
|
|
|
- this.Refresh();
|
|
|
- pictureBoxLeft.Refresh();
|
|
|
+ currentLine = new List<Point>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void toolStripStatusLabel1_Click(object sender, EventArgs e)
|
|
|
+
|
|
|
+ private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
-
|
|
|
+ mousePressed = false;
|
|
|
+ if (currentState.Equals(ProgramState.Draw))
|
|
|
+ {
|
|
|
+ lineList.Add(new Tuple<bool, Line>(true, new Line(currentLine)));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void canvasButton_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
-
|
|
|
+ DrawEmptyCanvas();
|
|
|
}
|
|
|
|
|
|
- private void pictureBoxLeft_Click(object sender, EventArgs e)
|
|
|
+
|
|
|
+ private void drawTimer_Tick(object sender, EventArgs e)
|
|
|
{
|
|
|
-
|
|
|
+ if (currentState.Equals(ProgramState.Draw) && mousePressed)
|
|
|
+ {
|
|
|
+ currentLine.Add(currentCursorPosition);
|
|
|
+ Line drawline = new Line(currentLine);
|
|
|
+ drawline.DrawLine(graph);
|
|
|
+ pictureBoxRight.Image = rightImage;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- private void toolStripButton1_Click(object sender, EventArgs e)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void DrawEmptyCanvas()
|
|
|
{
|
|
|
if (leftImage == null)
|
|
|
{
|
|
|
rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
|
|
|
- using (Graphics grp = Graphics.FromImage(rightImage))
|
|
|
- {
|
|
|
- grp.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
|
|
|
- }
|
|
|
+ graph = Graphics.FromImage(rightImage);
|
|
|
+ graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
|
|
|
+ pictureBoxRight.Image = rightImage;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
rightImage = new Bitmap(leftImage.Width, leftImage.Height);
|
|
|
- using (Graphics grp = Graphics.FromImage(rightImage))
|
|
|
+ graph = Graphics.FromImage(rightImage);
|
|
|
+ graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
|
|
|
+ pictureBoxRight.Image = rightImage;
|
|
|
+ }
|
|
|
+ this.Refresh();
|
|
|
+ pictureBoxRight.Refresh();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void RedrawRightImage()
|
|
|
+ {
|
|
|
+ DrawEmptyCanvas();
|
|
|
+ foreach (Tuple<bool, Line> lineBoolTuple in lineList)
|
|
|
+ {
|
|
|
+ if (lineBoolTuple.Item1)
|
|
|
{
|
|
|
- grp.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
|
|
|
+ lineBoolTuple.Item2.DrawLine(graph);
|
|
|
}
|
|
|
}
|
|
|
- this.Refresh();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void ChangeState(ProgramState newState)
|
|
|
+ {
|
|
|
+ switch (currentState)
|
|
|
+ {
|
|
|
+ case ProgramState.Draw:
|
|
|
+ drawButton.CheckState = CheckState.Unchecked;
|
|
|
+ drawTimer.Enabled = false;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ switch (newState)
|
|
|
+ {
|
|
|
+ case ProgramState.Draw:
|
|
|
+ drawButton.CheckState = CheckState.Checked;
|
|
|
+ drawTimer.Enabled = true;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ currentState = newState;
|
|
|
pictureBoxRight.Refresh();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Point ConvertCoordinates(Point cursorPosition)
|
|
|
+ {
|
|
|
+ Point realCoordinates = new Point(5,3);
|
|
|
+ if(pictureBoxRight.Image == null)
|
|
|
+ {
|
|
|
+ return cursorPosition;
|
|
|
+ }
|
|
|
+
|
|
|
+ int widthImage = pictureBoxRight.Image.Width;
|
|
|
+ int heightImage = pictureBoxRight.Image.Height;
|
|
|
+ int widthBox = pictureBoxRight.Width;
|
|
|
+ int heightBox = pictureBoxRight.Height;
|
|
|
+
|
|
|
+ float imageRatio = (float)widthImage / (float)heightImage;
|
|
|
+ float containerRatio = (float)widthBox / (float)heightBox;
|
|
|
+
|
|
|
+ if (imageRatio >= containerRatio)
|
|
|
+ {
|
|
|
+
|
|
|
+ float zoomFactor = (float)widthImage / (float)widthBox;
|
|
|
+ float scaledHeight = heightImage / zoomFactor;
|
|
|
+ float filler = (heightBox - scaledHeight) / 2;
|
|
|
+ realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
|
|
|
+ realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ float zoomFactor = (float)heightImage / (float)heightBox;
|
|
|
+ float scaledWidth = widthImage / zoomFactor;
|
|
|
+ float filler = (widthBox - scaledWidth) / 2;
|
|
|
+ realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
|
|
|
+ realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
|
|
|
+ }
|
|
|
+ return realCoordinates;
|
|
|
+ }
|
|
|
}
|
|
|
}
|