summaryrefslogtreecommitdiffstats
path: root/examples/lib-dfscq-log/src/labeled.rs
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
committerMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
commit92a02c34628343153b33602eae00cef46e28d191 (patch)
tree8622ec528d24e456be22d984d93aa9bcafc97399 /examples/lib-dfscq-log/src/labeled.rs
downloaddiaphragm-92a02c34628343153b33602eae00cef46e28d191.tar.gz
diaphragm-92a02c34628343153b33602eae00cef46e28d191.zip
WIP
Diffstat (limited to 'examples/lib-dfscq-log/src/labeled.rs')
-rw-r--r--examples/lib-dfscq-log/src/labeled.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/lib-dfscq-log/src/labeled.rs b/examples/lib-dfscq-log/src/labeled.rs
new file mode 100644
index 0000000..20aebcd
--- /dev/null
+++ b/examples/lib-dfscq-log/src/labeled.rs
@@ -0,0 +1,64 @@
1use diaphragm_core::{
2 core_shapes::Text,
3 text::FontDescription,
4 types::{Bounds, Float, ShapeContext},
5 ComplexShape, DrawResult, DynClone, SolverContext,
6};
7
8pub struct Labeled {
9 pub label: String,
10 pub label_font: FontDescription,
11 pub content: Box<dyn ComplexShape>,
12 pub content_left: Float,
13}
14
15impl ComplexShape for Labeled {
16 fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult {
17 let mut result = DrawResult::new();
18
19 // Label
20
21 let label = Text {
22 content: self.label.clone(),
23 font: self.label_font.clone(),
24 };
25
26 let mut label_context = context.clone();
27 // TODO: make the text constrain the width
28 label_context.bounds.width = solver.new_free_float();
29
30 let label_right = solver.float_add(&[context.bounds.left, label_context.bounds.width]);
31 let content_left_constrain = solver.float_eq(label_right, self.content_left);
32 solver.constrain(content_left_constrain);
33
34 result.push_shape(label, label_context);
35
36 // Content
37
38 let content_context = ShapeContext {
39 bounds: Bounds {
40 top: context.bounds.top,
41 left: solver.float_add(&[context.bounds.left, self.content_left]),
42 height: context.bounds.height,
43 width: context.bounds.width,
44 },
45 fill: context.fill.clone(),
46 stroke: context.stroke.clone(),
47 };
48
49 result.push_boxed_shape(self.content.dyn_clone(), content_context);
50
51 result
52 }
53}
54
55impl DynClone for Labeled {
56 fn dyn_clone(&self) -> Box<dyn ComplexShape> {
57 Box::new(Labeled {
58 label: self.label.clone(),
59 label_font: self.label_font.clone(),
60 content: self.content.dyn_clone(),
61 content_left: self.content_left.clone(),
62 })
63 }
64}