mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 20:16:29 -05:00
119 lines
4.8 KiB
Rust
119 lines
4.8 KiB
Rust
//! #17764: Pencil's starting preset controls size, independently of the bar overrides.
|
|
//! The very-low-pressure minimum footprint belongs to #17765, not this default change.
|
|
|
|
use egui::Modifiers;
|
|
use photocraft_engine::paint::{BrushSettings, Control, Dynamic, ShapeDynamics};
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::PhotocraftApp;
|
|
use crate::canvas::{ToolEvent, tool_event};
|
|
use crate::state::Tool;
|
|
|
|
fn app() -> Box<PhotocraftApp> {
|
|
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
|
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
|
|
app.run("layer.new.layer", json!({})).unwrap();
|
|
app.sync_views();
|
|
app
|
|
}
|
|
|
|
fn invoke(app: &mut PhotocraftApp, id: &str, params: Value) {
|
|
let ctx = app.ctx().clone();
|
|
crate::menus::invoke(app, &ctx, id, params).unwrap();
|
|
}
|
|
|
|
fn assert_defaults(brush: &BrushSettings) {
|
|
assert!(brush.shape_dynamics.enabled);
|
|
assert_eq!(brush.shape_dynamics.size, Dynamic::controlled(Control::PenPressure));
|
|
assert!(!brush.transfer.enabled);
|
|
assert_eq!(brush.transfer.opacity.control, Control::Off);
|
|
assert!(!brush.pressure_size && !brush.pressure_opacity);
|
|
assert_eq!((brush.size, brush.hardness, brush.spacing), (1.0, 1.0, 0.25));
|
|
}
|
|
|
|
#[test]
|
|
fn first_use_and_reset_tool_restore_the_pressure_preset_without_bar_overrides() {
|
|
let mut app = app();
|
|
// The outgoing tool cannot supply the Pencil's dynamics or toggles.
|
|
app.session.tools.brush.pressure_size = true;
|
|
app.session.tools.brush.pressure_opacity = true;
|
|
app.session.tools.brush.transfer.enabled = true;
|
|
app.session.tools.brush.shape_dynamics.size.jitter = 0.8;
|
|
invoke(&mut app, "tools.select.pencil", json!({}));
|
|
assert_defaults(&app.session.tools.brush);
|
|
app.run(
|
|
"tools.setBrush",
|
|
json!({"brush": {
|
|
"shapeDynamics": {"enabled": false, "size": {"control": "off", "jitter": 0.9, "minimum": 0.8}},
|
|
"transfer": {"enabled": true, "opacity": {"control": "penPressure"}},
|
|
"pressureSize": true, "pressureOpacity": true
|
|
}}),
|
|
)
|
|
.unwrap();
|
|
invoke(&mut app, "tool.reset", json!({}));
|
|
assert_defaults(&app.session.tools.brush);
|
|
}
|
|
|
|
#[test]
|
|
fn reset_all_restores_the_saved_pencil_without_changing_other_tools() {
|
|
let mut app = app();
|
|
invoke(&mut app, "tools.select.pencil", json!({}));
|
|
app.session.tools.brush.shape_dynamics.enabled = false;
|
|
invoke(&mut app, "tools.select.brush", json!({}));
|
|
invoke(&mut app, "tool.resetAll", json!({"confirmed": true}));
|
|
assert!(!app.session.tools.brush.shape_dynamics.enabled);
|
|
invoke(&mut app, "tools.select.pencil", json!({}));
|
|
assert_defaults(&app.session.tools.brush);
|
|
|
|
let current = BrushSettings::default();
|
|
for tool in [Tool::BackgroundEraser, Tool::Healing, Tool::CloneStamp] {
|
|
let brush = crate::paint_mouse::fresh_brush(tool, ¤t);
|
|
assert_eq!(brush.shape_dynamics, ShapeDynamics::default(), "{tool:?}");
|
|
assert_eq!(brush.transfer, current.transfer, "{tool:?}");
|
|
assert!(!brush.pressure_size && !brush.pressure_opacity, "{tool:?}");
|
|
}
|
|
}
|
|
|
|
fn prepare_stroke(app: &mut PhotocraftApp) {
|
|
invoke(app, "tools.select.pencil", json!({}));
|
|
invoke(app, "tool.reset", json!({}));
|
|
app.run("tools.setBrush", json!({"brush": {"size": 41, "smoothing": {"amount": 0.0}}})).unwrap();
|
|
}
|
|
|
|
fn opaque_width(app: &PhotocraftApp) -> usize {
|
|
let state = app.session.active().unwrap();
|
|
let surface = state.doc.layer(state.active_layer.unwrap()).unwrap().surface().unwrap();
|
|
(0..100)
|
|
.filter(|&y| {
|
|
let alpha = surface.rgba(100, y)[3];
|
|
assert!(alpha == 0.0 || alpha == 1.0, "Pencil stays opaque: {alpha}");
|
|
alpha > 0.0
|
|
})
|
|
.count()
|
|
}
|
|
|
|
#[test]
|
|
fn reset_pencil_draws_half_and_full_pressure_widths_through_the_actual_tool_path() {
|
|
for (pressure, allowed) in [(0.5, 20..=22), (1.0, 41..=41)] {
|
|
let mut app = app();
|
|
prepare_stroke(&mut app);
|
|
tool_event(&mut app, ToolEvent::Down { x: 30.0, y: 50.0, pressure }, Modifiers::NONE);
|
|
for x in (40..=170).step_by(10) {
|
|
tool_event(&mut app, ToolEvent::Move { x: f64::from(x), y: 50.0, pressure }, Modifiers::NONE);
|
|
}
|
|
tool_event(&mut app, ToolEvent::Up { x: 170.0, y: 50.0 }, Modifiers::NONE);
|
|
assert!(!app.ui.status_error, "{}", app.ui.status);
|
|
let width = opaque_width(&app);
|
|
assert!(allowed.contains(&width), "pressure={pressure}, width={width}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn no_pressure_mouse_points_keep_the_full_nominal_width() {
|
|
let mut app = app();
|
|
prepare_stroke(&mut app);
|
|
// Omitted pressure is the command's real no-pen path, not a fake zero-pressure pen.
|
|
app.run("paint.pencil", json!({"points": [[30, 50], [170, 50]]})).unwrap();
|
|
assert_eq!(opaque_width(&app), 41);
|
|
}
|