From 82ffe3187a32bad4ecca0736882a23793a800822 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Fri, 3 Feb 2023 19:59:43 +0100 Subject: add support for images, and some chores - SVG files not yet supported - Remove dead commented code - Add inherent constraints for text, path, and images - Formatting --- lua-bindings/src/lib.rs | 84 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 24 deletions(-) (limited to 'lua-bindings/src') diff --git a/lua-bindings/src/lib.rs b/lua-bindings/src/lib.rs index 86a759d..26081c8 100644 --- a/lua-bindings/src/lib.rs +++ b/lua-bindings/src/lib.rs @@ -11,7 +11,8 @@ use diaphragm_cairo_renderer::CairoRenderer; use diaphragm_core::{ colors::Color, core_shapes::{ - CoreDrawable, CoreShape, Rectangle as CoreRectangle, StraightPath as CoreStraightPath, + CoreDrawable, CoreShape, Image as CoreImage, Rectangle as CoreRectangle, + StraightPath as CoreStraightPath, }, solving::VariableHandle, styles::Pattern, @@ -517,28 +518,6 @@ fn text(_: &Lua, params: LuaTable) -> LuaResult { let context = new_shape_context(); - let content_2 = content.clone(); - let font_2 = font.0.clone(); - let context_2 = context.clone(); - - // TODO: this shouldn't be here, this should be an innate property of Text. Move the - // Drawable struct in core? - runtime_thread_do(Box::new(move |r| { - let (text_width, text_height) = r.renderer().text_extents(&content_2, &font_2); - let solver = r.solver_ctx(); - // let scale = solver.float_div(font.0.size, CoreFloat::Fixed(height)); - - let calculated_width = solver.float_mul(&[CoreFloat::Fixed(text_width), font.0.size]); - let bounds_width = context_2.bounds().width(solver); - let width_constraint = solver.float_eq(bounds_width, calculated_width); - solver.constrain(width_constraint); - - let calculated_height = solver.float_mul(&[CoreFloat::Fixed(text_height), font.0.size]); - let bounds_height = context_2.bounds().height(solver); - let height_constraint = solver.float_eq(bounds_height, calculated_height); - solver.constrain(height_constraint); - })); - Ok(Text(Drawable { shape: CoreText { content, @@ -646,6 +625,63 @@ fn straight_path(_: &Lua, params: LuaTable) -> LuaResult { points.try_into() } +#[derive(Debug, Clone)] +struct Image(Drawable); +impl LuaUserData for Image { + fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_method("draw", |_, this, _params: ()| { + let drawable = this.0.clone().into(); + runtime_thread_do(Box::new(|r| { + r.add_drawable(drawable); + })); + Ok(()) + }) + } + + fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { + add_bound_fields(fields); + } +} + +impl HasContext for Image { + fn get_context(&self) -> &CoreShapeContext { + &self.0.context + } + + fn get_context_mut(&mut self) -> &mut CoreShapeContext { + &mut self.0.context + } +} + +impl TryFrom> for Image { + type Error = LuaError; + + fn try_from(value: LuaValue) -> Result { + match value { + LuaValue::Table(t) => { + let path: String = t.get("path")?; + let keep_aspect_ratio = t.get::<_, Option<_>>("keep_aspect_ratio")?.unwrap_or(true); + + Ok(Image(Drawable { + shape: CoreImage::new(path.into(), keep_aspect_ratio), + context: new_shape_context(), + })) + } + + LuaValue::UserData(u) => Ok(Image(u.borrow::()?.0.clone())), + _ => Err(LuaError::FromLuaConversionError { + from: value.type_name(), + to: "StraightPath", + message: Some("Only a list of points are allowed".to_string()), + }), + } + } +} + +fn image(_: &Lua, params: LuaTable) -> LuaResult { + LuaValue::Table(params).try_into() +} + // It just has a context for bounds, the rest is handled manually in Lua #[derive(Debug, Clone)] struct ComplexShape(CoreShapeContext); @@ -800,8 +836,8 @@ fn libdiaphragm(lua: &Lua) -> LuaResult { exports.set("font", lua.create_function(font)?)?; exports.set("rectangle", lua.create_function(rectangle)?)?; - exports.set("straight_path", lua.create_function(straight_path)?)?; + exports.set("image", lua.create_function(image)?)?; exports.set("complex_shape", lua.create_function(complex_shape)?)?; -- cgit v1.2.3