mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Keep layer edge snapping inside fractional bounds
This commit is contained in:
1 parent
87cb40df30
commit
d64ec0c3b3
2 files changed
+24
-20
No files matched your search
@@ -511,8 +511,8 @@ fn pasteboards(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, idx: usiz
|
||||
Pasteboard::new(label, &pages).border(border).uncovered(&uncovered).show_at(ui, rect);
|
||||
}
|
||||
|
||||
/// Paint one device pixel inside each layer rectangle. A filled mesh keeps straight,
|
||||
/// pixel-aligned edges solid blue without stroking across the layer's boundary.
|
||||
/// Paint one device pixel inside each layer rectangle. A filled mesh keeps the line solid
|
||||
/// blue while preserving the layer's boundary at fractional device coordinates.
|
||||
fn layer_edge_shapes(view: &CanvasView, ppp: f32, edges: &craft::behavior::kurbo::BezPath) -> Vec<egui::Shape> {
|
||||
let pixel = ppp.recip();
|
||||
path_runs(view, edges)
|
||||
@@ -520,16 +520,18 @@ fn layer_edge_shapes(view: &CanvasView, ppp: f32, edges: &craft::behavior::kurbo
|
||||
.filter_map(|(points, _)| {
|
||||
let mut outer: [Pos2; 4] = points.try_into().ok()?;
|
||||
if view.rotation == 0.0 {
|
||||
let snapped = outer.map(|corner| egui::pos2((corner.x * ppp).round() * pixel, (corner.y * ppp).round() * pixel));
|
||||
// At low zoom a nonempty layer can span less than one device pixel.
|
||||
let snap_x = snapped[0].x != snapped[1].x;
|
||||
let snap_y = snapped[0].y != snapped[3].y;
|
||||
for (corner, snapped) in outer.iter_mut().zip(snapped) {
|
||||
if snap_x {
|
||||
corner.x = snapped.x;
|
||||
let bounds = Rect::from_two_pos(outer[0], outer[2]);
|
||||
let snap = |point: f32| (point * ppp).round() * pixel;
|
||||
let min = egui::pos2(bounds.min.x.max(snap(bounds.min.x)), bounds.min.y.max(snap(bounds.min.y)));
|
||||
let max = egui::pos2(bounds.max.x.min(snap(bounds.max.x)), bounds.max.y.min(snap(bounds.max.y)));
|
||||
// Keep fractional edges where the nearest grid line lies outside the layer.
|
||||
// A thin axis keeps its original bounds if the remaining interval is empty.
|
||||
for corner in &mut outer {
|
||||
if min.x < max.x {
|
||||
corner.x = corner.x.clamp(min.x, max.x);
|
||||
}
|
||||
if snap_y {
|
||||
corner.y = snapped.y;
|
||||
if min.y < max.y {
|
||||
corner.y = corner.y.clamp(min.y, max.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,11 +79,7 @@ fn canvas_draws_one_blue_device_pixel_inside_each_targeted_layer() {
|
||||
for mesh in outlines {
|
||||
let corner = mesh.vertices[0].pos;
|
||||
let inside = mesh.vertices[4].pos;
|
||||
assert_eq!((inside - corner) * ppp, egui::vec2(1.0, 1.0));
|
||||
for v in &mesh.vertices {
|
||||
assert_eq!((v.pos.x * ppp).fract(), 0.0);
|
||||
assert_eq!((v.pos.y * ppp).fract(), 0.0);
|
||||
}
|
||||
assert!(((inside - corner) * ppp - egui::Vec2::splat(1.0)).length() < 0.001);
|
||||
assert!(covers(mesh, corner + egui::Vec2::splat(0.5 / ppp)), "the outermost pixel is blue");
|
||||
assert!(!covers(mesh, corner + egui::Vec2::splat(1.5 / ppp)), "the next pixel stays untouched");
|
||||
assert!(!covers(mesh, corner - egui::Vec2::splat(0.5 / ppp)), "the line stays inside");
|
||||
@@ -94,13 +90,19 @@ fn canvas_draws_one_blue_device_pixel_inside_each_targeted_layer() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapping_keeps_thin_layer_bounds_at_half_zoom() {
|
||||
fn snapping_keeps_thin_and_fractional_layer_bounds_at_half_zoom() {
|
||||
use craft::behavior::kurbo::BezPath;
|
||||
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, egui::Vec2::splat(400.0));
|
||||
let view = CanvasView::new(screen, Point::new(200.0, 200.0), 0.5);
|
||||
let mut edges = BezPath::new();
|
||||
for [x0, y0, x1, y1] in [[1.0, 11.0, 2.0, 52.0], [11.0, 1.0, 52.0, 2.0]] {
|
||||
let cases = [
|
||||
([1.0, 11.0, 2.0, 52.0], [100.5, 106.0, 101.0, 126.0]),
|
||||
([11.0, 1.0, 52.0, 2.0], [106.0, 100.5, 126.0, 101.0]),
|
||||
([1.0, 10.0, 2.0, 51.0], [100.5, 105.0, 101.0, 125.5]),
|
||||
([10.0, 1.0, 51.0, 2.0], [105.0, 100.5, 125.5, 101.0]),
|
||||
];
|
||||
for ([x0, y0, x1, y1], _) in cases {
|
||||
edges.move_to((x0, y0));
|
||||
edges.line_to((x1, y0));
|
||||
edges.line_to((x1, y1));
|
||||
@@ -108,8 +110,8 @@ fn snapping_keeps_thin_layer_bounds_at_half_zoom() {
|
||||
edges.close_path();
|
||||
}
|
||||
let shapes = layer_edge_shapes(&view, 1.0, &edges);
|
||||
assert_eq!(shapes.len(), 2, "neither one-document-pixel target disappears");
|
||||
for (shape, [x0, y0, x1, y1]) in shapes.iter().zip([[100.5, 106.0, 101.0, 126.0], [106.0, 100.5, 126.0, 101.0]]) {
|
||||
assert_eq!(shapes.len(), cases.len(), "no one-document-pixel target disappears");
|
||||
for (shape, (_, [x0, y0, x1, y1])) in shapes.iter().zip(cases) {
|
||||
let egui::Shape::Mesh(mesh) = shape else { panic!("expected a layer outline mesh") };
|
||||
let [a, b, c, d] = [egui::pos2(x0, y0), egui::pos2(x1, y0), egui::pos2(x1, y1), egui::pos2(x0, y1)];
|
||||
assert_eq!(mesh.vertices[..4].iter().map(|v| v.pos).collect::<Vec<_>>(), [a, b, c, d]);
|
||||
|
||||
Reference in new issue
Block a user