ex-inh-03-shapes/Shapes/Program.cs
2025-03-16 23:21:14 +01:00

68 lines
1.4 KiB
C#

using System.Text;
using Shapes;
using Shapes.Shapes;
using SimpleDrawing;
using SimpleDrawing.Core;
Console.OutputEncoding = Encoding.UTF8;
const int Size = 600;
var shapes = new List<Shape>();
var generator = new ShapeGenerator(Size, Size);
LeoCanvas.Init(Run, Size, Size, clickAction: HandleClick, windowTitle: "Shapes");
return;
void Run()
{
try
{
// initial rendering
Redraw();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
void HandleClick(ClickEvent @event)
{
// Check if clicked on existing shape
foreach (var shape in shapes.ToList())
{
if (shape.PointInShape(@event.ClickedPoint))
{
shapes.Remove(shape);
Console.WriteLine($"Removed shape with id {shape.Id}");
Redraw();
return;
}
}
// Create new shape
var generatedShape = generator.CreateNewShape(@event.ClickedPoint);
shapes.Add(generatedShape);
Console.WriteLine($"Added shape with id {generatedShape.Id}");
Redraw();
}
void Redraw()
{
LeoCanvas.Clear();
foreach (var shape in shapes)
{
if (!shape.DrawSelf())
{
Console.WriteLine($"Couldn't draw shape with id {shape.Id} 😭");
}
}
LeoCanvas.Render();
}