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
use std::hash::Hash;
use amethyst_core::shrev::{EventChannel, ReaderId};
use amethyst_core::specs::prelude::{Read, Resources, System, Write};
use winit::Event;
use {Bindings, InputEvent, InputHandler};
pub struct InputSystem<AX, AC>
where
AX: Hash + Eq,
AC: Hash + Eq,
{
reader: Option<ReaderId<Event>>,
bindings: Option<Bindings<AX, AC>>,
}
impl<AX, AC> InputSystem<AX, AC>
where
AX: Hash + Eq,
AC: Hash + Eq,
{
pub fn new(bindings: Option<Bindings<AX, AC>>) -> Self {
InputSystem {
reader: None,
bindings,
}
}
fn process_event(
event: &Event,
handler: &mut InputHandler<AX, AC>,
output: &mut EventChannel<InputEvent<AC>>,
) where
AX: Hash + Eq + Clone + Send + Sync + 'static,
AC: Hash + Eq + Clone + Send + Sync + 'static,
{
handler.send_event(event, output);
}
}
impl<'a, AX, AC> System<'a> for InputSystem<AX, AC>
where
AX: Hash + Eq + Clone + Send + Sync + 'static,
AC: Hash + Eq + Clone + Send + Sync + 'static,
{
type SystemData = (
Read<'a, EventChannel<Event>>,
Write<'a, InputHandler<AX, AC>>,
Write<'a, EventChannel<InputEvent<AC>>>,
);
fn run(&mut self, (input, mut handler, mut output): Self::SystemData) {
for event in input.read(&mut self.reader.as_mut().unwrap()) {
Self::process_event(event, &mut *handler, &mut *output);
}
}
fn setup(&mut self, res: &mut Resources) {
use amethyst_core::specs::prelude::SystemData;
Self::SystemData::setup(res);
self.reader = Some(res.fetch_mut::<EventChannel<Event>>().register_reader());
if let Some(ref bindings) = self.bindings {
res.fetch_mut::<InputHandler<AX, AC>>().bindings = bindings.clone();
}
}
}