Compare commits
3 Commits
59a4a6cfe8
...
b8e50acc52
| Author | SHA1 | Date |
|---|---|---|
|
|
b8e50acc52 | |
|
|
0fdc73b9e4 | |
|
|
a2cf104697 |
|
|
@ -31,10 +31,9 @@ use druid::{
|
|||
|
||||
use im::Vector;
|
||||
|
||||
use stiletto::tool::{CanvasToolCtx, CanvasToolParams};
|
||||
use stiletto::widget::{
|
||||
build_simple_tool_widget, CanvasState, CanvasToolIconState, CanvasWidget,
|
||||
};
|
||||
use stiletto::tool::CanvasToolParams;
|
||||
use stiletto::widget::tool_ctx::{CanvasToolCtx};
|
||||
use stiletto::widget::{build_simple_tool_widget, CanvasState, CanvasToolIconState, CanvasWidget};
|
||||
use stiletto::DocumentSnapshot;
|
||||
|
||||
pub fn main() {
|
||||
|
|
|
|||
170
src/tool.rs
170
src/tool.rs
|
|
@ -14,12 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use druid::kurbo::BezPath;
|
||||
use druid::{Affine, Color, Data, Env, Event, EventCtx, PaintCtx};
|
||||
|
||||
use crate::canvas;
|
||||
use crate::canvas::{Canvas, CanvasElement};
|
||||
use crate::history::VersionedCanvas;
|
||||
use druid::{Color, Data};
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
pub enum CanvasToolParams {
|
||||
|
|
@ -32,166 +27,3 @@ pub enum CanvasToolType {
|
|||
Pen,
|
||||
Eraser,
|
||||
}
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
pub enum CanvasToolState {
|
||||
Idle,
|
||||
DrawingFreehand {
|
||||
pen_params: CanvasToolParams,
|
||||
current_path: CanvasElement,
|
||||
},
|
||||
Erasing,
|
||||
}
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
pub struct CanvasToolCtx {
|
||||
initial_params: CanvasToolParams,
|
||||
state: CanvasToolState,
|
||||
}
|
||||
|
||||
impl CanvasToolParams {
|
||||
pub fn tool_type(&self) -> CanvasToolType {
|
||||
match self {
|
||||
CanvasToolParams::Pen { .. } => CanvasToolType::Pen,
|
||||
CanvasToolParams::Eraser => CanvasToolType::Eraser,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasToolCtx {
|
||||
pub fn new(params: CanvasToolParams) -> Self {
|
||||
CanvasToolCtx {
|
||||
initial_params: params,
|
||||
state: CanvasToolState::Idle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_event(
|
||||
&mut self,
|
||||
mut ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
mut vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
env: &Env,
|
||||
) {
|
||||
match self.initial_params.tool_type() {
|
||||
CanvasToolType::Pen => self.handle_pen_event(ctx, event, vcanvas, transform, env),
|
||||
CanvasToolType::Eraser => self.handle_erase_event(ctx, event, vcanvas, transform, env),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_erase_event(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
_env: &Env,
|
||||
) {
|
||||
match (&mut self.state, event) {
|
||||
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
||||
self.state = CanvasToolState::Erasing;
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
||||
let eraser_rect =
|
||||
druid::Rect::from_center_size(transform * mouse_event.pos, (5.0, 5.0));
|
||||
let old_elements = vcanvas.get().elements();
|
||||
let mut new_elements = old_elements.clone();
|
||||
new_elements.retain(|elem| {
|
||||
// Check if the element intersects the eraser rect
|
||||
if elem.bounding_box().intersect(eraser_rect).area() > 0.0 {
|
||||
if elem.intersects_rect(eraser_rect) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
});
|
||||
if new_elements.len() != old_elements.len() {
|
||||
vcanvas.update(|canvas: &mut Canvas| {
|
||||
*canvas = Canvas::new_with_elements(new_elements);
|
||||
});
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
||||
self.state = CanvasToolState::Idle;
|
||||
ctx.set_handled();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_pen_event(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
_env: &Env,
|
||||
) {
|
||||
match (&mut self.state, event) {
|
||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) => {
|
||||
let mut kurbo_path = BezPath::new();
|
||||
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||
if let CanvasToolParams::Pen { thickness, color } = &self.initial_params {
|
||||
self.state = CanvasToolState::DrawingFreehand {
|
||||
pen_params: self.initial_params.clone(),
|
||||
current_path: CanvasElement::Freehand {
|
||||
path: canvas::Path { kurbo_path },
|
||||
thickness: *thickness,
|
||||
stroke_color: color.clone(),
|
||||
},
|
||||
};
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(
|
||||
CanvasToolState::DrawingFreehand {
|
||||
ref mut current_path,
|
||||
..
|
||||
},
|
||||
Event::MouseMove(mouse_event),
|
||||
) => {
|
||||
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
||||
path.kurbo_path
|
||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
||||
vcanvas.update(move |canvas: &mut Canvas| {
|
||||
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
||||
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
||||
if let CanvasElement::Freehand {
|
||||
mut path,
|
||||
mut thickness,
|
||||
stroke_color,
|
||||
} = current_path
|
||||
{
|
||||
path.kurbo_path.apply_affine(transform);
|
||||
canvas.add_element(CanvasElement::Freehand {
|
||||
path,
|
||||
thickness,
|
||||
stroke_color,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
ctx.set_handled();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn needs_repaint(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn paint(&self, ctx: &mut PaintCtx, _env: &Env) {
|
||||
match &self.state {
|
||||
CanvasToolState::DrawingFreehand { current_path, .. } => current_path.draw(ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@
|
|||
|
||||
use im::Vector;
|
||||
|
||||
use super::tool_ctx::{CanvasToolCtx};
|
||||
use crate::canvas::Canvas;
|
||||
use crate::history::VersionedCanvas;
|
||||
use crate::tool::CanvasToolCtx;
|
||||
use crate::DocumentSnapshot;
|
||||
|
||||
use druid::scroll_component::ScrollComponent;
|
||||
|
|
@ -86,7 +86,7 @@ impl CanvasState {
|
|||
&mut self.tool_ctx
|
||||
}
|
||||
|
||||
pub fn handle_event(&mut self, mut ctx: &mut EventCtx, event: &Event, transform: Affine, env: &Env) {
|
||||
pub fn handle_event(&mut self, ctx: &mut EventCtx, event: &Event, transform: Affine, env: &Env) {
|
||||
self.tool_ctx
|
||||
.handle_event(ctx, event, &mut self.versioned_canvas, transform, env);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
pub mod canvas;
|
||||
pub mod tool_icon;
|
||||
pub mod tool_ctx;
|
||||
|
||||
pub use canvas::*;
|
||||
pub use tool_icon::*;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,190 @@
|
|||
// Stiletto
|
||||
// Copyright (C) 2020 Stiletto Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::tool::{CanvasToolParams, CanvasToolType};
|
||||
use crate::canvas::{Canvas, CanvasElement};
|
||||
use crate::history::VersionedCanvas;
|
||||
|
||||
use druid::kurbo::BezPath;
|
||||
use druid::{Affine, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx};
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
pub enum CanvasToolState {
|
||||
Idle,
|
||||
DrawingFreehand {
|
||||
pen_params: CanvasToolParams,
|
||||
current_path: CanvasElement,
|
||||
},
|
||||
Erasing,
|
||||
}
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
pub struct CanvasToolCtx {
|
||||
initial_params: CanvasToolParams,
|
||||
state: CanvasToolState,
|
||||
}
|
||||
|
||||
impl CanvasToolParams {
|
||||
pub fn tool_type(&self) -> CanvasToolType {
|
||||
match self {
|
||||
CanvasToolParams::Pen { .. } => CanvasToolType::Pen,
|
||||
CanvasToolParams::Eraser => CanvasToolType::Eraser,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pressed(mouse_event: &MouseEvent) -> bool {
|
||||
mouse_event.buttons.contains(MouseButton::Left)
|
||||
|| mouse_event.button == MouseButton::Left
|
||||
}
|
||||
|
||||
impl CanvasToolCtx {
|
||||
pub fn new(params: CanvasToolParams) -> Self {
|
||||
CanvasToolCtx {
|
||||
initial_params: params,
|
||||
state: CanvasToolState::Idle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_event(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
env: &Env,
|
||||
) {
|
||||
match self.initial_params.tool_type() {
|
||||
CanvasToolType::Pen => self.handle_pen_event(ctx, event, vcanvas, transform, env),
|
||||
CanvasToolType::Eraser => self.handle_erase_event(ctx, event, vcanvas, transform, env),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_erase_event(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
_env: &Env,
|
||||
) {
|
||||
match (&mut self.state, event) {
|
||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
||||
self.state = CanvasToolState::Erasing;
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) if pressed(mouse_event) => {
|
||||
let eraser_rect =
|
||||
druid::Rect::from_center_size(transform * mouse_event.pos, (5.0, 5.0));
|
||||
let old_elements = vcanvas.get().elements();
|
||||
let mut new_elements = old_elements.clone();
|
||||
new_elements.retain(|elem| {
|
||||
// Check if the element intersects the eraser rect
|
||||
if elem.bounding_box().intersect(eraser_rect).area() > 0.0 {
|
||||
if elem.intersects_rect(eraser_rect) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if new_elements.len() != old_elements.len() {
|
||||
vcanvas.update(|canvas: &mut Canvas| {
|
||||
*canvas = Canvas::new_with_elements(new_elements);
|
||||
});
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::Erasing, Event::MouseUp(mouse_event)) if pressed(mouse_event) => {
|
||||
self.state = CanvasToolState::Idle;
|
||||
ctx.set_handled();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_pen_event(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
_env: &Env,
|
||||
) {
|
||||
match (&mut self.state, event) {
|
||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
||||
let mut kurbo_path = BezPath::new();
|
||||
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||
if let CanvasToolParams::Pen { thickness, color } = &self.initial_params {
|
||||
self.state = CanvasToolState::DrawingFreehand {
|
||||
pen_params: self.initial_params.clone(),
|
||||
current_path: CanvasElement::Freehand {
|
||||
path: crate::canvas::Path { kurbo_path },
|
||||
thickness: *thickness,
|
||||
stroke_color: color.clone(),
|
||||
},
|
||||
};
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(
|
||||
CanvasToolState::DrawingFreehand {
|
||||
ref mut current_path,
|
||||
..
|
||||
},
|
||||
Event::MouseMove(mouse_event),
|
||||
) => if pressed(mouse_event) {
|
||||
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
||||
path.kurbo_path
|
||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(mouse_event)) if pressed(mouse_event) => {
|
||||
vcanvas.update(move |canvas: &mut Canvas| {
|
||||
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
||||
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
||||
if let CanvasElement::Freehand {
|
||||
mut path,
|
||||
thickness,
|
||||
stroke_color,
|
||||
} = current_path
|
||||
{
|
||||
path.kurbo_path.apply_affine(transform);
|
||||
canvas.add_element(CanvasElement::Freehand {
|
||||
path,
|
||||
thickness,
|
||||
stroke_color,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
ctx.set_handled();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn needs_repaint(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn paint(&self, ctx: &mut PaintCtx, _env: &Env) {
|
||||
match &self.state {
|
||||
CanvasToolState::DrawingFreehand { current_path, .. } => current_path.draw(ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue