From 8747ce527166106afb0afd64076f730f9e7af4ca Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 14 Nov 2026 17:59:49 +0100 Subject: [PATCH] Add toggleable layer input previews and crop guide opacity --- crates/midra-layout/src/capture_ui.rs | 127 ++++++++++++++++++++++- crates/midra-layout/src/format_ui.rs | 135 ++++++++++++++++++++++++- crates/midra-layout/src/gui.rs | 16 ++- docs/interfaces/midra-input-preview.md | 24 ++++- docs/midra-layout/README.md | 16 ++- docs/midra-layout/changelog.md | 2 + docs/midra-layout/plan.md | 7 +- 7 files changed, 306 insertions(+), 21 deletions(-) diff --git a/crates/midra-layout/src/capture_ui.rs b/crates/midra-layout/src/capture_ui.rs index 9a59e5c..685730f 100644 --- a/crates/midra-layout/src/capture_ui.rs +++ b/crates/midra-layout/src/capture_ui.rs @@ -2,10 +2,18 @@ use super::*; use midra_layout::device_capture; type CaptureJob = std::sync::mpsc::Receiver>; +struct LayerPreview { + source: usize, + texture: egui::TextureHandle, + name: String, + enabled: bool, + guides: f32, +} #[derive(Default)] pub(super) struct CaptureUi { pub open: bool, input: u32, + layers: std::collections::BTreeMap, host: String, pending: Option, preview: Option, @@ -16,6 +24,7 @@ pub(super) struct CaptureUi { impl CaptureUi { pub(super) fn clear_preview(&mut self) { self.preview = None; + self.layers.clear(); self.open = false; self.discard_result = true; } @@ -25,6 +34,65 @@ impl CaptureUi { } } impl Editor { + pub(super) fn layer_preview(&self, i: usize) -> Option<(egui::TextureId, f32)> { + self.capture + .layers + .get(&self.layout.layer_info(i).id) + .filter(|p| p.enabled && p.source == self.layout.formats().source_for[i]) + .map(|p| (p.texture.id(), p.guides)) + } + pub(super) fn crop_preview_controls(&mut self, ui: &mut egui::Ui, i: usize) { + let id = self.layout.layer_info(i).id; + let source = self.layout.formats().source_for[i]; + if let Some(preview) = self + .capture + .layers + .get_mut(&id) + .filter(|p| p.source == source) + { + ui.checkbox(&mut preview.enabled, "Show captured input"); + ui.small(&preview.name); + let mut opacity = if preview.enabled { + preview.guides * 100.0 + } else { + 100.0 + }; + if ui + .add_enabled( + preview.enabled, + egui::Slider::new(&mut opacity, 0.0..=100.0) + .text("Crop guide opacity") + .suffix("%"), + ) + .changed() + { + preview.guides = opacity / 100.0; + } + } else { + ui.add_enabled( + false, + egui::Checkbox::new(&mut false, "Show captured input"), + ); + let mut opacity = 100.0; + ui.add_enabled( + false, + egui::Slider::new(&mut opacity, 0.0..=100.0) + .text("Crop guide opacity") + .suffix("%"), + ); + ui.small("Capture a still and use it for this layer to adjust guide opacity."); + } + if ui + .add_enabled( + self.device.online(), + egui::Button::new("Capture input preview…"), + ) + .clicked() + { + self.capture.open = true; + ui.close_menu(); + } + } pub(super) fn capture_window(&mut self, ctx: &egui::Context) { if self.capture.host != self.device.host || !self.device.online() { self.capture.discard_result = true; @@ -70,8 +138,9 @@ impl Editor { let mut open = true; let mut capture = false; let mut add = false; + let mut use_layer = false; egui::Window::new("Capture Midra input").open(&mut open).default_width(560.0).collapsible(false).show(ctx, |ui| { - ui.label("Capture a still input preview from WebRCS and add it as a fixed reference image."); + ui.label("Capture a still input preview for a selected layer or add it as a fixed reference image."); ui.small("Snapshots are reduced-resolution images, not a live feed. Their size depends on the device's snapshot settings."); let before = self.capture.input; ui.add_enabled_ui(self.capture.pending.is_none(), |ui| { @@ -101,6 +170,7 @@ impl Editor { if let Some((severe, warning)) = reference.warning(self.layout.formats().output) { ui.colored_label(if severe { ui.visuals().error_fg_color } else { theme::palette(ctx).amber }, warning); } + use_layer = ui.add_enabled(self.layout.selected().is_some(), egui::Button::new("Use for selected layer")).clicked(); add = ui.button("Add as reference").clicked(); } if !self.capture.error.is_empty() { ui.colored_label(ui.visuals().error_fg_color, &self.capture.error); } @@ -134,6 +204,23 @@ impl Editor { ctx.request_repaint(); }); } + if use_layer + && let Some(i) = self.layout.selected() + && let Some(reference) = &self.capture.preview + { + self.capture.layers.insert( + self.layout.layer_info(i).id, + LayerPreview { + source: self.layout.formats().source_for[i], + texture: reference.texture.clone(), + name: reference.name.clone(), + enabled: true, + guides: 0.35, + }, + ); + self.edit_content = true; + self.capture.open = false; + } if add && let Some(reference) = self.capture.preview.take() { if self.references.len() + self.reference_pending.len() >= 32 || self.references.iter().map(|r| r.bytes.len()).sum::() @@ -194,6 +281,12 @@ mod tests { click_text(&mut app, &ctx, "Capture still"); assert!(app.capture.pending.is_none()); assert_eq!(app.capture.last_request, requested); + click_text(&mut app, &ctx, "Use for selected layer"); + let selected = app.layout.selected().unwrap(); + assert_eq!(app.layer_preview(selected).unwrap().1, 0.35); + assert!(app.edit_content); + assert!(app.references.is_empty()); + app.capture.open = true; click_text(&mut app, &ctx, "Add as reference"); assert_eq!(app.references.len(), 1); assert_eq!(app.project().references.len(), 1); @@ -216,4 +309,36 @@ mod tests { assert!(app.capture.error.is_empty()); assert!(app.references.is_empty()); } + #[test] + fn layer_preview_toggle_keeps_opacity_and_follows_stable_layer_id() { + let ctx = egui::Context::default(); + let mut app = Editor::default(); + let i = app.layout.selected().unwrap(); + let id = app.layout.layer_info(i).id; + let texture = ctx.load_texture( + "test preview", + egui::ColorImage::new([2, 2], Color32::RED), + egui::TextureOptions::default(), + ); + app.capture.layers.insert( + id, + LayerPreview { + source: app.layout.formats().source_for[i], + texture, + name: "Input still".into(), + enabled: true, + guides: 0.25, + }, + ); + assert_eq!(app.layer_preview(i).unwrap().1, 0.25); + app.capture.layers.get_mut(&id).unwrap().enabled = false; + assert!(app.layer_preview(i).is_none()); + app.capture.layers.get_mut(&id).unwrap().enabled = true; + assert_eq!(app.layer_preview(i).unwrap().1, 0.25); + assert!(app.layer_preview(1 - i).is_none()); + app.capture.layers.get_mut(&id).unwrap().source = usize::MAX; + assert!(app.layer_preview(i).is_none()); + app.capture.clear_preview(); + assert!(app.capture.layers.is_empty()); + } } diff --git a/crates/midra-layout/src/format_ui.rs b/crates/midra-layout/src/format_ui.rs index 511e891..3568cfa 100644 --- a/crates/midra-layout/src/format_ui.rs +++ b/crates/midra-layout/src/format_ui.rs @@ -475,11 +475,12 @@ impl Editor { } }); if !ui.input(|input| input.pointer.primary_down()) { d.crop_before = None; } + self.crop_preview_controls(ui, i); let (_,rect)=ui.allocate_space(Vec2::new(ui.available_width(),170.0)); let preview_rect=fit_rect(rect,[current.width,current.height]); - paint_pattern(ui.painter(),preview_rect,self.layout.formats().sources[self.layout.formats().source_for[i]],self.layout.formats().advanced[i]); + paint_source(ui.painter(),preview_rect,self.layout.formats().sources[self.layout.formats().source_for[i]],self.layout.formats().advanced[i],self.layer_preview(i)); ui.small("Crop zoom and offsets apply as you edit. Offsets stop at the content edges and adjust when zoom decreases. Undo restores earlier values."); - ui.label("Source test pattern only; the screen reference image is not source content."); + ui.label("Captured stills follow this layer’s input and dynamic crops. Guides are drawn above the image; screen references are separate."); } if !d.error.is_empty() {ui.colored_label(theme::palette(ui.ctx()).amber,&d.error);} ui.collapsing("Calculated crops / provisional WebRCS mapping",|ui| { @@ -537,6 +538,7 @@ pub(super) fn paint_content_overflow( rect: Rect, source: Source, crop: Crop, + preview: Option<(egui::TextureId, f32)>, ) { let Some(full) = content_bounds(rect, source, crop) else { return; @@ -552,13 +554,60 @@ pub(super) fn paint_content_overflow( if clip.is_positive() { let mut ghost = painter.with_clip_rect(clip); ghost.multiply_opacity(0.5); - paint_pattern_clipped(&ghost, rect, source, crop, clip); + paint_source_clipped(&ghost, rect, source, crop, clip, preview); } } } -pub(super) fn paint_pattern(painter: &egui::Painter, rect: Rect, source: Source, crop: Crop) { - paint_pattern_clipped(painter, rect, source, crop, rect); +pub(super) fn paint_source( + painter: &egui::Painter, + rect: Rect, + source: Source, + crop: Crop, + preview: Option<(egui::TextureId, f32)>, +) { + paint_source_clipped(painter, rect, source, crop, rect, preview); +} + +fn paint_source_clipped( + painter: &egui::Painter, + rect: Rect, + source: Source, + crop: Crop, + clip: Rect, + preview: Option<(egui::TextureId, f32)>, +) { + let mut guides = painter.clone(); + if let Some((texture, opacity)) = preview { + let Ok(input) = source.input() else { + return; + }; + let Ok(retained) = crop.remaining(input) else { + return; + }; + let k = rect.width() as f64 / retained[0]; + let native = Rect::from_min_size( + rect.min + - Vec2::new( + ((source.input_crop.left + crop.left) * k) as f32, + ((source.input_crop.top + crop.top) * k) as f32, + ), + Vec2::new(source.native[0] as f32, source.native[1] as f32) * k as f32, + ); + painter + .with_clip_rect(painter.clip_rect().intersect(clip)) + .image( + texture, + native, + Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)), + Color32::WHITE, + ); + if opacity <= 0.0 { + return; + } + guides.multiply_opacity(opacity); + } + paint_pattern_clipped(&guides, rect, source, crop, clip); } fn paint_pattern_clipped( @@ -1212,3 +1261,79 @@ mod tests { assert_eq!(*app.layout.formats(), formats); } } + +#[cfg(test)] +mod preview_paint_tests { + use super::*; + #[test] + fn captured_texture_follows_both_crops_with_guides_above_and_opaque_fallback() { + let ctx = egui::Context::default(); + let rect = Rect::from_min_size(Pos2::new(100., 100.), Vec2::new(132., 88.)); + let source = Source { + input_crop: Crop { + left: 100., + right: 100., + top: 0., + bottom: 0., + }, + ..Source::default() + }; + let crop = Crop { + left: 200., + right: 200., + top: 100., + bottom: 100., + }; + for preview in [Some((egui::TextureId::User(91), 0.35)), None] { + let output = ctx.run(egui::RawInput::default(), |ctx| { + let p = ctx.layer_painter(egui::LayerId::background()); + paint_source(&p, rect, source, crop, preview); + }); + let shapes = &output.shapes; + let image = shapes.iter().position(|s| matches!(&s.shape, egui::Shape::Mesh(m) if m.texture_id == egui::TextureId::User(91))); + let guides: Vec<_> = shapes + .iter() + .enumerate() + .filter_map(|(i, s)| { + if let egui::Shape::Rect(r) = &s.shape + && r.fill != Color32::TRANSPARENT + { + Some((i, r.fill)) + } else { + None + } + }) + .collect(); + assert!(!guides.is_empty()); + if preview.is_some() { + let index = image.unwrap(); + let egui::Shape::Mesh(mesh) = &shapes[index].shape else { + unreachable!() + }; + let min = mesh + .vertices + .iter() + .map(|v| v.pos) + .fold(Pos2::new(f32::INFINITY, f32::INFINITY), |a, b| a.min(b)); + let max = mesh + .vertices + .iter() + .map(|v| v.pos) + .fold(Pos2::new(f32::NEG_INFINITY, f32::NEG_INFINITY), |a, b| { + a.max(b) + }); + assert_eq!(min, Pos2::new(70., 90.)); + assert_eq!(max, Pos2::new(262., 198.)); + assert!( + guides + .iter() + .all(|(i, c)| *i > index && c.a() < 255 && c.a() > 0) + ); + assert_eq!(shapes[index].clip_rect, rect); + } else { + assert!(image.is_none()); + assert!(guides.iter().all(|(_, c)| c.a() == 255)); + } + } + } +} diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 88a6a85..b0a041e 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -1755,6 +1755,9 @@ impl Editor { self.edit_content = !self.edit_content; ui.close_menu(); } + ui.menu_button("Input preview & crop guides", |ui| { + self.crop_preview_controls(ui, i) + }); if ui.button("Reset crop").clicked() { match self .layout @@ -2868,6 +2871,7 @@ impl Editor { rect, self.layout.formats().sources[self.layout.formats().source_for[i]], self.layout.formats().advanced[i], + self.layer_preview(i), ); } for i in self.layout.viewport_order() { @@ -2903,15 +2907,19 @@ impl Editor { Stroke::new(1.0_f32, color), StrokeKind::Inside, ); - if self.edit_content + let show_guides = self.edit_content && ((self.show_pattern && !self.outline_layers) - || self.layout.selected() == Some(i)) - { - format_ui::paint_pattern( + || self.layout.selected() == Some(i)); + let preview = self.layer_preview(i); + if show_guides || preview.is_some() { + format_ui::paint_source( p, rect, self.layout.formats().sources[self.layout.formats().source_for[i]], self.layout.formats().advanced[i], + preview.map(|(texture, opacity)| { + (texture, if show_guides { opacity } else { 0.0 }) + }), ); p.rect_stroke(rect, 0.0, Stroke::new(1.0_f32, color), StrokeKind::Inside); } diff --git a/docs/interfaces/midra-input-preview.md b/docs/interfaces/midra-input-preview.md index 4b4523a..36b3aaa 100644 --- a/docs/interfaces/midra-input-preview.md +++ b/docs/interfaces/midra-input-preview.md @@ -47,7 +47,23 @@ The initial image name contains the input number and configured device IP. Added stills use the existing [project v10 reference object](midra-layout.md): name, embedded PNG image_base64, visible, opacity, preserve_ratio, native_size, offset. No new fields, version or migration are needed. -Reloading a project makes no capture requests. Captures are fixed viewport guides, -not layer-content textures, live feeds or export placeholders. Generic artwork -exports include them only through the existing visible-reference/background option. -Automatic refresh and separate image-file saving are not implemented. +**Use for selected layer** assigns a session-only still texture to the selected +editor layer's stable ID and current local input. It uses the editor's native +source rectangle, input crop and dynamic crop/zoom mapping, including faded +overflow while editing. It does not infer or change device routing. Changing the +local input hides that assignment; opening/New/Recovery clears assignments. +Reordering layers does not move an assignment to another layer. + +**Show captured input** toggles this texture without discarding it. **Crop guide +opacity** is a per-layer session value from 0–100%, initially 35% when assigned. +The source test-pattern guides paint above the image; selection borders/handles +remain opaque. When the preview is hidden or absent, the slider is disabled at +100% and the original opaque pattern is used. Showing the preview again restores +the chosen opacity. Outside-box content retains the existing 50% ghost treatment. +The same mapping/compositing is used in the viewport and Crop properties preview. + +Layer-preview assignments, texture bytes, visibility toggles and guide opacity +are not saved/exported. Add as reference remains the separate persistent path; +generic artwork exports include those references only through the existing +background option. Reloading a project makes no capture requests. +Automatic refresh, live feeds and separate image-file saving are not implemented. diff --git a/docs/midra-layout/README.md b/docs/midra-layout/README.md index fb107b3..21da5f2 100644 --- a/docs/midra-layout/README.md +++ b/docs/midra-layout/README.md @@ -1004,7 +1004,8 @@ mode only (except outline-only layers). Connect to Midra, then choose **Import → Capture Midra input…** (also available under the viewport context menu's **Reference images**). Choose an available input, -click **Capture still**, inspect the preview, then **Add as reference**. Signal +click **Capture still**, inspect the preview, then **Use for selected layer** or +**Add as reference**. Signal indicators reflect the last connection refresh; an inactive input may return a blank image. If input snapshots are disabled, enable them in WebRCS and refresh the connection. @@ -1014,9 +1015,16 @@ and a minimum five-second gap between requests. Cancel/close ignores the respons another request waits for the old one to finish or time out (20 seconds). The returned resolution/freshness comes from the device; this is not a full-size source recording. Existing image-size/aspect warnings apply before adding. -Added stills are fixed references with independent hide/show/remove controls and -are embedded in saved projects. They do not become layer-content textures or -change source routing. See the [input preview contract](../interfaces/midra-input-preview.md). +**Use for selected layer** shows the still as that layer's source content, following +input and dynamic crop/zoom. In **Crop properties**, or right-click → **Input preview +& crop guides**, toggle **Show captured input** and adjust **Crop guide opacity**. +Guides paint over the image, initially at 35%; without an enabled preview, guides +are fully opaque and the slider is disabled. Selection handles stay fully visible. +Layer previews/settings are session-only and do not trigger refreshes or device writes. + +**Add as reference** instead creates a fixed screen guide with independent +hide/show/remove controls, embedded in saved projects. Neither action changes +source routing. See the [input preview contract](../interfaces/midra-input-preview.md). Opening a project or restoring recovery now reads, validates and decodes embedded references in a worker. The current editor stays intact until the whole project diff --git a/docs/midra-layout/changelog.md b/docs/midra-layout/changelog.md index 56d55f8..79ae074 100644 --- a/docs/midra-layout/changelog.md +++ b/docs/midra-layout/changelog.md @@ -4,6 +4,8 @@ ## Unreleased +- Use captured input stills as toggleable layer previews, with crop guides composited above them and a 0–100% opacity slider in Crop properties and the layer context menu. Guides return to full opacity without an enabled preview; source/input/dynamic crop and ghost overflow share the same mapping. Layer-preview assignments/settings are session-only; capture remains manual and throttled. + - Group the guide and supporting documentation in this tool’s folder; update portal, cross-document and packaging links for the new paths. - Add manual Midra input-still capture through the verified WebRCS PNG endpoint, with preview/Add as reference, one request at a time and a five-second minimum interval. No live polling or device-setting writes; active-input 512 × 288 and inactive blank-image responses checked on the Eikos. diff --git a/docs/midra-layout/plan.md b/docs/midra-layout/plan.md index b5a035f..0a4716a 100644 --- a/docs/midra-layout/plan.md +++ b/docs/midra-layout/plan.md @@ -34,7 +34,8 @@ device readback and physical-screen acceptance. preserve the editor. Manual project loading is cancellable and recovery autosave pauses while loading. - [x] Explicit Midra input snapshot capture: available-input picker, still - preview and Add as reference. Manual only, one request at a time and at least + preview, Use for selected layer, preview visibility and guide-opacity controls, + plus Add as reference. Manual only, one request at a time and at least five seconds between requests. No live polling or automatic refresh. - [x] Clipboard/import mapping review with per-row target/ignore, independent size/position/available-zoom toggles, draw.io stroke handling and own-copy @@ -77,8 +78,8 @@ device readback and physical-screen acceptance. verifying those semantics. Source routing synchronization remains separate from the opt-in shared-input settings editor. - [ ] **Optional capture extensions, if wanted.** Automatic refresh, standalone - capture saving and using captured pixels directly as layer-content textures - are not implemented. Current captures are ordinary fixed reference images; + capture saving and persistence of layer-preview assignments are not implemented. + Captures can be session-only layer textures or saved fixed reference images; low device load remains a requirement for any refresh extension. - [ ] **Additional file codecs, if requested.** PDF, PSD, VSDX and ODG are outside the current geometry contract, not promised existing import/export features.