@@ -28,6 +28,11 @@ pub struct Painter {
28
28
/// If set, all shapes will have their colors modified to be closer to this.
29
29
/// This is used to implement grayed out interfaces.
30
30
fade_to_color : Option < Color32 > ,
31
+
32
+ /// If set, all shapes will have their colors modified with [`Color32::gamma_multiply`] with
33
+ /// this value as the factor.
34
+ /// This is used to make interfaces semi-transparent.
35
+ opacity_factor : f32 ,
31
36
}
32
37
33
38
impl Painter {
@@ -38,6 +43,7 @@ impl Painter {
38
43
layer_id,
39
44
clip_rect,
40
45
fade_to_color : None ,
46
+ opacity_factor : 1.0 ,
41
47
}
42
48
}
43
49
@@ -49,6 +55,7 @@ impl Painter {
49
55
layer_id,
50
56
clip_rect : self . clip_rect ,
51
57
fade_to_color : None ,
58
+ opacity_factor : 1.0 ,
52
59
}
53
60
}
54
61
@@ -62,6 +69,7 @@ impl Painter {
62
69
layer_id : self . layer_id ,
63
70
clip_rect : rect. intersect ( self . clip_rect ) ,
64
71
fade_to_color : self . fade_to_color ,
72
+ opacity_factor : self . opacity_factor ,
65
73
}
66
74
}
67
75
@@ -75,6 +83,12 @@ impl Painter {
75
83
self . fade_to_color = fade_to_color;
76
84
}
77
85
86
+ pub ( crate ) fn set_opacity ( & mut self , opacity : f32 ) {
87
+ if opacity. is_finite ( ) {
88
+ self . opacity_factor = opacity. clamp ( 0.0 , 1.0 ) ;
89
+ }
90
+ }
91
+
78
92
pub ( crate ) fn is_visible ( & self ) -> bool {
79
93
self . fade_to_color != Some ( Color32 :: TRANSPARENT )
80
94
}
@@ -151,13 +165,16 @@ impl Painter {
151
165
if let Some ( fade_to_color) = self . fade_to_color {
152
166
tint_shape_towards ( shape, fade_to_color) ;
153
167
}
168
+ if self . opacity_factor < 1.0 {
169
+ multiply_opacity ( shape, self . opacity_factor ) ;
170
+ }
154
171
}
155
172
156
173
/// It is up to the caller to make sure there is room for this.
157
174
/// Can be used for free painting.
158
175
/// NOTE: all coordinates are screen coordinates!
159
176
pub fn add ( & self , shape : impl Into < Shape > ) -> ShapeIdx {
160
- if self . fade_to_color == Some ( Color32 :: TRANSPARENT ) {
177
+ if self . fade_to_color == Some ( Color32 :: TRANSPARENT ) || self . opacity_factor == 0.0 {
161
178
self . paint_list ( |l| l. add ( self . clip_rect , Shape :: Noop ) )
162
179
} else {
163
180
let mut shape = shape. into ( ) ;
@@ -170,18 +187,18 @@ impl Painter {
170
187
///
171
188
/// Calling this once is generally faster than calling [`Self::add`] multiple times.
172
189
pub fn extend < I : IntoIterator < Item = Shape > > ( & self , shapes : I ) {
173
- if self . fade_to_color == Some ( Color32 :: TRANSPARENT ) {
190
+ if self . fade_to_color == Some ( Color32 :: TRANSPARENT ) || self . opacity_factor == 0.0 {
174
191
return ;
175
192
}
176
- if self . fade_to_color . is_some ( ) {
193
+ if self . fade_to_color . is_some ( ) || self . opacity_factor < 1.0 {
177
194
let shapes = shapes. into_iter ( ) . map ( |mut shape| {
178
195
self . transform_shape ( & mut shape) ;
179
196
shape
180
197
} ) ;
181
198
self . paint_list ( |l| l. extend ( self . clip_rect , shapes) ) ;
182
199
} else {
183
200
self . paint_list ( |l| l. extend ( self . clip_rect , shapes) ) ;
184
- } ;
201
+ }
185
202
}
186
203
187
204
/// Modify an existing [`Shape`].
@@ -496,3 +513,11 @@ fn tint_shape_towards(shape: &mut Shape, target: Color32) {
496
513
}
497
514
} ) ;
498
515
}
516
+
517
+ fn multiply_opacity ( shape : & mut Shape , opacity : f32 ) {
518
+ epaint:: shape_transform:: adjust_colors ( shape, & |color| {
519
+ if * color != Color32 :: PLACEHOLDER {
520
+ * color = color. gamma_multiply ( opacity) ;
521
+ }
522
+ } ) ;
523
+ }
0 commit comments