pub struct Painter {
ctx: Context,
pixels_per_point: f32,
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).
A Painter
never outlive a single frame/pass.
Fields§
§ctx: Context
§pixels_per_point: f32
§layer_id: LayerId
§clip_rect: Rect
§fade_to_color: Option<Color32>
§opacity_factor: f32
Implementations§
§impl Painter
impl Painter
pub fn new(ctx: Context, layer_id: LayerId, clip_rect: Rect) -> Painter
pub fn new(ctx: Context, layer_id: LayerId, clip_rect: Rect) -> Painter
Create a painter to a specific layer within a certain clip rectangle.
pub fn with_layer_id(self, layer_id: LayerId) -> Painter
pub fn with_layer_id(self, layer_id: LayerId) -> Painter
Redirect where you are painting.
pub fn with_clip_rect(&self, rect: Rect) -> Painter
pub fn with_clip_rect(&self, rect: Rect) -> Painter
pub fn set_layer_id(&mut self, layer_id: LayerId)
pub fn set_layer_id(&mut self, layer_id: LayerId)
Redirect where you are painting.
It is undefined behavior to change the LayerId
of crate::Ui::painter
.
pub fn set_fade_to_color(&mut self, fade_to_color: Option<Color32>)
pub fn set_fade_to_color(&mut self, fade_to_color: Option<Color32>)
If set, colors will be modified to look like this
pub 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
.
pub 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
.
pub 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 fn is_visible(&self) -> bool
pub fn is_visible(&self) -> bool
If false
, nothing you paint will show up.
Also checks Context::will_discard
.
pub fn set_invisible(&mut self)
pub fn set_invisible(&mut self)
If false
, nothing added to the painter will be visible
pub fn pixels_per_point(&self) -> f32
pub fn pixels_per_point(&self) -> f32
Number of physical pixels for each logical UI point.
pub 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.
pub fn shrink_clip_rect(&mut self, new_clip_rect: Rect)
pub fn shrink_clip_rect(&mut self, new_clip_rect: Rect)
Constrain the rectangle in which we can paint.
Short for painter.set_clip_rect(painter.clip_rect().intersect(new_clip_rect))
.
See also: Self::clip_rect
and Self::set_clip_rect
.
pub 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.
Warning: growing the clip rect might cause unexpected results!
When in doubt, use Self::shrink_clip_rect
instead.
pub fn round_to_pixel_center(&self, point: f32) -> f32
pub fn round_to_pixel_center(&self, point: f32) -> f32
Useful for pixel-perfect rendering of lines that are one pixel wide (or any odd number of pixels).
pub fn round_pos_to_pixel_center(&self, pos: Pos2) -> Pos2
👎Deprecated: Use emath::GuiRounding
with painter.pixels_per_point()
instead
pub fn round_pos_to_pixel_center(&self, pos: Pos2) -> Pos2
emath::GuiRounding
with painter.pixels_per_point()
insteadUseful for pixel-perfect rendering of lines that are one pixel wide (or any odd number of pixels).
pub fn round_to_pixel(&self, point: f32) -> f32
👎Deprecated: Use emath::GuiRounding
with painter.pixels_per_point()
instead
pub fn round_to_pixel(&self, point: f32) -> f32
emath::GuiRounding
with painter.pixels_per_point()
insteadUseful for pixel-perfect rendering of filled shapes.
pub fn round_vec_to_pixels(&self, vec: Vec2) -> Vec2
👎Deprecated: Use emath::GuiRounding
with painter.pixels_per_point()
instead
pub fn round_vec_to_pixels(&self, vec: Vec2) -> Vec2
emath::GuiRounding
with painter.pixels_per_point()
insteadUseful for pixel-perfect rendering.
pub fn round_pos_to_pixels(&self, pos: Pos2) -> Pos2
👎Deprecated: Use emath::GuiRounding
with painter.pixels_per_point()
instead
pub fn round_pos_to_pixels(&self, pos: Pos2) -> Pos2
emath::GuiRounding
with painter.pixels_per_point()
insteadUseful for pixel-perfect rendering.
pub fn round_rect_to_pixels(&self, rect: Rect) -> Rect
👎Deprecated: Use emath::GuiRounding
with painter.pixels_per_point()
instead
pub fn round_rect_to_pixels(&self, rect: Rect) -> Rect
emath::GuiRounding
with painter.pixels_per_point()
insteadUseful for pixel-perfect rendering.
§impl Painter
impl Painter
§Low level
pub 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!
pub fn extend<I>(&self, shapes: I)where
I: IntoIterator<Item = Shape>,
pub fn extend<I>(&self, shapes: I)where
I: IntoIterator<Item = Shape>,
Add many shapes at once.
Calling this once is generally faster than calling Self::add
multiple times.
pub 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.
§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
pub 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
.
§impl Painter
impl Painter
§Paint different primitives
pub fn line_segment(
&self,
points: [Pos2; 2],
stroke: impl Into<Stroke>,
) -> ShapeIdx
pub fn line_segment( &self, points: [Pos2; 2], stroke: impl Into<Stroke>, ) -> ShapeIdx
Paints a line from the first point to the second.
pub fn line(&self, points: Vec<Pos2>, stroke: impl Into<PathStroke>) -> ShapeIdx
pub fn line(&self, points: Vec<Pos2>, stroke: impl Into<PathStroke>) -> ShapeIdx
Paints a line connecting the points. NOTE: all coordinates are screen coordinates!
pub fn hline(
&self,
x: impl Into<Rangef>,
y: f32,
stroke: impl Into<Stroke>,
) -> ShapeIdx
pub fn hline( &self, x: impl Into<Rangef>, y: f32, stroke: impl Into<Stroke>, ) -> ShapeIdx
Paints a horizontal line.
pub fn vline(
&self,
x: f32,
y: impl Into<Rangef>,
stroke: impl Into<Stroke>,
) -> ShapeIdx
pub fn vline( &self, x: f32, y: impl Into<Rangef>, stroke: impl Into<Stroke>, ) -> 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,
corner_radius: impl Into<CornerRadius>,
fill_color: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke_kind: StrokeKind,
) -> ShapeIdx
pub fn rect( &self, rect: Rect, corner_radius: impl Into<CornerRadius>, fill_color: impl Into<Color32>, stroke: impl Into<Stroke>, stroke_kind: StrokeKind, ) -> ShapeIdx
See also Self::rect_filled
and Self::rect_stroke
.
pub fn rect_filled( &self, rect: Rect, corner_radius: impl Into<CornerRadius>, fill_color: impl Into<Color32>, ) -> ShapeIdx
pub fn rect_stroke( &self, rect: Rect, corner_radius: impl Into<CornerRadius>, stroke: impl Into<Stroke>, stroke_kind: StrokeKind, ) -> ShapeIdx
pub 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()
.
pub 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"))
.corner_radius(5)
.tint(egui::Color32::LIGHT_BLUE)
.paint_at(ui, rect);
§impl Painter
impl Painter
§Text
pub 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.
pub 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
.
pub 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
.
pub 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
.
pub 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.
pub 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
insteadTrait Implementations§
Auto Trait Implementations§
impl Freeze for Painter
impl !RefUnwindSafe for Painter
impl Send for Painter
impl Sync for Painter
impl Unpin for Painter
impl !UnwindSafe for Painter
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.