tui-rs/examples/canvas.rs

205 lines
5.5 KiB
Rust
Raw Normal View History

2016-11-07 23:35:46 +00:00
extern crate termion;
2017-10-30 21:28:37 +00:00
extern crate tui;
2016-11-07 23:35:46 +00:00
use std::io;
2018-05-06 10:59:24 +00:00
use std::sync::mpsc;
2016-11-07 23:35:46 +00:00
use std::thread;
use std::time;
use termion::event;
use termion::input::TermRead;
2017-09-03 13:34:05 +00:00
use tui::backend::MouseBackend;
use tui::layout::{Constraint, Direction, Layout, Rect};
2016-11-07 23:35:46 +00:00
use tui::style::Color;
2018-05-06 10:59:24 +00:00
use tui::widgets::canvas::{Canvas, Line, Map, MapResolution};
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
struct App {
size: Rect,
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 {
size: Default::default(),
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 advance(&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
}
}
}
enum Event {
Input(event::Key),
Tick,
}
fn main() {
// Terminal initialization
2017-09-03 13:34:05 +00:00
let backend = MouseBackend::new().unwrap();
2016-11-07 23:35:46 +00:00
let mut terminal = Terminal::new(backend).unwrap();
// Channels
let (tx, rx) = mpsc::channel();
let input_tx = tx.clone();
let clock_tx = tx.clone();
// Input
thread::spawn(move || {
let stdin = io::stdin();
for c in stdin.keys() {
let evt = c.unwrap();
input_tx.send(Event::Input(evt)).unwrap();
if evt == event::Key::Char('q') {
break;
}
}
});
// Tick
2017-05-21 09:13:24 +00:00
thread::spawn(move || loop {
2017-09-11 05:58:37 +00:00
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
2016-11-07 23:35:46 +00:00
// App
let mut app = App::new();
// First draw call
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
2017-05-08 19:34:41 +00:00
app.size = terminal.size().unwrap();
draw(&mut terminal, &app).unwrap();
2016-11-07 23:35:46 +00:00
loop {
2017-05-08 19:34:41 +00:00
let size = terminal.size().unwrap();
if size != app.size {
terminal.resize(size).unwrap();
app.size = size;
}
2016-11-07 23:35:46 +00:00
let evt = rx.recv().unwrap();
match evt {
2017-10-30 21:28:37 +00:00
Event::Input(input) => match input {
event::Key::Char('q') => {
break;
2016-11-07 23:35:46 +00:00
}
2017-10-30 21:28:37 +00:00
event::Key::Down => {
app.y += 1.0;
}
event::Key::Up => {
app.y -= 1.0;
}
event::Key::Right => {
app.x += 1.0;
}
event::Key::Left => {
app.x -= 1.0;
}
_ => {}
},
2016-11-07 23:35:46 +00:00
Event::Tick => {
app.advance();
}
}
draw(&mut terminal, &app).unwrap();
2016-11-07 23:35:46 +00:00
}
terminal.show_cursor().unwrap();
}
fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
t.draw(|mut f| {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
2018-08-12 22:27:56 +00:00
.split(app.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])
2018-08-12 22:27:56 +00:00
.render(&mut f, chunks[0]);
Canvas::default()
.block(Block::default().borders(Borders::ALL).title("List"))
.paint(|ctx| {
ctx.draw(&Line {
x1: f64::from(app.ball.left()),
y1: f64::from(app.ball.top()),
x2: f64::from(app.ball.right()),
y2: f64::from(app.ball.top()),
color: Color::Yellow,
});
ctx.draw(&Line {
x1: f64::from(app.ball.right()),
y1: f64::from(app.ball.top()),
x2: f64::from(app.ball.right()),
y2: f64::from(app.ball.bottom()),
color: Color::Yellow,
});
ctx.draw(&Line {
x1: f64::from(app.ball.right()),
y1: f64::from(app.ball.bottom()),
x2: f64::from(app.ball.left()),
y2: f64::from(app.ball.bottom()),
color: Color::Yellow,
});
ctx.draw(&Line {
x1: f64::from(app.ball.left()),
y1: f64::from(app.ball.bottom()),
x2: f64::from(app.ball.left()),
y2: f64::from(app.ball.top()),
color: Color::Yellow,
});
})
.x_bounds([10.0, 110.0])
.y_bounds([10.0, 110.0])
2018-08-12 22:27:56 +00:00
.render(&mut f, chunks[1]);
})
2016-11-07 23:35:46 +00:00
}