Skip to main content

custom_controller/
custom_controller.rs

1//! Build your own camera controller.
2//!
3//! Input handling is a replaceable layer: resolve events into an `ActionFrame`
4//! with `ViewportInput`, then apply that frame to a `Camera` however you like.
5//! This example does exactly that with a hand-rolled controller that has nothing
6//! to do with the built-in `OrbitCameraController` - it needs no renderer and no
7//! window, just the input crate.
8//!
9//! Run: `cargo run -p viewport-lib-input --example custom_controller`
10
11use viewport_lib_input::input::{
12    BindingPreset, ButtonState, InputOwnership, MouseButton, ViewportContext, ViewportEvent,
13    ViewportInput,
14};
15use viewport_lib_input::{Camera, CameraController};
16use viewport_lib_types::input::ActionFrame;
17
18/// A custom controller: orbit at half sensitivity and ignore pan/zoom entirely.
19///
20/// Replacing the built-in policy is this small - implement `apply`, read the
21/// resolved navigation deltas, drive the camera your way. `dt` is there for held
22/// input (a key held to move); this controller reads only deltas, so it ignores it.
23struct HalfSpeedOrbit;
24
25impl CameraController for HalfSpeedOrbit {
26    fn apply(&mut self, camera: &mut Camera, frame: &ActionFrame, _dt: f32) {
27        let nav = &frame.navigation;
28        if nav.orbit.x != 0.0 || nav.orbit.y != 0.0 {
29            let mut target = camera.orbit_target();
30            target.orbit(nav.orbit.x * 0.5, nav.orbit.y * 0.5);
31            camera.set_orbit_target(target);
32        }
33        // A real custom controller might map zoom to dolly, add inertia, clamp
34        // pitch differently, etc. The point is the app decides here.
35    }
36}
37
38fn main() {
39    // 1. RESOLVE: feed native-shaped events into ViewportInput for one frame.
40    let mut input = ViewportInput::from_preset(BindingPreset::Viewer);
41    input.begin_frame(ViewportContext::new(
42        InputOwnership::full_window(),
43        [800.0, 600.0],
44    ));
45
46    // In the primitives preset, plain left-drag (no modifiers) orbits. Drive it
47    // by hand: place the pointer, press the button there (that sets the drag
48    // origin), then move across the viewport to accumulate the drag delta.
49    input.push_event(ViewportEvent::PointerMoved {
50        position: [400.0, 300.0].into(),
51    });
52    input.push_event(ViewportEvent::MouseButton {
53        button: MouseButton::Left,
54        state: ButtonState::Pressed,
55    });
56    input.push_event(ViewportEvent::PointerMoved {
57        position: [460.0, 300.0].into(),
58    });
59
60    let frame: ActionFrame = input.resolve();
61
62    // 2. APPLY: hand the resolved frame to your controller.
63    let mut camera = Camera::default();
64    let before = camera.orientation();
65
66    let mut controller = HalfSpeedOrbit;
67    controller.apply(&mut camera, &frame, 1.0 / 60.0);
68
69    println!("resolved orbit delta : {:?}", frame.navigation.orbit);
70    println!("camera orientation before : {before:?}");
71    println!("camera orientation after  : {:?}", camera.orientation());
72    println!(
73        "moved: {}",
74        before.abs_diff_eq(camera.orientation(), 1e-6) == false
75    );
76}