3D: Map Art X and Y keep the points a document unit converts to

Typing 10 into X in a millimeters document stored 28.35 pt instead of
28.3464567 pt, because the X and Y setters rounded to two point
decimals. They now only clamp. The flat view also wrote the map back
through the box on every frame, rounding the offsets again; it now
writes the map only when the box was edited.
This commit is contained in:
dotCooCoo committed 2026-11-29 12:57:08 -08:00
1 parent fb199e5d96
commit b893f05f61
1 file changed
+33 -4
+33 -4
View File
@@ -282,8 +282,9 @@ fn set_map_value(fields: &mut DialogFields, key: &str, value: Value) -> Result<(
let n = value.as_f64().filter(|n| n.is_finite()).ok_or("not a number")?;
let mut m = old.ok_or("the surface has no map")?;
match key {
"mapX" => m.x = round_clamp(n, 2, -GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
"mapY" => m.y = round_clamp(n, 2, -GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
// X and Y arrive in points converted from the document's unit: rounding them would move a typed 10 mm.
"mapX" => m.x = n.clamp(-GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
"mapY" => m.y = n.clamp(-GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
"mapScaleX" => m.scale_x = round_clamp(n, 2, GRAPHIC_SCALE_RANGE.0, GRAPHIC_SCALE_RANGE.1),
"mapScaleY" => m.scale_y = round_clamp(n, 2, GRAPHIC_SCALE_RANGE.0, GRAPHIC_SCALE_RANGE.1),
"mapRotation" => m.rotation = n.clamp(-360.0, 360.0),
@@ -380,6 +381,12 @@ fn map_of_box(state: &BoxState, rect: Rect, base: &SurfaceMap) -> SurfaceMap {
}
}
/// The map a box edit leaves, or none while the box is still the one `map` drew (`drawn`), so a frame without an edit
/// keeps the map's numbers exactly as typed.
fn map_after_edit(state: &BoxState, drawn: &BoxState, rect: Rect, map: &SurfaceMap) -> Option<SurfaceMap> {
(state != drawn).then(|| map_of_box(state, rect, map)).filter(|moved| moved != map)
}
/// The id of Map Art's Done, in the footer as Reset all is on the other pages.
pub(crate) const MAP_DONE: &str = "mapDone";
@@ -416,6 +423,7 @@ fn flat_view(ui: &mut Ui, fields: &mut DialogFields, flat: &Flat) -> Response {
}
let (Some(m), Some(rect)) = (map.as_ref(), rect) else { return };
let mut state = map_box(m, rect);
let drawn = state;
if m.rotation.abs() < 0.01
&& let (Some(doc), Some((w, h))) = (flat.doc.as_ref(), flat.art)
{
@@ -432,8 +440,7 @@ fn flat_view(ui: &mut Ui, fields: &mut DialogFields, flat: &Flat) -> Response {
.reference_point(false)
.rotate_zone(RotateZone::border(18.0))
.show_at(ui, body.rect);
let moved = map_of_box(&state, rect, m);
if moved != *m {
if let Some(moved) = map_after_edit(&state, &drawn, rect, m) {
edited = Some(moved);
}
});
@@ -585,4 +592,26 @@ mod tests {
let turned = map(190.0);
assert_eq!(map_of_box(&map_box(&turned, rect), rect, &turned).rotation, -170.0, "a turn wraps to ±180");
}
#[test]
fn map_offsets_keep_the_points_a_document_unit_converts_to() {
// 10 mm, as the X and Y rows hand it over in points.
let ten_mm = 10.0 * 72.0 / 25.4;
let mut fields = DialogFields::new();
store_map(&mut fields, 1, Some(&map(0.0)));
assert_eq!(set_map_value(&mut fields, "mapX", json!(ten_mm)), Ok(()));
assert_eq!(set_map_value(&mut fields, "mapY", json!(-ten_mm)), Ok(()));
let stored = maps_of(&fields).into_iter().find(|m| m.surface == 1).map(|m| (m.x, m.y));
assert_eq!(stored, Some((ten_mm, -ten_mm)), "the typed offset is stored unrounded");
assert_eq!(set_map_value(&mut fields, "mapX", json!(GRAPHIC_MAX_OFFSET * 2.0)), Ok(()));
assert_eq!(maps_of(&fields).first().map(|m| m.x), Some(GRAPHIC_MAX_OFFSET), "the offset still keeps to its bounds");
// A frame without a box edit leaves those numbers as they are.
let rect = art_rect((20.0, 10.0));
let typed = SurfaceMap { x: ten_mm, y: -ten_mm, ..map(0.0) };
let drawn = map_box(&typed, rect);
assert_eq!(map_after_edit(&drawn, &drawn, rect, &typed), None);
let dragged = drawn.moved(Vec2::new(1.0, 0.0));
assert!(map_after_edit(&dragged, &drawn, rect, &typed).is_some(), "a drag still edits the map");
}
}