From b04a8cdfdc49941034d18eda4933c2c4f1f6b71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Tue, 18 Jan 2022 21:26:56 +0100 Subject: [PATCH] Update SelectionTool.cs --- VectorPaint/ViewModels/Tools/SelectionTool.cs | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) 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();