WIP: Rectangular selections #37
|
|
@ -128,12 +128,26 @@ 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 {
|
||||||
|
|
@ -141,6 +155,7 @@ 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![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -184,6 +199,14 @@ impl Canvas {
|
||||||
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 {
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,11 @@ 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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,9 +104,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.set_tool_ctx(CanvasToolCtx::new(
|
self.canvas.change_tool(
|
||||||
self.tool_icons.get(new_tool).unwrap().tool_params.clone(),
|
self.tool_icons.get(new_tool).unwrap().tool_params.clone(),
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,9 @@
|
||||||
|
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use super::tool_ctx::{CanvasToolCtx};
|
use super::tool_ctx::{CanvasToolCtx, CanvasToolState};
|
||||||
use crate::canvas::Canvas;
|
use crate::canvas::Canvas;
|
||||||
|
use crate::tool::CanvasToolParams;
|
||||||
use crate::{DocumentSnapshot, VersionedCanvas};
|
use crate::{DocumentSnapshot, VersionedCanvas};
|
||||||
|
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
|
|
@ -41,12 +42,14 @@ 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();
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,6 +73,17 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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;
|
||||||
}
|
}
|
||||||
|
|
@ -177,11 +191,20 @@ 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);
|
||||||
for element in data.versioned_canvas.get().elements().iter() {
|
let canvas = data.versioned_canvas.get();
|
||||||
element.draw(ctx);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if data.tool_ctx.needs_repaint() {
|
if data.tool_ctx.needs_repaint() {
|
||||||
data.tool_ctx.paint(ctx, env);
|
data.tool_ctx.paint(ctx, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ use crate::VersionedCanvas;
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext};
|
use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext};
|
||||||
|
|
||||||
use im::Vector;
|
use im::{vector, Vector};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub enum CanvasToolState {
|
pub enum CanvasToolState {
|
||||||
|
|
@ -34,7 +34,8 @@ pub enum CanvasToolState {
|
||||||
SelectingRect {
|
SelectingRect {
|
||||||
origin: druid::Point,
|
origin: druid::Point,
|
||||||
current_rect: druid::Rect,
|
current_rect: druid::Rect,
|
||||||
}
|
},
|
||||||
|
ActiveSelection,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
|
|
@ -66,6 +67,19 @@ 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,
|
||||||
ctx: &EventCtx,
|
ctx: &EventCtx,
|
||||||
|
|
@ -88,6 +102,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
@ -125,6 +140,7 @@ 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));
|
||||||
|
|
@ -166,6 +182,7 @@ impl CanvasToolCtx {
|
||||||
thickness,
|
thickness,
|
||||||
stroke_color,
|
stroke_color,
|
||||||
});
|
});
|
||||||
|
*canvas.selected_elements_mut() = vector![];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -182,6 +199,7 @@ 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::SelectingRect {
|
self.state = CanvasToolState::SelectingRect {
|
||||||
origin: mouse_event.pos,
|
origin: mouse_event.pos,
|
||||||
|
|
@ -208,8 +226,12 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}).collect::<Vector<usize>>();
|
}).collect::<Vector<usize>>();
|
||||||
dbg!(selected_elements_idx);
|
if !elements.is_empty() {
|
||||||
self.state = CanvasToolState::Idle;
|
vcanvas.update(move |canvas: &mut Canvas| {
|
||||||
|
*canvas.selected_elements_mut() = selected_elements_idx;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self.state = CanvasToolState::ActiveSelection;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -219,6 +241,13 @@ impl CanvasToolCtx {
|
||||||
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),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue