Struct Painter

Source
pub struct Painter { /* private fields */ }
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).

Implementations§

Source§

impl Painter

Source

pub fn new(ctx: Context, layer_id: LayerId, clip_rect: Rect) -> Painter

Create a painter to a specific layer within a certain clip rectangle.

Source

pub fn with_layer_id(self, layer_id: LayerId) -> Painter

Redirect where you are painting.

Source

pub fn with_clip_rect(&self, rect: Rect) -> Painter

Create a painter for a sub-region of this Painter.

The clip-rect of the returned Painter will be the intersection of the given rectangle and the clip_rect() of the parent Painter.

Source

pub fn set_layer_id(&mut self, layer_id: LayerId)

Redirect where you are painting.

Source

pub fn sub_region(&self, rect: Rect) -> Painter

👎Deprecated: Use Painter::with_clip_rect
Source§

impl Painter

§Accessors etc
Source

pub fn ctx(&self) -> &Context

Get a reference to the parent Context.

Source

pub fn fonts<R>(&self, reader: impl FnOnce(&Fonts) -> R) -> R

Read-only access to the shared Fonts.

See Context documentation for how locks work.

Source

pub fn layer_id(&self) -> LayerId

Where we paint

Source

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.

Source

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.

Source

pub fn round_to_pixel(&self, point: f32) -> f32

Useful for pixel-perfect rendering.

Source

pub fn round_vec_to_pixels(&self, vec: Vec2) -> Vec2

Useful for pixel-perfect rendering.

Source

pub fn round_pos_to_pixels(&self, pos: Pos2) -> Pos2

Useful for pixel-perfect rendering.

Source§

impl Painter

§Low level
Source

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!

Source

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.

Source

pub fn set(&self, idx: ShapeIdx, shape: impl Into<Shape>)

Modify an existing Shape.

Source§

impl Painter

§Debug painting
Source

pub fn debug_rect(&self, rect: Rect, color: Color32, text: impl ToString)

Source

pub fn error(&self, pos: Pos2, text: impl Display) -> Rect

Source

pub fn debug_text( &self, pos: Pos2, anchor: Align2, color: Color32, text: impl ToString, ) -> Rect

text with a background

Source§

impl Painter

§Paint different primitives

Source

pub fn line_segment(&self, points: [Pos2; 2], stroke: impl Into<Stroke>)

Paints a line from the first point to the second.

Source

pub fn hline(&self, x: impl Into<Rangef>, y: f32, stroke: impl Into<Stroke>)

Paints a horizontal line.

Source

pub fn vline(&self, x: f32, y: impl Into<Rangef>, stroke: impl Into<Stroke>)

Paints a vertical line.

Source

pub fn circle( &self, center: Pos2, radius: f32, fill_color: impl Into<Color32>, stroke: impl Into<Stroke>, )

Source

pub fn circle_filled( &self, center: Pos2, radius: f32, fill_color: impl Into<Color32>, )

Source

pub fn circle_stroke( &self, center: Pos2, radius: f32, stroke: impl Into<Stroke>, )

Source

pub fn rect( &self, rect: Rect, rounding: impl Into<Rounding>, fill_color: impl Into<Color32>, stroke: impl Into<Stroke>, )

Source

pub fn rect_filled( &self, rect: Rect, rounding: impl Into<Rounding>, fill_color: impl Into<Color32>, )

Source

pub fn rect_stroke( &self, rect: Rect, rounding: impl Into<Rounding>, stroke: impl Into<Stroke>, )

Source

pub fn arrow(&self, origin: Pos2, vec: Vec2, stroke: Stroke)

Show an arrow starting at origin and going in the direction of vec, with the length vec.length().

Source

pub fn image(&self, texture_id: TextureId, rect: Rect, uv: Rect, tint: Color32)

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

§Text
Source

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.

Source

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.

Source

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.

Source

pub fn galley(&self, pos: Pos2, galley: Arc<Galley>)

Paint text that has already been laid out in a Galley.

You can create the Galley with Self::layout.

If you want to change the color of the text, use Self::galley_with_color.

Source

pub fn galley_with_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.

The text color in the Galley will be replaced with the given color.

Trait Implementations§

Source§

impl Clone for Painter

Source§

fn clone(&self) -> Painter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,