Skip to content

Commit

Permalink
Make opacity non-optional in painter with default = 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
YgorSouza committed Feb 5, 2024
1 parent 23cc068 commit 72a1d33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Painter {
/// If set, all shapes will have their colors modified with [`Color32::gamma_multiply`] with
/// this value as the factor.
/// This is used to make interfaces semi-transparent.
opacity: Option<f32>,
opacity_factor: f32,
}

impl Painter {
Expand All @@ -43,7 +43,7 @@ impl Painter {
layer_id,
clip_rect,
fade_to_color: None,
opacity: None,
opacity_factor: 1.0,
}
}

Expand All @@ -55,7 +55,7 @@ impl Painter {
layer_id,
clip_rect: self.clip_rect,
fade_to_color: None,
opacity: None,
opacity_factor: 1.0,
}
}

Expand All @@ -69,7 +69,7 @@ impl Painter {
layer_id: self.layer_id,
clip_rect: rect.intersect(self.clip_rect),
fade_to_color: self.fade_to_color,
opacity: self.opacity,
opacity_factor: self.opacity_factor,
}
}

Expand All @@ -83,8 +83,10 @@ impl Painter {
self.fade_to_color = fade_to_color;
}

pub(crate) fn set_opacity(&mut self, opacity: Option<f32>) {
self.opacity = opacity.filter(|f| f.is_finite()).map(|f| f.clamp(0.0, 1.0));
pub(crate) fn set_opacity(&mut self, opacity: f32) {
if opacity.is_finite() {
self.opacity_factor = opacity.clamp(0.0, 1.0);
}
}

pub(crate) fn is_visible(&self) -> bool {
Expand Down Expand Up @@ -163,8 +165,8 @@ impl Painter {
if let Some(fade_to_color) = self.fade_to_color {
tint_shape_towards(shape, fade_to_color);
}
if let Some(opacity) = self.opacity {
set_shape_opacity(shape, opacity);
if self.opacity_factor < 1.0 {
multiply_opacity(shape, self.opacity_factor);
}
}

Expand All @@ -185,10 +187,12 @@ impl Painter {
///
/// Calling this once is generally faster than calling [`Self::add`] multiple times.
pub fn extend<I: IntoIterator<Item = Shape>>(&self, shapes: I) {
match (self.fade_to_color, self.opacity) {
(None, None) => self.paint_list(|l| l.extend(self.clip_rect, shapes)),
match (self.fade_to_color, self.opacity_factor) {
(None, opacity) if opacity == 1.0 => {
self.paint_list(|l| l.extend(self.clip_rect, shapes));
}
(Some(Color32::TRANSPARENT), _) => (),
(_, Some(_)) | (Some(_), _) => {
_ => {
let shapes = shapes.into_iter().map(|mut shape| {
self.transform_shape(&mut shape);
shape
Expand Down Expand Up @@ -511,7 +515,7 @@ fn tint_shape_towards(shape: &mut Shape, target: Color32) {
});
}

fn set_shape_opacity(shape: &mut Shape, opacity: f32) {
fn multiply_opacity(shape: &mut Shape, opacity: f32) {
epaint::shape_transform::adjust_colors(shape, &|color| {
if *color != Color32::PLACEHOLDER {
*color = color.gamma_multiply(opacity);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl Ui {
/// # });
/// ```
pub fn set_opacity(&mut self, opacity: f32) {
self.painter.set_opacity(Some(opacity));
self.painter.set_opacity(opacity);
}

/// Read the [`Layout`].
Expand Down

0 comments on commit 72a1d33

Please sign in to comment.