Struct ruscii::drawing::Pencil

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

An object that stores several text style options and the Canvas to which text and shapes can be written.

Options

  • Origin - A Vec2 on the Canvas which becomes the new “origin” for drawn characters. All Vec2s subsequently passed to associated functions are relative to this Vec2. For example, if the origin is set to (2, 3) and Pencil::draw_text is called with position (1, 0), the string will be drawn at (3, 3).
  • Foreground (character) Color
  • Background Color
  • Style (boldness)

Examples

An introductory example using a Pencil could be:

let mut pencil = Pencil::new(window.canvas_mut());
pencil.draw_text("Hello, world!", Vec2::xy(0, 0));

Most associated functions return a mutable reference to the function’s receiver (self), allowing for chaining multiple calls as in the following example.

From pong.rs in the examples folder:

Pencil::new(window.canvas_mut())
    .draw_text(&format!("FPS: {}", fps_counter.count()), Vec2::xy(0, 0))
    .set_origin(Vec2::xy((win_size.x - score_msg.len() as i32) / 2, (win_size.y - state.dimension.y) / 2 - 1))
    .draw_text(score_msg, Vec2::xy(0, 0))
    .set_origin((win_size - state.dimension) / 2)
    .draw_rect(&RectCharset::simple_round_lines(), Vec2::zero(), state.dimension)
    .draw_vline('\'', Vec2::xy(state.dimension.x / 2, 1), state.dimension.y - 2)
    .set_foreground(Color::Blue)
    .draw_rect(&RectCharset::double_lines(), state.left_player.position - Vec2::y(PAD_HEIGHT), Vec2::xy(2, PAD_HEIGHT * 2))
    .set_foreground(Color::Red)
    .draw_rect(&RectCharset::double_lines(), state.right_player.position - Vec2::y(PAD_HEIGHT), Vec2::xy(2, PAD_HEIGHT * 2))
    .set_foreground(Color::Yellow)
    .set_style(Style::Bold)
    .draw_char('o', state.ball_position);

Implementations§

source§

impl<'a> Pencil<'a>

source

pub fn new(canvas: &'a mut Canvas) -> Pencil<'_>

Constructs a Pencil that can write to the given Canvas.

source

pub fn origin(&self) -> Vec2

source

pub fn dimension(&self) -> Vec2

Returns the dimensions of the positive drawable space past the current origin.

Example
let mut canvas = Canvas::new(Vec2::xy(10, 5), &VisualElement::default());
let mut pencil = Pencil::new(&mut canvas);
pencil.set_origin(Vec2::xy(5, 0));
assert_eq!(pencil.dimension(), Vec2::xy(5, 5))
source

pub fn foreground(&self) -> &Color

source

pub fn background(&self) -> &Color

source

pub fn style(&self) -> &Style

source

pub fn set_origin(&mut self, position: Vec2) -> &mut Pencil<'a>

source

pub fn move_origin(&mut self, displacement: Vec2) -> &mut Pencil<'a>

Moves the origin by the given displacement.

source

pub fn set_foreground(&mut self, color: Color) -> &mut Pencil<'a>

source

pub fn set_background(&mut self, color: Color) -> &mut Pencil<'a>

source

pub fn set_style(&mut self, style: Style) -> &mut Pencil<'a>

source

pub fn draw_char(&mut self, value: char, position: Vec2) -> &mut Pencil<'a>

Draws a character at the given position according to the previously set text style options.

Returns the receiver for chaining.

source

pub fn draw_text(&mut self, text: &str, position: Vec2) -> &mut Pencil<'a>

Draws a string at the given position according to the previously set text style options.

If the string has multiple characters, each subsequent character is drawn one point to the right (as you’d probably expect).

Returns the receiver for chaining.

source

pub fn draw_center_text( &mut self, text: &str, position: Vec2 ) -> &mut Pencil<'a>

Draws a string centered at the given position according to the previously set text style options.

Returns the receiver for chaining.

source

pub fn draw_right_aligned_text( &mut self, text: &str, position: Vec2 ) -> &mut Pencil<'a>

Draws a right-aligned string ending at the given position.

Returns the receiver for chaining.

source

pub fn draw_vline<T: ToPrimitive>( &mut self, value: char, position: Vec2, size: T ) -> &mut Pencil<'a>

Draws a vertical line starting from the given position and extending for size lines downwards. This line is composed of the given value characters.

Returns the receiver for chaining.

source

pub fn draw_hline<T: ToPrimitive>( &mut self, value: char, position: Vec2, size: T ) -> &mut Pencil<'a>

Draws a horizontal line starting from the given position and extending for size lines rightwards. This line is composed of the given value characters.

Returns the receiver for chaining.

source

pub fn draw_rect( &mut self, charset: &RectCharset, position: Vec2, dimension: Vec2 ) -> &mut Pencil<'a>

Draws an empty rectangle from the given charset with the given dimension. The given position sets the position of the top-left corner of the rectangle.

The given dimension includes the characters used to draw the rectangle, so the dimension of the enclosed space has a width of dimension.x - 2 and a height of dimension.y - 2.

Returns the receiver for chaining.

source

pub fn draw_filled_rect( &mut self, fill: char, position: Vec2, dimension: Vec2 ) -> &mut Pencil<'a>

Draws a filled rectangle from the given charset with the given dimension. The given position sets the position of the top-left corner of the rectangle. The rectangle is composed of the given fill characters.

Returns the receiver for chaining.

source

pub fn draw_animator(&mut self, animator: &mut Animator, position: Vec2)

Draws one of the frames of the given animator based on the number of times this has been previously called.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Pencil<'a>

§

impl<'a> Send for Pencil<'a>

§

impl<'a> Sync for Pencil<'a>

§

impl<'a> Unpin for Pencil<'a>

§

impl<'a> !UnwindSafe for Pencil<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.