Completed Shape.cs and Program.cs

This commit is contained in:
MarcUs7i 2025-03-16 23:21:14 +01:00
parent 05b49929ba
commit 8342eff07e
2 changed files with 55 additions and 7 deletions

View file

@ -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();
}

View file

@ -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)
/// <summary>
/// The base constructor for all shapes
/// </summary>
/// <param name="position">The position of the shape</param>
/// <param name="scale">The scale of the shape</param>
/// <param name="lineThickness">The thickness of the line (optional)</param>
/// <param name="lineColor">The color of the line (optional)</param>
/// <param name="fillColor">The color of the fill (optional)</param>
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);
/// <summary>
/// Draws the shape
/// Not specified values will use the stored ones at the instance creation
/// </summary>
/// <param name="position">The position of the shape (optional)</param>
/// <param name="scale">The scale of the shape (optional)</param>
/// <param name="lineThickness">The thickness of the line (optional)</param>
/// <param name="lineColor">The color of the line (optional)</param>
/// <param name="fillColor">The color of the fill (optional)</param>
/// <returns>True if the drawing was successful, false otherwise</returns>
public abstract bool DrawSelf(Point position = default, Point scale = default, double? lineThickness = null, IBrush? lineColor = null, IBrush? fillColor = null);
/// <summary>
/// Checks if the <see cref="mousePoint"/> is inside the shape
/// </summary>
/// <param name="mousePoint">The mouse point</param>
/// <returns>True if the mouse point is inside the shape, false otherwise</returns>
public abstract bool PointInShape(Point mousePoint);
}