Improve canvas API and fix erasing of multiple elements
This commit is contained in:
parent
57ef9e5e0e
commit
390de72aa2
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
use im::Vector;
|
use im::{vector, Vector};
|
||||||
|
|
||||||
use serde::de::{Deserializer, SeqAccess, Visitor};
|
use serde::de::{Deserializer, SeqAccess, Visitor};
|
||||||
use serde::ser::Serializer;
|
use serde::ser::Serializer;
|
||||||
|
|
@ -128,20 +128,43 @@ impl CanvasElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand { path, .. } => Some(path),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
#[derive(Clone, druid::Data)]
|
||||||
pub struct Canvas {
|
pub struct Canvas {
|
||||||
pub elements: Vector<CanvasElement>,
|
elements: Vector<CanvasElement>,
|
||||||
|
content_size: druid::Size,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Canvas {
|
impl Canvas {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Canvas {
|
||||||
|
elements: vector![],
|
||||||
|
content_size: druid::Size::new(0.0, 0.0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_elements(elements: Vector<CanvasElement>) -> Self {
|
||||||
|
let mut cv = Canvas::new();
|
||||||
|
for e in elements {
|
||||||
|
cv.add_element(e);
|
||||||
|
}
|
||||||
|
cv
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_element(&mut self, element: CanvasElement) {
|
||||||
|
self.content_size = self
|
||||||
|
.content_size
|
||||||
|
.to_rect()
|
||||||
|
.union(element.bounding_box())
|
||||||
|
.size();
|
||||||
|
self.elements.push_back(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn elements(&self) -> &Vector<CanvasElement> {
|
||||||
|
&self.elements
|
||||||
|
}
|
||||||
|
|
||||||
/// Find all CanvasElement that intersect with rect
|
/// Find all CanvasElement that intersect with rect
|
||||||
pub fn find_intersections(&self, rect: druid::Rect) -> Vec<usize> {
|
pub fn find_intersections(&self, rect: druid::Rect) -> Vec<usize> {
|
||||||
let mut found_elements = Vec::<usize>::new();
|
let mut found_elements = Vec::<usize>::new();
|
||||||
|
|
@ -157,6 +180,10 @@ impl Canvas {
|
||||||
|
|
||||||
found_elements
|
found_elements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn content_size(&self) -> druid::Size {
|
||||||
|
self.content_size
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Path {
|
impl Serialize for Path {
|
||||||
|
|
|
||||||
|
|
@ -49,12 +49,7 @@ pub fn main() {
|
||||||
color: Color::rgb(255.0, 0.0, 0.0),
|
color: Color::rgb(255.0, 0.0, 0.0),
|
||||||
};
|
};
|
||||||
let canvas_data = StilettoState {
|
let canvas_data = StilettoState {
|
||||||
canvas: CanvasState {
|
canvas: CanvasState::new(CanvasToolCtx::new(default_pen_params.clone())),
|
||||||
versioned_canvas: VersionedCanvas::new(Canvas {
|
|
||||||
elements: vector![],
|
|
||||||
}),
|
|
||||||
tool_ctx: CanvasToolCtx::new(default_pen_params.clone()),
|
|
||||||
},
|
|
||||||
tool_icons: vector![
|
tool_icons: vector![
|
||||||
CanvasToolIconState {
|
CanvasToolIconState {
|
||||||
tool_params: default_pen_params,
|
tool_params: default_pen_params,
|
||||||
|
|
|
||||||
42
src/tool.rs
42
src/tool.rs
|
|
@ -91,14 +91,22 @@ impl CanvasToolCtx {
|
||||||
self.state = CanvasToolState::Erasing;
|
self.state = CanvasToolState::Erasing;
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
||||||
let eraser_rect = druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
|
let eraser_rect =
|
||||||
let elements = vcanvas.get().find_intersections(eraser_rect);
|
druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
|
||||||
|
let old_elements = vcanvas.get().elements();
|
||||||
if !elements.is_empty() {
|
let mut new_elements = old_elements.clone();
|
||||||
vcanvas.update(|canvas: &mut Canvas| {
|
new_elements.retain(|elem| {
|
||||||
for i in elements {
|
// Check if the element intersects the eraser rect
|
||||||
canvas.elements.remove(i);
|
if elem.bounding_box().intersect(eraser_rect).area() > 0.0 {
|
||||||
|
if elem.intersects_rect(eraser_rect) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
if new_elements.len() != old_elements.len() {
|
||||||
|
vcanvas.update(|canvas: &mut Canvas| {
|
||||||
|
*canvas = Canvas::new_with_elements(new_elements);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -138,17 +146,27 @@ impl CanvasToolCtx {
|
||||||
},
|
},
|
||||||
Event::MouseMove(mouse_event),
|
Event::MouseMove(mouse_event),
|
||||||
) => {
|
) => {
|
||||||
current_path
|
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
||||||
.get_path_mut()
|
path.kurbo_path
|
||||||
.unwrap()
|
|
||||||
.kurbo_path
|
|
||||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
||||||
vcanvas.update(move |canvas: &mut Canvas| {
|
vcanvas.update(move |canvas: &mut Canvas| {
|
||||||
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
||||||
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
||||||
canvas.elements.push_back(current_path);
|
if let CanvasElement::Freehand {
|
||||||
|
mut path,
|
||||||
|
mut thickness,
|
||||||
|
stroke_color,
|
||||||
|
} = current_path
|
||||||
|
{
|
||||||
|
canvas.add_element(CanvasElement::Freehand {
|
||||||
|
path,
|
||||||
|
thickness,
|
||||||
|
stroke_color,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,12 @@ pub struct CanvasState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanvasState {
|
impl CanvasState {
|
||||||
|
pub fn new(tool_ctx: CanvasToolCtx) -> Self {
|
||||||
|
CanvasState {
|
||||||
|
versioned_canvas: VersionedCanvas::new(Canvas::new()),
|
||||||
|
tool_ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn perform_undo(&mut self) {
|
pub fn perform_undo(&mut self) {
|
||||||
//if !self.is_drawing() {
|
//if !self.is_drawing() {
|
||||||
self.versioned_canvas.undo();
|
self.versioned_canvas.undo();
|
||||||
|
|
@ -50,7 +56,7 @@ impl CanvasState {
|
||||||
canvas_elements: self
|
canvas_elements: self
|
||||||
.versioned_canvas
|
.versioned_canvas
|
||||||
.get()
|
.get()
|
||||||
.elements
|
.elements()
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect(),
|
.collect(),
|
||||||
|
|
@ -58,9 +64,9 @@ impl CanvasState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_from_snapshot(&mut self, snapshot: DocumentSnapshot) {
|
pub fn set_from_snapshot(&mut self, snapshot: DocumentSnapshot) {
|
||||||
self.versioned_canvas = VersionedCanvas::new(Canvas {
|
self.versioned_canvas = VersionedCanvas::new(Canvas::new_with_elements(Vector::from(
|
||||||
elements: Vector::from(snapshot.canvas_elements),
|
snapshot.canvas_elements,
|
||||||
});
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tool_ctx(&mut self, ctx: CanvasToolCtx) {
|
pub fn set_tool_ctx(&mut self, ctx: CanvasToolCtx) {
|
||||||
|
|
@ -128,11 +134,9 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
// (ctx.size() returns the size of the layout rect we're painting in)
|
// (ctx.size() returns the size of the layout rect we're painting in)
|
||||||
let size = ctx.size();
|
let size = ctx.size();
|
||||||
let rect = size.to_rect();
|
let rect = size.to_rect();
|
||||||
// Note: ctx also has a `clear` method, but that clears the whole context,
|
|
||||||
// and we only want to clear this widget's area.
|
|
||||||
ctx.fill(rect, &Color::WHITE);
|
|
||||||
|
|
||||||
for element in data.versioned_canvas.get().elements.iter() {
|
ctx.fill(rect, &Color::WHITE);
|
||||||
|
for element in data.versioned_canvas.get().elements().iter() {
|
||||||
element.draw(ctx);
|
element.draw(ctx);
|
||||||
}
|
}
|
||||||
if data.tool_ctx.needs_repaint() {
|
if data.tool_ctx.needs_repaint() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue