Struct ratatui::widgets::canvas::Painter

source ·
pub struct Painter<'a, 'b> { /* private fields */ }
Expand description

Painter is an abstraction over the Context that allows to draw shapes on the grid.

It is used by the Shape trait to draw shapes on the grid. It can be useful to think of this as similar to the Buffer struct that is used to draw widgets on the terminal.

Implementations§

source§

impl<'a, 'b> Painter<'a, 'b>

source

pub fn get_point(&self, x: f64, y: f64) -> Option<(usize, usize)>

Convert the (x, y) coordinates to location of a point on the grid

(x, y) coordinates are expressed in the coordinate system of the canvas. The origin is in the lower left corner of the canvas (unlike most other coordinates in Ratatui where the origin is the upper left corner). The x and y bounds of the canvas define the specific area of some coordinate system that will be drawn on the canvas. The resolution of the grid is used to convert the (x, y) coordinates to the location of a point on the grid.

The grid coordinates are expressed in the coordinate system of the grid. The origin is in the top left corner of the grid. The x and y bounds of the grid are always [0, width - 1] and [0, height - 1] respectively. The resolution of the grid is used to convert the (x, y) coordinates to the location of a point on the grid.

§Examples
use ratatui::{prelude::*, widgets::canvas::*};

let mut ctx = Context::new(2, 2, [1.0, 2.0], [0.0, 2.0], symbols::Marker::Braille);
let mut painter = Painter::from(&mut ctx);

let point = painter.get_point(1.0, 0.0);
assert_eq!(point, Some((0, 7)));

let point = painter.get_point(1.5, 1.0);
assert_eq!(point, Some((1, 3)));

let point = painter.get_point(0.0, 0.0);
assert_eq!(point, None);

let point = painter.get_point(2.0, 2.0);
assert_eq!(point, Some((3, 0)));

let point = painter.get_point(1.0, 2.0);
assert_eq!(point, Some((0, 0)));
source

pub fn paint(&mut self, x: usize, y: usize, color: Color)

Paint a point of the grid

§Example
use ratatui::{prelude::*, widgets::canvas::*};

let mut ctx = Context::new(1, 1, [0.0, 2.0], [0.0, 2.0], symbols::Marker::Braille);
let mut painter = Painter::from(&mut ctx);
painter.paint(1, 3, Color::Red);

Trait Implementations§

source§

impl<'a, 'b> Debug for Painter<'a, 'b>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, 'b> From<&'a mut Context<'b>> for Painter<'a, 'b>

source§

fn from(context: &'a mut Context<'b>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for Painter<'a, 'b>

§

impl<'a, 'b> !RefUnwindSafe for Painter<'a, 'b>

§

impl<'a, 'b> !Send for Painter<'a, 'b>

§

impl<'a, 'b> !Sync for Painter<'a, 'b>

§

impl<'a, 'b> Unpin for Painter<'a, 'b>

§

impl<'a, 'b> !UnwindSafe for Painter<'a, 'b>

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.