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

source

pub fn new() -> Self

Create a new ButtonController. This is the preferred way to create it.

source

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.

source

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.

source

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.

source

pub fn vector<T>(&self) -> Vector2D<T>
where T: From<i32> + FixedWidthUnsignedInteger,

Returns true if all the buttons specified in keys are pressed.

source

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.

source

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.

source

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.

source

pub fn is_pressed(&self, keys: Button) -> bool

Returns true if the provided keys are all pressed, and false if not.

source

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).

source

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
    }
}
source

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.

Trait Implementations§

source§

impl Default for ButtonController

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for ButtonController

§

impl RefUnwindSafe for ButtonController

§

impl Send for ButtonController

§

impl Sync for ButtonController

§

impl Unpin for ButtonController

§

impl UnwindSafe for ButtonController

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.