Skip to main content

viewport_lib/runtime/systems/
selection.rs

1//! Built-in selection system for ViewportRuntime.
2
3use crate::runtime::context::RuntimeFrameContext;
4use crate::runtime::output::RuntimeOutput;
5use crate::runtime::output::SelectionOp;
6
7/// Built-in click-to-select system.
8///
9/// When enabled on [`super::super::ViewportRuntime`], handles primary click and
10/// shift-click selection automatically in the `Select` phase. The app supplies
11/// `RuntimeFrameContext::pick_hit`, `clicked`, and `shift_held`; this system
12/// produces the corresponding [`SelectionOp`] in [`RuntimeOutput::selection_ops`].
13pub struct SelectionSystem;
14
15impl Default for SelectionSystem {
16    fn default() -> Self {
17        Self
18    }
19}
20
21impl SelectionSystem {
22    /// Create a new SelectionSystem.
23    pub fn new() -> Self {
24        Self
25    }
26
27    pub(crate) fn step(&self, frame: &RuntimeFrameContext, output: &mut RuntimeOutput) {
28        if !frame.clicked {
29            return;
30        }
31        match &frame.pick_hit {
32            Some(hit) => {
33                if frame.shift_held {
34                    output.selection_ops.push(SelectionOp::Toggle(hit.id));
35                } else {
36                    output.selection_ops.push(SelectionOp::SelectOne(hit.id));
37                }
38            }
39            None => {
40                if !frame.shift_held {
41                    output.selection_ops.push(SelectionOp::Clear);
42                }
43            }
44        }
45    }
46}