GitCorner

Code

ossartcrafttexturecraft🌐
d84914ef1e5a
//! 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 {
        if !self.project_open {
            return false;
        }

        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 };

        match self.target {
            StackTarget::Content => matches!(layer.kind, LayerKind::Paint),
            StackTarget::Mask => true,
            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 => true,
                    _ => false,
                }
            }
        }
    }
}