bones_framework/input.rs
1//! Input resources.
2
3use bones_lib::ecs::World;
4use bones_schema::HasSchema;
5use std::fmt::Debug;
6
7pub mod gamepad;
8pub mod gilrs;
9pub mod keyboard;
10pub mod mouse;
11pub mod proto;
12pub mod window;
13
14/// The state of a button, either pressed or released.
15#[derive(HasSchema, Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
16#[repr(u8)]
17pub enum ButtonState {
18 /// The button is pressed.
19 Pressed,
20 #[default]
21 /// The button is released.
22 Released,
23}
24
25impl ButtonState {
26 /// Get whether or not the button is pressed.
27 pub fn pressed(&self) -> bool {
28 matches!(self, ButtonState::Pressed)
29 }
30}
31
32/// Module prelude.
33pub mod prelude {
34 pub use super::{gamepad::*, keyboard::*, mouse::*, proto, window::*, ButtonState};
35 pub use crate::input::{Controls, DenseControl, DenseInput, DenseInputConfig, InputCollector};
36}
37
38/// Dense input super trait for network replication.
39pub trait DenseInput:
40 bytemuck::Pod + bytemuck::Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
41{
42}
43// Automatic implementation for `DenseInput`.
44impl<T> DenseInput for T where
45 T: bytemuck::Pod + bytemuck::Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
46{
47}
48
49/// Collects local input state for the network client from the [`World`].
50pub trait InputCollector<'a, State>: Send + Sync {
51 /// Update the internal state with the [`World`].
52 ///
53 /// This should be called every render frame / cpu cycle after input events.
54 fn apply_inputs(&mut self, world: &World);
55
56 /// Indicates to the collector that its input has been consumed.
57 ///
58 /// This should be called on frame update when input for this frame has been
59 /// consumed as opposed to the cpu cycle. In a network session, by the time
60 /// this is called, the dense input should be sent across the network.
61 fn advance_frame(&mut self);
62
63 /// Get the internally stored input state.
64 fn get_state(&self) -> State;
65}
66
67/// A trait representing a control type capable of being updated by a [`DenseInput`].
68pub trait DenseControl<Dense: DenseInput>: Send + Sync + Default {
69 /// Update this control with a [`DenseInput`].
70 fn update_from_dense(&mut self, new_control: &Dense);
71}
72
73/// Trait for getting every player control via index.
74pub trait Controls<'a, Control> {
75 /// Get control for player.
76 fn get_control(&self, player_idx: usize) -> &Control;
77
78 /// Get mutable control for player.
79 fn get_control_mut(&mut self, player_idx: usize) -> &mut Control;
80}
81
82/// Defines input types for use in networking.
83#[allow(missing_docs)]
84pub trait DenseInputConfig<'a> {
85 /// Current input state for a client. This is what is sent across the
86 /// network. All input history and other data derived from the state should
87 /// be stored by the [`Control`](DenseInputConfig::Control).
88 type Dense: DenseInput + Debug + Default;
89
90 /// Local configuration for getting [`Dense`](DenseInputConfig::Dense) from
91 /// the [`World`] to be sent across the network.
92 type InputCollector: InputCollector<'a, Self::Dense> + Default;
93
94 /// The complete input data of a client. Updated with
95 /// [`Dense`](DenseInputConfig::Dense) when recieved from the network. The
96 /// [`Controls`](DenseInputConfig::Controls) resource in the [`World`] will
97 /// contain every client's control after update.
98 type Control: DenseControl<Self::Dense>;
99
100 /// The resource that will contain a [`Control`](DenseInputConfig::Control)
101 /// for every client in a network session.
102 type Controls: Controls<'a, Self::Control>
103 // HasSchema because expected to be retrieved from `World` as
104 // `Resource`.
105 + HasSchema
106 // Default because not expected to be initialized when retrieved from
107 // `World`.
108 + Default;
109}