From c407a7772e5354bbd13447f7e217e526d662d6cb Mon Sep 17 00:00:00 2001 From: Minijackson Date: Wed, 4 Jan 2023 09:35:17 +0100 Subject: core: re-add StraightPath core shape --- core/src/core_shapes.rs | 42 ++++++++++++++++++++++++------------------ core/src/rendering.rs | 37 +++++++++++++++++++------------------ core/src/solving.rs | 30 +++++++++++++++++------------- 3 files changed, 60 insertions(+), 49 deletions(-) diff --git a/core/src/core_shapes.rs b/core/src/core_shapes.rs index d8017ab..e6d79c9 100644 --- a/core/src/core_shapes.rs +++ b/core/src/core_shapes.rs @@ -1,3 +1,7 @@ +use crate::types::{CoreShapeContext, DefinedCoreShapeContext, DefinedPoint2D, Point2D}; + +pub use super::text::{DefinedText, Text}; + pub struct CoreDrawable { pub(crate) shape: CoreShape, pub(crate) context: CoreShapeContext, @@ -12,7 +16,7 @@ impl CoreDrawable { pub enum CoreShape { Rectangle(Rectangle), Text(Text), - // StraightPath(StraightPath), + StraightPath(StraightPath), } impl From for CoreShape { @@ -27,6 +31,12 @@ impl From for CoreShape { } } +impl From for CoreShape { + fn from(path: StraightPath) -> Self { + CoreShape::StraightPath(path) + } +} + pub struct DefinedCoreDrawable { pub(crate) shape: DefinedCoreShape, pub(crate) context: DefinedCoreShapeContext, @@ -35,7 +45,7 @@ pub struct DefinedCoreDrawable { pub enum DefinedCoreShape { Rectangle(Rectangle), Text(DefinedText), - // StraightPath(StraightPath), + StraightPath(DefinedStraightPath), } /* @@ -61,7 +71,6 @@ impl ComplexShape for T { } */ -// TODO: add default #[derive(Copy, Clone, Debug, Default)] pub struct Rectangle {} @@ -73,10 +82,7 @@ impl CoreShape for Rectangle { } */ -use crate::types::{CoreShapeContext, DefinedCoreShapeContext}; - -pub use super::text::{DefinedText, Text}; - +// TODO: re-enable this in some way /* impl CoreShape for Text { fn constrain( @@ -106,22 +112,22 @@ impl CoreShape for Text { } */ -// #[derive(Clone, Debug, Default)] -// pub struct StraightPath { -// pub(crate) points: Vec, -// } -// -// impl StraightPath { -// pub fn new(points: Vec) -> Self { -// Self { points } -// } -// } +#[derive(Clone, Debug, Default)] +pub struct StraightPath { + pub(crate) points: Vec, +} + +impl StraightPath { + pub fn new(points: Vec) -> Self { + Self { points } + } +} -/* pub struct DefinedStraightPath { pub(crate) points: Vec, } +/* impl CoreShape for StraightPath { fn to_render(&self, model: &dyn SolverModel) -> Option> { self.fixate(model) diff --git a/core/src/rendering.rs b/core/src/rendering.rs index 9c9f9e4..3f2303b 100644 --- a/core/src/rendering.rs +++ b/core/src/rendering.rs @@ -28,6 +28,7 @@ impl Render for DefinedCoreShape { match self { Self::Rectangle(r) => r.render(context, renderer), Self::Text(t) => t.render(context, renderer), + Self::StraightPath(p) => p.render(context, renderer), } } } @@ -75,21 +76,21 @@ impl Render for DefinedText { } } -// impl Render for DefinedStraightPath { -// fn render(&self, context: DefinedShapeContext, renderer: &mut dyn Renderer) { -// let mut iter = self.points.iter(); -// -// let first_point = match iter.next() { -// Some(point) => point, -// None => return, -// }; -// -// renderer.move_to(first_point.x, first_point.y); -// -// for point in iter { -// renderer.line_to(point.x, point.y); -// } -// -// draw(&context.fill, &context.stroke, renderer); -// } -// } +impl Render for DefinedStraightPath { + fn render(&self, context: DefinedCoreShapeContext, renderer: &mut dyn Renderer) { + let mut iter = self.points.iter(); + + let first_point = match iter.next() { + Some(point) => point, + None => return, + }; + + renderer.move_to(first_point.x, first_point.y); + + for point in iter { + renderer.line_to(point.x, point.y); + } + + draw(&context.fill, &context.stroke, renderer); + } +} diff --git a/core/src/solving.rs b/core/src/solving.rs index 261b4d4..bf5fe95 100644 --- a/core/src/solving.rs +++ b/core/src/solving.rs @@ -1,4 +1,7 @@ -use crate::core_shapes::{CoreDrawable, CoreShape, DefinedCoreDrawable, DefinedCoreShape}; +use crate::core_shapes::{ + CoreDrawable, CoreShape, DefinedCoreDrawable, DefinedCoreShape, DefinedStraightPath, + StraightPath, +}; use super::styles::*; use super::text::*; @@ -155,6 +158,7 @@ impl Constrainable for CoreShape { match self { CoreShape::Rectangle(r) => Some(DefinedCoreShape::Rectangle(*r)), CoreShape::Text(t) => t.fixate(model).map(DefinedCoreShape::Text), + CoreShape::StraightPath(p) => p.fixate(model).map(DefinedCoreShape::StraightPath), } } } @@ -288,15 +292,15 @@ impl Constrainable for Text { } } -// impl Constrainable for StraightPath { -// type Fixated = DefinedStraightPath; -// -// fn fixate(&self, model: &dyn SolverModel) -> Option { -// let points: Option<_> = self -// .points -// .iter() -// .map(|point| point.fixate(model)) -// .collect(); -// Some(DefinedStraightPath { points: points? }) -// } -// } +impl Constrainable for StraightPath { + type Fixated = DefinedStraightPath; + + fn fixate(&self, model: &dyn SolverModel) -> Option { + let points: Option<_> = self + .points + .iter() + .map(|point| point.fixate(model)) + .collect(); + Some(DefinedStraightPath { points: points? }) + } +} -- cgit v1.2.3