mirror of
https://github.com/storytold/figcraft.git
synced 2026-10-31 22:06:49 -06:00
M12: stamps stick to the objects they land on, which are highlighted first (#1671)
* M12: stamps stick to the objects they land on A stamp sticks to the topmost object under its centre (BoardItem::Stamp gains an optional on, left out when empty so older files open with stamps on nothing) and the board's normalize pass carries it when that object moves or turns, once when both move together. A stamp dragged onto another object sticks to that one, off everything to nothing; one whose object is deleted stays where it is. Gap 24. * M12: the object a stamp will stick to is highlighted While the stamp tool is over an object, or a stamp is dragged over one, the object it will stick to is outlined with the canvas's hover outline (Overlay::stamp_target, found by the same stamp_target as board.stamp and settle_stamps). A drag no longer drops what it moves into the auto layout of a sticky, shape with text or table: board objects take no drops; a stamp already inside its sticky moves with it once. Builds on #1671. Gap 24.
This commit is contained in:
@@ -194,7 +194,7 @@ fn stamps_and_ink_and_the_eraser() {
|
||||
s.prefs.user_name = "Grace".into();
|
||||
let st = id(&s.execute("board.stamp", &json!({"x": 100, "y": 100, "stamp": "heart"})).unwrap(), "id");
|
||||
let n = node(&s, st);
|
||||
assert_eq!(n.board, Some(BoardItem::Stamp { stamp: Stamp::Heart, author: "Grace".into() }));
|
||||
assert_eq!(n.board, Some(BoardItem::Stamp { stamp: Stamp::Heart, author: "Grace".into(), on: None }), "on the empty board: on nothing");
|
||||
assert_eq!(s.doc().unwrap().world_bounds(st).unwrap().center(), Point::new(100.0, 100.0));
|
||||
assert!(!n.children.is_empty());
|
||||
// Marker and highlighter strokes through the tools; the eraser takes what it touches.
|
||||
@@ -219,6 +219,58 @@ fn stamps_and_ink_and_the_eraser() {
|
||||
assert_eq!(s.tool.tool, ToolId::Eraser);
|
||||
}
|
||||
|
||||
/// A stamp sticks to the object it lands on and moves with it (as the incumbent's stamps do); one
|
||||
/// dragged onto another object sticks to that one, one dragged off sticks to nothing, and one
|
||||
/// whose object is deleted stays where it is.
|
||||
#[test]
|
||||
fn stamps_stick_to_what_they_land_on() {
|
||||
let mut s = board();
|
||||
let a = id(&s.execute("board.sticky", &json!({"x": 0, "y": 0})).unwrap(), "id");
|
||||
let b = id(&s.execute("board.sticky", &json!({"x": 400, "y": 0})).unwrap(), "id");
|
||||
let on = |s: &Session, st: NodeId| match node(s, st).board {
|
||||
Some(BoardItem::Stamp { on, .. }) => on,
|
||||
other => panic!("not a stamp: {other:?}"),
|
||||
};
|
||||
let centre = |s: &Session, st: NodeId| s.doc().unwrap().world_bounds(st).unwrap().center();
|
||||
let mv = |s: &mut Session, ids: &[NodeId], dx: f64, dy: f64| {
|
||||
s.execute("transform.move", &json!({"ids": ids, "dx": dx, "dy": dy})).unwrap();
|
||||
};
|
||||
let st = id(&s.execute("board.stamp", &json!({"x": 50, "y": 50, "stamp": "heart"})).unwrap(), "id");
|
||||
assert_eq!(on(&s, st), Some(a));
|
||||
// The sticky moves, the stamp with it; both moved together, once; undo takes both back.
|
||||
mv(&mut s, &[a], 30.0, 200.0);
|
||||
assert_eq!(centre(&s, st), Point::new(80.0, 250.0));
|
||||
mv(&mut s, &[a, st], 10.0, 0.0);
|
||||
assert_eq!((centre(&s, st), on(&s, st)), (Point::new(90.0, 250.0), Some(a)));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(centre(&s, st), Point::new(80.0, 250.0));
|
||||
// Dragged onto the other sticky, it sticks to that one.
|
||||
mv(&mut s, &[st], 370.0, -200.0);
|
||||
assert_eq!(on(&s, st), Some(b));
|
||||
mv(&mut s, &[b], 0.0, 100.0);
|
||||
assert_eq!(centre(&s, st), Point::new(450.0, 150.0));
|
||||
mv(&mut s, &[a], 0.0, 300.0);
|
||||
assert_eq!(centre(&s, st), Point::new(450.0, 150.0), "the first sticky doesn't take it along any more");
|
||||
// Its sticky deleted, it stays where it is, on nothing; dragged off everything, on nothing.
|
||||
s.execute("edit.delete", &json!({"ids": [b]})).unwrap();
|
||||
assert_eq!((on(&s, st), centre(&s, st)), (None, Point::new(450.0, 150.0)));
|
||||
let other = id(&s.execute("board.stamp", &json!({"x": 130, "y": 600, "stamp": "star"})).unwrap(), "id");
|
||||
assert_eq!(on(&s, other), Some(a));
|
||||
mv(&mut s, &[other], 0.0, 1000.0);
|
||||
assert_eq!(on(&s, other), None);
|
||||
// One that sits inside its sticky (dropped into it before stickies stopped taking layers in)
|
||||
// moves with it once, as its child.
|
||||
let inside = id(&s.execute("board.stamp", &json!({"x": 100, "y": 600, "stamp": "check"})).unwrap(), "id");
|
||||
assert_eq!(on(&s, inside), Some(a));
|
||||
s.doc_mut().unwrap().reparent(inside, a, None).unwrap();
|
||||
let was = centre(&s, inside);
|
||||
mv(&mut s, &[a], 40.0, 0.0);
|
||||
assert_eq!(centre(&s, inside), Point::new(was.x + 40.0, was.y), "moved once");
|
||||
// A file from before stamps stuck opens with its stamps on nothing.
|
||||
let old: BoardItem = serde_json::from_value(json!({"type": "stamp", "stamp": "heart", "author": "Grace"})).unwrap();
|
||||
assert_eq!(old, BoardItem::Stamp { stamp: Stamp::Heart, author: "Grace".into(), on: None });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sticky_and_connector_tools() {
|
||||
let mut s = board();
|
||||
|
||||
@@ -31,7 +31,7 @@ pub(crate) use connectors::end_handle_at;
|
||||
pub use connectors::{END_HANDLE_PX, selected_connector_ends};
|
||||
use connectors::{connector_cmd, reattach};
|
||||
pub use derived::route;
|
||||
pub(crate) use derived::{settle_routes, settle_text};
|
||||
pub(crate) use derived::{settle_routes, settle_stamps, settle_text, stamp_target};
|
||||
pub use edit::label_of;
|
||||
pub(crate) use edit::{copied_items, copy_map, rejoin};
|
||||
use edit::{label_cmd, set_color, set_style};
|
||||
|
||||
@@ -81,6 +81,77 @@ pub(crate) fn settle_routes(doc: &mut Document, before: &Document, touched: &BTr
|
||||
}
|
||||
}
|
||||
|
||||
/// After layout: a stamp moves with the object it sits on; one moved by itself (a drag, a nudge)
|
||||
/// sits on whatever is under its centre now; one whose object is gone, or on another page, sits on
|
||||
/// nothing and stays where it is.
|
||||
pub(crate) fn settle_stamps(doc: &mut Document, before: &Document, touched: &BTreeSet<NodeId>) {
|
||||
if !is_board(doc) || touched.is_empty() {
|
||||
return;
|
||||
}
|
||||
let hit = |doc: &Document, id: NodeId| touched.contains(&id) || doc.ancestors(id).iter().any(|a| touched.contains(a));
|
||||
let stamps: Vec<(NodeId, Option<NodeId>)> = doc
|
||||
.nodes()
|
||||
.filter_map(|n| match &n.board {
|
||||
Some(BoardItem::Stamp { on, .. }) => Some((n.id, *on)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
for (id, on) in stamps {
|
||||
let moved_itself = touched.contains(&id) && before.node(id).zip(doc.node(id)).is_some_and(|(b, n)| b.transform != n.transform);
|
||||
let gone = on.is_some_and(|t| !doc.contains(t) || doc.page_of(t) != doc.page_of(id));
|
||||
let now = if moved_itself {
|
||||
match (doc.page_of(id), doc.world_bounds(id)) {
|
||||
(Some(page), Some(b)) => stamp_target(doc, page, b.center(), Some(id)),
|
||||
_ => None,
|
||||
}
|
||||
} else if gone {
|
||||
None
|
||||
} else {
|
||||
if let Some(t) = on.filter(|t| hit(doc, *t)) {
|
||||
carry(doc, before, id, t);
|
||||
}
|
||||
on
|
||||
};
|
||||
if now != on
|
||||
&& let Some(BoardItem::Stamp { on: slot, .. }) = doc.node_mut(id).and_then(|n| n.board.as_mut())
|
||||
{
|
||||
*slot = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The object a stamp at `p` on `page` sits on: the topmost layer of the page whose box holds `p`,
|
||||
/// other than stamps, connectors and their labels, ink and `except`.
|
||||
pub(crate) fn stamp_target(doc: &Document, page: NodeId, p: Point, except: Option<NodeId>) -> Option<NodeId> {
|
||||
let top = doc.node(page)?.children.clone();
|
||||
top.into_iter().rev().filter(|c| Some(*c) != except).find(|c| {
|
||||
let takes = doc.node(*c).is_some_and(|n| {
|
||||
n.visible && !matches!(n.board, Some(BoardItem::Stamp { .. } | BoardItem::Connector(_) | BoardItem::Label { .. } | BoardItem::Ink))
|
||||
});
|
||||
takes && doc.world_bounds(*c).is_some_and(|b| b.contains(p))
|
||||
})
|
||||
}
|
||||
|
||||
/// Stamp `id` follows object `on` from where it was in `before`: its centre goes where the object's
|
||||
/// move takes it (the stamp itself neither turns nor scales).
|
||||
fn carry(doc: &mut Document, before: &Document, id: NodeId, on: NodeId) {
|
||||
// One inside its object (dropped into it before objects stopped taking layers in) already
|
||||
// moves with it.
|
||||
if before.node(on).is_none() || doc.is_ancestor(on, id) {
|
||||
return;
|
||||
}
|
||||
let (was, is) = (before.world_transform(on), doc.world_transform(on));
|
||||
let Some(c) = doc.world_bounds(id).map(|b| b.center()) else { return };
|
||||
let d = is * (was.inverse() * c) - c;
|
||||
if !(d.x.is_finite() && d.y.is_finite()) || d.hypot() <= 1e-9 {
|
||||
return;
|
||||
}
|
||||
let parent = doc.node(id).and_then(|n| n.parent).map_or(Affine::IDENTITY, |p| doc.world_transform(p));
|
||||
if let Some(n) = doc.node_mut(id) {
|
||||
n.transform = parent.inverse() * Affine::translate(d) * parent * n.transform;
|
||||
}
|
||||
}
|
||||
|
||||
/// The ends of connector `id` (as `c`) whose layer is gone or on another page stay where they are
|
||||
/// drawn, as points. Whether any did.
|
||||
fn detach_gone_ends(doc: &Document, id: NodeId, c: &mut Connector) -> bool {
|
||||
|
||||
@@ -315,12 +315,14 @@ pub(super) fn stamp_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let page = s.page()?;
|
||||
let half = STAMP_SIZE / 2.0;
|
||||
let doc = s.doc_mut()?;
|
||||
// It sticks to the object it lands on (it moves with it).
|
||||
let on = super::derived::stamp_target(doc, page, figcraft_geom::Point::new(x, y), None);
|
||||
let id = add(doc, page, NodeKind::Frame, stamp.label(), (x - half, y - half), (STAMP_SIZE, STAMP_SIZE), |n| {
|
||||
n.fills = vec![Paint::solid(stamp.color())];
|
||||
n.corners.radii = [half; 4];
|
||||
n.effects = vec![soft_shadow()];
|
||||
n.clip = false;
|
||||
n.board = Some(BoardItem::Stamp { stamp, author });
|
||||
n.board = Some(BoardItem::Stamp { stamp, author, on });
|
||||
})?;
|
||||
for part in stamp.mark() {
|
||||
let path = figcraft_geom::BezPath::from_svg(part.d).map_err(|e| EngineError::Other(format!("stamp mark: {e}")))?;
|
||||
|
||||
@@ -122,6 +122,14 @@ impl Session {
|
||||
self.tool.overlay.drop_target = end.and_then(|e| e.node());
|
||||
}
|
||||
|
||||
/// A stamp among the layers `moving` (being dragged): the object it would stick to where it is
|
||||
/// now, under its centre (as [`super::settle_stamps`] decides when it is let go).
|
||||
pub(crate) fn dragged_stamp_target(&self, moving: &[NodeId], page: NodeId) -> Option<NodeId> {
|
||||
let doc = self.doc().ok().filter(|d| is_board(d))?;
|
||||
let stamp = moving.iter().copied().find(|id| matches!(doc.node(*id).and_then(|n| n.board.as_ref()), Some(BoardItem::Stamp { .. })))?;
|
||||
super::stamp_target(doc, page, doc.world_bounds(stamp)?.center(), Some(stamp))
|
||||
}
|
||||
|
||||
/// The pointer moving with the connector tool and no button down: the attach points under it.
|
||||
pub(crate) fn connector_hover(&mut self, ev: &PointerEvent) {
|
||||
let end = self.doc().is_ok_and(is_board).then(|| self.end_at(ev));
|
||||
|
||||
@@ -154,7 +154,9 @@ pub fn drop_parent(doc: &Document, page: NodeId, p: Point, exclude: &[NodeId]) -
|
||||
return None;
|
||||
}
|
||||
let n = doc.node(id)?;
|
||||
if !n.visible || n.locked {
|
||||
// Board objects (stickies, shapes with text, tables, stamps) never take other layers in: a
|
||||
// drop on one goes to what is under it.
|
||||
if !n.visible || n.locked || n.is_board_atom() {
|
||||
return None;
|
||||
}
|
||||
let container = n.kind.is_frame_like() || matches!(n.kind, NodeKind::Section { .. });
|
||||
|
||||
@@ -336,6 +336,9 @@ pub struct Overlay {
|
||||
pub rubber_handle: Option<[f64; 2]>,
|
||||
/// Frame/section the pointer is drawing or dropping into (highlighted).
|
||||
pub drop_target: Option<NodeId>,
|
||||
/// Boards: the object a stamp would stick to, under the stamp tool's pointer or a stamp being
|
||||
/// dragged (highlighted).
|
||||
pub stamp_target: Option<NodeId>,
|
||||
/// Connector tool: the attach points (side middles, page x, y) of the object under the pointer,
|
||||
/// and whether the connector end is at each.
|
||||
pub attach: Vec<([f64; 2], bool)>,
|
||||
|
||||
@@ -323,6 +323,7 @@ impl Session {
|
||||
});
|
||||
self.tool.overlay.drop_cell = drop_cell;
|
||||
self.tool.overlay.drop_target = drop_target;
|
||||
self.tool.overlay.stamp_target = self.dragged_stamp_target(&placed, page);
|
||||
self.tool.overlay.cursor = if self.refused(&placed, true) {
|
||||
Cursor::NotAllowed
|
||||
} else if dup {
|
||||
|
||||
@@ -113,6 +113,7 @@ impl Session {
|
||||
self.tool.overlay.measure_box = None;
|
||||
self.tool.overlay.insert_line = None;
|
||||
self.tool.overlay.drop_cell = None;
|
||||
self.tool.overlay.stamp_target = None;
|
||||
let drop_target = self.tool.overlay.drop_target.take();
|
||||
match g {
|
||||
Gesture::Move { ids, moved, duplicate, start, deselect, keep, .. } => {
|
||||
|
||||
@@ -11,6 +11,7 @@ impl Session {
|
||||
if !self.tool.busy() {
|
||||
self.tool.overlay.guides.clear();
|
||||
self.tool.overlay.measure_box = None;
|
||||
self.tool.overlay.stamp_target = None;
|
||||
}
|
||||
self.vector_pointer_left();
|
||||
self.follow_widget_hover(Vec::new());
|
||||
@@ -280,6 +281,9 @@ impl Session {
|
||||
if tool == ToolId::Connector {
|
||||
self.connector_hover(ev);
|
||||
}
|
||||
// The stamp tool: the object a click would stick the stamp to (as `board.stamp` finds it).
|
||||
self.tool.overlay.stamp_target =
|
||||
(tool == ToolId::Stamp && crate::board::is_board(&doc)).then(|| crate::board::stamp_target(&doc, page, ev.p(), None)).flatten();
|
||||
self.follow_widget_hover(widget_hover);
|
||||
let ds = self.ds_mut()?;
|
||||
if ds.hover != hover {
|
||||
|
||||
@@ -161,6 +161,7 @@ pub fn normalize(doc: &mut Document, before: &Document) {
|
||||
touched.extend(doc.take_dirty());
|
||||
// 4b. Board connectors follow the layers they join; their labels sit at their middles.
|
||||
board::settle_routes(doc, before, &touched);
|
||||
board::settle_stamps(doc, before, &touched);
|
||||
touched.extend(doc.take_dirty());
|
||||
// 5. Ready-for-dev layers whose contents changed show "Changed".
|
||||
mark_dev_changes(doc, before, &touched);
|
||||
|
||||
Reference in New Issue
Block a user