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