Skip to content

Commit 83a59c8

Browse files
combination_pressed no longer spams true
1 parent 91586ba commit 83a59c8

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/utils/runtime.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct ProjectConfig {
6565
#[derive(Debug)]
6666
pub struct InputManager {
6767
key_history: Vec<glfw::Key>,
68+
key_combination_used: bool,
6869
keys_down: HashSet<glfw::Key>,
6970
keys_pressed: HashSet<glfw::Key>,
7071
keys_released: HashSet<glfw::Key>,
@@ -77,6 +78,7 @@ impl InputManager {
7778
pub fn new() -> Self {
7879
Self {
7980
key_history: Vec::new(),
81+
key_combination_used: false,
8082
keys_down: HashSet::new(),
8183
keys_pressed: HashSet::new(),
8284
keys_released: HashSet::new(),
@@ -100,6 +102,7 @@ impl InputManager {
100102
match event {
101103
glfw::WindowEvent::Key(key, _, action, _) => match action {
102104
glfw::Action::Press => {
105+
self.key_combination_used = false;
103106
if !self.keys_down.contains(&key) {
104107
self.keys_pressed.insert(key);
105108
}
@@ -137,6 +140,12 @@ impl InputManager {
137140
&self.key_history
138141
}
139142

143+
pub fn combination_pressed(&mut self, key_codes: &[glfw::Key]) -> bool {
144+
let previous_key_combination_used = self.key_combination_used;
145+
self.key_combination_used = self.key_history().ends_with(&key_codes);
146+
self.key_combination_used && !previous_key_combination_used
147+
}
148+
140149
pub fn is_key_down(&self, key: glfw::Key) -> bool {
141150
self.keys_down.contains(&key)
142151
}

src/utils/sprite/builtins/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn last_key(state: &State) -> function::Result {
4040
}
4141
}
4242

43-
pub fn combination_pressed(state: &State, args: &[Value]) -> function::Result {
43+
pub fn combination_pressed(state: &mut State, args: &[Value]) -> function::Result {
4444
let key_codes: Result<Vec<glfw::Key>, _> = args
4545
.iter()
4646
.map(|arg| match arg {
@@ -54,7 +54,7 @@ pub fn combination_pressed(state: &State, args: &[Value]) -> function::Result {
5454
_ => return Ok(Value::Boolean(false)),
5555
};
5656

57-
let matches = state.input_manager.key_history().ends_with(&key_codes);
57+
let matches = state.input_manager.combination_pressed(&key_codes);
5858
Ok(Value::Boolean(matches))
5959
}
6060

0 commit comments

Comments
 (0)