summaryrefslogtreecommitdiffstats
path: root/core/src/core_shapes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/core_shapes.rs')
-rw-r--r--core/src/core_shapes.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/core/src/core_shapes.rs b/core/src/core_shapes.rs
new file mode 100644
index 0000000..c3d3cae
--- /dev/null
+++ b/core/src/core_shapes.rs
@@ -0,0 +1,86 @@
1use super::complex_shapes::{ComplexShape, DrawResult};
2use super::rendering::{Render, Renderer};
3use super::solving::{Constrainable, SolverContext, SolverModel};
4use super::types::*;
5
6pub trait CoreShape {
7 fn constrain(
8 &self,
9 _context: &ShapeContext,
10 _solver: &mut dyn SolverContext,
11 _renderer: &dyn Renderer,
12 ) {
13 }
14 fn to_render(&self, model: &dyn SolverModel) -> Option<Box<dyn Render>>;
15}
16
17impl<T: CoreShape + Clone + 'static> ComplexShape for T {
18 fn as_core_shape(&self) -> Option<&dyn CoreShape> {
19 Some(self)
20 }
21
22 fn draw(&self, _context: &ShapeContext, _solver: &mut dyn SolverContext) -> DrawResult {
23 panic!("Tried to decompose core shape")
24 }
25}
26
27// TODO: add default
28#[derive(Copy, Clone, Debug, Default)]
29pub struct Rectangle {}
30
31impl CoreShape for Rectangle {
32 fn to_render(&self, _model: &dyn SolverModel) -> Option<Box<dyn Render>> {
33 Some(Box::new(self.clone()))
34 }
35}
36
37pub use super::text::{DefinedText, Text};
38
39impl CoreShape for Text {
40 fn constrain(
41 &self,
42 context: &ShapeContext,
43 solver: &mut dyn SolverContext,
44 renderer: &dyn Renderer,
45 ) {
46 let height_constraint = solver.float_eq(context.bounds.height, self.font.size);
47 solver.constrain(height_constraint);
48
49 // TODO: handle multiline
50 let (width, height) = renderer.text_extents(&self.content, &self.font);
51 dbg!(height);
52
53 let scale = solver.float_div(self.font.size, Float::Fixed(height));
54
55 let calculated_width = solver.float_mul(&[Float::Fixed(width), scale]);
56 let width_constraint = solver.float_eq(context.bounds.width, calculated_width);
57 solver.constrain(width_constraint);
58 }
59
60 fn to_render(&self, model: &dyn SolverModel) -> Option<Box<dyn Render>> {
61 self.fixate(model)
62 .map(|path| -> Box<dyn Render> { Box::new(path) })
63 }
64}
65
66#[derive(Clone, Debug, Default)]
67pub struct StraightPath {
68 pub(crate) points: Vec<Point2D>,
69}
70
71impl StraightPath {
72 pub fn new(points: Vec<Point2D>) -> Self {
73 Self { points }
74 }
75}
76
77pub struct DefinedStraightPath {
78 pub(crate) points: Vec<DefinedPoint2D>,
79}
80
81impl CoreShape for StraightPath {
82 fn to_render(&self, model: &dyn SolverModel) -> Option<Box<dyn Render>> {
83 self.fixate(model)
84 .map(|path| -> Box<dyn Render> { Box::new(path) })
85 }
86}