Add support to stylus eraser

This commit is contained in:
Enrico Lumetti 2021-03-01 23:19:41 +01:00
parent 022377cb48
commit f330760c1c
4 changed files with 134 additions and 40 deletions

View File

@ -26,6 +26,8 @@ pub mod commands {
use druid::Selector; use druid::Selector;
pub const UPDATE_TOOL: Selector = Selector::new("stiletto_update_tool"); pub const UPDATE_TOOL: Selector = Selector::new("stiletto_update_tool");
pub const PUSH_ERASER: Selector<()> = Selector::new("push_eraser_context");
pub const POP_ERASER: Selector<()> = Selector::new("pop_eraser_context");
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

View File

@ -21,7 +21,9 @@ use log::{info, warn};
use druid::commands; use druid::commands;
use druid::im::vector; use druid::im::vector;
use druid::widget::prelude::*; use druid::widget::prelude::*;
use druid::widget::{Align, Button, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt}; use druid::widget::{
Align, Button, Controller, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt,
};
use druid::{ use druid::{
AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec, AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec,
Handled, Lens, Target, WindowDesc, Handled, Lens, Target, WindowDesc,
@ -70,6 +72,7 @@ pub fn main() {
}, },
], ],
current_tool: 0, current_tool: 0,
eraser_tool_id: 2,
current_file_path: None, current_file_path: None,
}; };
AppLauncher::with_window(window) AppLauncher::with_window(window)
@ -84,10 +87,24 @@ struct StilettoState {
canvas: CanvasState, canvas: CanvasState,
tool_icons: Vector<CanvasToolIconState>, tool_icons: Vector<CanvasToolIconState>,
current_tool: usize, current_tool: usize,
eraser_tool_id: usize,
#[data(same_fn = "PartialEq::eq")] #[data(same_fn = "PartialEq::eq")]
current_file_path: Option<PathBuf>, current_file_path: Option<PathBuf>,
} }
impl StilettoState {
pub fn set_tool(&mut self, new_tool: usize) {
let old_tool = self.current_tool;
info!("Changing active tool to: tool #{}", new_tool);
self.tool_icons.get_mut(old_tool).unwrap().selected = false;
self.tool_icons.get_mut(new_tool).unwrap().selected = true;
self.current_tool = new_tool;
self.canvas.set_tool_ctx(CanvasToolCtx::new(
self.tool_icons.get(new_tool).unwrap().tool_params.clone(),
));
}
}
fn build_ui() -> impl Widget<StilettoState> { fn build_ui() -> impl Widget<StilettoState> {
let history_buttons = Flex::row() let history_buttons = Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
@ -107,17 +124,19 @@ fn build_ui() -> impl Widget<StilettoState> {
let save_buttons = Flex::row() let save_buttons = Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
.with_child(Button::new("Save").on_click(move |ctx, data: &mut StilettoState, _| { .with_child(
if data.current_file_path.is_some() { Button::new("Save").on_click(move |ctx, data: &mut StilettoState, _| {
ctx.submit_command(Command::new(commands::SAVE_FILE, (), Target::Auto)); if data.current_file_path.is_some() {
} else { ctx.submit_command(Command::new(commands::SAVE_FILE, (), Target::Auto));
ctx.submit_command(Command::new( } else {
druid::commands::SHOW_SAVE_PANEL, ctx.submit_command(Command::new(
save_dialog_options_clone.clone(), druid::commands::SHOW_SAVE_PANEL,
Target::Auto, save_dialog_options_clone.clone(),
)) Target::Auto,
} ))
})) }
}),
)
.with_child(Button::new("Save As").on_click(move |ctx, _, _| { .with_child(Button::new("Save As").on_click(move |ctx, _, _| {
ctx.submit_command(Command::new( ctx.submit_command(Command::new(
druid::commands::SHOW_SAVE_PANEL, druid::commands::SHOW_SAVE_PANEL,
@ -141,7 +160,7 @@ fn build_ui() -> impl Widget<StilettoState> {
.padding((8.0, 0.0)) .padding((8.0, 0.0))
.on_click(|ctx, tool_state, _| { .on_click(|ctx, tool_state, _| {
tool_state.selected = true; tool_state.selected = true;
ctx.submit_command(stiletto::commands::UPDATE_TOOL); ctx.submit_notification(stiletto::commands::UPDATE_TOOL);
}) })
}) })
.horizontal() .horizontal()
@ -163,13 +182,60 @@ fn build_ui() -> impl Widget<StilettoState> {
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
.must_fill_main_axis(true) .must_fill_main_axis(true)
.with_child(SizedBox::new(Align::left(toolbar)).height(50.0)) .with_child(SizedBox::new(Align::left(toolbar)).height(50.0))
.with_flex_child((CanvasWidget {}).lens(StilettoState::canvas), 1.0) .with_flex_child((CanvasWidget).lens(StilettoState::canvas), 1.0)
.controller(ToolSwitcher::new())
}
struct ToolSwitcher {
saved_tool: usize,
}
impl ToolSwitcher {
pub fn new() -> Self {
ToolSwitcher { saved_tool: 0 }
}
}
impl<W: Widget<StilettoState>> Controller<StilettoState, W> for ToolSwitcher {
fn event(
&mut self,
child: &mut W,
ctx: &mut EventCtx,
event: &Event,
data: &mut StilettoState,
env: &Env,
) {
match event {
Event::Notification(cmd) => {
let new_tool = if cmd.get(stiletto::commands::PUSH_ERASER).is_some() {
self.saved_tool = data.current_tool;
Some(data.eraser_tool_id)
} else if cmd.get(stiletto::commands::POP_ERASER).is_some() {
Some(self.saved_tool)
} else if cmd.get(stiletto::commands::UPDATE_TOOL).is_some() {
let old_tool = data.current_tool;
data.tool_icons
.iter()
.enumerate()
.position(|(pos, x)| pos != old_tool && x.selected)
} else {
None
};
if let Some(new_tool) = new_tool {
data.set_tool(new_tool);
ctx.set_handled();
}
}
_ => {}
}
child.event(ctx, event, data, env);
}
} }
struct Delegate; struct Delegate;
impl AppDelegate<StilettoState> for Delegate { impl AppDelegate<StilettoState> for Delegate {
fn command( fn command(
&mut self, &mut self,
_ctx: &mut DelegateCtx, _ctx: &mut DelegateCtx,
@ -181,7 +247,8 @@ impl AppDelegate<StilettoState> for Delegate {
use std::fs::File; use std::fs::File;
if cmd.get(commands::SAVE_FILE_AS).is_some() || cmd.get(commands::SAVE_FILE).is_some() { if cmd.get(commands::SAVE_FILE_AS).is_some() || cmd.get(commands::SAVE_FILE).is_some() {
let mut path_buf = cmd.get(commands::SAVE_FILE_AS) let mut path_buf = cmd
.get(commands::SAVE_FILE_AS)
.map(|file_info| file_info.path().to_path_buf()) .map(|file_info| file_info.path().to_path_buf())
.or_else(|| data.current_file_path.as_ref().cloned()) .or_else(|| data.current_file_path.as_ref().cloned())
.unwrap(); .unwrap();
@ -220,22 +287,6 @@ impl AppDelegate<StilettoState> for Delegate {
} }
return Handled::Yes; return Handled::Yes;
} }
if cmd.get(stiletto::commands::UPDATE_TOOL).is_some() {
let old_tool = data.current_tool;
if let Some(new_tool) = data
.tool_icons
.iter()
.enumerate()
.position(|(pos, x)| pos != old_tool && x.selected)
{
info!("Changing active tool to: tool #{}", new_tool);
data.tool_icons.get_mut(old_tool).unwrap().selected = false;
data.current_tool = new_tool;
data.canvas.set_tool_ctx(CanvasToolCtx::new(
data.tool_icons.get(new_tool).unwrap().tool_params.clone(),
));
}
}
Handled::No Handled::No
} }
} }

View File

@ -91,8 +91,7 @@ impl CanvasToolCtx {
self.state = CanvasToolState::Erasing; self.state = CanvasToolState::Erasing;
} }
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => { (CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
let eraser_rect = let eraser_rect = druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
let old_elements = vcanvas.get().elements(); let old_elements = vcanvas.get().elements();
let mut new_elements = old_elements.clone(); let mut new_elements = old_elements.clone();
new_elements.retain(|elem| { new_elements.retain(|elem| {

View File

@ -22,19 +22,21 @@ use crate::tool::CanvasToolCtx;
use crate::DocumentSnapshot; use crate::DocumentSnapshot;
use druid::widget::prelude::*; use druid::widget::prelude::*;
use druid::{Color, Data, Env, Event}; use druid::{Color, Data, Env, Event, PointerType};
#[derive(Clone, Data)] #[derive(Clone, Data)]
pub struct CanvasState { pub struct CanvasState {
pub versioned_canvas: VersionedCanvas, versioned_canvas: VersionedCanvas,
pub tool_ctx: CanvasToolCtx, tool_ctx: CanvasToolCtx,
temporary_erasing: bool,
} }
impl CanvasState { impl CanvasState {
pub fn new(tool_ctx: CanvasToolCtx) -> Self { pub fn new(tool_ctx: CanvasToolCtx) -> Self {
CanvasState { CanvasState {
versioned_canvas: VersionedCanvas::new(Canvas::new()), versioned_canvas: VersionedCanvas::new(Canvas::new()),
tool_ctx, tool_ctx: tool_ctx,
temporary_erasing: true,
} }
} }
pub fn perform_undo(&mut self) { pub fn perform_undo(&mut self) {
@ -72,14 +74,54 @@ impl CanvasState {
pub fn set_tool_ctx(&mut self, ctx: CanvasToolCtx) { pub fn set_tool_ctx(&mut self, ctx: CanvasToolCtx) {
self.tool_ctx = ctx; self.tool_ctx = ctx;
} }
pub fn get_tool_ctx(&self) -> &CanvasToolCtx {
&self.tool_ctx
}
pub fn get_tool_ctx_mut(&mut self) -> &mut CanvasToolCtx {
&mut self.tool_ctx
}
pub fn handle_event(&mut self, mut ctx: &mut EventCtx, event: &Event, env: &Env) {
self.tool_ctx
.handle_event(ctx, event, &mut self.versioned_canvas, env);
}
} }
pub struct CanvasWidget; pub struct CanvasWidget;
impl Widget<CanvasState> for CanvasWidget { impl Widget<CanvasState> for CanvasWidget {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, env: &Env) { fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, env: &Env) {
data.tool_ctx ctx.request_focus();
.handle_event(ctx, event, &mut data.versioned_canvas, env); let mut toggle_eraser_event = false;
let mut enable_temporary_erasing = false;
match event {
Event::MouseDown(mouse_event) => {
toggle_eraser_event = true;
enable_temporary_erasing = mouse_event.pointer_type == PointerType::Eraser;
}
Event::MouseMove(mouse_event) => {
toggle_eraser_event = true;
enable_temporary_erasing = mouse_event.pointer_type == PointerType::Eraser;
}
Event::MouseUp(mouse_event) => {
toggle_eraser_event = true;
enable_temporary_erasing = mouse_event.pointer_type == PointerType::Eraser;
}
_ => {}
}
// TODO: the first eraser toggle is not handled
if toggle_eraser_event && data.temporary_erasing != enable_temporary_erasing {
if enable_temporary_erasing {
ctx.submit_notification(crate::commands::PUSH_ERASER);
} else {
ctx.submit_notification(crate::commands::POP_ERASER);
}
data.temporary_erasing = enable_temporary_erasing;
} else {
data.handle_event(ctx, event, env);
}
} }
fn lifecycle( fn lifecycle(