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        // -- Object manipulation --
128        Binding::in_modes(
129            Action::BeginMove,
130            key_press(KeyCode::G, Modifiers::NONE),
131            normal,
132        ),
133        Binding::in_modes(
134            Action::BeginRotate,
135            key_press(KeyCode::R, Modifiers::NONE),
136            normal,
137        ),
138        Binding::in_modes(
139            Action::BeginScale,
140            key_press(KeyCode::S, Modifiers::NONE),
141            normal,
142        ),
143        Binding::in_modes(
144            Action::ConstrainX,
145            key_press(KeyCode::X, Modifiers::NONE),
146            manip,
147        ),
148        Binding::in_modes(
149            Action::ConstrainY,
150            key_press(KeyCode::Y, Modifiers::NONE),
151            manip,
152        ),
153        Binding::in_modes(
154            Action::ConstrainZ,
155            key_press(KeyCode::Z, Modifiers::NONE),
156            manip,
157        ),
158        Binding::in_modes(
159            Action::ExcludeX,
160            key_press(KeyCode::X, Modifiers::SHIFT),
161            manip,
162        ),
163        Binding::in_modes(
164            Action::ExcludeY,
165            key_press(KeyCode::Y, Modifiers::SHIFT),
166            manip,
167        ),
168        Binding::in_modes(
169            Action::ExcludeZ,
170            key_press(KeyCode::Z, Modifiers::SHIFT),
171            manip,
172        ),
173        // -- Confirm / Cancel (fly mode + manipulation) --
174        Binding::in_modes(
175            Action::Confirm,
176            key_press(KeyCode::Enter, Modifiers::NONE),
177            fly_manip,
178        ),
179        Binding::in_modes(Action::Confirm, mouse_click(MouseButton::Left), fly_manip),
180        Binding::in_modes(
181            Action::Cancel,
182            key_press(KeyCode::Escape, Modifiers::NONE),
183            fly_manip,
184        ),
185        Binding::in_modes(Action::Cancel, mouse_click(MouseButton::Right), fly_manip),
186        // -- Scene object shortcuts --
187        Binding::in_modes(
188            Action::OpenAddMenu,
189            key_press(KeyCode::A, Modifiers::SHIFT),
190            normal,
191        ),
192        Binding::in_modes(
193            Action::DeleteSelected,
194            key_press(KeyCode::X, Modifiers::NONE),
195            normal,
196        ),
197        // -- Global (all modes) --
198        Binding::global(Action::Undo, key_press(KeyCode::Z, Modifiers::CTRL)),
199        Binding::global(Action::Redo, key_press(KeyCode::Z, Modifiers::CTRL_SHIFT)),
200        Binding::global(Action::Redo, key_press(KeyCode::Y, Modifiers::CTRL)),
201    ]
202}