1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::input_system::input_system;
use fnv::FnvHashMap;
use serde::Deserialize;
use std::any::Any;
use std::hash::Hash;
use zengine_engine::{Module, Stage};
use zengine_macro::Resource;
mod input;
mod input_system;
pub use input::*;
pub struct InputModule<T: InputType>(pub Bindings<T>);
impl<T: InputType> Module for InputModule<T> {
fn init(self, engine: &mut zengine_engine::Engine) {
engine.add_system_into_stage(input_system(self.0), Stage::PreUpdate);
}
}
pub mod device {
use serde::Deserialize;
pub type Key = winit::event::VirtualKeyCode;
pub type MouseButton = winit::event::MouseButton;
pub type ControllerButton = gilrs::Button;
#[derive(Eq, PartialEq, Hash, Debug, Deserialize, Clone)]
pub enum Which {
Left,
Right,
}
}
pub trait InputType:
Any + Eq + PartialEq + Hash + Clone + Send + Sync + Default + std::fmt::Debug
{
}
impl InputType for String {}
#[derive(Debug, Deserialize, PartialEq)]
pub struct AxisBind {
source: Input,
#[serde(default)]
invert: bool,
#[serde(default)]
discrete_map: Option<f32>,
}
impl AxisBind {
pub fn with_source(source: Input) -> Self {
Self {
source,
invert: false,
discrete_map: None,
}
}
pub fn invert_input(mut self) -> Self {
self.invert = true;
self
}
pub fn with_discrete_map(mut self, discrete_map: f32) -> Self {
self.discrete_map = Some(discrete_map);
self
}
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
pub struct ActionBind {
source: Input,
}
impl ActionBind {
pub fn with_source(source: Input) -> Self {
Self { source }
}
}
#[derive(Default, Deserialize, PartialEq)]
pub struct Bindings<T: InputType> {
pub axis_mappings: Option<FnvHashMap<T, Vec<AxisBind>>>,
pub action_mappings: Option<FnvHashMap<T, Vec<ActionBind>>>,
}
impl<T: InputType> Bindings<T> {
pub fn add_action(mut self, input_type: T, bind: ActionBind) -> Self {
match &mut self.action_mappings {
Some(action_mappings) => match action_mappings.get_mut(&input_type) {
Some(mappings) => {
mappings.push(bind);
}
None => {
action_mappings.insert(input_type, vec![bind]);
}
},
None => {
let mut action_mappings = FnvHashMap::default();
action_mappings.insert(input_type, vec![bind]);
self.action_mappings = Some(action_mappings);
}
};
self
}
pub fn add_axis(mut self, input_type: T, bind: AxisBind) -> Self {
match &mut self.axis_mappings {
Some(axis_mappings) => match axis_mappings.get_mut(&input_type) {
Some(mappings) => {
mappings.push(bind);
}
None => {
axis_mappings.insert(input_type, vec![bind]);
}
},
None => {
let mut axis_mappings = FnvHashMap::default();
axis_mappings.insert(input_type, vec![bind]);
self.axis_mappings = Some(axis_mappings);
}
};
self
}
}
#[derive(Resource, Debug)]
pub struct InputHandler<T: InputType> {
actions_value: FnvHashMap<T, Vec<(Input, bool)>>,
axes_value: FnvHashMap<T, Vec<(Input, f32)>>,
}
impl<T: InputType> Default for InputHandler<T> {
fn default() -> Self {
InputHandler {
actions_value: FnvHashMap::default(),
axes_value: FnvHashMap::default(),
}
}
}
impl<T: InputType> InputHandler<T> {
pub fn axis_value(&self, input_type: T) -> f32 {
self.axes_value
.get(&input_type)
.and_then(|entry| entry.iter().last().map(|last_event| last_event.1))
.unwrap_or(0.0)
}
pub fn action_value(&self, input_type: T) -> bool {
self.actions_value
.get(&input_type)
.and_then(|entry| entry.iter().last().map(|last_event| last_event.1))
.unwrap_or(false)
}
}