Trait piston_window::prelude::PressEvent
source · pub trait PressEvent: Sized {
// Required methods
fn from_button(button: Button, old_event: &Self) -> Option<Self>;
fn press<U, F>(&self, f: F) -> Option<U>
where F: FnMut(Button) -> U;
// Provided method
fn press_args(&self) -> Option<Button> { ... }
}
Expand description
The press of a button.
Required Methods§
Creates a press event.
Preserves scancode from original button event, if any. Preserves time stamp from original input event, if any.
Provided Methods§
sourcefn press_args(&self) -> Option<Button>
fn press_args(&self) -> Option<Button>
Returns press arguments.
Examples found in repository?
examples/hello_piston.rs (line 24)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fn main() {
let title = "Hello Piston! (press any key to enter inner loop)";
let mut window: PistonWindow = WindowSettings::new(title, [640, 480])
.exit_on_esc(true)
.build()
.unwrap_or_else(|e| panic!("Failed to build PistonWindow: {}", e));
window.set_lazy(true);
while let Some(e) = window.next() {
window.draw_2d(&e, |c, g, _| {
clear([0.5, 1.0, 0.5, 1.0], g);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[50.0, 50.0, 100.0, 100.0],
c.transform,
g,
);
});
if e.press_args().is_some() {
InnerApp {
title: "Inner loop (press X to exit inner loop)",
exit_button: Button::Keyboard(Key::X),
}
.run(&mut window);
window.set_title(title.into());
}
}
}
/// Stores application state of inner event loop.
pub struct InnerApp {
pub title: &'static str,
pub exit_button: Button,
}
impl InnerApp {
pub fn run(&mut self, window: &mut PistonWindow) {
window.set_title(self.title.into());
while let Some(e) = window.next() {
window.draw_2d(&e, |c, g, _| {
clear([0.5, 0.5, 1.0, 1.0], g);
ellipse(
[1.0, 0.0, 0.0, 1.0],
[50.0, 50.0, 100.0, 100.0],
c.transform,
g,
);
});
if let Some(button) = e.press_args() {
if button == self.exit_button {
break;
}
}
}
}
Object Safety§
This trait is not object safe.