1
1
use std:: sync:: Arc ;
2
2
3
+ use emath:: GuiRounding as _;
4
+ use epaint:: {
5
+ text:: { Fonts , Galley , LayoutJob } ,
6
+ CircleShape , ClippedShape , PathStroke , RectShape , Rounding , Shape , Stroke ,
7
+ } ;
8
+
3
9
use crate :: {
4
10
emath:: { Align2 , Pos2 , Rangef , Rect , Vec2 } ,
5
11
layers:: { LayerId , PaintList , ShapeIdx } ,
6
12
Color32 , Context , FontId ,
7
13
} ;
8
- use epaint:: {
9
- text:: { Fonts , Galley , LayoutJob } ,
10
- CircleShape , ClippedShape , PathStroke , RectShape , Rounding , Shape , Stroke ,
11
- } ;
12
14
13
15
/// Helper to paint shapes and text to a specific region on a specific layer.
14
16
///
15
17
/// All coordinates are screen coordinates in the unit points (one point can consist of many physical pixels).
18
+ ///
19
+ /// A [`Painter`] never outlive a single frame/pass.
16
20
#[ derive( Clone ) ]
17
21
pub struct Painter {
18
22
/// Source of fonts and destination of shapes
19
23
ctx : Context ,
20
24
25
+ /// For quick access, without having to go via [`Context`].
26
+ pixels_per_point : f32 ,
27
+
21
28
/// Where we paint
22
29
layer_id : LayerId ,
23
30
@@ -38,8 +45,10 @@ pub struct Painter {
38
45
impl Painter {
39
46
/// Create a painter to a specific layer within a certain clip rectangle.
40
47
pub fn new ( ctx : Context , layer_id : LayerId , clip_rect : Rect ) -> Self {
48
+ let pixels_per_point = ctx. pixels_per_point ( ) ;
41
49
Self {
42
50
ctx,
51
+ pixels_per_point,
43
52
layer_id,
44
53
clip_rect,
45
54
fade_to_color : None ,
@@ -49,28 +58,20 @@ impl Painter {
49
58
50
59
/// Redirect where you are painting.
51
60
#[ must_use]
52
- pub fn with_layer_id ( self , layer_id : LayerId ) -> Self {
53
- Self {
54
- ctx : self . ctx ,
55
- layer_id,
56
- clip_rect : self . clip_rect ,
57
- fade_to_color : None ,
58
- opacity_factor : 1.0 ,
59
- }
61
+ #[ inline]
62
+ pub fn with_layer_id ( mut self , layer_id : LayerId ) -> Self {
63
+ self . layer_id = layer_id;
64
+ self
60
65
}
61
66
62
67
/// Create a painter for a sub-region of this [`Painter`].
63
68
///
64
69
/// The clip-rect of the returned [`Painter`] will be the intersection
65
70
/// of the given rectangle and the `clip_rect()` of the parent [`Painter`].
66
71
pub fn with_clip_rect ( & self , rect : Rect ) -> Self {
67
- Self {
68
- ctx : self . ctx . clone ( ) ,
69
- layer_id : self . layer_id ,
70
- clip_rect : rect. intersect ( self . clip_rect ) ,
71
- fade_to_color : self . fade_to_color ,
72
- opacity_factor : self . opacity_factor ,
73
- }
72
+ let mut new_self = self . clone ( ) ;
73
+ new_self. clip_rect = rect. intersect ( self . clip_rect ) ;
74
+ new_self
74
75
}
75
76
76
77
/// Redirect where you are painting.
@@ -82,7 +83,7 @@ impl Painter {
82
83
}
83
84
84
85
/// If set, colors will be modified to look like this
85
- pub ( crate ) fn set_fade_to_color ( & mut self , fade_to_color : Option < Color32 > ) {
86
+ pub fn set_fade_to_color ( & mut self , fade_to_color : Option < Color32 > ) {
86
87
self . fade_to_color = fade_to_color;
87
88
}
88
89
@@ -118,24 +119,27 @@ impl Painter {
118
119
/// If `false`, nothing you paint will show up.
119
120
///
120
121
/// Also checks [`Context::will_discard`].
121
- pub ( crate ) fn is_visible ( & self ) -> bool {
122
+ pub fn is_visible ( & self ) -> bool {
122
123
self . fade_to_color != Some ( Color32 :: TRANSPARENT ) && !self . ctx . will_discard ( )
123
124
}
124
125
125
126
/// If `false`, nothing added to the painter will be visible
126
- pub ( crate ) fn set_invisible ( & mut self ) {
127
+ pub fn set_invisible ( & mut self ) {
127
128
self . fade_to_color = Some ( Color32 :: TRANSPARENT ) ;
128
129
}
129
- }
130
130
131
- /// ## Accessors etc
132
- impl Painter {
133
131
/// Get a reference to the parent [`Context`].
134
132
#[ inline]
135
133
pub fn ctx ( & self ) -> & Context {
136
134
& self . ctx
137
135
}
138
136
137
+ /// Number of physical pixels for each logical UI point.
138
+ #[ inline]
139
+ pub fn pixels_per_point ( & self ) -> f32 {
140
+ self . pixels_per_point
141
+ }
142
+
139
143
/// Read-only access to the shared [`Fonts`].
140
144
///
141
145
/// See [`Context`] documentation for how locks work.
@@ -180,37 +184,42 @@ impl Painter {
180
184
/// Useful for pixel-perfect rendering of lines that are one pixel wide (or any odd number of pixels).
181
185
#[ inline]
182
186
pub fn round_to_pixel_center ( & self , point : f32 ) -> f32 {
183
- self . ctx ( ) . round_to_pixel_center ( point )
187
+ point . round_to_pixel_center ( self . pixels_per_point ( ) )
184
188
}
185
189
186
190
/// Useful for pixel-perfect rendering of lines that are one pixel wide (or any odd number of pixels).
191
+ #[ deprecated = "Use `emath::GuiRounding` with `painter.pixels_per_point()` instead" ]
187
192
#[ inline]
188
193
pub fn round_pos_to_pixel_center ( & self , pos : Pos2 ) -> Pos2 {
189
- self . ctx ( ) . round_pos_to_pixel_center ( pos )
194
+ pos . round_to_pixel_center ( self . pixels_per_point ( ) )
190
195
}
191
196
192
197
/// Useful for pixel-perfect rendering of filled shapes.
198
+ #[ deprecated = "Use `emath::GuiRounding` with `painter.pixels_per_point()` instead" ]
193
199
#[ inline]
194
200
pub fn round_to_pixel ( & self , point : f32 ) -> f32 {
195
- self . ctx ( ) . round_to_pixel ( point )
201
+ point . round_to_pixels ( self . pixels_per_point ( ) )
196
202
}
197
203
198
204
/// Useful for pixel-perfect rendering.
205
+ #[ deprecated = "Use `emath::GuiRounding` with `painter.pixels_per_point()` instead" ]
199
206
#[ inline]
200
207
pub fn round_vec_to_pixels ( & self , vec : Vec2 ) -> Vec2 {
201
- self . ctx ( ) . round_vec_to_pixels ( vec )
208
+ vec . round_to_pixels ( self . pixels_per_point ( ) )
202
209
}
203
210
204
211
/// Useful for pixel-perfect rendering.
212
+ #[ deprecated = "Use `emath::GuiRounding` with `painter.pixels_per_point()` instead" ]
205
213
#[ inline]
206
214
pub fn round_pos_to_pixels ( & self , pos : Pos2 ) -> Pos2 {
207
- self . ctx ( ) . round_pos_to_pixels ( pos )
215
+ pos . round_to_pixels ( self . pixels_per_point ( ) )
208
216
}
209
217
210
218
/// Useful for pixel-perfect rendering.
219
+ #[ deprecated = "Use `emath::GuiRounding` with `painter.pixels_per_point()` instead" ]
211
220
#[ inline]
212
221
pub fn round_rect_to_pixels ( & self , rect : Rect ) -> Rect {
213
- self . ctx ( ) . round_rect_to_pixels ( rect )
222
+ rect . round_to_pixels ( self . pixels_per_point ( ) )
214
223
}
215
224
}
216
225
@@ -337,7 +346,7 @@ impl Painter {
337
346
/// # Paint different primitives
338
347
impl Painter {
339
348
/// Paints a line from the first point to the second.
340
- pub fn line_segment ( & self , points : [ Pos2 ; 2 ] , stroke : impl Into < PathStroke > ) -> ShapeIdx {
349
+ pub fn line_segment ( & self , points : [ Pos2 ; 2 ] , stroke : impl Into < Stroke > ) -> ShapeIdx {
341
350
self . add ( Shape :: LineSegment {
342
351
points,
343
352
stroke : stroke. into ( ) ,
@@ -351,13 +360,13 @@ impl Painter {
351
360
}
352
361
353
362
/// Paints a horizontal line.
354
- pub fn hline ( & self , x : impl Into < Rangef > , y : f32 , stroke : impl Into < PathStroke > ) -> ShapeIdx {
355
- self . add ( Shape :: hline ( x, y, stroke. into ( ) ) )
363
+ pub fn hline ( & self , x : impl Into < Rangef > , y : f32 , stroke : impl Into < Stroke > ) -> ShapeIdx {
364
+ self . add ( Shape :: hline ( x, y, stroke) )
356
365
}
357
366
358
367
/// Paints a vertical line.
359
- pub fn vline ( & self , x : f32 , y : impl Into < Rangef > , stroke : impl Into < PathStroke > ) -> ShapeIdx {
360
- self . add ( Shape :: vline ( x, y, stroke. into ( ) ) )
368
+ pub fn vline ( & self , x : f32 , y : impl Into < Rangef > , stroke : impl Into < Stroke > ) -> ShapeIdx {
369
+ self . add ( Shape :: vline ( x, y, stroke) )
361
370
}
362
371
363
372
pub fn circle (
@@ -398,6 +407,7 @@ impl Painter {
398
407
} )
399
408
}
400
409
410
+ /// The stroke extends _outside_ the [`Rect`].
401
411
pub fn rect (
402
412
& self ,
403
413
rect : Rect ,
@@ -417,6 +427,7 @@ impl Painter {
417
427
self . add ( RectShape :: filled ( rect, rounding, fill_color) )
418
428
}
419
429
430
+ /// The stroke extends _outside_ the [`Rect`].
420
431
pub fn rect_stroke (
421
432
& self ,
422
433
rect : Rect ,
0 commit comments