From 45ab0f42d9b26600f710dbf87e6a74aed7b35bfc Mon Sep 17 00:00:00 2001 From: Rebornim Date: Sun, 8 Nov 2026 01:23:12 +0000 Subject: [PATCH] Image drop: clearer decal specs, layer/effect check, menu edge cases layer.add.decal's spec says the other-set selection is for x/y only and documents the maskEffect it returns; layer.decal.set's says image keeps the decal's channel or mask and refuses an effect given with another layer. The drop menu's channel list scrolls past 60% of the window. A waiting drop lets go once its menu goes undrawn (the 3D view hidden), and the click that closes the menu no longer moves a decal being placed. --- crates/engine/src/decal_cmds.rs | 10 ++++- crates/engine/tests/painter_gaps.rs | 3 ++ crates/ui-egui/src/decal_drop.rs | 39 +++++++++++++---- crates/ui-egui/src/viewport.rs | 3 +- crates/ui-egui/tests/shelf_drop.rs | 65 +++++++++++++++++++++++++++-- 5 files changed, 105 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/decal_cmds.rs b/crates/engine/src/decal_cmds.rs index f3ec8b28..bd25c185 100644 --- a/crates/engine/src/decal_cmds.rs +++ b/crates/engine/src/decal_cmds.rs @@ -16,7 +16,7 @@ pub fn specs() -> Vec { "Add decal", [], None, - "{image: resource name, channel?: a channel id (default baseColor) | \"mask\" (a new fill layer masked by the image's gray), x?, y?: viewport pixels (default: the view center; a point on another texture set selects it), viewport?: [w,h] | position?: [x,y,z], normal?: [x,y,z], up?: [x,y,z] (the image's up; default the view's up, or the view direction when that is along the normal), size?: height | [w,h] (world units; default a quarter of the mesh), rotation?: degrees, depth?, name?} — a fill layer projecting the image from a frame placed on the surface; seen from the front, the image reads the right way round", + "{image: resource name, channel?: a channel id (default baseColor) | \"mask\" (a new fill layer masked by the image's gray), x?, y?: viewport pixels (default: the view center; a point on another texture set selects it), viewport?: [w,h] | position?: [x,y,z], normal?: [x,y,z] (on the selected texture set), up?: [x,y,z] (the image's up; default the view's up, or the view direction when that is along the normal), size?: height | [w,h] (world units; default a quarter of the mesh), rotation?: degrees, depth?, name?} → {layer, decal, maskEffect: the mask fill effect holding the image with channel \"mask\", else null} — a fill layer projecting the image from a frame placed on the surface; seen from the front, the image reads the right way round", always, add ), @@ -25,7 +25,7 @@ pub fn specs() -> Vec { "Edit decal", [], None, - "{layer? | effect?: a mask fill effect id (a decal placed as a mask), x?, y?, viewport? | position?, normal? (re-place it), up?: [x,y,z], size?: height | [w,h], rotation?: degrees, offset?: [right, up] (world units from where it was placed), depth?, image?, merge?: true continues the previous Edit decal undo step (drags)}", + "{layer? | effect?: a mask fill effect id (a decal placed as a mask; with `layer`, it must be in that layer), x?, y?, viewport? | position?, normal? (re-place it), up?: [x,y,z], size?: height | [w,h], rotation?: degrees, offset?: [right, up] (world units from where it was placed), depth?, image?: replaces the image in the channel or mask it was placed in, merge?: true continues the previous Edit decal undo step (drags)}", has_layer, set ), @@ -185,6 +185,12 @@ fn set(s: &mut Session, p: &Value) -> Result { Some(effect) => s.doc.project.effect(effect).map(|(owner, _)| owner).ok_or_else(|| bad(cmd, format!("no effect {}", effect.0)))?, None => s.doc.project.content_layer(layer_param(s, cmd, p)?).map(|layer| layer.id).ok_or_else(|| bad(cmd, "no layer"))?, }; + if let (Some(effect), Some(layer)) = (effect, u64p(p, "layer")) + && layer != id.0 + { + return Err(bad(cmd, format!("effect {} is not in layer {layer}", effect.0))); + } + let mut decal = decal_of(s, cmd, id, effect)?; let mesh_diagonal = mesh_size(s); if let Some((center, normal, ts)) = placement(s, cmd, p, true)? { diff --git a/crates/engine/tests/painter_gaps.rs b/crates/engine/tests/painter_gaps.rs index c150de79..66f19612 100644 --- a/crates/engine/tests/painter_gaps.rs +++ b/crates/engine/tests/painter_gaps.rs @@ -408,6 +408,9 @@ fn a_decal_image_fills_a_chosen_channel_or_a_mask() { assert!(red(at(&mut s, Channel::BaseColor, 150.0, 200.0)), "a mask decal moves like a decal layer"); assert!(!red(at(&mut s, Channel::BaseColor, 230.0, 200.0))); assert!(execute(&mut s, "layer.decal.set", &json!({"effect": rough.0, "x": 150, "y": 200})).is_err(), "a layer id is not an effect"); + execute(&mut s, "layer.decal.set", &json!({"effect": effect, "layer": masked.0, "size": 0.4})).expect("an effect with its own layer"); + let mismatched = json!({"effect": effect, "layer": rough.0, "size": 0.4}); + assert!(execute(&mut s, "layer.decal.set", &mismatched).is_err(), "an effect with another layer is refused"); } /// A decal's new image stays in the channel the decal was placed in, read as gray there diff --git a/crates/ui-egui/src/decal_drop.rs b/crates/ui-egui/src/decal_drop.rs index 1dfba5f2..4c2fd6db 100644 --- a/crates/ui-egui/src/decal_drop.rs +++ b/crates/ui-egui/src/decal_drop.rs @@ -9,6 +9,9 @@ use crate::i18n::tr; /// What the menu offers besides the texture set's channels: a new fill layer masked by the image const MASK_CHOICE: &str = "mask"; +/// The most of the window's height the channel list takes before it scrolls +const MENU_MAX_HEIGHT_SHARE: f32 = 0.6; + /// An image dropped on the 3D view, placed as a decal once the menu at the drop point is picked #[derive(Clone)] pub struct DecalDrop { @@ -32,18 +35,33 @@ impl DecalDrop { /// Open the menu for this drop (replacing any other waiting drop): it stays open while the drop waits pub fn open(self, ui: &Ui) { - ui.ctx().data_mut(|data| data.insert_temp(Self::menu_id(), self)); + let frame = ui.ctx().cumulative_frame_nr(); + ui.ctx().data_mut(|data| data.insert_temp(Self::menu_id(), (self, frame))); } /// Whether a drop waits for its menu pick: the views don't paint meanwhile, so the click that dismisses the menu paints nothing pub fn waiting(ctx: &egui::Context) -> bool { - ctx.data(|data| data.get_temp::(Self::menu_id()).is_some()) + Self::current(ctx).is_some() + } + + /// The waiting drop; one whose menu went undrawn for a frame (the 3D view was hidden) is dropped instead of blocking the views + fn current(ctx: &egui::Context) -> Option { + let frame = ctx.cumulative_frame_nr(); + let (drop, menu_frame) = ctx.data(|data| data.get_temp::<(DecalDrop, u64)>(Self::menu_id()))?; + if frame > menu_frame + 1 { + ctx.data_mut(|data| data.remove::<(DecalDrop, u64)>(Self::menu_id())); + return None; + } + + Some(drop) } /// Show the waiting drop's menu over the 3D view `response`; a pick places the decal, a click elsewhere drops nothing pub fn show_menu(app: &mut TextureCraftApp, ui: &Ui, response: &egui::Response) { let id = Self::menu_id(); - let Some(drop) = ui.ctx().data(|data| data.get_temp::(id)) else { return }; + let Some(drop) = Self::current(ui.ctx()) else { return }; + let frame = ui.ctx().cumulative_frame_nr(); + ui.ctx().data_mut(|data| data.insert_temp(id, (drop.clone(), frame))); let channels = app.session.doc.project.texture_set(drop.texture_set).map(|set| set.channel_list()).unwrap_or_default(); let mut picked = None; @@ -58,15 +76,20 @@ impl DecalDrop { picked = Some(MASK_CHOICE); } ui.separator(); - for channel in channels { - if ui.button(tr(channel.label())).clicked() { - picked = Some(channel.id()); + + // Many channels scroll instead of running off the window + let max_height = ui.ctx().content_rect().height() * MENU_MAX_HEIGHT_SHARE; + egui::ScrollArea::vertical().max_height(max_height).show(ui, |ui| { + for channel in channels { + if ui.button(tr(channel.label())).clicked() { + picked = Some(channel.id()); + } } - } + }); }); if picked.is_some() || shown.is_none_or(|menu| menu.response.should_close()) { - ui.ctx().data_mut(|data| data.remove::(id)); + ui.ctx().data_mut(|data| data.remove::<(DecalDrop, u64)>(id)); } let Some(channel) = picked else { return }; diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index ec58ce89..f0229e25 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -542,11 +542,12 @@ fn view_3d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin } } - // Decal placement: a click moves the decal being placed there + // Decal placement: a click moves the decal being placed there, unless it dismisses a waiting image drop's menu let placing = app.placing_decal.filter(|_| app.ui.mode == crate::state::Mode::Painting); if let Some(layer) = placing { ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair); if !alt + && !DecalDrop::waiting(ui.ctx()) && response.clicked_by(PointerButton::Primary) && let Some(pointer) = response.interact_pointer_pos() { diff --git a/crates/ui-egui/tests/shelf_drop.rs b/crates/ui-egui/tests/shelf_drop.rs index 36a89de6..b278a397 100644 --- a/crates/ui-egui/tests/shelf_drop.rs +++ b/crates/ui-egui/tests/shelf_drop.rs @@ -8,6 +8,7 @@ use texturecraft_engine::core::Channel; use texturecraft_engine::geom::glam::Vec2; use texturecraft_ui_egui::TextureCraftApp; use texturecraft_ui_egui::shelf::{Asset, ShelfDrag}; +use texturecraft_ui_egui::state::ViewLayout; /// Distance between the view points searched for a set or the background const SEARCH_STEP: f32 = 8.0; @@ -82,14 +83,20 @@ fn a_material_dropped_on_the_background_changes_nothing() { assert_eq!(harness.state().session.history_labels().len(), history); } -/// The sample app in English with a white image dropped on texture set `position_over` (the background when `None`) -fn drop_image(position_over: Option) -> (Harness<'static, TextureCraftApp>, Pos2) { - let mut harness = sample_app(); - harness.state_mut().ui.prefs.language = "en".into(); +/// Import a small white image and return its resource name +fn import_white_image(harness: &mut Harness<'_, TextureCraftApp>) -> String { let mut png = Vec::new(); image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 255, 255, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); let name = harness.state_mut().session.import_resource_bytes("logo.png", png).unwrap(); harness.run_steps(2); + name +} + +/// The sample app in English with a white image dropped on texture set `position_over` (the background when `None`) +fn drop_image(position_over: Option) -> (Harness<'static, TextureCraftApp>, Pos2) { + let mut harness = sample_app(); + harness.state_mut().ui.prefs.language = "en".into(); + let name = import_white_image(&mut harness); let position = point_over(&harness, position_over); drop_asset(&mut harness, Asset::Texture(name), position); @@ -142,3 +149,53 @@ fn an_image_drop_places_nothing_until_a_pick_and_nothing_on_the_background() { let (harness, position) = drop_image(None); assert!(!menu_open(&harness, position), "the background opens no menu"); } + +#[test] +fn hiding_the_3d_view_lets_go_of_a_waiting_image_drop() { + let (mut harness, position) = drop_image(Some(1)); + assert!(menu_open(&harness, position)); + + // Only the 3D view shows the menu, so a 2D-only layout would otherwise leave the views blocked + harness.state_mut().ui.layout = ViewLayout::Only2D; + harness.run_steps(3); + harness.state_mut().ui.layout = ViewLayout::Split; + harness.run_steps(3); + + assert!(!menu_open(&harness, position), "the drop lapsed while its menu was hidden"); +} + +#[test] +fn the_click_that_closes_the_drop_menu_does_not_move_a_decal_being_placed() { + let (mut harness, position) = drop_image(Some(1)); + let distance = |node: &egui_kittest::Node<'_>| node.rect().center().distance(position); + harness.get_all_by_label("Roughness").min_by(|a, b| distance(a).total_cmp(&distance(b))).unwrap().click(); + harness.run_steps(2); + let decal_layer = harness.state().session.selected_layer().unwrap(); + harness.state_mut().placing_decal = Some(decal_layer); + + let image = import_white_image(&mut harness); + drop_asset(&mut harness, Asset::Texture(image), position); + harness.run_steps(2); + assert!(menu_open(&harness, position)); + let history = harness.state().session.history_labels().len(); + + // A point on the same set, away from the menu + let rect = harness.state().viewport_rect; + let elsewhere = (1..(rect.width() / SEARCH_STEP) as usize) + .map(|step| Pos2::new(rect.max.x - step as f32 * SEARCH_STEP, position.y)) + .find(|point| { + let app = harness.state(); + let local = *point - rect.min; + point.distance(position) > MENU_REACH * 2.0 && app.session.texture_set_at(Vec2::new(local.x, local.y), app.session.viewport) == Some(1) + }) + .unwrap(); + harness.hover_at(elsewhere); + harness.run_steps(1); + harness.event(Event::PointerButton { pos: elsewhere, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + harness.run_steps(1); + harness.event(Event::PointerButton { pos: elsewhere, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + harness.run_steps(2); + + assert!(!menu_open(&harness, position)); + assert_eq!(harness.state().session.history_labels().len(), history, "the decal wasn't moved"); +}