mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
* Right-click opens the Brush Preset picker (or erases), eraser-on-Background tests, README comma - #75: the right mouse button never paints with the tool. With a painting tool it opens the Brush Preset picker at the pointer (Photoshop). New preference Preferences › Tools › Right-click with painting tools (tools.rightClickWithPaintingTools: brushPicker | erase, serde default brushPicker): with erase, a right drag with the Brush erases with the current brush (one paint.stroke, one undo step, pen pressure/tilt as usual). ui.pointer accepts "button": "secondary". New module ui-egui/src/paint_mouse.rs; picker body shared with the options-bar chip. - #76: the engine already paints the background colour when erasing a transparency-locked layer (the Background); added tests at 8/16/32-bit in RGB/CMYK/Gray, with a selection, on a normal layer (erases to transparency), on a transparency-locked layer, and through the UI. - paint.stroke/pencil refuse coordinates beyond ±1e6 (an f64::MAX point ran out of memory). - #73: test that the canvas hands the live stroke over to the committed document with no frame lacking the stroke, refreshing only the stroke's rect. - #69: README comma. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Brush smoothing follows the options bar; the live stroke draws the catch-up tail (#73) - The options-bar Smoothing % (pro and classic bars, Brush and Eraser) edits the session brush's smoothing, which the live stroke and the committed paint.stroke both use; the canvas no longer forces 0.3. The session brush starts at Photoshop's 10 %. - With smoothing the brush lags the pointer and catches up when the stroke ends. LiveStroke now draws that catch-up tail on every step (StrokeRenderer::tail_preview renders only the tiles the tail touches; the previous step's tail is restored first), so the preview at the last move equals the commit and nothing pops in on release. At 0 % there is no lag. - Path › Stroke Path ignores smoothing (it strokes the exact geometry). - Tests: paint tail_preview = finished stroke; canvas: 0 % preview = commit, 50 % preview = commit with the end reaching the pointer, smoothing changes the stroke, commit equals paint.stroke with that smoothing, no stale tails after sharp turns. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Smoothing is a per-tool option that brush presets keep - Picking a brush preset (tools.setBrush / paint.* "preset", .abr import with select) keeps the current smoothing amount and mode instead of the preset's (BrushSettings::picked_over), as Photoshop's options bar does. An explicit smoothing alongside the preset still applies. - The Brush and the Eraser each remember their smoothing (UiState smoothing_tool / tool_smoothing, synced on tool switch before the options bar, the canvas and each stroke); a tool seen for the first time starts at 10 %. - Tests: engine presets keep 10 % / a user value with pulled-string mode; UI per-tool smoothing across Brush/Eraser/other tools and on the first stroke after a switch. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
452 lines
14 KiB
Rust
452 lines
14 KiB
Rust
//! The brush model: tip shape plus Photoshop-style dynamics sections, all plain serde data.
|
||
//!
|
||
//! Every section is optional (an `enabled` flag, or a neutral default), so a partial JSON object
|
||
//! such as `{"size": 40, "scattering": {"enabled": true, "scatter": {"jitter": 2.0}}}` deserialises
|
||
//! onto the defaults. Field names are camelCase in JSON, matching the command parameters.
|
||
//!
|
||
//! Units: sizes are pixels, angles are degrees, every percentage is a 0..1 fraction (scatter and
|
||
//! texture scale may exceed 1, like Photoshop's 1000 % scatter).
|
||
|
||
use photocraft_color::BlendMode;
|
||
use serde::{Deserialize, Serialize};
|
||
|
||
use crate::tile::GrayTile;
|
||
|
||
/// What drives a dynamic parameter (Photoshop's "Control" pop-ups).
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub enum Control {
|
||
#[default]
|
||
Off,
|
||
/// Ramp over `fadeSteps` spacing steps (from full to the minimum).
|
||
Fade,
|
||
PenPressure,
|
||
PenTilt,
|
||
StylusWheel,
|
||
/// Barrel rotation of the stylus.
|
||
Rotation,
|
||
/// Angle only: the direction of the first stroke segment.
|
||
InitialDirection,
|
||
/// Angle only: the current stroke direction.
|
||
Direction,
|
||
}
|
||
|
||
/// One dynamic parameter: random jitter plus a controller with a floor.
|
||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct Dynamic {
|
||
/// 0..1 random variation per dab (for scatter: the amount, may exceed 1).
|
||
pub jitter: f32,
|
||
pub control: Control,
|
||
/// Steps for [`Control::Fade`].
|
||
pub fade_steps: u32,
|
||
/// 0..1 floor the control/jitter may reduce the value to (minimum diameter, roundness, depth…).
|
||
pub minimum: f32,
|
||
}
|
||
|
||
impl Default for Dynamic {
|
||
fn default() -> Self {
|
||
Self { jitter: 0.0, control: Control::Off, fade_steps: 25, minimum: 0.0 }
|
||
}
|
||
}
|
||
|
||
impl Dynamic {
|
||
pub const fn controlled(control: Control) -> Self {
|
||
Self { jitter: 0.0, control, fade_steps: 25, minimum: 0.0 }
|
||
}
|
||
pub const fn jitter(jitter: f32) -> Self {
|
||
Self { jitter, control: Control::Off, fade_steps: 25, minimum: 0.0 }
|
||
}
|
||
pub fn is_active(&self) -> bool {
|
||
self.jitter > 0.0 || self.control != Control::Off
|
||
}
|
||
}
|
||
|
||
/// The brush tip bitmap source.
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub enum TipShape {
|
||
/// Computed round/elliptical tip (hardness, angle, roundness).
|
||
#[default]
|
||
Round,
|
||
/// Sampled tip: grayscale bitmap where 1 = full paint. Scaled so its larger side equals `size`.
|
||
Sampled(GrayTile),
|
||
}
|
||
|
||
/// Shape Dynamics.
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct ShapeDynamics {
|
||
pub enabled: bool,
|
||
/// Size jitter / control; `minimum` is Photoshop's Minimum Diameter.
|
||
pub size: Dynamic,
|
||
/// Angle jitter (fraction of ±180°) / control.
|
||
pub angle: Dynamic,
|
||
/// Roundness jitter / control; `minimum` is Minimum Roundness.
|
||
pub roundness: Dynamic,
|
||
pub flip_x_jitter: bool,
|
||
pub flip_y_jitter: bool,
|
||
}
|
||
|
||
/// Scattering.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct Scattering {
|
||
pub enabled: bool,
|
||
/// `jitter` = scatter amount (1.0 = 100 %: up to one radius of displacement); control scales it.
|
||
pub scatter: Dynamic,
|
||
/// Scatter radially instead of only perpendicular to the stroke.
|
||
pub both_axes: bool,
|
||
/// Dabs per spacing step (1..16).
|
||
pub count: u32,
|
||
/// Count jitter / control (reduces the count towards 1).
|
||
pub count_jitter: Dynamic,
|
||
}
|
||
|
||
impl Default for Scattering {
|
||
fn default() -> Self {
|
||
Self { enabled: false, scatter: Dynamic::default(), both_axes: false, count: 1, count_jitter: Dynamic::default() }
|
||
}
|
||
}
|
||
|
||
/// How a grayscale mask (texture or dual tip) combines with the brush coverage.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub enum MaskMode {
|
||
#[default]
|
||
Multiply,
|
||
Subtract,
|
||
Darken,
|
||
Overlay,
|
||
ColorDodge,
|
||
ColorBurn,
|
||
LinearBurn,
|
||
HardMix,
|
||
LinearHeight,
|
||
Height,
|
||
}
|
||
|
||
/// Procedural pattern styles (generated deterministically; no bundled pattern files).
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub enum PatternStyle {
|
||
/// Smooth value noise.
|
||
#[default]
|
||
Noise,
|
||
/// Woven canvas.
|
||
Canvas,
|
||
/// Fibrous paper tooth.
|
||
Paper,
|
||
/// Halftone-like dots.
|
||
Dots,
|
||
}
|
||
|
||
/// A texture pattern: a procedural generator or an explicit grayscale tile (tiled infinitely).
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub enum Pattern {
|
||
Procedural { style: PatternStyle, size: u32, seed: u32 },
|
||
Tile(GrayTile),
|
||
}
|
||
|
||
impl Default for Pattern {
|
||
fn default() -> Self {
|
||
Pattern::Procedural { style: PatternStyle::Paper, size: 128, seed: 1 }
|
||
}
|
||
}
|
||
|
||
/// Texture.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct Texture {
|
||
pub enabled: bool,
|
||
pub pattern: Pattern,
|
||
pub invert: bool,
|
||
/// Pattern scale (1 = 100 %).
|
||
pub scale: f32,
|
||
/// -1..1 (Photoshop's -150..150 mapped to ±1).
|
||
pub brightness: f32,
|
||
/// -1..1.
|
||
pub contrast: f32,
|
||
/// Apply the texture to every dab (enables depth jitter) instead of once to the stroke.
|
||
pub each_tip: bool,
|
||
pub mode: MaskMode,
|
||
/// 0..1.
|
||
pub depth: f32,
|
||
/// Depth jitter / control (per tip only); `minimum` is Minimum Depth.
|
||
pub depth_jitter: Dynamic,
|
||
}
|
||
|
||
impl Default for Texture {
|
||
fn default() -> Self {
|
||
Self {
|
||
enabled: false,
|
||
pattern: Pattern::default(),
|
||
invert: false,
|
||
scale: 1.0,
|
||
brightness: 0.0,
|
||
contrast: 0.0,
|
||
each_tip: false,
|
||
mode: MaskMode::Multiply,
|
||
depth: 1.0,
|
||
depth_jitter: Dynamic::default(),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Dual Brush: a second tip stroked along the same path; the stroke is masked by it.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct DualBrush {
|
||
pub enabled: bool,
|
||
pub mode: MaskMode,
|
||
pub tip: TipShape,
|
||
pub size: f32,
|
||
pub hardness: f32,
|
||
pub roundness: f32,
|
||
pub angle: f32,
|
||
pub spacing: f32,
|
||
pub scatter: f32,
|
||
pub both_axes: bool,
|
||
pub count: u32,
|
||
pub flip: bool,
|
||
}
|
||
|
||
impl Default for DualBrush {
|
||
fn default() -> Self {
|
||
Self {
|
||
enabled: false,
|
||
mode: MaskMode::Multiply,
|
||
tip: TipShape::Round,
|
||
size: 20.0,
|
||
hardness: 1.0,
|
||
roundness: 1.0,
|
||
angle: 0.0,
|
||
spacing: 0.25,
|
||
scatter: 0.0,
|
||
both_axes: false,
|
||
count: 1,
|
||
flip: false,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Color Dynamics.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct ColorDynamics {
|
||
pub enabled: bool,
|
||
/// Vary every dab (true) or once per stroke (false).
|
||
pub per_tip: bool,
|
||
/// Foreground/background mix jitter / control.
|
||
pub fg_bg: Dynamic,
|
||
/// 0..1 fraction of ±half the hue wheel.
|
||
pub hue_jitter: f32,
|
||
pub saturation_jitter: f32,
|
||
pub brightness_jitter: f32,
|
||
/// -1 (fully desaturated) .. 1 (fully saturated).
|
||
pub purity: f32,
|
||
}
|
||
|
||
impl Default for ColorDynamics {
|
||
fn default() -> Self {
|
||
Self { enabled: false, per_tip: true, fg_bg: Dynamic::default(), hue_jitter: 0.0, saturation_jitter: 0.0, brightness_jitter: 0.0, purity: 0.0 }
|
||
}
|
||
}
|
||
|
||
/// Transfer: per-dab opacity ceiling and flow.
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct Transfer {
|
||
pub enabled: bool,
|
||
pub opacity: Dynamic,
|
||
pub flow: Dynamic,
|
||
}
|
||
|
||
/// Brush Pose: stylus values used instead of (override) or for missing device data.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct Pose {
|
||
pub enabled: bool,
|
||
/// Degrees, -90..90 (W3C Pointer Events convention).
|
||
pub tilt_x: f32,
|
||
pub tilt_y: f32,
|
||
/// Degrees 0..360.
|
||
pub rotation: f32,
|
||
/// 0..1.
|
||
pub pressure: f32,
|
||
pub override_tilt: bool,
|
||
pub override_rotation: bool,
|
||
pub override_pressure: bool,
|
||
}
|
||
|
||
impl Default for Pose {
|
||
fn default() -> Self {
|
||
Self {
|
||
enabled: false,
|
||
tilt_x: 0.0,
|
||
tilt_y: 0.0,
|
||
rotation: 0.0,
|
||
pressure: 1.0,
|
||
override_tilt: false,
|
||
override_rotation: false,
|
||
override_pressure: false,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Stroke smoothing (Photoshop's Smoothing % and its options).
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct Smoothing {
|
||
/// 0..1.
|
||
pub amount: f32,
|
||
/// Lazy-mouse: paint only when the pointer pulls a string of `amount × 100 px` taut.
|
||
pub pulled_string: bool,
|
||
/// Keep catching up with the pointer while it pauses (uses point timestamps).
|
||
pub catch_up: bool,
|
||
/// Finish the stroke at the last pointer position.
|
||
pub catch_up_on_end: bool,
|
||
/// Divide the string length by the view zoom (the command's `zoom` param).
|
||
pub adjust_for_zoom: bool,
|
||
}
|
||
|
||
impl Default for Smoothing {
|
||
fn default() -> Self {
|
||
Self { amount: 0.0, pulled_string: false, catch_up: true, catch_up_on_end: true, adjust_for_zoom: true }
|
||
}
|
||
}
|
||
|
||
/// A complete brush: tip shape, dynamics, and stroke-level options.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
#[serde(default, rename_all = "camelCase")]
|
||
pub struct BrushSettings {
|
||
/// Diameter in pixels.
|
||
pub size: f32,
|
||
/// 0 (soft) ..1 (hard). Computed tips only.
|
||
pub hardness: f32,
|
||
/// Spacing between dabs as a fraction of the diameter.
|
||
pub spacing: f32,
|
||
/// Maximum coverage for the whole stroke.
|
||
pub opacity: f32,
|
||
/// Per-dab coverage.
|
||
pub flow: f32,
|
||
/// Shortcut for Shape Dynamics › Size › Pen Pressure (the options-bar button).
|
||
#[serde(alias = "pressure_size")]
|
||
pub pressure_size: bool,
|
||
/// Shortcut for Transfer › Opacity › Pen Pressure.
|
||
#[serde(alias = "pressure_opacity")]
|
||
pub pressure_opacity: bool,
|
||
/// Straight RGBA colour (display RGB). Commands fill it from the foreground colour.
|
||
pub color: [f32; 4],
|
||
/// Background colour for Color Dynamics' foreground/background jitter.
|
||
pub background: [f32; 4],
|
||
/// Erase instead of paint.
|
||
pub erase: bool,
|
||
/// Brush blend mode (Normal, Multiply, Dissolve…).
|
||
pub mode: BlendMode,
|
||
// --- Brush Tip Shape ---
|
||
pub tip: TipShape,
|
||
/// Degrees, counter-clockwise.
|
||
pub angle: f32,
|
||
/// 0..1 (1 = circle).
|
||
pub roundness: f32,
|
||
pub flip_x: bool,
|
||
pub flip_y: bool,
|
||
/// Pencil: no anti-aliasing (coverage is 0 or 1).
|
||
pub aliased: bool,
|
||
// --- dynamics sections ---
|
||
pub shape_dynamics: ShapeDynamics,
|
||
pub scattering: Scattering,
|
||
pub texture: Texture,
|
||
pub dual_brush: DualBrush,
|
||
pub color_dynamics: ColorDynamics,
|
||
pub transfer: Transfer,
|
||
pub pose: Pose,
|
||
/// Add grain to the soft parts of the tip.
|
||
pub noise: bool,
|
||
/// Watercolour look: lighter interior, darker rims; overlapping dabs don't build up.
|
||
pub wet_edges: bool,
|
||
/// Airbrush: keep depositing dabs over time while the pointer lingers.
|
||
pub build_up: bool,
|
||
/// Build-up rate in dabs per second.
|
||
pub build_up_rate: f32,
|
||
pub smoothing: Smoothing,
|
||
/// Keep the current texture (pattern + scale) when switching to another textured preset.
|
||
pub protect_texture: bool,
|
||
/// Random seed for jitters (commands derive it from the stroke when not given), so strokes replay.
|
||
pub seed: u64,
|
||
}
|
||
|
||
impl Default for BrushSettings {
|
||
fn default() -> Self {
|
||
Self {
|
||
size: 20.0,
|
||
hardness: 1.0,
|
||
spacing: 0.1,
|
||
opacity: 1.0,
|
||
flow: 1.0,
|
||
pressure_size: true,
|
||
pressure_opacity: false,
|
||
color: [0.0, 0.0, 0.0, 1.0],
|
||
background: [1.0, 1.0, 1.0, 1.0],
|
||
erase: false,
|
||
mode: BlendMode::Normal,
|
||
tip: TipShape::Round,
|
||
angle: 0.0,
|
||
roundness: 1.0,
|
||
flip_x: false,
|
||
flip_y: false,
|
||
aliased: false,
|
||
shape_dynamics: ShapeDynamics::default(),
|
||
scattering: Scattering::default(),
|
||
texture: Texture::default(),
|
||
dual_brush: DualBrush::default(),
|
||
color_dynamics: ColorDynamics::default(),
|
||
transfer: Transfer::default(),
|
||
pose: Pose::default(),
|
||
noise: false,
|
||
wet_edges: false,
|
||
build_up: false,
|
||
build_up_rate: 20.0,
|
||
smoothing: Smoothing::default(),
|
||
protect_texture: false,
|
||
seed: 0,
|
||
}
|
||
}
|
||
}
|
||
|
||
impl BrushSettings {
|
||
/// This brush (a preset) picked while `current` is the tool's brush: Smoothing is a tool
|
||
/// option, so it stays the tool's (Photoshop), and a protected texture stays too.
|
||
pub fn picked_over(self, current: &BrushSettings) -> Self {
|
||
let mut b = self.with_protected_texture(current);
|
||
b.smoothing = current.smoothing.clone();
|
||
b
|
||
}
|
||
|
||
/// Apply Protect Texture: if `current` protects its texture, a newly chosen brush with a texture
|
||
/// keeps `current`'s pattern and scale.
|
||
pub fn with_protected_texture(mut self, current: &BrushSettings) -> Self {
|
||
if current.protect_texture && current.texture.enabled && self.texture.enabled {
|
||
self.texture.pattern = current.texture.pattern.clone();
|
||
self.texture.scale = current.texture.scale;
|
||
self.protect_texture = true;
|
||
}
|
||
self
|
||
}
|
||
}
|
||
|
||
/// A named brush preset (session tool state; serde round-trips).
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct BrushPreset {
|
||
pub name: String,
|
||
pub brush: BrushSettings,
|
||
/// Shipped with Photocraft (built-ins can be deleted from a session but are regenerated on reset).
|
||
#[serde(default)]
|
||
pub builtin: bool,
|
||
/// Preset group (folder) in the Brushes panel, e.g. "General" or an imported file's name.
|
||
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||
pub group: String,
|
||
}
|