summaryrefslogtreecommitdiffstats
path: root/core/src/core_shapes.rs
blob: c38087041815275e5b9a3be39692e071548a648f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::path::PathBuf;

use crate::{
    text::{DefinedText, Text},
    types::{CoreShapeContext, DefinedCoreShapeContext, DefinedPoint2D, Float, Point2D},
    Renderer, SolverContext,
};

#[derive(Debug, Clone)]
pub struct CoreDrawable {
    pub(crate) shape: CoreShape,
    pub(crate) context: CoreShapeContext,
}

impl CoreDrawable {
    pub fn new(shape: CoreShape, context: CoreShapeContext) -> Self {
        Self { shape, context }
    }

    // TODO: I don't like this design
    // TODO: plus, having an enum for CoreShape could be bad since types have
    // various sizes
    pub(crate) fn inherent_constraints(
        &self,
        solver: &mut dyn SolverContext,
        renderer: &mut dyn Renderer,
    ) {
        match &self.shape {
            CoreShape::Text(text) => {
                let (text_width, text_height) = renderer.text_extents(&text.content, &text.font);

                let calculated_width =
                    solver.float_mul(&[Float::Fixed(text_width), text.font.size]);
                let bounds_width = self.context.bounds().width(solver);
                let width_constraint = solver.float_eq(bounds_width, calculated_width);
                solver.constrain(width_constraint);

                let calculated_height =
                    solver.float_mul(&[Float::Fixed(text_height), text.font.size]);
                let bounds_height = self.context.bounds().height(solver);
                let height_constraint = solver.float_eq(bounds_height, calculated_height);
                solver.constrain(height_constraint);
            }
            CoreShape::StraightPath(path) => {
                let (all_x, all_y): (Vec<_>, Vec<_>) =
                    path.points.iter().map(|p| (p.x, p.y)).unzip();

                let min_x = solver.float_min(&all_x);
                let max_x = solver.float_max(&all_x);
                let min_y = solver.float_min(&all_y);
                let max_y = solver.float_max(&all_y);

                let bounds_top = self.context.bounds().top(solver);
                let bounds_bottom = self.context.bounds().bottom(solver);
                let bounds_left = self.context.bounds().left(solver);
                let bounds_right = self.context.bounds().right(solver);

                let top_constraint = solver.float_eq(bounds_top, min_x);
                solver.constrain(top_constraint);

                let bottom_constraint = solver.float_eq(bounds_bottom, max_x);
                solver.constrain(bottom_constraint);

                let left_constraint = solver.float_eq(bounds_left, min_y);
                solver.constrain(left_constraint);

                let right_constraint = solver.float_eq(bounds_right, max_y);
                solver.constrain(right_constraint);
            }
            CoreShape::Image(image) => {
                if !image.keep_aspect_ratio {
                    return;
                }

                let scale_x = solver.new_free_float();
                let scale_y = solver.new_free_float();

                let constraint = solver.float_eq(scale_x, scale_y);
                solver.constrain(constraint);

                let (orig_width, orig_height) = renderer.geometry_for_image(&image.path);

                let orig_width_scaled = solver.float_mul(&[scale_x, Float::Fixed(orig_width)]);
                let width_constraint =
                    solver.float_eq(self.context.bounds().width, orig_width_scaled);
                solver.constrain(width_constraint);

                let orig_height_scaled = solver.float_mul(&[scale_y, Float::Fixed(orig_height)]);
                let height_constraint =
                    solver.float_eq(self.context.bounds().height, orig_height_scaled);
                solver.constrain(height_constraint);
            }
            _ => (),
        }
    }
}

#[derive(Debug, Clone)]
pub enum CoreShape {
    Rectangle(Rectangle),
    Text(Text),
    StraightPath(StraightPath),
    Image(Image),
}

impl From<Rectangle> for CoreShape {
    fn from(rectangle: Rectangle) -> Self {
        CoreShape::Rectangle(rectangle)
    }
}

impl From<Text> for CoreShape {
    fn from(text: Text) -> Self {
        CoreShape::Text(text)
    }
}

impl From<StraightPath> for CoreShape {
    fn from(path: StraightPath) -> Self {
        CoreShape::StraightPath(path)
    }
}

impl From<Image> for CoreShape {
    fn from(image: Image) -> Self {
        CoreShape::Image(image)
    }
}

pub struct DefinedCoreDrawable {
    pub(crate) shape: DefinedCoreShape,
    pub(crate) context: DefinedCoreShapeContext,
}

pub enum DefinedCoreShape {
    Rectangle(Rectangle),
    Text(DefinedText),
    StraightPath(DefinedStraightPath),
    Image(Image),
}

#[derive(Copy, Clone, Debug, Default)]
pub struct Rectangle {}

#[derive(Clone, Debug, Default)]
pub struct StraightPath {
    pub(crate) points: Vec<Point2D>,
}

impl StraightPath {
    pub fn new(points: Vec<Point2D>) -> Self {
        Self { points }
    }
}

pub struct DefinedStraightPath {
    pub(crate) points: Vec<DefinedPoint2D>,
}

#[derive(Clone, Debug, Default)]
pub struct Image {
    pub(crate) path: PathBuf,
    pub(crate) keep_aspect_ratio: bool,
}

impl Image {
    pub fn new(path: PathBuf, keep_aspect_ratio: bool) -> Self {
        Self {
            path,
            keep_aspect_ratio,
        }
    }
}