Skip to content

Commit e204917

Browse files
author
Tom
committed
Move selection manipulation into methods on Data
1 parent 5a64ac5 commit e204917

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

drawing/src/lib.rs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ impl<F: DrawingFeature> Data<F> {
120120
}
121121

122122
pub fn delete_feature(&mut self, k: slotmap::DefaultKey) -> bool {
123+
self.selected_map.remove(&k);
124+
123125
match self.features.remove(k) {
124126
Some(_v) => {
125127
// Find and also remove any features dependent on what we just removed.
@@ -151,12 +153,43 @@ impl<F: DrawingFeature> Data<F> {
151153
}
152154
}
153155

154-
pub fn delete_selection(&mut self) {
156+
pub fn selection_delete(&mut self) {
155157
let elements: Vec<_> = self.selected_map.drain().map(|(k, _)| k).collect();
156158
for k in elements {
157159
self.delete_feature(k);
158160
}
159161
}
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+
}
160193
}
161194

162195
/// Colors describes the colors with which different elements should be styled.
@@ -303,25 +336,9 @@ where
303336
if s.area() > 200. {
304337
let shift_held = ui.input(|i| i.modifiers.shift);
305338
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();
324340
}
341+
self.drawing.select_features_in_rect(s, true);
325342
}
326343
ui.memory_mut(|mem| mem.data.remove::<egui::Pos2>(state_id));
327344
None
@@ -347,26 +364,14 @@ where
347364

348365
// feature clicked: add-to or replace selection
349366
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();
366369
}
370+
self.drawing
371+
.select_feature(k, !self.drawing.feature_selected(k));
367372
} else if !shift_held {
368373
// empty space clicked, clear selection.
369-
self.drawing.selected_map.clear();
374+
self.drawing.selection_clear();
370375
}
371376
}
372377

@@ -375,7 +380,7 @@ where
375380
&& self.drawing.selected_map.len() > 0
376381
&& ui.input(|i| i.key_pressed(egui::Key::Escape))
377382
{
378-
self.drawing.selected_map.clear();
383+
self.drawing.selection_clear();
379384
}
380385

381386
// Handle: Ctrl-A selects all
@@ -399,7 +404,7 @@ where
399404
&& self.drawing.selected_map.len() > 0
400405
&& ui.input(|i| i.key_pressed(egui::Key::Delete))
401406
{
402-
self.drawing.delete_selection();
407+
self.drawing.selection_delete();
403408
}
404409

405410
current_drag

0 commit comments

Comments
 (0)