cursive/examples/position.rs
2018-06-16 11:03:39 -07:00

44 lines
1.2 KiB
Rust

extern crate cursive;
use cursive::view::Position;
use cursive::views::LayerPosition;
use cursive::views::TextView;
use cursive::Cursive;
/// Moves top layer by the specified amount
fn move_top(c: &mut Cursive, x_in: isize, y_in: isize) {
// Step 1. Get the current position of the layer.
let s = c.screen_mut();
let l = LayerPosition::FromFront(0);
// Step 2. add the specifed amount
let pos = s.offset().saturating_add((x_in, y_in));
// convert the new x and y into a position
let p = Position::absolute(pos);
// Step 3. Apply the new position
s.reposition_layer(l, p);
}
fn main() {
let mut siv = Cursive::default();
// We can quit by pressing `q`
siv.add_global_callback('q', Cursive::quit);
// Next Gen FPS Controls.
siv.add_global_callback('w', |s| move_top(s, 0, -1));
siv.add_global_callback('a', |s| move_top(s, -1, 0));
siv.add_global_callback('s', |s| move_top(s, 0, 1));
siv.add_global_callback('d', |s| move_top(s, 1, 0));
// Add window to fly around.
siv.add_layer(TextView::new(
"Press w,a,s,d to move the window.\n\
Press q to quit the application.",
));
// Run the event loop
siv.run();
}