Almost working, but not quite
This commit is contained in:
parent
49f5cd7e4d
commit
0b486563f8
95
src/main.rs
95
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<dyn CanvasElement>;
|
||||
// fn same_box(&self, other: &Box<dyn CanvasElement>) -> bool;
|
||||
//}
|
||||
pub trait CanvasElementData {
|
||||
fn clone_box(&self) -> Box<dyn CanvasElement>;
|
||||
}
|
||||
|
||||
//impl<T> CanvasElementData for T
|
||||
//where
|
||||
// T: 'static + CanvasElement + druid::Data
|
||||
//{
|
||||
// fn clone_box(&self) -> Box<dyn CanvasElement> {
|
||||
// Box::new(self.clone())
|
||||
// }
|
||||
impl<T> CanvasElementData for T
|
||||
where
|
||||
T: 'static + CanvasElement + druid::Data
|
||||
{
|
||||
fn clone_box(&self) -> Box<dyn CanvasElement> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
impl Clone for Box<dyn CanvasElement> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
// fn same_box(&self, other: &Box<T>) -> bool {
|
||||
// self.same(other)
|
||||
// }
|
||||
//}
|
||||
|
||||
//impl druid::Data for Box<dyn CanvasElement> {
|
||||
// fn same(&self, other: &Self) -> bool {
|
||||
// return self.same(other);
|
||||
// }
|
||||
//}
|
||||
|
||||
//impl Clone for Box<dyn CanvasElement> {
|
||||
// fn clone(&self) -> Self {
|
||||
// self.clone_box()
|
||||
// }
|
||||
//}
|
||||
impl druid::Data for Box<dyn CanvasElement> {
|
||||
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<Rc<dyn stiletto::CanvasElement>>,
|
||||
current_element: Option<Box<dyn stiletto::CanvasElement>>,
|
||||
elements: Vector<Rc<Box<dyn stiletto::CanvasElement>>>,
|
||||
is_drawing: bool,
|
||||
}
|
||||
|
||||
impl Widget<CanvasData> 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,28 +118,28 @@ impl Widget<CanvasData> 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 {
|
||||
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,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue