Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to Semantic Versioning.
### Added

* `window::set_text_input_enabled` and `window::is_text_input_enabled` were added, allowing you to enable and disable text input events. They are currently on by default, but this may change in a future version. ([@Tairesh](https://github.com/Tairesh) in [#356](https://github.com/17cupsofcoffee/tetra/pull/356))
* Text can be aligned when it is drawn. `HorizontalAlignment`, `VerticalAlignment`, `TextAlignment`, `Text::alignment`, `Text::set_alignment` and `Text::draw_aligned` were added. The default alignment does not move existing text. ([@1kauratus](https://github.com/1kauratus) in [#359](https://github.com/17cupsofcoffee/tetra/pull/359))

### Changed

Expand Down
15 changes: 13 additions & 2 deletions examples/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tetra::graphics::text::{Font, Text};
use tetra::graphics::text::{Font, HorizontalAlignment, Text, TextAlignment, VerticalAlignment};
use tetra::graphics::{self, Color};
use tetra::math::Vec2;
use tetra::{Context, ContextBuilder, State};
Expand All @@ -8,15 +8,20 @@ const TEXT_OFFSET: Vec2<f32> = Vec2::new(16.0, 16.0);
struct GameState {
vector_text: Text,
bitmap_text: Text,
centered_text: Text,
}

impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<GameState> {
let vector_font = Font::vector(ctx, "./examples/resources/DejaVuSansMono.ttf", 16.0)?;

let vector_text = Text::new(
"Hello, world!\n\nThis is some text being rendered from a TTF font.",
Font::vector(ctx, "./examples/resources/DejaVuSansMono.ttf", 16.0)?,
vector_font.clone(),
);

let centered_text = Text::new("This line is centered.", vector_font);

let bitmap_text = Text::new(
"Hello, world!\n\nThis is some text being rendered from a bitmap font.",
Font::bmfont(ctx, "./examples/resources/DejaVuSansMono.fnt")?,
Expand All @@ -25,6 +30,7 @@ impl GameState {
Ok(GameState {
vector_text,
bitmap_text,
centered_text,
})
}
}
Expand All @@ -36,6 +42,11 @@ impl State for GameState {
self.vector_text.draw(ctx, TEXT_OFFSET);
self.bitmap_text
.draw(ctx, TEXT_OFFSET + Vec2::new(0.0, 128.0));
self.centered_text.draw_aligned(
ctx,
Vec2::new(640.0, 360.0),
TextAlignment::new(HorizontalAlignment::Center, VerticalAlignment::Middle),
);

Ok(())
}
Expand Down
Loading