From 472fa825a81da4b8cc6d4a78294313784576c7c6 Mon Sep 17 00:00:00 2001 From: miroiu Date: Wed, 8 Jan 2025 14:25:28 +0200 Subject: [PATCH] Unselect connections on EndSelecting, allowing selection cancellation to work correctly --- Nodify/Editor/NodifyEditor.Selecting.cs | 18 ++++------- Nodify/Utilities/SelectionHelper.cs | 43 +++++++++++++++++++------ 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Nodify/Editor/NodifyEditor.Selecting.cs b/Nodify/Editor/NodifyEditor.Selecting.cs index d92a7cf2..ffd53e87 100644 --- a/Nodify/Editor/NodifyEditor.Selecting.cs +++ b/Nodify/Editor/NodifyEditor.Selecting.cs @@ -362,7 +362,6 @@ public void BeginSelecting(Point location, SelectionType type = SelectionType.Re return; } - UnselectAllConnections(); SelectedArea = _selection.Start(ItemContainers, location, type, EnableRealtimeSelection); IsSelecting = true; } @@ -398,6 +397,11 @@ public void EndSelecting() return; } + if (_selection.Type == SelectionType.Replace) + { + UnselectAllConnections(); + } + SelectedArea = _selection.End(); ApplyPreviewingSelection(); IsSelecting = false; @@ -418,7 +422,7 @@ public void CancelSelecting() if (IsSelecting) { - ClearPreviewingSelection(); + _selection.Cancel(); IsSelecting = false; } } @@ -445,16 +449,6 @@ private void ApplyPreviewingSelection() EndUpdateSelectedItems(); } - private void ClearPreviewingSelection() - { - ItemCollection items = Items; - for (var i = 0; i < items.Count; i++) - { - var container = (ItemContainer)ItemContainerGenerator.ContainerFromIndex(i); - container.IsPreviewingSelection = null; - } - } - #endregion #region Selection Handlers diff --git a/Nodify/Utilities/SelectionHelper.cs b/Nodify/Utilities/SelectionHelper.cs index b9cecc8b..02a611de 100644 --- a/Nodify/Utilities/SelectionHelper.cs +++ b/Nodify/Utilities/SelectionHelper.cs @@ -14,12 +14,13 @@ internal sealed class SelectionHelper { private Point _startLocation; private Point _endLocation; - private SelectionType _selectionType; private bool _isRealtime; - private IReadOnlyCollection _items = Array.Empty(); + private IReadOnlyList _items = Array.Empty(); private IReadOnlyList _initialSelection = Array.Empty(); private Rect _selectedArea; + public SelectionType Type { get; private set; } + /// Attempts to start a new selection. /// The containers that can be part of the selection. /// The location inside the graph. @@ -30,7 +31,7 @@ public Rect Start(IEnumerable containers, Point location, Selecti _items = containers.Where(x => x.IsSelectable).ToList(); _initialSelection = containers.Where(x => x.IsSelected).ToList(); - _selectionType = selectionType; + Type = selectionType; _isRealtime = realtime; _startLocation = location; @@ -79,9 +80,18 @@ public Rect End() return _selectedArea; } + public void Cancel() + { + ClearPreviewingSelection(); + _items = Array.Empty(); + _initialSelection = Array.Empty(); + } + + #region Selection preview + private void PreviewSelection(Rect area) { - switch (_selectionType) + switch (Type) { case SelectionType.Replace: PreviewSelectArea(area); @@ -114,9 +124,9 @@ private void PreviewSelection(Rect area) private void PreviewUnselectAll() { - foreach (var container in _items) + for (int i = 0; i < _items.Count; i++) { - container.IsPreviewingSelection = false; + _items[i].IsPreviewingSelection = false; } } @@ -129,8 +139,9 @@ private void PreviewSelectArea(Rect area, bool append = false, bool fit = false) if (area.X != 0 || area.Y != 0 || area.Width > 0 || area.Height > 0) { - foreach (var container in _items) + for (int i = 0; i < _items.Count; i++) { + ItemContainer? container = _items[i]; if (container.IsSelectableInArea(area, fit)) { container.IsPreviewingSelection = true; @@ -141,8 +152,9 @@ private void PreviewSelectArea(Rect area, bool append = false, bool fit = false) private void PreviewUnselectArea(Rect area, bool fit = false) { - foreach (var container in _items) + for (int i = 0; i < _items.Count; i++) { + ItemContainer? container = _items[i]; if (container.IsSelectableInArea(area, fit)) { container.IsPreviewingSelection = false; @@ -152,7 +164,7 @@ private void PreviewUnselectArea(Rect area, bool fit = false) private static void PreviewSelectContainers(IReadOnlyList containers) { - for (var i = 0; i < containers.Count; i++) + for (int i = 0; i < containers.Count; i++) { containers[i].IsPreviewingSelection = true; } @@ -160,14 +172,25 @@ private static void PreviewSelectContainers(IReadOnlyList contain private void PreviewInvertSelection(Rect area, bool fit = false) { - foreach (var container in _items) + for (int i = 0; i < _items.Count; i++) { + ItemContainer? container = _items[i]; if (container.IsSelectableInArea(area, fit)) { container.IsPreviewingSelection = !container.IsPreviewingSelection; } } } + + private void ClearPreviewingSelection() + { + for (int i = 0; i < _items.Count; i++) + { + _items[i].IsPreviewingSelection = null; + } + } + + #endregion } internal static class SelectionGesturesExtensions