Snap pivot placement to nine anchors and configured targets
Beffroi/beffroi-tools/pipeline/head There was a failure building this commit Details

This commit is contained in:
Dandaedre 2026-11-14 12:42:11 +01:00
parent 01f2986006
commit c64fbd58f4
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
6 changed files with 267 additions and 4 deletions

View File

@ -1890,6 +1890,28 @@ impl Editor {
pointer.distance(point) <= 9.0
})
}
fn move_pivot_snapped(
&mut self,
i: usize,
point: [f64; 2],
modifiers: egui::Modifiers,
scale: f32,
) {
let mut options = self.snapping;
if modifiers.alt {
options.enabled = !options.enabled;
}
let (target, guides) = self.layout.snap_pivot(
i,
point,
options,
6.0 / scale as f64,
self.effective_grid(modifiers).map(f64::from),
!modifiers.alt,
);
self.move_pivot(i, target);
self.snap_guides = guides;
}
fn move_pivot(&mut self, i: usize, point: [f64; 2]) {
let g = self.layout.layers()[i];
let mut relative = [(point[0] - g.x) / g.width, (point[1] - g.y) / g.height];
@ -2062,7 +2084,7 @@ impl Editor {
2,
None,
"Pivot",
"Pivot resize — click to toggle. Hold Ctrl for the same effect; if selected, Ctrl temporarily disables it. Combines with either grid. Drag the pivot; double-click it to center. Applies to layer handles.",
"Pivot resize — click to toggle. Hold Ctrl for the same effect; if selected, Ctrl temporarily disables it. Combines with either grid. Drag the pivot: snaps to 9 layer anchors and enabled grids/edges; Alt bypasses anchors. Double-click to center. Applies to layer handles.",
),
] {
let rect = Rect::from_min_size(
@ -2563,7 +2585,7 @@ impl Editor {
let p = (p - origin) / scale;
let point = [p.x as f64, p.y as f64];
if let Some((i, _)) = self.pivot_drag {
self.move_pivot(i, point);
self.move_pivot_snapped(i, point, modifiers, scale);
} else if let Some(m) = self.marquee.as_mut() {
m.end = point;
self.layout.select_region(m.start, point, &m.base);
@ -2580,7 +2602,7 @@ impl Editor {
let p = (p - origin) / scale;
let point = [p.x as f64, p.y as f64];
if let Some((i, _)) = self.pivot_drag {
self.move_pivot(i, point);
self.move_pivot_snapped(i, point, modifiers, scale);
} else if let Some(m) = self.marquee.as_mut() {
m.end = point;
self.layout.select_region(m.start, point, &m.base);
@ -3742,6 +3764,80 @@ mod tests {
);
frame(app, ctx, vec![mouse(pos, false)]);
}
#[test]
fn pivot_snap_modifiers_and_linked_markers_preserve_geometry() {
let mut app = Editor {
resize_mode: ResizeMode::Mirror,
pivot_tool: true,
grid_tool: Some(100),
..Default::default()
};
app.layout.set_selection([true; 2]);
app.sync();
let before = app.layout.layers().clone();
app.move_pivot_snapped(0, [122.0, 361.0], egui::Modifiers::NONE, 1.0);
assert_eq!(app.pivot_for(0), [0.0, 0.5]);
assert_eq!(app.pivot_for(1), [1.0, 0.5]);
assert_eq!(app.snap_guides, [Some(120.0), Some(360.0)]);
app.move_pivot_snapped(0, [833.0, 653.0], egui::Modifiers::NONE, 1.0);
assert_eq!(app.snap_guides, [Some(800.0), Some(700.0)]);
app.move_pivot_snapped(
0,
[833.0, 653.0],
egui::Modifiers {
ctrl: true,
shift: true,
..Default::default()
},
1.0,
);
assert_eq!(app.snap_guides, [None; 2]);
app.snapping.enabled = true;
app.move_pivot_snapped(
0,
[122.0, 361.0],
egui::Modifiers {
alt: true,
..Default::default()
},
1.0,
);
assert_eq!(app.snap_guides, [None; 2]);
assert_ne!(app.pivot_for(0), [0.0, 0.5]);
assert_eq!(app.layout.layers(), &before);
}
#[test]
fn pivot_snap_survives_pointer_release_and_escape_restores_placement() {
let ctx = egui::Context::default();
let mut app = Editor {
pivot_tool: true,
..Default::default()
};
let before = app.layout.layers().clone();
let output = frame(&mut app, &ctx, vec![]);
let rect = layer_rects(&output)[1];
let start = rect.center();
let end = rect.left_top() + Vec2::new(2.0, 1.0);
frame(
&mut app,
&ctx,
vec![egui::Event::PointerMoved(start), mouse(start, true)],
);
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end)]);
assert_eq!(app.pivot_relative, [0.0; 2]);
frame(&mut app, &ctx, vec![mouse(end, false)]);
assert_eq!(app.pivot_relative, [0.0; 2]);
let end2 = rect.right_center() - Vec2::new(2.0, 1.0);
frame(&mut app, &ctx, vec![mouse(end, true)]);
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end2)]);
assert_eq!(app.pivot_relative, [1.0, 0.5]);
frame(&mut app, &ctx, vec![key(egui::Key::Escape)]);
frame(&mut app, &ctx, vec![mouse(end2, false)]);
assert_eq!(app.pivot_relative, [0.0; 2]);
assert_eq!(app.layout.layers(), &before);
}
#[test]
fn optional_layer_point_changes_measurements_only_and_center_survives_size_edits() {
let ctx = egui::Context::default();

View File

@ -20,6 +20,73 @@ impl Default for Snapping {
}
}
impl Layout {
/// Snap a pivot without editing geometry. Local anchors take precedence over
/// grid/edge options; the caller may bypass them for precise free placement.
pub fn snap_pivot(
&self,
index: usize,
point: [f64; 2],
options: Snapping,
tolerance: f64,
grid: Option<f64>,
anchors: bool,
) -> ([f64; 2], [Option<f64>; 2]) {
let Some(g) = self.layers.get(index).copied() else {
return (point, [None; 2]);
};
if !tolerance.is_finite() || tolerance < 0.0 || point.iter().any(|v| !v.is_finite()) {
return (point, [None; 2]);
}
if anchors {
let distance = |p: [f64; 2]| (p[0] - point[0]).hypot(p[1] - point[1]);
if let Some(target) = ResizeHandle::positions(g)
.into_iter()
.map(|(_, p)| p)
.chain(std::iter::once(g.center()))
.filter(|p| distance(*p) <= tolerance)
.min_by(|a, b| distance(*a).total_cmp(&distance(*b)))
{
return (target, target.map(Some));
}
}
if let Some(spacing) = grid.filter(|s| s.is_finite() && *s > 0.0) {
let target = point.map(|v| (v / spacing).round() * spacing);
return (target, target.map(Some));
}
if !options.enabled {
return (point, [None; 2]);
}
let mut target = point;
let mut guides = [None; 2];
for axis in 0..2 {
let mut edges = Vec::new();
if options.screen_edges {
edges.extend([0.0, self.formats.output[axis] as f64]);
}
if options.layer_edges {
for (i, other) in self.layers.iter().enumerate() {
if i != index && self.visible(i) {
edges.extend(if axis == 0 {
[other.x, other.x + other.width]
} else {
[other.y, other.y + other.height]
});
}
}
}
if let Some(edge) = edges
.into_iter()
.filter(|e| (*e - point[axis]).abs() <= tolerance)
.min_by(|a, b| (a - point[axis]).abs().total_cmp(&(b - point[axis]).abs()))
{
target[axis] = edge;
guides[axis] = Some(edge);
} else if options.pixels {
target[axis] = point[axis].round();
}
}
(target, guides)
}
/// Recompute from the gesture's original snapshot, never from a previous snapped frame.
/// `tolerance` is in screen pixels; the UI converts its fixed logical hit distance.
/// Layer resize handles preserve anchors/ratios; content crop gestures do not snap.

View File

@ -187,3 +187,88 @@ fn ratio_locked_corner_snaps_without_shifting_pivot() {
g.width / g.height,
);
}
#[test]
fn pivot_placement_snaps_to_all_nine_anchors_even_with_snapping_off_or_grid_on() {
let mut layout = Layout::default();
let g = Geometry {
x: 120.25,
y: 180.75,
width: 641.5,
height: 361.25,
};
layout.apply(0, g).unwrap();
let before = layout.layers().clone();
for scale in [0.25, 1.0, 2.0] {
for target in ResizeHandle::positions(g)
.into_iter()
.map(|(_, p)| p)
.chain(std::iter::once(g.center()))
{
let point = [target[0] + 2.0 / scale, target[1] - 1.0 / scale];
for grid in [None, Some(10.0), Some(100.0)] {
assert_eq!(
layout.snap_pivot(0, point, Snapping::default(), 6.0 / scale, grid, true),
(target, target.map(Some))
);
}
assert_eq!(
layout.snap_pivot(0, point, Snapping::default(), 6.0 / scale, None, false),
(point, [None; 2])
);
}
}
assert_eq!(layout.layers(), &before);
}
#[test]
fn pivot_placement_honors_screen_layer_pixel_and_grid_options() {
let mut layout = Layout::default();
let mut options = Snapping {
enabled: true,
pixels: false,
screen_edges: true,
layer_edges: false,
};
assert_eq!(
layout.snap_pivot(1, [3.0, 600.0], options, 6.0, None, true),
([0.0, 600.0], [Some(0.0), None])
);
options.screen_edges = false;
options.layer_edges = true;
layout.set_locked(0, true);
assert_eq!(
layout.snap_pivot(1, [763.0, 600.0], options, 6.0, None, true),
([760.0, 600.0], [Some(760.0), None])
);
layout.set_visible(0, false);
assert_eq!(
layout.snap_pivot(1, [763.0, 600.0], options, 6.0, None, true),
([763.0, 600.0], [None; 2])
);
options.layer_edges = false;
options.pixels = true;
assert_eq!(
layout.snap_pivot(1, [833.3, 653.7], options, 6.0, None, true),
([833.0, 654.0], [None; 2])
);
for spacing in [10.0, 100.0] {
let target = [833.3_f64, 653.7].map(|p| (p / spacing).round() * spacing);
assert_eq!(
layout.snap_pivot(
1,
[833.3, 653.7],
Snapping::default(),
6.0,
Some(spacing),
true
),
(target, target.map(Some))
);
}
// Being near an edge alone is not one of the nine local point targets.
assert_eq!(
layout.snap_pivot(1, [1162.0, 270.0], Snapping::default(), 6.0, None, true),
([1162.0, 270.0], [None; 2])
);
}

View File

@ -102,7 +102,7 @@ and save immediately; closing the window requires no Apply step. Restore default
preferences resets appearance and editing values, including theme history and snapping, without
changing layer geometry or pending position/size text fields. Snapping uses a
fixed six-logical-pixel proximity radius converted by viewport scale. It affects
layer movement and resize handles; numeric entry, nudges and crop-content gestures
layer movement, resize handles and pivot placement; numeric entry, nudges and crop-content gestures
bypass it. Resize snapping preserves anchors and proportions and shows guides only
when the resulting edge actually reaches its target.
The session-only toolbar grid tools select 10 or 100 screen pixels (mutually exclusive). Shift temporarily selects the 10 px grid; Ctrl+Shift selects 100 px. If that same grid tool is selected, the matching shortcut temporarily disables it instead. Alt overrides both grids and temporarily inverts the saved snapping-enabled value (off becomes on; on becomes off), including on release. The chosen pixel/screen/layer-edge targets still apply. These modifiers do not change the saved options or settings version.
@ -116,3 +116,9 @@ The session-only Pivot tool can combine with either grid. Ctrl without Shift tem
Move/Pan/Zoom navigation tool choice is also session-only and survives project loads.
It starts at Move on launch; V/H/Z switch tools only outside text entry. Camera
zoom/pan remain part of the project, not application settings.
Pivot placement also has nine built-in local anchors (corners, side midpoints,
center), active independently of `snapping.enabled` within the same six-logical-pixel
radius. Anchors take priority over active grids and configured edge/pixel snapping.
Alt bypasses these anchors and grids while inverting configured snapping as usual.
This adds no settings fields; pivot positions remain session-only.

View File

@ -379,6 +379,13 @@ resize or restores the previous pivot placement.
| Mirror horizontally | The partner's pivot is reflected left/right |
| Opposite handles | The partner's pivot is reflected on both axes |
Dragging a pivot snaps within six logical display pixels of the layer's four corners,
four side midpoints and center, even with snapping disabled. Nearby local anchors
win over a grid; elsewhere the active 10/100 px grid or enabled pixel/screen-edge/
visible-layer-edge options apply. Guides show reached anchors/edges/grid lines.
Alt bypasses the nine built-in anchors and grids while retaining its usual
inversion of configured snapping. Release uses the current modifiers.
Moving any marker updates the related markers. Each layer's pivot stays fixed
while resizing; unequal sizes retain proportional scaling. Aspect-ratio lock,
snapping (including edge contact), Alt inversion and Undo still apply. A handle

View File

@ -4,6 +4,8 @@
## Unreleased
- Pivot dragging snaps to corners, side midpoints and center even with snapping off; also honors grid/pixel/screen/layer options. Alt bypasses built-in anchors; guides and linked pivots follow the result.
- Optional “Use a different layer point” exposes independent screen/layer measurement points (e.g. screen top-left → layer center). Numeric sizing preserves the endpoint. Project v8 saves the choice; v1–7 retain matched points.
- Hide bottom interaction hints when viewport space is tight, reclaiming their 40 px footer. Off-view layer warnings remain visible.