2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Config, Event, Events};
|
2020-03-13 01:02:51 +00:00
|
|
|
use std::{error::Error, io, time::Duration};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Constraint, Direction, Layout, Rect},
|
|
|
|
style::Color,
|
|
|
|
widgets::{
|
|
|
|
canvas::{Canvas, Map, MapResolution, Rectangle},
|
|
|
|
Block, Borders,
|
|
|
|
},
|
|
|
|
Terminal,
|
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App {
|
|
|
|
x: f64,
|
|
|
|
y: f64,
|
2020-03-03 08:27:28 +00:00
|
|
|
ball: Rectangle,
|
2016-11-07 23:35:46 +00:00
|
|
|
playground: Rect,
|
2020-03-03 08:27:28 +00:00
|
|
|
vx: f64,
|
|
|
|
vy: f64,
|
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,
|
2020-03-03 08:27:28 +00:00
|
|
|
ball: Rectangle {
|
|
|
|
x: 10.0,
|
|
|
|
y: 30.0,
|
|
|
|
width: 10.0,
|
|
|
|
height: 10.0,
|
|
|
|
color: Color::Yellow,
|
|
|
|
},
|
2016-11-07 23:35:46 +00:00
|
|
|
playground: Rect::new(10, 10, 100, 100),
|
2020-03-03 08:27:28 +00:00
|
|
|
vx: 1.0,
|
|
|
|
vy: 1.0,
|
2017-05-08 19:34:41 +00:00
|
|
|
dir_x: true,
|
|
|
|
dir_y: true,
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn update(&mut self) {
|
2020-03-03 08:27:28 +00:00
|
|
|
if self.ball.x < self.playground.left() as f64
|
|
|
|
|| self.ball.x + self.ball.width > self.playground.right() as f64
|
2017-09-11 05:58:37 +00:00
|
|
|
{
|
2017-05-21 09:13:24 +00:00
|
|
|
self.dir_x = !self.dir_x;
|
|
|
|
}
|
2020-03-03 08:27:28 +00:00
|
|
|
if self.ball.y < self.playground.top() as f64
|
|
|
|
|| self.ball.y + self.ball.height > self.playground.bottom() as f64
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2016-11-07 23:35:46 +00:00
|
|
|
// Terminal initialization
|
2018-09-23 18:59:51 +00:00
|
|
|
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 {
|
2020-03-03 08:27:28 +00:00
|
|
|
tick_rate: Duration::from_millis(250),
|
2018-09-23 18:59:51 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let events = Events::with_config(config);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
loop {
|
2018-09-23 18:59:51 +00:00
|
|
|
terminal.draw(|mut f| {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2019-12-15 20:38:18 +00:00
|
|
|
let canvas = Canvas::default()
|
2018-09-23 18:59:51 +00:00
|
|
|
.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);
|
2018-12-07 15:17:33 +00:00
|
|
|
})
|
|
|
|
.x_bounds([-180.0, 180.0])
|
2019-12-15 20:38:18 +00:00
|
|
|
.y_bounds([-90.0, 90.0]);
|
|
|
|
f.render_widget(canvas, chunks[0]);
|
|
|
|
let canvas = Canvas::default()
|
2018-09-23 18:59:51 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).title("Pong"))
|
|
|
|
.paint(|ctx| {
|
2020-03-03 08:27:28 +00:00
|
|
|
ctx.draw(&app.ball);
|
2018-12-07 15:17:33 +00:00
|
|
|
})
|
|
|
|
.x_bounds([10.0, 110.0])
|
2019-12-15 20:38:18 +00:00
|
|
|
.y_bounds([10.0, 110.0]);
|
|
|
|
f.render_widget(canvas, chunks[1]);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
2017-10-30 21:28:37 +00:00
|
|
|
Event::Input(input) => match input {
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Char('q') => {
|
2017-10-30 21:28:37 +00:00
|
|
|
break;
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Down => {
|
2017-10-30 21:28:37 +00:00
|
|
|
app.y += 1.0;
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Up => {
|
2017-10-30 21:28:37 +00:00
|
|
|
app.y -= 1.0;
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Right => {
|
2017-10-30 21:28:37 +00:00
|
|
|
app.x += 1.0;
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Left => {
|
2017-10-30 21:28:37 +00:00
|
|
|
app.x -= 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
},
|
2016-11-07 23:35:46 +00:00
|
|
|
Event::Tick => {
|
2018-09-23 18:59:51 +00:00
|
|
|
app.update();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|