mod hand;
mod pen;
pub(crate) mod select;
mod shape;
mod text;
pub use hand::HandTool;
use kurbo::Point;
pub use pen::PenTool;
pub use select::SelectTool;
pub use shape::ShapeTool;
pub use text::TextTool;
use crate::CanvasError;
use crate::CanvasState;
use crate::Cursor;
use crate::Input;
use crate::OverlayItem;
use crate::input::Phase;
use crate::input::PointerEvent;
pub const DRAG_THRESHOLD: f64 = 3.0;
pub const HIT_TOLERANCE: f64 = 4.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Outcome {
#[default]
Idle,
Redraw,
EditText(canvist_scene::NodeId),
Done,
}
impl Outcome {
#[must_use]
pub fn and(self, other: Self) -> Self {
match (self, other) {
(Self::Idle, other) => other,
(this, Self::Idle | Self::Redraw) => this,
(_, other) => other,
}
}
}
pub trait Tool: Send {
fn name(&self) -> &str;
fn input(&mut self, canvas: &mut CanvasState, input: &Input) -> Result<Outcome, CanvasError>;
fn overlay(&self, canvas: &CanvasState, out: &mut Vec<OverlayItem>) {
let _ = (canvas, out);
}
fn cursor(&self, canvas: &CanvasState) -> Cursor {
let _ = canvas;
Cursor::Default
}
fn cancel(&mut self, canvas: &mut CanvasState) {
let _ = canvas;
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Press {
pub(crate) start: Point,
pub(crate) dragging: bool,
}
impl Press {
pub(crate) fn new(start: Point) -> Self {
Self {
start,
dragging: false,
}
}
pub(crate) fn moved(&mut self, position: Point) -> bool {
self.dragging |= position.distance(self.start) > DRAG_THRESHOLD;
self.dragging
}
}
pub(crate) fn pointer(input: &Input) -> Option<&PointerEvent> {
match input {
Input::Pointer(event) => Some(event),
_ => None,
}
}
pub(crate) fn is_primary_down(event: &PointerEvent) -> bool {
event.phase == Phase::Down && event.buttons & 1 == 1
}