@@ -7,6 +7,7 @@ pub mod gamepad;
77pub mod gilrs;
88pub mod keyboard;
99pub mod mouse;
10+ pub mod proto;
1011pub mod window;
1112
1213/// The state of a button, ether pressed or released.
@@ -29,8 +30,10 @@ impl ButtonState {
2930
3031/// Module prelude.
3132pub mod prelude {
32- pub use super :: { gamepad:: * , keyboard:: * , mouse:: * , window:: * , ButtonState } ;
33- pub use crate :: input:: { InputCollector , PlayerControls } ;
33+ pub use super :: { gamepad:: * , keyboard:: * , mouse:: * , proto, window:: * , ButtonState } ;
34+ pub use crate :: input:: {
35+ Controls , DenseControl , DenseInput , DenseInputCollector , DenseInputConfig , InputCollector ,
36+ } ;
3437}
3538
3639/// Maps raw inputs to game controls and exposes controls for respective player and their control source.
@@ -40,11 +43,7 @@ pub mod prelude {
4043/// [`InputCollector::update_just_pressed`] computes any changes in pressed buttons that may be stored on control.
4144///
4245/// [`InputCollector::advance_frame`] is used to mark that the input has been consumed, and update the prev frame inputs to current, to compute changes next frame.
43- ///
44- /// Generic type param ControlMapping is HasSchema because it is expected to be a Resource retrievable on world.
45- pub trait InputCollector < ' a , ControlMapping : HasSchema , ControlSource , Control > :
46- Send + Sync
47- {
46+ pub trait InputCollector < ' a , Control > : Send + Sync {
4847 /// Update the internal state with new inputs. This must be called every render frame with the
4948 /// input events. This updates which buttons are pressed, but does not compute what buttons were "just_pressed".
5049 /// use [`InputCollector::update_just_pressed`] to do this.
@@ -62,30 +61,80 @@ pub trait InputCollector<'a, ControlMapping: HasSchema, ControlSource, Control>:
6261 /// This does not modify previous frame's input, to do this use [`InputCollector::advance_frame`].
6362 fn update_just_pressed ( & mut self ) ;
6463
65- /// Get control for player based on provided `ControlSource` .
66- fn get_control ( & self , player_idx : usize , control_source : ControlSource ) -> & Control ;
64+ /// Get control for player.
65+ fn get_control ( & self ) -> & Control ;
6766}
6867
6968/// Trait that tracks player control state. Provides associated types for other input trait implementations.
70- pub trait PlayerControls < ' a , Control > {
71- /// InputCollector used to update controls .
72- type InputCollector : InputCollector < ' a , Self :: ControlMapping , Self :: ControlSource , Control > ;
69+ pub trait Controls < ' a , Control > {
70+ /// Get control for player .
71+ fn get_control ( & self , player_idx : usize ) -> & Control ;
7372
74- /// Control mapping from raw input, expected to be able to be retrieved as `Resource` from world.
75- type ControlMapping : HasSchema ;
73+ /// Get mutable control for player.
74+ fn get_control_mut ( & mut self , player_idx : usize ) -> & mut Control ;
75+ }
7676
77- /// Type used to map source of input to control.
78- type ControlSource ;
77+ use std:: fmt:: Debug ;
7978
80- /// Update control state from input collector.
81- fn update_controls ( & mut self , collector : & mut Self :: InputCollector ) ;
79+ /// Dense input for network replication.
80+ pub trait DenseInput :
81+ bytemuck:: Pod + bytemuck:: Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
82+ {
83+ }
8284
83- /// Get `ControlSource` for player (only present for local player).
84- fn get_control_source ( & self , local_player_idx : usize ) -> Option < Self :: ControlSource > ;
85+ /// Automatic implementation for `DenseInput`.
86+ impl < T > DenseInput for T where
87+ T : bytemuck:: Pod + bytemuck:: Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
88+ {
89+ }
8590
86- /// Get control for player.
87- fn get_control ( & self , player_idx : usize ) -> & Control ;
91+ /// Define input types used by game for use in networking.
92+ ///
93+ /// As long as types `Controls` and `InputCollector` implement traits [`Controls`] and [`InputCollector`],
94+ /// trait bounds [`DenseControl`] and [`DenseInputCollector`] are automatically implemented.
95+ #[ allow( missing_docs) ]
96+ pub trait DenseInputConfig < ' a > {
97+ type Dense : DenseInput + Debug + Default ;
98+ type Control : DenseControl < Self :: Dense > ;
99+
100+ // Must be HasSchema because expected to be retrieved from `World` as `Resource`.
101+ type Controls : Controls < ' a , Self :: Control > + HasSchema ;
102+
103+ // InputCollector type params must match that of Controls, so using associated types.
104+ type InputCollector : InputCollector < ' a , Self :: Control > + Default ;
105+ }
88106
89- /// Get mutable control for player.
90- fn get_control_mut ( & mut self , player_idx : usize ) -> & mut Control ;
107+ /// Trait allowing for creating and applying [`DenseInput`] from control.
108+ pub trait DenseControl < Dense : DenseInput > : Send + Sync + Default {
109+ /// Get [`DenseInput`] for control.
110+ fn get_dense_input ( & self ) -> Dense ;
111+
112+ /// Update control from [`DenseInput`].
113+ fn update_from_dense ( & mut self , new_control : & Dense ) ;
114+ }
115+
116+ /// Extension of [`InputCollector`] exposing dense control for networking.
117+ ///
118+ /// This trait is automatically implemented for [`InputCollector`]'s such that `Control`
119+ /// implements [`DenseControl`] (i.e. implements dense input)
120+ pub trait DenseInputCollector < ' a , Dense , Control > : InputCollector < ' a , Control >
121+ where
122+ Dense : DenseInput ,
123+ Control : DenseControl < Dense > ,
124+ {
125+ /// Get dense control
126+ fn get_dense_control ( & self ) -> Dense ;
127+ }
128+
129+ /// Provide automatic [`DenseInputCollector`] for [`InputCollector`] when type parameters
130+ /// meet required bounds for networking.
131+ impl < ' a , T , Dense , Control > DenseInputCollector < ' a , Dense , Control > for T
132+ where
133+ Dense : DenseInput ,
134+ Control : DenseControl < Dense > ,
135+ T : InputCollector < ' a , Control > ,
136+ {
137+ fn get_dense_control ( & self ) -> Dense {
138+ self . get_control ( ) . get_dense_input ( )
139+ }
91140}
0 commit comments