mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 13:33:51 +00:00
Shapes: a Circle tool drag ends where it is let go
A release away from the last drag point now moves the circle's points to the release position before the circle is drawn, for a drag from the first press, from a second press and from a press on the first point. A release within 2 screen points of the press is still a click and keeps the point of the press.
This commit is contained in:
@@ -136,6 +136,14 @@ impl CircleTool {
|
||||
let (c, v) = command(points);
|
||||
vec![Action::Exec(c, v)]
|
||||
}
|
||||
|
||||
/// Is the press a drag at `ev`: dragged already, or `ev` is [`DRAG_PX`] or more from the press?
|
||||
fn drags(&self, cx: &ToolContext, ev: &PointerEvent) -> bool {
|
||||
match self.press {
|
||||
Some(Press::First { from, dragged, .. } | Press::Second { from, dragged, .. }) => dragged || ev.pos.distance(from) >= cx.tol(DRAG_PX),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tool for CircleTool {
|
||||
@@ -147,6 +155,11 @@ impl Tool for CircleTool {
|
||||
if !ev.pos.is_finite() {
|
||||
return vec![];
|
||||
}
|
||||
// A drag ends where it is let go: the release moves the points as a drag to it would. A
|
||||
// click, or a release within DRAG_PX of the press, keeps the points of the press.
|
||||
if ev.kind == PointerKind::Up && self.drags(cx, ev) {
|
||||
self.pointer(cx, &PointerEvent { kind: PointerKind::Drag, ..*ev });
|
||||
}
|
||||
match ev.kind {
|
||||
PointerKind::Move => {
|
||||
let leave = self.leave(cx, ev.mods);
|
||||
|
||||
@@ -94,6 +94,32 @@ fn a_click_and_a_drag_draw_the_circle_through_three_points() {
|
||||
assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (300.0, 360.0), Mods::default())), circle(&[(300.0, 300.0), (380.0, 300.0), (300.0, 360.0)]));
|
||||
}
|
||||
|
||||
/// A drag ends where it is let go, also when the release is away from the last drag: from a first
|
||||
/// press, from a second press, and from a press on the first point. A release within 2 screen
|
||||
/// points of a second press is still a click and keeps the point of the press.
|
||||
#[test]
|
||||
fn a_drag_ends_where_it_is_let_go() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) };
|
||||
let (down, drag, up) = (PointerKind::Down, PointerKind::Drag, PointerKind::Up);
|
||||
let mut t = CircleTool::default();
|
||||
t.pointer(&cx, &ev(down, (300.0, 300.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(drag, (380.0, 300.0), Mods::default()));
|
||||
assert_eq!(t.pointer(&cx, &ev(up, (400.0, 300.0), Mods::default())), circle(&[(300.0, 300.0), (400.0, 300.0)]));
|
||||
click(&mut t, &cx, (300.0, 300.0));
|
||||
t.pointer(&cx, &ev(down, (380.0, 300.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(drag, (300.0, 340.0), Mods::default()));
|
||||
assert_eq!(t.pointer(&cx, &ev(up, (300.0, 360.0), Mods::default())), circle(&[(300.0, 300.0), (380.0, 300.0), (300.0, 360.0)]));
|
||||
click(&mut t, &cx, (300.0, 300.0));
|
||||
t.pointer(&cx, &ev(down, (300.0, 300.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(drag, (300.0, 380.0), Mods::default()));
|
||||
assert_eq!(t.pointer(&cx, &ev(up, (300.0, 400.0), Mods::default())), circle(&[(300.0, 300.0), (300.0, 400.0)]));
|
||||
click(&mut t, &cx, (300.0, 300.0));
|
||||
t.pointer(&cx, &ev(down, (380.0, 360.0), Mods::default()));
|
||||
assert_eq!(t.pointer(&cx, &ev(up, (381.0, 360.0), Mods::default())), circle(&[(300.0, 300.0), (380.0, 360.0)]));
|
||||
}
|
||||
|
||||
/// Three points on one line show no circle, and the release draws nothing and starts afresh. A
|
||||
/// circle past the canvas is not shown either.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user