This commit is contained in:
Wiesław Šoltés 2022-01-18 21:26:56 +01:00
Родитель ce70c08d51
Коммит b04a8cdfdc
1 изменённых файлов: 37 добавлений и 8 удалений

Просмотреть файл

@ -1,4 +1,5 @@
using Avalonia; using System.Collections.Generic;
using Avalonia;
using Avalonia.Input; using Avalonia.Input;
using VectorPaint.ViewModels.Drawables; using VectorPaint.ViewModels.Drawables;
@ -6,11 +7,13 @@ namespace VectorPaint.ViewModels.Tools;
public class SelectionTool : Tool public class SelectionTool : Tool
{ {
private Drawable? _drawable; private readonly HashSet<Drawable> _selected = new();
private Point _start; private Point _start;
public override string Title => "Selection"; public override string Title => "Selection";
public HashSet<Drawable> Selected => _selected;
public override void OnPointerPressed(IDrawing drawing, PointerPressedEventArgs e) public override void OnPointerPressed(IDrawing drawing, PointerPressedEventArgs e)
{ {
var point = e.GetCurrentPoint(drawing.Input).Position; var point = e.GetCurrentPoint(drawing.Input).Position;
@ -18,28 +21,54 @@ public class SelectionTool : Tool
var drawable = drawing.HitTest(point); var drawable = drawing.HitTest(point);
if (drawable is { }) 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; _start = point;
e.Pointer.Capture(drawing.Input); e.Pointer.Capture(drawing.Input);
} }
else
{
_selected.Clear();
}
} }
public override void OnPointerReleased(IDrawing drawing, PointerReleasedEventArgs e) public override void OnPointerReleased(IDrawing drawing, PointerReleasedEventArgs e)
{ {
if (_drawable is { }) if (!Equals(e.Pointer.Captured, drawing.Input))
{ {
e.Pointer.Capture(null); return;
_drawable = null;
} }
e.Pointer.Capture(null);
} }
public override void OnPointerMoved(IDrawing drawing, PointerEventArgs e) 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; var point = e.GetCurrentPoint(drawing.Input).Position;
_drawable.Move(point - _start); foreach (var drawable in _selected)
{
drawable.Move(point - _start);
}
_start = point; _start = point;
drawing.Invalidate(); drawing.Invalidate();