using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
// This is the code for your desktop app.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
namespace SketchAssistant
{
public partial class Form1 : Form, MVP_View
{
public Form1()
{
InitializeComponent();
ProgramPresenter = new MVP_Presenter(this);
}
/**********************************/
/*** CLASS VARIABLES START HERE ***/
/**********************************/
//important: add new variables only at the end of the list to keep the order of definition consistent with the order in which they are returned by GetAllVariables()
public enum ButtonState
{
Enabled,
Disabled,
Active
}
///
/// Different Program States
///
public enum ProgramState
{
Idle,
Draw,
Delete
}
///
/// Dialog to select a file.
///
OpenFileDialog openFileDialog = new OpenFileDialog();
///
/// All Lines in the current session
///
List> rightLineList = new List>();
///
/// Queue for the cursorPositions
///
Queue cursorPositions = new Queue();
///
/// The Presenter Component of the MVP-Model
///
MVP_Presenter ProgramPresenter;
/******************************************/
/*** FORM SPECIFIC FUNCTIONS START HERE ***/
/******************************************/
private void Form1_Load(object sender, EventArgs e)
{
this.DoubleBuffered = true;
}
///
/// Resize Function connected to the form resize event, will refresh the form when it is resized
///
private void Form1_Resize(object sender, System.EventArgs e)
{
ProgramPresenter.Resize(new Tuple(pictureBoxLeft.Width, pictureBoxLeft.Height),
new Tuple(pictureBoxRight.Width, pictureBoxRight.Height));
this.Refresh();
}
///
/// Import button, will open an OpenFileDialog
///
private void examplePictureToolStripMenuItem_Click(object sender, EventArgs e)
{
ProgramPresenter.ExamplePictureToolStripMenuItemClick();
}
///
/// Changes the state of the program to drawing
///
private void drawButton_Click(object sender, EventArgs e)
{
ProgramPresenter.ChangeState(true);
}
///
/// Changes the state of the program to deletion
///
private void deleteButton_Click(object sender, EventArgs e)
{
ProgramPresenter.ChangeState(false);
}
///
/// Undo an Action.
///
private void undoButton_Click(object sender, EventArgs e)
{
ProgramPresenter.Undo();
}
///
/// Redo an Action.
///
private void redoButton_Click(object sender, EventArgs e)
{
ProgramPresenter.Redo();
}
///
/// Detect Keyboard Shortcuts.
///
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z)
{
ProgramPresenter.Undo();
}
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Y)
{
ProgramPresenter.Redo();
}
}
///
/// The Picture box is clicked.
///
private void pictureBoxRight_Click(object sender, EventArgs e)
{
ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Click);
}
///
/// Get current Mouse positon within the right picture box.
///
private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
{
ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e);
}
///
/// Hold left mouse button to start drawing.
///
private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
{
ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
}
///
/// Lift left mouse button to stop drawing and add a new Line.
///
private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
{
ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
}
///
/// Button to create a new Canvas. Will create an empty image
/// which is the size of the left image, if there is one.
/// If there is no image loaded the canvas will be the size of the right picture box
///
private void canvasButton_Click(object sender, EventArgs e)
{
ProgramPresenter.NewCanvas();
}
///
/// Add a Point on every tick to the Drawpath.
/// Or detect lines for deletion on every tick
///
private void mouseTimer_Tick(object sender, EventArgs e)
{
ProgramPresenter.Tick();
}
/*************************/
/*** PRESENTER -> VIEW ***/
/*************************/
///
/// Enables the timer of the View, which will tick the Presenter.
///
public void EnableTimer()
{
mouseTimer.Enabled = true;
}
///
/// A function that opens a file dialog and returns the filename.
///
/// The filter that should be applied to the new Dialog.
/// Returns the FileName and the SafeFileName if the user correctly selects a file,
/// else returns a tuple with empty strigns
public Tuple openNewDialog(String Filter)
{
openFileDialog.Filter = Filter;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
return new Tuple(openFileDialog.FileName, openFileDialog.SafeFileName);
}
else
{
return new Tuple("", "");
}
}
///
/// Sets the contents of the load status indicator label.
///
/// The new contents
public void SetToolStripLoadStatus(String message)
{
toolStripLoadStatus.Text = message;
}
///
/// Sets the contents of the last action taken indicator label.
///
/// The new contents
public void SetLastActionTakenText(String message)
{
lastActionTakenLabel.Text = message;
}
///
/// Changes the states of a tool strip button.
///
/// The name of the button.
/// The new state of the button.
public void SetToolStripButtonStatus(String buttonName, ButtonState state)
{
ToolStripButton buttonToChange;
switch (buttonName)
{
case "canvasButton":
buttonToChange = canvasButton;
break;
case "drawButton":
buttonToChange = drawButton;
break;
case "deleteButton":
buttonToChange = deleteButton;
break;
case "undoButton":
buttonToChange = undoButton;
break;
case "redoButton":
buttonToChange = redoButton;
break;
default:
Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
return;
}
switch (state)
{
case ButtonState.Active:
buttonToChange.Checked = true;
break;
case ButtonState.Disabled:
buttonToChange.Checked = false;
buttonToChange.Enabled = false;
break;
case ButtonState.Enabled:
buttonToChange.Checked = false;
buttonToChange.Enabled = true;
break;
}
}
///
/// Displays an image in the left Picture box.
///
/// The new image.
public void DisplayInLeftPictureBox(Image img)
{
pictureBoxLeft.Image = img;
pictureBoxLeft.Refresh();
}
///
/// Displays an image in the right Picture box.
///
/// The new image.
public void DisplayInRightPictureBox(Image img)
{
pictureBoxRight.Image = img;
pictureBoxRight.Refresh();
}
///
/// shows the given info message in a popup and asks the user to aknowledge it
///
/// the message to show
public void ShowInfoMessage(String message)
{
MessageBox.Show(message);
}
///
/// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
///
/// The message of the warning.
/// True if the user confirms (Yes), negative if he doesn't (No)
public bool ShowWarning(String message)
{
return (MessageBox.Show(message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes);
}
}
}