mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Brush: Clear on the Background paints as Normal and the Mode reads Normal
Fixes #11897. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
b28b2f665e
commit
be428bf90e
5 files changed
+108
-2
No files matched your search
@@ -0,0 +1,59 @@
|
||||
//! #11897: the Brush's Clear mode on the Background paints as Normal with the foreground colour,
|
||||
//! not the background colour; on a pixel layer Clear still erases, and the Eraser on the
|
||||
//! Background still paints the background colour.
|
||||
|
||||
use crate::Session;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
|
||||
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
|
||||
const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 64, "height": 32, "background": "white"})).unwrap();
|
||||
s.tools.foreground = BLACK;
|
||||
s.tools.background = GREEN;
|
||||
s
|
||||
}
|
||||
|
||||
fn pixel(s: &mut Session, x: i32, y: i32) -> [f32; 4] {
|
||||
let v = s.execute("document.pixel", json!({"x": x, "y": y})).unwrap();
|
||||
let a: Vec<f32> = v.as_array().unwrap().iter().map(|c| c.as_f64().unwrap() as f32).collect();
|
||||
[a[0], a[1], a[2], a[3]]
|
||||
}
|
||||
|
||||
fn close(a: [f32; 4], b: [f32; 4]) -> bool {
|
||||
a.iter().zip(&b).all(|(x, y)| (x - y).abs() <= 0.02)
|
||||
}
|
||||
|
||||
/// A hard stroke across (10..30, 10), as the canvas sends it with Clear chosen.
|
||||
fn clear_stroke() -> Value {
|
||||
json!({"points": [[10, 10], [30, 10]], "size": 8, "hardness": 1.0, "erase": true, "mode": "clear"})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_on_the_background_paints_the_foreground() {
|
||||
let mut s = session();
|
||||
s.execute("paint.stroke", clear_stroke()).unwrap();
|
||||
let p = pixel(&mut s, 20, 10);
|
||||
assert!(close(p, BLACK), "painted {p:?}, not the background colour");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_on_a_pixel_layer_still_erases() {
|
||||
let mut s = session();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
s.execute("edit.fill", json!({"contents": "color", "color": [1.0, 0.0, 0.0, 1.0]})).unwrap();
|
||||
s.execute("paint.stroke", clear_stroke()).unwrap();
|
||||
let p = pixel(&mut s, 20, 10);
|
||||
assert!(close(p, WHITE), "erased down to the white Background: {p:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_eraser_on_the_background_still_paints_the_background_colour() {
|
||||
let mut s = session();
|
||||
s.execute("paint.stroke", json!({"points": [[10, 10], [30, 10]], "size": 8, "hardness": 1.0, "erase": true})).unwrap();
|
||||
let p = pixel(&mut s, 20, 10);
|
||||
assert!(close(p, GREEN), "{p:?}");
|
||||
}
|
||||
@@ -424,10 +424,24 @@ fn with_blend_mode(mut b: BrushSettings, p: &Value) -> BrushSettings {
|
||||
b
|
||||
}
|
||||
|
||||
/// The Brush's Clear on the Background paints as Normal with its own colour, as in Photoshop
|
||||
/// (#11897): the Background has no transparency to clear. Masks, channels, other layers (even
|
||||
/// with locked transparency) and the Pencil keep Clear.
|
||||
fn clear_on_background(s: &Session, p: &Value, mut brush: BrushSettings) -> Result<BrushSettings> {
|
||||
if !clear_mode(p) || is_mask_target(p) || crate::channel_cmds::is_channel_target(p) || crate::color_channels::target(s, p)?.is_some() {
|
||||
return Ok(brush);
|
||||
}
|
||||
let id = layer_id(s, p)?;
|
||||
if s.active().is_some_and(|st| st.doc.is_background(id)) {
|
||||
brush.erase = false;
|
||||
}
|
||||
Ok(brush)
|
||||
}
|
||||
|
||||
/// `paint.stroke`: the Brush (and Eraser) tool.
|
||||
pub fn paint_stroke(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let pts = parse_points(p, "paint.stroke")?;
|
||||
let brush = with_blend_mode(resolve_brush(s, p, "paint.stroke")?, p);
|
||||
let brush = clear_on_background(s, p, with_blend_mode(resolve_brush(s, p, "paint.stroke")?, p))?;
|
||||
let label = if brush.erase && !clear_mode(p) { "Eraser" } else { "Brush Tool" };
|
||||
if flag(p, "fill", false) {
|
||||
return fill_region(s, p, label, brush, pts);
|
||||
@@ -605,7 +619,7 @@ impl LiveStroke {
|
||||
"paint.pencil" => true,
|
||||
other => return Err(bad(other, "live strokes are `paint.stroke` or `paint.pencil`")),
|
||||
};
|
||||
let brush = if pencil { pencil_brush(s, p)? } else { with_blend_mode(resolve_brush(s, p, cmd)?, p) };
|
||||
let brush = if pencil { pencil_brush(s, p)? } else { clear_on_background(s, p, with_blend_mode(resolve_brush(s, p, cmd)?, p))? };
|
||||
let (seed, fg) = (brush.seed, brush.color);
|
||||
let (layer, mut brush, zoom) = stroke_target(s, p, brush)?;
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
|
||||
@@ -355,4 +355,6 @@ mod tests;
|
||||
#[cfg(test)]
|
||||
mod fx_mode_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_clear_background_tests;
|
||||
pub mod command_hints;
|
||||
@@ -1023,3 +1023,24 @@ fn a_right_click_moved_and_released_in_one_frame_still_opens_its_menu() {
|
||||
assert_eq!(h.state().ui.brush_picker, None);
|
||||
assert_eq!(h.state().ui.canvas_tool_menu, None);
|
||||
}
|
||||
|
||||
/// #11897: Clear chosen on a pixel layer, then the Background activated: a Brush stroke there
|
||||
/// paints the foreground, and the Mode reads Normal afterwards.
|
||||
#[test]
|
||||
fn chosen_clear_paints_normally_on_the_background_and_the_mode_reads_normal() {
|
||||
let mut h = harness(None);
|
||||
h.state_mut().ui.tool_options.brush_clear = true;
|
||||
let bg = h.state().session.active().unwrap().doc.background().unwrap().id;
|
||||
h.state_mut().run("layer.select", json!({"layer": bg.0})).unwrap();
|
||||
h.run_steps(2);
|
||||
let (a, b) = drag(&mut h, PointerButton::Primary);
|
||||
assert!(!h.state().ui.tool_options.brush_clear, "the Mode reads Normal");
|
||||
let last = strokes(&h).last().cloned().unwrap();
|
||||
assert!(last.get("mode").is_none() && !last["erase"].as_bool().unwrap(), "{last}");
|
||||
let app = h.state();
|
||||
let v = app.ui.views[0].clone();
|
||||
let d = ((a + (b - a) * 0.5) - app.last_canvas_rect.center()) / v.zoom;
|
||||
let st = app.session.active().unwrap();
|
||||
let px = st.doc.layer(bg).unwrap().surface().unwrap().rgba((d.x + v.center[0]) as i32, (d.y + v.center[1]) as i32);
|
||||
assert!(px[2] > 0.9 && px[0] < 0.1, "the foreground blue, not the background colour: {px:?}");
|
||||
}
|
||||
@@ -360,6 +360,15 @@ fn handle_eyedropper_event(app: &mut PhotocraftApp, tool: Tool, ev: ToolEvent, m
|
||||
}
|
||||
|
||||
/// Tool state machine. Shared by mouse input and automation.
|
||||
/// The Brush's chosen Clear paints as Normal on the Background, and its Mode then reads Normal
|
||||
/// (#11897). A held `~` keeps today's behaviour.
|
||||
fn clear_off_on_background(app: &mut PhotocraftApp, tool: Tool) {
|
||||
let on_background = app.session.active().is_some_and(|st| st.active_layer.is_some_and(|id| st.doc.is_background(id)));
|
||||
if tool == Tool::Brush && app.ui.tool_options.brush_clear && !app.held_erase && on_background && paint_target(app) == serde_json::json!("pixels") {
|
||||
app.ui.tool_options.brush_clear = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) {
|
||||
if consumes_cancelled_vector_event(app, ev) {
|
||||
return;
|
||||
@@ -524,6 +533,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
|
||||
_ => {}
|
||||
}
|
||||
crate::paint_mouse::sync_tool_brush(app);
|
||||
clear_off_on_background(app, tool);
|
||||
// Clear mode (`~` held, or chosen in the options bar) erases with the tool's own brush.
|
||||
let clear = crate::paint_mouse::clears(app, tool);
|
||||
let erase = std::mem::take(&mut app.secondary_erase) || tool == Tool::Eraser || clear;
|
||||
|
||||
Reference in new issue
Block a user