Struct playdate_controls::peripherals::Buttons
source · pub struct Buttons {
pub current: PDButtons,
pub pushed: PDButtons,
pub released: PDButtons,
}Expand description
Wrapped Buttons API.
Represents buttons state.
currentindicates which buttons are currently down.pushedandreleasedreflects which buttons were pushed or released over the previous update cycle.
Note: at the nominal frame rate of 50 ms, fast button presses can be missed if you just poll the instantaneous state.
Fields§
§current: PDButtonsIndicating which buttons are currently down.
pushed: PDButtonsReflect which buttons were pushed over the previous update cycle. See struct doc.
released: PDButtonsReflect which buttons were released over the previous update cycle. See struct doc.
Implementations§
source§impl Buttons
impl Buttons
sourcepub fn get() -> Option<Self>
pub fn get() -> Option<Self>
Uses getButtonState.
Examples found in repository?
examples/buttons.rs (line 47)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
fn update(&mut self) -> Option<()> {
// get buttons state
let buttons = controls::peripherals::Buttons::get()?;
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// render buttons state to string
let text: Cow<str> = if buttons.current.is_empty() {
"[Press any button]".into()
} else {
format!("{:?}", buttons.current.iter().singles().collect::<Vec<_>>()).into()
};
let c_text = CString::new(text.as_ref()).ok()?;
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
c_text.as_ptr() as *const _,
text.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
c_text.as_ptr() as *const _,
text.len(),
PDStringEncoding::kUTF8Encoding,
self.pos.x,
self.pos.y,
);
// move the label
const SPEED: i32 = 4;
if buttons.current.right() {
self.pos.x += SPEED;
}
if buttons.current.left() {
self.pos.x -= SPEED;
}
if buttons.current.up() {
self.pos.y -= SPEED;
}
if buttons.current.down() {
self.pos.y += SPEED;
}
// check screen boundaries
if self.pos.x < 0 {
self.pos.x = 0
} else if self.pos.x > LCD_COLUMNS as i32 - text_width {
self.pos.x = LCD_COLUMNS as i32 - text_width
}
if self.pos.y < 0 {
self.pos.y = 0
} else if self.pos.y > LCD_ROWS as i32 - TEXT_HEIGHT as i32 {
self.pos.y = LCD_ROWS as i32 - TEXT_HEIGHT as i32
}
(*(*sys::API).system).drawFPS?(0, 0);
Some(())
}
}sourcepub fn get_to(
current: &mut PDButtons,
pushed: &mut PDButtons,
released: &mut PDButtons
) -> Option<()>
pub fn get_to( current: &mut PDButtons, pushed: &mut PDButtons, released: &mut PDButtons ) -> Option<()>
Uses getButtonState.
sourcepub fn get_current() -> Option<PDButtons>
pub fn get_current() -> Option<PDButtons>
Uses getButtonState.
Requests & returns only current part of state, see Self::current
sourcepub fn get_pushed() -> Option<PDButtons>
pub fn get_pushed() -> Option<PDButtons>
Uses getButtonState.
Requests & returns only current part of state, see Self::pushed
sourcepub fn get_released() -> Option<PDButtons>
pub fn get_released() -> Option<PDButtons>
Uses getButtonState.
Requests & returns only current part of state, see Self::released
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Buttons
impl Send for Buttons
impl Sync for Buttons
impl Unpin for Buttons
impl UnwindSafe for Buttons
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more