Skip to content

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

Layer Style: the shadow/bevel Angle slider now moves the light (#350) (#411)

Alex Gorevski6237f6e46e3e
diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv
index 19305c6ad..e429b48b6 100644
--- a/crates/ui-egui/src/i18n/cs.tsv
+++ b/crates/ui-egui/src/i18n/cs.tsv
@@ -265,6 +265,7 @@
Layer Style Styl vrstvy
Blending Options… Možnosti prolnutí…
Bevel & Emboss… Úkos a reliéf…
+ Use Global Light Použít globální světlo
Inner Shadow… Vnitřní stín…
Inner Glow… Vnitřní záře…
Satin… Satén…
diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv
index d57feccfc..9ac979c1f 100644
--- a/crates/ui-egui/src/i18n/es.tsv
+++ b/crates/ui-egui/src/i18n/es.tsv
@@ -257,6 +257,7 @@
Layer Style Estilo de capa
Blending Options… Opciones de fusión…
Bevel & Emboss… Bisel y relieve…
+ Use Global Light Usar luz global
Inner Shadow… Sombra interior…
Inner Glow… Resplandor interior…
Satin… Satinado…
diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv
index 8f6be3f60..39de8b8a6 100644
--- a/crates/ui-egui/src/i18n/ja.tsv
+++ b/crates/ui-egui/src/i18n/ja.tsv
@@ -261,6 +261,7 @@
Layer Style レイヤースタイル
Blending Options… レイヤー効果の描画オプション…
Bevel & Emboss… ベベルとエンボス…
+ Use Global Light グローバルライトを使用
Inner Shadow… シャドウ(内側)…
Inner Glow… 光彩(内側)…
Satin… サテン…
diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv
index 74a1235d1..a452c90dd 100644
--- a/crates/ui-egui/src/i18n/ru.tsv
+++ b/crates/ui-egui/src/i18n/ru.tsv
@@ -186,6 +186,7 @@
Outer Glow… Внешнее свечение…
Inner Glow… Внутреннее свечение…
Bevel & Emboss… Фаска и рельеф…
+ Use Global Light Использовать глобальный свет
Satin… Глянец…
Color Overlay… Наложение цвета…
Gradient Overlay… Наложение градиента…
diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv
index 41027088f..4791dcbd9 100644
--- a/crates/ui-egui/src/i18n/zh-hans.tsv
+++ b/crates/ui-egui/src/i18n/zh-hans.tsv
@@ -260,6 +260,7 @@
Layer Style 图层样式
Blending Options… 混合选项…
Bevel & Emboss… 斜面和浮雕…
+ Use Global Light 使用全局光
Inner Shadow… 内阴影…
Inner Glow… 内发光…
Satin… 光泽…
diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv
index 6f6c23d1e..7263c17f0 100644
--- a/crates/ui-egui/src/i18n/zh-hant.tsv
+++ b/crates/ui-egui/src/i18n/zh-hant.tsv
@@ -265,6 +265,7 @@
Layer Style 圖層樣式
Blending Options… 混合選項…
Bevel & Emboss… 斜角和浮雕…
+ Use Global Light 使用全域光
Inner Shadow… 內陰影…
Inner Glow… 內光暈…
Satin… 緞面…
diff --git a/crates/ui-egui/src/layer_style.rs b/crates/ui-egui/src/layer_style.rs
index 1974c9d4c..276a62b85 100644
--- a/crates/ui-egui/src/layer_style.rs
+++ b/crates/ui-egui/src/layer_style.rs
@@ -49,6 +49,7 @@ fn spec(kind: &str) -> &'static [(&'static str, &'static str, P)] {
("color", "Color", P::Color),
("opacity", "Opacity", P::Slider(0.0, 100.0, "%")),
("angle", "Angle", P::Slider(-180.0, 180.0, "°")),
+ ("useGlobalLight", "Use Global Light", P::Check),
("distance", "Distance", P::Slider(0.0, 300.0, "px")),
("spread", "Spread", P::Slider(0.0, 100.0, "%")),
("size", "Size", P::Slider(0.0, 250.0, "px")),
@@ -59,6 +60,7 @@ fn spec(kind: &str) -> &'static [(&'static str, &'static str, P)] {
("color", "Color", P::Color),
("opacity", "Opacity", P::Slider(0.0, 100.0, "%")),
("angle", "Angle", P::Slider(-180.0, 180.0, "°")),
+ ("useGlobalLight", "Use Global Light", P::Check),
("distance", "Distance", P::Slider(0.0, 300.0, "px")),
("choke", "Choke", P::Slider(0.0, 100.0, "%")),
("size", "Size", P::Slider(0.0, 250.0, "px")),
@@ -112,6 +114,7 @@ fn spec(kind: &str) -> &'static [(&'static str, &'static str, P)] {
("size", "Size", P::Slider(0.0, 250.0, "px")),
("soften", "Soften", P::Slider(0.0, 16.0, "px")),
("angle", "Angle", P::Slider(-180.0, 180.0, "°")),
+ ("useGlobalLight", "Use Global Light", P::Check),
("altitude", "Altitude", P::Slider(0.0, 90.0, "°")),
],
"satin" => &[
@@ -134,8 +137,12 @@ fn spec(kind: &str) -> &'static [(&'static str, &'static str, P)] {
fn defaults(kind: &str) -> Value {
match kind {
- "dropShadow" => json!({"blend": "Multiply", "color": "#000000", "opacity": 75, "angle": 120, "distance": 5, "spread": 0, "size": 5, "knocksOut": true}),
- "innerShadow" => json!({"blend": "Multiply", "color": "#000000", "opacity": 75, "angle": 120, "distance": 5, "choke": 0, "size": 5}),
+ "dropShadow" => {
+ json!({"blend": "Multiply", "color": "#000000", "opacity": 75, "angle": 120, "useGlobalLight": true, "distance": 5, "spread": 0, "size": 5, "knocksOut": true})
+ }
+ "innerShadow" => {
+ json!({"blend": "Multiply", "color": "#000000", "opacity": 75, "angle": 120, "useGlobalLight": true, "distance": 5, "choke": 0, "size": 5})
+ }
"outerGlow" => json!({"blend": "Screen", "opacity": 75, "color": "#ffffbe", "spread": 0, "size": 5, "range": 50}),
"innerGlow" => json!({"blend": "Screen", "opacity": 75, "color": "#ffffbe", "source": "edge", "choke": 0, "size": 5}),
"stroke" => json!({"size": 3, "position": "outside", "blend": "Normal", "opacity": 100, "color": "#000000"}),
@@ -144,7 +151,9 @@ fn defaults(kind: &str) -> Value {
json!({"blend": "Normal", "opacity": 100, "from": "#000000", "to": "#ffffff", "reverse": false, "style": "linear", "angle": 90, "scale": 100})
}
"patternOverlay" => json!({"blend": "Normal", "opacity": 100, "pattern": "", "angle": 0, "scale": 100, "link": true}),
- "bevelEmboss" => json!({"style": "inner", "depth": 100, "direction": "up", "size": 5, "soften": 0, "angle": 120, "altitude": 30}),
+ "bevelEmboss" => {
+ json!({"style": "inner", "depth": 100, "direction": "up", "size": 5, "soften": 0, "angle": 120, "useGlobalLight": true, "altitude": 30})
+ }
"satin" => json!({"blend": "Multiply", "color": "#000000", "opacity": 50, "angle": 19, "distance": 11, "size": 14, "invert": true}),
_ => json!({}),
}
@@ -171,7 +180,8 @@ fn kind_of(e: &Effect) -> &'static str {
}
/// Current values of an existing effect, in the command's parameter units.
-fn values_of(e: &Effect) -> Value {
+/// `light` is the document's global light angle: an effect that uses it shows that angle.
+fn values_of(e: &Effect, light: f32) -> Value {
let mut v = defaults(kind_of(e));
let set = |v: &mut Value, k: &str, x: Value| {
v[k] = x;
@@ -181,7 +191,8 @@ fn values_of(e: &Effect) -> Value {
set(&mut v, "blend", json!(s.common.blend.label()));
set(&mut v, "opacity", json!((s.common.opacity * 100.0).round()));
set(&mut v, "color", json!(hex(&s.color)));
- set(&mut v, "angle", json!(s.angle));
+ set(&mut v, "angle", json!(if s.use_global_light { light } else { s.angle }));
+ set(&mut v, "useGlobalLight", json!(s.use_global_light));
set(&mut v, "distance", json!(s.distance));
set(&mut v, if matches!(e, Effect::DropShadow(_)) { "spread" } else { "choke" }, json!((s.spread * 100.0).round()));
set(&mut v, "size", json!(s.size));
@@ -235,7 +246,8 @@ fn values_of(e: &Effect) -> Value {
set(&mut v, "depth", json!(b.depth));
set(&mut v, "size", json!(b.size));
set(&mut v, "soften", json!(b.soften));
- set(&mut v, "angle", json!(b.angle));
+ set(&mut v, "angle", json!(if b.use_global_light { light } else { b.angle }));
+ set(&mut v, "useGlobalLight", json!(b.use_global_light));
set(&mut v, "altitude", json!(b.altitude));
set(&mut v, "direction", json!(if b.up { "up" } else { "down" }));
}
@@ -254,9 +266,11 @@ fn values_of(e: &Effect) -> Value {
}
/// Dialog fields for a layer: selected kind, and per kind {enabled, params}.
-pub fn initial_fields(layer: &Layer, select: Option<&str>) -> Map<String, Value> {
+/// `light` is the document's global light angle, shown by effects that use it.
+pub fn initial_fields(layer: &Layer, select: Option<&str>, light: f32) -> Map<String, Value> {
let mut f = Map::new();
f.insert("layer".into(), json!(layer.id.0));
+ f.insert("globalLight".into(), json!(light));
f.insert(
format!("p:{BLENDING}"),
json!({"blend": layer.blend.label(), "opacity": (layer.opacity * 100.0).round(), "fillOpacity": (layer.fill_opacity * 100.0).round()}),
@@ -268,7 +282,14 @@ pub fn initial_fields(layer: &Layer, select: Option<&str>) -> Map<String, Value>
first = Some(kind);
}
f.insert(format!("on:{kind}"), json!(existing.is_some_and(|e| e.enabled())));
- f.insert(format!("p:{kind}"), existing.map(values_of).unwrap_or_else(|| defaults(kind)));
+ let params = existing.map(|e| values_of(e, light)).unwrap_or_else(|| {
+ let mut params = defaults(kind);
+ if params.get("useGlobalLight").and_then(Value::as_bool) == Some(true) {
+ params["angle"] = json!(light);
+ }
+ params
+ });
+ f.insert(format!("p:{kind}"), params);
}
let sel = select.or(first).unwrap_or("dropShadow");
f.insert("selected".into(), json!(sel));
@@ -281,7 +302,8 @@ pub fn initial_fields(layer: &Layer, select: Option<&str>) -> Map<String, Value>
pub fn open(app: &mut PhotocraftApp, select: Option<&str>) -> Option<u64> {
let st = app.session.active()?;
let layer = st.doc.layer(st.active_layer?)?.clone();
- let mut f = initial_fields(&layer, select);
+ let light = st.doc.global_light.angle;
+ let mut f = initial_fields(&layer, select, light);
f.insert("patternList".into(), pattern_list(app));
Some(app.ui.open_dialog(crate::state::DialogKind::LayerStyle, f))
}
@@ -307,19 +329,32 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result<Value,
/// Runs the dialog's commands through `run`: blending options, clear, then each enabled effect.
fn apply(f: &Map<String, Value>, mut run: impl FnMut(&str, Value) -> Result<Value, String>) -> Result<Value, String> {
let layer = f.get("layer").cloned().unwrap_or(Value::Null);
+ let initial_light_angle = f.get("globalLight").and_then(Value::as_f64);
if let Some(Value::Object(bo)) = f.get(&format!("p:{BLENDING}")) {
let mut p = Value::Object(bo.clone());
p["layer"] = layer.clone();
run("layer.layerStyle.blendingOptions", p)?;
}
let _ = run("layer.layerStyle.clear", json!({"layer": layer}));
+ // An effect lit by the global light follows the document's light angle, so the Angle slider
+ // drives that shared angle rather than the per-effect angle the compositor ignores.
+ let mut light_angle: Option<f64> = None;
for &(kind, _) in KINDS {
if f.get(&format!("on:{kind}")).and_then(Value::as_bool) == Some(true) {
let mut p = f.get(&format!("p:{kind}")).cloned().unwrap_or_else(|| json!({}));
p["layer"] = layer.clone();
+ if p.get("useGlobalLight").and_then(Value::as_bool) == Some(true)
+ && let Some(angle) = p.get("angle").and_then(Value::as_f64)
+ && initial_light_angle.is_none_or(|initial| angle != initial)
+ {
+ light_angle = Some(angle);
+ }
run(&format!("layer.layerStyle.{kind}"), p)?;
}
}
+ if let Some(angle) = light_angle {
+ run("layer.layerStyle.globalLight", json!({"angle": angle}))?;
+ }
Ok(Value::Null)
}
@@ -517,7 +552,8 @@ mod tests {
#[test]
fn initial_fields_select_requested_kind() {
let l = Layer::raster("x", photocraft_doc::PixelFormat::RGBA8);
- let f = initial_fields(&l, Some("stroke"));
+ let f = initial_fields(&l, Some("stroke"), 120.0);
+ assert_eq!(f["globalLight"], json!(120.0));
assert_eq!(f["selected"], "stroke");
assert_eq!(f["on:stroke"], true);
assert_eq!(f["on:dropShadow"], false);
@@ -529,7 +565,7 @@ mod tests {
s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
let st = s.active().unwrap();
- let mut f = initial_fields(st.doc.layer(st.active_layer.unwrap()).unwrap(), Some("colorOverlay"));
+ let mut f = initial_fields(st.doc.layer(st.active_layer.unwrap()).unwrap(), Some("colorOverlay"), st.doc.global_light.angle);
let h = preview_hash(&f);
f.insert("selected".into(), json!("stroke"));
assert_eq!(preview_hash(&f), h, "switching pages doesn't re-render");
@@ -549,9 +585,89 @@ mod tests {
s.execute("layer.layerStyle.dropShadow", json!({"spread": 12, "add": true})).unwrap();
let st = s.active().unwrap();
let l = st.doc.layer(st.active_layer.unwrap()).unwrap();
- let f = initial_fields(l, None);
+ let f = initial_fields(l, None, st.doc.global_light.angle);
assert_eq!(f["p:outerGlow"]["spread"], json!(6.0));
assert_eq!(f["p:outerGlow"]["range"], json!(40.0));
assert_eq!(f["p:dropShadow"]["spread"], json!(12.0));
}
+
+ #[test]
+ fn drop_shadow_angle_reaches_the_effect() {
+ // #350: the Angle slider was dead — the dialog never sent `useGlobalLight`, so the engine
+ // defaulted it to true and the compositor used the fixed global light angle, ignoring the slider.
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let id = s.active().unwrap().active_layer.unwrap();
+ let shadow = |s: &photocraft_engine::Session| {
+ s.active().unwrap().doc.layer(id).unwrap().effects.items.iter().find_map(|e| match e {
+ Effect::DropShadow(sh) => Some(sh.clone()),
+ _ => None,
+ })
+ };
+
+ // Use Global Light off: the per-effect angle is stored and used.
+ let mut f = Map::new();
+ f.insert("layer".into(), json!(id));
+ f.insert("on:dropShadow".into(), json!(true));
+ f.insert("p:dropShadow".into(), json!({"angle": 45.0, "useGlobalLight": false, "distance": 5, "size": 5}));
+ apply(&f, |cmd, p| s.execute(cmd, p).map_err(|e| e.to_string())).unwrap();
+ let eff = shadow(&s).unwrap();
+ assert!(!eff.use_global_light, "Use Global Light off keeps the per-effect angle");
+ assert_eq!(eff.angle, 45.0);
+
+ // Use Global Light on: the Angle slider drives the document's shared light angle.
+ let mut f = Map::new();
+ f.insert("layer".into(), json!(id));
+ f.insert("on:dropShadow".into(), json!(true));
+ f.insert("p:dropShadow".into(), json!({"angle": 30.0, "useGlobalLight": true, "distance": 5, "size": 5}));
+ apply(&f, |cmd, p| s.execute(cmd, p).map_err(|e| e.to_string())).unwrap();
+ assert_eq!(s.active().unwrap().doc.global_light.angle, 30.0, "the Angle slider moves the shared light");
+ assert!(shadow(&s).unwrap().use_global_light);
+
+ // Reopening the dialog shows the effective angle and the checkbox state.
+ let st = s.active().unwrap();
+ let f = initial_fields(st.doc.layer(id).unwrap(), None, st.doc.global_light.angle);
+ assert_eq!(f["p:dropShadow"]["useGlobalLight"], json!(true));
+ assert_eq!(f["p:dropShadow"]["angle"], json!(30.0));
+ }
+
+ #[test]
+ fn bevel_angle_change_is_not_overridden_by_unchanged_drop_shadow_angle() {
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let (id, mut f, initial_light) = {
+ let st = s.active().unwrap();
+ let id = st.active_layer.unwrap();
+ let f = initial_fields(st.doc.layer(id).unwrap(), None, st.doc.global_light.angle);
+ (id, f, st.doc.global_light.angle)
+ };
+ f.insert("on:bevelEmboss".into(), json!(true));
+ f["p:bevelEmboss"]["angle"] = json!(45.0);
+ f.insert("on:dropShadow".into(), json!(true));
+ assert_eq!(f["p:dropShadow"]["angle"].as_f64(), Some(f64::from(initial_light)));
+
+ apply(&f, |cmd, p| s.execute(cmd, p).map_err(|e| e.to_string())).unwrap();
+
+ assert_eq!(s.active().unwrap().doc.global_light.angle, 45.0);
+ assert_eq!(s.active().unwrap().doc.layer(id).unwrap().effects.items.len(), 2);
+ }
+
+ #[test]
+ fn unchanged_global_light_angle_is_not_applied() {
+ let layer = Layer::raster("x", photocraft_doc::PixelFormat::RGBA8);
+ let mut f = initial_fields(&layer, None, 47.5);
+ f.insert("on:bevelEmboss".into(), json!(true));
+ assert_eq!(f["p:bevelEmboss"]["angle"].as_f64(), Some(47.5));
+ let mut commands = Vec::new();
+
+ apply(&f, |cmd, _| {
+ commands.push(cmd.to_string());
+ Ok(Value::Null)
+ })
+ .unwrap();
+
+ assert!(!commands.iter().any(|cmd| cmd == "layer.layerStyle.globalLight"));
+ }
}