Tool availability: which tools can act on the selection

Session::tool_usable answers per tool: brushes and Path act on paint layer
content, paint sublayers, masks and the quick mask while it is edited; fill
content, filters, generators, folders, instances and an empty selection take
none of them. Smudge and Clone can't paint mask values yet. Path also edits a
selected path sublayer, wherever it is. The color picker always acts; the
geometry mask needs a layer.
This commit is contained in:
Rebornim committed 2026-11-11 06:35:41 +00:00
1 parent e52393a869
commit 77d5edbce6
4 files changed
+298

No files matched your search

+1
View File
@@ -55,6 +55,7 @@ pub mod smart_masks;
pub mod template_cmds;
pub mod text_cmds;
pub mod thumbs;
mod tool_availability;
pub mod tool_cmds;
mod triangle_partition;
mod uv_stroke_state;
+46
View File
@@ -0,0 +1,46 @@
//! Which tools can act on the current selection: the tool shelf and its shortcuts grey the rest
use texturecraft_core::{EffectKind, LayerKind};
use crate::{Session, StackTarget, Tool};
impl Session {
/// Whether `tool` can act on the selection (or on the quick mask while it is being edited)
pub fn tool_usable(&self, tool: Tool) -> bool {
match tool {
// The picker reads the composite under the cursor, whatever is selected
Tool::ColorPicker => return true,
Tool::GeometryMask => return self.selected_layer().is_some(),
_ => {}
}
// Brushes paint the quick mask whatever is selected; paths can't edit it
if self.quick_mask.editing {
return tool != Tool::Path;
}
let project = &self.doc.project;
let Some(layer) = self.selected_layer().and_then(|id| project.layers.get(&id)) else { return false };
// Smudge and Clone read layer content, so they can't yet paint mask values
let reads_content = matches!(tool, Tool::Smudge | Tool::Clone);
match self.target {
StackTarget::Content => matches!(layer.kind, LayerKind::Paint),
StackTarget::Mask => !reads_content,
StackTarget::Effect(effect) => {
let Some((owner, found)) = project.effect(effect) else { return false };
let Some(owner_layer) = project.layers.get(&owner) else { return false };
let in_content = owner_layer.content_effects.iter().any(|candidate| candidate.id == effect);
let in_fill_content = in_content && matches!(owner_layer.kind, LayerKind::Fill(_));
match found.kind {
// Paths saved in a fill layer before paths there were removed stay editable
EffectKind::Path(_) => tool == Tool::Path,
EffectKind::Paint if tool == Tool::Path => !in_fill_content,
EffectKind::Paint => in_content || !reads_content,
_ => false,
}
}
}
}
}
+1
View File
@@ -24,6 +24,7 @@ mod polygon_fill;
mod project_recovery;
mod sample;
mod thumbs;
mod tool_availability;
mod tools;
mod udim;
mod uv_strokes;
+250
View File
@@ -0,0 +1,250 @@
//! Which tools can act on each selection
use serde_json::{Value, json};
use texturecraft_engine::core::LayerId;
use texturecraft_engine::geom::glam::{Vec2, Vec3};
use texturecraft_engine::{Session, StackTarget, Tool, execute};
/// Two triangles side by side, each a mesh part with its own corner of the UV square
const PARTS_OBJ: &str = "o Lid\nv 0 0 0\nv 2 0 0\nv 0 2 0\nvt 0 0\nvt 0.5 0\nvt 0 0.5\nf 1/1 2/2 3/3\n\
o Body\nv 2 2 0\nv 4 2 0\nv 2 4 0\nvt 0.5 0.5\nvt 1 0.5\nvt 0.5 1\nf 4/4 5/5 6/6\n";
const VIEWPORT_PIXELS: f32 = 400.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Selection {
PaintContent,
FillContent,
PaintMask,
FillMask,
FolderMask,
InstanceOwnMask,
PaintSublayer,
PaintSublayerInMask,
PaintSublayerInFill,
PathSublayer,
FilterInPaint,
FilterInFill,
FilterInMask,
FillEffectInPaint,
GeneratorInFillMask,
FolderContent,
InstanceOfPaint,
InstanceOfFill,
Nothing,
QuickMaskOverFill,
QuickMaskOverNothing,
}
impl Selection {
const ALL: [Selection; 21] = [
Selection::PaintContent,
Selection::FillContent,
Selection::PaintMask,
Selection::FillMask,
Selection::FolderMask,
Selection::InstanceOwnMask,
Selection::PaintSublayer,
Selection::PaintSublayerInMask,
Selection::PaintSublayerInFill,
Selection::PathSublayer,
Selection::FilterInPaint,
Selection::FilterInFill,
Selection::FilterInMask,
Selection::FillEffectInPaint,
Selection::GeneratorInFillMask,
Selection::FolderContent,
Selection::InstanceOfPaint,
Selection::InstanceOfFill,
Selection::Nothing,
Selection::QuickMaskOverFill,
Selection::QuickMaskOverNothing,
];
/// Whether the shelf offers `tool` for this selection
fn offers(self, tool: Tool) -> bool {
let reads_content = matches!(tool, Tool::Smudge | Tool::Clone);
match tool {
Tool::ColorPicker => true,
Tool::GeometryMask => !matches!(self, Selection::Nothing | Selection::QuickMaskOverNothing),
_ => match self {
Selection::PaintContent | Selection::PaintSublayer => true,
Selection::PaintSublayerInFill => tool != Tool::Path,
Selection::PaintMask | Selection::FillMask | Selection::FolderMask | Selection::InstanceOwnMask | Selection::PaintSublayerInMask => {
!reads_content
}
Selection::PathSublayer => tool == Tool::Path,
Selection::QuickMaskOverFill | Selection::QuickMaskOverNothing => tool != Tool::Path,
_ => false,
},
}
}
}
fn stencil_png() -> Vec<u8> {
let mut out = Vec::new();
let green = image::RgbaImage::from_fn(8, 8, |_, _| image::Rgba([0, 255, 0, 255]));
green.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
out
}
fn base_session() -> Session {
let mut session = Session::from_mesh_bytes("parts.obj", PARTS_OBJ.as_bytes(), 256, "parts").unwrap();
session.viewport = Vec2::splat(VIEWPORT_PIXELS);
(session.camera.yaw, session.camera.pitch) = (0.0, 0.0);
let (low, high) = session.mesh.bounds();
session.camera.frame(low, high);
session.update();
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
execute(&mut session, "tool.brush", &json!({"size": 6})).unwrap();
let stencil = session.import_resource_bytes("stencil.png", stencil_png()).unwrap();
execute(&mut session, "tool.stencil", &json!({"image": stencil})).unwrap();
session
}
fn screen(session: &Session, point: [f32; 3]) -> Vec2 {
let ndc = session.camera.project(Vec3::from(point), 1.0).unwrap();
Vec2::new((ndc.x + 1.0) * VIEWPORT_PIXELS / 2.0, (1.0 - ndc.y) * VIEWPORT_PIXELS / 2.0)
}
fn add_layer(session: &mut Session, command: &str) -> u64 {
execute(session, command, &json!({})).unwrap()["layer"].as_u64().unwrap()
}
fn select(session: &mut Session, layer: u64, target: Value) {
execute(session, "layer.select", &json!({"layer": layer, "target": target})).unwrap();
}
fn select_nothing(session: &mut Session) {
let set = session.ts();
session.selected.remove(&set);
session.target = StackTarget::Content;
}
/// Adds an effect with `command` to the layer's content or mask and selects it
fn select_new_effect(session: &mut Session, layer: u64, mask: bool, command: &str, params: Value) {
if mask {
execute(session, "mask.add.black", &json!({"layer": layer})).unwrap();
}
let target = if mask { "mask" } else { "content" };
let mut params = params;
params["layer"] = json!(layer);
params["target"] = json!(target);
execute(session, command, &params).unwrap();
let owner = session.doc.project.layer(LayerId(layer)).unwrap();
let effects = if mask { &owner.mask.as_ref().unwrap().effects } else { &owner.content_effects };
let effect = effects.last().unwrap().id.0;
select(session, layer, json!(effect));
}
fn select_new_mask(session: &mut Session, layer: u64) {
execute(session, "mask.add.black", &json!({"layer": layer})).unwrap();
select(session, layer, json!("mask"));
}
fn session_with(selection: Selection) -> Session {
let mut session = base_session();
match selection {
Selection::PaintContent => {
let layer = add_layer(&mut session, "layer.add.paint");
select(&mut session, layer, json!("content"));
}
Selection::FillContent => {
let layer = add_layer(&mut session, "layer.add.fill");
select(&mut session, layer, json!("content"));
}
Selection::PaintMask => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_mask(&mut session, layer);
}
Selection::FillMask => {
let layer = add_layer(&mut session, "layer.add.fill");
select_new_mask(&mut session, layer);
}
Selection::FolderMask => {
let folder = add_layer(&mut session, "layer.add.group");
select_new_mask(&mut session, folder);
}
Selection::InstanceOwnMask => {
let source = add_layer(&mut session, "layer.add.paint");
let instance = execute(&mut session, "layer.instance", &json!({"layer": source})).unwrap()["layer"].as_u64().unwrap();
select_new_mask(&mut session, instance);
}
Selection::PaintSublayer => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_effect(&mut session, layer, false, "effect.add.paint", json!({}));
}
Selection::PaintSublayerInMask => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_effect(&mut session, layer, true, "effect.add.paint", json!({}));
}
Selection::PaintSublayerInFill => {
let layer = add_layer(&mut session, "layer.add.fill");
select_new_effect(&mut session, layer, false, "effect.add.paint", json!({}));
}
Selection::PathSublayer => {
let layer = add_layer(&mut session, "layer.add.paint");
select(&mut session, layer, json!("content"));
let (start, end) = (screen(&session, [0.2, 0.2, 0.0]), screen(&session, [1.0, 0.4, 0.0]));
execute(&mut session, "tool.path", &json!({"points": [[start.x, start.y], [end.x, end.y]]})).unwrap();
execute(&mut session, "tool.path", &json!({"commit": true})).unwrap();
let path = session.doc.project.layer(LayerId(layer)).unwrap().content_effects.last().unwrap().id.0;
select(&mut session, layer, json!(path));
}
Selection::FilterInPaint | Selection::FilterInFill | Selection::FilterInMask => {
let command = if selection == Selection::FilterInFill { "layer.add.fill" } else { "layer.add.paint" };
let layer = add_layer(&mut session, command);
select_new_effect(&mut session, layer, selection == Selection::FilterInMask, "effect.add.filter", json!({"filter": "invert"}));
}
Selection::FillEffectInPaint => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_effect(&mut session, layer, false, "effect.add.fill", json!({}));
}
Selection::GeneratorInFillMask => {
let layer = add_layer(&mut session, "layer.add.fill");
select_new_effect(&mut session, layer, true, "effect.add.generator", json!({}));
}
Selection::FolderContent => {
let folder = add_layer(&mut session, "layer.add.group");
select(&mut session, folder, json!("content"));
}
Selection::InstanceOfPaint | Selection::InstanceOfFill => {
let command = if selection == Selection::InstanceOfFill { "layer.add.fill" } else { "layer.add.paint" };
let source = add_layer(&mut session, command);
let instance = execute(&mut session, "layer.instance", &json!({"layer": source})).unwrap()["layer"].as_u64().unwrap();
select(&mut session, instance, json!("content"));
}
Selection::Nothing => select_nothing(&mut session),
Selection::QuickMaskOverFill => {
let layer = add_layer(&mut session, "layer.add.fill");
select(&mut session, layer, json!("content"));
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
}
Selection::QuickMaskOverNothing => {
select_nothing(&mut session);
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
}
}
session
}
#[test]
fn every_selection_offers_the_tools_that_can_act_on_it() {
let mut wrong = Vec::new();
for selection in Selection::ALL {
let session = session_with(selection);
for tool in Tool::ALL {
let usable = session.tool_usable(tool);
if usable != selection.offers(tool) {
wrong.push(format!("{selection:?} {tool:?}: usable {usable}"));
}
}
}
assert!(wrong.is_empty(), "{wrong:#?}");
}