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 theCanvas
which becomes the new “origin” for drawn characters. AllVec2
s subsequently passed to associated functions are relative to thisVec2
. For example, if the origin is set to (2, 3) andPencil::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>
impl<'a> Pencil<'a>
pub fn origin(&self) -> Vec2
sourcepub fn dimension(&self) -> Vec2
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))
pub fn foreground(&self) -> &Color
pub fn background(&self) -> &Color
pub fn style(&self) -> &Style
pub fn set_origin(&mut self, position: Vec2) -> &mut Pencil<'a>
sourcepub fn move_origin(&mut self, displacement: Vec2) -> &mut Pencil<'a>
pub fn move_origin(&mut self, displacement: Vec2) -> &mut Pencil<'a>
Moves the origin by the given displacement
.
pub fn set_foreground(&mut self, color: Color) -> &mut Pencil<'a>
pub fn set_background(&mut self, color: Color) -> &mut Pencil<'a>
pub fn set_style(&mut self, style: Style) -> &mut Pencil<'a>
sourcepub fn draw_char(&mut self, value: char, position: Vec2) -> &mut Pencil<'a>
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.
sourcepub fn draw_text(&mut self, text: &str, position: Vec2) -> &mut Pencil<'a>
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.
sourcepub fn draw_center_text(
&mut self,
text: &str,
position: Vec2
) -> &mut Pencil<'a>
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.
sourcepub fn draw_right_aligned_text(
&mut self,
text: &str,
position: Vec2
) -> &mut Pencil<'a>
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.
sourcepub fn draw_vline<T: ToPrimitive>(
&mut self,
value: char,
position: Vec2,
size: T
) -> &mut Pencil<'a>
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.
sourcepub fn draw_hline<T: ToPrimitive>(
&mut self,
value: char,
position: Vec2,
size: T
) -> &mut Pencil<'a>
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.
sourcepub fn draw_rect(
&mut self,
charset: &RectCharset,
position: Vec2,
dimension: Vec2
) -> &mut Pencil<'a>
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.
sourcepub fn draw_filled_rect(
&mut self,
fill: char,
position: Vec2,
dimension: Vec2
) -> &mut Pencil<'a>
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.
sourcepub fn draw_animator(&mut self, animator: &mut Animator, position: Vec2)
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.