mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
Shapes: hovering a box side skips the outline test of shapes out of reach (#11473 review)
The side band asks on every hover frame whether another shape is under the pointer, and the pick sampled every shape layer's curves to find out. It now skips that sampling for a shape whose control-point box, grown by the pick tolerance, doesn't hold the point, since none of its outline can be in reach there. What it picks is unchanged, Path Selection's clicks included; a test pins hits just past a shape's box and on a stroke's pixels past its path.
This commit is contained in:
2 files changed
+44
-1
No files matched your search
@@ -454,3 +454,34 @@ fn a_press_on_a_neighbouring_shape_or_past_the_band_stays_the_tools() {
|
||||
near(bounds(&app, b), [420.0, 318.0, 723.0, 451.0]);
|
||||
}
|
||||
}
|
||||
|
||||
/// #11473 review: the pick under the side band skips a shape's outline test where the point is
|
||||
/// out of reach of it, and still finds every shape it found before: an outline within the pick
|
||||
/// tolerance just past the shape's box, and a fill's pixels past the path (an outside stroke).
|
||||
#[test]
|
||||
fn the_pick_still_finds_shapes_just_past_their_path_bounds() {
|
||||
use crate::vector_ui::pick_shape_at;
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 1000, "height": 700, "background": "white"})).unwrap();
|
||||
// A stroke-only ellipse 200,200–400,300: picked by its outline alone.
|
||||
app.run("shape.create", json!({"kind": "ellipse", "rect": [200, 200, 200, 100], "fill": null, "stroke": {"width": 2}})).unwrap();
|
||||
let e = app.session.active().unwrap().active_layer.unwrap();
|
||||
// A filled rectangle 500,400–600,500 with a 20 px stroke outside its path.
|
||||
app.run("shape.create", json!({"kind": "rect", "rect": [500, 400, 100, 100], "fill": "#20a040", "stroke": {"width": 20, "align": "outside"}})).unwrap();
|
||||
let r = app.session.active().unwrap().active_layer.unwrap();
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
let pick = |p: [f64; 2], tol: f64| pick_shape_at(doc, p, tol);
|
||||
// Within the tolerance of the ellipse's extreme anchors, outside its box.
|
||||
assert_eq!(pick([405.5, 250.0], 6.0), Some(e));
|
||||
assert_eq!(pick([300.0, 194.5], 6.0), Some(e));
|
||||
assert_eq!(pick([406.5, 250.0], 6.0), None);
|
||||
assert_eq!(pick([300.0, 193.5], 6.0), None);
|
||||
// Within it of a curve, off the anchors: the ellipse's outline at 45°.
|
||||
let (cx, cy) = (300.0 + 100.0 * std::f64::consts::FRAC_1_SQRT_2, 250.0 + 50.0 * std::f64::consts::FRAC_1_SQRT_2);
|
||||
assert_eq!(pick([cx + 4.0, cy], 6.0), Some(e));
|
||||
// The stroke's pixels up to 20 px past the rectangle's path, far past the tolerance.
|
||||
assert_eq!(pick([612.0, 450.0], 1.0), Some(r));
|
||||
assert_eq!(pick([550.0, 381.5], 1.0), Some(r));
|
||||
assert_eq!(pick([625.0, 450.0], 1.0), None);
|
||||
assert_eq!(pick([550.0, 378.5], 1.0), None);
|
||||
}
|
||||
@@ -253,7 +253,7 @@ pub fn pick_shape_at(doc: &Document, at: [f64; 2], tol: f64) -> Option<photocraf
|
||||
continue;
|
||||
}
|
||||
if let LayerContent::Shape(sh) = &l.content {
|
||||
if photocraft_vector::edit::hit(&sh.path, photocraft_geom::Point::new(at[0], at[1]), tol, &[]).is_some() {
|
||||
if near_outline(&sh.path, at, tol) && photocraft_vector::edit::hit(&sh.path, photocraft_geom::Point::new(at[0], at[1]), tol, &[]).is_some() {
|
||||
return Some(l.id);
|
||||
}
|
||||
if sh.fill.is_some() {
|
||||
@@ -295,6 +295,18 @@ pub fn pick_shape_at(doc: &Document, at: [f64; 2], tol: f64) -> Option<photocraf
|
||||
None
|
||||
}
|
||||
|
||||
/// Whether `at` lies within `tol` of the box round `path`'s anchors and control points, which
|
||||
/// holds every curve of it (each segment stays inside its control points' hull). Outside it
|
||||
/// `edit::hit` can't find an anchor or a segment, so the pick skips its curve sampling: the box's
|
||||
/// side band asks on every hover frame, past every shape layer (#11473). The box is grown a hair
|
||||
/// more than `tol` so rounding in the sampled distances never drops a hit.
|
||||
fn near_outline(path: &Path, at: [f64; 2], tol: f64) -> bool {
|
||||
let Some((x0, y0, x1, y1)) = path.control_bounds() else { return false };
|
||||
let pad = tol * 1.001 + 1e-9 * (1.0 + at[0].abs() + at[1].abs());
|
||||
// Written so a NaN bound keeps the check (`edit::hit` decides), as an infinite one does.
|
||||
!(at[0] < x0 - pad || at[0] > x1 + pad || at[1] < y0 - pad || at[1] > y1 + pad)
|
||||
}
|
||||
|
||||
pub fn path_selection_finish(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {
|
||||
let (dx, dy) = (end[0] - start[0], end[1] - start[1]);
|
||||
let tol = 6.0 / f64::from(app.point_zoom().max(0.01));
|
||||
|
||||
Reference in new issue
Block a user