canvas-element-enum #3
|
|
@ -0,0 +1,38 @@
|
||||||
|
#[derive(Clone, druid::Data)]
|
||||||
|
pub struct Path {
|
||||||
|
pub kurbo_path: druid::kurbo::BezPath,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, druid::Data)]
|
||||||
|
pub enum CanvasElement {
|
||||||
|
Freehand {
|
||||||
|
path: Path,
|
||||||
|
thickness: f64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasElement {
|
||||||
|
pub fn bounding_box(&self) -> druid::Rect {
|
||||||
|
match self {
|
||||||
|
CanvasElement::Freehand{ path, thickness } => {
|
||||||
|
use druid::kurbo::Shape;
|
||||||
|
path.kurbo_path.bounding_box().inflate(*thickness, *thickness)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn draw(&self, ctx: &mut druid::PaintCtx) {
|
||||||
|
match self {
|
||||||
|
CanvasElement::Freehand{ path, thickness } => {
|
||||||
|
use druid::RenderContext;
|
||||||
|
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
||||||
|
ctx.stroke(&path.kurbo_path, &stroke_color, *thickness);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
||||||
|
match self {
|
||||||
|
CanvasElement::Freehand{ path, .. } => Some(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
65
src/main.rs
65
src/main.rs
|
|
@ -19,50 +19,6 @@ use druid::kurbo::BezPath;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
||||||
|
|
||||||
mod stiletto {
|
|
||||||
use druid::widget::prelude::*;
|
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
|
||||||
pub struct Path {
|
|
||||||
pub kurbo_path: druid::kurbo::BezPath,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
|
||||||
pub enum CanvasElement {
|
|
||||||
Freehand {
|
|
||||||
path: Path,
|
|
||||||
thickness: f64
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CanvasElement {
|
|
||||||
pub fn bounding_box(&self) -> druid::Rect {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand{ path, thickness } => {
|
|
||||||
use druid::kurbo::Shape;
|
|
||||||
path.kurbo_path.bounding_box().inflate(*thickness, *thickness)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn draw(&self, ctx: &mut druid::PaintCtx) {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand{ path, thickness } => {
|
|
||||||
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
|
||||||
ctx.stroke(&path.kurbo_path, &stroke_color, *thickness);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand{ path, .. } => Some(path),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CanvasWidget;
|
|
||||||
|
|
||||||
use stiletto::CanvasElement;
|
use stiletto::CanvasElement;
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
|
|
@ -77,6 +33,8 @@ impl CanvasData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct CanvasWidget;
|
||||||
|
|
||||||
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 {
|
||||||
|
|
@ -177,8 +135,25 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_ui() -> impl Widget<CanvasData> {
|
||||||
|
use druid::widget::{Align, Button, Flex, CrossAxisAlignment, SizedBox};
|
||||||
|
let toolbar = Flex::row()
|
||||||
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
|
.with_spacer(30.0)
|
||||||
|
.with_child(
|
||||||
|
Button::new("Undo"))
|
||||||
|
.with_child(
|
||||||
|
Button::new("Redo"));
|
||||||
|
|
||||||
|
Flex::column()
|
||||||
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
|
.must_fill_main_axis(true)
|
||||||
|
.with_child(SizedBox::new(Align::left(toolbar)).height(50.0))
|
||||||
|
.with_flex_child(CanvasWidget {}, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let window = WindowDesc::new(|| CanvasWidget {})
|
let window = WindowDesc::new(build_ui)
|
||||||
.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"),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue