11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Windows ;
4- using System . Windows . Controls ;
55using System . Windows . Input ;
66
77namespace Nodify
88{
99 /// <summary>
10- /// Helps with selecting <see cref="ItemContainer"/>s and updating the <see cref="NodifyEditor.SelectedArea"/> and <see cref="NodifyEditor.IsSelecting"/> properties .
10+ /// Helps with selecting <see cref="ItemContainer"/>s.
1111 /// </summary>
12- public sealed class SelectionHelper
12+ internal sealed class SelectionHelper
1313 {
14- private readonly NodifyEditor _host ;
1514 private Point _startLocation ;
15+ private Point _endLocation ;
1616 private SelectionType _selectionType ;
1717 private bool _isRealtime ;
18- private IReadOnlyList < ItemContainer > _initialSelection = new List < ItemContainer > ( ) ;
19-
20- /// <summary>Constructs a new instance of a <see cref="SelectionHelper"/>.</summary>
21- /// <param name="host">The editor to select items from.</param>
22- public SelectionHelper ( NodifyEditor host )
23- => _host = host ;
24-
25- /// <summary>Available selection logic.</summary>
26- public enum SelectionType
27- {
28- /// <summary>Replaces the old selection.</summary>
29- Replace ,
30- /// <summary>Removes items from existing selection.</summary>
31- Remove ,
32- /// <summary>Adds items to the current selection.</summary>
33- Append ,
34- /// <summary>Inverts the selection.</summary>
35- Invert
36- }
18+ private IReadOnlyCollection < ItemContainer > _items = Array . Empty < ItemContainer > ( ) ;
19+ private IReadOnlyList < ItemContainer > _initialSelection = Array . Empty < ItemContainer > ( ) ;
20+ private Rect _selectedArea ;
3721
3822 /// <summary>Attempts to start a new selection.</summary>
23+ /// <param name="containers">The containers that can be part of the selection.</param>
3924 /// <param name="location">The location inside the graph.</param>
4025 /// <param name="selectionType">The type of selection.</param>
4126 /// <remarks>Will not do anything if selection is in progress.</remarks>
42- public void Start ( Point location , SelectionType selectionType )
27+ public Rect Start ( IEnumerable < ItemContainer > containers , Point location , SelectionType selectionType , bool realtime )
4328 {
44- if ( ! _host . IsSelecting )
45- {
46- _selectionType = selectionType ;
47- _initialSelection = _host . SelectedContainers ;
29+ _items = containers . Where ( x => x . IsSelectable ) . ToList ( ) ;
30+ _initialSelection = containers . Where ( x => x . IsSelected ) . ToList ( ) ;
4831
49- _isRealtime = _host . EnableRealtimeSelection ;
50- _startLocation = location ;
32+ _selectionType = selectionType ;
5133
52- _host . SelectedArea = new Rect ( ) ;
53- _host . IsSelecting = true ;
54- }
34+ _isRealtime = realtime ;
35+ _startLocation = location ;
36+ _endLocation = location ;
37+
38+ _selectedArea = new Rect ( ) ;
39+ return _selectedArea ;
5540 }
5641
5742 /// <summary>Update the end location for the selection.</summary>
5843 /// <param name="endLocation">An absolute location.</param>
59- public void Update ( Point endLocation )
44+ public Rect Update ( Point endLocation )
6045 {
61- double left = endLocation . X < _startLocation . X ? endLocation . X : _startLocation . X ;
62- double top = endLocation . Y < _startLocation . Y ? endLocation . Y : _startLocation . Y ;
63- double width = Math . Abs ( endLocation . X - _startLocation . X ) ;
64- double height = Math . Abs ( endLocation . Y - _startLocation . Y ) ;
46+ _endLocation = endLocation ;
47+
48+ double left = _endLocation . X < _startLocation . X ? _endLocation . X : _startLocation . X ;
49+ double top = _endLocation . Y < _startLocation . Y ? _endLocation . Y : _startLocation . Y ;
50+ double width = Math . Abs ( _endLocation . X - _startLocation . X ) ;
51+ double height = Math . Abs ( _endLocation . Y - _startLocation . Y ) ;
6552
66- _host . SelectedArea = new Rect ( left , top , width , height ) ;
53+ _selectedArea = new Rect ( left , top , width , height ) ;
6754
6855 if ( _isRealtime )
6956 {
70- PreviewSelection ( _host . SelectedArea ) ;
57+ PreviewSelection ( _selectedArea ) ;
7158 }
59+
60+ return _selectedArea ;
7261 }
7362
74- /// <summary>Commits the current selection to the editor .</summary>
75- public void End ( )
63+ /// <summary>Increase the selected area by the specified amount .</summary>
64+ public Rect Update ( Vector amount )
7665 {
77- if ( _host . IsSelecting )
78- {
79- PreviewSelection ( _host . SelectedArea ) ;
66+ _endLocation += amount ;
8067
81- _host . ApplyPreviewingSelection ( ) ;
82- _host . IsSelecting = false ;
83- }
68+ return Update ( _endLocation ) ;
8469 }
8570
86- /// <summary>Aborts the current selection.</summary>
87- public void Abort ( )
71+ /// <summary>Commits the current selection to the editor .</summary>
72+ public Rect End ( )
8873 {
89- if ( _host . IsSelecting )
90- {
91- _host . ClearPreviewingSelection ( ) ;
92- _host . IsSelecting = false ;
93- }
74+ PreviewSelection ( _selectedArea ) ;
75+ _items = Array . Empty < ItemContainer > ( ) ;
76+ _initialSelection = Array . Empty < ItemContainer > ( ) ;
77+
78+ return _selectedArea ;
9479 }
9580
9681 private void PreviewSelection ( Rect area )
@@ -128,10 +113,8 @@ private void PreviewSelection(Rect area)
128113
129114 private void PreviewUnselectAll ( )
130115 {
131- ItemCollection items = _host . Items ;
132- for ( var i = 0 ; i < items . Count ; i ++ )
116+ foreach ( var container in _items )
133117 {
134- var container = ( ItemContainer ) _host . ItemContainerGenerator . ContainerFromIndex ( i ) ;
135118 container . IsPreviewingSelection = false ;
136119 }
137120 }
@@ -145,10 +128,8 @@ private void PreviewSelectArea(Rect area, bool append = false, bool fit = false)
145128
146129 if ( area . X != 0 || area . Y != 0 || area . Width > 0 || area . Height > 0 )
147130 {
148- ItemCollection items = _host . Items ;
149- for ( var i = 0 ; i < items . Count ; i ++ )
131+ foreach ( var container in _items )
150132 {
151- var container = ( ItemContainer ) _host . ItemContainerGenerator . ContainerFromIndex ( i ) ;
152133 if ( container . IsSelectableInArea ( area , fit ) )
153134 {
154135 container . IsPreviewingSelection = true ;
@@ -159,10 +140,8 @@ private void PreviewSelectArea(Rect area, bool append = false, bool fit = false)
159140
160141 private void PreviewUnselectArea ( Rect area , bool fit = false )
161142 {
162- ItemCollection items = _host . Items ;
163- for ( var i = 0 ; i < items . Count ; i ++ )
143+ foreach ( var container in _items )
164144 {
165- var container = ( ItemContainer ) _host . ItemContainerGenerator . ContainerFromIndex ( i ) ;
166145 if ( container . IsSelectableInArea ( area , fit ) )
167146 {
168147 container . IsPreviewingSelection = false ;
@@ -180,10 +159,8 @@ private static void PreviewSelectContainers(IReadOnlyList<ItemContainer> contain
180159
181160 private void PreviewInvertSelection ( Rect area , bool fit = false )
182161 {
183- ItemCollection items = _host . Items ;
184- for ( var i = 0 ; i < items . Count ; i ++ )
162+ foreach ( var container in _items )
185163 {
186- var container = ( ItemContainer ) _host . ItemContainerGenerator . ContainerFromIndex ( i ) ;
187164 if ( container . IsSelectableInArea ( area , fit ) )
188165 {
189166 container . IsPreviewingSelection = ! container . IsPreviewingSelection ;
0 commit comments