mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:05:03 +00:00
Merge pull request #8178 from dotCooCoo/feat/image-trace-split-view
Image Trace: Split View shows the source image and the result on either side of a divider
This commit is contained in:
@@ -47,6 +47,7 @@ pub mod slice;
|
||||
pub mod symbolism;
|
||||
pub mod text;
|
||||
pub mod touchtype;
|
||||
pub mod trace_split;
|
||||
pub mod typewidget;
|
||||
pub mod xform;
|
||||
|
||||
|
||||
@@ -184,4 +184,29 @@ mod tests {
|
||||
spec.kind = RepeatKind::Mirror { angle: f64::NAN, center: Point::ZERO };
|
||||
assert!(MirrorAxis::of(&doc, &selection, 1.0).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tool_switch_cancels_a_mirror_axis_drag() {
|
||||
use crate::{PointerEvent, PointerKind, Tool};
|
||||
let (mut doc, id) = doc_with_rect();
|
||||
let mut source = doc.node(id).unwrap().clone();
|
||||
source.id = doc.alloc_id();
|
||||
doc.node_mut(id).unwrap().kind = NodeKind::Repeat(RepeatSpec::mirror(vec![Arc::new(source)], 90.0, 10.0));
|
||||
let mut selection = Selection::default();
|
||||
selection.set([id]);
|
||||
let paint = paint();
|
||||
let c = cx(&doc, &selection, &paint);
|
||||
let axis = MirrorAxis::of(&doc, &selection, c.zoom).unwrap();
|
||||
let mut t = crate::select::SelectionTool::default();
|
||||
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, axis.center.x, axis.center.y)).is_empty());
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, axis.center.x + 20.0, axis.center.y));
|
||||
assert!(matches!(a.first(), Some(Action::Begin(l)) if l == "Repeat Options"), "{a:?}");
|
||||
assert_eq!(t.deactivate(&c), vec![Action::Cancel]);
|
||||
assert!(!t.busy());
|
||||
// A press that has not moved the axis leaves nothing to cancel.
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, axis.center.x, axis.center.y));
|
||||
assert!(t.busy());
|
||||
assert!(t.deactivate(&c).is_empty());
|
||||
assert!(!t.busy());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,4 +392,24 @@ mod tests {
|
||||
assert_eq!(cursor(&c, g.rows_at), Some(Cursor::ResizeV));
|
||||
assert_eq!(cursor(&c, g.corner_at), Some(Cursor::ResizeNwSe));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tool_switch_cancels_a_grid_handle_drag() {
|
||||
use crate::{PointerEvent, PointerKind, Tool};
|
||||
let (doc, selection) = grid();
|
||||
let paint = paint();
|
||||
let c = cx(&doc, &selection, &paint);
|
||||
let g = GridHandles::of(&doc, &selection, crate::select::selection_box(&c), c.zoom).unwrap();
|
||||
let mut t = crate::select::SelectionTool::default();
|
||||
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, g.rows_at.x, g.rows_at.y)).is_empty());
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, g.rows_at.x, g.rows_at.y + 150.0));
|
||||
assert!(preview(&a).is_some(), "{a:?}");
|
||||
assert_eq!(t.deactivate(&c), vec![Action::Cancel]);
|
||||
assert!(!t.busy());
|
||||
// A press that has not changed the grid leaves nothing to cancel.
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, g.rows_at.x, g.rows_at.y));
|
||||
assert!(t.busy());
|
||||
assert!(t.deactivate(&c).is_empty());
|
||||
assert!(!t.busy());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -681,4 +681,26 @@ mod tests {
|
||||
assert_eq!(preview(&d.drag(h.instances_at + Vec2::new(0.0, 32.0))), Some(&json!({"instances": 9})));
|
||||
assert!(matches!(d.finish().as_slice(), [Action::Commit]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tool_switch_cancels_a_radial_handle_drag() {
|
||||
use crate::{PointerEvent, PointerKind, Tool};
|
||||
let (doc, selection) = radial();
|
||||
let paint = paint();
|
||||
let c = cx(&doc, &selection, &paint);
|
||||
let h = RadialHandles::of(&doc, &selection, crate::select::selection_box(&c), c.zoom).unwrap();
|
||||
let out = (h.radius_at - h.center) / 100.0;
|
||||
let to = h.radius_at + out * 25.0;
|
||||
let mut t = crate::select::SelectionTool::default();
|
||||
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, h.radius_at.x, h.radius_at.y)).is_empty());
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, to.x, to.y));
|
||||
assert_eq!(preview(&a), Some(&json!({"radius": 125.0})), "{a:?}");
|
||||
assert_eq!(t.deactivate(&c), vec![Action::Cancel]);
|
||||
assert!(!t.busy());
|
||||
// A press that has not changed the repeat leaves nothing to cancel.
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, h.radius_at.x, h.radius_at.y));
|
||||
assert!(t.busy());
|
||||
assert!(t.deactivate(&c).is_empty());
|
||||
assert!(!t.busy());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +153,8 @@ enum State {
|
||||
GridRepeat(crate::repeat_grid::GridDrag),
|
||||
/// Drag a radial repeat's instances or radius handle.
|
||||
RadialRepeat(crate::repeat_radial::RadialDrag),
|
||||
/// Drag the divider of an Image Trace object in the Split view.
|
||||
TraceSplit(crate::trace_split::SplitDrag),
|
||||
/// Dragging a bracket of type on a path.
|
||||
Bracket(BracketDrag),
|
||||
/// Dragging a 9-slice guide of the symbol being edited.
|
||||
@@ -831,7 +833,7 @@ impl Tool for SelectionTool {
|
||||
/// E during a marquee drag switches the marquee between the objects it touches and only the
|
||||
/// objects inside it. Escape during a drag (a move or an Alt-drag copy, a bounding-box scale or
|
||||
/// rotation, a marquee, a corner widget, a pie widget, a polygon's side widget, a blend widget, the Rotate All, Space, Move All or Select/Move widget, the brackets of type on a path, a mirror
|
||||
/// axis, a grid repeat's handles, a ruler guide) cancels it: what it changed goes back, no copy
|
||||
/// axis, a grid repeat's handles, an Image Trace divider, a ruler guide) cancels it: what it changed goes back, no copy
|
||||
/// is left, and the tool waits for the next press. With the pointer loaded with text, Escape
|
||||
/// drops the text.
|
||||
fn key(&mut self, cx: &ToolContext, key: ToolKey, mods: Mods) -> Vec<Action> {
|
||||
@@ -905,14 +907,19 @@ impl Tool for SelectionTool {
|
||||
out
|
||||
}
|
||||
|
||||
/// Switching tools during a drag of a blend widget, a corner widget, a pie widget or a
|
||||
/// polygon's side widget cancels it, as Escape does: its value goes back.
|
||||
/// Switching tools during a drag of a blend widget, a corner widget, a pie widget, a
|
||||
/// polygon's side widget, a mirror axis, a grid or radial repeat's handles or an Image Trace
|
||||
/// divider cancels it, as Escape does: its value goes back.
|
||||
fn deactivate(&mut self, _cx: &ToolContext) -> Vec<Action> {
|
||||
match std::mem::take(&mut self.state) {
|
||||
State::BlendWidget(d) if d.adjusting() => vec![Action::Cancel],
|
||||
State::Corner(c) if c.adjusting() => vec![Action::Cancel],
|
||||
State::Pie(d) if d.adjusting() => vec![Action::Cancel],
|
||||
State::Sides(d) => d.cancel(),
|
||||
State::Mirror(d) => d.cancel(),
|
||||
State::GridRepeat(d) => d.cancel(),
|
||||
State::RadialRepeat(d) => d.cancel(),
|
||||
State::TraceSplit(d) => d.cancel(),
|
||||
State::BlendWidget(_) | State::Corner(_) | State::Pie(_) => vec![],
|
||||
other => {
|
||||
self.state = other;
|
||||
@@ -926,7 +933,9 @@ impl Tool for SelectionTool {
|
||||
State::Marquee { .. } => key == ToolKey::Letter('e'),
|
||||
State::Corner(_) => matches!(key, ToolKey::Up | ToolKey::Down),
|
||||
State::BlendWidget(_) => matches!(key, ToolKey::Up | ToolKey::Down | ToolKey::Left | ToolKey::Right),
|
||||
State::Loaded { .. } | State::Mirror(_) | State::GridRepeat(_) | State::RadialRepeat(_) | State::NineSlice(_) => key == ToolKey::Escape,
|
||||
State::Loaded { .. } | State::Mirror(_) | State::GridRepeat(_) | State::RadialRepeat(_) | State::TraceSplit(_) | State::NineSlice(_) => {
|
||||
key == ToolKey::Escape
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -970,6 +979,7 @@ impl Tool for SelectionTool {
|
||||
State::Mirror(d) => d.adjusting(),
|
||||
State::GridRepeat(d) => d.adjusting(),
|
||||
State::RadialRepeat(d) => d.adjusting(),
|
||||
State::TraceSplit(d) => d.adjusting(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -1296,6 +1306,9 @@ impl Tool for SelectionTool {
|
||||
if let State::RadialRepeat(d) = &self.state {
|
||||
return d.cursor(cx);
|
||||
}
|
||||
if let State::TraceSplit(d) = &self.state {
|
||||
return d.cursor(cx);
|
||||
}
|
||||
if matches!(self.state, State::NineSlice(_)) || (matches!(self.state, State::Idle) && crate::nineslice::over_guide(cx, p)) {
|
||||
return Cursor::Move;
|
||||
}
|
||||
@@ -1303,8 +1316,10 @@ impl Tool for SelectionTool {
|
||||
&& !m.cmd
|
||||
&& !m.ctrl
|
||||
&& !m.alt
|
||||
&& let Some(cursor) =
|
||||
crate::mirror::cursor(cx, p).or_else(|| crate::repeat_grid::cursor(cx, p)).or_else(|| crate::repeat_radial::cursor(cx, p))
|
||||
&& let Some(cursor) = crate::mirror::cursor(cx, p)
|
||||
.or_else(|| crate::repeat_grid::cursor(cx, p))
|
||||
.or_else(|| crate::repeat_radial::cursor(cx, p))
|
||||
.or_else(|| crate::trace_split::cursor(cx, p))
|
||||
{
|
||||
return cursor;
|
||||
}
|
||||
@@ -1668,6 +1683,21 @@ impl SelectionTool {
|
||||
_ => return Some(vec![]),
|
||||
}
|
||||
}
|
||||
if let State::TraceSplit(mut d) = self.state.clone() {
|
||||
match ev.kind {
|
||||
PointerKind::Drag | PointerKind::Up => {
|
||||
let mut out = d.drag(cx, ev.pos);
|
||||
if ev.kind == PointerKind::Up {
|
||||
out.extend(d.finish());
|
||||
self.state = State::Idle;
|
||||
} else {
|
||||
self.state = State::TraceSplit(d);
|
||||
}
|
||||
return Some(out);
|
||||
}
|
||||
_ => return Some(vec![]),
|
||||
}
|
||||
}
|
||||
if matches!(self.state, State::Idle)
|
||||
&& ev.kind == PointerKind::Down
|
||||
&& let Some(d) = crate::mirror::MirrorDrag::hit(cx, ev.pos, ev.mods)
|
||||
@@ -1692,6 +1722,14 @@ impl SelectionTool {
|
||||
self.hover = None;
|
||||
return Some(vec![]);
|
||||
}
|
||||
if matches!(self.state, State::Idle)
|
||||
&& ev.kind == PointerKind::Down
|
||||
&& let Some(d) = crate::trace_split::SplitDrag::hit(cx, ev.pos, ev.mods)
|
||||
{
|
||||
self.state = State::TraceSplit(d);
|
||||
self.hover = None;
|
||||
return Some(vec![]);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
//! The divider of an Image Trace object in the Split view: the source image shows on its left
|
||||
//! (in the image's own pixels) and the tracing result on its right. Drawing and hit testing share
|
||||
//! the document-space geometry here; a drag previews `imageTrace.setView` and commits it as one
|
||||
//! undo step.
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::{Document, NodeId, NodeKind, Selection, TraceView};
|
||||
use vectorcraft_geom::{Affine, Point};
|
||||
|
||||
use crate::{Action, Cursor, Mods, ToolContext};
|
||||
|
||||
/// How near (screen points) the pointer must come to the divider to take it.
|
||||
pub const HIT_PX: f64 = 5.0;
|
||||
|
||||
/// The divider's half length past each end of its grip, in screen points.
|
||||
pub const GRIP_HALF: f32 = 10.0;
|
||||
|
||||
/// The Split view's divider of one selected Image Trace object, in document space.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct TraceDivider {
|
||||
/// The Image Trace object.
|
||||
pub id: NodeId,
|
||||
/// The fraction of the source image's width, from its left edge, that shows the image.
|
||||
pub split: f64,
|
||||
/// Where the divider meets the image's top and bottom edges.
|
||||
pub ends: [Point; 2],
|
||||
/// The source image's pixel-to-document transform.
|
||||
xf: Affine,
|
||||
width: f64,
|
||||
}
|
||||
|
||||
impl TraceDivider {
|
||||
/// The divider of the one Image Trace object selected whole, when it shows the Split view.
|
||||
pub fn of(doc: &Document, selection: &Selection, zoom: f64) -> Option<Self> {
|
||||
let [id] = selection.objects.as_slice() else { return None };
|
||||
if !selection.anchors.is_empty() || !selection.segments.is_empty() || !doc.is_editable(*id) || !zoom.is_finite() || zoom <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let n = doc.node(*id)?;
|
||||
if n.extras().trace.is_none() || n.trace_view() != TraceView::Split {
|
||||
return None;
|
||||
}
|
||||
let NodeKind::Group { children, clip: false } = &n.kind else { return None };
|
||||
let NodeKind::Image(im) = &children.first()?.kind else { return None };
|
||||
let (width, height) = (f64::from(im.width), f64::from(im.height));
|
||||
let det = im.xf.determinant();
|
||||
if width <= 0.0 || height <= 0.0 || !det.is_finite() || det.abs() < 1e-12 || !im.xf.as_coeffs().iter().all(|c| c.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let split = n.trace_split();
|
||||
let at = width * split;
|
||||
let ends = [im.xf * Point::new(at, 0.0), im.xf * Point::new(at, height)];
|
||||
ends.iter().all(|p| finite(*p)).then_some(Self { id: *id, split, ends, xf: im.xf, width })
|
||||
}
|
||||
|
||||
/// The divider position for document point `p`: the fraction of the image's width left of
|
||||
/// it, from 0 to 1, rounded to a ten-thousandth.
|
||||
pub fn split_at(&self, p: Point) -> Option<f64> {
|
||||
let local = self.xf.inverse() * p;
|
||||
let f = (local.x / self.width).clamp(0.0, 1.0);
|
||||
f.is_finite().then(|| (f * 10_000.0).round() / 10_000.0)
|
||||
}
|
||||
|
||||
/// How far the image's left-to-right direction is turned (counter-clockwise degrees on the
|
||||
/// page), for the resize cursor.
|
||||
fn angle(&self) -> f64 {
|
||||
let across = self.xf * Point::new(1.0, 0.0) - self.xf * Point::ZERO;
|
||||
(-across.y).atan2(across.x).to_degrees()
|
||||
}
|
||||
|
||||
/// The divider under document point `p`. Nothing with the bounding box hidden or in Outline
|
||||
/// mode, where the canvas draws neither the divider nor the split.
|
||||
fn hit(cx: &ToolContext, p: Point) -> Option<Self> {
|
||||
if !cx.show_bbox || cx.outline || !finite(p) {
|
||||
return None;
|
||||
}
|
||||
let d = Self::of(cx.doc, cx.selection, cx.zoom)?;
|
||||
let [a, b] = d.ends;
|
||||
let v = b - a;
|
||||
let length = v.hypot();
|
||||
if !length.is_finite() || length <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let unit = v / length;
|
||||
let along = (p - a).dot(unit).clamp(0.0, length);
|
||||
// A bounding-box handle or a shown center point under the pointer keeps its own drag.
|
||||
((a + unit * along).distance(p) <= cx.tol(HIT_PX)
|
||||
&& !crate::select::bbox_handle_at(cx, p)
|
||||
&& crate::select::hit_center(cx, p, cx.point_tol()).is_none())
|
||||
.then_some(d)
|
||||
}
|
||||
}
|
||||
|
||||
fn finite(p: Point) -> bool {
|
||||
p.x.is_finite() && p.y.is_finite()
|
||||
}
|
||||
|
||||
fn resize_cursor(d: &TraceDivider, cx: &ToolContext) -> Cursor {
|
||||
crate::select::handle_cursor(crate::bbox::Handle::Right, d.angle() - cx.view_angle())
|
||||
}
|
||||
|
||||
/// The resize cursor over the divider at `p`, turned with the image and the view.
|
||||
pub fn cursor(cx: &ToolContext, p: Point) -> Option<Cursor> {
|
||||
TraceDivider::hit(cx, p).map(|d| resize_cursor(&d, cx))
|
||||
}
|
||||
|
||||
/// A drag of the divider: it previews `imageTrace.setView` while the pointer moves and commits
|
||||
/// it as one undo step.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SplitDrag {
|
||||
divider: TraceDivider,
|
||||
start: Point,
|
||||
began: bool,
|
||||
}
|
||||
|
||||
impl SplitDrag {
|
||||
pub fn hit(cx: &ToolContext, p: Point, mods: Mods) -> Option<Self> {
|
||||
if mods.cmd || mods.ctrl || mods.alt {
|
||||
return None;
|
||||
}
|
||||
let divider = TraceDivider::hit(cx, p)?;
|
||||
Some(Self { divider, start: p, began: false })
|
||||
}
|
||||
|
||||
pub fn adjusting(&self) -> bool {
|
||||
self.began
|
||||
}
|
||||
|
||||
pub fn cursor(&self, cx: &ToolContext) -> Cursor {
|
||||
resize_cursor(&self.divider, cx)
|
||||
}
|
||||
|
||||
pub fn drag(&mut self, cx: &ToolContext, p: Point) -> Vec<Action> {
|
||||
if !finite(p) || (!self.began && p.distance(self.start) < cx.tol(3.0)) {
|
||||
return vec![];
|
||||
}
|
||||
let Some(split) = self.divider.split_at(p) else { return vec![] };
|
||||
let mut out = vec![];
|
||||
if !self.began {
|
||||
self.began = true;
|
||||
out.push(Action::Begin("Image Trace View".into()));
|
||||
}
|
||||
out.push(Action::Preview("imageTrace.setView".into(), json!({"id": self.divider.id.0, "view": TraceView::Split.id(), "split": split})));
|
||||
out
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Vec<Action> {
|
||||
if self.began { vec![Action::Commit] } else { vec![] }
|
||||
}
|
||||
|
||||
pub fn cancel(self) -> Vec<Action> {
|
||||
if self.began { vec![Action::Cancel] } else { vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::testutil::{cx, paint};
|
||||
use std::sync::Arc;
|
||||
use vectorcraft_doc::{ImageObject, Node};
|
||||
|
||||
/// A document with an Image Trace object over a 100 × 50 image placed by `xf`, in `view`.
|
||||
fn traced(xf: Affine, view: &str, split: f64) -> (Document, NodeId) {
|
||||
let mut doc = Document::new(400.0, 400.0);
|
||||
let im = ImageObject { key: "k".into(), width: 100, height: 50, xf, link: None, placement: Default::default(), pixels: None };
|
||||
let mut source = Node::new(doc.alloc_id(), NodeKind::Image(Box::new(im)));
|
||||
source.visible = false;
|
||||
let mut g = Node::group(doc.alloc_id(), vec![Arc::new(source)]);
|
||||
g.extras_mut().trace = Some(Box::new(json!({"view": view, "split": split})));
|
||||
let id = g.id;
|
||||
let layer = doc.layers[0].id;
|
||||
doc.insert(Some(layer), 0, g).unwrap();
|
||||
(doc, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_divider_shows_only_for_one_selected_split_view_and_follows_the_image() {
|
||||
let paint = paint();
|
||||
let mut selection = Selection::default();
|
||||
let (doc, id) = traced(Affine::translate((10.0, 20.0)), "split", 0.25);
|
||||
assert!(TraceDivider::of(&doc, &selection, 1.0).is_none(), "nothing selected");
|
||||
selection.set([id]);
|
||||
let d = TraceDivider::of(&doc, &selection, 1.0).unwrap();
|
||||
assert_eq!((d.split, d.ends), (0.25, [Point::new(35.0, 20.0), Point::new(35.0, 70.0)]));
|
||||
for zoom in [0.0, -1.0, f64::NAN] {
|
||||
assert!(TraceDivider::of(&doc, &selection, zoom).is_none());
|
||||
}
|
||||
let (other, id) = traced(Affine::IDENTITY, "sourceImage", 0.25);
|
||||
selection.set([id]);
|
||||
assert!(TraceDivider::of(&other, &selection, 1.0).is_none(), "another view");
|
||||
// A turned and scaled image: the divider runs along the image's own height.
|
||||
let xf = Affine::translate((200.0, 100.0)) * Affine::rotate(std::f64::consts::FRAC_PI_2) * Affine::scale(2.0);
|
||||
let (doc, id) = traced(xf, "split", 0.5);
|
||||
selection.set([id]);
|
||||
let d = TraceDivider::of(&doc, &selection, 1.0).unwrap();
|
||||
assert!(d.ends[0].distance(Point::new(200.0, 200.0)) < 1e-9 && d.ends[1].distance(Point::new(100.0, 200.0)) < 1e-9, "{:?}", d.ends);
|
||||
assert_eq!(d.split_at(Point::new(150.0, 150.0)), Some(0.25));
|
||||
assert_eq!(d.split_at(Point::new(150.0, 900.0)), Some(1.0), "clamped");
|
||||
let c = cx(&doc, &selection, &paint);
|
||||
assert_eq!(cursor(&c, Point::new(150.0, 200.0)), Some(Cursor::ResizeV));
|
||||
assert_eq!(cursor(&c, Point::new(150.0, 210.0)), None, "past the hit distance");
|
||||
let mut c = cx(&doc, &selection, &paint);
|
||||
c.show_bbox = false;
|
||||
assert_eq!(cursor(&c, Point::new(150.0, 200.0)), None, "bounding box hidden");
|
||||
assert!(SplitDrag::hit(&c, Point::new(150.0, 200.0), Mods::default()).is_none(), "bounding box hidden");
|
||||
let mut c = cx(&doc, &selection, &paint);
|
||||
c.outline = true;
|
||||
assert!(SplitDrag::hit(&c, Point::new(150.0, 200.0), Mods::default()).is_none(), "Outline mode");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dragging_the_divider_previews_the_view_and_commits_once() {
|
||||
let paint = paint();
|
||||
let mut selection = Selection::default();
|
||||
let (doc, id) = traced(Affine::translate((10.0, 20.0)), "split", 0.5);
|
||||
selection.set([id]);
|
||||
for zoom in [0.5, 1.0, 4.0] {
|
||||
let mut c = cx(&doc, &selection, &paint);
|
||||
c.zoom = zoom;
|
||||
let on = Point::new(60.0 + 4.0 / zoom, 40.0);
|
||||
assert_eq!(cursor(&c, on), Some(Cursor::ResizeH), "zoom {zoom}");
|
||||
assert!(SplitDrag::hit(&c, Point::new(60.0 + 6.0 / zoom, 40.0), Mods::default()).is_none(), "zoom {zoom}");
|
||||
assert!(SplitDrag::hit(&c, on, Mods { alt: true, ..Default::default() }).is_none());
|
||||
let mut drag = SplitDrag::hit(&c, on, Mods::default()).unwrap();
|
||||
assert!(drag.drag(&c, on + vectorcraft_geom::Vec2::new(1.0 / zoom, 0.0)).is_empty(), "under the drag threshold");
|
||||
assert!(drag.drag(&c, Point::new(f64::NAN, 0.0)).is_empty());
|
||||
let out = drag.drag(&c, Point::new(85.0, 300.0));
|
||||
assert!(
|
||||
matches!(&out[..], [Action::Begin(_), Action::Preview(id, p)]
|
||||
if id == "imageTrace.setView" && p["split"] == json!(0.75) && p["view"] == "split"),
|
||||
"{out:?}"
|
||||
);
|
||||
let out = drag.drag(&c, Point::new(-50.0, 30.0));
|
||||
assert!(matches!(&out[..], [Action::Preview(_, p)] if p["split"] == json!(0.0)), "{out:?}");
|
||||
assert!(drag.adjusting());
|
||||
assert!(matches!(drag.clone().finish()[..], [Action::Commit]));
|
||||
assert!(matches!(drag.cancel()[..], [Action::Cancel]));
|
||||
}
|
||||
}
|
||||
|
||||
/// With Show Center on, a press on the center point where the divider crosses it moves the
|
||||
/// object; the rest of the divider still takes a press.
|
||||
#[test]
|
||||
fn the_center_point_keeps_its_drag_on_the_divider() {
|
||||
use vectorcraft_doc::{Appearance, LiveShape};
|
||||
let paint = paint();
|
||||
let mut selection = Selection::default();
|
||||
let (mut doc, id) = traced(Affine::translate((10.0, 20.0)), "split", 0.5);
|
||||
let xf = Affine::translate((10.0, 20.0));
|
||||
let shape = LiveShape::Rectangle { w: 100.0, h: 50.0, radii: [0.0; 4], kinds: Default::default(), radii_relative: false, xf };
|
||||
let path = Node::path(doc.alloc_id(), shape.to_path(), Appearance::default_art());
|
||||
doc.insert(Some(id), 1, path).unwrap();
|
||||
doc.node_mut(id).unwrap().edit_attrs(|a| a.show_center = Some(true));
|
||||
selection.set([id]);
|
||||
let c = cx(&doc, &selection, &paint);
|
||||
assert_eq!(crate::select::hit_center(&c, Point::new(60.0, 45.0), c.point_tol()).map(|(i, _)| i), Some(id));
|
||||
assert_eq!(cursor(&c, Point::new(60.0, 45.0)), None, "the center point");
|
||||
assert!(SplitDrag::hit(&c, Point::new(60.0, 45.0), Mods::default()).is_none(), "the center point");
|
||||
assert_eq!(cursor(&c, Point::new(60.0, 33.0)), Some(Cursor::ResizeH));
|
||||
}
|
||||
|
||||
/// Switching away from the Selection tool during a divider drag cancels it, as Escape does:
|
||||
/// the view goes back. After a press that has not moved there is nothing to put back.
|
||||
#[test]
|
||||
fn a_tool_switch_cancels_a_divider_drag() {
|
||||
use crate::{PointerEvent, PointerKind, Tool};
|
||||
let paint = paint();
|
||||
let mut selection = Selection::default();
|
||||
let (doc, id) = traced(Affine::translate((10.0, 20.0)), "split", 0.5);
|
||||
selection.set([id]);
|
||||
let c = cx(&doc, &selection, &paint);
|
||||
let mut t = crate::select::SelectionTool::default();
|
||||
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, 60.0, 40.0)).is_empty());
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 85.0, 40.0));
|
||||
assert!(matches!(a.first(), Some(Action::Begin(_))), "{a:?}");
|
||||
assert_eq!(t.deactivate(&c), vec![Action::Cancel]);
|
||||
assert!(!t.busy() && !t.transforming());
|
||||
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Up, 85.0, 40.0)).is_empty());
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, 60.0, 40.0));
|
||||
assert!(t.busy());
|
||||
assert!(t.deactivate(&c).is_empty());
|
||||
assert!(!t.busy());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user