From 0b486563f8b2a0a2739a742b7d3702ae8deec30f Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Sun, 8 Nov 2020 11:47:26 +0100 Subject: [PATCH] Almost working, but not quite --- src/main.rs | 97 +++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index e5b9aab..f65cfff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,47 +22,50 @@ use std::rc::Rc; mod stiletto { use druid::widget::prelude::*; - pub trait CanvasElement { + pub trait CanvasElement: CanvasElementData { //: CanvasElementData { /// returns the axis-aligned bounding box fn bounding_box(&self) -> druid::Rect; fn draw(&self, ctx: &mut druid::PaintCtx); - fn get_path(&mut self) -> Option<&mut CanvasPath> { + fn get_path(&self) -> Option<&CanvasPath> { + None + } + + fn get_path_mut(&mut self) -> Option<&mut CanvasPath> { None } } - //pub trait CanvasElementData { - // fn clone_box(&self) -> Box; - // fn same_box(&self, other: &Box) -> bool; - //} + pub trait CanvasElementData { + fn clone_box(&self) -> Box; + } - //impl CanvasElementData for T - //where - // T: 'static + CanvasElement + druid::Data - //{ - // fn clone_box(&self) -> Box { - // Box::new(self.clone()) - // } + impl CanvasElementData for T + where + T: 'static + CanvasElement + druid::Data + { + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + } + impl Clone for Box { + fn clone(&self) -> Self { + self.clone_box() + } + } - // fn same_box(&self, other: &Box) -> bool { - // self.same(other) - // } - //} - - //impl druid::Data for Box { - // fn same(&self, other: &Self) -> bool { - // return self.same(other); - // } - //} - - //impl Clone for Box { - // fn clone(&self) -> Self { - // self.clone_box() - // } - //} + impl druid::Data for Box { + fn same(&self, other: &Self) -> bool { + if let (Some(p1), Some(p2)) = (self.get_path(), other.get_path()) + { + p1.same(p2) + } else { + false + } + } + } #[derive(Clone, druid::Data)] pub struct CanvasPath { @@ -72,17 +75,21 @@ mod stiletto { impl CanvasElement for CanvasPath { fn bounding_box(&self) -> druid::Rect { use druid::kurbo::Shape; - return self.kurbo_path.bounding_box(); - } - - fn get_path(&mut self) -> Option<&mut CanvasPath> { - Some(self) + self.kurbo_path.bounding_box() } fn draw(&self, ctx: &mut druid::PaintCtx) { let stroke_color = druid::Color::rgb8(0, 128, 0); ctx.stroke(&self.kurbo_path, &stroke_color, 2.0); } + + fn get_path(&self) -> Option<&CanvasPath> { + None + } + + fn get_path_mut(&mut self) -> Option<&mut CanvasPath> { + Some(self) + } } } @@ -90,16 +97,17 @@ struct CanvasWidget; #[derive(Clone, Data)] struct CanvasData { - elements: Vector>, + current_element: Option>, + elements: Vector>>, is_drawing: bool, } impl Widget for CanvasWidget { - fn event<'a>( + fn event( &mut self, _ctx: &mut EventCtx, event: &Event, - data: &'a mut CanvasData, + data: &mut CanvasData, _env: &Env, ) { match event { @@ -110,26 +118,26 @@ impl Widget for CanvasWidget { data.is_drawing = true; let mut kurbo_path = BezPath::new(); kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y)); - let new_path = Rc::new(stiletto::CanvasPath { kurbo_path }); - data.elements.push_back(new_path); + data.current_element = Some(Box::new(stiletto::CanvasPath { kurbo_path } )); } } Event::MouseMove(mouse_event) => { if data.is_drawing /*&& (mouse_event.pointer_type == druid::PointerType::Stylus || mouse_event.pointer_type == druid::PointerType::Mouse)*/ { - let ptr: &'a mut dyn stiletto::CanvasElement = - Rc::get_mut(data.elements.back_mut().unwrap()).unwrap(); - let last_canvas_path: &'a mut stiletto::CanvasPath = ptr.get_path().unwrap(); + let current_path = data.current_element.as_mut().unwrap().get_path_mut().unwrap(); - last_canvas_path + current_path .kurbo_path .line_to((mouse_event.pos.x, mouse_event.pos.y)); } } Event::MouseUp(_) => { if data.is_drawing { - data.is_drawing = false; + if let Some(b) = data.current_element.as_mut() { + data.elements.push_back(Rc::new(b)); + data.is_drawing = false; + } } } _ => {} @@ -198,6 +206,7 @@ pub fn main() { .with_placeholder("Fancy Colors"), ); let canvas_data = CanvasData { + current_element: None, elements: vector![], is_drawing: false, };