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 {
|
mod stiletto {
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
pub trait CanvasElement {
|
pub trait CanvasElement: CanvasElementData {
|
||||||
//: CanvasElementData {
|
//: CanvasElementData {
|
||||||
/// returns the axis-aligned bounding box
|
/// returns the axis-aligned bounding box
|
||||||
fn bounding_box(&self) -> druid::Rect;
|
fn bounding_box(&self) -> druid::Rect;
|
||||||
|
|
||||||
fn draw(&self, ctx: &mut druid::PaintCtx);
|
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
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//pub trait CanvasElementData {
|
pub trait CanvasElementData {
|
||||||
// fn clone_box(&self) -> Box<dyn CanvasElement>;
|
fn clone_box(&self) -> Box<dyn CanvasElement>;
|
||||||
// fn same_box(&self, other: &Box<dyn CanvasElement>) -> bool;
|
}
|
||||||
//}
|
|
||||||
|
|
||||||
//impl<T> CanvasElementData for T
|
impl<T> CanvasElementData for T
|
||||||
//where
|
where
|
||||||
// T: 'static + CanvasElement + druid::Data
|
T: 'static + CanvasElement + druid::Data
|
||||||
//{
|
{
|
||||||
// fn clone_box(&self) -> Box<dyn CanvasElement> {
|
fn clone_box(&self) -> Box<dyn CanvasElement> {
|
||||||
// Box::new(self.clone())
|
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 {
|
impl druid::Data for Box<dyn CanvasElement> {
|
||||||
// self.same(other)
|
fn same(&self, other: &Self) -> bool {
|
||||||
// }
|
if let (Some(p1), Some(p2)) = (self.get_path(), other.get_path())
|
||||||
//}
|
{
|
||||||
|
p1.same(p2)
|
||||||
//impl druid::Data for Box<dyn CanvasElement> {
|
} else {
|
||||||
// fn same(&self, other: &Self) -> bool {
|
false
|
||||||
// return self.same(other);
|
}
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
|
|
||||||
//impl Clone for Box<dyn CanvasElement> {
|
|
||||||
// fn clone(&self) -> Self {
|
|
||||||
// self.clone_box()
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
#[derive(Clone, druid::Data)]
|
||||||
pub struct CanvasPath {
|
pub struct CanvasPath {
|
||||||
|
|
@ -72,17 +75,21 @@ mod stiletto {
|
||||||
impl CanvasElement for CanvasPath {
|
impl CanvasElement for CanvasPath {
|
||||||
fn bounding_box(&self) -> druid::Rect {
|
fn bounding_box(&self) -> druid::Rect {
|
||||||
use druid::kurbo::Shape;
|
use druid::kurbo::Shape;
|
||||||
return self.kurbo_path.bounding_box();
|
self.kurbo_path.bounding_box()
|
||||||
}
|
|
||||||
|
|
||||||
fn get_path(&mut self) -> Option<&mut CanvasPath> {
|
|
||||||
Some(self)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&self, ctx: &mut druid::PaintCtx) {
|
fn draw(&self, ctx: &mut druid::PaintCtx) {
|
||||||
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
||||||
ctx.stroke(&self.kurbo_path, &stroke_color, 2.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)]
|
#[derive(Clone, Data)]
|
||||||
struct CanvasData {
|
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,
|
is_drawing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget<CanvasData> for CanvasWidget {
|
impl Widget<CanvasData> for CanvasWidget {
|
||||||
fn event<'a>(
|
fn event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &mut EventCtx,
|
_ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
data: &'a mut CanvasData,
|
data: &mut CanvasData,
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match event {
|
match event {
|
||||||
|
|
@ -110,28 +118,28 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
data.is_drawing = true;
|
data.is_drawing = true;
|
||||||
let mut kurbo_path = BezPath::new();
|
let mut kurbo_path = BezPath::new();
|
||||||
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
let new_path = Rc::new(stiletto::CanvasPath { kurbo_path });
|
data.current_element = Some(Box::new(stiletto::CanvasPath { kurbo_path } ));
|
||||||
data.elements.push_back(new_path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::MouseMove(mouse_event) => {
|
Event::MouseMove(mouse_event) => {
|
||||||
if data.is_drawing
|
if data.is_drawing
|
||||||
/*&& (mouse_event.pointer_type == druid::PointerType::Stylus || mouse_event.pointer_type == druid::PointerType::Mouse)*/
|
/*&& (mouse_event.pointer_type == druid::PointerType::Stylus || mouse_event.pointer_type == druid::PointerType::Mouse)*/
|
||||||
{
|
{
|
||||||
let ptr: &'a mut dyn stiletto::CanvasElement =
|
let current_path = data.current_element.as_mut().unwrap().get_path_mut().unwrap();
|
||||||
Rc::get_mut(data.elements.back_mut().unwrap()).unwrap();
|
|
||||||
let last_canvas_path: &'a mut stiletto::CanvasPath = ptr.get_path().unwrap();
|
|
||||||
|
|
||||||
last_canvas_path
|
current_path
|
||||||
.kurbo_path
|
.kurbo_path
|
||||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::MouseUp(_) => {
|
Event::MouseUp(_) => {
|
||||||
if data.is_drawing {
|
if data.is_drawing {
|
||||||
|
if let Some(b) = data.current_element.as_mut() {
|
||||||
|
data.elements.push_back(Rc::new(b));
|
||||||
data.is_drawing = false;
|
data.is_drawing = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -198,6 +206,7 @@ pub fn main() {
|
||||||
.with_placeholder("Fancy Colors"),
|
.with_placeholder("Fancy Colors"),
|
||||||
);
|
);
|
||||||
let canvas_data = CanvasData {
|
let canvas_data = CanvasData {
|
||||||
|
current_element: None,
|
||||||
elements: vector![],
|
elements: vector![],
|
||||||
is_drawing: false,
|
is_drawing: false,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue