@@ -120,6 +120,8 @@ impl<F: DrawingFeature> Data<F> {
120
120
}
121
121
122
122
pub fn delete_feature ( & mut self , k : slotmap:: DefaultKey ) -> bool {
123
+ self . selected_map . remove ( & k) ;
124
+
123
125
match self . features . remove ( k) {
124
126
Some ( _v) => {
125
127
// Find and also remove any features dependent on what we just removed.
@@ -151,12 +153,43 @@ impl<F: DrawingFeature> Data<F> {
151
153
}
152
154
}
153
155
154
- pub fn delete_selection ( & mut self ) {
156
+ pub fn selection_delete ( & mut self ) {
155
157
let elements: Vec < _ > = self . selected_map . drain ( ) . map ( |( k, _) | k) . collect ( ) ;
156
158
for k in elements {
157
159
self . delete_feature ( k) ;
158
160
}
159
161
}
162
+
163
+ pub fn select_feature ( & mut self , feature : & slotmap:: DefaultKey , select : bool ) {
164
+ let currently_selected = self . selected_map . contains_key ( feature) ;
165
+ if currently_selected && !select {
166
+ self . selected_map . remove ( feature) ;
167
+ } else if !currently_selected && select {
168
+ let next_idx = self . selected_map . values ( ) . fold ( 0 , |acc, x| acc. max ( * x) ) + 1 ;
169
+ self . selected_map . insert ( feature. clone ( ) , next_idx) ;
170
+ }
171
+ }
172
+
173
+ pub fn select_features_in_rect ( & mut self , rect : egui:: Rect , select : bool ) {
174
+ let keys: Vec < _ > = self
175
+ . features
176
+ . iter ( )
177
+ . filter ( |( _, v) | rect. contains_rect ( v. bb ( self ) ) )
178
+ . map ( |( k, _) | k)
179
+ . collect ( ) ;
180
+
181
+ for k in keys. into_iter ( ) {
182
+ self . select_feature ( & k, select) ;
183
+ }
184
+ }
185
+
186
+ pub fn selection_clear ( & mut self ) {
187
+ self . selected_map . clear ( ) ;
188
+ }
189
+
190
+ pub fn feature_selected ( & self , feature : & slotmap:: DefaultKey ) -> bool {
191
+ self . selected_map . get ( feature) . is_some ( )
192
+ }
160
193
}
161
194
162
195
/// Colors describes the colors with which different elements should be styled.
@@ -303,25 +336,9 @@ where
303
336
if s. area ( ) > 200. {
304
337
let shift_held = ui. input ( |i| i. modifiers . shift ) ;
305
338
if !shift_held {
306
- self . drawing . selected_map . clear ( ) ;
307
- }
308
- for ( k, v) in self . drawing . features . iter ( ) {
309
- if s. contains_rect ( v. bb ( self . drawing ) )
310
- && !self . drawing . selected_map . contains_key ( & k)
311
- {
312
- let next_idx = if !shift_held {
313
- 0
314
- } else {
315
- self . drawing
316
- . selected_map
317
- . values ( )
318
- . fold ( 0 , |acc, x| acc. max ( * x) )
319
- + 1
320
- } ;
321
-
322
- self . drawing . selected_map . insert ( k, next_idx) ;
323
- }
339
+ self . drawing . selection_clear ( ) ;
324
340
}
341
+ self . drawing . select_features_in_rect ( s, true ) ;
325
342
}
326
343
ui. memory_mut ( |mem| mem. data . remove :: < egui:: Pos2 > ( state_id) ) ;
327
344
None
@@ -347,26 +364,14 @@ where
347
364
348
365
// feature clicked: add-to or replace selection
349
366
if let Some ( ( k, _) ) = hf {
350
- // Allow deselect when holding shift
351
- if shift_held && self . drawing . selected_map . contains_key ( k) {
352
- self . drawing . selected_map . remove ( k) ;
353
- } else {
354
- let next_idx = if !shift_held {
355
- self . drawing . selected_map . clear ( ) ;
356
- 0
357
- } else {
358
- self . drawing
359
- . selected_map
360
- . values ( )
361
- . fold ( 0 , |acc, x| acc. max ( * x) )
362
- + 1
363
- } ;
364
-
365
- self . drawing . selected_map . insert ( k. clone ( ) , next_idx) ;
367
+ if !shift_held {
368
+ self . drawing . selection_clear ( ) ;
366
369
}
370
+ self . drawing
371
+ . select_feature ( k, !self . drawing . feature_selected ( k) ) ;
367
372
} else if !shift_held {
368
373
// empty space clicked, clear selection.
369
- self . drawing . selected_map . clear ( ) ;
374
+ self . drawing . selection_clear ( ) ;
370
375
}
371
376
}
372
377
@@ -375,7 +380,7 @@ where
375
380
&& self . drawing . selected_map . len ( ) > 0
376
381
&& ui. input ( |i| i. key_pressed ( egui:: Key :: Escape ) )
377
382
{
378
- self . drawing . selected_map . clear ( ) ;
383
+ self . drawing . selection_clear ( ) ;
379
384
}
380
385
381
386
// Handle: Ctrl-A selects all
@@ -399,7 +404,7 @@ where
399
404
&& self . drawing . selected_map . len ( ) > 0
400
405
&& ui. input ( |i| i. key_pressed ( egui:: Key :: Delete ) )
401
406
{
402
- self . drawing . delete_selection ( ) ;
407
+ self . drawing . selection_delete ( ) ;
403
408
}
404
409
405
410
current_drag
0 commit comments