mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-26 17:30:45 +01:00
Merge pull request #841 from vangel76/feat/mask-toggle-776
Switch a mask on or off with Shift+click, and leave mask view from the material thumbnail
This commit is contained in:
5 files changed
+211
-41
No files matched your search
@@ -33,7 +33,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
has_layer,
|
||||
add_color_mask
|
||||
),
|
||||
cmd!("mask.toggle", "Toggle mask", ["Layers", "Mask"], None, "{layer?, enabled?}", has_layer, toggle),
|
||||
cmd!("mask.toggle", "Toggle mask", ["Layers", "Mask"], None, "{layer?, enabled?} — defaults to the selected layer", always, toggle),
|
||||
cmd!("mask.invertBackground", "Invert mask background", ["Layers", "Mask"], None, "{layer?}", has_layer, invert_background),
|
||||
cmd!("mask.invert", "Invert mask", ["Layers", "Mask"], None, "{layer?}", has_layer, invert),
|
||||
cmd!("mask.clear", "Clear mask", ["Layers", "Mask"], None, "{layer?}", has_layer, clear),
|
||||
@@ -132,6 +132,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
|
||||
fn with_mask<T>(s: &mut Session, p: &Value, cmd: &str, label: &str, f: impl FnOnce(&mut Session, LayerId) -> Result<T>) -> Result<T> {
|
||||
let id = layer_param(s, cmd, p)?;
|
||||
|
||||
s.edit(label, |s| f(s, id))
|
||||
}
|
||||
|
||||
@@ -174,13 +175,14 @@ fn mask_of(s: &mut Session, id: LayerId) -> Result<&mut Mask> {
|
||||
l.mask.as_mut().ok_or_else(|| EngineError::Other(format!("\"{name}\" has no mask")))
|
||||
}
|
||||
|
||||
fn toggle(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let on = with_mask(s, p, "mask.toggle", "Toggle mask", |s, id| {
|
||||
let m = mask_of(s, id)?;
|
||||
m.enabled = boolp(p, "enabled").unwrap_or(!m.enabled);
|
||||
Ok(m.enabled)
|
||||
fn toggle(session: &mut Session, params: &Value) -> Result<Value> {
|
||||
let enabled = with_mask(session, params, "mask.toggle", "Toggle mask", |session, layer| {
|
||||
let mask = mask_of(session, layer)?;
|
||||
mask.enabled = boolp(params, "enabled").unwrap_or(!mask.enabled);
|
||||
Ok(mask.enabled)
|
||||
})?;
|
||||
Ok(json!({ "enabled": on }))
|
||||
|
||||
Ok(json!({ "enabled": enabled }))
|
||||
}
|
||||
|
||||
fn invert_background(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
//! dialog's Output templates tab list, read, save and remove. On the desktop the user presets
|
||||
//! are mirrored as `<name>.json` files in a library folder (see [`Session::load_export_presets`]).
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_formats::export::{MAX_PRESET_NAME, sanitize_file_stem};
|
||||
use texturecraft_formats::{ExportPreset, builtin_preset, builtin_presets, preset_from_json};
|
||||
@@ -67,13 +69,16 @@ impl UserPresets {
|
||||
|
||||
/// The first available name, adding a numbered suffix and shortening the base to fit.
|
||||
fn unique_name(&self, name: &str, skip: Option<usize>) -> Option<String> {
|
||||
// File keys also cover case-insensitive name matches
|
||||
let file_keys: HashSet<_> = self.list.iter().enumerate().filter(|(index, _)| Some(*index) != skip).map(|(_, preset)| file_key(&preset.name)).collect();
|
||||
|
||||
(1..=MAX_USER_PRESETS + 1)
|
||||
.map(|number| {
|
||||
let suffix = if number == 1 { String::new() } else { format!(" {number}") };
|
||||
let base: String = name.chars().take(MAX_PRESET_NAME.saturating_sub(suffix.len())).collect();
|
||||
format!("{}{suffix}", base.trim_end())
|
||||
})
|
||||
.find(|candidate| !self.taken(candidate, skip))
|
||||
.find(|candidate| !file_keys.contains(&file_key(candidate)) && builtin_preset(candidate).is_none())
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -295,6 +300,21 @@ mod tests {
|
||||
assert_eq!(run(&mut session, "export.preset.get", json!({"name": "Mine"})).unwrap(), replacement);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_saves_skip_presets_with_colliding_file_stems() {
|
||||
let mut session = Session::closed().unwrap();
|
||||
for name in ["My Preset", "My_Preset_2"] {
|
||||
run(&mut session, "export.preset.save", json!({"preset": preset(name)})).unwrap();
|
||||
}
|
||||
|
||||
let saved = run(&mut session, "export.preset.save", json!({"preset": preset("My Preset"), "unique": true})).unwrap();
|
||||
|
||||
assert_eq!(saved["name"], "My Preset 3");
|
||||
for name in ["My Preset", "My_Preset_2", "My Preset 3"] {
|
||||
assert_eq!(session.export_preset(name).unwrap().name, name);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_save_at_capacity_requires_an_explicit_rename() {
|
||||
let mut session = Session::closed().unwrap();
|
||||
|
||||
+32
-12
@@ -454,7 +454,6 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
|
||||
let (rect, row) = ui.allocate_exact_size(vec2(ui.available_width(), row_height), Sense::click_and_drag());
|
||||
row.widget_info(|| egui::WidgetInfo::selected(egui::Role::ListItem, true, highlighted, &layer.name));
|
||||
|
||||
// Background, folder lines, the selected rows' accent edge and the colour tag
|
||||
let painter = ui.painter().clone();
|
||||
|
||||
// The active layer gets the strong tint; the other selected rows the plain selected shade
|
||||
@@ -550,14 +549,21 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
|
||||
if !thumb_box(ui, app, thumbnail, ThumbTarget::Content { layer: layer.id, channel }, !layer.visible) {
|
||||
content_glyph(&painter, &tokens, thumbnail, layer);
|
||||
}
|
||||
|
||||
let content_label = tr("Select the layer content");
|
||||
let content_response = ui.interact(thumbnail, ui.scope_id().with(("content", layer.id.0)), Sense::click()).on_hover_text(content_label);
|
||||
painter.rect_stroke(thumbnail, THUMBNAIL_ROUNDING, target_stroke(&tokens, content_target), egui::StrokeKind::Outside);
|
||||
if content_response.clicked() {
|
||||
let modifiers = ui.input(|input| input.modifiers);
|
||||
click_layer(app, modifiers, layer.id.0, Some("content"));
|
||||
|
||||
if !modifiers.alt && matches!(app.session.view, texturecraft_engine::ViewMode::Mask(_)) {
|
||||
app.run("viewport.view", json!({"mode": "material"}));
|
||||
}
|
||||
}
|
||||
|
||||
thumbnail_context_menu(ui, &content_response, content_label, &layer.name, content_target, |ui| layer_context_menu(app, ui, layer, true));
|
||||
|
||||
x = thumbnail.right() + THUMBNAIL_SPACING;
|
||||
|
||||
// An instance draws its source's content, blending and (unless it has its own) mask
|
||||
@@ -574,9 +580,29 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
|
||||
|
||||
// Mask thumbnail, selectable on its own; or the slot that adds one
|
||||
let mask_rect;
|
||||
if let Some((mask_enabled, mask_background, mask_effect_count)) = mask {
|
||||
if let Some((mut mask_enabled, mask_background, mask_effect_count)) = mask {
|
||||
let mask_target = selected && !targets_content(app);
|
||||
mask_rect = Rect::from_min_size(pos2(x, rect.center().y - thumbnail_size * 0.5), vec2(thumbnail_size, thumbnail_size));
|
||||
let mask_label = tr("Select the mask (Alt+click: show it in the viewport, Shift+click: switch it on or off)");
|
||||
let mask_response = ui.interact(mask_rect, ui.scope_id().with(("mask", layer.id.0)), Sense::click()).on_hover_text(mask_label);
|
||||
if mask_response.clicked() {
|
||||
let modifiers = ui.input(|input| input.modifiers);
|
||||
if modifiers.shift && !modifiers.alt {
|
||||
if app.run("mask.toggle", json!({"layer": layer.id.0})).is_some() {
|
||||
mask_enabled = !mask_enabled;
|
||||
mask_response.request_focus();
|
||||
}
|
||||
} else {
|
||||
app.run("layer.select", json!({"layer": layer.id.0, "target": "mask"}));
|
||||
|
||||
if modifiers.alt {
|
||||
let showing = app.session.view == texturecraft_engine::ViewMode::Mask(layer.id);
|
||||
let view = if showing { json!({"mode": "material"}) } else { json!({"mode": "mask", "layer": layer.id.0}) };
|
||||
app.run("viewport.view", view);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !thumb_box(ui, app, mask_rect, ThumbTarget::Mask { layer: layer.id }, !layer.visible || !mask_enabled) {
|
||||
let white = mask_background == texturecraft_engine::core::MaskBackground::White;
|
||||
painter.rect_filled(mask_rect, THUMBNAIL_ROUNDING, Color32::from_gray(if white { WHITE_MASK_GRAY } else { BLACK_MASK_GRAY }));
|
||||
@@ -594,17 +620,10 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
|
||||
if !mask_enabled {
|
||||
painter.line_segment([mask_rect.left_top(), mask_rect.right_bottom()], Stroke::new(HIDDEN_STRIKE_WIDTH, tokens.danger));
|
||||
}
|
||||
let mask_label = tr("Select the mask (Alt+click: show it in the viewport)");
|
||||
let mask_response = ui.interact(mask_rect, ui.scope_id().with(("mask", layer.id.0)), Sense::click()).on_hover_text(mask_label);
|
||||
if mask_response.clicked() {
|
||||
app.run("layer.select", json!({"layer": layer.id.0, "target": "mask"}));
|
||||
if ui.input(|input| input.modifiers.alt) {
|
||||
let showing = app.session.view == texturecraft_engine::ViewMode::Mask(layer.id);
|
||||
let view = if showing { json!({"mode": "material"}) } else { json!({"mode": "mask", "layer": layer.id.0}) };
|
||||
app.run("viewport.view", view);
|
||||
}
|
||||
}
|
||||
|
||||
thumbnail_context_menu(ui, &mask_response, mask_label, &layer.name, mask_target, |ui| mask_management_menu(app, ui, layer, true));
|
||||
ui.ctx().accesskit_node_builder(mask_response.id, |node| node.set_description(if mask_enabled { tr("Mask on") } else { tr("Mask off") }));
|
||||
|
||||
if app.session.view == texturecraft_engine::ViewMode::Mask(layer.id) {
|
||||
painter.rect_stroke(
|
||||
mask_rect.expand(HIGHLIGHT_OUTSET),
|
||||
@@ -627,6 +646,7 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
|
||||
egui::Popup::menu(&slot).show(|ui| crate::layer_toolbar::mask_slot_menu(app, ui, layer, picker));
|
||||
crate::layer_toolbar::bitmap_picker(app, &row, picker, layer.id);
|
||||
}
|
||||
|
||||
x = mask_rect.right() + THUMBNAIL_GAP;
|
||||
|
||||
// Name (or rename editor) with the quieter subtitle under it
|
||||
|
||||
@@ -179,6 +179,11 @@ pub fn enabled(app: &TextureCraftApp, id: &str) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
// A shortcut has no explicit layer to toggle
|
||||
if id == "mask.toggle" && app.session.selected_layer().is_none() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if is_ui(id) {
|
||||
return app.session.project_open || !ui_needs_project(id);
|
||||
}
|
||||
|
||||
@@ -37,6 +37,9 @@ fn thumb(h: &Harness<'_, TextureCraftApp>, name: &str, mask: bool) -> egui::Pos2
|
||||
egui::pos2(x, row.center().y)
|
||||
}
|
||||
|
||||
/// The accessible name of the mask thumbnail of the layer "Masked fill"
|
||||
const MASK_THUMBNAIL_NAME: &str = "Select the mask (Alt+click: show it in the viewport, Shift+click: switch it on or off) (Masked fill)";
|
||||
|
||||
/// The `label` header that follows the "Masked fill" row (other layers have groups too).
|
||||
fn header(h: &mut Harness<'_, TextureCraftApp>, label: &str) {
|
||||
let top = h.get_by_label("Masked fill").rect().top();
|
||||
@@ -351,22 +354,25 @@ fn thumbnail_menus_apply_actions_to_the_clicked_layer_and_stack() {
|
||||
|
||||
#[test]
|
||||
fn focused_mask_thumbnail_opens_and_dismisses_its_menu_from_the_keyboard() {
|
||||
let (mut h, _) = harness();
|
||||
let thumbnail = "Select the mask (Alt+click: show it in the viewport) (Masked fill)";
|
||||
h.get_by_label(thumbnail).focus();
|
||||
h.run_steps(3);
|
||||
assert!(h.get_by_label(thumbnail).is_focused());
|
||||
let (mut harness, _) = harness();
|
||||
|
||||
h.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::F10);
|
||||
h.run_steps(3);
|
||||
assert!(h.query_by_label("Toggle mask").is_some());
|
||||
h.key_press(egui::Key::ArrowDown);
|
||||
h.run_steps(3);
|
||||
assert!(h.get_by_label("Toggle mask").is_focused());
|
||||
h.key_press(egui::Key::Escape);
|
||||
h.run_steps(3);
|
||||
assert!(h.query_by_label("Toggle mask").is_none());
|
||||
assert!(h.get_by_label(thumbnail).is_focused());
|
||||
harness.get_by_label(MASK_THUMBNAIL_NAME).focus();
|
||||
harness.run_steps(3);
|
||||
assert!(harness.get_by_label(MASK_THUMBNAIL_NAME).is_focused());
|
||||
|
||||
harness.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::F10);
|
||||
harness.run_steps(3);
|
||||
assert!(harness.query_by_label("Toggle mask").is_some());
|
||||
|
||||
harness.key_press(egui::Key::ArrowDown);
|
||||
harness.run_steps(3);
|
||||
assert!(harness.get_by_label("Toggle mask").is_focused());
|
||||
|
||||
harness.key_press(egui::Key::Escape);
|
||||
harness.run_steps(3);
|
||||
|
||||
assert!(harness.query_by_label("Toggle mask").is_none());
|
||||
assert!(harness.get_by_label(MASK_THUMBNAIL_NAME).is_focused());
|
||||
}
|
||||
|
||||
/// Click the eye of the top-level row `name` with `modifiers` held (the eye sits in the first 20 points after the 7-point edge).
|
||||
@@ -425,12 +431,18 @@ fn alt_click_on_the_eye_solos_and_a_plain_click_still_hides() {
|
||||
}
|
||||
|
||||
fn alt_click(h: &mut Harness<'_, TextureCraftApp>, at: egui::Pos2) {
|
||||
h.event(egui::Event::ModifiersChanged(egui::Modifiers::ALT));
|
||||
h.hover_at(at);
|
||||
click_with(h, at, egui::Modifiers::ALT);
|
||||
}
|
||||
|
||||
/// A primary click at `position` with `modifiers` held
|
||||
fn click_with(h: &mut Harness<'_, TextureCraftApp>, position: egui::Pos2, modifiers: egui::Modifiers) {
|
||||
h.event(egui::Event::ModifiersChanged(modifiers));
|
||||
h.hover_at(position);
|
||||
for pressed in [true, false] {
|
||||
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::ALT });
|
||||
h.event(egui::Event::PointerButton { pos: position, button: egui::PointerButton::Primary, pressed, modifiers });
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
h.event(egui::Event::ModifiersChanged(egui::Modifiers::NONE));
|
||||
h.run_steps(3);
|
||||
}
|
||||
@@ -460,11 +472,122 @@ fn alt_click_on_a_mask_thumbnail_shows_that_mask_alone_without_changing_the_docu
|
||||
|
||||
#[test]
|
||||
fn alt_click_on_the_content_thumbnail_leaves_the_view_alone() {
|
||||
let (mut h, _) = harness();
|
||||
let at = thumb(&h, "Masked fill", false);
|
||||
alt_click(&mut h, at);
|
||||
let (mut h, layer) = harness();
|
||||
let content_thumbnail = thumb(&h, "Masked fill", false);
|
||||
let mask_thumbnail = thumb(&h, "Masked fill", true);
|
||||
|
||||
alt_click(&mut h, content_thumbnail);
|
||||
|
||||
assert_eq!(h.state().session.view, texturecraft_engine::ViewMode::Material);
|
||||
|
||||
alt_click(&mut h, mask_thumbnail);
|
||||
|
||||
assert_eq!(h.state().session.view, texturecraft_engine::ViewMode::Mask(layer));
|
||||
assert_eq!(h.state().session.target, StackTarget::Mask);
|
||||
|
||||
alt_click(&mut h, content_thumbnail);
|
||||
|
||||
assert_eq!(h.state().session.view, texturecraft_engine::ViewMode::Mask(layer));
|
||||
assert_eq!(h.state().session.target, StackTarget::Content);
|
||||
}
|
||||
|
||||
/// Whether a bypassed mask's strike is painted across the thumbnail at `position`
|
||||
fn mask_strike_shown(h: &Harness<'_, TextureCraftApp>, position: egui::Pos2) -> bool {
|
||||
let danger = texturecraft_ui_egui::theme::Tokens::get(&h.ctx).danger;
|
||||
shapes(h).iter().any(|shape| matches!(shape, egui::Shape::LineSegment { points, stroke } if stroke.color == danger && egui::Rect::from_two_pos(points[0], points[1]).contains(position)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_click_on_a_mask_thumbnail_switches_the_mask_off_and_on_in_one_undo_step_each() {
|
||||
let (mut h, layer) = harness();
|
||||
let at = thumb(&h, "Masked fill", true);
|
||||
let target = h.state().session.target;
|
||||
let history = h.state().session.history_labels().len();
|
||||
let mask = h.state().session.doc.project.layers[&layer].mask.clone().unwrap();
|
||||
|
||||
assert!(!mask_strike_shown(&h, at));
|
||||
assert_eq!(h.get_by_label(MASK_THUMBNAIL_NAME).accesskit_node().description().as_deref(), Some("Mask on"));
|
||||
|
||||
click_with(&mut h, at, egui::Modifiers::SHIFT);
|
||||
|
||||
let bypassed = h.state().session.doc.project.layers[&layer].mask.as_ref().unwrap();
|
||||
assert!(!bypassed.enabled);
|
||||
assert_eq!(bypassed.effects, mask.effects, "the mask keeps its effects while bypassed");
|
||||
assert_eq!(h.state().session.history_labels().len(), history + 1);
|
||||
assert_eq!(h.state().session.target, target, "Shift+click toggles instead of selecting the mask");
|
||||
assert!(mask_strike_shown(&h, at), "the disabled indicator shows while the mask is bypassed");
|
||||
|
||||
let thumbnail = h.get_by_label(MASK_THUMBNAIL_NAME);
|
||||
assert!(thumbnail.is_focused());
|
||||
assert_eq!(thumbnail.accesskit_node().description().as_deref(), Some("Mask off"));
|
||||
|
||||
click_with(&mut h, at, egui::Modifiers::SHIFT);
|
||||
|
||||
assert_eq!(h.state().session.doc.project.layers[&layer].mask, Some(mask), "toggling back restores the mask as it was");
|
||||
assert_eq!(h.state().session.history_labels().len(), history + 2);
|
||||
assert!(!mask_strike_shown(&h, at));
|
||||
assert_eq!(h.get_by_label(MASK_THUMBNAIL_NAME).accesskit_node().description().as_deref(), Some("Mask on"));
|
||||
|
||||
h.state_mut().session.undo().unwrap();
|
||||
|
||||
assert!(!h.state().session.doc.project.layers[&layer].mask.as_ref().unwrap().enabled, "one undo undoes one toggle");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_click_on_a_mask_thumbnail_works_with_no_layer_selected() {
|
||||
let (mut harness, layer) = harness();
|
||||
let row = harness.get_by_label("Masked fill").rect();
|
||||
let mask_thumbnail = thumb(&harness, "Masked fill", true);
|
||||
let history = harness.state().session.history_labels().len();
|
||||
|
||||
click_with(&mut harness, row.center(), egui::Modifiers::COMMAND);
|
||||
|
||||
assert!(harness.state().session.selected_layers().is_empty());
|
||||
assert!(harness.state().session.doc.project.layers[&layer].mask.as_ref().unwrap().enabled);
|
||||
|
||||
click_with(&mut harness, mask_thumbnail, egui::Modifiers::SHIFT);
|
||||
|
||||
assert!(!harness.state().session.doc.project.layers[&layer].mask.as_ref().unwrap().enabled);
|
||||
assert!(harness.state().session.selected_layers().is_empty());
|
||||
assert_eq!(harness.state().session.history_labels().len(), history + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_mask_toggle_shortcut_requires_a_selected_layer() {
|
||||
let (mut harness, layer) = harness();
|
||||
let row = harness.get_by_label("Masked fill").rect();
|
||||
harness.state_mut().ui.keymap.insert("mask.toggle".into(), "F7".into());
|
||||
|
||||
harness.key_press(egui::Key::F7);
|
||||
harness.run_steps(3);
|
||||
|
||||
assert!(!harness.state().session.doc.project.layers[&layer].mask.as_ref().unwrap().enabled);
|
||||
|
||||
click_with(&mut harness, row.center(), egui::Modifiers::COMMAND);
|
||||
assert!(harness.state().session.selected_layers().is_empty());
|
||||
let toast_count = harness.state().toasts.len();
|
||||
|
||||
harness.key_press(egui::Key::F7);
|
||||
harness.run_steps(3);
|
||||
|
||||
assert_eq!(harness.state().toasts.len(), toast_count, "the shortcut is inactive when it has no target");
|
||||
assert!(!harness.state().session.doc.project.layers[&layer].mask.as_ref().unwrap().enabled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clicking_the_material_thumbnail_leaves_the_mask_view() {
|
||||
let (mut h, layer) = harness();
|
||||
let mask_thumbnail = thumb(&h, "Masked fill", true);
|
||||
let material_thumbnail = thumb(&h, "Masked fill", false);
|
||||
|
||||
alt_click(&mut h, mask_thumbnail);
|
||||
|
||||
assert_eq!(h.state().session.view, texturecraft_engine::ViewMode::Mask(layer));
|
||||
|
||||
click(&mut h, material_thumbnail);
|
||||
|
||||
assert_eq!(h.state().session.view, texturecraft_engine::ViewMode::Material);
|
||||
assert_eq!(h.state().session.target, StackTarget::Content, "the layer content is selected");
|
||||
}
|
||||
|
||||
/// Pointer moves of the opacity cell drag, one per frame
|
||||
|
||||
Reference in new issue
Block a user