Struct agb::input::ButtonController
source · pub struct ButtonController { /* private fields */ }
Expand description
Helper to make it easy to get the current state of the GBA’s buttons.
§Example
use agb::input::{ButtonController, Tri};
let mut input = ButtonController::new();
loop {
input.update(); // call update every loop
match input.x_tri() {
Tri::Negative => { /* left is being pressed */ }
Tri::Positive => { /* right is being pressed */ }
Tri::Zero => { /* Neither left nor right (or both) are pressed */ }
}
}
Implementations§
source§impl ButtonController
impl ButtonController
sourcepub fn update(&mut self)
pub fn update(&mut self)
Updates the state of the button controller. You should call this every frame (either at the start or the end) to ensure that you have the latest state of each button press. Calls to any method won’t change until you call this.
sourcepub fn x_tri(&self) -> Tri
pub fn x_tri(&self) -> Tri
Returns Tri::Positive if right is pressed, Tri::Negative if left is pressed and Tri::Zero if neither or both are pressed. This is the normal behaviour you’ll want if you’re using orthogonal inputs.
sourcepub fn y_tri(&self) -> Tri
pub fn y_tri(&self) -> Tri
Returns Tri::Positive if down is pressed, Tri::Negative if up is pressed and Tri::Zero if neither or both are pressed. This is the normal behaviour you’ll want if you’re using orthogonal inputs.
sourcepub fn vector<T>(&self) -> Vector2D<T>where
T: From<i32> + FixedWidthUnsignedInteger,
pub fn vector<T>(&self) -> Vector2D<T>where
T: From<i32> + FixedWidthUnsignedInteger,
Returns true if all the buttons specified in keys
are pressed.
sourcepub fn just_pressed_x_tri(&self) -> Tri
pub fn just_pressed_x_tri(&self) -> Tri
Returns Tri::Positive if left was just pressed, Tri::Negative if right was just pressed and Tri::Zero if neither or both are just pressed.
Also returns Tri::Zero after the call to update()
if the button is still held.
sourcepub fn just_pressed_y_tri(&self) -> Tri
pub fn just_pressed_y_tri(&self) -> Tri
Returns Tri::Positive if down was just pressed, Tri::Negative if up was just pressed and Tri::Zero if neither or both are just pressed.
Also returns Tri::Zero after the call to update()
if the button is still held.
sourcepub fn just_pressed_vector<T>(&self) -> Vector2D<T>where
T: From<i32> + FixedWidthUnsignedInteger,
pub fn just_pressed_vector<T>(&self) -> Vector2D<T>where
T: From<i32> + FixedWidthUnsignedInteger,
Returns a vector which represents the direction the button was just pressed in.
sourcepub fn is_pressed(&self, keys: Button) -> bool
pub fn is_pressed(&self, keys: Button) -> bool
Returns true
if the provided keys are all pressed, and false
if not.
sourcepub fn is_released(&self, keys: Button) -> bool
pub fn is_released(&self, keys: Button) -> bool
Returns true if all the buttons specified in keys
are not pressed. Equivalent to !is_pressed(keys)
.
sourcepub fn is_just_pressed(&self, keys: Button) -> bool
pub fn is_just_pressed(&self, keys: Button) -> bool
Returns true if all the buttons specified in keys
went from not pressed to pressed in the last frame.
Very useful for menu navigation or selection if you want the players actions to only happen for one frame.
§Example
use agb::input::{Button, ButtonController};
let mut button_controller = ButtonController::new();
loop {
button_controller.update();
if button_controller.is_just_pressed(Button::A) {
// A button was just pressed, maybe select the currently selected item
}
}
sourcepub fn is_just_released(&self, keys: Button) -> bool
pub fn is_just_released(&self, keys: Button) -> bool
Returns true if all the buttons specified in keys
went from pressed to not pressed in the last frame.
Very useful for menu navigation or selection if you want players actions to only happen for one frame.