Skip to content

Commit

Permalink
[bevy_text] Add offset to TextSection
Browse files Browse the repository at this point in the history
This makes it possible to shift text (such as for animations), while
still laying it out with ab_glyph.
  • Loading branch information
zmbush committed Jun 26, 2024
1 parent 19d078c commit 0c3b9b8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
20 changes: 17 additions & 3 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl TextPipeline {
y_axis_orientation: YAxisOrientation,
) -> Result<TextLayoutInfo, TextError> {
let mut scaled_fonts = Vec::with_capacity(sections.len());
let glyph_offsets: Vec<_> = sections
.iter()
.flat_map(|section| {
std::iter::repeat(section.offset).take(section.value.chars().count())
})
.collect();
let sections = sections
.iter()
.map(|section| {
Expand All @@ -77,9 +83,17 @@ impl TextPipeline {
})
.collect::<Result<Vec<_>, _>>()?;

let section_glyphs =
self.brush
.compute_glyphs(&sections, bounds, text_alignment, linebreak_behavior)?;
let section_glyphs: Vec<_> = self
.brush
.compute_glyphs(&sections, bounds, text_alignment, linebreak_behavior)?
.into_iter()
.zip(glyph_offsets)
.map(|(mut g, offset)| {
g.glyph.position.x += offset.x;
g.glyph.position.y += offset.y;
g
})
.collect();

if section_glyphs.is_empty() {
return Ok(TextLayoutInfo::default());
Expand Down
20 changes: 18 additions & 2 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy_asset::Handle;
use bevy_color::Color;
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_math::Vec2;
use bevy_reflect::prelude::*;
use bevy_utils::default;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -106,10 +107,21 @@ impl Text {
}
}

#[derive(Debug, Default, Clone, Reflect)]
#[derive(Debug, Clone, Reflect)]
pub struct TextSection {
pub value: String,
pub style: TextStyle,
pub offset: Vec2,
}

impl Default for TextSection {
fn default() -> Self {
Self {
value: Default::default(),
style: Default::default(),
offset: Vec2::new(0., 0.),
}
}
}

impl TextSection {
Expand All @@ -118,14 +130,18 @@ impl TextSection {
Self {
value: value.into(),
style,

..Default::default()
}
}

/// Create an empty [`TextSection`] from a style. Useful when the value will be set dynamically.
pub const fn from_style(style: TextStyle) -> Self {
Self {
value: String::new(),
style,

value: String::new(),
offset: Vec2::new(0., 0.),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/asset/multi_asset_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn setup_ui(mut commands: Commands) {
color: Color::BLACK,
..Default::default()
},
..Default::default()
}],
justify: JustifyText::Right,
..Default::default()
Expand Down
6 changes: 6 additions & 0 deletions examples/input/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
font: font.clone_weak(),
..default()
},
..default()
},
TextSection {
value: "false\n".to_string(),
Expand All @@ -51,13 +52,15 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 30.0,
..default()
},
..default()
},
TextSection {
value: "IME Active: ".to_string(),
style: TextStyle {
font: font.clone_weak(),
..default()
},
..default()
},
TextSection {
value: "false\n".to_string(),
Expand All @@ -66,6 +69,7 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 30.0,
..default()
},
..default()
},
TextSection {
value: "click to toggle IME, press return to start a new line\n\n".to_string(),
Expand All @@ -74,6 +78,7 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 18.0,
..default()
},
..default()
},
TextSection {
value: "".to_string(),
Expand All @@ -82,6 +87,7 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 25.0,
..default()
},
..default()
},
])
.with_style(Style {
Expand Down
1 change: 1 addition & 0 deletions examples/stress_tests/many_glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn setup(mut commands: Commands) {
font_size: 4.,
..default()
},
..default()
}],
justify: JustifyText::Left,
linebreak_behavior: BreakLineOn::AnyCharacter,
Expand Down
2 changes: 2 additions & 0 deletions examples/stress_tests/text_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: (4 + i % 10) as f32,
color: BLUE.into(),
},
..default()
},
TextSection {
value: "pipeline".repeat(i),
Expand All @@ -57,6 +58,7 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: (4 + i % 11) as f32,
color: YELLOW.into(),
},
..default()
},
]
})
Expand Down
5 changes: 5 additions & 0 deletions examples/tools/gamepad_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,17 @@ fn setup_sticks(
TextSection {
value: format!("{:.3}", 0.),
style: style.clone(),
..default()
},
TextSection {
value: ", ".to_string(),
style: style.clone(),
..default()
},
TextSection {
value: format!("{:.3}", 0.),
style,
..default()
},
]),
text_anchor: Anchor::BottomCenter,
Expand Down Expand Up @@ -424,10 +427,12 @@ fn setup_connected(mut commands: Commands) {
TextSection {
value: "Connected Gamepads:\n".to_string(),
style: text_style.clone(),
..default()
},
TextSection {
value: "None".to_string(),
style: text_style,
..default()
},
]),
style: Style {
Expand Down
1 change: 1 addition & 0 deletions examples/ui/text_wrap_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
sections: vec![TextSection {
value: message.clone(),
style: text_style.clone(),
..Default::default()
}],
justify: JustifyText::Left,
linebreak_behavior,
Expand Down

0 comments on commit 0c3b9b8

Please sign in to comment.