Skip to main content

viewport_lib/interaction/input/
defaults.rs

1use super::action::Action;
2use super::binding::*;
3use super::mode::InputMode;
4
5/// Helper: key press trigger with given modifiers.
6fn key_press(key: KeyCode, mods: Modifiers) -> Trigger {
7    Trigger {
8        kind: TriggerKind::Key(key),
9        modifiers: mods,
10        activation: ActivationMode::OnPress,
11        ignore_modifiers: false,
12    }
13}
14
15/// Helper: key held trigger that ignores modifiers (for fly mode WASD).
16fn key_held_any_mod(key: KeyCode) -> Trigger {
17    Trigger {
18        kind: TriggerKind::Key(key),
19        modifiers: Modifiers::NONE,
20        activation: ActivationMode::WhileHeld,
21        ignore_modifiers: true,
22    }
23}
24
25/// Helper: mouse drag trigger.
26fn mouse_drag(btn: MouseButton, mods: Modifiers) -> Trigger {
27    Trigger {
28        kind: TriggerKind::MouseButton(btn),
29        modifiers: mods,
30        activation: ActivationMode::OnDrag,
31        ignore_modifiers: false,
32    }
33}
34
35/// Helper: scroll trigger.
36fn scroll(mods: Modifiers) -> Trigger {
37    Trigger {
38        kind: TriggerKind::Scroll,
39        modifiers: mods,
40        activation: ActivationMode::OnScroll,
41        ignore_modifiers: false,
42    }
43}
44
45/// Helper: mouse click trigger.
46fn mouse_click(btn: MouseButton) -> Trigger {
47    Trigger {
48        kind: TriggerKind::MouseButton(btn),
49        modifiers: Modifiers::NONE,
50        activation: ActivationMode::OnPress,
51        ignore_modifiers: false,
52    }
53}
54
55/// Returns the default binding table for viewport navigation and manipulation.
56pub fn default_bindings() -> Vec<Binding> {
57    let normal = &[InputMode::Normal];
58    let fly = &[InputMode::FlyMode];
59    let manip = &[InputMode::Manipulating];
60    let fly_manip = &[InputMode::FlyMode, InputMode::Manipulating];
61
62    vec![
63        // -- Viewport navigation --
64        Binding::in_modes(
65            Action::Orbit,
66            mouse_drag(MouseButton::Left, Modifiers::ALT),
67            normal,
68        ),
69        Binding::in_modes(
70            Action::Pan,
71            mouse_drag(MouseButton::Left, Modifiers::SHIFT),
72            normal,
73        ),
74        Binding::in_modes(
75            Action::Pan,
76            mouse_drag(MouseButton::Middle, Modifiers::NONE),
77            normal,
78        ),
79        Binding::in_modes(Action::Zoom, scroll(Modifiers::NONE), normal),
80        Binding::in_modes(
81            Action::Zoom,
82            mouse_drag(MouseButton::Right, Modifiers::ALT),
83            normal,
84        ),
85        Binding::in_modes(
86            Action::FocusObject,
87            key_press(KeyCode::F, Modifiers::NONE),
88            normal,
89        ),
90        Binding::in_modes(
91            Action::ResetView,
92            key_press(KeyCode::R, Modifiers::NONE),
93            normal,
94        ),
95        Binding::in_modes(
96            Action::ToggleWireframe,
97            key_press(KeyCode::W, Modifiers::NONE),
98            normal,
99        ),
100        Binding::in_modes(
101            Action::CycleGizmoMode,
102            key_press(KeyCode::Tab, Modifiers::NONE),
103            normal,
104        ),
105        // -- Fly mode --
106        Binding::in_modes(
107            Action::EnterFlyMode,
108            key_press(KeyCode::Backtick, Modifiers::SHIFT),
109            normal,
110        ),
111        Binding::in_modes(Action::FlyForward, key_held_any_mod(KeyCode::W), fly),
112        Binding::in_modes(Action::FlyBackward, key_held_any_mod(KeyCode::S), fly),
113        Binding::in_modes(Action::FlyLeft, key_held_any_mod(KeyCode::A), fly),
114        Binding::in_modes(Action::FlyRight, key_held_any_mod(KeyCode::D), fly),
115        Binding::in_modes(Action::FlyUp, key_held_any_mod(KeyCode::E), fly),
116        Binding::in_modes(Action::FlyDown, key_held_any_mod(KeyCode::Q), fly),
117        // FlySpeedBoost is handled by checking modifiers.shift directly in fly-mode logic,
118        // not via a binding, because Shift is a modifier and not a standalone key.
119        Binding::in_modes(Action::FlySpeedIncrease, scroll(Modifiers::NONE), fly),
120        Binding::in_modes(Action::FlySpeedDecrease, scroll(Modifiers::SHIFT), fly),
121        // -- Gizmo space toggle --
122        Binding::in_modes(
123            Action::ToggleGizmoSpace,
124            key_press(KeyCode::Backtick, Modifiers::NONE),
125            normal,
126        ),
127        // -- Pivot mode cycling --
128        Binding::in_modes(
129            Action::CyclePivotModeForward,
130            key_press(KeyCode::LeftBracket, Modifiers::NONE),
131            &[InputMode::Normal, InputMode::Manipulating],
132        ),
133        Binding::in_modes(
134            Action::CyclePivotModeBackward,
135            key_press(KeyCode::RightBracket, Modifiers::NONE),
136            &[InputMode::Normal, InputMode::Manipulating],
137        ),
138        // -- Object manipulation --
139        Binding::in_modes(
140            Action::BeginMove,
141            key_press(KeyCode::G, Modifiers::NONE),
142            normal,
143        ),
144        Binding::in_modes(
145            Action::BeginRotate,
146            key_press(KeyCode::R, Modifiers::NONE),
147            normal,
148        ),
149        Binding::in_modes(
150            Action::BeginScale,
151            key_press(KeyCode::S, Modifiers::NONE),
152            normal,
153        ),
154        Binding::in_modes(
155            Action::ConstrainX,
156            key_press(KeyCode::X, Modifiers::NONE),
157            manip,
158        ),
159        Binding::in_modes(
160            Action::ConstrainY,
161            key_press(KeyCode::Y, Modifiers::NONE),
162            manip,
163        ),
164        Binding::in_modes(
165            Action::ConstrainZ,
166            key_press(KeyCode::Z, Modifiers::NONE),
167            manip,
168        ),
169        Binding::in_modes(
170            Action::ExcludeX,
171            key_press(KeyCode::X, Modifiers::SHIFT),
172            manip,
173        ),
174        Binding::in_modes(
175            Action::ExcludeY,
176            key_press(KeyCode::Y, Modifiers::SHIFT),
177            manip,
178        ),
179        Binding::in_modes(
180            Action::ExcludeZ,
181            key_press(KeyCode::Z, Modifiers::SHIFT),
182            manip,
183        ),
184        // -- Confirm / Cancel (fly mode + manipulation) --
185        Binding::in_modes(
186            Action::Confirm,
187            key_press(KeyCode::Enter, Modifiers::NONE),
188            fly_manip,
189        ),
190        Binding::in_modes(Action::Confirm, mouse_click(MouseButton::Left), fly_manip),
191        Binding::in_modes(
192            Action::Cancel,
193            key_press(KeyCode::Escape, Modifiers::NONE),
194            fly_manip,
195        ),
196        Binding::in_modes(Action::Cancel, mouse_click(MouseButton::Right), fly_manip),
197        // -- Scene object shortcuts --
198        Binding::in_modes(
199            Action::OpenAddMenu,
200            key_press(KeyCode::A, Modifiers::SHIFT),
201            normal,
202        ),
203        Binding::in_modes(
204            Action::DeleteSelected,
205            key_press(KeyCode::X, Modifiers::NONE),
206            normal,
207        ),
208        // -- Global (all modes) --
209        Binding::global(Action::Undo, key_press(KeyCode::Z, Modifiers::CTRL)),
210        Binding::global(Action::Redo, key_press(KeyCode::Z, Modifiers::CTRL_SHIFT)),
211        Binding::global(Action::Redo, key_press(KeyCode::Y, Modifiers::CTRL)),
212    ]
213}