2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Canvas example
|
|
|
|
//!
|
|
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
|
|
//!
|
|
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
|
|
//! crates.io, or the one that you have installed locally.
|
|
|
|
//!
|
|
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
|
|
//! library you are using.
|
|
|
|
//!
|
2024-08-21 18:35:08 +00:00
|
|
|
//! [Ratatui]: https://github.com/ratatui/ratatui
|
|
|
|
//! [examples]: https://github.com/ratatui/ratatui/blob/main/examples
|
|
|
|
//! [examples readme]: https://github.com/ratatui/ratatui/blob/main/examples/README.md
|
2024-01-24 19:50:18 +00:00
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2023-06-12 05:07:15 +00:00
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
use color_eyre::Result;
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
crossterm::event::{self, Event, KeyCode},
|
2024-05-29 11:42:29 +00:00
|
|
|
layout::{Constraint, Layout, Rect},
|
|
|
|
style::{Color, Stylize},
|
|
|
|
symbols::Marker,
|
|
|
|
widgets::{
|
|
|
|
canvas::{Canvas, Circle, Map, MapResolution, Rectangle},
|
|
|
|
Block, Widget,
|
|
|
|
},
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
DefaultTerminal, Frame,
|
2023-07-16 09:11:59 +00:00
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
color_eyre::install()?;
|
|
|
|
let terminal = ratatui::init();
|
|
|
|
let app_result = App::new().run(terminal);
|
|
|
|
ratatui::restore();
|
|
|
|
app_result
|
2023-09-30 13:03:03 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-10-14 22:04:56 +00:00
|
|
|
const fn new() -> Self {
|
2024-03-02 09:06:53 +00:00
|
|
|
Self {
|
2016-11-07 23:35:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
2023-09-30 13:03:03 +00:00
|
|
|
let tick_rate = Duration::from_millis(16);
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
let mut last_tick = Instant::now();
|
2023-09-30 13:03:03 +00:00
|
|
|
loop {
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
terminal.draw(|frame| self.draw(frame))?;
|
2023-09-30 13:03:03 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
|
|
|
if event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
KeyCode::Char('q') => break Ok(()),
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => self.y += 1.0,
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => self.y -= 1.0,
|
|
|
|
KeyCode::Right | KeyCode::Char('l') => self.x += 1.0,
|
|
|
|
KeyCode::Left | KeyCode::Char('h') => self.x -= 1.0,
|
2023-09-30 13:03:03 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
self.on_tick();
|
2023-09-30 13:03:03 +00:00
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2024-03-02 09:06:53 +00:00
|
|
|
if ball.x - ball.radius < f64::from(playground.left())
|
|
|
|
|| ball.x + ball.radius > f64::from(playground.right())
|
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
|
|
|
}
|
2024-03-02 09:06:53 +00:00
|
|
|
if ball.y - ball.radius < f64::from(playground.top())
|
|
|
|
|| ball.y + ball.radius > f64::from(playground.bottom())
|
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
|
|
|
}
|
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
These are simple opinionated methods for creating a terminal that is
useful to use in most apps. The new init method creates a crossterm
backend writing to stdout, enables raw mode, enters the alternate
screen, and sets a panic handler that restores the terminal on panic.
A minimal hello world now looks a bit like:
```rust
use ratatui::{
crossterm::event::{self, Event},
text::Text,
Frame,
};
fn main() {
let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))
.expect("Failed to draw");
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
break;
}
}
ratatui::restore();
}
```
A type alias `DefaultTerminal` is added to represent this terminal
type and to simplify any cases where applications need to pass this
terminal around. It is equivalent to:
`Terminal<CrosstermBackend<Stdout>>`
We also added `ratatui::try_init()` and `try_restore()`, for situations
where you might want to handle initialization errors yourself instead
of letting the panic handler fire and cleanup. Simple Apps should
prefer the `init` and `restore` functions over these functions.
Corresponding functions to allow passing a `TerminalOptions` with
a `Viewport` (e.g. inline, fixed) are also available
(`init_with_options`,
and `try_init_with_options`).
The existing code to create a backend and terminal will remain and
is not deprecated by this approach. This just provides a simple one
line initialization using the common options.
---------
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
2024-08-22 12:16:35 +00:00
|
|
|
fn draw(&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)]);
|
2024-08-06 03:15:14 +00:00
|
|
|
let [map, right] = horizontal.areas(frame.area());
|
2024-02-02 04:26:35 +00:00
|
|
|
let [pong, boxes] = vertical.areas(right);
|
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()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("World"))
|
2023-09-30 13:03:03 +00:00
|
|
|
.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()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Pong"))
|
2023-09-30 13:03:03 +00:00
|
|
|
.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 {
|
2024-03-02 09:06:53 +00:00
|
|
|
let left = 0.0;
|
|
|
|
let right = f64::from(area.width);
|
|
|
|
let bottom = 0.0;
|
|
|
|
let top = f64::from(area.height).mul_add(2.0, -4.0);
|
2023-09-30 13:03:03 +00:00
|
|
|
Canvas::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Rects"))
|
2023-09-30 13:03:03 +00:00
|
|
|
.marker(self.marker)
|
|
|
|
.x_bounds([left, right])
|
|
|
|
.y_bounds([bottom, top])
|
|
|
|
.paint(|ctx| {
|
|
|
|
for i in 0..=11 {
|
|
|
|
ctx.draw(&Rectangle {
|
2024-03-02 09:06:53 +00:00
|
|
|
x: f64::from(i * i + 3 * i) / 2.0 + 2.0,
|
2023-09-30 13:03:03 +00:00
|
|
|
y: 2.0,
|
2024-03-02 09:06:53 +00:00
|
|
|
width: f64::from(i),
|
|
|
|
height: f64::from(i),
|
2023-09-30 13:03:03 +00:00
|
|
|
color: Color::Red,
|
|
|
|
});
|
|
|
|
ctx.draw(&Rectangle {
|
2024-03-02 09:06:53 +00:00
|
|
|
x: f64::from(i * i + 3 * i) / 2.0 + 2.0,
|
2023-09-30 13:03:03 +00:00
|
|
|
y: 21.0,
|
2024-03-02 09:06:53 +00:00
|
|
|
width: f64::from(i),
|
|
|
|
height: f64::from(i),
|
2023-09-30 13:03:03 +00:00
|
|
|
color: Color::Blue,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for i in 0..100 {
|
|
|
|
if i % 10 != 0 {
|
2024-03-02 09:06:53 +00:00
|
|
|
ctx.print(f64::from(i) + 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 {
|
2024-03-02 09:06:53 +00:00
|
|
|
ctx.print(0.0, f64::from(i), 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
|
|
|
}
|