From 945a69d2f20b95d75b2e63c06a8c247080b25ef5 Mon Sep 17 00:00:00 2001 From: StratusFearMe21 <57533634+StratusFearMe21@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:55:18 +0000 Subject: [PATCH] Add opacity factor to `TextShape` (#3916) This PR simply allows you to override the opacity of a Galley when you draw it on screen. Last year I opened #3548 and some changes were requested to the PR, but unfortunately school got really busy and I wasn't able to apply them. This PR supersedes #3548 and applys the changes requested in that PR Closes #3548 --- crates/epaint/src/shape.rs | 12 ++++++++++++ crates/epaint/src/shape_transform.rs | 1 + crates/epaint/src/tessellator.rs | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/crates/epaint/src/shape.rs b/crates/epaint/src/shape.rs index 61b9d75e92b..ceb5dd072e5 100644 --- a/crates/epaint/src/shape.rs +++ b/crates/epaint/src/shape.rs @@ -745,6 +745,10 @@ pub struct TextShape { /// This only affects the glyphs and will NOT replace background color nor strikethrough/underline color. pub override_text_color: Option, + /// If set, the text will be rendered with the given opacity in gamma space + /// Affects everything: backgrounds, glyphs, strikethough, underline, etc. + pub opacity_factor: f32, + /// Rotate text by this many radians clockwise. /// The pivot is `pos` (the upper left corner of the text). pub angle: f32, @@ -762,6 +766,7 @@ impl TextShape { underline: Stroke::NONE, fallback_color, override_text_color: None, + opacity_factor: 1.0, angle: 0.0, } } @@ -792,6 +797,13 @@ impl TextShape { self.angle = angle; self } + + /// Render text with this opacity in gamma space + #[inline] + pub fn with_opacity_factor(mut self, opacity_factor: f32) -> Self { + self.opacity_factor = opacity_factor; + self + } } impl From for Shape { diff --git a/crates/epaint/src/shape_transform.rs b/crates/epaint/src/shape_transform.rs index b36accb5a3f..c8edff1fcaf 100644 --- a/crates/epaint/src/shape_transform.rs +++ b/crates/epaint/src/shape_transform.rs @@ -56,6 +56,7 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) { underline, fallback_color, override_text_color, + opacity_factor: _, angle: _, }) => { adjust_color(&mut underline.color); diff --git a/crates/epaint/src/tessellator.rs b/crates/epaint/src/tessellator.rs index 72899f2844c..bc729045dd7 100644 --- a/crates/epaint/src/tessellator.rs +++ b/crates/epaint/src/tessellator.rs @@ -1474,6 +1474,7 @@ impl Tessellator { underline, override_text_color, fallback_color, + opacity_factor, angle, } = text_shape; @@ -1481,6 +1482,10 @@ impl Tessellator { return; } + if *opacity_factor <= 0.0 { + return; + } + if galley.pixels_per_point != self.pixels_per_point { eprintln!("epaint: WARNING: pixels_per_point (dpi scale) have changed between text layout and tessellation. \ You must recreate your text shapes if pixels_per_point changes."); @@ -1548,6 +1553,10 @@ impl Tessellator { color = *fallback_color; } + if *opacity_factor < 1.0 { + color = color.gamma_multiply(*opacity_factor); + } + crate::epaint_assert!(color != Color32::PLACEHOLDER, "A placeholder color made it to the tessellator. You forgot to set a fallback color."); let offset = if *angle == 0.0 {