218 lines
7.8 KiB
C#
218 lines
7.8 KiB
C#
/// file: Pencil.cs
|
|
/// Author: Maxime Rohmer <maxluligames@gmail.com>
|
|
/// Brief: Paint tool that is kind of the default tool
|
|
/// Version: 0.1.0
|
|
/// Date: 25/05/2022
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Paint_2
|
|
{
|
|
public class Pencil : PaintTool
|
|
{
|
|
const int POST_PROCESSING_PRECISION = 3;
|
|
|
|
private List<List<Point>> _drawings;
|
|
private List<List<Point>> _drawingsRedo;
|
|
private List<Color> _colors;
|
|
private List<Color> _colorsRedo;
|
|
private List<int> _widths;
|
|
private List<int> _widthsRedo;
|
|
private Color _color;
|
|
private string _name;
|
|
private int _width;
|
|
|
|
public List<List<Point>> Drawings { get => _drawings; set => _drawings = value; }
|
|
public List<List<Point>> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; }
|
|
public List<Color> Colors { get => _colors; set => _colors = value; }
|
|
public List<Color> ColorsRedo { get => _colorsRedo; set => _colorsRedo = value; }
|
|
public List<int> Widths { get => _widths; set => _widths = value; }
|
|
public List<int> WidthsRedo { get => _widthsRedo; set => _widthsRedo = value; }
|
|
public Color Color { get => _color; set => _color = value; }
|
|
public string Name { get => _name; set => _name = value; }
|
|
public int Width { get => _width; set => _width = value; }
|
|
|
|
public Pencil(string name)
|
|
{
|
|
Drawings = new List<List<Point>>();
|
|
DrawingsRedo = new List<List<Point>>();
|
|
Colors = new List<Color>();
|
|
ColorsRedo = new List<Color>();
|
|
Widths = new List<int>();
|
|
WidthsRedo = new List<int>();
|
|
Name = name;
|
|
}
|
|
public void Add(Point point)
|
|
{
|
|
Drawings[Drawings.Count - 1].Add(point);
|
|
}
|
|
public void Paint(Bitmap canvas)
|
|
{
|
|
Graphics gr = Graphics.FromImage(canvas);
|
|
int drawingCounter = 0;
|
|
Size pointSize;
|
|
|
|
foreach (List<Point> drawing in Drawings)
|
|
{
|
|
int pointCounter = 0;
|
|
foreach (Point p in drawing)
|
|
{
|
|
pointSize = new Size(Widths[drawingCounter], Widths[drawingCounter]);
|
|
gr.FillEllipse(new SolidBrush(Colors[drawingCounter]), new Rectangle(new Point(p.X - pointSize.Width / 2, p.Y - pointSize.Height / 2), pointSize));
|
|
if (pointCounter > 0)
|
|
{
|
|
gr.DrawLine(new Pen(Colors[drawingCounter], Widths[drawingCounter]), drawing[pointCounter -1], p);
|
|
}
|
|
pointCounter += 1;
|
|
}
|
|
drawingCounter += 1;
|
|
}
|
|
}
|
|
public void Start(Color color, int width)
|
|
{
|
|
Color = color;
|
|
Width = width;
|
|
List<Point> points = new List<Point>();
|
|
Drawings.Add(points);
|
|
Colors.Add(Color);
|
|
Widths.Add(Width);
|
|
}
|
|
public void Clear()
|
|
{
|
|
Drawings = new List<List<Point>>();
|
|
DrawingsRedo = new List<List<Point>>();
|
|
Colors = new List<Color>();
|
|
ColorsRedo = new List<Color>();
|
|
Widths = new List<int>();
|
|
WidthsRedo = new List<int>();
|
|
}
|
|
public Bitmap Stop(Bitmap bmp)
|
|
{
|
|
for (int i = 0; i < Drawings.Count; i++)
|
|
{
|
|
List<Point> Drawing = Drawings[i];
|
|
//bmp = PostProcessing(Drawing, bmp, Widths[i]);
|
|
}
|
|
return bmp;
|
|
}
|
|
private Color GetRandomColor()
|
|
{
|
|
Random rnd = new Random();
|
|
return Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
|
|
}
|
|
private void ContinuousBezierGenerator(Graphics gr, List<Point> points, int width)
|
|
{
|
|
if (points.Count >= 4 && points.Count % 2 == 0)
|
|
{
|
|
float precision = 0.01f;
|
|
for (float t = 0; t <= 1; t += precision)
|
|
{
|
|
List<Point> DumpList = new List<Point>();
|
|
List<Point> WorkingList = new List<Point>(points);
|
|
|
|
while (WorkingList.Count != 1)
|
|
{
|
|
if (WorkingList.Count > 2)
|
|
{
|
|
for (int pos = 0; pos < WorkingList.Count - 1; pos++)
|
|
{
|
|
DumpList.Add(Lerp(WorkingList[pos], WorkingList[pos + 1], t));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DumpList.Add(Lerp(WorkingList[0], WorkingList[1], precision));
|
|
}
|
|
WorkingList = new List<Point>(DumpList);
|
|
DumpList = new List<Point>();
|
|
}
|
|
gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(WorkingList[0].X - width / 2, WorkingList[0].Y - width / 2, width, width));
|
|
}
|
|
}
|
|
}
|
|
private Point Lerp(Point start, Point end, float t)
|
|
{
|
|
int xDiff = end.X - start.X;
|
|
int yDiff = end.Y - start.Y;
|
|
int resultX = start.X + (int)(t * xDiff);
|
|
int resultY = start.Y + (int)(t * yDiff);
|
|
Point result = new Point(resultX, resultY);
|
|
return result;
|
|
}
|
|
private Bitmap PostProcessing(List<Point> Drawing,Bitmap bmp,int width)
|
|
{
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
ContinuousBezierGenerator(gr, Drawing,width);
|
|
return bmp;
|
|
}
|
|
public List<Color> GetLastColors(int colorNumber)
|
|
{
|
|
List<Color> result = new List<Color>();
|
|
if (Colors.Count <= colorNumber)
|
|
{
|
|
//We need to fill with black color
|
|
for (int i = Colors.Count; i > 0; i--)
|
|
{
|
|
result.Add(Colors[(Colors.Count) - i]);
|
|
}
|
|
for (int i = colorNumber - Colors.Count; i > 0; i--)
|
|
{
|
|
result.Add(Color.FromArgb(0x00, 0x00, 0x00));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = colorNumber; i > 0; i--)
|
|
{
|
|
result.Add(Colors[(Colors.Count) - i]);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public override string ToString()
|
|
{
|
|
return Name;
|
|
}
|
|
|
|
public void Undo()
|
|
{
|
|
int last = Drawings.Count - 1;
|
|
if (Drawings.Count > 1)
|
|
{
|
|
DrawingsRedo.Add(Drawings[last]);
|
|
Drawings.RemoveAt(Drawings.Count - 1);
|
|
ColorsRedo.Add(Colors[last]);
|
|
Colors.RemoveAt(Colors.Count - 1);
|
|
WidthsRedo.Add(Widths[last]);
|
|
Widths.RemoveAt(Widths.Count - 1);
|
|
}
|
|
else
|
|
{
|
|
DrawingsRedo.Add(Drawings[0]);
|
|
Drawings.Clear();
|
|
ColorsRedo.Add(Colors[0]);
|
|
Colors.Clear();
|
|
WidthsRedo.Add(Widths[0]);
|
|
Widths.Clear();
|
|
}
|
|
}
|
|
|
|
public void Redo()
|
|
{
|
|
if (DrawingsRedo.Count > 0 && WidthsRedo.Count > 0 && ColorsRedo.Count > 0)
|
|
{
|
|
Drawings.Add(DrawingsRedo[DrawingsRedo.Count - 1]);
|
|
DrawingsRedo.RemoveAt(DrawingsRedo.Count - 1);
|
|
Colors.Add(ColorsRedo[ColorsRedo.Count - 1]);
|
|
ColorsRedo.RemoveAt(ColorsRedo.Count - 1);
|
|
Widths.Add(WidthsRedo[WidthsRedo.Count - 1]);
|
|
WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
|
|
}
|
|
}
|
|
}
|
|
}
|