mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
193 lines
6.5 KiB
Rust
193 lines
6.5 KiB
Rust
//! The Pen and the Curvature Pen: anchors, handles, keys, Auto Add/Delete, the close cue and the tool's access
|
|
//! node, driven through `ToolFixture`.
|
|
|
|
use craft_behavior::kurbo::Point;
|
|
use craft_behavior::{CanvasView, EditEvent};
|
|
use craft_draw::ui::pen::{EditSubpath, Knot, PenEvent, PenTool};
|
|
use craft_ui::testing::{ToolFixture, trial};
|
|
use egui::{Event, Modifiers, PointerButton, Pos2, Rect, vec2};
|
|
|
|
/// The document point drawn at the middle of the canvas: the canvas shows (0, 0) to (400, 300) at zoom 1.
|
|
const MIDDLE: Point = Point::new(200.0, 150.0);
|
|
|
|
fn view_of(room: Rect) -> CanvasView {
|
|
CanvasView::new(room, MIDDLE, 1.0)
|
|
}
|
|
|
|
/// A pen on a canvas, with the events it reported so far.
|
|
struct Pen {
|
|
fixture: ToolFixture<PenTool>,
|
|
view: fn(Rect) -> CanvasView,
|
|
log: Vec<PenEvent>,
|
|
}
|
|
|
|
fn pen() -> Pen {
|
|
pen_with(PenTool::new(), view_of)
|
|
}
|
|
|
|
fn pen_with(tool: PenTool, view: fn(Rect) -> CanvasView) -> Pen {
|
|
Pen { fixture: ToolFixture::with_view("Pen", tool, view, |_, _| {}), view, log: Vec::new() }
|
|
}
|
|
|
|
fn button(at: Pos2, pressed: bool, modifiers: Modifiers) -> Event {
|
|
Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers }
|
|
}
|
|
|
|
fn doc(x: f64, y: f64) -> Point {
|
|
Point::new(x, y)
|
|
}
|
|
|
|
impl Pen {
|
|
fn sync(&mut self) {
|
|
let events = self.fixture.tool_mut().take_events();
|
|
self.log.extend(events);
|
|
}
|
|
|
|
/// The screen point of the document point `(x, y)`.
|
|
fn at(&self, x: f64, y: f64) -> Pos2 {
|
|
(self.view)(self.fixture.canvas().rect).to_screen(doc(x, y))
|
|
}
|
|
|
|
fn press(&mut self, at: Pos2, modifiers: Modifiers) {
|
|
self.fixture.fixture().hold(modifiers);
|
|
self.fixture.fixture().frame(vec![Event::PointerMoved(at)]);
|
|
self.fixture.fixture().frame(vec![button(at, true, modifiers)]);
|
|
self.sync();
|
|
}
|
|
|
|
fn move_to(&mut self, at: Pos2, modifiers: Modifiers) {
|
|
self.fixture.fixture().hold(modifiers);
|
|
self.fixture.fixture().frame(vec![Event::PointerMoved(at)]);
|
|
self.sync();
|
|
}
|
|
|
|
fn release(&mut self, at: Pos2, modifiers: Modifiers) {
|
|
self.fixture.fixture().frame(vec![button(at, false, modifiers)]);
|
|
self.fixture.fixture().hold(Modifiers::NONE);
|
|
self.fixture.fixture().frame(Vec::new());
|
|
self.sync();
|
|
}
|
|
|
|
fn click(&mut self, at: Pos2, modifiers: Modifiers) {
|
|
self.press(at, modifiers);
|
|
self.release(at, modifiers);
|
|
}
|
|
|
|
fn click_doc(&mut self, x: f64, y: f64) {
|
|
let at = self.at(x, y);
|
|
self.click(at, Modifiers::NONE);
|
|
}
|
|
|
|
/// A drag over several frames: press, two moves, release.
|
|
fn drag(&mut self, from: Pos2, to: Pos2, modifiers: Modifiers) {
|
|
self.press(from, modifiers);
|
|
self.move_to(from.lerp(to, 0.5), modifiers);
|
|
self.move_to(to, modifiers);
|
|
self.release(to, modifiers);
|
|
}
|
|
|
|
/// The draft as the host holds it: the last value the pen reported, empty once it finished.
|
|
fn draft(&self) -> EditSubpath {
|
|
let mut now = EditSubpath::default();
|
|
for event in &self.log {
|
|
match event {
|
|
PenEvent::Edit(EditEvent::Update { value, .. }) => now = value.clone(),
|
|
PenEvent::Edit(EditEvent::Commit { after, .. }) => now = after.clone(),
|
|
PenEvent::Finished => now = EditSubpath::default(),
|
|
_ => {}
|
|
}
|
|
}
|
|
now
|
|
}
|
|
|
|
/// The path the last `Commit` filed.
|
|
fn filed(&self) -> Option<EditSubpath> {
|
|
self.log.iter().rev().find_map(|event| match event {
|
|
PenEvent::Edit(EditEvent::Commit { after, .. }) => Some(after.clone()),
|
|
_ => None,
|
|
})
|
|
}
|
|
|
|
fn kinds(&self) -> Vec<&'static str> {
|
|
self.log
|
|
.iter()
|
|
.map(|event| match event {
|
|
PenEvent::Edit(EditEvent::Begin { .. }) => "begin",
|
|
PenEvent::Edit(EditEvent::Update { .. }) => "update",
|
|
PenEvent::Edit(EditEvent::Commit { .. }) => "commit",
|
|
PenEvent::Edit(EditEvent::Cancel { .. }) => "cancel",
|
|
PenEvent::Edit(_) => "other",
|
|
PenEvent::Finished => "finished",
|
|
PenEvent::Resumed(..) => "resumed",
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// A draft of three corner anchors at (60, 60), (160, 60), (160, 160).
|
|
fn three() -> Pen {
|
|
let mut pen = pen();
|
|
for (x, y) in [(60.0, 60.0), (160.0, 60.0), (160.0, 160.0)] {
|
|
pen.click_doc(x, y);
|
|
}
|
|
pen
|
|
}
|
|
}
|
|
|
|
#[trial]
|
|
fn a_press_on_the_first_anchor_closes_the_path_on_release_with_or_without_a_drag() {
|
|
let mut pen = Pen::three();
|
|
let first = pen.at(60.0, 60.0);
|
|
pen.press(first, Modifiers::NONE);
|
|
assert!(!pen.draft().closed, "closes on release");
|
|
pen.release(first, Modifiers::NONE);
|
|
let done = pen.filed().expect("commit");
|
|
assert!(pen.kinds().ends_with(&["commit", "finished"]), "{:?}", pen.kinds());
|
|
assert!(done.closed && done.knots.len() == 3 && done.knots[0] == Knot::corner(doc(60.0, 60.0)));
|
|
|
|
let mut pen = Pen::three();
|
|
let first = pen.at(60.0, 60.0);
|
|
pen.drag(first, first + vec2(-30.0, 20.0), Modifiers::NONE);
|
|
let done = pen.filed().expect("commit");
|
|
assert!(done.closed);
|
|
assert_eq!(done.knots[0], Knot::smooth(doc(60.0, 60.0), doc(30.0, 80.0)));
|
|
assert_eq!(done.knots.len(), 3, "no anchor added");
|
|
}
|
|
|
|
#[trial]
|
|
fn remove_last_waits_for_a_press_that_is_still_down() {
|
|
let mut pen = Pen::three();
|
|
let at = pen.at(260.0, 160.0);
|
|
pen.press(at, Modifiers::NONE);
|
|
assert!(!pen.fixture.tool_mut().remove_last(), "the press is held");
|
|
pen.release(at, Modifiers::NONE);
|
|
assert_eq!(pen.draft().knots.len(), 4);
|
|
assert!(pen.fixture.tool_mut().remove_last());
|
|
pen.sync();
|
|
assert_eq!(pen.draft().knots.len(), 3, "one request removes one anchor");
|
|
}
|
|
|
|
#[trial]
|
|
fn a_quick_drag_in_one_frame_pulls_the_handles() {
|
|
let mut pen = pen();
|
|
pen.click_doc(60.0, 60.0);
|
|
let (from, to) = (pen.at(160.0, 60.0), pen.at(160.0, 60.0) + vec2(40.0, 30.0));
|
|
pen.fixture.fixture().frame_through(vec![
|
|
Event::PointerMoved(from),
|
|
button(from, true, Modifiers::NONE),
|
|
Event::PointerMoved(to),
|
|
button(to, false, Modifiers::NONE),
|
|
]);
|
|
pen.sync();
|
|
assert_eq!(pen.draft().knots[1], Knot::smooth(doc(160.0, 60.0), doc(200.0, 90.0)));
|
|
}
|
|
|
|
#[trial]
|
|
fn the_pen_is_a_tool_every_canvas_accepts() {
|
|
let mut fixture = ToolFixture::with_view("Pen", PenTool::new(), view_of, |_, _| {});
|
|
fixture.check();
|
|
}
|
|
|
|
// ---- Curvature mode ----
|
|
|
|
// ---- Path modes and Path Options ----
|