mirror of https://github.com/fdehau/tui-rs
refactor(canvas): update shape drawing strategy
* Update the `Shape` trait. Instead of returning an iterator of point, all shapes are now aware of the surface they will be drawn to through a `Painter`. In order to draw themselves, they paint points of the "braille grid". * Rewrite how lines are drawn using a common line drawing algorithm (Bresenham).pull/242/head
parent
a6b35031ae
commit
140db9b2e2
@ -1,54 +1,52 @@
|
|||||||
use crate::layout::Rect;
|
use crate::{
|
||||||
use crate::style::Color;
|
style::Color,
|
||||||
use crate::widgets::canvas::{Line, Shape};
|
widgets::canvas::{Line, Painter, Shape},
|
||||||
use itertools::Itertools;
|
};
|
||||||
|
|
||||||
/// Shape to draw a rectangle from a `Rect` with the given color
|
/// Shape to draw a rectangle from a `Rect` with the given color
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct Rectangle {
|
pub struct Rectangle {
|
||||||
pub rect: Rect,
|
pub x: f64,
|
||||||
|
pub y: f64,
|
||||||
|
pub width: f64,
|
||||||
|
pub height: f64,
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Shape<'a> for Rectangle {
|
impl Shape for Rectangle {
|
||||||
fn color(&self) -> Color {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
self.color
|
let lines: [Line; 4] = [
|
||||||
}
|
Line {
|
||||||
|
x1: self.x,
|
||||||
fn points(&'a self) -> Box<dyn Iterator<Item = (f64, f64)> + 'a> {
|
y1: self.y,
|
||||||
let left_line = Line {
|
x2: self.x,
|
||||||
x1: f64::from(self.rect.x),
|
y2: self.y + self.height,
|
||||||
y1: f64::from(self.rect.y),
|
color: self.color,
|
||||||
x2: f64::from(self.rect.x),
|
},
|
||||||
y2: f64::from(self.rect.y + self.rect.height),
|
Line {
|
||||||
color: self.color,
|
x1: self.x,
|
||||||
};
|
y1: self.y + self.height,
|
||||||
let top_line = Line {
|
x2: self.x + self.width,
|
||||||
x1: f64::from(self.rect.x),
|
y2: self.y + self.height,
|
||||||
y1: f64::from(self.rect.y + self.rect.height),
|
color: self.color,
|
||||||
x2: f64::from(self.rect.x + self.rect.width),
|
},
|
||||||
y2: f64::from(self.rect.y + self.rect.height),
|
Line {
|
||||||
color: self.color,
|
x1: self.x + self.width,
|
||||||
};
|
y1: self.y,
|
||||||
let right_line = Line {
|
x2: self.x + self.width,
|
||||||
x1: f64::from(self.rect.x + self.rect.width),
|
y2: self.y + self.height,
|
||||||
y1: f64::from(self.rect.y),
|
color: self.color,
|
||||||
x2: f64::from(self.rect.x + self.rect.width),
|
},
|
||||||
y2: f64::from(self.rect.y + self.rect.height),
|
Line {
|
||||||
color: self.color,
|
x1: self.x,
|
||||||
};
|
y1: self.y,
|
||||||
let bottom_line = Line {
|
x2: self.x + self.width,
|
||||||
x1: f64::from(self.rect.x),
|
y2: self.y,
|
||||||
y1: f64::from(self.rect.y),
|
color: self.color,
|
||||||
x2: f64::from(self.rect.x + self.rect.width),
|
},
|
||||||
y2: f64::from(self.rect.y),
|
];
|
||||||
color: self.color,
|
for line in &lines {
|
||||||
};
|
line.draw(painter);
|
||||||
Box::new(
|
}
|
||||||
left_line.into_iter().merge(
|
|
||||||
top_line
|
|
||||||
.into_iter()
|
|
||||||
.merge(right_line.into_iter().merge(bottom_line.into_iter())),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue