mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 10:03:56 +01:00
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.
This commit is contained in:
1 parent
b41497e876
commit
6d72dbbf94
5 files changed
+94
-16
No files matched your search
+46
-14
@@ -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<Node<'a>> {
|
||||
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<u16>);
|
||||
|
||||
@@ -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<u16>, 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::<f32>() / 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::<f32>() / 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) };
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ fn flatten(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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<Value> {
|
||||
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<Value> {
|
||||
// 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<Channel> = 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);
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in new issue
Block a user