Update to druid's pointer events branch #41
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
13
Cargo.toml
|
|
@ -7,25 +7,26 @@ default-run = "stiletto"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
druid = { version = "0.7.0", features = ["im", "svg"] }
|
druid = { version = "0.7", features = ["im", "svg"] }
|
||||||
im = { version = "*" }
|
im = { version = "*" }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_bare = "0.3.0"
|
serde_bare = "0.3.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
clap = "3.0.0-beta.2"
|
clap = "3.1"
|
||||||
|
|
||||||
[target.'cfg(target_os="linux")'.dependencies.gtk]
|
[target.'cfg(target_os="linux")'.dependencies.gtk]
|
||||||
version = "0.9.2"
|
version = "0.14"
|
||||||
features = ["v3_22"]
|
features = ["v3_22"]
|
||||||
|
|
||||||
[target.'cfg(target_os="linux")'.dependencies.gio]
|
[target.'cfg(target_os="linux")'.dependencies.gio]
|
||||||
version = "0.9.1"
|
version = "0.14"
|
||||||
features = ["v2_56"]
|
features = ["v2_56"]
|
||||||
|
|
||||||
[target.'cfg(target_os="linux")'.dependencies.gdk]
|
[target.'cfg(target_os="linux")'.dependencies.gdk]
|
||||||
version = "0.13.2"
|
version = "0.14"
|
||||||
features = ["v3_22"]
|
features = ["v3_22"]
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
druid = { git = "https://github.com/jneem/druid", branch = "pointer", features = ["im", "svg"] }
|
#druid = { git = "https://github.com/jneem/druid", branch = "pointer", features = ["im", "svg"] }
|
||||||
|
druid = { path= "../druid/druid/" }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,35 +14,39 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use clap::{Arg, App};
|
use clap::{Arg, Command};
|
||||||
use stiletto::migration::open_stiletto_document;
|
use stiletto::migration::open_stiletto_document;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new("Stiletto Migration CLI")
|
let matches = Command::new("Stiletto Migration CLI")
|
||||||
.version("0.1.0")
|
.version("0.1.0")
|
||||||
.author("Stiletto Authors")
|
.author("Stiletto Authors")
|
||||||
.about("Migrate a stlt file to the latest version")
|
.about("Migrate a stlt file to the latest version")
|
||||||
.arg(Arg::new("INPUT")
|
.arg(
|
||||||
.about("Sets the input file to use")
|
Arg::new("INPUT")
|
||||||
.required(true)
|
.help("Sets the input file to use")
|
||||||
.index(1))
|
.required(true)
|
||||||
.arg(Arg::new("OUTPUT")
|
.index(1),
|
||||||
.about("Sets the output file to use")
|
)
|
||||||
.required(true)
|
.arg(
|
||||||
.index(2))
|
Arg::new("OUTPUT")
|
||||||
|
.help("Sets the output file to use")
|
||||||
|
.required(true)
|
||||||
|
.index(2),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let input_path = matches.value_of("INPUT").unwrap();
|
let input_path = matches.value_of("INPUT").unwrap();
|
||||||
let output_path = matches.value_of("OUTPUT").unwrap();
|
let output_path = matches.value_of("OUTPUT").unwrap();
|
||||||
|
|
||||||
let out_file = File::create(&output_path).expect("Cannot create output fle");
|
let out_file = File::create(&output_path).expect("Cannot create output fle");
|
||||||
|
|
||||||
let result = open_stiletto_document(&PathBuf::from(&input_path));
|
let result = open_stiletto_document(&PathBuf::from(&input_path));
|
||||||
if let Ok(document) = result {
|
if let Ok(document) = result {
|
||||||
let snapshot = document.migrate_to_latest();
|
let snapshot = document.migrate_to_latest();
|
||||||
snapshot.to_writer(out_file);
|
snapshot.to_writer(out_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
use druid::Color;
|
|
||||||
use im::{vector, Vector};
|
use im::{vector, Vector};
|
||||||
|
|
||||||
use serde::de::{Deserializer, SeqAccess, Visitor};
|
use serde::de::{Deserializer, SeqAccess, Visitor};
|
||||||
|
|
@ -81,19 +80,24 @@ pub enum CanvasElement {
|
||||||
Freehand {
|
Freehand {
|
||||||
path: Path,
|
path: Path,
|
||||||
thickness: f64,
|
thickness: f64,
|
||||||
#[serde(serialize_with = "serialize_color", deserialize_with = "deserialize_color")]
|
#[serde(
|
||||||
|
serialize_with = "serialize_color",
|
||||||
|
deserialize_with = "deserialize_color"
|
||||||
|
)]
|
||||||
stroke_color: druid::Color,
|
stroke_color: druid::Color,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_color<S>(c: &druid::Color, ser: S) -> Result<S::Ok, S::Error>
|
fn serialize_color<S>(c: &druid::Color, ser: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer
|
where
|
||||||
|
S: Serializer,
|
||||||
{
|
{
|
||||||
ser.serialize_u32(c.as_rgba_u32())
|
ser.serialize_u32(c.as_rgba_u32())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_color<'de, D>(de: D) -> Result<druid::Color, D::Error>
|
fn deserialize_color<'de, D>(de: D) -> Result<druid::Color, D::Error>
|
||||||
where D: Deserializer<'de>
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let c = u32::deserialize(de)?;
|
let c = u32::deserialize(de)?;
|
||||||
Ok(druid::Color::from_rgba32_u32(c))
|
Ok(druid::Color::from_rgba32_u32(c))
|
||||||
|
|
@ -201,11 +205,16 @@ impl Serialize for Path {
|
||||||
{
|
{
|
||||||
use druid::kurbo::PathEl;
|
use druid::kurbo::PathEl;
|
||||||
|
|
||||||
serializer.collect_seq(self.kurbo_path.iter().filter_map(|path_el| match path_el {
|
serializer.collect_seq(
|
||||||
PathEl::MoveTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
self.kurbo_path
|
||||||
PathEl::LineTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
.iter()
|
||||||
_ => None,
|
.filter_map(|path_el| match path_el {
|
||||||
}).collect::<Vec<(f64, f64)>>())
|
PathEl::MoveTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
||||||
|
PathEl::LineTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Vec<(f64, f64)>>(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,9 @@ use std::vec::Vec;
|
||||||
|
|
||||||
pub mod canvas;
|
pub mod canvas;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
|
pub mod migration;
|
||||||
pub mod tool;
|
pub mod tool;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
pub mod migration;
|
|
||||||
|
|
||||||
pub mod commands {
|
pub mod commands {
|
||||||
use druid::Selector;
|
use druid::Selector;
|
||||||
|
|
|
||||||
31
src/main.rs
31
src/main.rs
|
|
@ -32,7 +32,7 @@ use druid::{
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use stiletto::tool::CanvasToolParams;
|
use stiletto::tool::CanvasToolParams;
|
||||||
use stiletto::widget::tool_ctx::{CanvasToolCtx};
|
use stiletto::widget::tool_ctx::CanvasToolCtx;
|
||||||
use stiletto::widget::{build_simple_tool_widget, CanvasState, CanvasToolIconState, CanvasWidget};
|
use stiletto::widget::{build_simple_tool_widget, CanvasState, CanvasToolIconState, CanvasWidget};
|
||||||
use stiletto::DocumentSnapshot;
|
use stiletto::DocumentSnapshot;
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ pub fn main() {
|
||||||
current_file_path: None,
|
current_file_path: None,
|
||||||
};
|
};
|
||||||
AppLauncher::with_window(window)
|
AppLauncher::with_window(window)
|
||||||
.use_simple_logger()
|
.log_to_console()
|
||||||
.delegate(Delegate)
|
.delegate(Delegate)
|
||||||
.launch(canvas_data)
|
.launch(canvas_data)
|
||||||
.expect("launch failed");
|
.expect("launch failed");
|
||||||
|
|
@ -137,17 +137,19 @@ fn build_ui() -> impl Widget<StilettoState> {
|
||||||
ctx.submit_command(Command::new(CanvasWidget::SCROLL, 30.0, canvas_id));
|
ctx.submit_command(Command::new(CanvasWidget::SCROLL, 30.0, canvas_id));
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
.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,
|
||||||
|
|
@ -274,8 +276,7 @@ impl AppDelegate<StilettoState> for Delegate {
|
||||||
|
|
||||||
let res_file = File::create(&path_buf);
|
let res_file = File::create(&path_buf);
|
||||||
if let Ok(f) = res_file {
|
if let Ok(f) = res_file {
|
||||||
let write_res =
|
let write_res = data.canvas.get_document_snapshot().to_writer(f);
|
||||||
data.canvas.get_document_snapshot().to_writer(f);
|
|
||||||
if write_res.is_err() {
|
if write_res.is_err() {
|
||||||
warn!("Error while saving: {:?}", write_res.err());
|
warn!("Error while saving: {:?}", write_res.err());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,9 @@ impl StilettoDocument {
|
||||||
|
|
||||||
pub fn migrate_to_next(self) -> StilettoDocument {
|
pub fn migrate_to_next(self) -> StilettoDocument {
|
||||||
match self {
|
match self {
|
||||||
StilettoDocument::DocumentSnapshot0_1(snapshot) => StilettoDocument::DocumentSnapshot0_2(snapshot) ,
|
StilettoDocument::DocumentSnapshot0_1(snapshot) => {
|
||||||
|
StilettoDocument::DocumentSnapshot0_2(snapshot)
|
||||||
|
}
|
||||||
StilettoDocument::DocumentSnapshot0_2(_) => self,
|
StilettoDocument::DocumentSnapshot0_2(_) => self,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -60,12 +62,11 @@ impl StilettoDocument {
|
||||||
assert!(matches!(self, StilettoDocument::DocumentSnapshot0_2(_)));
|
assert!(matches!(self, StilettoDocument::DocumentSnapshot0_2(_)));
|
||||||
match self {
|
match self {
|
||||||
StilettoDocument::DocumentSnapshot0_2(snapshot) => snapshot,
|
StilettoDocument::DocumentSnapshot0_2(snapshot) => snapshot,
|
||||||
_ => panic!("Wrong Document Snapshot Version")
|
_ => panic!("Wrong Document Snapshot Version"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl fmt::Display for MigrationError {
|
impl fmt::Display for MigrationError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match &self {
|
match &self {
|
||||||
|
|
@ -74,10 +75,11 @@ impl fmt::Display for MigrationError {
|
||||||
MigrationError::IoError(e) => e.fmt(f),
|
MigrationError::IoError(e) => e.fmt(f),
|
||||||
MigrationError::UnexpectedVersion(major, minor, e_major, e_minor) => {
|
MigrationError::UnexpectedVersion(major, minor, e_major, e_minor) => {
|
||||||
write!(
|
write!(
|
||||||
f, "Unexpected version ({}, {}): was expecting ({}, {})",
|
f,
|
||||||
|
"Unexpected version ({}, {}): was expecting ({}, {})",
|
||||||
major, minor, e_major, e_minor
|
major, minor, e_major, e_minor
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -114,12 +116,12 @@ impl From<serde_bare::Error> for MigrationError {
|
||||||
fn open_0_1(path: &PathBuf) -> Result<StilettoDocument, MigrationError> {
|
fn open_0_1(path: &PathBuf) -> Result<StilettoDocument, MigrationError> {
|
||||||
let f = File::open(path)?;
|
let f = File::open(path)?;
|
||||||
let document_snapshot: DocumentSnapshot = serde_json::from_reader(f)?;
|
let document_snapshot: DocumentSnapshot = serde_json::from_reader(f)?;
|
||||||
if document_snapshot.format_version_major != 0 ||
|
if document_snapshot.format_version_major != 0 || document_snapshot.format_version_minor != 1 {
|
||||||
document_snapshot.format_version_minor != 1 {
|
|
||||||
Err(MigrationError::UnexpectedVersion(
|
Err(MigrationError::UnexpectedVersion(
|
||||||
document_snapshot.format_version_major,
|
document_snapshot.format_version_major,
|
||||||
document_snapshot.format_version_minor,
|
document_snapshot.format_version_minor,
|
||||||
0, 1
|
0,
|
||||||
|
1,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Ok(StilettoDocument::DocumentSnapshot0_1(document_snapshot))
|
Ok(StilettoDocument::DocumentSnapshot0_1(document_snapshot))
|
||||||
|
|
@ -139,4 +141,3 @@ pub fn open_stiletto_document(path: &PathBuf) -> Result<StilettoDocument, Migrat
|
||||||
|
|
||||||
open_0_2(path)
|
open_0_2(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
use druid::{Color, Data};
|
use druid::{Color, Data};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data, Debug)]
|
||||||
pub enum CanvasToolParams {
|
pub enum CanvasToolParams {
|
||||||
Pen { thickness: f64, color: Color },
|
Pen { thickness: f64, color: Color },
|
||||||
Eraser,
|
Eraser,
|
||||||
|
|
|
||||||
|
|
@ -16,18 +16,17 @@
|
||||||
|
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use super::tool_ctx::{CanvasToolCtx};
|
use super::tool_ctx::CanvasToolCtx;
|
||||||
use crate::canvas::Canvas;
|
use crate::canvas::Canvas;
|
||||||
use crate::history::VersionedCanvas;
|
use crate::history::VersionedCanvas;
|
||||||
use crate::DocumentSnapshot;
|
use crate::DocumentSnapshot;
|
||||||
|
|
||||||
|
use druid::kurbo::Point;
|
||||||
use druid::scroll_component::ScrollComponent;
|
use druid::scroll_component::ScrollComponent;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::widget::Viewport;
|
use druid::widget::Viewport;
|
||||||
use druid::kurbo::Point;
|
|
||||||
use druid::{Affine, Color, Data, Env, Event, PointerType, Selector};
|
use druid::{Affine, Color, Data, Env, Event, PointerType, Selector};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub struct CanvasState {
|
pub struct CanvasState {
|
||||||
versioned_canvas: VersionedCanvas,
|
versioned_canvas: VersionedCanvas,
|
||||||
|
|
@ -87,7 +86,13 @@ impl CanvasState {
|
||||||
&mut self.tool_ctx
|
&mut self.tool_ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_event(&mut self, 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
|
self.tool_ctx
|
||||||
.handle_event(ctx, event, &mut self.versioned_canvas, transform, env);
|
.handle_event(ctx, event, &mut self.versioned_canvas, transform, env);
|
||||||
}
|
}
|
||||||
|
|
@ -117,11 +122,10 @@ impl CanvasWidget {
|
||||||
let scale_x = self.viewport.view_size.width / self.widget_size.width;
|
let scale_x = self.viewport.view_size.width / self.widget_size.width;
|
||||||
let scale_y = self.viewport.view_size.height / self.widget_size.height;
|
let scale_y = self.viewport.view_size.height / self.widget_size.height;
|
||||||
Affine::translate(self.viewport.view_origin.to_vec2())
|
Affine::translate(self.viewport.view_origin.to_vec2())
|
||||||
* Affine::scale_non_uniform(scale_x, scale_y)
|
* Affine::scale_non_uniform(scale_x, scale_y)
|
||||||
} else {
|
} else {
|
||||||
Affine::scale(1.0) // identity
|
Affine::scale(1.0) // identity
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
||||||
|
|
@ -154,25 +158,23 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
viewport_transform = Some(Affine::translate((-30.0, 0.0)));
|
viewport_transform = Some(Affine::translate((-30.0, 0.0)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::PointerDown(pointer_event) => {
|
Event::MouseDown(pointer_event) => {
|
||||||
toggle_eraser_event = true;
|
toggle_eraser_event = true;
|
||||||
enable_temporary_erasing = false; //pointer_event.pointer_type == PointerType::Eraser;
|
enable_temporary_erasing = pointer_event.pointer_type == PointerType::Eraser;
|
||||||
}
|
}
|
||||||
Event::PointerMove(pointer_event) => {
|
Event::MouseMove(pointer_event) => {
|
||||||
toggle_eraser_event = true;
|
toggle_eraser_event = true;
|
||||||
enable_temporary_erasing = false; //pointer_event.pointer_type == PointerType::Eraser;
|
enable_temporary_erasing = pointer_event.pointer_type == PointerType::Eraser;
|
||||||
}
|
}
|
||||||
Event::PointerUp(pointer_event) => {
|
Event::MouseUp(pointer_event) => {
|
||||||
toggle_eraser_event = true;
|
toggle_eraser_event = true;
|
||||||
enable_temporary_erasing = false; //pointer_event.pointer_type == PointerType::Eraser;
|
enable_temporary_erasing = pointer_event.pointer_type == PointerType::Eraser;
|
||||||
}
|
}
|
||||||
Event::Wheel(pointer) => {
|
Event::Wheel(pointer) => {
|
||||||
let transform = self.widget_to_viewport();
|
let transform = self.widget_to_viewport();
|
||||||
viewport_transform = Some(
|
viewport_transform = Some(Affine::translate(
|
||||||
Affine::translate(
|
(pointer.wheel_delta.to_point()).to_vec2(),
|
||||||
(pointer.wheel_delta.to_point()).to_vec2()
|
));
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -188,7 +190,7 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
} else {
|
} else {
|
||||||
data.handle_event(ctx, event, self.widget_to_viewport(), env);
|
data.handle_event(ctx, event, self.widget_to_viewport(), env);
|
||||||
}
|
}
|
||||||
if let Some(transform) = viewport_transform {
|
if let Some(transform) = viewport_transform {
|
||||||
let mut new_rect = transform.transform_rect_bbox(self.viewport.view_rect());
|
let mut new_rect = transform.transform_rect_bbox(self.viewport.view_rect());
|
||||||
if new_rect.x0 <= 0f64 {
|
if new_rect.x0 <= 0f64 {
|
||||||
new_rect.x1 -= new_rect.x0;
|
new_rect.x1 -= new_rect.x0;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
pub mod canvas;
|
pub mod canvas;
|
||||||
pub mod tool_icon;
|
|
||||||
pub mod tool_ctx;
|
pub mod tool_ctx;
|
||||||
|
pub mod tool_icon;
|
||||||
|
|
||||||
pub use canvas::*;
|
pub use canvas::*;
|
||||||
pub use tool_icon::*;
|
pub use tool_icon::*;
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,14 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::tool::{CanvasToolParams, CanvasToolType};
|
|
||||||
use crate::canvas::{Canvas, CanvasElement};
|
use crate::canvas::{Canvas, CanvasElement};
|
||||||
use crate::history::VersionedCanvas;
|
use crate::history::VersionedCanvas;
|
||||||
|
use crate::tool::{CanvasToolParams, CanvasToolType};
|
||||||
|
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Affine, Data, Env, Event, EventCtx, PointerButton, PointerEvent, PaintCtx};
|
use druid::{Affine, Data, Env, Event, EventCtx, PaintCtx, PointerButton, PointerEvent};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data, Debug)]
|
||||||
pub enum CanvasToolState {
|
pub enum CanvasToolState {
|
||||||
Idle,
|
Idle,
|
||||||
DrawingFreehand {
|
DrawingFreehand {
|
||||||
|
|
@ -47,6 +47,11 @@ impl CanvasToolParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pressed(pointer_event: &PointerEvent) -> bool {
|
fn pressed(pointer_event: &PointerEvent) -> bool {
|
||||||
|
dbg!(&pointer_event);
|
||||||
|
dbg!(
|
||||||
|
pointer_event.buttons.contains(PointerButton::Left)
|
||||||
|
|| pointer_event.button == PointerButton::Left
|
||||||
|
);
|
||||||
pointer_event.buttons.contains(PointerButton::Left)
|
pointer_event.buttons.contains(PointerButton::Left)
|
||||||
|| pointer_event.button == PointerButton::Left
|
|| pointer_event.button == PointerButton::Left
|
||||||
}
|
}
|
||||||
|
|
@ -82,11 +87,11 @@ impl CanvasToolCtx {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::Idle, Event::PointerDown(pointer_event)) if pressed(pointer_event) => {
|
(CanvasToolState::Idle, Event::MouseDown(pointer_event)) if pressed(pointer_event) => {
|
||||||
self.state = CanvasToolState::Erasing;
|
self.state = CanvasToolState::Erasing;
|
||||||
ctx.set_handled();
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::PointerMove(pointer_event)) if pressed(pointer_event) => {
|
(CanvasToolState::Erasing, Event::MouseMove(pointer_event)) => {
|
||||||
let eraser_rect =
|
let eraser_rect =
|
||||||
druid::Rect::from_center_size(transform * pointer_event.pos, (5.0, 5.0));
|
druid::Rect::from_center_size(transform * pointer_event.pos, (5.0, 5.0));
|
||||||
let old_elements = vcanvas.get().elements();
|
let old_elements = vcanvas.get().elements();
|
||||||
|
|
@ -107,7 +112,7 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
ctx.set_handled();
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::PointerUp(pointer_event)) if pressed(pointer_event) => {
|
(CanvasToolState::Erasing, Event::MouseUp(pointer_event)) if pressed(pointer_event) => {
|
||||||
self.state = CanvasToolState::Idle;
|
self.state = CanvasToolState::Idle;
|
||||||
ctx.set_handled();
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
|
|
@ -124,7 +129,7 @@ impl CanvasToolCtx {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::Idle, Event::PointerDown(pointer_event)) if pressed(pointer_event) => {
|
(CanvasToolState::Idle, Event::MouseDown(pointer_event)) if pressed(pointer_event) => {
|
||||||
let mut kurbo_path = BezPath::new();
|
let mut kurbo_path = BezPath::new();
|
||||||
kurbo_path.move_to((pointer_event.pos.x, pointer_event.pos.y));
|
kurbo_path.move_to((pointer_event.pos.x, pointer_event.pos.y));
|
||||||
if let CanvasToolParams::Pen { thickness, color } = &self.initial_params {
|
if let CanvasToolParams::Pen { thickness, color } = &self.initial_params {
|
||||||
|
|
@ -144,31 +149,33 @@ impl CanvasToolCtx {
|
||||||
ref mut current_path,
|
ref mut current_path,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
Event::PointerMove(pointer_event),
|
Event::MouseMove(pointer_event),
|
||||||
) => if pressed(pointer_event) {
|
) => {
|
||||||
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
// TODO(enrico): investigate why mouse move doesn't carry buttons anymore
|
||||||
path.kurbo_path
|
//if pressed(pointer_event) {
|
||||||
.line_to((pointer_event.pos.x, pointer_event.pos.y));
|
let CanvasElement::Freehand { ref mut path, .. } = current_path;
|
||||||
}
|
path.kurbo_path
|
||||||
|
.line_to((pointer_event.pos.x, pointer_event.pos.y));
|
||||||
ctx.set_handled();
|
ctx.set_handled();
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
(CanvasToolState::DrawingFreehand { .. }, Event::PointerUp(pointer_event)) if pressed(pointer_event) => {
|
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(pointer_event))
|
||||||
|
if pressed(pointer_event) =>
|
||||||
|
{
|
||||||
vcanvas.update(move |canvas: &mut Canvas| {
|
vcanvas.update(move |canvas: &mut Canvas| {
|
||||||
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
||||||
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
||||||
if let CanvasElement::Freehand {
|
let CanvasElement::Freehand {
|
||||||
mut path,
|
mut path,
|
||||||
thickness,
|
thickness,
|
||||||
stroke_color,
|
stroke_color,
|
||||||
} = current_path
|
} = current_path;
|
||||||
{
|
path.kurbo_path.apply_affine(transform);
|
||||||
path.kurbo_path.apply_affine(transform);
|
canvas.add_element(CanvasElement::Freehand {
|
||||||
canvas.add_element(CanvasElement::Freehand {
|
path,
|
||||||
path,
|
thickness,
|
||||||
thickness,
|
stroke_color,
|
||||||
stroke_color,
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ctx.set_handled();
|
ctx.set_handled();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue