ferravex_render/
input.rs

1//! Runtime input uses logical display pixels. Legacy polling remains available;
2//! PlayerInput consumes the same ordered, bounded event journal.
3use crate::prelude::*;
4pub use ferravex_play::input::*;
5pub mod calibration;
6mod platform;
7mod player;
8mod presets;
9pub mod touch_layout;
10pub use player::{CaptureDevice, ControlsStatus, HintDevice, HintError, PlayerInput};
11pub use player::{VisibleHintError, VisibleTouchLabel};
12pub use presets::Preset;
13mod storage;
14pub use platform::PadState;
15pub(crate) use platform::{MAX_EVENTS, PlatformEvent, PlatformInput};
16pub use storage::StorageError;
17pub fn logical_touches() -> Vec<Touch> {
18    touches()
19}
20
21/// Feed a logical-pixel touch from an embedding platform host.
22pub fn touch_event(event: ferravex_play::input::touch::Event) {
23    use ferravex_play::input::touch::Phase;
24    crate::touch(
25        event.id,
26        match event.phase {
27            Phase::Start => 0,
28            Phase::Move => 1,
29            Phase::End => 2,
30            Phase::Cancel => 3,
31        },
32        event.position.x,
33        event.position.y,
34    );
35}
36/// Current controller states; at most four controllers and fixed channel counts.
37pub fn gamepads() -> [PadState; 4] {
38    crate::read(|s| s.input.pads)
39}
40/// Supply a controller sample from a first-party embedding host. The same bounded
41/// journal and normalized axes/buttons are used by browser and Linux providers.
42pub fn gamepad_sample(index: usize, state: PadState) -> bool {
43    if index >= 4 {
44        return false;
45    }
46    crate::edit(|s| {
47        s.input.pad_connected(index, state.connected);
48        if state.connected {
49            for (i, v) in state.axes.into_iter().enumerate() {
50                s.input.pad_axis(index, i, v);
51            }
52            for (i, v) in state.buttons.into_iter().enumerate() {
53                s.input.pad_button(index, i, v);
54            }
55        }
56    });
57    true
58}
59#[cfg(target_os = "linux")]
60mod gamepad;
61#[cfg(target_os = "linux")]
62pub(crate) fn poll_gamepads() {
63    thread_local! {static PADS:std::cell::RefCell<gamepad::Gamepads>=std::cell::RefCell::new(gamepad::Gamepads::default());}
64    if !crate::read(|s| s.input_enabled) {
65        return;
66    }
67    let pads = PADS.with(|p| p.borrow_mut().poll());
68    crate::edit(|s| {
69        for (i, pad) in pads.iter().enumerate() {
70            s.input.pad_connected(i, pad.connected);
71            if pad.connected {
72                for (a, &value) in pad.axes.iter().enumerate() {
73                    s.input.pad_axis(i, a, value);
74                }
75                for (b, &value) in pad.buttons.iter().enumerate() {
76                    s.input.pad_button(i, b, value);
77                }
78            }
79        }
80    });
81}
82#[cfg(all(not(target_os = "linux"), not(target_arch = "wasm32")))]
83pub(crate) fn poll_gamepads() {}
84
85/// Drive the shared action journal and optional immediate UI from a custom
86/// renderer host. Call once per render frame, outside a `start` game future.
87/// The callback can sample PlayerInput, advance its game, then draw settings.
88/// UI vertices remain available through the normal renderer ABI after return.
89pub fn host_frame<T>(dt: f32, width: u32, height: u32, run: impl FnOnce() -> T) -> T {
90    crate::begin_host_frame(dt, width, height);
91    let value = run();
92    crate::end_host_frame();
93    value
94}