Compare commits
No commits in common. "rectangular_selection" and "master" have entirely different histories.
rectangula
...
master
|
|
@ -14,6 +14,8 @@
|
||||||
// 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 std::vec::Vec;
|
||||||
|
|
||||||
use im::{vector, Vector};
|
use im::{vector, Vector};
|
||||||
|
|
||||||
use serde::de::{Deserializer, SeqAccess, Visitor};
|
use serde::de::{Deserializer, SeqAccess, Visitor};
|
||||||
|
|
@ -126,26 +128,12 @@ impl CanvasElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_selected(&self, ctx: &mut druid::PaintCtx) {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand {
|
|
||||||
path,
|
|
||||||
thickness,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
use druid::RenderContext;
|
|
||||||
ctx.stroke(&path.kurbo_path, &druid::Color::rgb(10.0, 0.0, 255.0), *thickness * 1.4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
#[derive(Clone, druid::Data)]
|
||||||
pub struct Canvas {
|
pub struct Canvas {
|
||||||
elements: Vector<CanvasElement>,
|
elements: Vector<CanvasElement>,
|
||||||
content_size: druid::Size,
|
content_size: druid::Size,
|
||||||
selected_elements: Vector<usize>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Canvas {
|
impl Canvas {
|
||||||
|
|
@ -153,7 +141,6 @@ impl Canvas {
|
||||||
Canvas {
|
Canvas {
|
||||||
elements: vector![],
|
elements: vector![],
|
||||||
content_size: druid::Size::new(0.0, 0.0),
|
content_size: druid::Size::new(0.0, 0.0),
|
||||||
selected_elements: vector![],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,43 +165,25 @@ impl Canvas {
|
||||||
&self.elements
|
&self.elements
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_elements_interesecting(&self, rect: druid::Rect) -> bool {
|
/// Find all CanvasElement that intersect with rect
|
||||||
|
pub fn find_intersections(&self, rect: druid::Rect) -> Vec<usize> {
|
||||||
|
let mut found_elements = Vec::<usize>::new();
|
||||||
|
|
||||||
for (i, elem) in self.elements.iter().enumerate() {
|
for (i, elem) in self.elements.iter().enumerate() {
|
||||||
// Check if the element intersects the eraser rect
|
// Check if the element intersects the eraser rect
|
||||||
if elem.bounding_box().intersect(rect).area() > 0.0 {
|
if elem.bounding_box().intersect(rect).area() > 0.0 {
|
||||||
if elem.intersects_rect(rect) {
|
if elem.intersects_rect(rect) {
|
||||||
return true;
|
found_elements.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Find all CanvasElement that intersect with rect
|
|
||||||
pub fn find_elements_intersecting(&self, rect: druid::Rect) -> Vector<usize> {
|
|
||||||
let found_elements = self.elements.iter().enumerate()
|
|
||||||
.filter_map(|(i, elem)| {
|
|
||||||
if elem.bounding_box().intersect(rect).area() > 0.0 {
|
|
||||||
if elem.intersects_rect(rect) {
|
|
||||||
return Some(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}).collect::<Vector<usize>>();
|
|
||||||
found_elements
|
found_elements
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn content_size(&self) -> druid::Size {
|
pub fn content_size(&self) -> druid::Size {
|
||||||
self.content_size
|
self.content_size
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn selected_elements(&self) -> &Vector<usize> {
|
|
||||||
&self.selected_elements
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn selected_elements_mut(&mut self) -> &mut Vector<usize> {
|
|
||||||
&mut self.selected_elements
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Path {
|
impl Serialize for Path {
|
||||||
|
|
|
||||||
|
|
@ -14,26 +14,27 @@
|
||||||
// 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 super::canvas::Canvas;
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
#[derive(Clone, druid::Data)]
|
||||||
pub struct Versioned<T: druid::Data + Clone> {
|
pub struct VersionedCanvas {
|
||||||
// We internally guarantee that this vector
|
// We internally guarantee that this vector
|
||||||
// is never empty
|
// is never empty
|
||||||
versions: Vector<T>,
|
versions: Vector<Canvas>,
|
||||||
curr_version: usize,
|
curr_version: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: druid::Data + Clone> Versioned<T> {
|
impl VersionedCanvas {
|
||||||
pub fn new(first_version: T) -> Versioned<T> {
|
pub fn new(canvas: Canvas) -> VersionedCanvas {
|
||||||
Versioned {
|
VersionedCanvas {
|
||||||
versions: im::vector![first_version],
|
versions: im::vector![canvas],
|
||||||
curr_version: 0,
|
curr_version: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current version
|
// Get current canvas version
|
||||||
pub fn get(&self) -> &T {
|
pub fn get(&self) -> &Canvas {
|
||||||
self.versions.get(self.curr_version).unwrap()
|
self.versions.get(self.curr_version).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,10 +58,10 @@ impl<T: druid::Data + Clone> Versioned<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, update_fn: impl FnOnce(&mut T)) {
|
pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
||||||
// Make a new copy of the current version,
|
// Make a new copy of the current canvas version,
|
||||||
// so that we can safely modify it without losing
|
// so that we can safely modify it without losing
|
||||||
// the previous version
|
// the previous canvas version
|
||||||
let mut new_version = self.get().clone();
|
let mut new_version = self.get().clone();
|
||||||
update_fn(&mut new_version);
|
update_fn(&mut new_version);
|
||||||
|
|
||||||
|
|
@ -76,8 +77,8 @@ impl<T: druid::Data + Clone> Versioned<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do inplace update, which will be irreversible
|
// Do inplace update, which will be irreversible
|
||||||
pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut T)) {
|
pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
||||||
// Do the update directly on the current version
|
// Do the update directly on the current canvas version
|
||||||
update_fn(self.versions.back_mut().unwrap());
|
update_fn(self.versions.back_mut().unwrap());
|
||||||
|
|
||||||
// This is a linear history,
|
// This is a linear history,
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,6 @@ pub mod tool;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
pub mod migration;
|
pub mod migration;
|
||||||
|
|
||||||
pub mod colors {
|
|
||||||
//use druid::Color;
|
|
||||||
//pub const SELECTED_STROKE: Color = Color::rgb(10.0, 0.0, 255.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod commands {
|
pub mod commands {
|
||||||
use druid::Selector;
|
use druid::Selector;
|
||||||
|
|
||||||
|
|
@ -51,5 +46,3 @@ impl DocumentSnapshot {
|
||||||
serde_bare::to_writer(writer, &self)
|
serde_bare::to_writer(writer, &self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type VersionedCanvas = history::Versioned<canvas::Canvas>;
|
|
||||||
|
|
|
||||||
22
src/main.rs
22
src/main.rs
|
|
@ -71,10 +71,6 @@ pub fn main() {
|
||||||
tool_params: CanvasToolParams::Eraser,
|
tool_params: CanvasToolParams::Eraser,
|
||||||
selected: false,
|
selected: false,
|
||||||
},
|
},
|
||||||
CanvasToolIconState {
|
|
||||||
tool_params: CanvasToolParams::RectangularSelection,
|
|
||||||
selected: false,
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
current_tool: 0,
|
current_tool: 0,
|
||||||
eraser_tool_id: 2,
|
eraser_tool_id: 2,
|
||||||
|
|
@ -104,9 +100,9 @@ impl StilettoState {
|
||||||
self.tool_icons.get_mut(old_tool).unwrap().selected = false;
|
self.tool_icons.get_mut(old_tool).unwrap().selected = false;
|
||||||
self.tool_icons.get_mut(new_tool).unwrap().selected = true;
|
self.tool_icons.get_mut(new_tool).unwrap().selected = true;
|
||||||
self.current_tool = new_tool;
|
self.current_tool = new_tool;
|
||||||
self.canvas.change_tool(
|
self.canvas.set_tool_ctx(CanvasToolCtx::new(
|
||||||
self.tool_icons.get(new_tool).unwrap().tool_params.clone(),
|
self.tool_icons.get(new_tool).unwrap().tool_params.clone(),
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,18 +116,6 @@ fn build_ui() -> impl Widget<StilettoState> {
|
||||||
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| data.canvas.perform_redo(),
|
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| data.canvas.perform_redo(),
|
||||||
));
|
));
|
||||||
|
|
||||||
let selection_buttons = Flex::row()
|
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
|
||||||
.with_child(Button::new("Copy").on_click(
|
|
||||||
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| {},
|
|
||||||
))
|
|
||||||
.with_child(Button::new("Cut").on_click(
|
|
||||||
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| {},
|
|
||||||
))
|
|
||||||
.with_child(Button::new("Delete").on_click(
|
|
||||||
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| data.canvas.delete_selected(),
|
|
||||||
));
|
|
||||||
|
|
||||||
let stlt = FileSpec::new("Stiletto notebook", &["stlt"]);
|
let stlt = FileSpec::new("Stiletto notebook", &["stlt"]);
|
||||||
let save_dialog_options = FileDialogOptions::new()
|
let save_dialog_options = FileDialogOptions::new()
|
||||||
.allowed_types(vec![stlt])
|
.allowed_types(vec![stlt])
|
||||||
|
|
@ -190,8 +174,6 @@ fn build_ui() -> impl Widget<StilettoState> {
|
||||||
.with_spacer(30.0)
|
.with_spacer(30.0)
|
||||||
.with_flex_child(Align::left(history_buttons), 1.0)
|
.with_flex_child(Align::left(history_buttons), 1.0)
|
||||||
.with_spacer(10.0)
|
.with_spacer(10.0)
|
||||||
.with_flex_child(Align::left(selection_buttons), 1.0)
|
|
||||||
.with_spacer(10.0)
|
|
||||||
.with_flex_child(Align::left(tool_buttons), 2.0)
|
.with_flex_child(Align::left(tool_buttons), 2.0)
|
||||||
.with_spacer(20.0)
|
.with_spacer(20.0)
|
||||||
.with_flex_child(Align::right(save_buttons), 1.0)
|
.with_flex_child(Align::right(save_buttons), 1.0)
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,11 @@ use druid::{Color, Data};
|
||||||
pub enum CanvasToolParams {
|
pub enum CanvasToolParams {
|
||||||
Pen { thickness: f64, color: Color },
|
Pen { thickness: f64, color: Color },
|
||||||
Eraser,
|
Eraser,
|
||||||
RectangularSelection,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub enum CanvasToolType {
|
pub enum CanvasToolType {
|
||||||
Pen,
|
Pen,
|
||||||
Eraser,
|
Eraser,
|
||||||
RectangularSelection,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,13 @@
|
||||||
|
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use super::tool_ctx::{CanvasToolCtx, CanvasToolState};
|
use super::tool_ctx::{CanvasToolCtx};
|
||||||
use crate::canvas::{Canvas, CanvasElement};
|
use crate::canvas::Canvas;
|
||||||
use crate::tool::CanvasToolParams;
|
use crate::history::VersionedCanvas;
|
||||||
use crate::{DocumentSnapshot, VersionedCanvas};
|
use crate::DocumentSnapshot;
|
||||||
|
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::{Color, Data, Env, Event, PointerType, Selector};
|
use druid::{Color, Data, Env, Event, PointerType};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub struct CanvasState {
|
pub struct CanvasState {
|
||||||
|
|
@ -42,14 +42,12 @@ impl CanvasState {
|
||||||
pub fn perform_undo(&mut self) {
|
pub fn perform_undo(&mut self) {
|
||||||
//if !self.is_drawing() {
|
//if !self.is_drawing() {
|
||||||
self.versioned_canvas.undo();
|
self.versioned_canvas.undo();
|
||||||
self.set_ctx_selection_state();
|
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn perform_redo(&mut self) {
|
pub fn perform_redo(&mut self) {
|
||||||
//if !self.is_drawing() {
|
//if !self.is_drawing() {
|
||||||
self.versioned_canvas.redo();
|
self.versioned_canvas.redo();
|
||||||
self.set_ctx_selection_state();
|
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,44 +71,6 @@ impl CanvasState {
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_ctx_selection_state(&mut self) {
|
|
||||||
if !self.versioned_canvas.get().selected_elements().is_empty() {
|
|
||||||
self.tool_ctx.set_state(CanvasToolState::ActiveSelection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete_selected(&mut self) {
|
|
||||||
let selected_elements = self.versioned_canvas.get().selected_elements();
|
|
||||||
if !selected_elements.is_empty() {
|
|
||||||
if !selected_elements.is_empty() {
|
|
||||||
let elements = self.versioned_canvas.get().elements();
|
|
||||||
// TODO(enrico): is this memory efficient?
|
|
||||||
// what is the difference between this and
|
|
||||||
// im::Vector::retain?
|
|
||||||
let new_elements = elements.iter().enumerate()
|
|
||||||
.filter_map(|(i, elem)| {
|
|
||||||
if !selected_elements.contains(&i) {
|
|
||||||
Some(elem)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.cloned()
|
|
||||||
.collect::<Vector<CanvasElement>>();
|
|
||||||
|
|
||||||
self.versioned_canvas.update(move |canvas: &mut Canvas| {
|
|
||||||
*canvas = Canvas::new_with_elements(new_elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Changes tool while mantaining the current CanvasToolCtx
|
|
||||||
pub fn change_tool(&mut self, tool_params: CanvasToolParams) {
|
|
||||||
self.tool_ctx.set_params(tool_params);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
@ -131,28 +91,12 @@ impl CanvasState {
|
||||||
|
|
||||||
pub struct CanvasWidget;
|
pub struct CanvasWidget;
|
||||||
|
|
||||||
impl CanvasWidget {
|
|
||||||
pub const IS_OVER_SELECTION: Selector<bool> = Selector::new("is_over_selection");
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
ctx.request_focus();
|
ctx.request_focus();
|
||||||
let mut toggle_eraser_event = false;
|
let mut toggle_eraser_event = false;
|
||||||
let mut enable_temporary_erasing = false;
|
let mut enable_temporary_erasing = false;
|
||||||
match event {
|
match event {
|
||||||
Event::Notification(cmd) => {
|
|
||||||
cmd.get(CanvasWidget::IS_OVER_SELECTION).map(|is_over_sel| {
|
|
||||||
if *is_over_sel {
|
|
||||||
use druid::Cursor;
|
|
||||||
ctx.set_cursor(&Cursor::OpenHand);
|
|
||||||
} else {
|
|
||||||
// TODO: this is not correct. Must check if had modified cursor before
|
|
||||||
ctx.clear_cursor();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
ctx.set_handled();
|
|
||||||
}
|
|
||||||
Event::MouseDown(mouse_event) => {
|
Event::MouseDown(mouse_event) => {
|
||||||
toggle_eraser_event = true;
|
toggle_eraser_event = true;
|
||||||
enable_temporary_erasing = mouse_event.pointer_type == PointerType::Eraser;
|
enable_temporary_erasing = mouse_event.pointer_type == PointerType::Eraser;
|
||||||
|
|
@ -234,20 +178,11 @@ 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 canvas = data.versioned_canvas.get();
|
for element in data.versioned_canvas.get().elements().iter() {
|
||||||
let selected = canvas.selected_elements();
|
|
||||||
|
|
||||||
for (pos, element) in canvas.elements().iter().enumerate() {
|
|
||||||
if data.tool_ctx.has_active_selection() && selected.contains(&pos) {
|
|
||||||
element.draw_selected(ctx);
|
|
||||||
} else {
|
|
||||||
element.draw(ctx);
|
element.draw(ctx);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if data.tool_ctx.needs_repaint() {
|
if data.tool_ctx.needs_repaint() {
|
||||||
data.tool_ctx.paint(ctx, env);
|
data.tool_ctx.paint(ctx, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,10 @@
|
||||||
|
|
||||||
use crate::tool::{CanvasToolParams, CanvasToolType};
|
use crate::tool::{CanvasToolParams, CanvasToolType};
|
||||||
use crate::canvas::{Canvas, CanvasElement};
|
use crate::canvas::{Canvas, CanvasElement};
|
||||||
use crate::widget::CanvasWidget;
|
use crate::history::VersionedCanvas;
|
||||||
use crate::VersionedCanvas;
|
|
||||||
|
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext};
|
use druid::{Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx};
|
||||||
|
|
||||||
use im::{vector, Vector};
|
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub enum CanvasToolState {
|
pub enum CanvasToolState {
|
||||||
|
|
@ -32,12 +29,6 @@ pub enum CanvasToolState {
|
||||||
current_path: CanvasElement,
|
current_path: CanvasElement,
|
||||||
},
|
},
|
||||||
Erasing,
|
Erasing,
|
||||||
SelectingRect {
|
|
||||||
origin: druid::Point,
|
|
||||||
current_rect: druid::Rect,
|
|
||||||
},
|
|
||||||
DraggingSelection,
|
|
||||||
ActiveSelection,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
|
|
@ -51,7 +42,6 @@ impl CanvasToolParams {
|
||||||
match self {
|
match self {
|
||||||
CanvasToolParams::Pen { .. } => CanvasToolType::Pen,
|
CanvasToolParams::Pen { .. } => CanvasToolType::Pen,
|
||||||
CanvasToolParams::Eraser => CanvasToolType::Eraser,
|
CanvasToolParams::Eraser => CanvasToolType::Eraser,
|
||||||
CanvasToolParams::RectangularSelection => CanvasToolType::RectangularSelection,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -61,14 +51,6 @@ fn pressed(mouse_event: &MouseEvent) -> bool {
|
||||||
|| mouse_event.button == MouseButton::Left
|
|| mouse_event.button == MouseButton::Left
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_over_active_selection(mouse_event: &MouseEvent, vcanvas: &VersionedCanvas) -> bool {
|
|
||||||
let mouse_rect = druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
|
|
||||||
// TODO(enrico): this can be faster
|
|
||||||
let intersecting_elements = vcanvas.get().find_elements_intersecting(mouse_rect);
|
|
||||||
let selected_elements = vcanvas.get().selected_elements();
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CanvasToolCtx {
|
impl CanvasToolCtx {
|
||||||
pub fn new(params: CanvasToolParams) -> Self {
|
pub fn new(params: CanvasToolParams) -> Self {
|
||||||
CanvasToolCtx {
|
CanvasToolCtx {
|
||||||
|
|
@ -77,22 +59,9 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_params(&mut self, params: CanvasToolParams) {
|
|
||||||
self.initial_params = params;
|
|
||||||
// TODO(enrico) is this necessary?
|
|
||||||
match self.state {
|
|
||||||
CanvasToolState::Idle | CanvasToolState::ActiveSelection => {}
|
|
||||||
_ => { self.state = CanvasToolState::Idle; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_state(&mut self, state: CanvasToolState) {
|
|
||||||
self.state = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_event(
|
pub fn handle_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
mut ctx: &mut EventCtx,
|
ctx: &EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
mut vcanvas: &mut VersionedCanvas,
|
mut vcanvas: &mut VersionedCanvas,
|
||||||
env: &Env,
|
env: &Env,
|
||||||
|
|
@ -100,7 +69,6 @@ impl CanvasToolCtx {
|
||||||
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, &mut vcanvas, &env),
|
||||||
CanvasToolType::Eraser => self.handle_erase_event(&ctx, &event, &mut vcanvas, &env),
|
CanvasToolType::Eraser => self.handle_erase_event(&ctx, &event, &mut vcanvas, &env),
|
||||||
CanvasToolType::RectangularSelection => self.handle_rectangular_select_event(&mut ctx, &event, &mut vcanvas, &env),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,7 +80,6 @@ impl CanvasToolCtx {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::ActiveSelection, Event::MouseDown(mouse_event)) |
|
|
||||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
||||||
self.state = CanvasToolState::Erasing;
|
self.state = CanvasToolState::Erasing;
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +117,6 @@ impl CanvasToolCtx {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::ActiveSelection, Event::MouseDown(mouse_event)) |
|
|
||||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
||||||
let mut kurbo_path = BezPath::new();
|
let mut kurbo_path = BezPath::new();
|
||||||
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
|
|
@ -171,7 +137,7 @@ impl CanvasToolCtx {
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
Event::MouseMove(mouse_event),
|
Event::MouseMove(mouse_event),
|
||||||
) if pressed(mouse_event) => {
|
) => if pressed(mouse_event) {
|
||||||
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
||||||
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));
|
||||||
|
|
@ -192,7 +158,6 @@ impl CanvasToolCtx {
|
||||||
thickness,
|
thickness,
|
||||||
stroke_color,
|
stroke_color,
|
||||||
});
|
});
|
||||||
*canvas.selected_elements_mut() = vector![];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -201,78 +166,13 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_rectangular_select_event(
|
|
||||||
&mut self,
|
|
||||||
ctx: &mut EventCtx,
|
|
||||||
event: &Event,
|
|
||||||
vcanvas: &mut VersionedCanvas,
|
|
||||||
_env: &Env,
|
|
||||||
) {
|
|
||||||
match (&mut self.state, event) {
|
|
||||||
(CanvasToolState::ActiveSelection, Event::MouseMove(mouse_event)) if !pressed(mouse_event) => {
|
|
||||||
ctx.submit_notification(CanvasWidget::IS_OVER_SELECTION.with(is_over_active_selection(mouse_event, vcanvas)));
|
|
||||||
}
|
|
||||||
(CanvasToolState::ActiveSelection, Event::MouseDown(mouse_event)) if pressed(mouse_event)
|
|
||||||
&& is_over_active_selection(mouse_event, vcanvas) => {
|
|
||||||
}
|
|
||||||
(CanvasToolState::DraggingSelection, Event::MouseMove(mouse_event)) if pressed(mouse_event) => {
|
|
||||||
}
|
|
||||||
(CanvasToolState::DraggingSelection, Event::MouseUp(mouse_event)) if pressed(mouse_event) => {
|
|
||||||
}
|
|
||||||
|
|
||||||
(CanvasToolState::ActiveSelection, Event::MouseDown(mouse_event)) |
|
|
||||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
|
||||||
self.state = CanvasToolState::SelectingRect {
|
|
||||||
origin: mouse_event.pos,
|
|
||||||
current_rect: druid::Rect::from_origin_size(mouse_event.pos, (0.0, 0.0))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
(CanvasToolState::SelectingRect{ ref mut current_rect, ref origin }, Event::MouseMove(mouse_event)) if pressed(mouse_event) => {
|
|
||||||
*current_rect = druid::Rect::from_origin_size(*origin, (0.0, 0.0))
|
|
||||||
.union_pt(mouse_event.pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
(CanvasToolState::SelectingRect{ ref mut current_rect, ref origin }, Event::MouseUp(mouse_event)) if pressed(mouse_event) => {
|
|
||||||
*current_rect = druid::Rect::from_origin_size(*origin, (0.0, 0.0))
|
|
||||||
.union_pt(mouse_event.pos);
|
|
||||||
let elements = vcanvas.get().elements();
|
|
||||||
let selected_elements_idx = elements.iter().enumerate()
|
|
||||||
.filter_map(|(i, elem)| {
|
|
||||||
let bbox = elem.bounding_box();
|
|
||||||
let p0 = druid::Point::new(bbox.x0, bbox.y0);
|
|
||||||
let p1 = druid::Point::new(bbox.x1, bbox.y1);
|
|
||||||
if current_rect.contains(p0) && current_rect.contains(p1) {
|
|
||||||
return Some(i);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}).collect::<Vector<usize>>();
|
|
||||||
if !elements.is_empty() {
|
|
||||||
vcanvas.update(move |canvas: &mut Canvas| {
|
|
||||||
*canvas.selected_elements_mut() = selected_elements_idx;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
self.state = CanvasToolState::ActiveSelection;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn needs_repaint(&self) -> bool {
|
pub fn needs_repaint(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_active_selection(&self) -> bool {
|
|
||||||
match self.state {
|
|
||||||
CanvasToolState::ActiveSelection => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn paint(&self, ctx: &mut PaintCtx, _env: &Env) {
|
pub fn paint(&self, ctx: &mut PaintCtx, _env: &Env) {
|
||||||
match &self.state {
|
match &self.state {
|
||||||
CanvasToolState::DrawingFreehand { current_path, .. } => current_path.draw(ctx),
|
CanvasToolState::DrawingFreehand { current_path, .. } => current_path.draw(ctx),
|
||||||
CanvasToolState::SelectingRect { current_rect, .. } => ctx.stroke(current_rect, &Color::BLACK, 0.5),
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,9 +34,6 @@ impl CanvasToolIconState {
|
||||||
CanvasToolParams::Eraser => include_str!("../../icons/eraser.svg")
|
CanvasToolParams::Eraser => include_str!("../../icons/eraser.svg")
|
||||||
.parse::<SvgData>()
|
.parse::<SvgData>()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
CanvasToolParams::RectangularSelection => include_str!("../../icons/eraser.svg")
|
|
||||||
.parse::<SvgData>()
|
|
||||||
.unwrap(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue