mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #12056 from DuBearte/fix/11891-brush-hud-rate
Brush HUD: hardness at the reference rate and a Diameter / Hardness / Opacity readout
This commit is contained in:
6 files changed
+118
-23
No files matched your search
@@ -0,0 +1,76 @@
|
||||
//! #11891: the Alt+right-drag brush HUD changes hardness at Photoshop's rate (50 screen points
|
||||
//! down from 0 % give 13 %), and its readout has three lines: "Diameter :", "Hardness :" and
|
||||
//! "Opacity :". The horizontal size change is unchanged.
|
||||
|
||||
use egui::{Modifiers, PointerButton, vec2};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::brush_resize::{readout_label, resized};
|
||||
use crate::state::Tool;
|
||||
|
||||
#[test]
|
||||
fn fifty_points_down_from_zero_give_thirteen_percent() {
|
||||
for zoom in [0.5, 1.0, 2.0] {
|
||||
// The drag is in document pixels; 50 screen points is 50 / zoom of them.
|
||||
let (_, hardness) = resized((13.0, 0.0), 0.0, f64::from(50.0 / zoom), zoom);
|
||||
assert!((hardness - 0.13).abs() <= 0.02, "zoom {zoom}: {hardness}");
|
||||
}
|
||||
assert_eq!(resized((13.0, 0.0), 0.0, -50.0, 1.0).1, 0.0, "up from 0 % stays 0 %");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_size_change_is_unchanged() {
|
||||
assert_eq!(resized((13.0, 0.0), 50.0, 0.0, 1.0).0, 113.0);
|
||||
assert_eq!(resized((13.0, 0.0), -278.0, 0.0, 1.0).0, 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn labels_get_a_space_before_the_colon() {
|
||||
assert_eq!(readout_label("Diameter:"), "Diameter :");
|
||||
assert_eq!(readout_label("Diamètre :"), "Diamètre :", "a translation's own spacing stays");
|
||||
assert_eq!(readout_label("Size"), "Size");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_readout_shows_diameter_hardness_and_opacity() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
app.run("tools.setBrush", json!({"brush": {"size": 13, "hardness": 0.0, "opacity": 1.0}})).unwrap();
|
||||
app.ui.tool = Tool::Brush;
|
||||
app.sync_views();
|
||||
let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
|
||||
},
|
||||
app,
|
||||
);
|
||||
crate::PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.run_steps(4);
|
||||
let c = h.state().last_canvas_rect.center();
|
||||
let alt = Modifiers::ALT;
|
||||
h.event(egui::Event::ModifiersChanged(alt));
|
||||
h.event(egui::Event::PointerMoved(c));
|
||||
h.run_steps(1);
|
||||
h.event(egui::Event::PointerButton { pos: c, button: PointerButton::Secondary, pressed: true, modifiers: alt });
|
||||
h.run_steps(1);
|
||||
for i in 1..=5 {
|
||||
h.event(egui::Event::PointerMoved(c + vec2(0.0, 10.0 * i as f32)));
|
||||
h.run_steps(1);
|
||||
}
|
||||
let hardness = h.state().session.tools.brush.hardness;
|
||||
assert!((hardness - 0.13).abs() <= 0.02, "{hardness}");
|
||||
for label in ["Diameter :", "Hardness :", "Opacity :"] {
|
||||
assert!(h.query_by_label(label).is_some(), "{label}");
|
||||
}
|
||||
assert!(h.query_by_label("13%").is_some() && h.query_by_label("100%").is_some());
|
||||
h.event(egui::Event::PointerButton { pos: c + vec2(0.0, 50.0), button: PointerButton::Secondary, pressed: false, modifiers: alt });
|
||||
h.run_steps(2);
|
||||
}
|
||||
@@ -917,11 +917,13 @@ fn brush_resize_gesture(mods: egui::Modifiers, secondary: bool) -> bool {
|
||||
}
|
||||
|
||||
/// Horizontal motion sets the diameter (the circle's edge follows the pointer). Vertical motion
|
||||
/// sets density, Liquify's edge falloff, over the same 200 px travel the canvas uses for hardness.
|
||||
/// sets density, Liquify's edge falloff, over 200 px of travel.
|
||||
fn resized_brush(start_size: f32, start_density: f32, dx: f32, dy: f32, zoom: f32) -> (f32, f32) {
|
||||
// Liquify stores density as 0–100; the shared brush gesture works on normalized hardness.
|
||||
// Use the same zoom-corrected screen movement and size limits as the main canvas.
|
||||
let zoom = if zoom.is_finite() && zoom > 0.0 { zoom } else { 1.0 };
|
||||
// Density keeps its 200 px travel; the canvas hardness took Photoshop's slower rate (#11891).
|
||||
let dy = dy * crate::brush_resize::HARDNESS_TRAVEL / 200.0;
|
||||
let (size, density) = crate::brush_resize::resized((start_size, start_density / 100.0), f64::from(dx / zoom), f64::from(dy / zoom), zoom);
|
||||
(size, (density * 100.0).round())
|
||||
}
|
||||
|
||||
@@ -888,7 +888,8 @@ fn alt_right_drag_resizes_the_workspace_brush() {
|
||||
let w = h.state().distort.select_mask.as_ref().unwrap();
|
||||
let want = (30.0 + 2.0 * f64::from(40.0 / zoom)).round();
|
||||
assert_eq!(w.brush_size, want, "horizontal motion sets the diameter (zoom {zoom})");
|
||||
assert_eq!(w.hardness, 70.0, "upward motion softens the brush, 200 points for 100 %");
|
||||
let soft = (100.0 - 60.0 * 100.0 / crate::brush_resize::HARDNESS_TRAVEL).round();
|
||||
assert_eq!(w.hardness, soft, "upward motion softens the brush, at the canvas HUD's rate");
|
||||
assert_eq!((w.quick_size, w.refine_size), (30.0, 30.0), "the other tools keep their sizes");
|
||||
assert!(w.strokes.is_empty() && w.edits.is_empty() && w.undo.is_empty(), "resizing selected nothing");
|
||||
assert!(w.resize.is_none(), "the gesture ends with the button");
|
||||
@@ -904,7 +905,7 @@ fn alt_right_drag_resizes_the_workspace_brush() {
|
||||
pointer_drag(&mut h, over, PointerButton::Primary, Modifiers::CTRL | Modifiers::ALT, vec2(-10.0, 0.0));
|
||||
let w = h.state().distort.select_mask.as_ref().unwrap();
|
||||
assert_eq!(w.quick_size, (30.0 - 2.0 * f64::from(10.0 / zoom)).round().max(1.0));
|
||||
assert_eq!((w.brush_size, w.hardness), (want, 70.0), "the Brush keeps its size and hardness");
|
||||
assert_eq!((w.brush_size, w.hardness), (want, soft), "the Brush keeps its size and hardness");
|
||||
assert!(w.strokes.is_empty() && w.edits.len() == 1, "resizing Quick Selection selected nothing");
|
||||
}
|
||||
|
||||
|
||||
@@ -1066,6 +1066,8 @@ mod background_eraser_bar_angle_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_alt_digit_smoothing_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_hud_rate_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_opacity_minimum_tests;
|
||||
#[cfg(test)]
|
||||
mod burn_default_tip_tests;
|
||||
|
||||
@@ -19,8 +19,12 @@ use crate::PhotocraftApp;
|
||||
use crate::canvas::{ToolEvent, ViewXform};
|
||||
use crate::state::Tool;
|
||||
|
||||
/// Screen points of vertical drag that take the hardness from 0 % to 100 %.
|
||||
pub const HARDNESS_TRAVEL: f32 = 200.0;
|
||||
/// Screen points of vertical drag that take the hardness from 0 % to 100 %: Photoshop's rate,
|
||||
/// where 50 points down from 0 % give 13 % (#11891).
|
||||
pub const HARDNESS_TRAVEL: f32 = 385.0;
|
||||
/// Screen points of vertical drag that take the opacity from 0 % to 100 % when "Vary Round Brush
|
||||
/// Hardness on HUD" is off: the rate it had before the hardness rate was measured (#11891).
|
||||
pub const OPACITY_TRAVEL: f32 = 200.0;
|
||||
/// The brush size range (`tools.setBrush` accepts the same).
|
||||
const SIZE: std::ops::RangeInclusive<f32> = 1.0..=MAX_BRUSH_SIZE;
|
||||
|
||||
@@ -122,10 +126,11 @@ fn update(app: &mut PhotocraftApp, r: Resize, x: f64, y: f64) {
|
||||
// No view: document axes, mirrored by View › Flip Horizontal.
|
||||
_ => ((x - r.anchor[0]) * if app.ui.view.flip_horizontal { -1.0 } else { 1.0 }, y - r.anchor[1], app.point_zoom()),
|
||||
};
|
||||
let (size, v) = resized(r.start, dx, dy, zoom);
|
||||
// "Vary Round Brush Hardness on HUD" (Preferences ▸ Tools, on by default): off, the vertical
|
||||
// movement takes the opacity instead of the hardness, as in Photoshop.
|
||||
// movement takes the opacity instead of the hardness, as in Photoshop, at its own rate.
|
||||
let vary = app.session.prefs().tools.vary_round_brush_hardness_on_hud;
|
||||
let dy = if vary { dy } else { dy * f64::from(HARDNESS_TRAVEL / OPACITY_TRAVEL) };
|
||||
let (size, v) = resized(r.start, dx, dy, zoom);
|
||||
let b = &app.session.tools.brush;
|
||||
if b.size == size && if vary { b.hardness == v } else { b.opacity == v } {
|
||||
return;
|
||||
@@ -151,15 +156,22 @@ pub fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) -> boo
|
||||
// Photoshop hides the pointer while the circle stays put.
|
||||
painter.ctx().set_cursor_icon(egui::CursorIcon::None);
|
||||
let at = painter.ctx().pointer_latest_pos().unwrap_or(c);
|
||||
let (label, value) = if app.session.prefs().tools.vary_round_brush_hardness_on_hud {
|
||||
(tl!("Hardness:"), format!("{}%", (b.hardness * 100.0).round() as i64))
|
||||
} else {
|
||||
(tl!("Opacity:"), format!("{}%", (b.opacity * 100.0).round() as i64))
|
||||
};
|
||||
crate::canvas::draw_readout(painter.ctx(), "brush-resize-readout", at, [tl!("Diameter:"), label], [format!("{} px", b.size.round() as i64), value]);
|
||||
let percent = |v: f32| format!("{}%", (v * 100.0).round() as i64);
|
||||
let labels = [readout_label(tl!("Diameter:")), readout_label(tl!("Hardness:")), readout_label(tl!("Opacity:"))];
|
||||
let values = [format!("{} px", b.size.round() as i64), percent(b.hardness), percent(b.opacity)];
|
||||
crate::canvas::draw_readout(painter.ctx(), "brush-resize-readout", at, labels.each_ref().map(String::as_str), values);
|
||||
true
|
||||
}
|
||||
|
||||
/// A readout label as Photoshop's HUD shows it, with a space before the colon ("Diameter :",
|
||||
/// #11891). A translation that already has one keeps it.
|
||||
pub(crate) fn readout_label(label: &str) -> String {
|
||||
match label.strip_suffix(':') {
|
||||
Some(head) if !head.ends_with(char::is_whitespace) => format!("{head} :"),
|
||||
_ => label.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The tip preview's tint: Preferences ▸ Cursors ▸ Brush Preview colour (red by default, as in
|
||||
/// Photoshop), at the overlay's alpha. Unreadable values keep the red.
|
||||
fn preview_tint(app: &PhotocraftApp) -> Color32 {
|
||||
|
||||
@@ -22,7 +22,7 @@ fn history(app: &PhotocraftApp) -> usize {
|
||||
#[test]
|
||||
fn mapping_is_bounded() {
|
||||
assert_eq!(resized((40.0, 0.5), 10.0, 0.0, 1.0), (60.0, 0.5), "the edge follows the pointer");
|
||||
assert_eq!(resized((40.0, 0.5), 0.0, 50.0, 2.0), (40.0, 1.0), "down is harder, in screen points");
|
||||
assert_eq!(resized((40.0, 0.5), 0.0, f64::from(HARDNESS_TRAVEL / 4.0), 2.0), (40.0, 1.0), "down is harder, in screen points");
|
||||
assert_eq!(resized((40.0, 0.5), -1e9, -1e9, 1.0), (1.0, 0.0));
|
||||
assert_eq!(resized((40.0, 0.5), 1e9, 1e9, 1.0), (MAX_BRUSH_SIZE, 1.0));
|
||||
assert_eq!(resized((40.0, 0.5), f64::NAN, f64::INFINITY, f32::NAN), (40.0, 0.5));
|
||||
@@ -41,11 +41,11 @@ fn ctrl_alt_drag_resizes_every_brush_tool_without_painting_or_history() {
|
||||
// Right grows the size.
|
||||
tool_event(&mut app, ToolEvent::Move { x: 130.0, y: 100.0, pressure: 1.0 }, CTRL_ALT);
|
||||
assert_eq!(app.session.tools.brush.size, 100.0, "{tool:?}");
|
||||
// Down raises the hardness (at 100 %, 200 points take it from 0 to 100 %).
|
||||
tool_event(&mut app, ToolEvent::Move { x: 130.0, y: 160.0, pressure: 1.0 }, CTRL_ALT);
|
||||
// Down raises the hardness (at 100 %, 385 points take it from 0 to 100 %, #11891).
|
||||
tool_event(&mut app, ToolEvent::Move { x: 130.0, y: 215.5, pressure: 1.0 }, CTRL_ALT);
|
||||
assert_eq!(app.session.tools.brush.hardness, 0.5, "{tool:?}");
|
||||
// Letting go of the keys mid-drag keeps resizing until the button is released.
|
||||
tool_event(&mut app, ToolEvent::Up { x: 120.0, y: 180.0 }, Modifiers::NONE);
|
||||
tool_event(&mut app, ToolEvent::Up { x: 120.0, y: 254.0 }, Modifiers::NONE);
|
||||
let b = &app.session.tools.brush;
|
||||
assert_eq!((b.size, b.hardness), (80.0, 0.6), "{tool:?}");
|
||||
assert!(app.brush_resize.is_none() && app.drag.is_none());
|
||||
@@ -297,9 +297,11 @@ fn vary_round_brush_hardness_on_hud_switches_the_vertical_target() {
|
||||
app.run("prefs.set", json!({"path": "tools.varyRoundBrushHardnessOnHud", "value": false})).unwrap();
|
||||
}
|
||||
let (h0, o0) = (app.session.tools.brush.hardness, app.session.tools.brush.opacity);
|
||||
// 20 % of each travel: 77 points for the hardness, 40 for the opacity, which keeps its rate.
|
||||
let y = if vary { 177.0 } else { 140.0 };
|
||||
tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 100.0, pressure: 1.0 }, CTRL_ALT);
|
||||
tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 140.0, pressure: 1.0 }, CTRL_ALT);
|
||||
tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 140.0 }, CTRL_ALT);
|
||||
tool_event(&mut app, ToolEvent::Move { x: 100.0, y, pressure: 1.0 }, CTRL_ALT);
|
||||
tool_event(&mut app, ToolEvent::Up { x: 100.0, y }, CTRL_ALT);
|
||||
let b = &app.session.tools.brush;
|
||||
if vary {
|
||||
assert_eq!((b.hardness, b.opacity), (h0 + 0.2, o0), "hardness moves with the drag");
|
||||
@@ -330,16 +332,16 @@ fn drag_in_view(rotation: f32, flip: bool, dx: f64, dy: f64) -> (f32, f32) {
|
||||
fn a_rotated_view_keeps_horizontal_for_size_and_vertical_for_hardness() {
|
||||
// Unrotated, with and without View › Flip Horizontal: unchanged.
|
||||
assert_eq!(drag_in_view(0.0, false, 30.0, 0.0), (100.0, 0.2), "right grows");
|
||||
assert_eq!(drag_in_view(0.0, false, 0.0, 40.0), (40.0, 0.4), "down hardens");
|
||||
assert_eq!(drag_in_view(0.0, false, 0.0, 77.0), (40.0, 0.4), "down hardens");
|
||||
assert_eq!(drag_in_view(0.0, true, -30.0, 0.0), (100.0, 0.2), "flipped: screen right grows");
|
||||
assert_eq!(drag_in_view(0.0, true, 0.0, 40.0), (40.0, 0.4), "flipped: down hardens");
|
||||
assert_eq!(drag_in_view(0.0, true, 0.0, 77.0), (40.0, 0.4), "flipped: down hardens");
|
||||
// 90° clockwise: document +y points screen-left and document +x screen-down.
|
||||
assert_eq!(drag_in_view(90.0, false, 0.0, 10.0), (20.0, 0.2), "screen-left shrinks, the hardness stays");
|
||||
assert_eq!(drag_in_view(90.0, false, 0.0, -30.0), (100.0, 0.2), "screen-right grows");
|
||||
assert_eq!(drag_in_view(90.0, false, 40.0, 0.0), (40.0, 0.4), "screen-down hardens, the size stays");
|
||||
assert_eq!(drag_in_view(90.0, false, 77.0, 0.0), (40.0, 0.4), "screen-down hardens, the size stays");
|
||||
// 180°: document -x is screen right.
|
||||
assert_eq!(drag_in_view(180.0, false, -30.0, 0.0), (100.0, 0.2), "screen right still grows");
|
||||
assert_eq!(drag_in_view(180.0, false, 0.0, -40.0), (40.0, 0.4), "screen down still hardens");
|
||||
assert_eq!(drag_in_view(180.0, false, 0.0, -77.0), (40.0, 0.4), "screen down still hardens");
|
||||
// 90° and flipped: document +y points screen-right.
|
||||
assert_eq!(drag_in_view(90.0, true, 0.0, 30.0), (100.0, 0.2), "rotated and flipped: screen right grows");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user