canvas-element-enum #3
30
src/main.rs
30
src/main.rs
|
|
@ -29,13 +29,13 @@ mod stiletto {
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
#[derive(Clone, druid::Data)]
|
||||||
pub enum CanvasElement {
|
pub enum CanvasElement {
|
||||||
Path(Path),
|
Freehand(Path),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanvasElement {
|
impl CanvasElement {
|
||||||
pub fn bounding_box(&self) -> druid::Rect {
|
pub fn bounding_box(&self) -> druid::Rect {
|
||||||
match self {
|
match self {
|
||||||
CanvasElement::Path(p) => {
|
CanvasElement::Freehand(p) => {
|
||||||
use druid::kurbo::Shape;
|
use druid::kurbo::Shape;
|
||||||
p.kurbo_path.bounding_box()
|
p.kurbo_path.bounding_box()
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +43,7 @@ mod stiletto {
|
||||||
}
|
}
|
||||||
pub fn draw(&self, ctx: &mut druid::PaintCtx) {
|
pub fn draw(&self, ctx: &mut druid::PaintCtx) {
|
||||||
match self {
|
match self {
|
||||||
CanvasElement::Path(p) => {
|
CanvasElement::Freehand(p) => {
|
||||||
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
||||||
ctx.stroke(&p.kurbo_path, &stroke_color, 2.0);
|
ctx.stroke(&p.kurbo_path, &stroke_color, 2.0);
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +52,7 @@ mod stiletto {
|
||||||
|
|
||||||
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
||||||
match self {
|
match self {
|
||||||
CanvasElement::Path(p) => Some(p),
|
CanvasElement::Freehand(p) => Some(p),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,20 +66,24 @@ use stiletto::CanvasElement;
|
||||||
struct CanvasData {
|
struct CanvasData {
|
||||||
current_element: Option<CanvasElement>,
|
current_element: Option<CanvasElement>,
|
||||||
elements: Vector<CanvasElement>,
|
elements: Vector<CanvasElement>,
|
||||||
is_drawing: bool,
|
}
|
||||||
|
|
||||||
|
impl CanvasData {
|
||||||
|
fn is_drawing(&self) -> bool {
|
||||||
|
self.current_element.is_some()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget<CanvasData> for CanvasWidget {
|
impl Widget<CanvasData> for CanvasWidget {
|
||||||
fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasData, _env: &Env) {
|
fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasData, _env: &Env) {
|
||||||
match event {
|
match event {
|
||||||
Event::MouseDown(mouse_event) => {
|
Event::MouseDown(mouse_event) => {
|
||||||
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));
|
||||||
data.current_element = Some(CanvasElement::Path(stiletto::Path { kurbo_path }));
|
data.current_element = Some(CanvasElement::Freehand(stiletto::Path { kurbo_path }));
|
||||||
}
|
}
|
||||||
Event::MouseMove(mouse_event) => {
|
Event::MouseMove(mouse_event) => {
|
||||||
if data.is_drawing {
|
if data.is_drawing() {
|
||||||
if let Some(current_element) = data.current_element.as_mut() {
|
if let Some(current_element) = data.current_element.as_mut() {
|
||||||
current_element
|
current_element
|
||||||
.get_path_mut()
|
.get_path_mut()
|
||||||
|
|
@ -90,10 +94,9 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::MouseUp(_) => {
|
Event::MouseUp(_) => {
|
||||||
if data.is_drawing {
|
if data.is_drawing() {
|
||||||
if let Some(current_element) = data.current_element.take() {
|
if let Some(current_element) = data.current_element.take() {
|
||||||
data.elements.push_back(current_element);
|
data.elements.push_back(current_element);
|
||||||
data.is_drawing = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,10 +121,10 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
// the current_element is moved to the elements array, no need to repaint
|
// the current_element is moved to the elements array, no need to repaint
|
||||||
if old_data.is_drawing && !data.is_drawing {
|
if old_data.is_drawing() && !data.is_drawing() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if data.is_drawing {
|
if data.is_drawing() {
|
||||||
data.current_element.as_ref().map(|e| {
|
data.current_element.as_ref().map(|e| {
|
||||||
ctx.request_paint_rect(e.bounding_box());
|
ctx.request_paint_rect(e.bounding_box());
|
||||||
});
|
});
|
||||||
|
|
@ -170,14 +173,13 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let window = WindowDesc::new(|| CanvasWidget {})
|
let window = WindowDesc::new(|| CanvasWidget {})
|
||||||
//.window_size((1024.0, 1400.0))
|
.window_size((1024.0, 1400.0))
|
||||||
.title(
|
.title(
|
||||||
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),
|
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),
|
||||||
);
|
);
|
||||||
let canvas_data = CanvasData {
|
let canvas_data = CanvasData {
|
||||||
current_element: None,
|
current_element: None,
|
||||||
elements: vector![],
|
elements: vector![],
|
||||||
is_drawing: false,
|
|
||||||
};
|
};
|
||||||
AppLauncher::with_window(window)
|
AppLauncher::with_window(window)
|
||||||
.use_simple_logger()
|
.use_simple_logger()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue