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
use std::hash::Hash;
use std::path::Path;
use std::result::Result as StdResult;
#[cfg(feature = "sdl_controller")]
use sdl_events_system::ControllerMappings;
use amethyst_config::{Config, ConfigError};
use amethyst_core::bundle::{Result, SystemBundle};
use amethyst_core::specs::prelude::DispatcherBuilder;
use serde::de::DeserializeOwned;
use serde::Serialize;
use {Bindings, InputSystem};
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct InputBundle<AX, AC>
where
AX: Hash + Eq,
AC: Hash + Eq,
{
bindings: Option<Bindings<AX, AC>>,
#[cfg(feature = "sdl_controller")]
controller_mappings: Option<ControllerMappings>,
}
impl<AX, AC> InputBundle<AX, AC>
where
AX: Hash + Eq,
AC: Hash + Eq,
{
pub fn new() -> Self {
Default::default()
}
pub fn with_bindings(mut self, bindings: Bindings<AX, AC>) -> Self {
self.bindings = Some(bindings);
self
}
pub fn with_bindings_from_file<P: AsRef<Path>>(self, file: P) -> StdResult<Self, ConfigError>
where
AX: DeserializeOwned + Serialize,
AC: DeserializeOwned + Serialize,
{
Ok(self.with_bindings(Bindings::load_no_fallback(file)?))
}
#[cfg(feature = "sdl_controller")]
pub fn with_sdl_controller_mappings(mut self, mappings: String) -> Self {
self.controller_mappings = Some(ControllerMappings::FromString(mappings));
self
}
#[cfg(feature = "sdl_controller")]
pub fn with_sdl_controller_mappings_from_file<P: AsRef<Path>>(mut self, file: P) -> Self
where
AX: DeserializeOwned + Serialize,
AC: DeserializeOwned + Serialize,
{
use std::path::PathBuf;
let path_buf = PathBuf::from(file.as_ref());
self.controller_mappings = Some(ControllerMappings::FromPath(path_buf));
self
}
}
impl<'a, 'b, AX, AC> SystemBundle<'a, 'b> for InputBundle<AX, AC>
where
AX: Hash + Eq + Clone + Send + Sync + 'static,
AC: Hash + Eq + Clone + Send + Sync + 'static,
{
fn build(self, builder: &mut DispatcherBuilder<'a, 'b>) -> Result<()> {
#[cfg(feature = "sdl_controller")]
{
use super::SdlEventsSystem;
builder.add_thread_local(
SdlEventsSystem::<AX, AC>::new(self.controller_mappings).unwrap(),
);
}
builder.add(
InputSystem::<AX, AC>::new(self.bindings),
"input_system",
&[],
);
Ok(())
}
}