Skip to content

Commit

Permalink
Provide API for reading inputs for remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 9, 2024
1 parent 920f9e4 commit f95d814
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/input_context/context_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl ActionMap {

let mut tracker = TriggerTracker::new(ActionValue::zero(self.dim));
for input_map in &mut self.inputs {
if let Some(value) = reader.read(input_map.input, self.consumes_input) {
if let Some(value) = reader.value(input_map.input, self.consumes_input) {
self.last_value = value.convert(self.dim);
}
let mut current_tracker = TriggerTracker::new(self.last_value);
Expand Down
58 changes: 56 additions & 2 deletions src/input_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,61 @@ impl InputReader<'_, '_> {
self.tracker.ignore_mouse = ignore;
}

pub fn update_state(&mut self) {
pub fn read(&mut self) -> impl Iterator<Item = (Input, ActionValue)> + '_ {
self.update_state();

let modifiers = self.tracker.modifiers;
let key_codes = self
.tracker
.key_codes
.iter()
.map(move |(&key_code, &value)| {
(
Input::Keyboard {
key_code,
modifiers,
},
value,
)
});

let mouse_buttons = self
.tracker
.mouse_buttons
.iter()
.map(move |(&button, &value)| (Input::MouseButton { button, modifiers }, value));

let mouse_motion = self
.tracker
.mouse_motion
.map(|value| (Input::MouseMotion { modifiers }, value));

let mouse_wheel = self
.tracker
.mouse_wheel
.map(|value| (Input::MouseWheel { modifiers }, value));

let gamepad_buttons = self
.tracker
.gamepad_buttons
.iter()
.map(|(&button, &value)| (Input::GamepadButton(button), value));

let gamepad_axes = self
.tracker
.gamepad_axes
.iter()
.map(|(&axis, &value)| (Input::GamepadAxis(axis), value));

key_codes
.chain(mouse_buttons)
.chain(mouse_motion)
.chain(mouse_wheel)
.chain(gamepad_buttons)
.chain(gamepad_axes)
}

pub(super) fn update_state(&mut self) {
self.reset_input();

if !self.tracker.ignore_keyboard {
Expand Down Expand Up @@ -115,7 +169,7 @@ impl InputReader<'_, '_> {
self.tracker.gamepad_axes.clear();
}

pub(super) fn read(&mut self, input: Input, consume: bool) -> Option<ActionValue> {
pub(super) fn value(&mut self, input: Input, consume: bool) -> Option<ActionValue> {
match input {
Input::Keyboard {
key_code,
Expand Down

0 comments on commit f95d814

Please sign in to comment.