Trait playdate_controls::buttons::PDButtonsExt
source · pub trait PDButtonsExt: Sized + BitAnd<Self> {
Show 16 methods
// Required methods
fn contains(&self, other: Self) -> bool;
fn contains_ref(&self, other: &Self) -> bool;
fn any(&self) -> bool;
fn is_empty(&self) -> bool;
fn Left() -> Self;
fn Right() -> Self;
fn Up() -> Self;
fn Down() -> Self;
fn B() -> Self;
fn A() -> Self;
// Provided methods
fn left(&self) -> bool { ... }
fn right(&self) -> bool { ... }
fn up(&self) -> bool { ... }
fn down(&self) -> bool { ... }
fn b(&self) -> bool { ... }
fn a(&self) -> bool { ... }
}Required Methods§
sourcefn contains(&self, other: Self) -> bool
fn contains(&self, other: Self) -> bool
Contains other button.
Note, other can contains one or many buttons.
In case of there’s many buttons, returns true if
at least one button of other is contained in this.
sourcefn contains_ref(&self, other: &Self) -> bool
fn contains_ref(&self, other: &Self) -> bool
Contains other button.
Same as Self::contains but not taking ownership of other.
sourcefn any(&self) -> bool
fn any(&self) -> bool
Contains any buttons, opposite of Self::is_empty.
fn Left() -> Self
fn Right() -> Self
fn Up() -> Self
fn Down() -> Self
fn B() -> Self
fn A() -> Self
Provided Methods§
sourcefn left(&self) -> bool
fn left(&self) -> bool
Contains left button.
Examples found in repository?
examples/buttons.rs (line 83)
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(())
}
}sourcefn right(&self) -> bool
fn right(&self) -> bool
Contains right button.
Examples found in repository?
examples/buttons.rs (line 80)
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(())
}
}sourcefn up(&self) -> bool
fn up(&self) -> bool
Contains up button.
Examples found in repository?
examples/buttons.rs (line 86)
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(())
}
}sourcefn down(&self) -> bool
fn down(&self) -> bool
Contains down button.
Examples found in repository?
examples/buttons.rs (line 89)
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(())
}
}