Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions samples/AvalonDraw/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,16 +617,18 @@ private void LoadProperties(SvgElement element)
private async void SvgView_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
var point = e.GetPosition(SvgView);
if (_toolService.CurrentTool == Tool.MultiSelect && e.GetCurrentPoint(SvgView).Properties.IsLeftButtonPressed)
var hasStart = SvgView.TryGetPicturePoint(point, out var picturePt);
if (IsMultiSelectionMode(e.KeyModifiers) && e.GetCurrentPoint(SvgView).Properties.IsLeftButtonPressed &&
!(_multiSelected.Count > 0 && !_multiBounds.IsEmpty && hasStart && _multiBounds.Contains(picturePt.X, picturePt.Y)))
{
if (_pathService.IsEditing)
_pathService.Stop();
_boxSelecting = true;
_boxStart = point;
_boxEnd = point;
if (SvgView.TryGetPicturePoint(point, out var sp))
if (hasStart)
{
_boxStartPicture = new SK.SKPoint((float)sp.X, (float)sp.Y);
_boxStartPicture = new SK.SKPoint((float)picturePt.X, (float)picturePt.Y);
_boxEndPicture = _boxStartPicture;
}
e.Pointer.Capture(SvgView);
Expand Down Expand Up @@ -892,19 +894,22 @@ private async void SvgView_OnPointerPressed(object? sender, PointerPressedEventA
}
else
{
if (_toolService.CurrentTool == Tool.MultiSelect && _multiSelected.Count > 0 && !_multiBounds.IsEmpty)
if (_multiSelected.Count > 0 && !_multiBounds.IsEmpty && _multiBounds.Contains(pp.X, pp.Y))
{
if (_multiBounds.Contains(pp.X, pp.Y))
{
StartDrag(_multiSelected[0], new Shim.SKPoint(pp.X, pp.Y), e.Pointer);
SvgView.InvalidateVisual();
return;
}
StartDrag(_multiSelected[0], new Shim.SKPoint(pp.X, pp.Y), e.Pointer);
SvgView.InvalidateVisual();
return;
}

_selectedDrawable = skSvg.HitTestDrawables(pp).FirstOrDefault();
_selectedElement = null;
_selectedSvgElement = null;
if (_multiSelected.Count > 0)
{
_multiSelected.Clear();
_multiDrawables.Clear();
_multiBounds = SK.SKRect.Empty;
}
if (_pathService.IsEditing)
_pathService.Stop();
if (_polyEditing)
Expand Down Expand Up @@ -1153,10 +1158,24 @@ private void SvgView_OnPointerReleased(object? sender, PointerReleasedEventArgs
var p1 = _boxStartPicture;
var p2 = _boxEndPicture;
var rect = new Shim.SKRect(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y), Math.Max(p1.X, p2.X), Math.Max(p1.Y, p2.Y));
var skRect = new SK.SKRect(rect.Left, rect.Top, rect.Right, rect.Bottom);
var elements = svg.HitTestElements(rect).OfType<SvgVisualElement>();
if (!_includeHidden)
elements = elements.Where(IsElementVisible);
var hits = elements.ToList();
var root = svg.Drawable as DrawableBase;
var hits = new List<SvgVisualElement>();
var leftToRight = p2.X >= p1.X;
foreach (var el in elements)
{
if (root is null)
continue;
var dr = FindDrawable(root, el);
if (dr is null)
continue;
var b = SelectionService.GetBoundsRect(GetBoundsInfo(dr));
if (!leftToRight || SelectionService.ContainsRect(skRect, b))
hits.Add(el);
}
var mods = e.KeyModifiers;
if ((mods & KeyModifiers.Shift) != 0)
{
Expand Down Expand Up @@ -1213,7 +1232,7 @@ private void SvgView_OnPointerReleased(object? sender, PointerReleasedEventArgs
}
_hitElements = hits;
var element = _hitElements[_hitIndex];
if (_toolService.CurrentTool == Tool.MultiSelect)
if (IsMultiSelectionMode(e.KeyModifiers))
{
var mods = e.KeyModifiers;
if ((mods & KeyModifiers.Shift) != 0)
Expand Down Expand Up @@ -1245,6 +1264,12 @@ private void SvgView_OnPointerReleased(object? sender, PointerReleasedEventArgs
}
else
{
if (_multiSelected.Count > 0)
{
_multiSelected.Clear();
_multiDrawables.Clear();
_multiBounds = SK.SKRect.Empty;
}
_selectedElement = element;
_selectedDrawable = skSvg.HitTestDrawables(_pressPoint).FirstOrDefault(d => d.Element == _selectedElement);
_selectedSvgElement = _selectedElement;
Expand All @@ -1261,6 +1286,12 @@ private void SvgView_OnPointerReleased(object? sender, PointerReleasedEventArgs
_selectedDrawable = null;
_selectedElement = null;
_selectedSvgElement = null;
if (_multiSelected.Count > 0)
{
_multiSelected.Clear();
_multiDrawables.Clear();
_multiBounds = SK.SKRect.Empty;
}
UpdateSelectedDrawable();
SvgView.InvalidateVisual();
}
Expand Down Expand Up @@ -1410,7 +1441,7 @@ private void VisibilityToggle_Click(object? sender, RoutedEventArgs e)

private void StartDrag(SvgVisualElement element, Shim.SKPoint start, IPointer pointer)
{
if (_toolService.CurrentTool == Tool.MultiSelect && _multiSelected.Count > 1)
if (_multiSelected.Count > 1)
{
SaveUndoState();
_multiDragInfos = new List<DragInfo>();
Expand Down Expand Up @@ -1519,6 +1550,11 @@ private float GetCanvasScale(SkiaSharp.SKCanvas? canvas = null)
return (float)SvgView.Zoom;
}

private bool IsMultiSelectionMode(KeyModifiers modifiers)
=> _toolService.CurrentTool == Tool.MultiSelect ||
(_toolService.CurrentTool == Tool.Select &&
(modifiers & (KeyModifiers.Control | KeyModifiers.Shift)) != 0);

private static SK.SKPoint Mid(SK.SKPoint a, SK.SKPoint b) => new((a.X + b.X) / 2f, (a.Y + b.Y) / 2f);

private SelectionService.BoundsInfo GetBoundsInfo(DrawableBase drawable)
Expand Down Expand Up @@ -2131,7 +2167,7 @@ private void RemoveElementMenuItem_Click(object? sender, RoutedEventArgs e)
if (_document is null)
return;

if (_toolService.CurrentTool == Tool.MultiSelect && _multiSelected.Count > 1)
if (_multiSelected.Count > 1)
{
SaveUndoState();
foreach (var el in _multiSelected.ToList())
Expand Down Expand Up @@ -2687,6 +2723,12 @@ private void MainWindow_OnKeyDown(object? sender, KeyEventArgs e)
_selectedDrawable = null;
_selectedElement = null;
_selectedSvgElement = null;
if (_multiSelected.Count > 0)
{
_multiSelected.Clear();
_multiDrawables.Clear();
_multiBounds = SK.SKRect.Empty;
}
DocumentTree.SelectedItem = null;
SvgView.InvalidateVisual();
}
Expand Down Expand Up @@ -3334,7 +3376,7 @@ private void FlipSelected(bool horizontal)
return;

var list = new List<(SvgVisualElement Element, DrawableBase Drawable)>();
if (_toolService.CurrentTool == Tool.MultiSelect && _multiSelected.Count > 0)
if (_multiSelected.Count > 0)
{
for (int i = 0; i < _multiSelected.Count && i < _multiDrawables.Count; i++)
list.Add((_multiSelected[i], _multiDrawables[i]));
Expand Down
6 changes: 6 additions & 0 deletions samples/AvalonDraw/Services/SelectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public static SK.SKRect GetBoundsRect(BoundsInfo b)
return new SK.SKRect(left, top, right, bottom);
}

public static bool ContainsRect(SK.SKRect outer, SK.SKRect inner)
{
return outer.Left <= inner.Left && outer.Right >= inner.Right &&
outer.Top <= inner.Top && outer.Bottom >= inner.Bottom;
}

public int HitHandle(BoundsInfo b, SK.SKPoint pt, float scale, out SK.SKPoint center)
{
center = b.Center;
Expand Down
Loading