2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
2023-09-30 13:03:03 +00:00
|
|
|
io::{self, stdout, Stdout},
|
2023-06-12 05:07:15 +00:00
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
2023-09-30 13:03:03 +00:00
|
|
|
event::{self, Event, KeyCode},
|
2021-11-01 21:27:46 +00:00
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
2023-09-30 13:03:03 +00:00
|
|
|
ExecutableCommand,
|
2021-11-01 21:27:46 +00:00
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{
|
|
|
|
prelude::*,
|
|
|
|
widgets::{canvas::*, *},
|
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2023-09-30 13:03:03 +00:00
|
|
|
fn main() -> io::Result<()> {
|
|
|
|
App::run()
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App {
|
|
|
|
x: f64,
|
|
|
|
y: f64,
|
2023-09-30 13:03:03 +00:00
|
|
|
ball: Circle,
|
2016-11-07 23:35:46 +00:00
|
|
|
playground: Rect,
|
2020-03-03 08:27:28 +00:00
|
|
|
vx: f64,
|
|
|
|
vy: f64,
|
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,
|
2023-09-30 13:03:03 +00:00
|
|
|
ball: Circle {
|
|
|
|
x: 20.0,
|
|
|
|
y: 40.0,
|
|
|
|
radius: 10.0,
|
2020-03-03 08:27:28 +00:00
|
|
|
color: Color::Yellow,
|
|
|
|
},
|
2023-09-30 13:03:03 +00:00
|
|
|
playground: Rect::new(10, 10, 200, 100),
|
2020-03-03 08:27:28 +00:00
|
|
|
vx: 1.0,
|
|
|
|
vy: 1.0,
|
2023-04-15 15:40:28 +00:00
|
|
|
tick_count: 0,
|
|
|
|
marker: Marker::Dot,
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 13:03:03 +00:00
|
|
|
pub fn run() -> io::Result<()> {
|
|
|
|
let mut terminal = init_terminal()?;
|
|
|
|
let mut app = App::new();
|
|
|
|
let mut last_tick = Instant::now();
|
|
|
|
let tick_rate = Duration::from_millis(16);
|
|
|
|
loop {
|
|
|
|
let _ = terminal.draw(|frame| app.ui(frame));
|
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
|
|
|
if event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => break,
|
2023-12-15 03:11:48 +00:00
|
|
|
KeyCode::Down | KeyCode::Char('j') => app.y += 1.0,
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => app.y -= 1.0,
|
|
|
|
KeyCode::Right | KeyCode::Char('l') => app.x += 1.0,
|
|
|
|
KeyCode::Left | KeyCode::Char('h') => app.x -= 1.0,
|
2023-09-30 13:03:03 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
restore_terminal()
|
|
|
|
}
|
|
|
|
|
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;
|
2023-09-30 13:03:03 +00:00
|
|
|
// only change marker every 180 ticks (3s) to avoid stroboscopic effect
|
|
|
|
if (self.tick_count % 180) == 0 {
|
2023-04-15 15:40:28 +00:00
|
|
|
self.marker = match self.marker {
|
2023-09-30 13:03:03 +00:00
|
|
|
Marker::Dot => Marker::Braille,
|
|
|
|
Marker::Braille => Marker::Block,
|
|
|
|
Marker::Block => Marker::HalfBlock,
|
|
|
|
Marker::HalfBlock => Marker::Bar,
|
|
|
|
Marker::Bar => Marker::Dot,
|
2023-04-15 15:40:28 +00:00
|
|
|
};
|
|
|
|
}
|
2023-09-30 13:03:03 +00:00
|
|
|
// bounce the ball by flipping the velocity vector
|
|
|
|
let ball = &self.ball;
|
|
|
|
let playground = self.playground;
|
|
|
|
if ball.x - ball.radius < playground.left() as f64
|
|
|
|
|| ball.x + ball.radius > playground.right() as f64
|
2017-09-11 05:58:37 +00:00
|
|
|
{
|
2023-09-30 13:03:03 +00:00
|
|
|
self.vx = -self.vx;
|
2017-05-21 09:13:24 +00:00
|
|
|
}
|
2023-09-30 13:03:03 +00:00
|
|
|
if ball.y - ball.radius < playground.top() as f64
|
|
|
|
|| ball.y + ball.radius > playground.bottom() as f64
|
2017-09-11 05:58:37 +00:00
|
|
|
{
|
2023-09-30 13:03:03 +00:00
|
|
|
self.vy = -self.vy;
|
2017-05-08 19:34:41 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 13:03:03 +00:00
|
|
|
self.ball.x += self.vx;
|
|
|
|
self.ball.y += self.vy;
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 13:03:03 +00:00
|
|
|
fn ui(&self, frame: &mut Frame) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let horizontal =
|
|
|
|
Layout::horizontal([Constraint::Percentage(50), Constraint::Percentage(50)]);
|
|
|
|
let vertical = Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)]);
|
|
|
|
let [map, right] = frame.size().split(&horizontal);
|
|
|
|
let [pong, boxes] = right.split(&vertical);
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
frame.render_widget(self.map_canvas(), map);
|
|
|
|
frame.render_widget(self.pong_canvas(), pong);
|
|
|
|
frame.render_widget(self.boxes_canvas(boxes), boxes);
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2023-09-30 13:03:03 +00:00
|
|
|
fn map_canvas(&self) -> impl Widget + '_ {
|
|
|
|
Canvas::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("World"))
|
|
|
|
.marker(self.marker)
|
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&Map {
|
|
|
|
color: Color::Green,
|
|
|
|
resolution: MapResolution::High,
|
|
|
|
});
|
|
|
|
ctx.print(self.x, -self.y, "You are here".yellow());
|
|
|
|
})
|
|
|
|
.x_bounds([-180.0, 180.0])
|
|
|
|
.y_bounds([-90.0, 90.0])
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2023-09-30 13:03:03 +00:00
|
|
|
fn pong_canvas(&self) -> impl Widget + '_ {
|
|
|
|
Canvas::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Pong"))
|
|
|
|
.marker(self.marker)
|
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&self.ball);
|
|
|
|
})
|
|
|
|
.x_bounds([10.0, 210.0])
|
|
|
|
.y_bounds([10.0, 110.0])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn boxes_canvas(&self, area: Rect) -> impl Widget {
|
|
|
|
let (left, right, bottom, top) =
|
|
|
|
(0.0, area.width as f64, 0.0, area.height as f64 * 2.0 - 4.0);
|
|
|
|
Canvas::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Rects"))
|
|
|
|
.marker(self.marker)
|
|
|
|
.x_bounds([left, right])
|
|
|
|
.y_bounds([bottom, top])
|
|
|
|
.paint(|ctx| {
|
|
|
|
for i in 0..=11 {
|
|
|
|
ctx.draw(&Rectangle {
|
|
|
|
x: (i * i + 3 * i) as f64 / 2.0 + 2.0,
|
|
|
|
y: 2.0,
|
|
|
|
width: i as f64,
|
|
|
|
height: i as f64,
|
|
|
|
color: Color::Red,
|
|
|
|
});
|
|
|
|
ctx.draw(&Rectangle {
|
|
|
|
x: (i * i + 3 * i) as f64 / 2.0 + 2.0,
|
|
|
|
y: 21.0,
|
|
|
|
width: i as f64,
|
|
|
|
height: i as f64,
|
|
|
|
color: Color::Blue,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for i in 0..100 {
|
|
|
|
if i % 10 != 0 {
|
|
|
|
ctx.print(i as f64 + 1.0, 0.0, format!("{i}", i = i % 10));
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2023-09-30 13:03:03 +00:00
|
|
|
if i % 2 == 0 && i % 10 != 0 {
|
|
|
|
ctx.print(0.0, i as f64, format!("{i}", i = i % 10));
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2017-10-30 21:28:37 +00:00
|
|
|
}
|
2023-09-30 13:03:03 +00:00
|
|
|
})
|
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-30 13:03:03 +00:00
|
|
|
fn init_terminal() -> io::Result<Terminal<CrosstermBackend<Stdout>>> {
|
|
|
|
enable_raw_mode()?;
|
|
|
|
stdout().execute(EnterAlternateScreen)?;
|
|
|
|
Terminal::new(CrosstermBackend::new(stdout()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_terminal() -> io::Result<()> {
|
|
|
|
disable_raw_mode()?;
|
|
|
|
stdout().execute(LeaveAlternateScreen)?;
|
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|