From 7b0e215008c94c4a7ddb5f47b12c341d807ea062 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Fri, 20 Jan 2023 16:03:25 +0100 Subject: tree-wide: properly set figure size --- lua-bindings/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'lua-bindings') diff --git a/lua-bindings/src/lib.rs b/lua-bindings/src/lib.rs index 35cdaf8..53622de 100644 --- a/lua-bindings/src/lib.rs +++ b/lua-bindings/src/lib.rs @@ -16,7 +16,9 @@ use diaphragm_core::{ solving::VariableHandle, styles::Pattern, text::{FontDescription as CoreFontDescription, FontStyle, FontWeight, Text as CoreText}, - types::{Bool as CoreBool, CoreShapeContext, Float as CoreFloat, Point2D as CorePoint2D}, + types::{ + Bool as CoreBool, Bounds, CoreShapeContext, Float as CoreFloat, Point2D as CorePoint2D, + }, Runtime, }; use diaphragm_z3_solver::{z3, Z3Context}; @@ -656,6 +658,7 @@ thread_local! { enum Message { Do(Box), + SetBounds(Bounds), } fn runtime_thread_do(fun: Box T + Send>) -> T { @@ -698,7 +701,7 @@ fn constrain(_: &Lua, bool: Bool) -> LuaResult<()> { Ok(()) } -fn draw(_: &Lua, params: LuaTable) -> LuaResult<()> { +fn draw(lua: &Lua, params: LuaTable) -> LuaResult<()> { // So.... The Z3 stuff isn't Send and contains lifetimes, so we can't store them in global // variables or convert them to Lua. Solution: handle everything in a specific thread, and // communicate through a channel. @@ -728,23 +731,54 @@ fn draw(_: &Lua, params: LuaTable) -> LuaResult<()> { // TODO: we shouldn't need the renderer until the end let mut runtime = Runtime::new(Box::new(ctx), Box::new(cairo_renderer)); + let mut bounds = None; for message in message_receiver { match message { Message::Do(fun) => { fun(&mut runtime); } + Message::SetBounds(new_bounds) => bounds = Some(new_bounds), } } - runtime.render(); + let bounds = bounds.unwrap(); + + // TODO: it would be nicer to do in Lua + let bounds_top = bounds.top(runtime.solver_ctx()); + let top_constraint = runtime + .solver_ctx() + .float_eq(bounds_top, CoreFloat::Fixed(0.0)); + runtime.solver_ctx().constrain(top_constraint); + + let bounds_left = bounds.left(runtime.solver_ctx()); + let left_constraint = runtime + .solver_ctx() + .float_eq(bounds_left, CoreFloat::Fixed(0.0)); + runtime.solver_ctx().constrain(left_constraint); + + runtime.render(bounds); }); let content: LuaValue = params.get("content")?; let _output: LuaTable = params.get("output")?; + let figure = complex_shape(lua, ())?; + let figure_bounds = figure.0.bounds().clone(); + + // TODO: sending bounds before callin `draw` prevents doing things like + // figure = dia.rectangle.new(...) + SENDER.with(|sender| { + sender + .borrow_mut() + .as_mut() + .expect("Not currently drawing") + .send(Message::SetBounds(figure_bounds)) + .unwrap() + }); + match content { // TODO: this doesn't stop the runtime thread? - LuaValue::Table(table) => table.call_method("draw", ()).unwrap(), + LuaValue::Table(table) => table.call_method("draw", figure).unwrap(), // TODO: switch to enum // LuaValue::UserData(user_data) => { -- cgit v1.2.3