Skip to content

Commit

Permalink
Handle missing hashmap entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice Cecile committed Nov 13, 2023
1 parent 1976d5d commit 253732f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

- Chords no longer have a max length
- `InputMap`, `UserInput` and all of the contained types now implement `Reflect`. As a result, the trait bound on `Actionlike` has been changed from `TypePath` to `Reflect`
- Added more constants for testing `ActionData::PRESSED/RELEASED/JUST_PRESSED/JUST_RELEASED` and `Timing::TEST`
- Added more constants for testing: `ActionData::PRESSED/RELEASED/JUST_PRESSED/JUST_RELEASED/CONSUMED` and `Timing::TEST`
- `ActionData::iter`, `ActionData::keys` and `InputMap::keys` have been added

### Performance
Expand Down
35 changes: 27 additions & 8 deletions src/action_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl ActionData {
timing: Timing::TEST,
consumed: false,
};

/// A default value, representing the state of an action that is currently.
pub const CONSUMED: ActionData = ActionData {
state: ButtonState::JustReleased,
value: 0.0,
axis_pair: None,
timing: Timing::TEST,
consumed: true,
};
}

/// Stores the canonical input-method-agnostic representation of the inputs received
Expand Down Expand Up @@ -374,7 +383,8 @@ impl<A: Actionlike> ActionState<A> {

action_data.state.press();
} else {
todo!("Handle missing action data")
self.action_data_map
.insert(action.clone(), ActionData::JUST_PRESSED);
}
}

Expand All @@ -393,7 +403,8 @@ impl<A: Actionlike> ActionState<A> {

action_data.state.release();
} else {
todo!("Handle missing action data")
self.action_data_map
.insert(action.clone(), ActionData::JUST_RELEASED);
}
}

Expand Down Expand Up @@ -443,7 +454,8 @@ impl<A: Actionlike> ActionState<A> {
if let Some(action_data) = self.action_data_map.get_mut(action) {
action_data.consumed = true;
} else {
todo!("Handle missing action data")
self.action_data_map
.insert(action.clone(), ActionData::CONSUMED);
}
}

Expand All @@ -467,48 +479,55 @@ impl<A: Actionlike> ActionState<A> {
}

/// Is this `action` currently pressed?
///
/// If the action is missing from the [`ActionState`], this will return `false`.
#[inline]
#[must_use]
pub fn pressed(&self, action: &A) -> bool {
if let Some(action_data) = self.action_data_map.get(action) {
action_data.state.pressed()
} else {
todo!("Handle missing action data")
false
}
}

/// Was this `action` pressed since the last time [tick](ActionState::tick) was called?
///
/// If the action is missing from the [`ActionState`], this will return `false`.
#[inline]
#[must_use]
pub fn just_pressed(&self, action: &A) -> bool {
if let Some(action_data) = self.action_data_map.get(action) {
action_data.state.just_pressed()
} else {
todo!("Handle missing action data")
false
}
}

/// Is this `action` currently released?
///
/// This is always the logical negation of [pressed](ActionState::pressed)
/// This is always the logical negation of [pressed](ActionState::pressed).
/// If the action is missing from the [`ActionState`], this will return `false`.
#[inline]
#[must_use]
pub fn released(&self, action: &A) -> bool {
if let Some(action_data) = self.action_data_map.get(action) {
action_data.state.released()
} else {
todo!("Handle missing action data")
true
}
}

/// Was this `action` released since the last time [tick](ActionState::tick) was called?
///
/// If the action is missing from the [`ActionState`], this will return `false`.
#[inline]
#[must_use]
pub fn just_released(&self, action: &A) -> bool {
if let Some(action_data) = self.action_data_map.get(action) {
action_data.state.just_released()
} else {
todo!("Handle missing action data")
false
}
}

Expand Down
32 changes: 15 additions & 17 deletions src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,32 +365,30 @@ impl<A: Actionlike> InputMap<A> {
let mut inputs = Vec::new();

for input in input_vec {
if let Some(action_data) = action_data_map.get_mut(action) {
// Merge axis pair into action data
let axis_pair = input_streams.input_axis_pair(input);
if let Some(axis_pair) = axis_pair {
if let Some(current_axis_pair) = &mut action_data.axis_pair {
*current_axis_pair = current_axis_pair.merged_with(axis_pair);
} else {
action_data.axis_pair = Some(axis_pair);
}
let action_data = action_data_map
.entry(action.clone())
.or_insert_with(ActionData::default);

// Merge axis pair into action data
let axis_pair = input_streams.input_axis_pair(input);
if let Some(axis_pair) = axis_pair {
if let Some(current_axis_pair) = &mut action_data.axis_pair {
*current_axis_pair = current_axis_pair.merged_with(axis_pair);
} else {
action_data.axis_pair = Some(axis_pair);
}
}

if input_streams.input_pressed(input) {
inputs.push(input.clone());
if input_streams.input_pressed(input) {
inputs.push(input.clone());

action_data.value += input_streams.input_value(input, true);
}
} else {
todo!("Handle missing actions.");
action_data.value += input_streams.input_value(input, true);
}
}

if !inputs.is_empty() {
if let Some(action_data) = action_data_map.get_mut(action) {
action_data.state = ButtonState::JustPressed;
} else {
todo!("Handle missing actions.");
}
}
}
Expand Down

0 comments on commit 253732f

Please sign in to comment.