mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
This commit is contained in:
commit
e7964eb78e
2 files changed
+202
-2
No files matched your search
@@ -169,3 +169,107 @@ fn new_pens_with_command_edit_paths_as_direct_selection() {
|
||||
assert_eq!(app.ui.tool, tool);
|
||||
}
|
||||
}
|
||||
|
||||
/// #6593: a Pen press on an endpoint of an open path carries on drawing it, as in Photoshop,
|
||||
/// from its last anchor or (reversed) its first; the commit extends that subpath in one step
|
||||
/// instead of adding a second one. Elsewhere the press starts a new subpath as before.
|
||||
#[test]
|
||||
fn the_pen_carries_on_an_open_path_from_either_end() {
|
||||
let mut app = app(Tool::Pen);
|
||||
let anchors = |app: &PhotocraftApp| -> Vec<Vec<[f64; 2]>> {
|
||||
work_path(app).subpaths.iter().map(|s| s.knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect()).collect()
|
||||
};
|
||||
let steps = |app: &PhotocraftApp| app.session.active().unwrap().history.past_len();
|
||||
click(&mut app, 20.0, 20.0);
|
||||
click(&mut app, 80.0, 20.0);
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
assert_eq!(anchors(&app), [vec![[20.0, 20.0], [80.0, 20.0]]]);
|
||||
// From the last anchor.
|
||||
let before = steps(&app);
|
||||
click(&mut app, 80.0, 20.0);
|
||||
click(&mut app, 80.0, 80.0);
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
assert_eq!(anchors(&app), [vec![[20.0, 20.0], [80.0, 20.0], [80.0, 80.0]]], "the same subpath, extended");
|
||||
assert_eq!(steps(&app), before + 1, "one History step");
|
||||
// From the first anchor: the subpath runs the other way and carries on past it.
|
||||
click(&mut app, 20.0, 20.0);
|
||||
click(&mut app, 20.0, 80.0);
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
assert_eq!(anchors(&app), [vec![[80.0, 80.0], [80.0, 20.0], [20.0, 20.0], [20.0, 80.0]]]);
|
||||
// Anywhere else: a new subpath next to it.
|
||||
click(&mut app, 150.0, 150.0);
|
||||
click(&mut app, 200.0, 150.0);
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
assert_eq!(anchors(&app).len(), 2, "a second subpath");
|
||||
// Closing on the far end of a resumed path closes that subpath.
|
||||
click(&mut app, 200.0, 150.0);
|
||||
click(&mut app, 175.0, 190.0);
|
||||
click(&mut app, 150.0, 150.0);
|
||||
assert!(app.ui.pen.is_none(), "closed and committed");
|
||||
let work = work_path(&app);
|
||||
assert_eq!((work.subpaths.len(), work.subpaths[1].closed, work.subpaths[1].knots.len()), (2, true, 3));
|
||||
}
|
||||
|
||||
/// #6593 review: carrying on a subpath keeps its path operation, and a draft whose subpath changed
|
||||
/// meanwhile is added as a new subpath instead of overwriting another one.
|
||||
#[test]
|
||||
fn a_resumed_subpath_keeps_its_operation_and_a_stale_draft_adds_a_new_one() {
|
||||
let mut app = app(Tool::Pen);
|
||||
let open = |ax: f64, bx: f64, op: &str| json!({"closed": false, "op": op, "knots": [{"anchor": [ax, 20.0]}, {"anchor": [bx, 20.0]}]});
|
||||
app.run("path.set", json!({"name": "work", "path": {"subpaths": [open(20.0, 80.0, "subtract")]}})).unwrap();
|
||||
click(&mut app, 80.0, 20.0);
|
||||
click(&mut app, 80.0, 80.0);
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
let work = work_path(&app);
|
||||
assert_eq!((work.subpaths.len(), work.subpaths[0].knots.len()), (1, 3));
|
||||
assert_eq!(work.subpaths[0].op, photocraft_doc::vector::PathOp::Subtract, "the operation is kept");
|
||||
// The work path is replaced while the draft is open: the draft becomes a new subpath.
|
||||
click(&mut app, 20.0, 20.0);
|
||||
click(&mut app, 20.0, 80.0);
|
||||
app.run("path.set", json!({"name": "work", "path": {"subpaths": [open(120.0, 180.0, "combine")]}})).unwrap();
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
let work = work_path(&app);
|
||||
assert_eq!(work.subpaths.len(), 2, "a new subpath, the replaced one untouched");
|
||||
assert_eq!(work.subpaths[0].knots.iter().map(|k| k.anchor.x).collect::<Vec<_>>(), [120.0, 180.0]);
|
||||
// The same with nothing selected in the Paths panel: still added to the Work Path it came from.
|
||||
click(&mut app, 180.0, 20.0);
|
||||
click(&mut app, 180.0, 90.0);
|
||||
app.ui.selected_path = None;
|
||||
app.run("path.set", json!({"name": "work", "path": {"subpaths": [open(120.0, 160.0, "combine")]}})).unwrap();
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
assert_eq!(work_path(&app).subpaths.len(), 2, "added, not replacing the Work Path");
|
||||
// And with another path selected: the draft still goes back to the path it came from.
|
||||
app.run("path.set", json!({"name": "Other", "path": {"subpaths": [open(10.0, 30.0, "combine")]}})).unwrap();
|
||||
click(&mut app, 160.0, 20.0);
|
||||
click(&mut app, 160.0, 90.0);
|
||||
app.ui.selected_path = Some("Other".into());
|
||||
app.run("path.set", json!({"name": "work", "path": {"subpaths": [open(120.0, 150.0, "combine")]}})).unwrap();
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
assert_eq!(work_path(&app).subpaths.len(), 2, "joined the Work Path it came from");
|
||||
let st = app.session.active().unwrap();
|
||||
let other = st.doc.paths.iter().find(|p| p.name == "Other").unwrap();
|
||||
assert_eq!(other.path.subpaths.len(), 1, "the selected path is untouched");
|
||||
}
|
||||
|
||||
/// #6593 review: undoing points in a resumed draft never removes the path's own anchors; past the
|
||||
/// added ones it drops the draft and the path stays as it was.
|
||||
#[test]
|
||||
fn undo_in_a_resumed_draft_stops_at_the_paths_own_anchors() {
|
||||
let mut app = app(Tool::Pen);
|
||||
click(&mut app, 20.0, 20.0);
|
||||
click(&mut app, 80.0, 20.0);
|
||||
crate::vector_ui::pen_commit(&mut app, false);
|
||||
let before = work_path(&app);
|
||||
click(&mut app, 80.0, 20.0);
|
||||
click(&mut app, 80.0, 80.0);
|
||||
assert!(crate::vector_ui::pen_undo_last_point(&mut app), "takes back the added anchor");
|
||||
assert_eq!(app.ui.pen.as_ref().map(|p| p.knots.len()), Some(2));
|
||||
// A ⌘/Ctrl selection on the draft's anchors.
|
||||
app.ui.direct_selection.target = Some(crate::direct_select::PathRef::Pen);
|
||||
app.ui.direct_selection.anchors = vec![[0, 1]];
|
||||
assert!(crate::vector_ui::pen_undo_last_point(&mut app), "drops the draft");
|
||||
assert!(app.ui.pen.is_none());
|
||||
assert_eq!(app.ui.direct_selection.target, None, "the selection on the dropped draft goes with it");
|
||||
assert!(app.ui.direct_selection.anchors.is_empty());
|
||||
assert_eq!(work_path(&app), before, "the path is as it was");
|
||||
}
|
||||
@@ -33,6 +33,21 @@ pub struct PenPath {
|
||||
/// Curvature Pen: the anchor being dragged.
|
||||
#[serde(skip)]
|
||||
pub moving: Option<usize>,
|
||||
/// Drawing carries on from an endpoint of this open subpath of a document path (#6593): the
|
||||
/// commit replaces that subpath instead of adding one.
|
||||
#[serde(default)]
|
||||
pub resumes: Option<Resumed>,
|
||||
}
|
||||
|
||||
/// The subpath a Pen path carries on: where it is and what it was when drawing resumed. The
|
||||
/// commit replaces it only while it is still that subpath of that document; otherwise (another
|
||||
/// document, or the path changed meanwhile) the drawing is added as a new subpath.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Resumed {
|
||||
pub doc: u64,
|
||||
pub path: String,
|
||||
pub index: usize,
|
||||
pub was: photocraft_doc::vector::Subpath,
|
||||
}
|
||||
|
||||
impl PenPath {
|
||||
@@ -223,6 +238,13 @@ pub fn closes_path_at(app: &PhotocraftApp, tool: Tool, x: f64, y: f64) -> bool {
|
||||
/// lands on the nearest 45° ray from the previous one, at the pointer's projection onto it.
|
||||
pub fn pen_down(app: &mut PhotocraftApp, x: f64, y: f64, shift: bool) {
|
||||
let tol = 6.0 / app.point_zoom().max(0.01) as f64;
|
||||
// A press on an endpoint of an open path carries on drawing it, as in Photoshop (#6593): the
|
||||
// press then acts on its last anchor, so a drag pulls out the next segment's handle.
|
||||
if app.ui.pen.as_ref().is_none_or(|p| p.knots.is_empty())
|
||||
&& let Some(resumed) = resume_at(app, x, y, tol)
|
||||
{
|
||||
app.ui.pen = Some(resumed);
|
||||
}
|
||||
let pen = app.ui.pen.get_or_insert_with(PenPath::default);
|
||||
if let Some(first) = pen.knots.first().map(|k| k[0])
|
||||
&& on_closing_anchor(pen, x, y, tol)
|
||||
@@ -286,6 +308,16 @@ fn shift_45(origin: [f64; 2], p: [f64; 2], shift: bool) -> [f64; 2] {
|
||||
/// knot rather than undoing an unrelated, already committed document operation.
|
||||
pub fn pen_undo_last_point(app: &mut PhotocraftApp) -> bool {
|
||||
let Some(pen) = app.ui.pen.as_mut() else { return false };
|
||||
// A resumed path's own anchors are not the draft's to take back: undoing past the ones added
|
||||
// since it resumed drops the draft and leaves the path as it was (#6593).
|
||||
if pen.resumes.as_ref().is_some_and(|r| pen.knots.len() <= r.was.knots.len()) {
|
||||
// A ⌘/Ctrl selection on the draft goes with it.
|
||||
if app.ui.direct_selection.target == Some(crate::direct_select::PathRef::Pen) {
|
||||
app.ui.direct_selection = Default::default();
|
||||
}
|
||||
app.ui.pen = None;
|
||||
return true;
|
||||
}
|
||||
if pen.knots.pop().is_none() {
|
||||
return false;
|
||||
}
|
||||
@@ -454,6 +486,11 @@ fn commit_path(app: &mut PhotocraftApp, pen: &PenPath, closed: bool) {
|
||||
stroke
|
||||
};
|
||||
app.run("shape.create", json!({"kind": "path", "path": path, "fill": fill, "stroke": stroke}))
|
||||
} else if let Some((name, p)) =
|
||||
pen.resumes.as_ref().and_then(|r| app.session.active().and_then(|st| with_resumed_subpath(&st.doc, r, &path)).map(|p| (r.path.clone(), p)))
|
||||
{
|
||||
// The resumed subpath, carried on, takes its old place: one step, no second subpath.
|
||||
app.run("path.set", json!({"name": name, "path": p})).inspect(|_| app.ui.selected_path = Some(name))
|
||||
} else if let Some((id, existing)) = targeted_vector_mask(app) {
|
||||
// A targeted vector mask takes the new subpath (#196), as in Photoshop.
|
||||
let mut p = photocraft_engine::vector_cmds::path_json(&existing);
|
||||
@@ -465,7 +502,11 @@ fn commit_path(app: &mut PhotocraftApp, pen: &PenPath, closed: bool) {
|
||||
// Photoshop adds the new subpath to the path selected in the Paths panel, which is the
|
||||
// Work Path right after drawing one, so path after path builds up there (#4721). With no
|
||||
// path selected, a new Work Path replaces the old one. The drawn path ends up selected.
|
||||
let target = app.ui.selected_path.clone().filter(|n| app.session.active().is_some_and(|st| path_exists(&st.doc, n)));
|
||||
// A resumed draft whose subpath changed meanwhile joins the path it came from, as a new
|
||||
// subpath, even with nothing selected in the Paths panel.
|
||||
let exists = |n: &String| app.session.active().is_some_and(|st| path_exists(&st.doc, n));
|
||||
let resumed_from = pen.resumes.as_ref().filter(|r| app.session.active().is_some_and(|st| st.doc.id.0 == r.doc)).map(|r| r.path.clone());
|
||||
let target = resumed_from.filter(exists).or_else(|| app.ui.selected_path.clone().filter(exists));
|
||||
let name = target.clone().unwrap_or_else(|| "work".into());
|
||||
let mut params = json!({"name": name, "path": path});
|
||||
if target.is_some() {
|
||||
@@ -481,7 +522,62 @@ fn commit_path(app: &mut PhotocraftApp, pen: &PenPath, closed: bool) {
|
||||
|
||||
/// Does `doc` have the path the Paths panel calls `name`: "work", or a saved path's name?
|
||||
fn path_exists(doc: &Document, name: &str) -> bool {
|
||||
if name == "work" { doc.work_path.is_some() } else { doc.paths.iter().any(|p| p.name == name) }
|
||||
doc_path(doc, name).is_some()
|
||||
}
|
||||
|
||||
/// The path the Paths panel calls `name`: "work", or a saved path's name.
|
||||
fn doc_path<'a>(doc: &'a Document, name: &str) -> Option<&'a Path> {
|
||||
if name == "work" { doc.work_path.as_ref() } else { doc.paths.iter().find(|p| p.name == name).map(|p| &p.path) }
|
||||
}
|
||||
|
||||
/// The open subpath of the path a new Pen path would join (the one selected in the Paths panel,
|
||||
/// else the work path) that has its first or last anchor at `(x, y)`, as a Pen path to carry on
|
||||
/// drawing from there (#6593). Its knots run towards that anchor: reversed, handles swapped, when
|
||||
/// it is the first. Shape mode and a targeted vector mask take new paths, so they resume nothing.
|
||||
fn resume_at(app: &PhotocraftApp, x: f64, y: f64, tol: f64) -> Option<PenPath> {
|
||||
if app.ui.tool_options.vector_mode == "shape" || targeted_vector_mask(app).is_some() {
|
||||
return None;
|
||||
}
|
||||
let st = app.session.active()?;
|
||||
let doc = st.doc.id.0;
|
||||
let name = app.ui.selected_path.clone().filter(|n| path_exists(&st.doc, n)).unwrap_or_else(|| "work".into());
|
||||
let near = |k: &photocraft_doc::Knot| (k.anchor.x - x).hypot(k.anchor.y - y) < tol;
|
||||
doc_path(&st.doc, &name)?.subpaths.iter().enumerate().filter(|(_, s)| !s.closed).find_map(|(i, s)| {
|
||||
let reversed = if s.knots.last().is_some_and(near) {
|
||||
false
|
||||
} else if s.knots.first().is_some_and(near) {
|
||||
true
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let mut knots: Vec<_> = s.knots.iter().map(|k| (knot_points(k), k.smooth)).collect();
|
||||
if reversed {
|
||||
knots.reverse();
|
||||
for (k, _) in &mut knots {
|
||||
k.swap(1, 2);
|
||||
}
|
||||
}
|
||||
let unlinked = knots.iter().enumerate().filter(|(_, (_, smooth))| !smooth).map(|(i, _)| i).collect();
|
||||
let resumes = Resumed { doc, path: name.clone(), index: i, was: s.clone() };
|
||||
Some(PenPath { knots: knots.into_iter().map(|(k, _)| k).collect(), unlinked, resumes: Some(resumes), ..Default::default() })
|
||||
})
|
||||
}
|
||||
|
||||
/// The resumed path with its subpath carried on: the one subpath of `drawn` takes the old one's
|
||||
/// anchors and closing, which keeps its other settings (its path operation). `None` when the
|
||||
/// subpath is no longer the one drawing resumed (another document, or the path changed).
|
||||
fn with_resumed_subpath(doc: &Document, r: &Resumed, drawn: &Value) -> Option<Value> {
|
||||
let path = doc_path(doc, &r.path).filter(|_| doc.id.0 == r.doc)?;
|
||||
if path.subpaths.get(r.index) != Some(&r.was) {
|
||||
return None;
|
||||
}
|
||||
let mut p = photocraft_engine::vector_cmds::path_json(path);
|
||||
let slot = p.get_mut("subpaths")?.as_array_mut()?.get_mut(r.index)?.as_object_mut()?;
|
||||
let new = drawn.get("subpaths")?.get(0)?;
|
||||
for key in ["closed", "knots"] {
|
||||
slot.insert(key.into(), new.get(key)?.clone());
|
||||
}
|
||||
Some(p)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user