2016-11-07 23:35:46 +00:00
|
|
|
extern crate tui;
|
|
|
|
extern crate termion;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
use std::thread;
|
|
|
|
use std::time;
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
use termion::event;
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
2016-11-28 08:52:51 +00:00
|
|
|
use tui::Terminal;
|
|
|
|
use tui::backend::TermionBackend;
|
2016-11-07 23:35:46 +00:00
|
|
|
use tui::widgets::{Widget, Block, border};
|
|
|
|
use tui::widgets::canvas::{Canvas, Map, MapResolution, Line};
|
|
|
|
use tui::layout::{Group, Rect, Direction, Size};
|
|
|
|
use tui::style::Color;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if self.ball.left() < self.playground.left() ||
|
2017-05-21 09:13:24 +00:00
|
|
|
self.ball.right() > self.playground.right() {
|
|
|
|
self.dir_x = !self.dir_x;
|
|
|
|
}
|
2017-05-08 19:34:41 +00:00
|
|
|
if self.ball.top() < self.playground.top() ||
|
2017-05-21 09:13:24 +00:00
|
|
|
self.ball.bottom() > self.playground.bottom() {
|
|
|
|
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
|
|
|
|
let backend = TermionBackend::new().unwrap();
|
|
|
|
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 {
|
|
|
|
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();
|
2016-11-07 23:35:46 +00:00
|
|
|
draw(&mut terminal, &app);
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Event::Input(input) => {
|
|
|
|
match input {
|
|
|
|
event::Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Tick => {
|
|
|
|
app.advance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
draw(&mut terminal, &app);
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.show_cursor().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
|
|
|
|
|
|
|
|
|
|
|
|
Group::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.sizes(&[Size::Percent(50), Size::Percent(50)])
|
|
|
|
.render(t, &app.size, |t, chunks| {
|
|
|
|
Canvas::default()
|
2017-05-21 09:13:24 +00:00
|
|
|
.block(Block::default().borders(border::ALL).title("World"))
|
2016-11-07 23:35:46 +00:00
|
|
|
.paint(|ctx| {
|
2017-05-21 09:13:24 +00:00
|
|
|
ctx.draw(&Map {
|
|
|
|
color: Color::White,
|
|
|
|
resolution: MapResolution::High,
|
|
|
|
});
|
|
|
|
ctx.print(app.x, -app.y, "You are here", Color::Yellow);
|
|
|
|
})
|
2016-11-07 23:35:46 +00:00
|
|
|
.x_bounds([-180.0, 180.0])
|
|
|
|
.y_bounds([-90.0, 90.0])
|
|
|
|
.render(t, &chunks[0]);
|
|
|
|
Canvas::default()
|
2017-05-21 09:13:24 +00:00
|
|
|
.block(Block::default().borders(border::ALL).title("List"))
|
2016-11-07 23:35:46 +00:00
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&Line {
|
2017-05-21 09:13:24 +00:00
|
|
|
x1: app.ball.left() as f64,
|
|
|
|
y1: app.ball.top() as f64,
|
|
|
|
x2: app.ball.right() as f64,
|
|
|
|
y2: app.ball.top() as f64,
|
|
|
|
color: Color::Yellow,
|
|
|
|
});
|
2016-11-07 23:35:46 +00:00
|
|
|
ctx.draw(&Line {
|
2017-05-21 09:13:24 +00:00
|
|
|
x1: app.ball.right() as f64,
|
|
|
|
y1: app.ball.top() as f64,
|
|
|
|
x2: app.ball.right() as f64,
|
|
|
|
y2: app.ball.bottom() as f64,
|
|
|
|
color: Color::Yellow,
|
|
|
|
});
|
2016-11-07 23:35:46 +00:00
|
|
|
ctx.draw(&Line {
|
2017-05-21 09:13:24 +00:00
|
|
|
x1: app.ball.right() as f64,
|
|
|
|
y1: app.ball.bottom() as f64,
|
|
|
|
x2: app.ball.left() as f64,
|
|
|
|
y2: app.ball.bottom() as f64,
|
|
|
|
color: Color::Yellow,
|
|
|
|
});
|
2016-11-07 23:35:46 +00:00
|
|
|
ctx.draw(&Line {
|
2017-05-21 09:13:24 +00:00
|
|
|
x1: app.ball.left() as f64,
|
|
|
|
y1: app.ball.bottom() as f64,
|
|
|
|
x2: app.ball.left() as f64,
|
|
|
|
y2: app.ball.top() as f64,
|
|
|
|
color: Color::Yellow,
|
|
|
|
});
|
2016-11-07 23:35:46 +00:00
|
|
|
})
|
|
|
|
.x_bounds([10.0, 110.0])
|
|
|
|
.y_bounds([10.0, 110.0])
|
|
|
|
.render(t, &chunks[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
t.draw().unwrap();
|
|
|
|
}
|