mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-29 05:51:57 +00:00
Add Map widget
This commit is contained in:
parent
286e345dbc
commit
c862fa9ce3
@ -22,7 +22,7 @@ use log4rs::config::{Appender, Config, Root};
|
||||
|
||||
use tui::Terminal;
|
||||
use tui::widgets::{Widget, Block, List, Gauge, Sparkline, Text, border, Chart, Axis, Dataset,
|
||||
BarChart, Marker, Tabs};
|
||||
BarChart, Marker, Tabs, Map};
|
||||
use tui::layout::{Group, Direction, Size, Rect};
|
||||
use tui::style::Color;
|
||||
|
||||
@ -285,7 +285,11 @@ fn draw(t: &mut Terminal, app: &App) {
|
||||
0 => {
|
||||
draw_main(t, app, &chunks[1]);
|
||||
}
|
||||
1 => {}
|
||||
1 => {
|
||||
Map::default()
|
||||
.block(Block::default().title("World").borders(border::ALL))
|
||||
.render(&chunks[1], t);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
});
|
||||
|
@ -83,12 +83,14 @@ impl<'a> Widget for Canvas<'a> {
|
||||
let width = canvas_area.width as usize;
|
||||
let height = canvas_area.height as usize;
|
||||
let mut grid: Vec<u16> = vec![BRAILLE_OFFSET; width * height + 1];
|
||||
let mut x_bounds = self.x_bounds.clone();
|
||||
x_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
let mut y_bounds = self.y_bounds.clone();
|
||||
y_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
for shape in self.shapes {
|
||||
for &(x, y) in shape.data.iter() {
|
||||
if x < self.x_bounds[0] || x > self.x_bounds[1] || y < self.y_bounds[0] ||
|
||||
y > self.y_bounds[1] {
|
||||
continue;
|
||||
}
|
||||
for &(x, y) in shape.data.iter().filter(|&&(x, y)| {
|
||||
!(x < x_bounds[0] || x > x_bounds[1] || y < y_bounds[0] || y > y_bounds[1])
|
||||
}) {
|
||||
let dy = ((self.y_bounds[1] - y) * canvas_area.height as f64 * 4.0 /
|
||||
(self.y_bounds[1] - self.y_bounds[0])) as usize;
|
||||
let dx = ((self.x_bounds[1] - x) * canvas_area.width as f64 * 2.0 /
|
||||
|
@ -313,6 +313,7 @@ impl<'a> Widget for Chart<'a> {
|
||||
(self.y_axis.bounds[1] - self.y_axis.bounds[0]);
|
||||
let dx = (self.x_axis.bounds[1] - x) * graph_area.width as f64 /
|
||||
(self.x_axis.bounds[1] - self.x_axis.bounds[0]);
|
||||
|
||||
buf.set_cell(dx as u16 + graph_area.left(),
|
||||
dy as u16 + graph_area.top(),
|
||||
symbols::BLACK_CIRCLE,
|
||||
|
@ -1,3 +1,41 @@
|
||||
use widgets::{Widget, Block, Canvas, Shape};
|
||||
use buffer::Buffer;
|
||||
use layout::Rect;
|
||||
|
||||
pub struct Map<'a> {
|
||||
block: Option<Block<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Default for Map<'a> {
|
||||
fn default() -> Map<'a> {
|
||||
Map { block: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Map<'a> {
|
||||
pub fn block(&mut self, block: Block<'a>) -> &mut Map<'a> {
|
||||
self.block = Some(block);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Widget for Map<'a> {
|
||||
fn buffer(&self, area: &Rect, buf: &mut Buffer) {
|
||||
let map_area = match self.block {
|
||||
Some(b) => {
|
||||
b.buffer(area, buf);
|
||||
b.inner(area)
|
||||
}
|
||||
None => *area,
|
||||
};
|
||||
|
||||
Canvas::default()
|
||||
.x_bounds([180.0, -180.0])
|
||||
.y_bounds([-90.0, 90.0])
|
||||
.shapes(&[Shape::default().data(&WORLD)])
|
||||
.buffer(&map_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
const WORLD: [(f64, f64); 1166] = [(-92.32, 48.24),
|
||||
(-88.13, 48.92),
|
||||
|
@ -18,6 +18,7 @@ pub use self::chart::{Chart, Axis, Dataset, Marker};
|
||||
pub use self::barchart::BarChart;
|
||||
pub use self::tabs::Tabs;
|
||||
pub use self::canvas::{Canvas, Shape};
|
||||
pub use self::map::Map;
|
||||
|
||||
use buffer::Buffer;
|
||||
use layout::Rect;
|
||||
|
Loading…
x
Reference in New Issue
Block a user