pub struct Painter {
ctx: Context,
layer_id: LayerId,
clip_rect: Rect,
fade_to_color: Option<Color32>,
opacity_factor: f32,
}
Expand description
Helper to paint shapes and text to a specific region on a specific layer.
All coordinates are screen coordinates in the unit points (one point can consist of many physical pixels).
Fields§
§ctx: Context
Source of fonts and destination of shapes
layer_id: LayerId
Where we paint
clip_rect: Rect
Everything painted in this Painter
will be clipped against this.
This means nothing outside of this rectangle will be visible on screen.
fade_to_color: Option<Color32>
If set, all shapes will have their colors modified to be closer to this. This is used to implement grayed out interfaces.
opacity_factor: f32
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.
Implementations§
source§impl Painter
impl Painter
sourcepub fn new(ctx: Context, layer_id: LayerId, clip_rect: Rect) -> Self
pub fn new(ctx: Context, layer_id: LayerId, clip_rect: Rect) -> Self
Create a painter to a specific layer within a certain clip rectangle.
sourcepub fn with_layer_id(self, layer_id: LayerId) -> Self
pub fn with_layer_id(self, layer_id: LayerId) -> Self
Redirect where you are painting.
sourcepub fn with_clip_rect(&self, rect: Rect) -> Self
pub fn with_clip_rect(&self, rect: Rect) -> Self
sourcepub fn set_layer_id(&mut self, layer_id: LayerId)
pub fn set_layer_id(&mut self, layer_id: LayerId)
Redirect where you are painting.
sourcepub(crate) fn set_fade_to_color(&mut self, fade_to_color: Option<Color32>)
pub(crate) fn set_fade_to_color(&mut self, fade_to_color: Option<Color32>)
If set, colors will be modified to look like this
sourcepub fn set_opacity(&mut self, opacity: f32)
pub fn set_opacity(&mut self, opacity: f32)
Set the opacity (alpha multiplier) of everything painted by this painter from this point forward.
opacity
must be between 0.0 and 1.0, where 0.0 means fully transparent (i.e., invisible)
and 1.0 means fully opaque.
See also: Self::opacity
and Self::multiply_opacity
.
sourcepub fn multiply_opacity(&mut self, opacity: f32)
pub fn multiply_opacity(&mut self, opacity: f32)
Like Self::set_opacity
, but multiplies the given value with the current opacity.
See also: Self::set_opacity
and Self::opacity
.
sourcepub fn opacity(&self) -> f32
pub fn opacity(&self) -> f32
Read the current opacity of the underlying painter.
See also: Self::set_opacity
and Self::multiply_opacity
.
pub(crate) fn is_visible(&self) -> bool
sourcepub(crate) fn set_invisible(&mut self)
pub(crate) fn set_invisible(&mut self)
If false
, nothing added to the painter will be visible
source§impl Painter
impl Painter
§Accessors etc
sourcepub fn clip_rect(&self) -> Rect
pub fn clip_rect(&self) -> Rect
Everything painted in this Painter
will be clipped against this.
This means nothing outside of this rectangle will be visible on screen.
sourcepub fn set_clip_rect(&mut self, clip_rect: Rect)
pub fn set_clip_rect(&mut self, clip_rect: Rect)
Everything painted in this Painter
will be clipped against this.
This means nothing outside of this rectangle will be visible on screen.
sourcepub fn round_to_pixel(&self, point: f32) -> f32
pub fn round_to_pixel(&self, point: f32) -> f32
Useful for pixel-perfect rendering.
sourcepub fn round_vec_to_pixels(&self, vec: Vec2) -> Vec2
pub fn round_vec_to_pixels(&self, vec: Vec2) -> Vec2
Useful for pixel-perfect rendering.
sourcepub fn round_pos_to_pixels(&self, pos: Pos2) -> Pos2
pub fn round_pos_to_pixels(&self, pos: Pos2) -> Pos2
Useful for pixel-perfect rendering.
sourcepub fn round_rect_to_pixels(&self, rect: Rect) -> Rect
pub fn round_rect_to_pixels(&self, rect: Rect) -> Rect
Useful for pixel-perfect rendering.
source§impl Painter
impl Painter
§Low level
fn paint_list<R>(&self, writer: impl FnOnce(&mut PaintList) -> R) -> R
fn transform_shape(&self, shape: &mut Shape)
sourcepub fn add(&self, shape: impl Into<Shape>) -> ShapeIdx
pub fn add(&self, shape: impl Into<Shape>) -> ShapeIdx
It is up to the caller to make sure there is room for this. Can be used for free painting. NOTE: all coordinates are screen coordinates!
sourcepub fn extend<I: IntoIterator<Item = Shape>>(&self, shapes: I)
pub fn extend<I: IntoIterator<Item = Shape>>(&self, shapes: I)
Add many shapes at once.
Calling this once is generally faster than calling Self::add
multiple times.
sourcepub fn for_each_shape(&self, reader: impl FnMut(&ClippedShape))
pub fn for_each_shape(&self, reader: impl FnMut(&ClippedShape))
Access all shapes added this frame.
source§impl Painter
impl Painter
§Debug painting
pub fn debug_rect(&self, rect: Rect, color: Color32, text: impl ToString)
pub fn error(&self, pos: Pos2, text: impl Display) -> Rect
sourcepub fn debug_text(
&self,
pos: Pos2,
anchor: Align2,
color: Color32,
text: impl ToString,
) -> Rect
pub fn debug_text( &self, pos: Pos2, anchor: Align2, color: Color32, text: impl ToString, ) -> Rect
Text with a background.
See also Context::debug_text
.
source§impl Painter
impl Painter
§Paint different primitives
sourcepub fn line_segment(
&self,
points: [Pos2; 2],
stroke: impl Into<PathStroke>,
) -> ShapeIdx
pub fn line_segment( &self, points: [Pos2; 2], stroke: impl Into<PathStroke>, ) -> ShapeIdx
Paints a line from the first point to the second.
sourcepub fn hline(
&self,
x: impl Into<Rangef>,
y: f32,
stroke: impl Into<PathStroke>,
) -> ShapeIdx
pub fn hline( &self, x: impl Into<Rangef>, y: f32, stroke: impl Into<PathStroke>, ) -> ShapeIdx
Paints a horizontal line.
sourcepub fn vline(
&self,
x: f32,
y: impl Into<Rangef>,
stroke: impl Into<PathStroke>,
) -> ShapeIdx
pub fn vline( &self, x: f32, y: impl Into<Rangef>, stroke: impl Into<PathStroke>, ) -> ShapeIdx
Paints a vertical line.
pub fn circle( &self, center: Pos2, radius: f32, fill_color: impl Into<Color32>, stroke: impl Into<Stroke>, ) -> ShapeIdx
pub fn circle_filled( &self, center: Pos2, radius: f32, fill_color: impl Into<Color32>, ) -> ShapeIdx
pub fn circle_stroke( &self, center: Pos2, radius: f32, stroke: impl Into<Stroke>, ) -> ShapeIdx
pub fn rect( &self, rect: Rect, rounding: impl Into<Rounding>, fill_color: impl Into<Color32>, stroke: impl Into<Stroke>, ) -> ShapeIdx
pub fn rect_filled( &self, rect: Rect, rounding: impl Into<Rounding>, fill_color: impl Into<Color32>, ) -> ShapeIdx
pub fn rect_stroke( &self, rect: Rect, rounding: impl Into<Rounding>, stroke: impl Into<Stroke>, ) -> ShapeIdx
sourcepub fn arrow(&self, origin: Pos2, vec: Vec2, stroke: impl Into<Stroke>)
pub fn arrow(&self, origin: Pos2, vec: Vec2, stroke: impl Into<Stroke>)
Show an arrow starting at origin
and going in the direction of vec
, with the length vec.length()
.
sourcepub fn image(
&self,
texture_id: TextureId,
rect: Rect,
uv: Rect,
tint: Color32,
) -> ShapeIdx
pub fn image( &self, texture_id: TextureId, rect: Rect, uv: Rect, tint: Color32, ) -> ShapeIdx
An image at the given position.
uv
should normally be Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0))
unless you want to crop or flip the image.
tint
is a color multiplier. Use Color32::WHITE
if you don’t want to tint the image.
Usually it is easier to use crate::Image::paint_at
instead:
egui::Image::new(egui::include_image!("../assets/ferris.png"))
.rounding(5.0)
.tint(egui::Color32::LIGHT_BLUE)
.paint_at(ui, rect);
source§impl Painter
impl Painter
§Text
sourcepub fn text(
&self,
pos: Pos2,
anchor: Align2,
text: impl ToString,
font_id: FontId,
text_color: Color32,
) -> Rect
pub fn text( &self, pos: Pos2, anchor: Align2, text: impl ToString, font_id: FontId, text_color: Color32, ) -> Rect
Lay out and paint some text.
To center the text at the given position, use Align2::CENTER_CENTER
.
To find out the size of text before painting it, use
Self::layout
or Self::layout_no_wrap
.
Returns where the text ended up.
sourcepub fn layout(
&self,
text: String,
font_id: FontId,
color: Color32,
wrap_width: f32,
) -> Arc<Galley>
pub fn layout( &self, text: String, font_id: FontId, color: Color32, wrap_width: f32, ) -> Arc<Galley>
Will wrap text at the given width and line break at \n
.
Paint the results with Self::galley
.
sourcepub fn layout_no_wrap(
&self,
text: String,
font_id: FontId,
color: Color32,
) -> Arc<Galley>
pub fn layout_no_wrap( &self, text: String, font_id: FontId, color: Color32, ) -> Arc<Galley>
Will line break at \n
.
Paint the results with Self::galley
.
sourcepub fn layout_job(&self, layout_job: LayoutJob) -> Arc<Galley>
pub fn layout_job(&self, layout_job: LayoutJob) -> Arc<Galley>
Lay out this text layut job in a galley.
Paint the results with Self::galley
.
sourcepub fn galley(&self, pos: Pos2, galley: Arc<Galley>, fallback_color: Color32)
pub fn galley(&self, pos: Pos2, galley: Arc<Galley>, fallback_color: Color32)
Paint text that has already been laid out in a Galley
.
You can create the Galley
with Self::layout
or Self::layout_job
.
Any uncolored parts of the Galley
(using Color32::PLACEHOLDER
) will be replaced with the given color.
Any non-placeholder color in the galley takes precedence over this fallback color.
sourcepub fn galley_with_override_text_color(
&self,
pos: Pos2,
galley: Arc<Galley>,
text_color: Color32,
)
pub fn galley_with_override_text_color( &self, pos: Pos2, galley: Arc<Galley>, text_color: Color32, )
Paint text that has already been laid out in a Galley
.
You can create the Galley
with Self::layout
.
All text color in the Galley
will be replaced with the given color.
pub fn galley_with_color( &self, pos: Pos2, galley: Arc<Galley>, text_color: Color32, )
Painter::galley
or Painter::galley_with_override_text_color
instead