🐛 Fix transform matrix being carried to non-children shapes

This commit is contained in:
Belén Albeza 2024-11-13 10:56:00 +01:00
parent 03acfc2b3c
commit 49b4eabe8b
3 changed files with 42 additions and 24 deletions

View file

@ -49,7 +49,7 @@ pub unsafe extern "C" fn draw_all_shapes(zoom: f32, pan_x: f32, pan_y: f32) {
scale(zoom, zoom); scale(zoom, zoom);
translate(pan_x, pan_y); translate(pan_x, pan_y);
render::render_shape(state, Uuid::nil()); render::render_shape_tree(state, Uuid::nil());
flush(); flush();
} }

View file

@ -2,6 +2,7 @@ use skia_safe as skia;
use skia_safe::gpu::{self, gl::FramebufferInfo, DirectContext}; use skia_safe::gpu::{self, gl::FramebufferInfo, DirectContext};
use uuid::Uuid; use uuid::Uuid;
use crate::shapes::Shape;
use crate::state::State; use crate::state::State;
extern "C" { 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); 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(); 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( let r = skia::Rect::new(
shape.selrect.x1, shape.selrect.x1,
shape.selrect.y1, shape.selrect.y1,
@ -91,35 +108,34 @@ pub(crate) fn render_shape(state: &mut State, id: Uuid) {
shape.selrect.y2, 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 // Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc
let mut matrix = skia::Matrix::new_identity(); let mut matrix = skia::Matrix::new_identity();
let (translate_x, translate_y) = shape.translation(); 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(); 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(); let mut center = r.center();
matrix.post_translate(center); matrix.post_translate(center);
center.negate(); center.negate();
matrix.pre_translate(center); 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. { if skew_x != 0. {
color = skia::Color::BLACK; color = skia::Color::BLACK;
} }
render_rect(&mut state.render_state.surface, r, color); render_rect(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);
}
} }

View file

@ -1,5 +1,7 @@
use uuid::Uuid; use uuid::Uuid;
use crate::state::State;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Kind { pub enum Kind {
None, None,
@ -53,7 +55,7 @@ impl Matrix {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Shape { pub struct Shape {
pub id: Uuid, pub id: Uuid,
pub shapes: Vec::<Uuid>, pub shapes: Vec<Uuid>,
pub kind: Kind, pub kind: Kind,
pub selrect: Rect, pub selrect: Rect,
pub transform: Matrix, pub transform: Matrix,