2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{
|
|
|
|
prelude::*,
|
|
|
|
widgets::{canvas::*, *},
|
|
|
|
};
|
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,
|
2023-04-15 15:40:28 +00:00
|
|
|
tick_count: u64,
|
|
|
|
marker: Marker,
|
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,
|
2023-04-15 15:40:28 +00:00
|
|
|
tick_count: 0,
|
|
|
|
marker: Marker::Dot,
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn on_tick(&mut self) {
|
2023-04-15 15:40:28 +00:00
|
|
|
self.tick_count += 1;
|
|
|
|
// only change marker every 4 ticks (1s) to avoid stroboscopic effect
|
|
|
|
if (self.tick_count % 4) == 0 {
|
|
|
|
self.marker = match self.marker {
|
|
|
|
Marker::Dot => Marker::Block,
|
|
|
|
Marker::Block => Marker::Bar,
|
|
|
|
Marker::Bar => Marker::Braille,
|
|
|
|
Marker::Braille => Marker::Dot,
|
|
|
|
};
|
|
|
|
}
|
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>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let tick_rate = Duration::from_millis(250);
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
2016-11-07 23:35:46 +00:00
|
|
|
loop {
|
2021-11-01 21:27:46 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
let timeout = tick_rate
|
|
|
|
.checked_sub(last_tick.elapsed())
|
|
|
|
.unwrap_or_else(|| Duration::from_secs(0));
|
|
|
|
if event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
KeyCode::Down => {
|
|
|
|
app.y += 1.0;
|
|
|
|
}
|
|
|
|
KeyCode::Up => {
|
|
|
|
app.y -= 1.0;
|
|
|
|
}
|
|
|
|
KeyCode::Right => {
|
|
|
|
app.x += 1.0;
|
|
|
|
}
|
|
|
|
KeyCode::Left => {
|
|
|
|
app.x -= 1.0;
|
|
|
|
}
|
|
|
|
_ => {}
|
2017-10-30 21:28:37 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
2021-11-01 21:27:46 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(f.size());
|
|
|
|
let canvas = Canvas::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("World"))
|
2023-04-15 15:40:28 +00:00
|
|
|
.marker(app.marker)
|
2021-11-01 21:27:46 +00:00
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&Map {
|
|
|
|
color: Color::White,
|
|
|
|
resolution: MapResolution::High,
|
|
|
|
});
|
2023-07-01 09:14:16 +00:00
|
|
|
ctx.print(app.x, -app.y, "You are here".yellow());
|
2021-11-01 21:27:46 +00:00
|
|
|
})
|
|
|
|
.x_bounds([-180.0, 180.0])
|
|
|
|
.y_bounds([-90.0, 90.0]);
|
|
|
|
f.render_widget(canvas, chunks[0]);
|
|
|
|
let canvas = Canvas::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Pong"))
|
2023-04-15 15:40:28 +00:00
|
|
|
.marker(app.marker)
|
2021-11-01 21:27:46 +00:00
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&app.ball);
|
|
|
|
})
|
|
|
|
.x_bounds([10.0, 110.0])
|
|
|
|
.y_bounds([10.0, 110.0]);
|
|
|
|
f.render_widget(canvas, chunks[1]);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|