button_tracker/
lib.rs

1#![deny(missing_docs)]
2
3//! A Piston library for tracking key/mouse press events
4//! from windows for use in update loops
5
6extern crate input;
7
8use input::Button;
9use std::collections::HashMap;
10
11/// Master struct for the button controller.
12/// Consumes button data from any source
13/// (though most commonly window event loop generated press events)
14/// and tracks them in an update loop friendly form.
15#[derive(Clone)]
16pub struct ButtonTracker {
17    current_buttons: HashMap<Button, bool>,
18    last_buttons: HashMap<Button, bool>,
19}
20
21impl ButtonTracker {
22    /// Creates a new ButtonTracker.
23    pub fn new() -> ButtonTracker {
24        ButtonTracker {
25            current_buttons: HashMap::new(),
26            last_buttons: HashMap::new(),
27        }
28    }
29
30    /// Copies the current input state into the last known button state.
31    /// Call this in your update loop logic, once per update.
32    pub fn update(&mut self) {
33        for (button, state) in &self.current_buttons {
34            self.last_buttons.insert(*button, *state);
35        }
36    }
37
38    /// Tracks that a button is currently pressed.
39    /// The most common way to do so is passing in Button contents
40    /// from PressEvents generated from your relevant window class.
41    pub fn register_press(&mut self, button: &Button) {
42        self.current_buttons.insert(*button, true);
43    }
44
45    /// Tracks that a button is currently un-pressed
46    /// (or notes that a previously pressed button is now not pressed).
47    /// The most common way to do so is passing in Button contents
48    /// from ReleaseEvents generated from your relevant window class.
49    pub fn register_release(&mut self, button: &Button) {
50        self.current_buttons.insert(*button, false);
51    }
52
53    /// Checks if a button is pressed as of the current update.
54    /// Will return false if a button has never been registered as pressed.
55    pub fn current_pressed(&self, button: &Button) -> bool {
56        if let Some(state) = self.current_buttons.get(button) {
57            *state
58        } else {
59            false
60        }
61    }
62
63    /// Checks if a button is pressed as of the last update.
64    /// Will return false if a button has never been registered as pressed.
65    pub fn last_pressed(&self, button: &Button) -> bool {
66        if let Some(state) = self.last_buttons.get(button) {
67            *state
68        } else {
69            false
70        }
71    }
72
73    /// Checks for a combination of a button press/release across
74    /// the current and last update.
75    /// Most useful for determining whether a button was just pressed
76    /// or released.
77    pub fn pressed_state(&self,
78                         button: &Button,
79                         last_state: bool,
80                         current_state: bool)
81                         -> bool {
82        self.last_pressed(button) == last_state && self.current_pressed(button) == current_state
83    }
84}