Compare commits
3 Commits
efde4d7c83
...
7c9ce7a734
| Author | SHA1 | Date |
|---|---|---|
|
|
7c9ce7a734 | |
|
|
a1f813631b | |
|
|
f330760c1c |
|
|
@ -338,7 +338,7 @@ checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "druid"
|
name = "druid"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#f0a6fd3f6bece1b1d10e261ce57673d7f6f1969a"
|
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#2cbb14456fb1813e673d2151626630d2c4db68bf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"console_log",
|
"console_log",
|
||||||
"druid-derive",
|
"druid-derive",
|
||||||
|
|
@ -361,7 +361,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "druid-derive"
|
name = "druid-derive"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#f0a6fd3f6bece1b1d10e261ce57673d7f6f1969a"
|
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#2cbb14456fb1813e673d2151626630d2c4db68bf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|
@ -371,7 +371,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "druid-shell"
|
name = "druid-shell"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#f0a6fd3f6bece1b1d10e261ce57673d7f6f1969a"
|
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#2cbb14456fb1813e673d2151626630d2c4db68bf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,14 @@ serde_json = "1.0"
|
||||||
druid = { git = "https://github.com/doppioandante/druid", branch = "v0.7.0_stiletto", features = ["im", "svg"] }
|
druid = { git = "https://github.com/doppioandante/druid", branch = "v0.7.0_stiletto", features = ["im", "svg"] }
|
||||||
#druid = { path = "../druid/druid/", features = ["im", "svg"] }
|
#druid = { path = "../druid/druid/", features = ["im", "svg"] }
|
||||||
|
|
||||||
[dependencies.gtk]
|
[target.'cfg(target_os="linux")'.dependencies.gtk]
|
||||||
version = "0.9.2"
|
version = "0.9.2"
|
||||||
features = ["v3_22"]
|
features = ["v3_22"]
|
||||||
|
|
||||||
[dependencies.gio]
|
[target.'cfg(target_os="linux")'.dependencies.gio]
|
||||||
version = "0.9.1"
|
version = "0.9.1"
|
||||||
features = ["v2_56"]
|
features = ["v2_56"]
|
||||||
|
|
||||||
[dependencies.gdk]
|
[target.'cfg(target_os="linux")'.dependencies.gdk]
|
||||||
version = "0.13.2"
|
version = "0.13.2"
|
||||||
features = ["v3_22"]
|
features = ["v3_22"]
|
||||||
|
|
|
||||||
|
|
@ -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)]
|
||||||
|
|
|
||||||
114
src/main.rs
114
src/main.rs
|
|
@ -21,16 +21,20 @@ 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, WidgetId, WindowDesc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use stiletto::tool::{CanvasToolCtx, CanvasToolParams};
|
use stiletto::tool::{CanvasToolCtx, CanvasToolParams};
|
||||||
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;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
|
@ -70,6 +74,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,11 +89,27 @@ 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 canvas_id = WidgetId::next();
|
||||||
|
|
||||||
let history_buttons = Flex::row()
|
let history_buttons = Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.with_child(Button::new("Undo").on_click(
|
.with_child(Button::new("Undo").on_click(
|
||||||
|
|
@ -107,6 +128,16 @@ 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("Up").on_click(
|
||||||
|
move |ctx: &mut EventCtx, _data: &mut StilettoState, _env: &Env| {
|
||||||
|
ctx.submit_command(Command::new(CanvasWidget::SCROLL, -30.0, canvas_id));
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.with_child(Button::new("Down").on_click(
|
||||||
|
move |ctx: &mut EventCtx, _data: &mut StilettoState, _env: &Env| {
|
||||||
|
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(Button::new("Save").on_click(move |ctx, data: &mut StilettoState, _| {
|
||||||
if data.current_file_path.is_some() {
|
if data.current_file_path.is_some() {
|
||||||
ctx.submit_command(Command::new(commands::SAVE_FILE, (), Target::Auto));
|
ctx.submit_command(Command::new(commands::SAVE_FILE, (), Target::Auto));
|
||||||
|
|
@ -141,7 +172,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 +194,65 @@ 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::new()
|
||||||
|
.lens(StilettoState::canvas)
|
||||||
|
.with_id(canvas_id),
|
||||||
|
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 +264,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 +304,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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
src/tool.rs
26
src/tool.rs
|
|
@ -15,7 +15,7 @@
|
||||||
// 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 druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Color, Data, Env, Event, EventCtx, PaintCtx};
|
use druid::{Affine, Color, Data, Env, Event, EventCtx, PaintCtx};
|
||||||
|
|
||||||
use crate::canvas;
|
use crate::canvas;
|
||||||
use crate::canvas::{Canvas, CanvasElement};
|
use crate::canvas::{Canvas, CanvasElement};
|
||||||
|
|
@ -68,31 +68,34 @@ impl CanvasToolCtx {
|
||||||
|
|
||||||
pub fn handle_event(
|
pub fn handle_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
ctx: &EventCtx,
|
mut ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
mut vcanvas: &mut VersionedCanvas,
|
mut vcanvas: &mut VersionedCanvas,
|
||||||
|
transform: Affine,
|
||||||
env: &Env,
|
env: &Env,
|
||||||
) {
|
) {
|
||||||
match self.initial_params.tool_type() {
|
match self.initial_params.tool_type() {
|
||||||
CanvasToolType::Pen => self.handle_pen_event(&ctx, &event, &mut vcanvas, &env),
|
CanvasToolType::Pen => self.handle_pen_event(ctx, event, vcanvas, transform, env),
|
||||||
CanvasToolType::Eraser => self.handle_erase_event(&ctx, &event, &mut vcanvas, &env),
|
CanvasToolType::Eraser => self.handle_erase_event(ctx, event, vcanvas, transform, env),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_erase_event(
|
pub fn handle_erase_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &EventCtx,
|
ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
vcanvas: &mut VersionedCanvas,
|
vcanvas: &mut VersionedCanvas,
|
||||||
|
transform: Affine,
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
||||||
self.state = CanvasToolState::Erasing;
|
self.state = CanvasToolState::Erasing;
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(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(transform * 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| {
|
||||||
|
|
@ -102,16 +105,18 @@ impl CanvasToolCtx {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
true
|
||||||
});
|
});
|
||||||
if new_elements.len() != old_elements.len() {
|
if new_elements.len() != old_elements.len() {
|
||||||
vcanvas.update(|canvas: &mut Canvas| {
|
vcanvas.update(|canvas: &mut Canvas| {
|
||||||
*canvas = Canvas::new_with_elements(new_elements);
|
*canvas = Canvas::new_with_elements(new_elements);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
||||||
self.state = CanvasToolState::Idle;
|
self.state = CanvasToolState::Idle;
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -119,9 +124,10 @@ impl CanvasToolCtx {
|
||||||
|
|
||||||
pub fn handle_pen_event(
|
pub fn handle_pen_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &EventCtx,
|
ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
vcanvas: &mut VersionedCanvas,
|
vcanvas: &mut VersionedCanvas,
|
||||||
|
transform: Affine,
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
|
|
@ -138,6 +144,7 @@ impl CanvasToolCtx {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
CanvasToolState::DrawingFreehand {
|
CanvasToolState::DrawingFreehand {
|
||||||
|
|
@ -150,6 +157,7 @@ impl CanvasToolCtx {
|
||||||
path.kurbo_path
|
path.kurbo_path
|
||||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
||||||
vcanvas.update(move |canvas: &mut Canvas| {
|
vcanvas.update(move |canvas: &mut Canvas| {
|
||||||
|
|
@ -161,6 +169,7 @@ impl CanvasToolCtx {
|
||||||
stroke_color,
|
stroke_color,
|
||||||
} = current_path
|
} = current_path
|
||||||
{
|
{
|
||||||
|
path.kurbo_path.apply_affine(transform);
|
||||||
canvas.add_element(CanvasElement::Freehand {
|
canvas.add_element(CanvasElement::Freehand {
|
||||||
path,
|
path,
|
||||||
thickness,
|
thickness,
|
||||||
|
|
@ -169,6 +178,7 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,20 +21,25 @@ use crate::history::VersionedCanvas;
|
||||||
use crate::tool::CanvasToolCtx;
|
use crate::tool::CanvasToolCtx;
|
||||||
use crate::DocumentSnapshot;
|
use crate::DocumentSnapshot;
|
||||||
|
|
||||||
|
use druid::scroll_component::ScrollComponent;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::{Color, Data, Env, Event};
|
use druid::widget::Viewport;
|
||||||
|
use druid::{Affine, Color, Data, Env, Event, PointerType, Selector};
|
||||||
|
|
||||||
|
|
||||||
#[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,23 +77,111 @@ 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, transform: Affine, env: &Env) {
|
||||||
|
self.tool_ctx
|
||||||
|
.handle_event(ctx, event, &mut self.versioned_canvas, transform, env);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CanvasWidget;
|
pub struct CanvasWidget {
|
||||||
|
viewport: Viewport,
|
||||||
|
scroll_component: ScrollComponent,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasWidget {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
CanvasWidget {
|
||||||
|
viewport: Viewport {
|
||||||
|
content_size: Size::new(0.0, 0.0),
|
||||||
|
rect: druid::Rect::new(0.0, 0.0, 0.0, 0.0),
|
||||||
|
},
|
||||||
|
scroll_component: ScrollComponent::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
self.scroll_component
|
||||||
|
.event(&mut self.viewport, ctx, event, env);
|
||||||
|
if !ctx.is_handled() {
|
||||||
|
let mut scroll_amount = 0.0;
|
||||||
|
let mut toggle_eraser_event = false;
|
||||||
|
let mut enable_temporary_erasing = false;
|
||||||
|
match event {
|
||||||
|
Event::Command(cmd) => {
|
||||||
|
if let Some(value) = cmd.get(CanvasWidget::SCROLL) {
|
||||||
|
scroll_amount = *value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::KeyDown(key_event) => {
|
||||||
|
if key_event.code == druid::Code::ArrowDown {
|
||||||
|
scroll_amount = 30.0;
|
||||||
|
} else if key_event.code == druid::Code::ArrowUp {
|
||||||
|
scroll_amount = -30.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
let transform =
|
||||||
|
Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
|
||||||
|
data.handle_event(ctx, event, transform, env);
|
||||||
|
}
|
||||||
|
if scroll_amount != 0.0 {
|
||||||
|
self.viewport.rect.y0 = 0.0_f64.max(self.viewport.rect.y0 + scroll_amount);
|
||||||
|
self.scroll_component
|
||||||
|
.reset_scrollbar_fade(|d| ctx.request_timer(d), env);
|
||||||
|
ctx.request_paint();
|
||||||
|
ctx.set_handled();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: replace this by own handling of Wheel event (must be able to extend content size)
|
||||||
|
self.scroll_component
|
||||||
|
.handle_scroll(&mut self.viewport, ctx, event, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lifecycle(
|
fn lifecycle(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &mut LifeCycleCtx,
|
ctx: &mut LifeCycleCtx,
|
||||||
_event: &LifeCycle,
|
event: &LifeCycle,
|
||||||
_data: &CanvasState,
|
_data: &CanvasState,
|
||||||
_env: &Env,
|
env: &Env,
|
||||||
) {
|
) {
|
||||||
|
self.scroll_component.lifecycle(ctx, event, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
|
@ -107,6 +200,7 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
// ctx.request_paint_rect(e.bounding_box());
|
// ctx.request_paint_rect(e.bounding_box());
|
||||||
// }
|
// }
|
||||||
//} else {
|
//} else {
|
||||||
|
|
||||||
ctx.request_paint();
|
ctx.request_paint();
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
@ -136,11 +230,26 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
let rect = size.to_rect();
|
let rect = size.to_rect();
|
||||||
|
|
||||||
ctx.fill(rect, &Color::WHITE);
|
ctx.fill(rect, &Color::WHITE);
|
||||||
|
|
||||||
|
let page_content_size = data.versioned_canvas.get().content_size();
|
||||||
|
self.viewport.rect = self.viewport.rect.with_size(size);
|
||||||
|
self.viewport.content_size = druid::Size::new(
|
||||||
|
self.viewport.rect.x1.max(page_content_size.width),
|
||||||
|
self.viewport.rect.y1.max(page_content_size.height),
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.save().unwrap();
|
||||||
|
ctx.transform(Affine::translate((
|
||||||
|
-self.viewport.rect.x0,
|
||||||
|
-self.viewport.rect.y0,
|
||||||
|
)));
|
||||||
for element in data.versioned_canvas.get().elements().iter() {
|
for element in data.versioned_canvas.get().elements().iter() {
|
||||||
element.draw(ctx);
|
element.draw(ctx);
|
||||||
}
|
}
|
||||||
|
ctx.restore().unwrap();
|
||||||
if data.tool_ctx.needs_repaint() {
|
if data.tool_ctx.needs_repaint() {
|
||||||
data.tool_ctx.paint(ctx, env);
|
data.tool_ctx.paint(ctx, env);
|
||||||
}
|
}
|
||||||
|
self.scroll_component.draw_bars(ctx, &self.viewport, env);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue