264 lines
8.8 KiB
C#
264 lines
8.8 KiB
C#
/// file: Form1.cs
|
|
/// Author: Maxime Rohmer <maxluligames@gmail.com>
|
|
/// Brief: Class that is controlling the view
|
|
/// Version: 0.1.0
|
|
/// Date: 25/05/2022
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Paint_2
|
|
{
|
|
public partial class PaintForm : Form
|
|
{
|
|
const int WINDOW_OFFSET = 10;
|
|
const string DEFAULT_FILEPATH = "C:/Paint2/Drawings/";
|
|
Sketch sketch;
|
|
List<PaintTool> toolList;
|
|
bool drawing = false;
|
|
bool randomColor = false;
|
|
Random rnd;
|
|
Color hoveringColor;
|
|
public PaintForm()
|
|
{
|
|
InitializeComponent();
|
|
rnd = new Random();
|
|
toolList = new List<PaintTool>();
|
|
toolList.Add(new Pencil("Default Pencil"));
|
|
toolList.Add(new DotPencil("Dotted Line"));
|
|
sketch = new Sketch(new Size(canvas.Width, canvas.Height), toolList);
|
|
tmrRefresh.Enabled = true;
|
|
|
|
RefreshUi();
|
|
}
|
|
private Point MousePositionToCanvasPosition()
|
|
{
|
|
//return new Point(MousePosition.X - canvas.Location.X, MousePosition.Y - canvas.Location.Y);
|
|
return canvas.PointToClient(MousePosition);
|
|
}
|
|
private void canvas_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
drawing = true;
|
|
if (randomColor)
|
|
{
|
|
sketch.StartDrawing(MousePositionToCanvasPosition(), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), (int)nupPencilWidth.Value);
|
|
}
|
|
else
|
|
{
|
|
sketch.StartDrawing(MousePositionToCanvasPosition(),sketch.CurrentTool.Color, (int)nupPencilWidth.Value);
|
|
}
|
|
}
|
|
private void canvas_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
sketch.AddDrawingPoint(MousePositionToCanvasPosition());
|
|
drawing = false;
|
|
RefreshUi();
|
|
}
|
|
|
|
private void tmrRefresh_Tick(object sender, EventArgs e)
|
|
{
|
|
if (drawing)
|
|
{
|
|
sketch.AddDrawingPoint(MousePositionToCanvasPosition());
|
|
}
|
|
RefreshUi();
|
|
}
|
|
private void BtnColor_Click(object sender, EventArgs e)
|
|
{
|
|
Button button = sender as Button;
|
|
sketch.ChangePaintToolColor(button.BackColor);
|
|
tmrRefresh_Tick(sender, e);
|
|
}
|
|
private string ColorToString(Color color)
|
|
{
|
|
// Expected Hex:FFFFFF R:255 G:255 B:255
|
|
string result = "";
|
|
int toBase = 16;
|
|
result += "Hex:" + Convert.ToString(color.R, toBase) + Convert.ToString(color.G, toBase) + Convert.ToString(color.B, toBase) + " ";
|
|
result += "R:" + color.R + " G:" + color.G + " B:" + color.B;
|
|
return result;
|
|
}
|
|
private void ForceRefresh()
|
|
{
|
|
canvas.Image = sketch.ForcePaint();
|
|
RefreshUi();
|
|
}
|
|
private void RefreshUi()
|
|
{
|
|
canvas.Image = sketch.Paint();
|
|
lblSelectedColor.Text = ColorToString(sketch.CurrentTool.Color);
|
|
btnSelectedColor.BackColor = sketch.CurrentTool.Color;
|
|
hoveringColor = GetHoverColor();
|
|
lblHoveringColor.Text = ColorToString(hoveringColor);
|
|
btnHoveringColor.BackColor = hoveringColor;
|
|
|
|
List<Color> colorHistory = sketch.CurrentTool.GetLastColors(4);
|
|
btnColorHistory1.BackColor = colorHistory[0];
|
|
btnColorHistory2.BackColor = colorHistory[1];
|
|
btnColorHistory3.BackColor = colorHistory[2];
|
|
btnColorHistory4.BackColor = colorHistory[3];
|
|
|
|
lsbTools.DataSource = toolList;
|
|
|
|
lblWidth.Text = "Width: "+canvas.Width.ToString();
|
|
lblHeight.Text = "Height: "+canvas.Height.ToString();
|
|
|
|
if (randomColor)
|
|
{
|
|
btnRandomColor.ForeColor = Color.Green;
|
|
}
|
|
else
|
|
{
|
|
btnRandomColor.ForeColor = Color.Red;
|
|
}
|
|
}
|
|
private Color GetHoverColor()
|
|
{
|
|
Point cursorLocation = MousePositionToCanvasPosition();
|
|
Color pixel;
|
|
if (cursorLocation.X > 0 && cursorLocation.X < canvas.Width && cursorLocation.Y > 0 && cursorLocation.Y < canvas.Height && cursorLocation.X < Width && cursorLocation.Y < Height)
|
|
{
|
|
int DEBUG = Width;
|
|
pixel = (canvas.Image as Bitmap).GetPixel(cursorLocation.X, cursorLocation.Y);
|
|
}
|
|
else
|
|
{
|
|
pixel = Color.FromArgb(0x00, 0x00, 0x00);
|
|
}
|
|
return pixel;
|
|
|
|
}
|
|
private void btnClear_Click(object sender, EventArgs e)
|
|
{
|
|
sketch.Clear();
|
|
ForceRefresh();
|
|
}
|
|
|
|
private void nupPencilWidth_ValueChanged(object sender, EventArgs e)
|
|
{
|
|
sketch.ChangePaintToolWidth((int)nupPencilWidth.Value);
|
|
}
|
|
|
|
private void btnSetColor_Click(object sender, EventArgs e)
|
|
{
|
|
string value = tbxColorHex.Text;
|
|
int fromBase = 16;
|
|
value = value.Replace("#", String.Empty);
|
|
int R, G, B;
|
|
try
|
|
{
|
|
R = Convert.ToInt32(String.Concat(value[0], value[1]), fromBase);
|
|
G = Convert.ToInt32(String.Concat(value[2], value[3]), fromBase);
|
|
B = Convert.ToInt32(String.Concat(value[4], value[4]), fromBase);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
MessageBox.Show("Please enter a valid hexadecimal value");
|
|
tbxColorHex.Text = "#FFFFFF";
|
|
R = 255;
|
|
G = 255;
|
|
B = 255;
|
|
}
|
|
sketch.ChangePaintToolColor(Color.FromArgb(R, G, B));
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
int R, G, B;
|
|
R = (int)nupRed.Value;
|
|
G = (int)nupGreen.Value;
|
|
B = (int)nupBlue.Value;
|
|
if (R > 255)
|
|
{
|
|
R = 255;
|
|
}
|
|
if (G > 255)
|
|
{
|
|
G = 25;
|
|
}
|
|
if (B > 255)
|
|
{
|
|
B = 255;
|
|
}
|
|
sketch.ChangePaintToolColor(Color.FromArgb(R, G, B));
|
|
}
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
{
|
|
string fileName = tbxProjectName.Text;
|
|
Bitmap image = (Bitmap)canvas.Image;
|
|
|
|
if (!Directory.Exists(DEFAULT_FILEPATH))
|
|
{
|
|
Directory.CreateDirectory(DEFAULT_FILEPATH);
|
|
}
|
|
if (!Directory.Exists(DEFAULT_FILEPATH + fileName))
|
|
{
|
|
Directory.CreateDirectory(DEFAULT_FILEPATH + fileName);
|
|
}
|
|
|
|
image.Save(DEFAULT_FILEPATH + fileName + "/" + fileName + ".png", System.Drawing.Imaging.ImageFormat.Png);
|
|
}
|
|
|
|
private void btnSaveCopy_Click(object sender, EventArgs e)
|
|
{
|
|
string fileName = tbxProjectName.Text;
|
|
int version = 1;
|
|
Bitmap image = (Bitmap)canvas.Image;
|
|
|
|
if (!Directory.Exists(DEFAULT_FILEPATH))
|
|
{
|
|
Directory.CreateDirectory(DEFAULT_FILEPATH);
|
|
}
|
|
if (!Directory.Exists(DEFAULT_FILEPATH + fileName))
|
|
{
|
|
Directory.CreateDirectory(DEFAULT_FILEPATH + fileName);
|
|
}
|
|
while (File.Exists(DEFAULT_FILEPATH + fileName + "/" + fileName + version + ".png"))
|
|
{
|
|
version += 1;
|
|
}
|
|
image.Save(DEFAULT_FILEPATH + fileName + "/" + fileName + version + ".png", System.Drawing.Imaging.ImageFormat.Png);
|
|
}
|
|
|
|
private void lsbTools_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
sketch.ChangeTool(lsbTools.SelectedIndex);
|
|
RefreshUi();
|
|
}
|
|
|
|
private void PaintForm_Resize(object sender, EventArgs e)
|
|
{
|
|
canvas.Size = new Size(this.Width - (this.Width - panelTools.Location.X) - canvas.Location.X - WINDOW_OFFSET, this.Height-(this.Height-panelHoverColor.Location.Y) - WINDOW_OFFSET - (panelFile.Location.Y + panelFile.Height));
|
|
sketch.Resize(canvas.Size);
|
|
RefreshUi();
|
|
}
|
|
|
|
private void btnRandomColor_Click(object sender, EventArgs e)
|
|
{
|
|
randomColor = !randomColor;
|
|
RefreshUi();
|
|
}
|
|
|
|
private void btnUndo_Click(object sender, EventArgs e)
|
|
{
|
|
sketch.Undo();
|
|
ForceRefresh();
|
|
}
|
|
|
|
private void btnRedo_Click(object sender, EventArgs e)
|
|
{
|
|
sketch.Redo();
|
|
ForceRefresh();
|
|
}
|
|
}
|
|
}
|