-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Stroke font text gizmos #22732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ickshonpe
wants to merge
66
commits into
bevyengine:main
Choose a base branch
from
ickshonpe:text-gizmos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Stroke font text gizmos #22732
Changes from 19 commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
a2fc8e4
Added text module to gizmos crate
ickshonpe a3e25b4
Added example and basic implementation with just two glyphs.
ickshonpe 7c051ce
Added simplex font coordinate data
ickshonpe 863ac8c
Added simplex font rendering
ickshonpe e77a956
Fixed orientation
ickshonpe edf970d
Cleanup
ickshonpe c8697c7
update examples, draw text for 2d_gizmos with text_2d gizmo function
ickshonpe 9700bbb
Add all glyphs display to text_gizmos example, increased LINE_HEIGHT …
ickshonpe 668ed7f
larger glyphs in example
ickshonpe 169c5e2
Added `text_3d` method impl.
ickshonpe 23efa88
Renamed `text_3d` to `text`
ickshonpe 5beef21
cargo run -p build-templated-pages -- update examples
ickshonpe 59fa5ad
Fixed errors in comments
ickshonpe 7cc29e8
use core instead of std
ickshonpe 678d96d
Merge branch 'main' into text-gizmos
ickshonpe 01d2e18
Added `text_gizmos_font` example
ickshonpe 23c0b0b
Merge branch 'text-gizmos' of https://github.com/ickshonpe/bevy into …
ickshonpe 1668ad7
Added `text_font` module, and moved font data into it.
ickshonpe b0c5a21
reorder text and text2d functions
ickshonpe df6bd54
cargo run -p build-templated-pages -- update examples
ickshonpe c467b57
Fixed misnaming in text doc example
ickshonpe 0130b43
removed redundant static lifetime
ickshonpe 0cde043
removed redundant static lifetime
ickshonpe 1e2c588
Document supported character range
ickshonpe 97d5d42
Documented supported character range.
ickshonpe f732b7b
Implemented anchoring. Added `anchor` params to `text` and `text_2d` …
ickshonpe 4dba3f7
removed uneeded line
ickshonpe 335fa3a
Merge branch 'main' into text-gizmos
ickshonpe 070e1cb
Removed unused import
ickshonpe 1961e98
Added anchor argument to doc tests
ickshonpe 075517c
reordered lib imports
ickshonpe e42db9d
Merge branch 'main' into text-gizmos
ickshonpe a976648
reverted 2d_gizmos changes
ickshonpe 7a1ecac
Merge branch 'text-gizmos' of https://github.com/ickshonpe/bevy into …
ickshonpe 4b6281f
Merge branch 'main' into text-gizmos
ickshonpe 4994789
The stroke font metrics have been moved into the new `StrokeTextMetri…
ickshonpe 6471390
Removed empty impl block
ickshonpe 7e0304e
Added `StrokeFont` type
ickshonpe f8761cf
use metrics when creating `StrokeFontIterator`
ickshonpe ef34864
Add a third iterator for the stroke line strips instead of allocating…
ickshonpe 9c21222
Removed unneeded `into_iter`s
ickshonpe abd3660
Added `ScaledStrokeFont` wrapper around `StrokeFont`
ickshonpe 31f461e
Clean up, added `StrokeTextLayout` API.
ickshonpe cb2b104
Added metrics to `StrokeTextLayout`, removed `StrokeFontMetrics`.
ickshonpe e167889
Added doc comments
ickshonpe 28acd59
More doc comments
ickshonpe 1bd3d28
More doc comments
ickshonpe 7d691ae
Simplify by removing multiple iterator types
ickshonpe 62d79eb
Clean up
ickshonpe 8a1f2fc
Use an inner while loop to skip multiple invalid strokes
ickshonpe 61c0438
use empty range to signal no strokes
ickshonpe a40bf3d
clean up
ickshonpe 9be9ae3
Removed advance function
ickshonpe 79cedd6
Improved doc comment for render function
ickshonpe 69a1ce5
use for loop instead of while
ickshonpe 24e792f
Removed needless borrows
ickshonpe c2e04fe
renamed modules to disambiguate them from bevy_text text
ickshonpe d61edb4
Merge branch 'main' into text-gizmos
ickshonpe 1f671b1
renamed text_gizmos example to 2d_text_gizmos
ickshonpe cae73cf
Fixed anchor application.
ickshonpe 926c0fd
Merge branch 'text-gizmos' of https://github.com/ickshonpe/bevy into …
ickshonpe 6465be1
color the text in the 3d example
ickshonpe b110746
Thicker text in 3d example
ickshonpe 397b8fd
rearranged 2d example
ickshonpe 530e09b
Added `anchored_text_gizmos` example
ickshonpe 50bb766
Merge branch 'main' into text-gizmos
ickshonpe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| //! This module draws text gizmos using a stroke font. | ||
|
|
||
| use crate::text_font::*; | ||
| use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup}; | ||
| use bevy_color::Color; | ||
| use bevy_math::{Isometry2d, Isometry3d, Vec2, Vec3}; | ||
|
|
||
| impl<Config, Clear> GizmoBuffer<Config, Clear> | ||
| where | ||
| Config: GizmoConfigGroup, | ||
| Clear: 'static + Send + Sync, | ||
| { | ||
| /// Draw text using a stroke font with the given isometry applied. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// - `isometry`: defines the translation and rotation of the text. | ||
| /// - `text`: the text to be drawn. | ||
| /// - `size`: the size of the text in pixels. | ||
| /// - `color`: the color of the text. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// # use bevy_gizmos::prelude::*; | ||
| /// # use bevy_math::prelude::*; | ||
| /// # use bevy_color::Color; | ||
| /// fn system(mut gizmos: Gizmos) { | ||
| /// gizmos.text_3d(Isometry3d::IDENTITY, "3D text gizmo", 25., Color::WHITE); | ||
| /// } | ||
| /// # bevy_ecs::system::assert_is_system(system); | ||
| /// ``` | ||
| pub fn text( | ||
| &mut self, | ||
| isometry: impl Into<Isometry3d>, | ||
| text: &str, | ||
| size: f32, | ||
| color: impl Into<Color>, | ||
| ) { | ||
| let isometry = isometry.into(); | ||
| let color = color.into(); | ||
| let scale = size / SIMPLEX_CAP_HEIGHT; | ||
| let line_height = LINE_HEIGHT * size; | ||
| let space_advance = SIMPLEX_GLYPHS[0].0 as f32 * scale; | ||
|
|
||
| let mut rx = 0.0; | ||
| let mut ry = 0.0; | ||
|
|
||
| for c in text.chars() { | ||
| if c == '\n' { | ||
| rx = 0.0; | ||
| ry -= line_height; | ||
| continue; | ||
| } | ||
|
|
||
| let code_point = c as usize; | ||
| if !(SIMPLEX_ASCII_START..=SIMPLEX_ASCII_END).contains(&code_point) { | ||
| rx += space_advance; | ||
| continue; | ||
| } | ||
|
|
||
| let glyph = &SIMPLEX_GLYPHS[code_point - SIMPLEX_ASCII_START]; | ||
| let advance = glyph.0 as f32 * scale; | ||
|
|
||
| for stroke_index in glyph.1.clone() { | ||
| let stroke = SIMPLEX_STROKES[stroke_index].clone(); | ||
| if stroke.len() < 2 { | ||
| continue; | ||
| } | ||
|
|
||
| self.linestrip( | ||
| SIMPLEX_POSITIONS[stroke].iter().map(|&[x, y]| { | ||
| isometry | ||
| * Vec3::new( | ||
| rx + scale * x as f32, | ||
| ry - scale * (SIMPLEX_CAP_HEIGHT - y as f32), | ||
| 0.0, | ||
| ) | ||
| }), | ||
| color, | ||
| ); | ||
| } | ||
|
|
||
| rx += advance; | ||
| } | ||
| } | ||
|
|
||
| /// Draw text using a stroke font in 2d with the given isometry applied. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// - `isometry`: defines the translation and rotation of the text. | ||
| /// - `text`: the text to be drawn. | ||
| /// - `size`: the size of the text. | ||
| /// - `color`: the color of the text. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// # use bevy_gizmos::prelude::*; | ||
| /// # use bevy_math::prelude::*; | ||
| /// # use bevy_color::Color; | ||
| /// fn system(mut gizmos: Gizmos) { | ||
| /// gizmos.text_2d(Isometry2d::IDENTITY, "2D text gizmo", 25., Color::WHITE); | ||
| /// } | ||
| /// # bevy_ecs::system::assert_is_system(system); | ||
| /// ``` | ||
| pub fn text_2d( | ||
| &mut self, | ||
| isometry: impl Into<Isometry2d>, | ||
| text: &str, | ||
| size: f32, | ||
| color: impl Into<Color>, | ||
| ) { | ||
| let isometry = isometry.into(); | ||
| let color = color.into(); | ||
| let scale = size / SIMPLEX_CAP_HEIGHT; | ||
| let line_height = LINE_HEIGHT * size; | ||
| let space_advance = SIMPLEX_GLYPHS[0].0 as f32 * scale; | ||
|
|
||
| let mut rx = 0.0; | ||
| let mut ry = 0.0; | ||
|
|
||
| for c in text.chars() { | ||
| if c == '\n' { | ||
| rx = 0.0; | ||
| ry -= line_height; | ||
| continue; | ||
| } | ||
|
|
||
| let code_point = c as usize; | ||
| if !(SIMPLEX_ASCII_START..=SIMPLEX_ASCII_END).contains(&code_point) { | ||
| rx += space_advance; | ||
| continue; | ||
| } | ||
|
|
||
| let glyph = &SIMPLEX_GLYPHS[code_point - SIMPLEX_ASCII_START]; | ||
| let advance = glyph.0 as f32 * scale; | ||
|
|
||
| for stroke_index in glyph.1.clone() { | ||
| let stroke = SIMPLEX_STROKES[stroke_index].clone(); | ||
| if stroke.len() < 2 { | ||
| continue; | ||
| } | ||
|
|
||
| self.linestrip_2d( | ||
| SIMPLEX_POSITIONS[stroke].iter().map(|&[x, y]| { | ||
| isometry | ||
| * Vec2::new( | ||
| rx + scale * x as f32, | ||
| ry - scale * (SIMPLEX_CAP_HEIGHT - y as f32), | ||
| ) | ||
| }), | ||
| color, | ||
| ); | ||
| } | ||
|
|
||
| rx += advance; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to display a replacement character
�for characters outside the range which are not whitespaces (if the font does have such a character) ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this, possibly? It wouldn't need an extra character, we could draw the question mark character from the font and then add a diamond border outline around it like the � character (except not filled in).
It might be best leaving it to a follow up PR, and keep this one simple.