From 49b4eabe8b2bb0153481d21806db69ba4a3614ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Wed, 13 Nov 2024 10:56:00 +0100 Subject: [PATCH] :bug: Fix transform matrix being carried to non-children shapes --- render-wasm/src/main.rs | 10 ++++---- render-wasm/src/render.rs | 52 +++++++++++++++++++++++++-------------- render-wasm/src/shapes.rs | 4 ++- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 5a427f05f..c54074e9a 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -49,7 +49,7 @@ pub unsafe extern "C" fn draw_all_shapes(zoom: f32, pan_x: f32, pan_y: f32) { scale(zoom, zoom); translate(pan_x, pan_y); - render::render_shape(state, Uuid::nil()); + render::render_shape_tree(state, Uuid::nil()); flush(); } @@ -143,16 +143,16 @@ pub extern "C" fn add_child_shape(a: u32, b: u32, c: u32, d: u32) { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); let id = uuid_from_u32_quartet(a, b, c, d); if let Some(shape) = state.current_shape.as_deref_mut() { - shape.shapes.push(id); - } + shape.shapes.push(id); + } } #[no_mangle] pub extern "C" fn clear_child_shapes() { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); if let Some(shape) = state.current_shape.as_deref_mut() { - shape.shapes.clear(); - } + shape.shapes.clear(); + } } fn main() { diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index 833b05c42..68a51d41b 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -2,6 +2,7 @@ use skia_safe as skia; use skia_safe::gpu::{self, gl::FramebufferInfo, DirectContext}; use uuid::Uuid; +use crate::shapes::Shape; use crate::state::State; extern "C" { @@ -82,8 +83,24 @@ pub(crate) fn render_rect(surface: &mut skia::Surface, rect: skia::Rect, color: surface.canvas().draw_rect(rect, &paint); } -pub(crate) fn render_shape(state: &mut State, id: Uuid) { +pub(crate) fn render_shape_tree(state: &mut State, id: Uuid) { let shape = state.shapes.get(&id).unwrap(); + + // This is needed so the next non-children shape does not carry this shape's transform + state.render_state.surface.canvas().save(); + + render_single_shape(&mut state.render_state.surface, shape); + + // draw all the children shapes + let shape_ids = shape.shapes.clone(); + for shape_id in shape_ids { + render_shape_tree(state, shape_id); + } + + state.render_state.surface.canvas().restore(); +} + +fn render_single_shape(surface: &mut skia::Surface, shape: &Shape) { let r = skia::Rect::new( shape.selrect.x1, shape.selrect.y1, @@ -91,35 +108,34 @@ pub(crate) fn render_shape(state: &mut State, id: Uuid) { shape.selrect.y2, ); - // TODO: check if this save and restore are really neaded or not - // state.render_state.surface.canvas().save(); - // Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc let mut matrix = skia::Matrix::new_identity(); let (translate_x, translate_y) = shape.translation(); - let (scale_x, scale_y) = shape.scale(); + let (scale_x, scale_y) = shape.scale(); let (skew_x, skew_y) = shape.skew(); - matrix.set_all(scale_x, skew_x, translate_x, skew_y, scale_y, translate_y, 0., 0., 1.); + matrix.set_all( + scale_x, + skew_x, + translate_x, + skew_y, + scale_y, + translate_y, + 0., + 0., + 1., + ); let mut center = r.center(); matrix.post_translate(center); center.negate(); matrix.pre_translate(center); - state.render_state.surface.canvas().concat(&matrix); + surface.canvas().concat(&matrix); - let mut color = skia::Color::RED;; + let mut color = skia::Color::RED; if skew_x != 0. { - color = skia::Color::BLACK; - } - render_rect(&mut state.render_state.surface, r, color); - - // state.render_state.surface.canvas().restore(); - - let shape_ids = shape.shapes.clone(); - - for shape_id in shape_ids { - render_shape(state, shape_id); + color = skia::Color::BLACK; } + render_rect(surface, r, color); } diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 38746d491..022ac1fff 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -1,5 +1,7 @@ use uuid::Uuid; +use crate::state::State; + #[derive(Debug, Clone, Copy)] pub enum Kind { None, @@ -53,7 +55,7 @@ impl Matrix { #[derive(Debug, Clone)] pub struct Shape { pub id: Uuid, - pub shapes: Vec::, + pub shapes: Vec, pub kind: Kind, pub selrect: Rect, pub transform: Matrix,