-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinput.rs
More file actions
135 lines (113 loc) · 2.55 KB
/
input.rs
File metadata and controls
135 lines (113 loc) · 2.55 KB
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
/**!
Inputs are configured via a map of axises and coresponding outputs.
For example:
```json
"input": {
"move_right": [
{
"key": "left_arrow",
"scale": -1.0
},
{
"key": "right_arrow",
"scale": 1.0
},
{
"key": "gamepad_left",
"scale": -1.0,
"meta": {
"deadzone": 0.25,
"sensitivity": 1.0
"invert": false
}
}
]
}
```
This is serialized and converted to a map of Strings and Axis'.
These are mapped to winit::Event instances, and updated when those events occur by the engine.
*/
use std::collections::HashMap;
pub type InputConfig = HashMap<String, Axis>;
pub type Axis = Vec<AxisValue>;
#[derive(Serialize, Deserialize, Clone)]
pub struct AxisValue {
#[serde(default = "key_default")]
pub key: String,
#[serde(default = "scale_default")]
pub scale: f32,
#[serde(default = "meta_default")]
pub meta: Option<AxisMeta>,
}
fn key_default() -> String {
//@TODO make unique generated hash based off current system time.
String::from("unknown")
}
fn scale_default() -> f32 {
1.0
}
fn meta_default() -> Option<AxisMeta> {
None
}
fn axis_value(key: String, scale: f32, meta: Option<AxisMeta>) -> AxisValue {
AxisValue { key, scale, meta }
}
#[derive(Serialize, Deserialize, Clone)]
pub struct AxisMeta {
#[serde(default = "deadzone_default")]
pub deadzone: f32,
#[serde(default = "sensitivity_default")]
pub sensitivity: f32,
#[serde(default = "invert_default")]
pub invert: bool,
}
fn deadzone_default() -> f32 {
0.25
}
fn sensitivity_default() -> f32 {
1.0
}
fn invert_default() -> bool {
false
}
macro_rules! input_config {
($($field:ident: [ $([$key:ident, $sensitivity:expr, $meta:expr ]),* ]),*) => {
{
let mut i = InputConfig::new();
$(
i.insert(
String::from(stringify!($field)),
vec![
$(
axis_value(String::from(stringify!($key)), $sensitivity, $meta),
)*
]
);
)*
i
}
};
}
pub fn default_input() -> InputConfig {
let i = input_config!(
move_right: [[
arrow_right,
1.0,
None
],[
arrow_left,
-1.0,
None
]],
move_forward: [[
arrow_up,
1.0,
None
],[
arrow_down,
-1.0,
None
]]
);
i
}