use crate::core_shapes::CoreDrawable; use crate::rendering::Render; use crate::types::Bounds; // use super::complex_shapes::{ComplexShape, Drawable}; use super::rendering::Renderer; use super::solving::{Constrainable, SolverContext}; // const RECURSION_LIMIT: u64 = 10_000; pub struct Runtime<'a> { solver_ctx: Box, renderer: Box, // drawables: Vec, drawables: Vec, } impl<'a> Runtime<'a> { pub fn new(solver_ctx: Box, renderer: Box) -> Self { Self { solver_ctx, renderer, // drawables: Vec::new(), drawables: Vec::new(), } } // pub fn add_drawable(&mut self, drawable: Drawable) { // self.drawables.push(drawable.into()) // } pub fn add_drawable(&mut self, drawable: CoreDrawable) { self.drawables.push(drawable) } pub fn solver_ctx(&mut self) -> &mut (dyn SolverContext + 'a) { &mut *self.solver_ctx } pub fn renderer(&mut self) -> &mut dyn Renderer { &mut *self.renderer } pub fn render(mut self, bounds: Bounds) { let model = self.solver_ctx.solve(); let bounds = bounds .fixate(&*model) .expect("Could not fixate figure bounds"); self.renderer.set_size(bounds.width, bounds.height); for drawable in &self.drawables { let defined_drawable = drawable .fixate(&*model) .expect("Could not fixate core shape"); defined_drawable .shape .render(defined_drawable.context, &mut *self.renderer); } } // TODO: preserve ordering of shapes // pub fn render(mut self) { // let mut drawables = self.drawables; // let mut waited_on_variables = Vec::new(); // let mut core_shapes = Vec::new(); // // /* // for drawable in &self.shapes { // let bounds = &drawable.bounds; // let shape = &drawable.shape; // // if let Some(core_shape) = shape.to_render() { // drawables.push((*drawable).clone()); // continue; // } // // let mut result = shape.draw(bounds); // drawables.append(&mut result.subshapes); // waited_on_variables.append(&mut result.waiting_on); // } // */ // // let mut recursion_count = 0; // // while !drawables.is_empty() { // recursion_count += 1; // // if recursion_count > RECURSION_LIMIT { // panic!("Recursion limit reached"); // } // // let mut tmp_drawables = Vec::new(); // // for drawable in drawables.drain(..) { // let shape_ctx = &drawable.context; // let shape = &drawable.shape; // // if let Some(core_shape) = shape.as_core_shape() { // core_shape.constrain(shape_ctx, &mut *self.solver_ctx, &*self.renderer); // core_shapes.push((shape.dyn_clone(), shape_ctx.clone())); // Better to Arc? Cow? // continue; // } // // let mut result = shape.draw(shape_ctx, &mut *self.solver_ctx); // tmp_drawables.append(&mut result.subshapes); // waited_on_variables.append(&mut result.waiting_on); // } // // drawables = tmp_drawables; // } // // let model = self.solver_ctx.solve(); // // // Delay rendering core shapes until later to have all the constraints // for (core_shape, shape_ctx) in core_shapes { // let core_shape = core_shape.as_core_shape().unwrap(); // // match (core_shape.to_render(&*model), shape_ctx.fixate(&*model)) { // (Some(defined_shape), Some(shape_ctx)) => { // defined_shape.render(shape_ctx, &mut *self.renderer) // } // _ => panic!("Failed to fixate core shape"), // } // } // } }