diff options
author | Minijackson <minijackson@riseup.net> | 2022-12-22 12:19:59 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2022-12-22 12:19:59 +0100 |
commit | 92a02c34628343153b33602eae00cef46e28d191 (patch) | |
tree | 8622ec528d24e456be22d984d93aa9bcafc97399 /examples/lib-dfscq-log/src/block_list.rs | |
download | diaphragm-92a02c34628343153b33602eae00cef46e28d191.tar.gz diaphragm-92a02c34628343153b33602eae00cef46e28d191.zip |
WIP
Diffstat (limited to 'examples/lib-dfscq-log/src/block_list.rs')
-rw-r--r-- | examples/lib-dfscq-log/src/block_list.rs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/examples/lib-dfscq-log/src/block_list.rs b/examples/lib-dfscq-log/src/block_list.rs new file mode 100644 index 0000000..be0eee0 --- /dev/null +++ b/examples/lib-dfscq-log/src/block_list.rs | |||
@@ -0,0 +1,183 @@ | |||
1 | use super::blocks::Blocks; | ||
2 | use super::bracket::*; | ||
3 | |||
4 | use diaphragm_core::{ | ||
5 | core_shapes::Text, | ||
6 | text::FontDescription, | ||
7 | types::{Bounds, Float, ShapeContext}, | ||
8 | ComplexShape, DrawResult, Drawable, SolverContext, | ||
9 | }; | ||
10 | |||
11 | #[derive(Clone)] | ||
12 | pub struct BlockList { | ||
13 | pub block_list: Vec<Drawable<Blocks>>, | ||
14 | pub block_unit_width: Float, | ||
15 | pub bracket_width: Float, | ||
16 | pub blocks_vert_padding: Float, | ||
17 | } | ||
18 | |||
19 | impl ComplexShape for BlockList { | ||
20 | fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult { | ||
21 | let mut result = DrawResult::new(); | ||
22 | |||
23 | let bounds = context.bounds(); | ||
24 | |||
25 | // Left bracket | ||
26 | let opening_bracket_left = { | ||
27 | let bracket = Drawable::builder(Bracket { | ||
28 | r#type: BracketType::Opening, | ||
29 | }) | ||
30 | .bounds( | ||
31 | Bounds::builder() | ||
32 | .top(bounds.top(solver)) | ||
33 | .left(bounds.left(solver)) | ||
34 | .width(self.bracket_width) | ||
35 | .height(bounds.height(solver)) | ||
36 | .build(solver), | ||
37 | ) | ||
38 | .stroke(context.stroke().clone()) | ||
39 | .build(solver); | ||
40 | |||
41 | let bracket_left = bracket.bounds().left(solver); | ||
42 | |||
43 | result.push(bracket); | ||
44 | |||
45 | bracket_left | ||
46 | }; | ||
47 | |||
48 | let bounds_left = bounds.left(solver); | ||
49 | let mut x_acc = solver.float_add(&[bounds_left, self.bracket_width]); | ||
50 | |||
51 | // Block list | ||
52 | |||
53 | let bounds_top = bounds.top(solver); | ||
54 | let bounds_height = bounds.height(solver); | ||
55 | let wanted_blocks_top = solver.float_add(&[bounds_top, self.blocks_vert_padding]); | ||
56 | let wanted_blocks_height = solver.float_sub(&[ | ||
57 | bounds_height, | ||
58 | self.blocks_vert_padding, | ||
59 | self.blocks_vert_padding, | ||
60 | ]); | ||
61 | |||
62 | let mut block_list_iter = self.block_list.iter(); | ||
63 | |||
64 | // First blocks | ||
65 | if let Some(first_blocks) = block_list_iter.next() { | ||
66 | // TODO: waaay too verbose | ||
67 | |||
68 | let blocks_top = first_blocks.bounds().top(solver); | ||
69 | let blocks_top_constraint = solver.float_eq(blocks_top, wanted_blocks_top); | ||
70 | solver.constrain(blocks_top_constraint); | ||
71 | |||
72 | let blocks_left = first_blocks.bounds().left(solver); | ||
73 | let blocks_left_constraint = solver.float_eq(blocks_left, x_acc); | ||
74 | solver.constrain(blocks_left_constraint); | ||
75 | |||
76 | let blocks_height = first_blocks.bounds().height(solver); | ||
77 | let blocks_height_constraint = solver.float_eq(blocks_height, wanted_blocks_height); | ||
78 | solver.constrain(blocks_height_constraint); | ||
79 | |||
80 | let block_unit_width_constraint = | ||
81 | solver.float_eq(first_blocks.shape().unit_width, self.block_unit_width); | ||
82 | solver.constrain(block_unit_width_constraint); | ||
83 | |||
84 | result.push(first_blocks.clone()); | ||
85 | |||
86 | let blocks_width = first_blocks.bounds().width(solver); | ||
87 | x_acc = solver.float_add(&[x_acc, blocks_width]); | ||
88 | } | ||
89 | |||
90 | // Rest of the blocks | ||
91 | for blocks in block_list_iter { | ||
92 | // Comma | ||
93 | let comma = Drawable::builder(Text { | ||
94 | content: String::from(", "), | ||
95 | font: FontDescription { | ||
96 | family: String::from("mono"), | ||
97 | style: Default::default(), | ||
98 | weight: Default::default(), | ||
99 | size: wanted_blocks_height, | ||
100 | }, | ||
101 | }) | ||
102 | .bounds( | ||
103 | Bounds::builder() | ||
104 | .top(wanted_blocks_top) | ||
105 | .left(x_acc) | ||
106 | .build(solver), | ||
107 | ) | ||
108 | .stroke(context.stroke().clone()) | ||
109 | .build(solver); | ||
110 | |||
111 | let comma_width = comma.bounds().width(solver); | ||
112 | |||
113 | x_acc = solver.float_add(&[x_acc, comma_width]); | ||
114 | |||
115 | result.push(comma); | ||
116 | |||
117 | // Blocks | ||
118 | /* | ||
119 | let blocks_context = ShapeContext { | ||
120 | bounds: Bounds { | ||
121 | top: blocks_top, | ||
122 | left: x_acc, | ||
123 | height: blocks_height, | ||
124 | width: blocks_width, | ||
125 | }, | ||
126 | fill: FillStyle::default(), | ||
127 | stroke: StrokeStyle::default(), | ||
128 | }; | ||
129 | */ | ||
130 | |||
131 | let blocks_top = blocks.bounds().top(solver); | ||
132 | let blocks_top_constraint = solver.float_eq(blocks_top, wanted_blocks_top); | ||
133 | solver.constrain(blocks_top_constraint); | ||
134 | |||
135 | let blocks_left = blocks.bounds().left(solver); | ||
136 | let blocks_left_constraint = solver.float_eq(blocks_left, x_acc); | ||
137 | solver.constrain(blocks_left_constraint); | ||
138 | |||
139 | let blocks_height = blocks.bounds().height(solver); | ||
140 | let blocks_height_constraint = solver.float_eq(blocks_height, wanted_blocks_height); | ||
141 | solver.constrain(blocks_height_constraint); | ||
142 | |||
143 | let block_unit_width_constraint = | ||
144 | solver.float_eq(blocks.shape().unit_width, self.block_unit_width); | ||
145 | solver.constrain(block_unit_width_constraint); | ||
146 | |||
147 | result.push(blocks.clone()); | ||
148 | |||
149 | let blocks_width = blocks.bounds().width(solver); | ||
150 | x_acc = solver.float_add(&[x_acc, blocks_width]); | ||
151 | } | ||
152 | |||
153 | // Right bracket | ||
154 | let closing_bracket_right = { | ||
155 | let bracket = Drawable::builder(Bracket { | ||
156 | r#type: BracketType::Closing, | ||
157 | }) | ||
158 | .bounds( | ||
159 | Bounds::builder() | ||
160 | .top(bounds.top(solver)) | ||
161 | .left(x_acc) | ||
162 | .width(self.bracket_width) | ||
163 | .height(bounds.height(solver)) | ||
164 | .build(solver), | ||
165 | ) | ||
166 | .stroke(context.stroke().clone()) | ||
167 | .build(solver); | ||
168 | |||
169 | let bracket_right = bracket.bounds().right(solver); | ||
170 | |||
171 | result.push(bracket); | ||
172 | |||
173 | bracket_right | ||
174 | }; | ||
175 | |||
176 | let this_width = solver.float_sub(&[closing_bracket_right, opening_bracket_left]); | ||
177 | let bounds_width = bounds.width(solver); | ||
178 | let bounds_width_constraint = solver.float_eq(bounds_width, this_width); | ||
179 | solver.constrain(bounds_width_constraint); | ||
180 | |||
181 | result | ||
182 | } | ||
183 | } | ||