Another round of polishing (#101)

* Implement basic refactorings

* Simplify some match statements

* Rename `ix` to `index`

If we're not going with a single letter name,
then a full word makes more sense.

* Rename `as_hex` to `to_hex`

`as_` implies lossless reinterpretation
while the function does a conversion that loses information

* Replace `for_each` with for loops

for loops are a lot easier to read and maintain.

* factor out x and y coords in Line::render

this is arguably more ergonomic

* Remove redundant `format!(format_args!())`
This commit is contained in:
T0mstone
2021-05-04 15:08:24 +02:00
committed by Keavon Chambers
parent c9fea54ec5
commit 47458115b8
12 changed files with 65 additions and 73 deletions

View File

@@ -55,7 +55,7 @@ impl Color {
pub fn components(&self) -> (f32, f32, f32, f32) {
(self.red, self.green, self.blue, self.alpha)
}
pub fn as_hex(&self) -> String {
pub fn to_hex(&self) -> String {
format!(
"{:02X?}{:02X?}{:02X?}{:02X?}",
(self.r() * 255.) as u8,

View File

@@ -1,7 +1,5 @@
use layers::PolyLine;
use crate::{
layers::{self, Folder, Layer, LayerData, LayerDataTypes, Line, Rect, Shape},
layers::{self, Folder, Layer, LayerData, LayerDataTypes, Line, PolyLine, Rect, Shape},
response::{LayerPanelEntry, LayerType},
DocumentError, DocumentResponse, LayerId, Operation,
};

View File

@@ -13,11 +13,12 @@ pub struct Folder {
impl LayerData for Folder {
fn render(&mut self, svg: &mut String) {
self.layers.iter_mut().for_each(|layer| {
for layer in &mut self.layers {
let _ = writeln!(svg, "{}", layer.render());
});
}
}
}
impl Folder {
pub fn add_layer(&mut self, layer: Layer, insert_index: isize) -> Option<LayerId> {
let mut insert_index = insert_index as i128;

View File

@@ -20,14 +20,9 @@ impl Line {
impl LayerData for Line {
fn render(&mut self, svg: &mut String) {
let _ = write!(
svg,
r#"<line x1="{}" y1="{}" x2="{}" y2="{}" {} />"#,
self.shape.p0.x,
self.shape.p0.y,
self.shape.p1.x,
self.shape.p1.y,
self.style.render(),
);
let kurbo::Point { x: x1, y: y1 } = self.shape.p0;
let kurbo::Point { x: x2, y: y2 } = self.shape.p1;
let _ = write!(svg, r#"<line x1="{}" y1="{}" x2="{}" y2="{}" {} />"#, x1, y1, x2, y2, self.style.render(),);
}
}

View File

@@ -36,16 +36,26 @@ pub enum LayerDataTypes {
Shape(Shape),
}
macro_rules! call_render {
($self:ident.render($svg:ident) { $($variant:ident),* }) => {
match $self {
$(Self::$variant(x) => x.render($svg)),*
}
};
}
impl LayerDataTypes {
pub fn render(&mut self, svg: &mut String) {
match self {
Self::Folder(f) => f.render(svg),
Self::Circle(c) => c.render(svg),
Self::Ellipse(e) => e.render(svg),
Self::Rect(r) => r.render(svg),
Self::Line(l) => l.render(svg),
Self::PolyLine(pl) => pl.render(svg),
Self::Shape(s) => s.render(svg),
call_render! {
self.render(svg) {
Folder,
Circle,
Ellipse,
Rect,
Line,
PolyLine,
Shape
}
}
}
}

View File

@@ -24,13 +24,14 @@ impl LayerData for PolyLine {
return;
}
let _ = write!(svg, r#"<polyline points=""#);
self.points.iter().for_each(|p| {
for p in &self.points {
let _ = write!(svg, " {:.3} {:.3}", p.x, p.y);
});
}
let _ = write!(svg, r#"" {}/>"#, self.style.render());
}
}
#[cfg(test)]
#[test]
fn polyline_should_render() {
let mut polyline = PolyLine {

View File

@@ -15,8 +15,8 @@ impl Fill {
}
pub fn render(&self) -> String {
match self.color {
Some(c) => format!("fill: #{};", c.as_hex()),
None => format!("fill: none;"),
Some(c) => format!("fill: #{};", c.to_hex()),
None => "fill: none;".to_string(),
}
}
}
@@ -33,7 +33,7 @@ impl Stroke {
Self { color, width }
}
pub fn render(&self) -> String {
format!("stroke: #{};stroke-width:{};", self.color.as_hex(), self.width)
format!("stroke: #{};stroke-width:{};", self.color.to_hex(), self.width)
}
}

View File

@@ -62,7 +62,7 @@ impl std::fmt::Display for ShapePoints {
#[doc(hidden)]
pub struct ShapePathIter {
shape: ShapePoints,
ix: usize,
index: usize,
}
impl Iterator for ShapePathIter {
@@ -74,11 +74,11 @@ impl Iterator for ShapePathIter {
let sine = theta.sin();
Vec2::new(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine)
}
self.ix += 1;
match self.ix {
self.index += 1;
match self.index {
1 => Some(PathEl::MoveTo(self.shape.center + self.shape.extent)),
_ => {
let radians = self.shape.apothem_offset_angle() * ((self.ix * 2 + (self.shape.sides % 2) as usize) as f64);
let radians = self.shape.apothem_offset_angle() * ((self.index * 2 + (self.shape.sides % 2) as usize) as f64);
let offset = rotate(&self.shape.extent, radians);
let point = self.shape.center + offset;
Some(PathEl::LineTo(point))