diff options
Diffstat (limited to 'examples/lib-dfscq-log/src/blocks.rs')
-rw-r--r-- | examples/lib-dfscq-log/src/blocks.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/examples/lib-dfscq-log/src/blocks.rs b/examples/lib-dfscq-log/src/blocks.rs new file mode 100644 index 0000000..0576b7d --- /dev/null +++ b/examples/lib-dfscq-log/src/blocks.rs | |||
@@ -0,0 +1,72 @@ | |||
1 | use diaphragm_core::{ | ||
2 | core_shapes::Rectangle, | ||
3 | types::{Float, ShapeContext}, | ||
4 | ComplexShape, DrawResult, Drawable, SolverContext, | ||
5 | }; | ||
6 | |||
7 | #[derive(Debug, Clone)] | ||
8 | pub struct Block { | ||
9 | pub grow: u8, | ||
10 | } | ||
11 | |||
12 | impl ComplexShape for Block { | ||
13 | fn draw(&self, context: &ShapeContext, _solver: &mut dyn SolverContext) -> DrawResult { | ||
14 | let mut result = DrawResult::new(); | ||
15 | |||
16 | // Grow is handled at the upper level | ||
17 | let block = Drawable::new(Rectangle {}, context.clone()); | ||
18 | |||
19 | result.push(block); | ||
20 | |||
21 | result | ||
22 | } | ||
23 | } | ||
24 | |||
25 | #[derive(Debug, Clone)] | ||
26 | pub struct Blocks { | ||
27 | pub blocks: Vec<Drawable<Block>>, | ||
28 | pub unit_width: Float, | ||
29 | } | ||
30 | |||
31 | impl ComplexShape for Blocks { | ||
32 | fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult { | ||
33 | let mut result = DrawResult::new(); | ||
34 | |||
35 | let sum: u8 = self.blocks.iter().map(|block| block.shape().grow).sum(); | ||
36 | |||
37 | let mut rect_left = context.bounds().left(solver); | ||
38 | let rect_top = context.bounds().top(solver); | ||
39 | let rect_height = context.bounds().height(solver); | ||
40 | |||
41 | for block in &self.blocks { | ||
42 | let block_top = block.bounds().top(solver); | ||
43 | let rect_top_constraint = solver.float_eq(block_top, rect_top); | ||
44 | solver.constrain(rect_top_constraint); | ||
45 | |||
46 | let block_left = block.bounds().left(solver); | ||
47 | let rect_left_constraint = solver.float_eq(block_left, rect_left); | ||
48 | solver.constrain(rect_left_constraint); | ||
49 | |||
50 | let grow = Float::Fixed(block.shape().grow as f64); | ||
51 | let block_width = block.bounds().width(solver); | ||
52 | let rect_width = solver.float_mul(&[self.unit_width, grow]); | ||
53 | let rect_width_constraint = solver.float_eq(block_width, rect_width); | ||
54 | solver.constrain(rect_width_constraint); | ||
55 | |||
56 | let block_height = block.bounds().height(solver); | ||
57 | let rect_height_constraint = solver.float_eq(block_height, rect_height); | ||
58 | solver.constrain(rect_height_constraint); | ||
59 | |||
60 | result.push(block.clone()); | ||
61 | |||
62 | rect_left = solver.float_add(&[rect_left, rect_width]); | ||
63 | } | ||
64 | |||
65 | let this_width = solver.float_mul(&[self.unit_width, Float::Fixed(sum as f64)]); | ||
66 | let bounds_width = context.bounds().width(solver); | ||
67 | let bounds_width_constraint = solver.float_eq(bounds_width, this_width); | ||
68 | solver.constrain(bounds_width_constraint); | ||
69 | |||
70 | result | ||
71 | } | ||
72 | } | ||