mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 08:11:41 +00:00
Three-Point Arc: save the direction option, fix two placement cases
- `tool.setOption` (and the Tool Options dialog) now accepts `direction` as true or false and refuses other values or unknown options. - The first press counts as a drag when the pointer moves 2 pt from where it was pressed, not from the point it snapped to. - `]` pressed while the third point is held down places that point as the new mode keeps it.
This commit is contained in:
@@ -198,3 +198,23 @@ fn three_clicks_with_the_tool_draw_the_arc() {
|
||||
let id = *s.doc().unwrap().selection.objects.first().unwrap();
|
||||
assert!(farthest_from_path(&s, id, &[Point::new(140.0, 260.0)]) < 1e-9);
|
||||
}
|
||||
|
||||
/// `tool.setOption` (the Tool Options dialog's Save) sets the tool's `direction` option, and
|
||||
/// refuses a value that is not true or false, or an option the tool doesn't have.
|
||||
#[test]
|
||||
fn set_option_sets_the_direction_and_refuses_bad_values() {
|
||||
let mut s = session();
|
||||
for on in [true, false] {
|
||||
let set = s.set_tool_option_cmd(&json!({"tool": "threePointArc", "values": {"direction": on}})).unwrap();
|
||||
assert_eq!(set["direction"], json!(on));
|
||||
assert_eq!(s.tool_options_of("threePointArc")["direction"], json!(on));
|
||||
}
|
||||
let cases = [
|
||||
(json!({"tool": "threePointArc", "key": "direction", "value": "yes"}), "tool `threePointArc`: `direction` must be true or false"),
|
||||
(json!({"tool": "threePointArc", "key": "bogus", "value": true}), "tool `threePointArc`: unknown option `bogus`"),
|
||||
];
|
||||
for (p, want) in cases {
|
||||
assert_eq!(s.set_tool_option_cmd(&p).unwrap_err(), want, "{p}");
|
||||
}
|
||||
assert_eq!(s.tool_options_of("threePointArc")["direction"], json!(false));
|
||||
}
|
||||
|
||||
@@ -38,8 +38,9 @@ pub const TOO_LARGE: &str = "Arc too large";
|
||||
/// The press in progress.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum Press {
|
||||
/// The first end, pressed at `at`: a release after a drag places the second end there.
|
||||
Start { at: Point, dragged: bool },
|
||||
/// The first end, placed at `at` by a press at `from` (before snapping): a release after a
|
||||
/// drag places the second end there. The drag is measured from `from`.
|
||||
Start { at: Point, from: Point, dragged: bool },
|
||||
/// The second end, pressed after a click placed the first: the release places it.
|
||||
End,
|
||||
/// The third point, pressed once both ends are placed: the release draws the arc.
|
||||
@@ -112,8 +113,8 @@ impl ThreePointArcTool {
|
||||
let moved = self.raw.is_none_or(|(p, _)| p != ev.pos);
|
||||
self.raw = Some((ev.pos, ev.mods));
|
||||
match &mut self.press {
|
||||
Some(Press::Start { at, dragged }) => {
|
||||
*dragged |= moved && ev.pos.distance(*at) >= cx.tol(DRAG_PX);
|
||||
Some(Press::Start { at, from, dragged }) => {
|
||||
*dragged |= moved && ev.pos.distance(*from) >= cx.tol(DRAG_PX);
|
||||
let leave = Leave::segment(cx, *at, ev.mods.shift);
|
||||
self.pointer = Some(self.snap.drag(cx, ev.pos, Some(&leave)));
|
||||
}
|
||||
@@ -157,7 +158,7 @@ impl Tool for ThreePointArcTool {
|
||||
(None, _) => {
|
||||
let at = self.snap.press(cx, ev.pos, &[], None);
|
||||
self.start = Some(at);
|
||||
self.press = Some(Press::Start { at, dragged: false });
|
||||
self.press = Some(Press::Start { at, from: ev.pos, dragged: false });
|
||||
self.pointer = Some(at);
|
||||
}
|
||||
(Some(a), None) => {
|
||||
@@ -189,7 +190,7 @@ impl Tool for ThreePointArcTool {
|
||||
self.follow(cx, ev);
|
||||
}
|
||||
match (self.press.take(), self.start, self.pointer) {
|
||||
(Some(Press::Start { at, dragged: true }), _, Some(b)) if b.distance(at) >= cx.tol(DRAG_PX) => self.end = Some(b),
|
||||
(Some(Press::Start { at, dragged: true, .. }), _, Some(b)) if b.distance(at) >= cx.tol(DRAG_PX) => self.end = Some(b),
|
||||
(Some(Press::End), Some(a), Some(b)) if b.distance(a) > cx.tol(REACH_PX) => self.end = Some(b),
|
||||
(Some(Press::Third), Some(a), Some(c)) => {
|
||||
let Some(b) = self.end else { return vec![] };
|
||||
@@ -212,10 +213,16 @@ impl Tool for ThreePointArcTool {
|
||||
}
|
||||
}
|
||||
|
||||
fn key(&mut self, _cx: &ToolContext, key: ToolKey, _mods: Mods) -> Vec<Action> {
|
||||
fn key(&mut self, cx: &ToolContext, key: ToolKey, _mods: Mods) -> Vec<Action> {
|
||||
match key {
|
||||
ToolKey::Escape => self.clear(),
|
||||
ToolKey::BracketRight if self.start.is_some() => self.direction = !self.direction,
|
||||
ToolKey::BracketRight if self.start.is_some() => {
|
||||
self.direction = !self.direction;
|
||||
// A press in progress: the point being placed keeps to the new mode.
|
||||
if let (Some(_), Some((p, mods))) = (self.press, self.raw) {
|
||||
self.follow(cx, &PointerEvent::new(PointerKind::Drag, p.x, p.y).with_mods(mods));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
vec![]
|
||||
@@ -305,6 +312,14 @@ impl Tool for ThreePointArcTool {
|
||||
self.direction = on;
|
||||
}
|
||||
}
|
||||
|
||||
fn check_option(&self, key: &str, v: &Value) -> Result<(), String> {
|
||||
use crate::option_check as c;
|
||||
match key {
|
||||
"direction" => c::boolean(key, v),
|
||||
_ => Err(c::unknown(key)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -188,6 +188,44 @@ fn the_direction_option_takes_a_bool() {
|
||||
assert_eq!(t.options(), json!({ "direction": false }));
|
||||
}
|
||||
|
||||
/// `]` pressed while the third point is held down places the point as the new mode keeps it: the
|
||||
/// point Shift kept on the direction line goes back to where the pointer is, and the other way.
|
||||
#[test]
|
||||
fn bracket_during_a_press_places_the_third_point_in_the_new_mode() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) };
|
||||
let mut t = ThreePointArcTool::default();
|
||||
let shift = Mods { shift: true, ..Mods::default() };
|
||||
t.set_option("direction", &json!(true));
|
||||
drag(&mut t, &cx, (300.0, 300.0), &[(380.0, 300.0)], Mods::default());
|
||||
t.pointer(&cx, &ev(PointerKind::Down, (303.0, 250.0), shift));
|
||||
t.key(&cx, ToolKey::BracketRight, Mods::default());
|
||||
assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (303.0, 250.0), shift)), through((300.0, 300.0), (380.0, 300.0), (303.0, 250.0)));
|
||||
// Through mode first, switched to direction mode during the press: Shift keeps the line upright.
|
||||
drag(&mut t, &cx, (300.0, 300.0), &[(380.0, 300.0)], Mods::default());
|
||||
t.pointer(&cx, &ev(PointerKind::Down, (303.0, 250.0), shift));
|
||||
t.key(&cx, ToolKey::BracketRight, Mods::default());
|
||||
let acts = t.pointer(&cx, &ev(PointerKind::Up, (303.0, 250.0), shift));
|
||||
let [Action::Exec(_, params)] = acts.as_slice() else { panic!("{acts:?}") };
|
||||
assert!(params["toward"][0].as_f64().is_some_and(|x| (x - 300.0).abs() < 1e-9) && params["at"] == "start", "{params}");
|
||||
}
|
||||
|
||||
/// With Snap to Point, a first press that snaps to the rectangle's corner, 1.5 pt from the
|
||||
/// pointer, and moves 1.9 pt before it is let go is a click: the drag is measured from the press,
|
||||
/// not from the corner, so only the first end is placed.
|
||||
#[test]
|
||||
fn a_short_move_from_a_snapped_press_is_not_a_drag() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, snap_to_point: true, ..cx(&d, &s, &p) };
|
||||
let mut t = ThreePointArcTool::default();
|
||||
t.pointer(&cx, &ev(PointerKind::Down, (201.5, 200.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(PointerKind::Drag, (203.4, 200.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(PointerKind::Up, (203.4, 200.0), Mods::default()));
|
||||
assert_eq!((t.start, t.end), (Some(Point::new(200.0, 200.0)), None));
|
||||
}
|
||||
|
||||
/// A release away from the last drag event places the point where the pointer is let go, for
|
||||
/// the second end and for the third point.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user