diff --git a/render-wasm/src/debug.rs b/render-wasm/src/debug.rs index d5f0a17b2..ff427339c 100644 --- a/render-wasm/src/debug.rs +++ b/render-wasm/src/debug.rs @@ -1,2 +1 @@ pub const DEBUG_VISIBLE: u32 = 0x01; - diff --git a/render-wasm/src/images.rs b/render-wasm/src/images.rs index 827a8fd6b..bbf5d600c 100644 --- a/render-wasm/src/images.rs +++ b/render-wasm/src/images.rs @@ -1,3 +1,3 @@ use skia_safe as skia; -pub type Image = skia::Image; \ No newline at end of file +pub type Image = skia::Image; diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 58bdb0286..44ab0635d 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -1,11 +1,11 @@ +pub mod debug; +pub mod images; +pub mod math; pub mod render; pub mod shapes; pub mod state; pub mod utils; pub mod view; -pub mod math; -pub mod images; -pub mod debug; use skia_safe as skia; @@ -57,7 +57,7 @@ pub extern "C" fn reset_canvas() { } #[no_mangle] -pub extern "C" fn resize_canvas(width: i32, height: i32) { +pub extern "C" fn resize_canvas(width: i32, height: i32) { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); state.resize(width, height); } diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index f0d34cfee..fae639c3b 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -1,12 +1,12 @@ -use skia_safe as skia; use skia::gpu::{self, gl::FramebufferInfo, DirectContext}; +use skia_safe as skia; use std::collections::HashMap; use uuid::Uuid; +use crate::debug; +use crate::images::Image; use crate::shapes::Shape; use crate::view::Viewbox; -use crate::images::Image; -use crate::debug; struct GpuState { pub context: DirectContext, @@ -52,8 +52,8 @@ impl GpuState { } pub(crate) struct CachedSurfaceImage { - pub image: Image, - pub viewbox: Viewbox, + pub image: Image, + pub viewbox: Viewbox, } pub(crate) struct RenderState { @@ -154,7 +154,9 @@ impl RenderState { self.drawing_surface.canvas().concat(&matrix); for fill in shape.fills().rev() { - self.drawing_surface.canvas().draw_rect(shape.selrect, &fill.to_paint()); + self.drawing_surface + .canvas() + .draw_rect(shape.selrect, &fill.to_paint()); } let mut paint = skia::Paint::default(); @@ -170,36 +172,39 @@ impl RenderState { .clear(skia::Color::TRANSPARENT); } - pub fn navigate( - &mut self, - viewbox: &Viewbox, - shapes: &HashMap, - debug: u32, - ) { + pub fn navigate(&mut self, viewbox: &Viewbox, shapes: &HashMap, debug: u32) { self.reset_canvas(); if let Some(cached_surface_image) = &self.cached_surface_image { // If we are drawing something bigger than the visible let's do a redraw - if (viewbox.x > cached_surface_image.viewbox.x) || - (-viewbox.x + viewbox.area.width() > -cached_surface_image.viewbox.x + cached_surface_image.viewbox.area.width()) || - (viewbox.y > cached_surface_image.viewbox.y) || - (-viewbox.y + viewbox.area.height() > -cached_surface_image.viewbox.y + cached_surface_image.viewbox.area.height()) + if (viewbox.x > cached_surface_image.viewbox.x) + || (-viewbox.x + viewbox.area.width() + > -cached_surface_image.viewbox.x + cached_surface_image.viewbox.area.width()) + || (viewbox.y > cached_surface_image.viewbox.y) + || (-viewbox.y + viewbox.area.height() + > -cached_surface_image.viewbox.y + cached_surface_image.viewbox.area.height()) { self.render_all(viewbox, shapes, debug); - } - else - { + } else { let image = &cached_surface_image.image; let paint = skia::Paint::default(); self.final_surface.canvas().save(); self.drawing_surface.canvas().save(); let navigate_zoom = viewbox.zoom / cached_surface_image.viewbox.zoom; - let navigate_x = cached_surface_image.viewbox.zoom * (viewbox.x - cached_surface_image.viewbox.x); - let navigate_y = cached_surface_image.viewbox.zoom * (viewbox.y - cached_surface_image.viewbox.y); + let navigate_x = cached_surface_image.viewbox.zoom + * (viewbox.x - cached_surface_image.viewbox.x); + let navigate_y = cached_surface_image.viewbox.zoom + * (viewbox.y - cached_surface_image.viewbox.y); - self.final_surface.canvas().scale((navigate_zoom, navigate_zoom)); - self.final_surface.canvas().translate((navigate_x, navigate_y)); - self.final_surface.canvas().draw_image(image.clone(), (0, 0), Some(&paint)); + self.final_surface + .canvas() + .scale((navigate_zoom, navigate_zoom)); + self.final_surface + .canvas() + .translate((navigate_x, navigate_y)); + self.final_surface + .canvas() + .draw_image(image.clone(), (0, 0), Some(&paint)); self.final_surface.canvas().restore(); self.drawing_surface.canvas().restore(); @@ -247,13 +252,11 @@ impl RenderState { fn render_debug_shape(&mut self, shape: &Shape, intersected: bool) { let mut paint = skia::Paint::default(); paint.set_style(skia::PaintStyle::Stroke); - paint.set_color( - if intersected { - skia::Color::from_argb(255, 255, 255, 0) - } else { - skia::Color::from_argb(255, 0, 255, 255) - } - ); + paint.set_color(if intersected { + skia::Color::from_argb(255, 255, 255, 0) + } else { + skia::Color::from_argb(255, 0, 255, 255) + }); paint.set_stroke_width(1.); let mut scaled_rect = shape.selrect.clone(); let x = 100. + scaled_rect.x() * 0.2; @@ -273,7 +276,6 @@ impl RenderState { skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::Nearest), Some(&paint), ); - } fn render_shape_tree(&mut self, id: &Uuid, viewbox: &Viewbox, shapes: &HashMap) { diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 019f6f7e9..48c7e8712 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -1,5 +1,5 @@ -use skia_safe as skia; use crate::math; +use skia_safe as skia; use uuid::Uuid; #[derive(Debug, Clone, Copy)] diff --git a/render-wasm/src/state.rs b/render-wasm/src/state.rs index 2344fab5c..903ebdb6e 100644 --- a/render-wasm/src/state.rs +++ b/render-wasm/src/state.rs @@ -1,10 +1,10 @@ use std::collections::HashMap; use uuid::Uuid; +use crate::math; use crate::render::RenderState; use crate::shapes::Shape; use crate::view::Viewbox; -use crate::math; /// This struct holds the state of the Rust application between JS calls. /// @@ -35,7 +35,7 @@ impl<'a> State<'a> { width: width as f32, height: height as f32, area: math::Rect::new_empty(), - } + }, } } diff --git a/render-wasm/src/utils.rs b/render-wasm/src/utils.rs index 8c5b39419..75fb32d33 100644 --- a/render-wasm/src/utils.rs +++ b/render-wasm/src/utils.rs @@ -1,8 +1,7 @@ use uuid::Uuid; -pub fn uuid_from_u32_quartet(a: u32, b: u32, c: u32, d: u32) -> Uuid -{ - let hi: u64 = ((a as u64) << 32) | b as u64; - let lo: u64 = ((c as u64) << 32) | d as u64; - Uuid::from_u64_pair(hi, lo) +pub fn uuid_from_u32_quartet(a: u32, b: u32, c: u32, d: u32) -> Uuid { + let hi: u64 = ((a as u64) << 32) | b as u64; + let lo: u64 = ((c as u64) << 32) | d as u64; + Uuid::from_u64_pair(hi, lo) } diff --git a/render-wasm/src/view.rs b/render-wasm/src/view.rs index 5b671c7f8..0f1ab1193 100644 --- a/render-wasm/src/view.rs +++ b/render-wasm/src/view.rs @@ -1,55 +1,49 @@ use skia_safe as skia; #[derive(Debug, Copy, Clone)] -pub(crate) struct Viewbox -{ - pub x: f32, - pub y: f32, - pub width: f32, - pub height: f32, - pub zoom: f32, - pub area: skia::Rect, +pub(crate) struct Viewbox { + pub x: f32, + pub y: f32, + pub width: f32, + pub height: f32, + pub zoom: f32, + pub area: skia::Rect, } impl Viewbox { - pub fn set_all(&mut self, zoom: f32, x: f32, y: f32) -> &Self { - self.x = x; - self.y = y; - self.zoom = zoom; - self.area.set_xywh( - -self.x, - -self.y, - self.width / self.zoom, - self.height / self.zoom - ); - self - } + pub fn set_all(&mut self, zoom: f32, x: f32, y: f32) -> &Self { + self.x = x; + self.y = y; + self.zoom = zoom; + self.area.set_xywh( + -self.x, + -self.y, + self.width / self.zoom, + self.height / self.zoom, + ); + self + } - pub fn set_zoom(&mut self, zoom: f32) -> &Self { - self.zoom = zoom; - self.area.set_wh( - self.width / self.zoom, - self.height / self.zoom - ); - self - } + pub fn set_zoom(&mut self, zoom: f32) -> &Self { + self.zoom = zoom; + self.area + .set_wh(self.width / self.zoom, self.height / self.zoom); + self + } - pub fn set_xy(&mut self, x: f32, y: f32) -> &Self { - self.x = x; - self.y = y; - self.area.left = -x; - self.area.top = -y; - self - } + pub fn set_xy(&mut self, x: f32, y: f32) -> &Self { + self.x = x; + self.y = y; + self.area.left = -x; + self.area.top = -y; + self + } - pub fn set_wh(&mut self, width: f32, height: f32) -> &Self { - self.width = width; - self.height = height; - self.area.set_wh( - self.width / self.zoom, - self.height / self.zoom - ); - self - } + pub fn set_wh(&mut self, width: f32, height: f32) -> &Self { + self.width = width; + self.height = height; + self.area + .set_wh(self.width / self.zoom, self.height / self.zoom); + self + } } -