mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Gradient tool: a handle dragged past the 10% scale floor stops; the other handle stays put (#1243)
Dragging one live-gradient handle towards the other clamps the fill's scale at 10%. For Linear the centre was still taken from the midpoint of the requested points, so the clamped gradient was re-centred and the handle that wasn't being dragged slid along the line. gradient.fill.set now shifts the clamped layout so the handle not given keeps its place. The same apply_set drives the drag preview, so the canvas matches.
This commit is contained in:
1 parent
e5e3e39752
commit
48adaa7edf
2 files changed
+44
-1
No files matched your search
@@ -222,8 +222,25 @@ pub fn apply_set(layer: &Layer, f: &Fill, canvas: Rect, p: &Value, fg: [f32; 4],
|
||||
}
|
||||
if from.is_some() || to.is_some() || (style_changed && !explicit_geometry) {
|
||||
let cur = if from.is_some() || to.is_some() { gf::handles(*style, *angle, *scale, *offset, frame) } else { old_handles };
|
||||
let (a, s, o) = gf::from_handles(*style, from.unwrap_or(cur.0), to.unwrap_or(cur.1), frame, *angle);
|
||||
let (f0, t0) = (from.unwrap_or(cur.0), to.unwrap_or(cur.1));
|
||||
let (a, s, o) = gf::from_handles(*style, f0, t0, frame, *angle);
|
||||
(*angle, *scale, *offset) = (a, clamp_scale(s), o);
|
||||
// Past the scale range the dragged handle stops; the other one stays where it is (#1243).
|
||||
// (Linear centres on the midpoint, so a clamped scale would otherwise move both ends.)
|
||||
let anchor = match (from, to) {
|
||||
(Some(_), None) => Some((t0, false)),
|
||||
(None, Some(_)) => Some((f0, true)),
|
||||
_ => None,
|
||||
};
|
||||
if let Some((pin, is_start)) = anchor
|
||||
&& *scale != s
|
||||
{
|
||||
let (f1, t1) = gf::handles(*style, *angle, *scale, *offset, frame);
|
||||
let at = if is_start { f1 } else { t1 };
|
||||
let (dx, dy) = (pin[0] - at[0], pin[1] - at[1]);
|
||||
let (w, h) = (frame.width().max(1) as f32, frame.height().max(1) as f32);
|
||||
*offset = (offset.0 + dx / w, offset.1 + dy / h);
|
||||
}
|
||||
}
|
||||
if let Some(v) = p.get("stops") {
|
||||
let arr = v.as_array().ok_or_else(|| bad(CMD, "`stops` is [[location, colour], …]"))?;
|
||||
|
||||
@@ -297,3 +297,29 @@ fn commands_fail_gracefully() {
|
||||
let g = s.execute(SET, json!({"scale": 1})).unwrap();
|
||||
assert!((g["scale"].as_f64().unwrap() - 10.0).abs() < 1e-3, "{g}");
|
||||
}
|
||||
|
||||
/// Dragging one handle past the scale range stops it; the other handle stays put (#1243).
|
||||
#[test]
|
||||
fn a_clamped_handle_drag_leaves_the_other_handle_alone() {
|
||||
let pt = |g: &Value, k: &str| [g[k][0].as_f64().unwrap(), g[k][1].as_f64().unwrap()];
|
||||
let close = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1e-2 && (a[1] - b[1]).abs() < 1e-2;
|
||||
for style in STYLES {
|
||||
for (from, to, short, short_start) in [([14.0, 16.0], [34.0, 16.0], [15.0, 16.0], [33.0, 16.0]), ([24.0, 6.0], [24.0, 26.0], [24.0, 7.0], [24.0, 25.0])]
|
||||
{
|
||||
let mut s = session(8, "white");
|
||||
s.execute(CREATE, json!({"from": from, "to": to, "style": style})).unwrap();
|
||||
// End dragged almost onto the start: far below 10 %.
|
||||
let g = s.execute(SET, json!({"to": short})).unwrap();
|
||||
assert!((g["scale"].as_f64().unwrap() - 10.0).abs() < 1e-3, "{style}: {g}");
|
||||
assert!(close(pt(&g, "from"), from), "{style}: start moved to {:?}", pt(&g, "from"));
|
||||
// The end stops on the line towards the pointer.
|
||||
let e = pt(&g, "to");
|
||||
assert!((e[0] - from[0]) * (short[0] - from[0]) >= 0.0 && (e[1] - from[1]) * (short[1] - from[1]) >= 0.0, "{style}: {e:?}");
|
||||
// And the same for the start handle.
|
||||
let mut s = session(8, "white");
|
||||
s.execute(CREATE, json!({"from": from, "to": to, "style": style})).unwrap();
|
||||
let g = s.execute(SET, json!({"from": short_start})).unwrap();
|
||||
assert!(close(pt(&g, "to"), to), "{style}: end moved: {g}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user