Add new shape: Rectangle

pull/119/head
Sven-Hendrik Haase 5 years ago committed by Florian Dehau
parent d7e4a252fb
commit a78fa73b34

@ -1,7 +1,8 @@
# Changelog # Changelog
## v0.3.1 - To be released ## To be released
* Added a new shape: Rectangle
* Upgraded to Rust 2018 edition * Upgraded to Rust 2018 edition
## v0.3.0 - 2018-11-04 ## v0.3.0 - 2018-11-04

@ -11,7 +11,7 @@ use termion::screen::AlternateScreen;
use tui::backend::TermionBackend; use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout, Rect}; use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::Color; use tui::style::Color;
use tui::widgets::canvas::{Canvas, Line, Map, MapResolution}; use tui::widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle};
use tui::widgets::{Block, Borders, Widget}; use tui::widgets::{Block, Borders, Widget};
use tui::Terminal; use tui::Terminal;
@ -106,32 +106,8 @@ fn main() -> Result<(), failure::Error> {
Canvas::default() Canvas::default()
.block(Block::default().borders(Borders::ALL).title("Pong")) .block(Block::default().borders(Borders::ALL).title("Pong"))
.paint(|ctx| { .paint(|ctx| {
ctx.draw(&Line { ctx.draw(&Rectangle {
x1: f64::from(app.ball.left()), rect: app.ball,
y1: f64::from(app.ball.top()),
x2: f64::from(app.ball.right()),
y2: f64::from(app.ball.top()),
color: Color::Yellow,
});
ctx.draw(&Line {
x1: f64::from(app.ball.right()),
y1: f64::from(app.ball.top()),
x2: f64::from(app.ball.right()),
y2: f64::from(app.ball.bottom()),
color: Color::Yellow,
});
ctx.draw(&Line {
x1: f64::from(app.ball.right()),
y1: f64::from(app.ball.bottom()),
x2: f64::from(app.ball.left()),
y2: f64::from(app.ball.bottom()),
color: Color::Yellow,
});
ctx.draw(&Line {
x1: f64::from(app.ball.left()),
y1: f64::from(app.ball.bottom()),
x2: f64::from(app.ball.left()),
y2: f64::from(app.ball.top()),
color: Color::Yellow, color: Color::Yellow,
}); });
}) })

@ -10,7 +10,7 @@ use termion::screen::AlternateScreen;
use tui::backend::{Backend, TermionBackend}; use tui::backend::{Backend, TermionBackend};
use tui::layout::{Constraint, Direction, Layout, Rect}; use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Modifier, Style}; use tui::style::{Color, Modifier, Style};
use tui::widgets::canvas::{Canvas, Line, Map, MapResolution}; use tui::widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle};
use tui::widgets::{ use tui::widgets::{
Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Marker, Paragraph, Row, Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Marker, Paragraph, Row,
SelectableList, Sparkline, Table, Tabs, Text, Widget, SelectableList, Sparkline, Table, Tabs, Text, Widget,
@ -459,6 +459,15 @@ where
resolution: MapResolution::High, resolution: MapResolution::High,
}); });
ctx.layer(); ctx.layer();
ctx.draw(&Rectangle {
rect: Rect {
x: 0,
y: 30,
width: 10,
height: 10,
},
color: Color::Yellow,
});
for (i, s1) in app.servers.iter().enumerate() { for (i, s1) in app.servers.iter().enumerate() {
for s2 in &app.servers[i + 1..] { for s2 in &app.servers[i + 1..] {
ctx.draw(&Line { ctx.draw(&Line {

@ -23,6 +23,7 @@ pub struct LineIterator {
impl Iterator for LineIterator { impl Iterator for LineIterator {
type Item = (f64, f64); type Item = (f64, f64);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.current < self.end { if self.current < self.end {
let pos = ( let pos = (
@ -40,6 +41,7 @@ impl Iterator for LineIterator {
impl<'a> IntoIterator for &'a Line { impl<'a> IntoIterator for &'a Line {
type Item = (f64, f64); type Item = (f64, f64);
type IntoIter = LineIterator; type IntoIter = LineIterator;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
let dx = self.x1.max(self.x2) - self.x1.min(self.x2); let dx = self.x1.max(self.x2) - self.x1.min(self.x2);
let dy = self.y1.max(self.y2) - self.y1.min(self.y2); let dy = self.y1.max(self.y2) - self.y1.min(self.y2);
@ -63,6 +65,7 @@ impl<'a> Shape<'a> for Line {
fn color(&self) -> Color { fn color(&self) -> Color {
self.color self.color
} }
fn points(&'a self) -> Box<Iterator<Item = (f64, f64)> + 'a> { fn points(&'a self) -> Box<Iterator<Item = (f64, f64)> + 'a> {
Box::new(self.into_iter()) Box::new(self.into_iter())
} }

@ -1,11 +1,13 @@
mod line; mod line;
mod map; mod map;
mod points; mod points;
mod rectangle;
mod world; mod world;
pub use self::line::Line; pub use self::line::Line;
pub use self::map::{Map, MapResolution}; pub use self::map::{Map, MapResolution};
pub use self::points::Points; pub use self::points::Points;
pub use self::rectangle::Rectangle;
use crate::buffer::Buffer; use crate::buffer::Buffer;
use crate::layout::Rect; use crate::layout::Rect;
@ -133,7 +135,8 @@ impl<'a> Context<'a> {
/// ///
/// ``` /// ```
/// # use tui::widgets::{Block, Borders}; /// # use tui::widgets::{Block, Borders};
/// # use tui::widgets::canvas::{Canvas, Shape, Line, Map, MapResolution}; /// # use tui::layout::Rect;
/// # use tui::widgets::canvas::{Canvas, Shape, Line, Rectangle, Map, MapResolution};
/// # use tui::style::Color; /// # use tui::style::Color;
/// # fn main() { /// # fn main() {
/// Canvas::default() /// Canvas::default()
@ -141,23 +144,25 @@ impl<'a> Context<'a> {
/// .x_bounds([-180.0, 180.0]) /// .x_bounds([-180.0, 180.0])
/// .y_bounds([-90.0, 90.0]) /// .y_bounds([-90.0, 90.0])
/// .paint(|ctx| { /// .paint(|ctx| {
/// ctx.draw(&Map{ /// ctx.draw(&Map {
/// resolution: MapResolution::High, /// resolution: MapResolution::High,
/// color: Color::White /// color: Color::White
/// }); /// });
/// ctx.layer(); /// ctx.layer();
/// ctx.draw(&Line{ /// ctx.draw(&Line {
/// x1: 0.0, /// x1: 0.0,
/// y1: 10.0, /// y1: 10.0,
/// x2: 10.0, /// x2: 10.0,
/// y2: 10.0, /// y2: 10.0,
/// color: Color::White, /// color: Color::White,
/// }); /// });
/// ctx.draw(&Line{ /// ctx.draw(&Rectangle {
/// x1: 10.0, /// rect: Rect {
/// y1: 10.0, /// x: 10,
/// x2: 20.0, /// y: 20,
/// y2: 20.0, /// width: 10,
/// height: 10,
/// },
/// color: Color::Red /// color: Color::Red
/// }); /// });
/// }); /// });

@ -0,0 +1,54 @@
use crate::layout::Rect;
use crate::style::Color;
use crate::widgets::canvas::{Line, Shape};
use itertools::Itertools;
/// Shape to draw a rectangle from a `Rect` with the given color
pub struct Rectangle {
pub rect: Rect,
pub color: Color,
}
impl<'a> Shape<'a> for Rectangle {
fn color(&self) -> Color {
self.color
}
fn points(&'a self) -> Box<Iterator<Item = (f64, f64)> + 'a> {
let left_line = Line {
x1: f64::from(self.rect.x),
y1: f64::from(self.rect.y),
x2: f64::from(self.rect.x),
y2: f64::from(self.rect.y + self.rect.height),
color: self.color,
};
let top_line = Line {
x1: f64::from(self.rect.x),
y1: f64::from(self.rect.y + self.rect.height),
x2: f64::from(self.rect.x + self.rect.width),
y2: f64::from(self.rect.y + self.rect.height),
color: self.color,
};
let right_line = Line {
x1: f64::from(self.rect.x + self.rect.width),
y1: f64::from(self.rect.y),
x2: f64::from(self.rect.x + self.rect.width),
y2: f64::from(self.rect.y + self.rect.height),
color: self.color,
};
let bottom_line = Line {
x1: f64::from(self.rect.x),
y1: f64::from(self.rect.y),
x2: f64::from(self.rect.x + self.rect.width),
y2: f64::from(self.rect.y),
color: self.color,
};
Box::new(
left_line.into_iter().merge(
top_line
.into_iter()
.merge(right_line.into_iter().merge(bottom_line.into_iter())),
),
)
}
}
Loading…
Cancel
Save