diff --git a/VectorPaint/ViewModels/Tools/SelectionTool.cs b/VectorPaint/ViewModels/Tools/SelectionTool.cs index 23189e5..c40a8bf 100644 --- a/VectorPaint/ViewModels/Tools/SelectionTool.cs +++ b/VectorPaint/ViewModels/Tools/SelectionTool.cs @@ -1,4 +1,5 @@ -using Avalonia; +using System.Collections.Generic; +using Avalonia; using Avalonia.Input; using VectorPaint.ViewModels.Drawables; @@ -6,11 +7,13 @@ namespace VectorPaint.ViewModels.Tools; public class SelectionTool : Tool { - private Drawable? _drawable; + private readonly HashSet _selected = new(); private Point _start; public override string Title => "Selection"; + public HashSet Selected => _selected; + public override void OnPointerPressed(IDrawing drawing, PointerPressedEventArgs e) { var point = e.GetCurrentPoint(drawing.Input).Position; @@ -18,28 +21,54 @@ public class SelectionTool : Tool var drawable = drawing.HitTest(point); if (drawable is { }) { - _drawable = drawable; + if (e.KeyModifiers == KeyModifiers.Shift) + { + _selected.Add(drawable); + } + else + { + if (!_selected.Contains(drawable)) + { + _selected.Clear(); + _selected.Add(drawable); + } + } + _start = point; e.Pointer.Capture(drawing.Input); } + else + { + _selected.Clear(); + } } public override void OnPointerReleased(IDrawing drawing, PointerReleasedEventArgs e) { - if (_drawable is { }) + if (!Equals(e.Pointer.Captured, drawing.Input)) { - e.Pointer.Capture(null); - _drawable = null; + return; } + + e.Pointer.Capture(null); } public override void OnPointerMoved(IDrawing drawing, PointerEventArgs e) { - if (_drawable is { }) + if (!Equals(e.Pointer.Captured, drawing.Input)) + { + return; + } + + if (_selected.Count > 0) { var point = e.GetCurrentPoint(drawing.Input).Position; - _drawable.Move(point - _start); + foreach (var drawable in _selected) + { + drawable.Move(point - _start); + } + _start = point; drawing.Invalidate();