🎉 Render wasm ellipses support

This commit is contained in:
Alejandro Alonso 2024-12-09 11:45:43 +01:00 committed by Belén Albeza
parent 33e70a4108
commit 307329cf2e
6 changed files with 67 additions and 7 deletions

View file

@ -7,6 +7,8 @@ mod state;
mod utils;
mod view;
use crate::shapes::Kind;
use crate::shapes::Path;
use skia_safe as skia;
use crate::state::State;
@ -102,6 +104,33 @@ pub extern "C" fn use_shape(a: u32, b: u32, c: u32, d: u32) {
state.use_shape(id);
}
#[no_mangle]
pub unsafe extern "C" fn set_shape_kind_circle() {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
if let Some(shape) = state.current_shape() {
shape.kind = Kind::Circle(math::Rect::new_empty());
}
}
#[no_mangle]
pub unsafe extern "C" fn set_shape_kind_rect() {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
if let Some(shape) = state.current_shape() {
shape.kind = Kind::Rect(math::Rect::new_empty());
}
}
#[no_mangle]
pub unsafe extern "C" fn set_shape_kind_path() {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
if let Some(shape) = state.current_shape() {
let p = Path::try_from(Vec::new()).unwrap();
shape.kind = Kind::Path(p);
}
}
#[no_mangle]
pub extern "C" fn set_shape_selrect(left: f32, top: f32, right: f32, bottom: f32) {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");

View file

@ -300,6 +300,11 @@ impl RenderState {
.canvas()
.draw_rect(rect, &fill.to_paint(&selrect));
}
(_, Kind::Circle(rect)) => {
self.drawing_surface
.canvas()
.draw_oval(rect, &fill.to_paint(&selrect));
}
(_, Kind::Path(path)) => {
self.drawing_surface
.canvas()

View file

@ -14,6 +14,7 @@ pub use paths::*;
#[derive(Debug, Clone, PartialEq)]
pub enum Kind {
Rect(math::Rect),
Circle(math::Rect),
Path(Path),
}
@ -77,9 +78,15 @@ impl Shape {
pub fn set_selrect(&mut self, left: f32, top: f32, right: f32, bottom: f32) {
self.selrect.set_ltrb(left, top, right, bottom);
if let Kind::Rect(_) = self.kind {
self.kind = Kind::Rect(self.selrect.to_owned());
}
match self.kind {
Kind::Rect(_) => {
self.kind = Kind::Rect(self.selrect.to_owned());
}
Kind::Circle(_) => {
self.kind = Kind::Circle(self.selrect.to_owned());
}
_ => {}
};
}
pub fn translation(&self) -> (f32, f32) {

View file

@ -1,10 +1,9 @@
use crate::math;
use crate::shapes::Kind;
use skia_safe as skia;
pub type Image = skia::Image;
use crate::shapes::Kind;
pub fn draw_image_in_container(
canvas: &skia::Canvas,
image: &Image,
@ -18,6 +17,7 @@ pub fn draw_image_in_container(
let container = match kind {
Kind::Rect(r) => r.to_owned(),
Kind::Circle(r) => r.to_owned(),
Kind::Path(p) => p.to_skia_path().bounds().to_owned(),
};
@ -53,6 +53,11 @@ pub fn draw_image_in_container(
Kind::Rect(_) => {
canvas.clip_rect(container, skia::ClipOp::Intersect, true);
}
Kind::Circle(_) => {
let mut oval_path = skia::Path::new();
oval_path.add_oval(container, None);
canvas.clip_path(&oval_path, skia::ClipOp::Intersect, true);
}
Kind::Path(p) => {
canvas.clip_path(&p.to_skia_path(), skia::ClipOp::Intersect, true);
}