WIP: Rectangular selections #37
|
|
@ -71,6 +71,10 @@ 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,
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,13 @@ 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@ use crate::canvas::{Canvas, CanvasElement};
|
||||||
use crate::history::VersionedCanvas;
|
use crate::history::VersionedCanvas;
|
||||||
|
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx};
|
use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext};
|
||||||
|
|
||||||
|
use im::Vector;
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub enum CanvasToolState {
|
pub enum CanvasToolState {
|
||||||
|
|
@ -29,6 +31,10 @@ pub enum CanvasToolState {
|
||||||
current_path: CanvasElement,
|
current_path: CanvasElement,
|
||||||
},
|
},
|
||||||
Erasing,
|
Erasing,
|
||||||
|
SelectingRect {
|
||||||
|
origin: druid::Point,
|
||||||
|
current_rect: druid::Rect,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
|
|
@ -42,6 +48,7 @@ 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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -69,6 +76,7 @@ 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(&ctx, &event, &mut vcanvas, &env),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,7 +145,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));
|
||||||
|
|
@ -166,6 +174,47 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_rectangular_select_event(
|
||||||
|
&mut self,
|
||||||
|
_ctx: &EventCtx,
|
||||||
|
event: &Event,
|
||||||
|
vcanvas: &mut VersionedCanvas,
|
||||||
|
_env: &Env,
|
||||||
|
) {
|
||||||
|
match (&mut self.state, 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>>();
|
||||||
|
dbg!(selected_elements_idx);
|
||||||
|
self.state = CanvasToolState::Idle;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn needs_repaint(&self) -> bool {
|
pub fn needs_repaint(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
@ -173,6 +222,7 @@ impl CanvasToolCtx {
|
||||||
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,6 +34,9 @@ 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