From 01f2986006ad7852485c9c6e81d8f168dc44d0f8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 14 Nov 2026 12:39:20 +0100 Subject: [PATCH] Add optional independent screen and layer measurement points --- crates/midra-layout/src/format_ui.rs | 15 +- crates/midra-layout/src/gui.rs | 199 +++++++++++++++---- crates/midra-layout/src/lib.rs | 72 +++++++ crates/midra-layout/src/project.rs | 28 ++- crates/midra-layout/src/project_ui.rs | 5 +- crates/midra-layout/tests/positioning.rs | 46 +++++ crates/midra-layout/tests/project.rs | 47 ++++- docs/interfaces/midra-layout-v8.example.json | 101 ++++++++++ docs/interfaces/midra-layout.md | 15 +- docs/midra-layout.md | 13 ++ docs/midralayout/changelog.md | 2 + 11 files changed, 477 insertions(+), 66 deletions(-) create mode 100644 docs/interfaces/midra-layout-v8.example.json diff --git a/crates/midra-layout/src/format_ui.rs b/crates/midra-layout/src/format_ui.rs index af6bd91..3c7a696 100644 --- a/crates/midra-layout/src/format_ui.rs +++ b/crates/midra-layout/src/format_ui.rs @@ -64,7 +64,7 @@ impl FormatDraft { fn apply( &mut self, layout: &mut Layout, - corner: PositionPoint, + corner: PositionReference, locked: bool, ) -> Result<(), String> { self.commit_ratio(locked)?; @@ -290,6 +290,7 @@ impl Editor { } } pub(super) fn formats_window(&mut self, ctx: &egui::Context) { + let position_reference = self.position_reference(); let Some(mut d) = self.format_draft.take() else { return; }; @@ -371,8 +372,8 @@ impl Editor { } if self.property_tab != PropertyTab::Crop { let g=self.layout.layers()[i]; - let [x,y]=self.corner.offsets_on(g,self.layout.formats().output); - let geometry=self.corner.geometry_on(x,y,d.size[0],d.size[1],self.layout.formats().output); + let [x,y]=self.position_reference().offsets_on(g,self.layout.formats().output); + let geometry=self.position_reference().geometry_on(x,y,d.size[0],d.size[1],self.layout.formats().output); let mut preview=self.layout.clone(); let input_matches=d.source.input().is_ok_and(|size| (size[0]-d.input_size[0]).abs()<1e-7 && (size[1]-d.input_size[1]).abs()<1e-7); let reviewed=if input_matches {geometry.and_then(|geometry|preview.configure(i,d.source_id,d.source,geometry,true))} else {Err("Input resolution is invalid or does not match the crop edges.".into())}; @@ -383,7 +384,7 @@ impl Editor { } let pending = d.pending(&self.layout); if apply_button(ui,"Apply source / input / layer formats", pending).clicked() { - match d.apply(&mut self.layout, self.corner, self.keep_aspect_ratio) { Ok(())=>{self.sync();d=FormatDraft::new(&self.layout,i);},Err(e)=>d.error=e } + match d.apply(&mut self.layout, position_reference, self.keep_aspect_ratio) { Ok(())=>{self.sync();d=FormatDraft::new(&self.layout,i);},Err(e)=>d.error=e } } ui.small("Apply commits pending Source and Layer changes together. Switching tabs keeps your edits."); } @@ -437,7 +438,7 @@ impl Editor { if d.closing { match pending_close(ctx, "formats-close", &d.error) { Some(true) => { - match d.apply(&mut self.layout, self.corner, self.keep_aspect_ratio) { + match d.apply(&mut self.layout, position_reference, self.keep_aspect_ratio) { Ok(()) => { self.sync(); return; @@ -609,7 +610,7 @@ mod tests { assert_eq!(layout.layers(), &before); assert!(draft.pending(&layout)); draft - .apply(&mut layout, PositionPoint::Center, true) + .apply(&mut layout, PositionPoint::Center.into(), true) .unwrap(); assert_eq!(layout.layers()[1].center(), before[1].center()); assert_eq!(layout.layers()[1].height, before[1].width * 0.75); @@ -619,7 +620,7 @@ mod tests { draft.ratio_edit.dirty = true; assert!( draft - .apply(&mut layout, PositionPoint::Center, true) + .apply(&mut layout, PositionPoint::Center.into(), true) .is_err() ); assert_eq!(layout.layers(), &before); diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 9d4a38f..e1cc37c 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -18,7 +18,8 @@ mod viewport_guides; use format_ui::{FormatDraft, OutputDraft, PropertyTab}; use midra_layout::import::{DetectedShape, ImportDocument, parse_svg, parse_values}; use midra_layout::{ - AlignTarget, Alignment, Corner, Geometry, Layout, PositionPoint, ResizeHandle, ResizeMode, + AlignTarget, Alignment, Corner, Geometry, Layout, PositionPoint, PositionReference, + ResizeHandle, ResizeMode, }; #[derive(Clone, Copy, Debug, Default, PartialEq)] @@ -41,6 +42,7 @@ pub struct Editor { layout: Layout, files: project_ui::FileUi, corner: PositionPoint, + position_layer_point: Option, fields: [String; 4], field_rects: [Rect; 4], fields_closing: bool, @@ -252,6 +254,7 @@ impl Default for Editor { layout: Layout::default(), files: project_ui::FileUi::default(), corner: PositionPoint::TopRight, + position_layer_point: None, fields: Default::default(), field_rects: [Rect::NOTHING; 4], fields_closing: false, @@ -492,6 +495,12 @@ impl Editor { }); self.reference_open = open; } + fn position_reference(&self) -> PositionReference { + PositionReference { + screen: self.corner, + layer: self.position_layer_point.unwrap_or(self.corner), + } + } fn sync(&mut self) { let selection: Vec<_> = self .layout @@ -529,7 +538,9 @@ impl Editor { self.fields_edited = false; if let Some(i) = self.layout.selected() { let g = self.layout.layers()[i]; - let [x, y] = self.corner.offsets_on(g, self.layout.formats().output); + let [x, y] = self + .position_reference() + .offsets_on(g, self.layout.formats().output); self.fields = [number(x), number(y), number(g.width), number(g.height)]; } self.error = self.layout.gesture_error.clone().unwrap_or_default(); @@ -610,7 +621,9 @@ impl Editor { return false; }; let g = self.layout.layers()[i]; - let [x, y] = self.corner.offsets_on(g, self.layout.formats().output); + let [x, y] = self + .position_reference() + .offsets_on(g, self.layout.formats().output); self.fields .iter() .zip([x, y, g.width, g.height]) @@ -631,7 +644,9 @@ impl Editor { .and_then(|mut v| { if let Some(i) = self.layout.selected() { let g = self.layout.layers()[i]; - let [x, y] = self.corner.offsets_on(g, self.layout.formats().output); + let [x, y] = self + .position_reference() + .offsets_on(g, self.layout.formats().output); let original = [x, y, g.width, g.height]; for j in 0..4 { if v[j] == number(original[j]).parse::().unwrap() { @@ -650,12 +665,14 @@ impl Editor { } } } - self.corner + self.position_reference() .geometry_on(v[0], v[1], v[2], v[3], self.layout.formats().output) .map(|mut geometry| { if let Some(i) = self.layout.selected() { let old = self.layout.layers()[i]; - let offsets = self.corner.offsets_on(old, self.layout.formats().output); + let offsets = self + .position_reference() + .offsets_on(old, self.layout.formats().output); if v[0] == offsets[0] && geometry.width == old.width { geometry.x = old.x; } @@ -952,7 +969,13 @@ impl Editor { let (r, _) = ui.allocate_exact_size( Vec2::new( ui.available_width(), - 1136.0 + (self.layout.layers().len() as f32 - 2.0) * 44.0, + 1136.0 + + if self.position_layer_point.is_some() { + 136.0 + } else { + 28.0 + } + + (self.layout.layers().len() as f32 - 2.0) * 44.0, ), egui::Sense::hover(), ); @@ -1186,7 +1209,17 @@ impl Editor { ).size(12.0).color(palette.muted)).wrap().halign(egui::Align::Min), ); let ink = Ink::new(ui, r.translate(Vec2::new(0.0, 108.0))); - ink.text(0.0, 267.0, "MEASURE POSITION FROM", 11.0, palette.muted); + ink.text( + 0.0, + 267.0, + if self.position_layer_point.is_some() { + "FROM SCREEN POINT" + } else { + "MEASURE POSITION FROM" + }, + 11.0, + palette.muted, + ); ink.card(0.0, 292.0, 94.0, 58.0, palette.field, palette.line); for (x, y, c) in [ (8.0, 300.0, PositionPoint::TopLeft), @@ -1235,6 +1268,61 @@ impl Editor { 12.0, palette.muted, ); + let mut independent = self.position_layer_point.is_some(); + if ui + .put( + Rect::from_min_size(ink.point(0.0, 360.0), Vec2::new(w, 24.0)), + egui::Checkbox::new(&mut independent, "Use a different layer point"), + ) + .changed() + { + self.position_layer_point = independent.then_some(self.corner); + self.sync(); + } + if let Some(mut point) = self.position_layer_point { + ink.text(0.0, 396.0, "TO LAYER POINT", 11.0, palette.muted); + ink.card(0.0, 416.0, 94.0, 58.0, palette.field, palette.line); + for (x, y, candidate) in [ + (8.0, 424.0, PositionPoint::TopLeft), + (84.0, 424.0, PositionPoint::TopRight), + (8.0, 466.0, PositionPoint::BottomLeft), + (84.0, 466.0, PositionPoint::BottomRight), + (46.0, 445.0, PositionPoint::Center), + ] { + ink.rect( + x - 3.0, + y - 3.0, + 6.0, + 6.0, + if point == candidate { + palette.cyan + } else { + palette.muted + }, + ); + if click( + ui, + &ink, + [x - 10.0, y - 10.0, 20.0, 20.0], + format!("layer-point-{candidate:?}"), + ) { + point = candidate; + self.position_layer_point = Some(point); + self.sync(); + } + } + let name = match point { + PositionPoint::TopLeft => "Top left", + PositionPoint::TopRight => "Top right", + PositionPoint::BottomLeft => "Bottom left", + PositionPoint::BottomRight => "Bottom right", + PositionPoint::Center => "Center", + }; + ink.text(112.0, 423.0, name, 18.0, palette.text); + ink.text(112.0, 450.0, "From screen → layer", 12.0, palette.muted); + } + let measure_extra = if independent { 136.0 } else { 28.0 }; + let ink = Ink::new(ui, r.translate(Vec2::new(0.0, 108.0 + measure_extra))); let half = (w - 12.0) / 2.0; let mut enter = false; for (index, x, y, title) in [ @@ -1283,7 +1371,7 @@ impl Editor { )), |ui| self.resize_ratio_option(ui), ); - let ink = Ink::new(ui, r.translate(Vec2::new(0.0, 160.0))); + let ink = Ink::new(ui, r.translate(Vec2::new(0.0, 160.0 + measure_extra))); let pending = self.fields_pending(); let apply = ui .scope_builder( @@ -1312,7 +1400,7 @@ impl Editor { .wrap(), ); } - let ink = Ink::new(ui, r.translate(Vec2::new(0.0, 184.0))); + let ink = Ink::new(ui, r.translate(Vec2::new(0.0, 184.0 + measure_extra))); ink.line([0.0, 640.0], [w, 640.0], palette.line); ink.text( 0.0, @@ -1471,9 +1559,9 @@ impl Editor { }; let g = self.layout.layers()[i]; let output = self.layout.formats().output; - let [x, y] = self.corner.offsets_on(g, output); + let [x, y] = self.position_reference().offsets_on(g, output); let next = self - .corner + .position_reference() .geometry_on(x, y, g.width, g.width / ratio, output)?; self.layout.apply(i, next)?; self.ratio_edit = Default::default(); @@ -2803,36 +2891,14 @@ impl Editor { if let Some(i) = self.layout.selected() { let g = self.layout.layers()[i]; if !g.outside_on(self.layout.formats().output) { - let [dx, dy] = self.corner.offsets_on(g, self.layout.formats().output); - let right = matches!( - self.corner, - PositionPoint::TopRight | PositionPoint::BottomRight - ); - let bottom = matches!( - self.corner, - PositionPoint::BottomLeft | PositionPoint::BottomRight - ); - let mut edge = origin - + Vec2::new( - if right { - (g.x + g.width) as f32 - } else { - g.x as f32 - }, - if bottom { - (g.y + g.height) as f32 - } else { - g.y as f32 - }, - ) * scale; - let mut boundary = origin - + Vec2::new(if right { sw } else { 0.0 }, if bottom { sh } else { 0.0 }) - * scale; - if self.corner == PositionPoint::Center { - let center = g.center(); - edge = origin + Vec2::new(center[0] as f32, center[1] as f32) * scale; - boundary = origin + Vec2::new(sw / 2.0, sh / 2.0) * scale; - } + let [dx, dy] = self + .position_reference() + .offsets_on(g, self.layout.formats().output); + let reference = self.position_reference(); + let point = reference.layer.point_on(g); + let from = reference.screen_point(output); + let edge = origin + Vec2::new(point[0] as f32, point[1] as f32) * scale; + let boundary = origin + Vec2::new(from[0] as f32, from[1] as f32) * scale; let guide = &screen.painter; let stroke = Stroke::new(1.0_f32, palette.amber); guide.line_segment([edge, Pos2::new(boundary.x, edge.y)], stroke); @@ -3676,6 +3742,55 @@ mod tests { ); frame(app, ctx, vec![mouse(pos, false)]); } + #[test] + fn optional_layer_point_changes_measurements_only_and_center_survives_size_edits() { + let ctx = egui::Context::default(); + let mut app = Editor { + corner: PositionPoint::TopLeft, + ..Default::default() + }; + app.sync(); + let before = app.layout.layers().clone(); + let has_advanced = |out: &egui::FullOutput| { + out.shapes.iter().any( + |s| matches!(&s.shape, egui::Shape::Text(t) if t.galley.text() == "TO LAYER POINT"), + ) + }; + assert!(!has_advanced(&frame(&mut app, &ctx, vec![]))); + click_text(&mut app, &ctx, "Use a different layer point"); + let output = frame(&mut app, &ctx, vec![]); + let center = output + .shapes + .iter() + .find_map(|s| match &s.shape { + egui::Shape::Text(t) if t.galley.text() == "TO LAYER POINT" => { + Some(t.pos + Vec2::new(46.0, 49.0)) + } + _ => None, + }) + .unwrap(); + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(center), mouse(center, true)], + ); + frame(&mut app, &ctx, vec![mouse(center, false)]); + assert_eq!(app.position_layer_point, Some(PositionPoint::Center)); + assert_eq!(app.layout.layers(), &before); + let center = before[1].center(); + assert_eq!(app.fields[0], number(center[0])); + assert_eq!(app.fields[1], number(center[1])); + app.fields[2] = "400".into(); + app.apply_fields(); + assert_eq!(app.layout.layers()[1].center(), center); + let resized = app.layout.layers().clone(); + click_text(&mut app, &ctx, "Use a different layer point"); + assert!(app.position_layer_point.is_none()); + assert!(!has_advanced(&frame(&mut app, &ctx, vec![]))); + assert_eq!(app.layout.layers(), &resized); + assert_eq!(app.fields[0], number(resized[1].x)); + } + #[test] fn interaction_footer_collapses_and_returns_without_hiding_output_labels_or_warnings() { let ctx = egui::Context::default(); diff --git a/crates/midra-layout/src/lib.rs b/crates/midra-layout/src/lib.rs index 08aa1af..21f60be 100644 --- a/crates/midra-layout/src/lib.rs +++ b/crates/midra-layout/src/lib.rs @@ -333,6 +333,78 @@ impl PositionPoint { } } +/// Independent screen origin and layer endpoint for numeric positioning. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PositionReference { + pub screen: PositionPoint, + pub layer: PositionPoint, +} +impl From for PositionReference { + fn from(point: PositionPoint) -> Self { + Self { + screen: point, + layer: point, + } + } +} +impl PositionPoint { + fn fractions(self) -> [f64; 2] { + match self { + Self::TopLeft => [0.0, 0.0], + Self::TopRight => [1.0, 0.0], + Self::BottomLeft => [0.0, 1.0], + Self::BottomRight => [1.0, 1.0], + Self::Center => [0.5, 0.5], + } + } + pub fn point_on(self, g: Geometry) -> [f64; 2] { + let f = self.fractions(); + [g.x + f[0] * g.width, g.y + f[1] * g.height] + } +} +impl PositionReference { + pub fn screen_point(self, output: [u32; 2]) -> [f64; 2] { + let f = self.screen.fractions(); + [f[0] * output[0] as f64, f[1] * output[1] as f64] + } + fn signs(self) -> [f64; 2] { + self.screen + .fractions() + .map(|f| if f == 1.0 { -1.0 } else { 1.0 }) + } + pub fn offsets_on(self, g: Geometry, output: [u32; 2]) -> [f64; 2] { + if self.screen == self.layer { + return self.screen.offsets_on(g, output); + } + let from = self.screen_point(output); + let to = self.layer.point_on(g); + let signs = self.signs(); + [(to[0] - from[0]) * signs[0], (to[1] - from[1]) * signs[1]] + } + pub fn geometry_on( + self, + dx: f64, + dy: f64, + width: f64, + height: f64, + output: [u32; 2], + ) -> Result { + if self.screen == self.layer { + return self.screen.geometry_on(dx, dy, width, height, output); + } + let from = self.screen_point(output); + let f = self.layer.fractions(); + let signs = self.signs(); + Geometry { + x: from[0] + dx * signs[0] - f[0] * width, + y: from[1] + dy * signs[1] - f[1] * height, + width, + height, + } + .validate() + } +} + /// Edge or center line to align, without resizing layers. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Alignment { diff --git a/crates/midra-layout/src/project.rs b/crates/midra-layout/src/project.rs index 7d4e2c1..2edb4fc 100644 --- a/crates/midra-layout/src/project.rs +++ b/crates/midra-layout/src/project.rs @@ -92,6 +92,8 @@ pub struct EditorSettings { pub backdrop_title: String, pub selected_layer_ids: Vec, pub position_point: PositionPoint, + #[serde(deserialize_with = "deserialize_layer_point")] + pub position_layer_point: Option, pub keep_aspect_ratio: bool, pub viewport_zoom: f32, pub viewport_pan: [f32; 2], @@ -99,6 +101,11 @@ pub struct EditorSettings { pub show_toolbar_icon_names: bool, pub outline_layers: bool, } +fn deserialize_layer_point<'de, D: serde::Deserializer<'de>>( + deserializer: D, +) -> Result, D::Error> { + Option::::deserialize(deserializer) +} #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Project { @@ -115,7 +122,7 @@ impl Project { pub fn from_layout(layout: &Layout) -> Self { Self { format: FORMAT.into(), - version: 7, + version: 8, next_layer_id: layout.next_id, output_resolution: layout.formats.output.into(), inputs: (0..layout.formats.sources.len()) @@ -151,6 +158,7 @@ impl Project { .map(|i| layout.info[i].id) .collect(), position_point: PositionPoint::TopLeft, + position_layer_point: None, keep_aspect_ratio: true, viewport_zoom: 1.0, viewport_pan: [0.0; 2], @@ -162,9 +170,9 @@ impl Project { } /// Build a fresh document only after validating the entire file. No history is restored. pub fn layout(&self) -> Result { - if self.format != FORMAT || self.version != 7 { + if self.format != FORMAT || self.version != 8 { return Err( - "Unsupported project format/version; expected beffroi.midra-layout version 7." + "Unsupported project format/version; expected beffroi.midra-layout version 8." .into(), ); } @@ -290,8 +298,8 @@ impl Project { } let header: Header = serde_json::from_str(text).map_err(|e| format!("Invalid project JSON: {e}"))?; - if !(1..=7).contains(&header.version) { - return Err("Unsupported project version; expected versions 1–7.".into()); + if !(1..=8).contains(&header.version) { + return Err("Unsupported project version; expected versions 1–8.".into()); } let mut migrated: serde_json::Value = if header.version == 1 || header.version == 2 { let mut value: serde_json::Value = @@ -395,6 +403,16 @@ impl Project { editor.insert("backdrop_title".into(), DEFAULT_BACKDROP_TITLE.into()); migrated["version"] = 7.into(); } + if (1..=7).contains(&header.version) { + let editor = migrated["editor"] + .as_object_mut() + .ok_or("Missing editor settings")?; + if editor.contains_key("position_layer_point") { + return Err("Unexpected layer position point in old project".into()); + } + editor.insert("position_layer_point".into(), serde_json::Value::Null); + migrated["version"] = 8.into(); + } let p: Self = serde_json::from_value(migrated).map_err(|e| format!("Invalid project JSON: {e}"))?; p.layout()?; diff --git a/crates/midra-layout/src/project_ui.rs b/crates/midra-layout/src/project_ui.rs index 8924491..9cc6810 100644 --- a/crates/midra-layout/src/project_ui.rs +++ b/crates/midra-layout/src/project_ui.rs @@ -13,6 +13,7 @@ impl Editor { fn project(&self) -> Project { let mut p = Project::from_layout(&self.layout); p.editor.position_point = self.corner; + p.editor.position_layer_point = self.position_layer_point; p.editor.backdrop_title = self.backdrop_title.clone(); p.editor.keep_aspect_ratio = self.keep_aspect_ratio; p.editor.viewport_zoom = self.zoom; @@ -70,6 +71,7 @@ impl Editor { layout, reference, corner: e.position_point, + position_layer_point: e.position_layer_point, backdrop_title: e.backdrop_title, keep_aspect_ratio: e.keep_aspect_ratio, zoom: e.viewport_zoom, @@ -344,7 +346,8 @@ mod tests { app.outline_layers = true; app.zoom = 2.5; app.pan = Vec2::new(-20.5, 40.0); - app.corner = PositionPoint::Center; + app.corner = PositionPoint::TopLeft; + app.position_layer_point = Some(PositionPoint::Center); app.backdrop_title = "Conference 2027 · Main stage".into(); app.keep_aspect_ratio = false; app.show_pattern = true; diff --git a/crates/midra-layout/tests/positioning.rs b/crates/midra-layout/tests/positioning.rs index 1aee9f9..0c56ca5 100644 --- a/crates/midra-layout/tests/positioning.rs +++ b/crates/midra-layout/tests/positioning.rs @@ -984,3 +984,49 @@ fn crop_corner_zoom_keeps_layer_fixed_and_supports_limits_history_and_cancel() { assert!(!l.dragging()); } } + +#[test] +fn independent_measurement_points_roundtrip_and_resize_about_the_layer_endpoint() { + use midra_layout::{PositionPoint, PositionReference}; + let points = [ + PositionPoint::TopLeft, + PositionPoint::TopRight, + PositionPoint::BottomLeft, + PositionPoint::BottomRight, + PositionPoint::Center, + ]; + let output = [1921, 1081]; + let geometry = Geometry { + x: -120.25, + y: 183.5, + width: 641.5, + height: 361.25, + }; + for screen in points { + for layer in points { + let reference = PositionReference { screen, layer }; + let [x, y] = reference.offsets_on(geometry, output); + assert_eq!( + reference + .geometry_on(x, y, geometry.width, geometry.height, output) + .unwrap(), + geometry + ); + let resized = reference.geometry_on(x, y, 200.25, 400.5, output).unwrap(); + assert_eq!(layer.point_on(resized), layer.point_on(geometry)); + assert_eq!(reference.offsets_on(resized, output), [x, y]); + } + } + let reference = PositionReference { + screen: PositionPoint::TopLeft, + layer: PositionPoint::Center, + }; + assert_eq!(reference.offsets_on(geometry, output), geometry.center()); + assert_eq!( + reference + .geometry_on(960.5, 540.5, 640.0, 360.0, output) + .unwrap() + .center(), + [960.5, 540.5] + ); +} diff --git a/crates/midra-layout/tests/project.rs b/crates/midra-layout/tests/project.rs index 9d5873f..9a13a56 100644 --- a/crates/midra-layout/tests/project.rs +++ b/crates/midra-layout/tests/project.rs @@ -225,7 +225,7 @@ fn persistent_midra_links_roundtrip_migrate_and_undo() { include_str!("../../../docs/interfaces/midra-layout-v5.example.json"), ] { let p = Project::from_json(text).unwrap(); - assert_eq!(p.version, 7); + assert_eq!(p.version, 8); assert!(p.layers.iter().all(|l| l.midra_link.is_none())); } let mut value = serde_json::to_value(&project).unwrap(); @@ -302,7 +302,7 @@ fn position_point_schema_migrates_corners_and_rejects_mislabeled_files() { .unwrap(); old["editor"]["position_corner"] = name.into(); let loaded = Project::from_json(&old.to_string()).unwrap(); - assert_eq!(loaded.version, 7); + assert_eq!(loaded.version, 8); assert_eq!(loaded.editor.position_point, expected); old["editor"]["position_corner"] = "center".into(); assert!(Project::from_json(&old.to_string()).is_err()); @@ -311,7 +311,7 @@ fn position_point_schema_migrates_corners_and_rejects_mislabeled_files() { assert!(Project::from_json(&old.to_string()).is_err()); } let value: serde_json::Value = serde_json::from_str(¤t.to_json().unwrap()).unwrap(); - for version in [0, 8] { + for version in [0, 9] { let mut invalid = value.clone(); invalid["version"] = version.into(); assert!( @@ -352,7 +352,7 @@ fn backdrop_title_roundtrips_and_older_projects_gain_the_default() { } let old = include_str!("../../../docs/interfaces/midra-layout-v6.example.json"); let migrated = Project::from_json(old).unwrap(); - assert_eq!(migrated.version, 7); + assert_eq!(migrated.version, 8); assert_eq!(migrated.editor.backdrop_title, DEFAULT_BACKDROP_TITLE); let mut invalid: serde_json::Value = serde_json::from_str(old).unwrap(); invalid["editor"]["backdrop_title"] = "Unexpected".into(); @@ -370,3 +370,42 @@ fn backdrop_title_roundtrips_and_older_projects_gain_the_default() { .remove("backdrop_title"); assert!(Project::from_json(&missing.to_string()).is_err()); } + +#[test] +fn independent_layer_point_is_explicit_and_old_projects_keep_matched_points() { + use midra_layout::PositionPoint; + let current = Project::from_json(include_str!( + "../../../docs/interfaces/midra-layout-v8.example.json" + )) + .unwrap(); + assert_eq!(current, Project::from_layout(&Layout::default())); + for point in [ + None, + Some(PositionPoint::Center), + Some(PositionPoint::BottomRight), + ] { + let mut project = current.clone(); + project.editor.position_layer_point = point; + assert_eq!( + Project::from_json(&project.to_json().unwrap()).unwrap(), + project + ); + } + let old = include_str!("../../../docs/interfaces/midra-layout-v7.example.json"); + assert!( + Project::from_json(old) + .unwrap() + .editor + .position_layer_point + .is_none() + ); + let mut invalid: serde_json::Value = serde_json::from_str(old).unwrap(); + invalid["editor"]["position_layer_point"] = "center".into(); + assert!(Project::from_json(&invalid.to_string()).is_err()); + let mut missing: serde_json::Value = serde_json::from_str(¤t.to_json().unwrap()).unwrap(); + missing["editor"] + .as_object_mut() + .unwrap() + .remove("position_layer_point"); + assert!(Project::from_json(&missing.to_string()).is_err()); +} diff --git a/docs/interfaces/midra-layout-v8.example.json b/docs/interfaces/midra-layout-v8.example.json new file mode 100644 index 0000000..ba9a4a8 --- /dev/null +++ b/docs/interfaces/midra-layout-v8.example.json @@ -0,0 +1,101 @@ +{ + "format": "beffroi.midra-layout", + "version": 8, + "output_resolution": { + "width": 1920, + "height": 1080 + }, + "inputs": [ + { + "id": 1, + "source_format": "sixteen_nine", + "native_resolution": { + "width": 1920, + "height": 1080 + }, + "input_crop": { + "left": 0.0, + "right": 0.0, + "top": 0.0, + "bottom": 0.0 + } + }, + { + "id": 2, + "source_format": "sixteen_nine", + "native_resolution": { + "width": 1920, + "height": 1080 + }, + "input_crop": { + "left": 0.0, + "right": 0.0, + "top": 0.0, + "bottom": 0.0 + } + } + ], + "layers": [ + { + "id": 1, + "source_id": 1, + "bounds": { + "x": 120.0, + "y": 180.0, + "width": 640.0, + "height": 360.0 + }, + "advanced_crop": { + "left": 0.0, + "right": 0.0, + "top": 0.0, + "bottom": 0.0 + }, + "visible": true, + "locked": false, + "name": "Presenter", + "folder": "", + "midra_link": null + }, + { + "id": 2, + "source_id": 2, + "bounds": { + "x": 1160.0, + "y": 180.0, + "width": 640.0, + "height": 360.0 + }, + "advanced_crop": { + "left": 0.0, + "right": 0.0, + "top": 0.0, + "bottom": 0.0 + }, + "visible": true, + "locked": false, + "name": "Slides", + "folder": "", + "midra_link": null + } + ], + "reference": null, + "editor": { + "backdrop_title": "Example Conference Name", + "selected_layer_ids": [ + 2 + ], + "position_point": "top_left", + "keep_aspect_ratio": true, + "viewport_zoom": 1.0, + "viewport_pan": [ + 0.0, + 0.0 + ], + "show_source_pattern": false, + "show_toolbar_icon_names": false, + "outline_layers": false, + "position_layer_point": null + }, + "next_layer_id": 3 +} diff --git a/docs/interfaces/midra-layout.md b/docs/interfaces/midra-layout.md index c9a0b10..3e5540c 100644 --- a/docs/interfaces/midra-layout.md +++ b/docs/interfaces/midra-layout.md @@ -5,7 +5,7 @@ These are offline editor formats, not Analog Way REST payloads. Implementation: [`project.rs`](../../crates/midra-layout/tests/project.rs). Update this contract, examples and tests in the same change as any save/export schema change. -## Project JSON, version 7 +## Project JSON, version 8 Suggested filename: `layout.midra.json`. Encoding: UTF-8, pretty-printed JSON, with a trailing newline. A project preserves applied document values at their @@ -18,16 +18,16 @@ and `next_layer_id: 3`; other values are preserved. The [version 1 example](midra-layout-v1.example.json) remains a migration fixture. Version 4 adds `editor.outline_layers: false`, and reference `native_size: false` and `offset: [0, 0]` when migrating versions 1–3. Old fitting behavior is preserved. -Version 5 adds required `midra_link` to every layer. Versions 1–4 migrate with null links; links cannot be supplied under an older version. Version 6 renames `editor.position_corner` to `editor.position_point` and adds `center`. Versions 1–5 retain their original corner; center and the new field are rejected in those older schemas. Version 7 adds `editor.backdrop_title`; versions 1–6 gain `"Example Conference Name"` and cannot supply this field under the old schema. Saving always writes version 7; older applications reject it. +Version 5 adds required `midra_link` to every layer. Versions 1–4 migrate with null links; links cannot be supplied under an older version. Version 6 renames `editor.position_corner` to `editor.position_point` and adds `center`. Versions 1–5 retain their original corner; center and the new field are rejected in those older schemas. Version 7 adds `editor.backdrop_title`; versions 1–6 gain `"Example Conference Name"` and cannot supply this field under the old schema. Version 8 adds required `editor.position_layer_point`: null keeps matched points; a point enables independent measurement. Versions 1–7 migrate to null and cannot supply this field. Saving always writes version 8; older applications reject it. Incompatible files are never partially applied. -[Complete example](midra-layout-v7.example.json) is a valid default project, +[Complete example](midra-layout-v8.example.json) is a valid default project, without a reference image. It is loaded by the regression tests. | Root field | Representation / meaning | | --- | --- | | `format` | Exactly `"beffroi.midra-layout"` | -| `version` | Integer `7` | +| `version` | Integer `8` | | `next_layer_id` | Positive integer strictly greater than every layer ID; not rewound by undo | | `output_resolution` | `{ "width": integer, "height": integer }`, screen pixels | | `inputs` | 1–1024 input objects, consecutive IDs starting at 1 | @@ -94,7 +94,8 @@ Editor object: | --- | --- | | `selected_layer_ids` | Array of unique IDs of visible, unlocked layers; may be empty | | `backdrop_title` | Required string, 0–128 Unicode characters, no control characters. Example backdrop title; empty hides the text. Preserved exactly, including spaces; omitted from SVG/device output. | -| `position_point` | `"top_left"`, `"top_right"`, `"bottom_left"`, `"bottom_right"`, `"center"` | +| `position_point` | Screen measurement origin: `"top_left"`, `"top_right"`, `"bottom_left"`, `"bottom_right"`, `"center"` | +| `position_layer_point` | Required: null for the matching layer point (advanced toggle off), or one of the same five point strings for an independent layer endpoint (toggle on) | | `keep_aspect_ratio` | Boolean for layer resizing | | `viewport_zoom` | Positive fit-relative scale, 0.0001–256 | | `viewport_pan` | `[x, y]`, viewport pan in screen-coordinate pixels; finite, absolute value at most 1e9 | @@ -102,7 +103,7 @@ Editor object: | `show_toolbar_icon_names` | Boolean: show names beneath toolbar icons; false by default | | `outline_layers` | Boolean: transparent layer fill for reference alignment; selection/handles remain visible | -Position offsets use matching layer/screen points. Corners measure inward from their screen edges. Center measures layer center minus screen center in pixels (positive right/down); resizing preserves those offsets. Changing the reference alone never moves a layer. Stored bounds remain top-left coordinates. +Position offsets measure from `position_point` on the screen to the chosen layer point (matching when `position_layer_point` is null). Screen corners retain inward signs: right origins reverse X, bottom origins reverse Y. Screen center uses positive right/down. For screen top-left → layer center, offsets are the layer’s absolute center coordinates. Numeric resizing and manual ratio edits preserve the chosen layer endpoint’s offsets. Switching either point or toggling independent measurement never moves a layer. Stored bounds remain top-left coordinates; viewport guides use the same endpoints. Resolution dimensions must be whole pixels from 1 to 16384. Layer x/y must be finite and within ±1,000,000 pixels; layer widths/heights and retained input dimensions must be positive, finite, and at most 1,000,000 @@ -178,7 +179,7 @@ and `viewBox="0 0 W H"` equal to the output screen. The root carries ## Compatibility changes -Project version 7 and SVG version 1 are strict contracts. A breaking project field/meaning change must bump +Project version 8 and SVG version 1 are strict contracts. A breaking project field/meaning change must bump `version` and either implement a tested migration or clearly reject older/newer files. Changing rectangle/marker semantics requires a corresponding documented SVG interface revision and importer tests. Update this document and its example diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 05ca7d7..3d04bda 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -291,6 +291,19 @@ Files are limited to 64 MiB and dimensions to 8192 pixels per side or the GPU's texture limit, whichever is smaller. Invalid imports keep the previous image. Reference settings are session-only and independent of layer undo/redo. +### Optional independent positioning points + +**Measure position from** normally uses matching screen/layer points, keeping the +inspector compact. Enable **Use a different layer point** to reveal **To layer +point**. The first selector is the screen origin; the second is the layer endpoint. +For absolute center coordinates, choose screen **Top left** and layer **Center**. + +Changing points or turning the toggle off only updates the displayed offsets. +Numeric size and ratio edits keep the selected layer endpoint fixed. Screen-right +and screen-bottom origins keep their existing inward-positive signs; screen +center uses positive right/down. Both points and the toggle save with the project; +older projects open with matched points. Canvas guides show the chosen endpoints. + ### Displaying and entering aspect ratios Layer dimensions and resolution controls show their current width:height ratio diff --git a/docs/midralayout/changelog.md b/docs/midralayout/changelog.md index 550fbf6..15b31c2 100644 --- a/docs/midralayout/changelog.md +++ b/docs/midralayout/changelog.md @@ -4,6 +4,8 @@ ## Unreleased +- 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. - Fit screen now includes the example backdrop’s Screen 1/resolution labels, preventing clipping in shorter/wider viewports.