ratatui/examples/canvas.rs

147 lines
3.9 KiB
Rust
Raw Normal View History

#[allow(dead_code)]
mod util;
2016-11-07 23:35:46 +00:00
use std::io;
use std::time::Duration;
2016-11-07 23:35:46 +00:00
use termion::event::Key;
use termion::input::MouseTerminal;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout, Rect};
2016-11-07 23:35:46 +00:00
use tui::style::Color;
2019-02-03 21:07:58 +00:00
use tui::widgets::canvas::{Canvas, Map, MapResolution, Rectangle};
2018-05-06 10:59:24 +00:00
use tui::widgets::{Block, Borders, Widget};
2018-06-09 09:02:37 +00:00
use tui::Terminal;
2016-11-07 23:35:46 +00:00
2019-01-06 11:57:06 +00:00
use crate::util::event::{Config, Event, Events};
2016-11-07 23:35:46 +00:00
struct App {
x: f64,
y: f64,
ball: Rect,
playground: Rect,
vx: u16,
vy: u16,
2017-05-08 19:34:41 +00:00
dir_x: bool,
dir_y: bool,
2016-11-07 23:35:46 +00:00
}
impl App {
fn new() -> App {
App {
x: 0.0,
y: 0.0,
2017-05-08 19:34:41 +00:00
ball: Rect::new(10, 30, 10, 10),
2016-11-07 23:35:46 +00:00
playground: Rect::new(10, 10, 100, 100),
vx: 1,
vy: 1,
2017-05-08 19:34:41 +00:00
dir_x: true,
dir_y: true,
2016-11-07 23:35:46 +00:00
}
}
fn update(&mut self) {
2017-10-30 21:28:37 +00:00
if self.ball.left() < self.playground.left() || self.ball.right() > self.playground.right()
2017-09-11 05:58:37 +00:00
{
2017-05-21 09:13:24 +00:00
self.dir_x = !self.dir_x;
}
2017-10-30 21:28:37 +00:00
if self.ball.top() < self.playground.top() || self.ball.bottom() > self.playground.bottom()
2017-09-11 05:58:37 +00:00
{
2017-05-21 09:13:24 +00:00
self.dir_y = !self.dir_y;
}
2017-05-08 19:34:41 +00:00
if self.dir_x {
self.ball.x += self.vx;
} else {
self.ball.x -= self.vx;
}
if self.dir_y {
self.ball.y += self.vy;
} else {
self.ball.y -= self.vy
2016-11-07 23:35:46 +00:00
}
}
}
fn main() -> Result<(), failure::Error> {
2016-11-07 23:35:46 +00:00
// Terminal initialization
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
// Setup event handlers
let config = Config {
tick_rate: Duration::from_millis(100),
..Default::default()
};
let events = Events::with_config(config);
2016-11-07 23:35:46 +00:00
// App
let mut app = App::new();
loop {
terminal.draw(|mut f| {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(f.size());
Canvas::default()
.block(Block::default().borders(Borders::ALL).title("World"))
.paint(|ctx| {
ctx.draw(&Map {
color: Color::White,
resolution: MapResolution::High,
});
ctx.print(app.x, -app.y, "You are here", Color::Yellow);
})
.x_bounds([-180.0, 180.0])
.y_bounds([-90.0, 90.0])
.render(&mut f, chunks[0]);
Canvas::default()
.block(Block::default().borders(Borders::ALL).title("Pong"))
.paint(|ctx| {
2019-01-14 20:14:57 +00:00
ctx.draw(&Rectangle {
rect: app.ball,
color: Color::Yellow,
});
})
.x_bounds([10.0, 110.0])
.y_bounds([10.0, 110.0])
.render(&mut f, chunks[1]);
})?;
match events.next()? {
2017-10-30 21:28:37 +00:00
Event::Input(input) => match input {
Key::Char('q') => {
2017-10-30 21:28:37 +00:00
break;
2016-11-07 23:35:46 +00:00
}
Key::Down => {
2017-10-30 21:28:37 +00:00
app.y += 1.0;
}
Key::Up => {
2017-10-30 21:28:37 +00:00
app.y -= 1.0;
}
Key::Right => {
2017-10-30 21:28:37 +00:00
app.x += 1.0;
}
Key::Left => {
2017-10-30 21:28:37 +00:00
app.x -= 1.0;
}
_ => {}
},
2016-11-07 23:35:46 +00:00
Event::Tick => {
app.update();
2016-11-07 23:35:46 +00:00
}
}
}
Ok(())
2016-11-07 23:35:46 +00:00
}