From c862fa9ce384f362521f0455be540cdafd3af216 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Thu, 27 Oct 2016 22:55:24 +0200 Subject: [PATCH] Add Map widget --- examples/prototype.rs | 8 ++++++-- src/widgets/canvas.rs | 12 +++++++----- src/widgets/chart.rs | 1 + src/widgets/map.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/widgets/mod.rs | 1 + 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/examples/prototype.rs b/examples/prototype.rs index 13e2a94..a083a1a 100644 --- a/examples/prototype.rs +++ b/examples/prototype.rs @@ -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); + } _ => {} }; }); diff --git a/src/widgets/canvas.rs b/src/widgets/canvas.rs index 2ed19d4..9f927cb 100644 --- a/src/widgets/canvas.rs +++ b/src/widgets/canvas.rs @@ -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 = 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 / diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index 547500c..f34749f 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -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, diff --git a/src/widgets/map.rs b/src/widgets/map.rs index 490fd25..07f49a0 100644 --- a/src/widgets/map.rs +++ b/src/widgets/map.rs @@ -1,3 +1,41 @@ +use widgets::{Widget, Block, Canvas, Shape}; +use buffer::Buffer; +use layout::Rect; + +pub struct Map<'a> { + block: Option>, +} + +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), diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 613730c..3691644 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -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;