From 8342eff07e0e4d7452458970e5c5f0dd7f46f916 Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:21:14 +0100 Subject: [PATCH] Completed Shape.cs and Program.cs --- Shapes/Program.cs | 17 +++++++++++++++- Shapes/Shapes/Shape.cs | 45 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Shapes/Program.cs b/Shapes/Program.cs index 82ffd95..dee87a6 100644 --- a/Shapes/Program.cs +++ b/Shapes/Program.cs @@ -33,7 +33,22 @@ void Run() void HandleClick(ClickEvent @event) { - // TODO - handle click at location + // 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(); } diff --git a/Shapes/Shapes/Shape.cs b/Shapes/Shapes/Shape.cs index 5757fba..7fc1314 100644 --- a/Shapes/Shapes/Shape.cs +++ b/Shapes/Shapes/Shape.cs @@ -1,21 +1,54 @@ -using System.Numerics; +using Avalonia; +using Avalonia.Media; namespace Shapes.Shapes; public abstract class Shape { private static int _nextId = 1; + // Ripped from https://github.com/haslingerm/simple-xplat-drawing/blob/master/SimpleDrawing/LeoCanvas.cs#L26 + protected const double DefaultLineThickness = 1D; public int Id { get; } - protected Vector2 Scale; - protected Vector2 Position; + protected Point Scale; + protected Point Position; + protected double? LineThickness; + protected IBrush? LineColor; + protected IBrush? FillColor; - protected Shape(Vector2 position, Vector2 scale) + /// + /// The base constructor for all shapes + /// + /// The position of the shape + /// The scale of the shape + /// The thickness of the line (optional) + /// The color of the line (optional) + /// The color of the fill (optional) + protected Shape(Point position, Point scale, double? lineThickness = null, IBrush? lineColor = null, IBrush? fillColor = null) { Id = _nextId++; Position = position; Scale = scale; + LineThickness = lineThickness; + LineColor = lineColor; + FillColor = fillColor; } - public abstract bool DrawSelf(Vector2 position = default, Vector2 scale = default); - public abstract bool PointInShape(Vector2 mousePoint); + /// + /// Draws the shape + /// Not specified values will use the stored ones at the instance creation + /// + /// The position of the shape (optional) + /// The scale of the shape (optional) + /// The thickness of the line (optional) + /// The color of the line (optional) + /// The color of the fill (optional) + /// True if the drawing was successful, false otherwise + public abstract bool DrawSelf(Point position = default, Point scale = default, double? lineThickness = null, IBrush? lineColor = null, IBrush? fillColor = null); + + /// + /// Checks if the is inside the shape + /// + /// The mouse point + /// True if the mouse point is inside the shape, false otherwise + public abstract bool PointInShape(Point mousePoint); }