mirror of
https://github.com/penpot/penpot.git
synced 2025-01-06 14:50:20 -05:00
💄 Format rust code
This commit is contained in:
parent
f7ff3129ed
commit
6fd6074934
8 changed files with 85 additions and 91 deletions
|
@ -1,2 +1 @@
|
|||
pub const DEBUG_VISIBLE: u32 = 0x01;
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
use skia_safe as skia;
|
||||
|
||||
pub type Image = skia::Image;
|
||||
pub type Image = skia::Image;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<Uuid, Shape>,
|
||||
debug: u32,
|
||||
) {
|
||||
pub fn navigate(&mut self, viewbox: &Viewbox, shapes: &HashMap<Uuid, Shape>, 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<Uuid, Shape>) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use skia_safe as skia;
|
||||
use crate::math;
|
||||
use skia_safe as skia;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
|
@ -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(),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue