diff options
author | Minijackson <minijackson@riseup.net> | 2022-12-29 01:40:08 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2022-12-29 01:40:08 +0100 |
commit | 9c15b76c5a6355902b2a105a7c6ee93f6b5016dc (patch) | |
tree | 44cacfe756169f7b3d3cc23f875864683ba2af74 /core/src/runtime.rs | |
parent | 30f7d39ca2ed4590b5d356b1a4c024d11156a383 (diff) | |
download | diaphragm-9c15b76c5a6355902b2a105a7c6ee93f6b5016dc.tar.gz diaphragm-9c15b76c5a6355902b2a105a7c6ee93f6b5016dc.zip |
WIP v2: text works, primitives works in Lua
Diffstat (limited to 'core/src/runtime.rs')
-rw-r--r-- | core/src/runtime.rs | 154 |
1 files changed, 89 insertions, 65 deletions
diff --git a/core/src/runtime.rs b/core/src/runtime.rs index bfb31fa..d70e4d0 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs | |||
@@ -1,13 +1,17 @@ | |||
1 | use super::complex_shapes::{ComplexShape, Drawable, DynDrawable}; | 1 | use crate::core_shapes::CoreDrawable; |
2 | use crate::rendering::Render; | ||
3 | |||
4 | // use super::complex_shapes::{ComplexShape, Drawable}; | ||
2 | use super::rendering::Renderer; | 5 | use super::rendering::Renderer; |
3 | use super::solving::{Constrainable, SolverContext}; | 6 | use super::solving::{Constrainable, SolverContext}; |
4 | 7 | ||
5 | const RECURSION_LIMIT: u64 = 10_000; | 8 | // const RECURSION_LIMIT: u64 = 10_000; |
6 | 9 | ||
7 | pub struct Runtime<'a> { | 10 | pub struct Runtime<'a> { |
8 | solver_ctx: Box<dyn SolverContext + 'a>, | 11 | solver_ctx: Box<dyn SolverContext + 'a>, |
9 | renderer: Box<dyn Renderer>, | 12 | renderer: Box<dyn Renderer>, |
10 | drawables: Vec<DynDrawable>, | 13 | // drawables: Vec<DynDrawable>, |
14 | drawables: Vec<CoreDrawable>, | ||
11 | } | 15 | } |
12 | 16 | ||
13 | impl<'a> Runtime<'a> { | 17 | impl<'a> Runtime<'a> { |
@@ -15,81 +19,101 @@ impl<'a> Runtime<'a> { | |||
15 | Self { | 19 | Self { |
16 | solver_ctx, | 20 | solver_ctx, |
17 | renderer, | 21 | renderer, |
22 | // drawables: Vec::new(), | ||
18 | drawables: Vec::new(), | 23 | drawables: Vec::new(), |
19 | } | 24 | } |
20 | } | 25 | } |
21 | 26 | ||
22 | pub fn add_drawable<T: ComplexShape + 'static>(&mut self, drawable: Drawable<T>) { | 27 | // pub fn add_drawable<T: ComplexShape + 'static>(&mut self, drawable: Drawable<T>) { |
23 | self.drawables.push(drawable.into()) | 28 | // self.drawables.push(drawable.into()) |
29 | // } | ||
30 | |||
31 | pub fn add_drawable(&mut self, drawable: CoreDrawable) { | ||
32 | self.drawables.push(drawable) | ||
24 | } | 33 | } |
25 | 34 | ||
26 | pub fn solver_ctx(&mut self) -> &mut (dyn SolverContext + 'a) { | 35 | pub fn solver_ctx(&mut self) -> &mut (dyn SolverContext + 'a) { |
27 | &mut *self.solver_ctx | 36 | &mut *self.solver_ctx |
28 | } | 37 | } |
29 | 38 | ||
30 | // TODO: preserve ordering of shapes | 39 | pub fn renderer(&mut self) -> &mut dyn Renderer { |
31 | pub fn render(mut self) { | 40 | &mut *self.renderer |
32 | let mut drawables = self.drawables; | 41 | } |
33 | let mut waited_on_variables = Vec::new(); | ||
34 | let mut core_shapes = Vec::new(); | ||
35 | |||
36 | /* | ||
37 | for drawable in &self.shapes { | ||
38 | let bounds = &drawable.bounds; | ||
39 | let shape = &drawable.shape; | ||
40 | |||
41 | if let Some(core_shape) = shape.to_render() { | ||
42 | drawables.push((*drawable).clone()); | ||
43 | continue; | ||
44 | } | ||
45 | |||
46 | let mut result = shape.draw(bounds); | ||
47 | drawables.append(&mut result.subshapes); | ||
48 | waited_on_variables.append(&mut result.waiting_on); | ||
49 | } | ||
50 | */ | ||
51 | |||
52 | let mut recursion_count = 0; | ||
53 | |||
54 | while !drawables.is_empty() { | ||
55 | recursion_count += 1; | ||
56 | |||
57 | if recursion_count > RECURSION_LIMIT { | ||
58 | panic!("Recursion limit reached"); | ||
59 | } | ||
60 | |||
61 | let mut tmp_drawables = Vec::new(); | ||
62 | |||
63 | for drawable in drawables.drain(..) { | ||
64 | let shape_ctx = &drawable.context; | ||
65 | let shape = &drawable.shape; | ||
66 | |||
67 | if let Some(core_shape) = shape.as_core_shape() { | ||
68 | core_shape.constrain(shape_ctx, &mut *self.solver_ctx, &*self.renderer); | ||
69 | core_shapes.push((shape.dyn_clone(), shape_ctx.clone())); // Better to Arc? Cow? | ||
70 | continue; | ||
71 | } | ||
72 | |||
73 | let mut result = shape.draw(shape_ctx, &mut *self.solver_ctx); | ||
74 | tmp_drawables.append(&mut result.subshapes); | ||
75 | waited_on_variables.append(&mut result.waiting_on); | ||
76 | } | ||
77 | |||
78 | drawables = tmp_drawables; | ||
79 | } | ||
80 | 42 | ||
43 | pub fn render(mut self) { | ||
81 | let model = self.solver_ctx.solve(); | 44 | let model = self.solver_ctx.solve(); |
82 | 45 | ||
83 | // Delay rendering core shapes until later to have all the constraints | 46 | for drawable in &self.drawables { |
84 | for (core_shape, shape_ctx) in core_shapes { | 47 | let defined_drawable = drawable |
85 | let core_shape = core_shape.as_core_shape().unwrap(); | 48 | .fixate(&*model) |
86 | 49 | .expect("Could not fixate core shape"); | |
87 | match (core_shape.to_render(&*model), shape_ctx.fixate(&*model)) { | 50 | defined_drawable.shape.render(defined_drawable.context, &mut *self.renderer); |
88 | (Some(defined_shape), Some(shape_ctx)) => { | ||
89 | defined_shape.render(shape_ctx, &mut *self.renderer) | ||
90 | } | ||
91 | _ => panic!("Failed to fixate core shape"), | ||
92 | } | ||
93 | } | 51 | } |
94 | } | 52 | } |
53 | |||
54 | // TODO: preserve ordering of shapes | ||
55 | // pub fn render(mut self) { | ||
56 | // let mut drawables = self.drawables; | ||
57 | // let mut waited_on_variables = Vec::new(); | ||
58 | // let mut core_shapes = Vec::new(); | ||
59 | // | ||
60 | // /* | ||
61 | // for drawable in &self.shapes { | ||
62 | // let bounds = &drawable.bounds; | ||
63 | // let shape = &drawable.shape; | ||
64 | // | ||
65 | // if let Some(core_shape) = shape.to_render() { | ||
66 | // drawables.push((*drawable).clone()); | ||
67 | // continue; | ||
68 | // } | ||
69 | // | ||
70 | // let mut result = shape.draw(bounds); | ||
71 | // drawables.append(&mut result.subshapes); | ||
72 | // waited_on_variables.append(&mut result.waiting_on); | ||
73 | // } | ||
74 | // */ | ||
75 | // | ||
76 | // let mut recursion_count = 0; | ||
77 | // | ||
78 | // while !drawables.is_empty() { | ||
79 | // recursion_count += 1; | ||
80 | // | ||
81 | // if recursion_count > RECURSION_LIMIT { | ||
82 | // panic!("Recursion limit reached"); | ||
83 | // } | ||
84 | // | ||
85 | // let mut tmp_drawables = Vec::new(); | ||
86 | // | ||
87 | // for drawable in drawables.drain(..) { | ||
88 | // let shape_ctx = &drawable.context; | ||
89 | // let shape = &drawable.shape; | ||
90 | // | ||
91 | // if let Some(core_shape) = shape.as_core_shape() { | ||
92 | // core_shape.constrain(shape_ctx, &mut *self.solver_ctx, &*self.renderer); | ||
93 | // core_shapes.push((shape.dyn_clone(), shape_ctx.clone())); // Better to Arc? Cow? | ||
94 | // continue; | ||
95 | // } | ||
96 | // | ||
97 | // let mut result = shape.draw(shape_ctx, &mut *self.solver_ctx); | ||
98 | // tmp_drawables.append(&mut result.subshapes); | ||
99 | // waited_on_variables.append(&mut result.waiting_on); | ||
100 | // } | ||
101 | // | ||
102 | // drawables = tmp_drawables; | ||
103 | // } | ||
104 | // | ||
105 | // let model = self.solver_ctx.solve(); | ||
106 | // | ||
107 | // // Delay rendering core shapes until later to have all the constraints | ||
108 | // for (core_shape, shape_ctx) in core_shapes { | ||
109 | // let core_shape = core_shape.as_core_shape().unwrap(); | ||
110 | // | ||
111 | // match (core_shape.to_render(&*model), shape_ctx.fixate(&*model)) { | ||
112 | // (Some(defined_shape), Some(shape_ctx)) => { | ||
113 | // defined_shape.render(shape_ctx, &mut *self.renderer) | ||
114 | // } | ||
115 | // _ => panic!("Failed to fixate core shape"), | ||
116 | // } | ||
117 | // } | ||
118 | // } | ||
95 | } | 119 | } |