Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Bound paint stroke work (#678, #679) (#751)

Alex Gorevski51a0816f99cd
diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs
index ff42a9b45..fd1958b2c 100644
--- a/crates/engine/src/brush_cmds.rs
+++ b/crates/engine/src/brush_cmds.rs
@@ -137,6 +137,17 @@ pub fn merge_brush(base: &BrushSettings, patch: &Value, cmd: &str) -> Result<Bru
Ok(out)
}
+pub(crate) fn validate_brush_size(brush: &BrushSettings, cmd: &str) -> Result<()> {
+ let max = photocraft_paint::MAX_BRUSH_SIZE;
+ if !brush.size.is_finite() || brush.size > max {
+ return Err(bad(cmd, format!("brush size must be finite and at most {max} px")));
+ }
+ if brush.dual_brush.enabled && (!brush.dual_brush.size.is_finite() || brush.dual_brush.size > max) {
+ return Err(bad(cmd, format!("dual brush size must be finite and at most {max} px")));
+ }
+ Ok(())
+}
+
fn find_preset<'a>(s: &'a Session, name: &str, cmd: &str) -> Result<&'a BrushPreset> {
photocraft_paint::presets::find(&s.tools.presets, name).ok_or_else(|| bad(cmd, format!("no brush preset named `{name}`")))
}
@@ -176,6 +187,7 @@ pub fn resolve_brush(s: &Session, p: &Value, cmd: &str) -> Result<BrushSettings>
Some(v) => v,
None => photocraft_paint::rng::seed_from_bytes(p.get("points").map(|v| v.to_string()).unwrap_or_default().as_bytes()),
};
+ validate_brush_size(&b, cmd)?;
Ok(b)
}
@@ -625,6 +637,7 @@ fn set_brush(s: &mut Session, p: &Value) -> Result<Value> {
}
}
b = merge_brush(&b, &patch, cmd)?;
+ validate_brush_size(&b, cmd)?;
let before = std::mem::replace(&mut s.tools.brush, b);
// A coalesced gesture (one slider drag) journals as one call: remember the brush it started from.
let key = p.get("coalesce").and_then(Value::as_str).filter(|_| p.get("preset").is_none() && p.get("reset").is_none());
@@ -720,7 +733,7 @@ pub fn specs() -> Vec<CommandSpec> {
spec!(
"paint.pencil",
"Pencil",
- r##"{"points":[[x,y,pressure?,tiltX?,tiltY?,rotation?,timeMs?,wheel?],…],"brush":{…}?,"preset":name?,"size":px?,"opacity":0..1?,"color":"#rrggbb"?=foreground,"mode":"normal|multiply|screen|…"="normal","erase":bool?,"autoErase":bool=false,"seed":u64?,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target}"##,
+ r##"{"points":[[x,y,pressure?,tiltX?,tiltY?,rotation?,timeMs?,wheel?],…],"brush":{…}?,"preset":name?,"size":0.5..5000 px?,"opacity":0..1?,"color":"#rrggbb"?=foreground,"mode":"normal|multiply|screen|…"="normal","erase":bool?,"autoErase":bool=false,"seed":u64?,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target}"##,
has_paintable,
pencil,
true
@@ -728,7 +741,7 @@ pub fn specs() -> Vec<CommandSpec> {
spec!(
"paint.mixerBrush",
"Mixer Brush",
- r##"{"points":[…],"brush":{…}?,"preset":name?,"size":px?,"wet":0..100=brush.mixer.wet,"load":0..100=brush.mixer.load,"mix":0..100=brush.mixer.mix,"flow":0..100=brush.mixer.flow,"color":"#rrggbb"?=foreground,"sampleAllLayers":bool=brush.mixer.sampleAllLayers,"cleanAfterStroke":bool=true,"loadAfterStroke":bool=true,"seed":u64?}"##,
+ r##"{"points":[…],"brush":{…}?,"preset":name?,"size":0.5..5000 px?,"wet":0..100=brush.mixer.wet,"load":0..100=brush.mixer.load,"mix":0..100=brush.mixer.mix,"flow":0..100=brush.mixer.flow,"color":"#rrggbb"?=foreground,"sampleAllLayers":bool=brush.mixer.sampleAllLayers,"cleanAfterStroke":bool=true,"loadAfterStroke":bool=true,"seed":u64?}"##,
has_paintable,
mixer_brush,
true
diff --git a/crates/engine/src/brush_cmds/tests.rs b/crates/engine/src/brush_cmds/tests.rs
index b55d1475d..9ef435169 100644
--- a/crates/engine/src/brush_cmds/tests.rs
+++ b/crates/engine/src/brush_cmds/tests.rs
@@ -51,6 +51,30 @@ fn stroke_accepts_full_points_brush_and_preset() {
assert!(s.execute("paint.stroke", json!({"points": [[1, 1]], "brush": {"size": "big"}})).is_err());
}
+#[test]
+fn paint_commands_bound_hostile_time_and_reject_oversized_brushes() {
+ let mut s = session(32, 32);
+ let timed = s.execute(
+ "paint.stroke",
+ json!({
+ "points": [[16, 16, 1, 0, 0, 0, 0], [16, 16, 1, 0, 0, 0, 1.7976931348623157e308]],
+ "brush": {"buildUp": true, "buildUpRate": 1000, "size": 4, "pressureSize": false}
+ }),
+ );
+ assert!(timed.is_ok(), "the bounded airbrush catch-up should still commit: {timed:?}");
+
+ for command in ["paint.stroke", "paint.pencil"] {
+ let result = s.execute(command, json!({"points": [[8, 8]], "size": 1e30}));
+ assert!(result.is_err(), "{command} must reject an oversized brush before rasterizing");
+ }
+ let result = s.execute("paint.stroke", json!({"points": [[8, 8]], "brush": {"dualBrush": {"enabled": true, "size": 1e30}}}));
+ assert!(result.is_err(), "enabled dual brush dimensions are bounded too");
+
+ s.execute("tools.setBrush", json!({"size": 5000})).expect("the shared brush limit is accepted");
+ assert!(s.execute("tools.setBrush", json!({"size": 5001})).is_err(), "tools.setBrush must reject sizes above the shared limit");
+ assert_eq!(s.tools.brush.size, 5000.0, "a rejected brush update must not change the session brush");
+}
+
#[test]
fn replay_is_deterministic() {
let params = json!({"points": [[10, 50], [60, 30, 0.7], [120, 60, 0.9], [180, 40]], "preset": "Spatter", "color": "#ff8800"});
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index b560b107c..8d7758ee1 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -739,7 +739,7 @@ fn build() -> Vec<CommandSpec> {
"Brush Stroke",
[],
None,
- r##"{"points":[[x,y,pressure?,tiltX?,tiltY?,rotation?,timeMs?,wheel?],…],"brush":{…BrushSettings}?,"preset":name?,"size":px?,"hardness":0..1?,"opacity":0..1?,"flow":0..1?,"spacing":0..10?,"color":"#rrggbb"?=foreground,"mode":"normal|multiply|screen|…"="normal","erase":bool?,"smoothing":0..1?,"zoom":number=1,"seed":u64?,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target}"##,
+ r##"{"points":[[x,y,pressure?,tiltX?,tiltY?,rotation?,timeMs?,wheel?],…],"brush":{…BrushSettings}?,"preset":name?,"size":0.5..5000 px?,"hardness":0..1?,"opacity":0..1?,"flow":0..1?,"spacing":0..10?,"color":"#rrggbb"?=foreground,"mode":"normal|multiply|screen|…"="normal","erase":bool?,"smoothing":0..1?,"zoom":number=1,"seed":u64?,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target}"##,
has_paintable,
crate::brush_cmds::paint_stroke
),
diff --git a/crates/engine/src/eraser_cmds.rs b/crates/engine/src/eraser_cmds.rs
index e0b545f95..cccb8847d 100644
--- a/crates/engine/src/eraser_cmds.rs
+++ b/crates/engine/src/eraser_cmds.rs
@@ -129,9 +129,6 @@ fn background_eraser(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad(CMD, "`points` must be finite canvas coordinates"));
}
let brush = crate::brush_cmds::resolve_brush(s, p, CMD)?;
- if !(brush.size.is_finite() && brush.size <= 5000.0) {
- return Err(bad(CMD, "`size` must be at most 5000 px"));
- }
let bs = BgEraseSettings {
sampling: enum_param(p, CMD, "sampling", Sampling::Continuous, "continuous|once|backgroundSwatch")?,
limits: enum_param(p, CMD, "limits", Limits::Contiguous, "discontiguous|contiguous|findEdges")?,
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 1a18398e7..5f1e072cd 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -90,6 +90,7 @@ fn parse_brush(s: &Session, p: &Value, cmd: &str) -> Result<(Stroke, Option<Laye
erase: false,
..base
};
+ crate::brush_cmds::validate_brush_size(&brush, cmd)?;
if crate::channel_cmds::is_channel_target(p) {
return Ok((Stroke { brush, points: pts }, None));
}
@@ -647,7 +648,7 @@ fn smudge_cmd(s: &mut Session, p: &Value) -> Result<Value> {
/// The brush every retouching command shares, plus the tool's own params.
macro_rules! brush_params {
($extra:literal) => {
- concat!("{", r#""points":[[x,y,pressure?],…],"size":px=tool size,"hardness":0..100=tool hardness,"opacity":1..100=100,"flow":1..100=100,"spacing":1..1000 (% of size)=25,"layer":id?=active,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target"#, $extra, "}")
+ concat!("{", r#""points":[[x,y,pressure?],…],"size":1..5000 px=tool size,"hardness":0..100=tool hardness,"opacity":1..100=100,"flow":1..100=100,"spacing":1..1000 (% of size)=25,"layer":id?=active,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target"#, $extra, "}")
};
}
diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs
index 662918793..c6cf3dbfd 100644
--- a/crates/engine/src/retouch_cmds/tests.rs
+++ b/crates/engine/src/retouch_cmds/tests.rs
@@ -65,6 +65,13 @@ fn clone_stamp_copies_exactly_at_all_depths() {
}
}
+#[test]
+fn retouch_rejects_brushes_larger_than_the_raster_budget() {
+ let mut s = session(32, 32, 8, "rgb");
+ let result = s.execute("paint.dodge", json!({"points": [[10, 10]], "size": 1e30}));
+ assert!(result.is_err(), "retouch must reject an oversized brush before footprint allocation");
+}
+
#[test]
fn clone_stamp_samples_pre_stroke_state_and_non_aligned_returns_source() {
let mut s = session(80, 20, 16, "rgb");
diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rs
index 03067fdfd..fe2d8ef97 100644
--- a/crates/engine/src/vector_cmds.rs
+++ b/crates/engine/src/vector_cmds.rs
@@ -1012,6 +1012,7 @@ fn path_stroke(s: &mut Session, p: &Value) -> Result<Value> {
"brush" | "eraser" => {}
o => return Err(bad("path.stroke", format!("unknown tool `{o}` (brush|pencil|eraser)"))),
}
+ crate::brush_cmds::validate_brush_size(&brush, "path.stroke")?;
let lines = vector::flatten_path(&path, 0.1);
let id = layer_id(s, p)?;
let bg = s.tools.background;
@@ -1369,7 +1370,7 @@ pub fn specs() -> Vec<CommandSpec> {
"path.stroke",
"Stroke Path",
[],
- r##"{"name":str|"work"|"layer"="work","layer":id? (pixel layer),"tool":"brush|pencil|eraser"="brush","size":px?,"hardness":0..1?,"opacity":0..100?,"color":"#rrggbb"=foreground} (current brush settings otherwise)"##,
+ r##"{"name":str|"work"|"layer"="work","layer":id? (pixel layer),"tool":"brush|pencil|eraser"="brush","size":0.5..5000 px?,"hardness":0..1?,"opacity":0..100?,"color":"#rrggbb"=foreground} (current brush settings otherwise)"##,
has_layer,
path_stroke
),
diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rs
index 7977a5d7e..a335209e4 100644
--- a/crates/engine/src/vector_cmds/tests.rs
+++ b/crates/engine/src/vector_cmds/tests.rs
@@ -462,3 +462,11 @@ fn all_vector_commands_are_registered() {
ids.dedup();
assert_eq!(ids.len(), n, "duplicate command ids");
}
+
+#[test]
+fn path_stroke_rejects_oversized_brush_before_rendering() {
+ let mut s = session(32, 32, 8);
+ s.execute("path.set", json!({"name": "work", "path": {"subpaths": [{"closed": false, "knots": [[2, 2], [20, 20]]}]}})).unwrap();
+ let result = s.execute("path.stroke", json!({"size": 1e30}));
+ assert!(result.is_err(), "path strokes share the bounded paint renderer");
+}
diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs
index 01ffc756b..b43a39c73 100644
--- a/crates/paint/src/brush.rs
+++ b/crates/paint/src/brush.rs
@@ -13,6 +13,9 @@ use serde::{Deserialize, Serialize};
use crate::mixer::MixerSettings;
use crate::tile::GrayTile;
+/// Largest brush diameter accepted by the rasterizer and brush controls.
+pub const MAX_BRUSH_SIZE: f32 = 5000.0;
+
/// What drives a dynamic parameter (Photoshop's "Control" pop-ups).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -458,6 +461,20 @@ impl Default for BrushSettings {
}
impl BrushSettings {
+ /// Copy the settings with the primary and dual diameters constrained for rasterization.
+ ///
+ /// Engine commands reject out-of-range values; this is a final guard for direct users of the
+ /// infallible paint API, which must not turn malformed brush dimensions into giant allocations.
+ pub fn bounded_for_render(&self) -> Self {
+ let safe_size = |size: f32| {
+ if size.is_finite() { size.clamp(0.5, MAX_BRUSH_SIZE) } else { 0.5 }
+ };
+ let mut brush = self.clone();
+ brush.size = safe_size(brush.size);
+ brush.dual_brush.size = safe_size(brush.dual_brush.size);
+ brush
+ }
+
/// This brush (a preset) picked while `current` is the tool's brush: Smoothing is a tool
/// option, so it stays the tool's (Photoshop), a protected texture stays too, and so does every
/// section `current` has locked (the locks themselves are tool state and carry over).
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs
index ab3be0407..fca342801 100644
--- a/crates/paint/src/brush_tests.rs
+++ b/crates/paint/src/brush_tests.rs
@@ -395,6 +395,33 @@ fn build_up_accumulates_over_time() {
assert_eq!(dabs_of(&nb, &[a, z]).len(), 1);
}
+#[test]
+fn huge_airbrush_time_gap_emits_bounded_catch_up() {
+ let b = BrushSettings { build_up: true, build_up_rate: 1000.0, ..brush() };
+ let a = StrokePoint { time: 0.0, ..StrokePoint::new(20.0, 20.0, 1.0) };
+ let brief_pause = StrokePoint { time: 200.0, ..a };
+ assert_eq!(dabs_of(&b, &[a, brief_pause]).len(), 201, "a 200 ms pause should retain its airbrush build-up");
+
+ let z = StrokePoint { time: f64::MAX, ..a };
+ let d = dabs_of(&b, &[a, z]);
+ assert!(d.len() <= 2, "a huge pause should resume with one dab, not replay its time backlog: {}", d.len());
+ assert_eq!(d.last().map(|dab| dab.center), Some(Point::new(20.0, 20.0)));
+
+ let infinite = StrokePoint { time: f64::INFINITY, ..a };
+ assert!(dabs_of(&b, &[a, infinite]).len() <= 1);
+}
+
+#[test]
+fn unbounded_brush_dimensions_are_safe_in_the_public_renderer() {
+ let brush = BrushSettings { size: f32::MAX, dual_brush: DualBrush { enabled: true, size: f32::INFINITY, ..Default::default() }, ..brush() };
+ let ctx = BrushContext::new(&brush);
+ assert_eq!(ctx.brush.size, crate::MAX_BRUSH_SIZE);
+ assert_eq!(ctx.brush.dual_brush.size, 0.5);
+
+ let d = dabs_of(&brush, &[StrokePoint::new(10.0, 10.0, 1.0)]);
+ assert!(d.iter().all(|dab| dab.radius <= crate::MAX_BRUSH_SIZE / 2.0));
+}
+
#[test]
fn texture_modulates_coverage() {
let tile = GrayTile::from_fn(8, 8, |x, _| if x < 4 { 1.0 } else { 0.0 });
diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs
index 64caebeaf..e54d137f6 100644
--- a/crates/paint/src/dynamics.rs
+++ b/crates/paint/src/dynamics.rs
@@ -19,6 +19,8 @@ pub const SPEED_SPACING_MS: f64 = 8.0;
/// Densest speed spacing, in pixels between dabs (a slow, long stroke never floods the buffer).
const MIN_SPEED_STEP: f64 = 0.5;
+/// Maximum airbrush catch-up dabs for one input segment.
+const MAX_AIRBRUSH_DABS_PER_SEGMENT: usize = 512;
#[inline]
fn lerp_pt(a: &StrokePoint, b: &StrokePoint, f: f64) -> StrokePoint {
@@ -240,12 +242,21 @@ impl PathWalker {
}
if let Some(iv) = self.interval {
let dt = p.time - a.time;
- if dt > 0.0 {
+ if dt > 0.0 && dt.is_finite() {
+ if dt > iv * MAX_AIRBRUSH_DABS_PER_SEGMENT as f64 {
+ // A stalled or hostile timestamp must not replay an arbitrarily long backlog.
+ self.time_acc = 0.0;
+ self.emit(p, out);
+ self.last = Some(p);
+ return;
+ }
self.time_acc += dt;
- while self.time_acc >= iv {
+ let mut n = 0;
+ while self.time_acc >= iv && n < MAX_AIRBRUSH_DABS_PER_SEGMENT {
self.time_acc -= iv;
let f = ((dt - self.time_acc) / dt).clamp(0.0, 1.0);
self.emit(lerp_pt(&a, &p, f), out);
+ n += 1;
}
}
}
@@ -592,14 +603,15 @@ pub struct DabGenerator {
impl DabGenerator {
pub fn new(brush: &BrushSettings, zoom: f32) -> Self {
+ let brush = brush.bounded_for_render();
let interval = brush.build_up.then(|| 1000.0 / f64::from(brush.build_up_rate.clamp(0.1, 1000.0)));
let dual_on = brush.dual_brush.enabled;
Self {
smoother: Smoother::new(&brush.smoothing, zoom),
walker: if brush.spacing_enabled { PathWalker::new(interval) } else { PathWalker::new(interval).speed_spacing(SPEED_SPACING_MS) },
dual_walker: dual_on.then(|| PathWalker::new(None)),
- builder: DabBuilder::new(brush),
- dual: dual_on.then(|| DualBuilder::new(brush)),
+ builder: DabBuilder::new(&brush),
+ dual: dual_on.then(|| DualBuilder::new(&brush)),
scratch: Vec::new(),
}
}
diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs
index 6fd6aa7d8..27fa5882a 100644
--- a/crates/paint/src/lib.rs
+++ b/crates/paint/src/lib.rs
@@ -31,8 +31,8 @@ pub mod rng;
pub mod tile;
pub use brush::{
- BrushPreset, BrushSettings, ColorDynamics, Control, DualBrush, Dynamic, MaskMode, Pattern, PatternStyle, Pose, Scattering, SectionLocks, ShapeDynamics,
- Smoothing, Texture, TipShape, Transfer,
+ BrushPreset, BrushSettings, ColorDynamics, Control, DualBrush, Dynamic, MAX_BRUSH_SIZE, MaskMode, Pattern, PatternStyle, Pose, Scattering, SectionLocks,
+ ShapeDynamics, Smoothing, Texture, TipShape, Transfer,
};
pub use mixer::MixerSettings;
pub use render::{BrushContext, StrokeRenderer, grid_center, grid_square, render_stroke};
diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs
index 5ae222dea..f76b11b2b 100644
--- a/crates/paint/src/render.rs
+++ b/crates/paint/src/render.rs
@@ -111,6 +111,7 @@ fn prepare_pattern(b: &BrushSettings) -> Option<Arc<PatternImage>> {
impl BrushContext {
pub fn new(brush: &BrushSettings) -> Self {
+ let brush = brush.bounded_for_render();
let mips = |t: &TipShape| match t {
TipShape::Sampled(g) if g.is_valid() => Some(Arc::new(Mips::new(g))),
_ => None,
@@ -118,8 +119,8 @@ impl BrushContext {
Self {
tip: mips(&brush.tip),
dual_tip: if brush.dual_brush.enabled { mips(&brush.dual_brush.tip) } else { None },
- texture: prepare_pattern(brush),
- brush: brush.clone(),
+ texture: prepare_pattern(&brush),
+ brush,
}
}
diff --git a/crates/ui-egui/src/brush_picker.rs b/crates/ui-egui/src/brush_picker.rs
index 184dc5a84..cb4a22803 100644
--- a/crates/ui-egui/src/brush_picker.rs
+++ b/crates/ui-egui/src/brush_picker.rs
@@ -11,7 +11,7 @@ use std::collections::BTreeSet;
use egui::{Color32, RichText, Sense, Stroke, pos2, vec2};
use photocraft_engine::BrushSettings;
-use photocraft_engine::paint::BrushPreset;
+use photocraft_engine::paint::{BrushPreset, MAX_BRUSH_SIZE};
use serde_json::json;
use crate::brush_panel::{full_uv, grouped_presets, is_current, new_preset_name, run_or_status};
@@ -97,14 +97,14 @@ pub fn body(ui: &mut egui::Ui, b: &mut BrushSettings, presets: &[BrushPreset]) -
ui.label(RichText::new(tl!("Size")).color(t.text_dim));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let mut size = b.size;
- if widgets::value_field(ui, &mut size, 1.0..=5000.0, "px", 72.0).changed() {
- b.size = size.round().clamp(1.0, 5000.0);
+ if widgets::value_field(ui, &mut size, 1.0..=MAX_BRUSH_SIZE, "px", 72.0).changed() {
+ b.size = size.round().clamp(1.0, MAX_BRUSH_SIZE);
}
});
});
let mut lv = b.size.max(1.0).ln();
- if widgets::slider(ui, &mut lv, 0.0..=5000f32.ln(), None).changed() {
- b.size = lv.exp().round().clamp(1.0, 5000.0);
+ if widgets::slider(ui, &mut lv, 0.0..=MAX_BRUSH_SIZE.ln(), None).changed() {
+ b.size = lv.exp().round().clamp(1.0, MAX_BRUSH_SIZE);
}
let mut hard = b.hardness * 100.0;
if widgets::slider_row(ui, "Hardness", &mut hard, 0.0..=100.0, "%", None).changed() {
diff --git a/crates/ui-egui/src/brush_resize.rs b/crates/ui-egui/src/brush_resize.rs
index 4dd3d4676..753ee7948 100644
--- a/crates/ui-egui/src/brush_resize.rs
+++ b/crates/ui-egui/src/brush_resize.rs
@@ -12,6 +12,7 @@
use std::sync::atomic::{AtomicU64, Ordering};
use egui::{Color32, Pos2, Stroke, vec2};
+use photocraft_engine::paint::MAX_BRUSH_SIZE;
use serde_json::json;
use crate::PhotocraftApp;
@@ -21,7 +22,7 @@ use crate::state::Tool;
/// Screen points of vertical drag that take the hardness from 0 % to 100 %.
pub const HARDNESS_TRAVEL: f32 = 200.0;
/// The brush size range (`tools.setBrush` accepts the same).
-const SIZE: std::ops::RangeInclusive<f32> = 1.0..=5000.0;
+const SIZE: std::ops::RangeInclusive<f32> = 1.0..=MAX_BRUSH_SIZE;
/// A resize drag in progress.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -200,7 +201,7 @@ mod tests {
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), -1e9, -1e9, 1.0), (1.0, 0.0));
- assert_eq!(resized((40.0, 0.5), 1e9, 1e9, 1.0), (5000.0, 1.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));
assert!(is_gesture(CTRL_ALT) && !is_gesture(Modifiers::ALT) && !is_gesture(Modifiers::CTRL) && !is_gesture(Modifiers::SHIFT));
}
diff --git a/crates/ui-egui/src/brush_sections.rs b/crates/ui-egui/src/brush_sections.rs
index e14e14a7c..7e1076a03 100644
--- a/crates/ui-egui/src/brush_sections.rs
+++ b/crates/ui-egui/src/brush_sections.rs
@@ -5,7 +5,7 @@
use egui::{Color32, RichText, Sense, Stroke, pos2, vec2};
use photocraft_engine::BrushSettings;
-use photocraft_engine::paint::{self, BrushPreset, Control, Dynamic, MaskMode, Pattern, PatternStyle, TipShape};
+use photocraft_engine::paint::{self, BrushPreset, Control, Dynamic, MAX_BRUSH_SIZE, MaskMode, Pattern, PatternStyle, TipShape};
use crate::brush_preview;
use crate::theme::{self, Tokens};
@@ -295,7 +295,7 @@ fn tip_shape(ui: &mut egui::Ui, b: &mut BrushSettings, presets: &[BrushPreset])
TipShape::Sampled(g) => Some(g.width.max(g.height) as f32),
TipShape::Round => None,
};
- size_row(ui, tl!("Size"), &mut b.size, 5000.0, orig);
+ size_row(ui, tl!("Size"), &mut b.size, MAX_BRUSH_SIZE, orig);
ui.horizontal(|ui| {
widgets::checkbox(ui, &mut b.flip_x, tl!("Flip X"));
ui.add_space(8.0);
diff --git a/crates/ui-egui/src/brushes_tab.rs b/crates/ui-egui/src/brushes_tab.rs
index e0205bf33..e84586cbd 100644
--- a/crates/ui-egui/src/brushes_tab.rs
+++ b/crates/ui-egui/src/brushes_tab.rs
@@ -7,7 +7,7 @@
//! command, so it is journaled, drivable, and persisted by the preset store.
use egui::{Color32, RichText, Sense, Stroke, pos2, vec2};
-use photocraft_engine::paint::BrushPreset;
+use photocraft_engine::paint::{BrushPreset, MAX_BRUSH_SIZE};
use serde_json::json;
use crate::brush_panel::{BrushesView, Renaming, UNGROUPED, WIDTH, commit_gesture, full_uv, grouped_presets, is_current, new_preset_name, run_or_status};
@@ -293,15 +293,15 @@ pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.label(RichText::new(tl!("Size")).color(t.text_dim));
let mut lv = b.size.max(1.0).ln();
ui.add_sized(vec2(WIDTH - 140.0, 18.0), |ui: &mut egui::Ui| {
- let r = widgets::slider(ui, &mut lv, 0.0..=5000f32.ln(), None);
+ let r = widgets::slider(ui, &mut lv, 0.0..=MAX_BRUSH_SIZE.ln(), None);
if r.changed() {
- b.size = lv.exp().round().clamp(1.0, 5000.0);
+ b.size = lv.exp().round().clamp(1.0, MAX_BRUSH_SIZE);
}
r
});
let mut s = b.size;
- if widgets::value_field(ui, &mut s, 1.0..=5000.0, "px", 74.0).changed() {
- b.size = s.round().clamp(1.0, 5000.0);
+ if widgets::value_field(ui, &mut s, 1.0..=MAX_BRUSH_SIZE, "px", 74.0).changed() {
+ b.size = s.round().clamp(1.0, MAX_BRUSH_SIZE);
}
});
commit_gesture(app, ui.ctx(), &before, &b);
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index d05eb3d15..981476d76 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -485,7 +485,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
widgets::vline(ui, 22.0);
if !t.pro {
opt_label(ui, tl!("Size"));
- widgets::value_field(ui, &mut b.size, 1.0..=5000.0, "px", 76.0);
+ widgets::value_field(ui, &mut b.size, 1.0..=photocraft_engine::paint::MAX_BRUSH_SIZE, "px", 76.0);
widgets::vline(ui, 22.0);
}
opt_label(ui, tl!("Mode"));