Skip to main content

brep_render/engine_state/
box_select.rs

1//! BOX selection in the 3D view (Shift+left-drag): [`crate::box_pick`]'s
2//! visible entities, through the bottom selection filter, into the selection.
3
4use super::*;
5use crate::box_pick::{box_pick, ScreenBox};
6
7/// What a box drag does to the selection.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum BoxSelectMode {
10    /// Shift+drag: the box's entities become the selection.
11    Replace,
12    /// Primary+Shift+drag: they are added.
13    Add,
14    /// Alt/Option+Shift+drag: they are removed.
15    Remove,
16}
17
18impl EngineState {
19    /// The candidates a box from `start` to `end` (viewport CSS px) takes,
20    /// filter applied: WINDOW (left to right, fully inside) or CROSSING
21    /// (right to left, touching), visible entities only. With the filter's
22    /// COMPONENT lane on, an entity of an assembly component is taken as its
23    /// component (window: every member solid inside).
24    pub fn box_candidates(&self, start: (f64, f64), end: (f64, f64)) -> Vec<pick::PickCandidate> {
25        let rect = ScreenBox::from_drag(start, end);
26        let kinds = self.selection_filter.enabled_kinds();
27        let component_on = self.selection_filter.component;
28        if kinds.is_empty() && !component_on {
29            return Vec::new();
30        }
31        let raw = box_pick(&self.scene, &self.camera, rect, &self.pick_options());
32        let mut out: Vec<pick::PickCandidate> = Vec::new();
33        for candidate in &raw {
34            if component_on && self.hit_owning_component(candidate).is_some() {
35                continue;
36            }
37            if self.candidate_admitted(&kinds, candidate) {
38                out.push(candidate.clone());
39            }
40        }
41        if component_on {
42            let components = if rect.crossing() {
43                self.component_candidates(&raw)
44            } else {
45                let taken: std::collections::HashSet<&str> = raw
46                    .iter()
47                    .filter(|c| c.kind == pick::PickKind::Solid)
48                    .map(|c| c.name.as_str())
49                    .collect();
50                self.component_candidates(&raw)
51                    .into_iter()
52                    .filter(|component| {
53                        let members = self.component_member_solids(&component.name);
54                        !members.is_empty() && members.iter().all(|m| taken.contains(m.as_str()))
55                    })
56                    .collect()
57            };
58            out.extend(components);
59        }
60        out
61    }
62
63    /// Apply a box drag to the selection (see [`BoxSelectMode`]). Returns how
64    /// many entities the box took.
65    pub fn box_select(&mut self, start: (f64, f64), end: (f64, f64), mode: BoxSelectMode) -> usize {
66        let candidates = self.box_candidates(start, end);
67        if mode == BoxSelectMode::Replace {
68            self.clear_selection();
69        }
70        for candidate in &candidates {
71            let selected = self.candidate_is_selected(candidate);
72            let wanted = mode != BoxSelectMode::Remove;
73            if selected != wanted {
74                self.toggle_candidate(candidate);
75            }
76        }
77        self.dirty = true;
78        candidates.len()
79    }
80}
81
82/// The kind select-all / invert act on: the coarsest kind the filter admits
83/// that has a visible entity, COMPONENT > SOLID > FACE > EDGE > VERTEX.
84const SELECT_ALL_ORDER: [pick::PickKind; 5] = [
85    pick::PickKind::Component,
86    pick::PickKind::Solid,
87    pick::PickKind::Face,
88    pick::PickKind::Edge,
89    pick::PickKind::Vertex,
90];
91
92impl EngineState {
93    /// Every VISIBLE entity of the current filter type (primary+A): the
94    /// coarsest kind the selection filter — or a held letter — admits that
95    /// has anything on screen (components, else solids, faces, edges,
96    /// vertices). `invert` (primary+I) flips the selection within that kind
97    /// instead: the visible ones not selected become selected and the
98    /// selected ones are dropped. Replaces the selection of every other kind
99    /// either way. Returns how many entities are selected of that kind after.
100    pub fn select_all_visible(&mut self, invert: bool) -> usize {
101        let (w, h) = (self.camera.width, self.camera.height);
102        let all = self.box_candidates((w, 0.0), (0.0, h));
103        let Some(kind) = SELECT_ALL_ORDER.iter().copied().find(|k| all.iter().any(|c| c.kind == *k)) else {
104            if !invert {
105                self.clear_selection();
106            }
107            return 0;
108        };
109        let of_kind: Vec<pick::PickCandidate> = all.into_iter().filter(|c| c.kind == kind).collect();
110        let selected: Vec<bool> = of_kind.iter().map(|c| self.candidate_is_selected(c)).collect();
111        self.clear_selection();
112        let mut count = 0;
113        for (candidate, was) in of_kind.iter().zip(selected) {
114            if !invert || !was {
115                if !self.candidate_is_selected(candidate) {
116                    self.toggle_candidate(candidate);
117                }
118                count += 1;
119            }
120        }
121        self.dirty = true;
122        let noun = match kind {
123            pick::PickKind::Component => "component",
124            pick::PickKind::Solid => "solid",
125            pick::PickKind::Face => "face",
126            pick::PickKind::Edge => "edge",
127            _ => "vertex",
128        };
129        let plural = match (count, kind) {
130            (1, _) => noun.to_string(),
131            (_, pick::PickKind::Vertex) => "vertices".to_string(),
132            _ => format!("{noun}s"),
133        };
134        // Say what it took (master 2026-11-26): the status readout.
135        self.push_notice_as(NoticeSeverity::Info, format!("Selected {count} {plural}"));
136        count
137    }
138}
139
140impl EngineState {
141    /// Primary+A while a MULTIPLE reference field is picking: every visible
142    /// entity of the coarsest kind the field admits goes into its running
143    /// list (de-duplicated, named as a click would name it). Returns how many
144    /// it offered; 0 for a single field or none picking.
145    pub fn ref_select_all_visible(&mut self) -> usize {
146        if !self.ref_select.as_ref().is_some_and(|state| state.multiple) {
147            return 0;
148        }
149        let (w, h) = (self.camera.width, self.camera.height);
150        let all = self.box_candidates((w, 0.0), (0.0, h));
151        let Some(kind) = SELECT_ALL_ORDER.iter().copied().find(|k| all.iter().any(|c| c.kind == *k)) else {
152            return 0;
153        };
154        let of_kind: Vec<pick::PickCandidate> = all.into_iter().filter(|c| c.kind == kind).collect();
155        for candidate in &of_kind {
156            self.ref_select_take_candidate(candidate);
157        }
158        self.dirty = true;
159        of_kind.len()
160    }
161}
162
163impl EngineState {
164    /// The LIVE box preview (owner 2026-11-26): while a box is dragged the
165    /// pointer's own hover highlight is off and the hover emphasis lights
166    /// exactly what releasing the box here would take — the same
167    /// [`Self::box_candidates`] the release applies. Returns how many.
168    pub fn hover_box_preview(&mut self, start: (f64, f64), end: (f64, f64)) -> usize {
169        use crate::pick::PickKind;
170        let candidates = self.box_candidates(start, end);
171        let had_datums = !self.emphasis.hovered_datums.is_empty();
172        self.emphasis.hovered_solids.clear();
173        self.emphasis.hovered_faces.clear();
174        self.emphasis.hovered_edges.clear();
175        self.emphasis.hovered_vertices.clear();
176        self.emphasis.hovered_datums.clear();
177        for candidate in &candidates {
178            match candidate.kind {
179                PickKind::Solid => {
180                    self.emphasis.hovered_solids.insert(self.candidate_solid_name(candidate));
181                }
182                PickKind::Face => {
183                    self.emphasis.hovered_faces.insert(candidate.name.clone());
184                }
185                PickKind::Edge => {
186                    self.emphasis.hovered_edges.insert(candidate.name.clone());
187                }
188                PickKind::Vertex => self.emphasis.hovered_vertices.push(crate::style::VertexRef {
189                    solid: candidate.solid.clone(),
190                    position: candidate.position,
191                }),
192                PickKind::Plane => {
193                    self.emphasis.hovered_datums.insert(candidate.name.clone());
194                }
195                PickKind::Component => {
196                    for member in self.component_member_solids(&candidate.name) {
197                        self.emphasis.hovered_solids.insert(member);
198                    }
199                }
200            }
201        }
202        self.emphasis.generation = self.emphasis.generation.wrapping_add(1);
203        self.dirty = true;
204        if had_datums {
205            self.refresh_construction_datums();
206        }
207        candidates.len()
208    }
209}