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
use crate::app::FrameContext;
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Options for button source.
pub enum ButtonSource {
/// Key option.
Key(String), // Debug-format of winit::keyboard::Key (e.g., "Character(\"A\")", "Named(Enter)")
/// Mouse left option.
MouseLeft,
/// Mouse right option.
MouseRight,
/// Mouse middle option.
MouseMiddle,
/// Gamepad button option.
GamepadButton(String), // Placeholder for future gamepad support
}
#[derive(Debug, Clone)]
/// ButtonBinding data.
pub struct ButtonBinding {
/// Source value.
pub source: ButtonSource,
}
#[derive(Debug, Clone)]
/// Options for axis source.
pub enum AxisSource {
/// Pair of keyboard keys that drive a signed axis.
KeyPair {
/// Key that drives the negative direction.
negative: String,
/// Key that drives the positive direction.
positive: String,
},
/// Named gamepad axis.
GamepadAxis {
/// Gamepad axis name.
name: String,
},
}
#[derive(Debug, Clone)]
/// AxisBinding data.
pub struct AxisBinding {
/// Source value.
pub source: AxisSource,
/// Uniform scale multiplier.
pub scale: f32,
/// Deadzone value.
pub deadzone: f32,
}
#[derive(Debug, Default, Clone)]
/// ActionMap data.
pub struct ActionMap {
button_bindings: HashMap<String, Vec<ButtonBinding>>, // action name -> bindings
axis_bindings: HashMap<String, Vec<AxisBinding>>, // axis name -> bindings
}
impl ActionMap {
/// Creates a new value.
pub fn new() -> Self {
Self::default()
}
/// Bind button.
pub fn bind_button(&mut self, action: impl Into<String>, source: ButtonSource) {
self.button_bindings
.entry(action.into())
.or_default()
.push(ButtonBinding { source });
}
/// Bind axis.
pub fn bind_axis(
&mut self,
axis: impl Into<String>,
source: AxisSource,
scale: f32,
deadzone: f32,
) {
self.axis_bindings
.entry(axis.into())
.or_default()
.push(AxisBinding {
source,
scale,
deadzone,
});
}
// Resolve pressed actions and axis values for this frame using FrameContext events
// Buttons are edge-triggered: pressed if any binding fired this frame
// Axes from KeyPair: -1 for negative key, +1 for positive key when present in this frame
/// Resolve.
pub fn resolve(&self, frame: &FrameContext) -> (HashSet<String>, HashMap<String, f32>) {
let mut pressed: HashSet<String> = HashSet::new();
let mut axes: HashMap<String, f32> = HashMap::new();
// Precompute key strings for this frame
let key_strs: Vec<String> = frame
.pressed_keys
.iter()
.map(|key| key.debug_name())
.collect();
// Buttons
for (action, binds) in &self.button_bindings {
for b in binds {
let fired = match &b.source {
ButtonSource::Key(s) => key_strs.iter().any(|ks| ks == s),
ButtonSource::MouseLeft => frame.mouse_info.is_lmb_clicked,
ButtonSource::MouseRight => frame.mouse_info.is_rmb_clicked,
ButtonSource::MouseMiddle => frame.mouse_info.is_mmb_clicked,
ButtonSource::GamepadButton(_name) => false, // not implemented yet
};
if fired {
pressed.insert(action.clone());
break;
}
}
}
// Axes
for (axis, binds) in &self.axis_bindings {
let mut value = 0.0f32;
for b in binds {
match &b.source {
AxisSource::KeyPair { negative, positive } => {
let neg = key_strs.iter().any(|ks| ks == negative);
let pos = key_strs.iter().any(|ks| ks == positive);
let v = match (neg, pos) {
(true, false) => -1.0,
(false, true) => 1.0,
_ => 0.0,
};
value += v * b.scale;
}
AxisSource::GamepadAxis { .. } => {
// not implemented; placeholder
}
}
}
// Deadzone
if value.abs() < binds.iter().map(|b| b.deadzone).fold(0.0, f32::max) {
value = 0.0;
}
axes.insert(axis.clone(), value.clamp(-1.0, 1.0));
}
(pressed, axes)
}
}