add coloring per text section in gizmos#23120
add coloring per text section in gizmos#23120nuts-rice wants to merge 4 commits intobevyengine:mainfrom
Conversation
|
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
There was a problem hiding this comment.
Yeah this is pretty good, the only problem is the extra allocations, which should be avoidable if StrokeTextLayout's text: &str field is replaced with a slice or iterator. Then make a function, something like:
fn colored_chars(s: &[(&str, Color)]) -> impl Iterator<Item = (char, Color)> {
s.into_iter()
.map(|(t, color)| t.chars().map(|c| (c, *color)))
.flatten()
}that maps the sections to an iterator that yields the individual chars and their colors for rendering.
|
|
||
| /// Builds a parallel per-character color array. | ||
| fn build_sections<S: AsRef<str>, C: Into<Color>>( | ||
| sections: impl IntoIterator<Item = (S, C)>, | ||
| ) -> (String, Vec<Color>) { | ||
| let mut full_text = String::new(); | ||
| let mut char_colors: Vec<Color> = Vec::new(); | ||
| for (text, color) in sections { | ||
| let text = text.as_ref(); | ||
| let color: Color = color.into(); | ||
| full_text.push_str(text); | ||
| for _ in text.chars() { | ||
| char_colors.push(color); | ||
| } | ||
| } | ||
| (full_text, char_colors) | ||
| } |
There was a problem hiding this comment.
Everything else is fine, but we don't want the extra allocations from this function. Instead of this, you could just replace the Chars iterator that is created at the start of the render function with an iterator that yields (char, Color) tuples.
| where | ||
| F: Fn(usize) -> Color + 'a, | ||
| { | ||
| let mut chars = self.text.chars(); |
There was a problem hiding this comment.
Instead of StrokeTextLayout's text field being an &str, it could be a Clone + IntoIterator<Item = (&'a str, Color)> or just an (&'str, Color) slice. Then it just needs another function that takes an (&'a str, Color) iterator and yields a sequence of (char, Color)s. So this line would become something like:
| let mut chars = self.text.chars(); | |
| let mut colored_chars = self.colored_chars(); |
| let c = chars.next()?; | ||
| let char_color = color_fn(char_idx); |
There was a problem hiding this comment.
Then here, the iterator yields the char and color together:
| let c = chars.next()?; | |
| let char_color = color_fn(char_idx); | |
| let (c, char_color) = colored_chars.next()?; |
and everything else should work?
|
Sorry was really tired last night and my review comments were kind of rambling, hopefully you get the idea though. |
|
you're alright, gonna resolve them soon |
|
It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note. Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes. |
|
|
||
| /// Returns an iterator over the font strokes for this text layout, grouped into polylines | ||
| /// of `Vec2` points, each paired with its color from the text sections. | ||
| pub fn render_colored( |
There was a problem hiding this comment.
| pub fn render_colored( | |
| pub fn render( |
| /// Returns an iterator over the font strokes for this text layout, | ||
| /// grouped into polylines of `Vec2` points. | ||
| pub fn render(&'a self) -> impl Iterator<Item = impl Iterator<Item = Vec2>> + 'a { | ||
| let mut chars = self.text.chars(); | ||
| let mut x = 0.0; | ||
| self.render_colored().map(|(_, stroke)| stroke) | ||
| } |
There was a problem hiding this comment.
Doesn't seem like this function is useful anymore, should just delete it and rename the other one back to render I think.
| @@ -4,11 +4,12 @@ authors: ["@ickshonpe"] | |||
| pull_requests: [22732] | |||
There was a problem hiding this comment.
| pull_requests: [22732] | |
| pull_requests: [22732, 23120] |
| @@ -4,11 +4,12 @@ authors: ["@ickshonpe"] | |||
ccf1541 to
ed05f64
Compare

Objective
bevy_gizmos: Multicolored text support. #23002Solution
StrokeTextLayout::render_with_color_fn()to return polyline and color pairingtext_sections()to build concat string consisting of sections of character-coloring pair(char, Color)textfield inStrokeTextLayoutwithsectionsthat is an iterable&'a [(&'a str, Color)]Testing
Showcase
Example Usage: