From 6d72dbbf940f0cb59501ceb826a8252dfc78bca5 Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Fri, 27 Nov 2026 05:04:00 +0000 Subject: [PATCH] Layers: Escape closes the layer list, and a flattened folder keeps its look over the layers below and its hidden state From the first test build: Escape only took the focus off the list's top row, a folder whose layers blend with Multiply, Screen or Overlay over the layers below it flattened into a layer that looked different, and a hidden folder flattened into a visible layer. --- crates/compose/src/eval.rs | 60 ++++++++++++++++++------ crates/engine/src/layer_ops.rs | 3 +- crates/engine/tests/it/flatten_folder.rs | 35 ++++++++++++++ crates/ui-egui/src/menus.rs | 2 + crates/ui-egui/tests/it/layers_panel.rs | 10 +++- 5 files changed, 94 insertions(+), 16 deletions(-) diff --git a/crates/compose/src/eval.rs b/crates/compose/src/eval.rs index d901bbda..93c6b0c2 100644 --- a/crates/compose/src/eval.rs +++ b/crates/compose/src/eval.rs @@ -1369,6 +1369,17 @@ fn below_plan<'a>( return Vec::new(); } + stack_plan_below(inp, draw, channels, (tile_x, tile_y), fields) +} + +/// Plan the whole stack up to a draw, whatever the draw reads +fn stack_plan_below<'a>( + inp: &EvaluationInputs<'a>, + draw: (LayerId, usize), + channels: &[Channel], + (tile_x, tile_y): (u32, u32), + fields: Option<&'a Fields>, +) -> Vec> { let Ok(set) = inp.project.texture_set(inp.ts) else { return Vec::new() }; let compare_effects = layer_mask(inp.project, draw.0).into_iter().flat_map(|mask| mask.effects.iter()); let planned = compared_channels(compare_effects, channels.to_vec()); @@ -4988,6 +4999,26 @@ pub fn flatten_layer(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Opti Some(out.into_iter().flatten().collect()) } +/// The premultiplied value and coverage (last) of a Normal layer that shows `shown` over `under`, covering at least `coverage`, with straight values inside `range` (Multiply or Overlay hide each component by a different amount, so the coverage grows where a straight value would leave it); None where it shows the stack as it is +fn normal_over(under: &[f32; 3], shown: &[f32; 3], coverage: f32, comps: usize, (low, high): (f32, f32)) -> Option<[f32; 4]> { + let needed = under.iter().zip(shown).take(comps).fold(coverage, |needed, (under, shown)| { + let change = shown - under; + let room = if change > 0.0 { high - under } else { under - low }; + if change.abs() <= f32::EPSILON || room <= 0.0 { needed } else { needed.max(change.abs() / room) } + }); + let coverage = needed.min(1.0); + if coverage <= 0.0 { + return None; + } + + // Over `under`, the layer shows its premultiplied value plus what it leaves of the stack + let mut premultiplied = [0.0, 0.0, 0.0, coverage]; + for ((slot, under), shown) in premultiplied.iter_mut().zip(under).zip(shown).take(comps) { + *slot = shown - (1.0 - coverage) * under; + } + Some(premultiplied) +} + /// One tile of a flattened folder's mask: (tile, mask value + coverage data) pub type FlatMaskTile = ((u32, u32), Vec); @@ -5017,7 +5048,7 @@ pub fn flatten_folder(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Opt let nodes = plan(&visible_inputs, &[id], &channels, tx, ty, 0, Some(&fields)); let Some(node) = nodes.first() else { return (Vec::new(), None) }; let Content::Group(children) = &node.content else { return (Vec::new(), None) }; - let below = below_plan(&visible_inputs, (id, 0), &planned_channels, (tx, ty), Some(&fields), node.lights); + let below = stack_plan_below(&visible_inputs, (id, 0), &planned_channels, (tx, ty), Some(&fields)); // One value + coverage buffer per channel the folder plans; signed channels start at their middle let mut buffers: Vec<(Channel, Vec, bool)> = node @@ -5039,29 +5070,30 @@ pub fn flatten_folder(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Opt for (x, y, offset) in tile_texels(inputs.gbuf, tx, ty) { // The texel with the stack below the folder run once, so masks and fills that read it or its anchors find them let mut over_below = Texel::new(&visible_inputs, x, y, offset, 0.0); - if !below.is_empty() { - over_below.anchors_below(&below, (id, 0)); - } + over_below.anchors_below(&below, (id, 0)); + let stack_below = over_below.below.unwrap_or_else(|| Channel::ALL.map(Channel::default_value)); - // Laid over an all-0 and an all-1 stack, the layers show their premultiplied value and how much of the stack they hide - let over = |background: f32| { + // The layers over an all-0 and an all-1 stack show how much of the stack they hide, and over the real stack what they show there + let over = |mut stack: Acc| { let mut texel = over_below.clone(); - let mut stack: Acc = [[background; 3]; NCH]; texel.eval(children, &mut stack); stack }; - let (low, high) = (over(0.0), over(1.0)); + let (low, high, shown) = (over([[0.0; 3]; NCH]), over([[1.0; 3]; NCH]), over(stack_below)); for (planned, (channel, buffer, laid_down)) in node.chans.iter().zip(buffers.iter_mut()) { - let (Some(premultiplied), Some(shown)) = (low.get(planned.ch), high.get(planned.ch)) else { continue }; - let comps = planned.comps.min(3); - let hidden = premultiplied.iter().zip(shown).take(comps).map(|(low, high)| 1.0 - (high - low)).sum::() / comps as f32; - let coverage = hidden.clamp(0.0, 1.0); - if coverage <= 0.0 { + let (Some(low), Some(high), Some(shown), Some(under)) = + (low.get(planned.ch), high.get(planned.ch), shown.get(planned.ch), stack_below.get(planned.ch)) + else { continue; - } + }; + let comps = planned.comps.min(3); + let hidden = low.iter().zip(high).take(comps).map(|(low, high)| 1.0 - (high - low)).sum::() / comps as f32; + let range = if channel.kind() == ChannelKind::Signed { (-1.0, 1.0) } else { (0.0, 1.0) }; + let Some(premultiplied) = normal_over(under, shown, hidden.clamp(0.0, 1.0), comps, range) else { continue }; *laid_down = true; let Some(pixel) = buffer.get_mut(offset * (planned.comps + 1)..(offset + 1) * (planned.comps + 1)) else { continue }; + let coverage = premultiplied[3]; for (slot, value) in pixel.iter_mut().zip(premultiplied.iter().take(planned.comps)) { *slot = if channel.kind() == ChannelKind::Signed { to_u16_signed(*value) } else { to_u16(*value) }; } diff --git a/crates/engine/src/layer_ops.rs b/crates/engine/src/layer_ops.rs index e11121d3..4f1cf4d1 100644 --- a/crates/engine/src/layer_ops.rs +++ b/crates/engine/src/layer_ops.rs @@ -236,7 +236,7 @@ fn flatten(s: &mut Session, p: &Value) -> Result { Ok(json!({ "layer": id.0 })) } -/// Flatten `folder` of texture set `ts` into a new fill layer above it, reading one image per channel it lays down, with its mask baked into a painted mask; the folder is hidden and kept +/// Flatten `folder` of texture set `ts` into a new fill layer above it, reading one image per channel it lays down, with its mask baked into a painted mask; the folder is hidden and kept, and the new layer shows only if the folder did fn flatten_folder(s: &mut Session, ts: usize, folder: &Layer) -> Result { s.update(); let size = s.caches.get(ts).ok_or_else(|| EngineError::Other("texture set not ready".into()))?.size; @@ -260,6 +260,7 @@ fn flatten_folder(s: &mut Session, ts: usize, folder: &Layer) -> Result { // Only the channels the folder lays down, at its opacity (a folder mixes its result in by opacity alone, whatever its blend mode): a channel without an image would cover the stack with its default let channels: Vec = fill.sources.keys().copied().collect(); let mut layer = Layer::new(s.doc.project.alloc_id(), format!("{} (flattened)", folder.name), LayerKind::Fill(fill), &channels); + layer.visible = folder.visible; for (channel, blend) in &mut layer.channels { blend.opacity = folder.blend(*channel).map_or(1.0, |folder_blend| folder_blend.opacity); } diff --git a/crates/engine/tests/it/flatten_folder.rs b/crates/engine/tests/it/flatten_folder.rs index 741f7e80..77fefc27 100644 --- a/crates/engine/tests/it/flatten_folder.rs +++ b/crates/engine/tests/it/flatten_folder.rs @@ -97,3 +97,38 @@ fn a_flattened_folder_bakes_a_mask_that_reads_the_layers_below() { assert!(largest_difference(&composite(&mut s), &before) < TOLERANCE, "{}", largest_difference(&composite(&mut s), &before)); } + +/// Inside a folder, layers blending with Multiply, Screen or Overlay over the layers below the folder hide each part of the stack by a different amount: the flattened layer still shows what they did +#[test] +fn a_flattened_folder_looks_the_same_when_its_layers_blend_with_the_layers_below_it() { + for mode in ["multiply", "screen", "overlay"] { + let mut s = session(); + id(execute(&mut s, "layer.add.fill", &json!({"color": [0.2, 0.5, 0.8], "roughness": 0.4})).unwrap()); + let folder = id(execute(&mut s, "layer.add.group", &Value::Null).unwrap()); + let inner = id(execute(&mut s, "layer.add.fill", &json!({"color": [0.9, 0.3, 0.1], "roughness": 0.7})).unwrap()); + for channel in ["baseColor", "roughness"] { + execute(&mut s, "layer.blend", &json!({"layer": inner, "channel": channel, "mode": mode})).unwrap(); + } + let before = composite(&mut s); + + execute(&mut s, "layer.flatten", &json!({"layer": folder})).unwrap(); + + assert!(largest_difference(&composite(&mut s), &before) < TOLERANCE, "{mode}: {}", largest_difference(&composite(&mut s), &before)); + } +} + +/// A hidden folder shows nothing, so the layer it flattens into is hidden too +#[test] +fn a_hidden_folder_flattens_into_a_hidden_layer() { + let mut s = session(); + id(execute(&mut s, "layer.add.fill", &json!({"color": [0.0, 0.0, 1.0]})).unwrap()); + let folder = id(execute(&mut s, "layer.add.group", &Value::Null).unwrap()); + id(execute(&mut s, "layer.add.fill", &json!({"color": [1.0, 0.2, 0.0]})).unwrap()); + execute(&mut s, "layer.visible", &json!({"layer": folder, "visible": false})).unwrap(); + let before = composite(&mut s); + + let flat = id(execute(&mut s, "layer.flatten", &json!({"layer": folder})).unwrap()); + + assert!(!s.doc.project.layer(LayerId(flat)).unwrap().visible, "the new layer is hidden like the folder"); + assert!(largest_difference(&composite(&mut s), &before) < TOLERANCE, "the image does not change"); +} diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 1d4588ea..be585658 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -528,6 +528,8 @@ pub fn shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context) { crate::import_resources::keys_in(app, input, nothing_focused, popup_open); } else if !popup_open && app.picking_color.is_some() && input.consume_key(Modifiers::NONE, Key::Escape) { app.picking_color = None; + } else if escape_is_free && app.layer_pick_list.is_some() && input.consume_key(Modifiers::NONE, Key::Escape) { + app.layer_pick_list = None; } else if escape_is_free && app.properties_popup.is_some() && input.consume_key(Modifiers::NONE, Key::Escape) { app.properties_popup = None; } else if escape_is_free && crate::rail_popup::is_visible(app) && input.consume_key(Modifiers::NONE, Key::Escape) { diff --git a/crates/ui-egui/tests/it/layers_panel.rs b/crates/ui-egui/tests/it/layers_panel.rs index a2dc1368..38c865c4 100644 --- a/crates/ui-egui/tests/it/layers_panel.rs +++ b/crates/ui-egui/tests/it/layers_panel.rs @@ -543,7 +543,7 @@ fn properties_top(h: &Harness<'_, TextureCraftApp>) -> f32 { h.query_all(by().role(egui::Role::Tab).label_contains("Properties")).next().map(|tab| tab.rect().top()).unwrap_or(f32::MAX) } -/// Holding R and clicking the model lists the layers laying something down there instead of painting with the current tool; a row of the list, clicked or reached with the arrow keys and Enter, selects its layer +/// Holding R and clicking the model lists the layers laying something down there instead of painting with the current tool; a row of the list, clicked or reached with the arrow keys and Enter, selects its layer, and Escape closes it #[test] fn r_and_a_click_on_the_model_list_the_layers_there_and_a_row_selects_one_without_painting() { let (mut h, _) = harness(); @@ -585,4 +585,12 @@ fn r_and_a_click_on_the_model_list_the_layers_there_and_a_row_selects_one_withou assert_eq!(h.state().session.selected_layer(), Some(upper), "the clicked row's layer is selected"); assert_eq!(h.state().session.history_labels().len(), steps, "no stroke was painted"); + + // The first Escape moves egui's focus off the top row: the list must close on it all the same + open_list(&mut h); + h.key_press(egui::Key::Escape); + h.run_steps(SETTLE_FRAMES); + + assert!(h.state().layer_pick_list.is_none(), "Escape closes the list"); + assert_eq!(h.state().session.selected_layer(), Some(upper), "and selects nothing"); }