Skip to content

Commit

Permalink
use IntoIterator for text builders
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB committed Oct 3, 2024
1 parent 7431c56 commit 898708b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
29 changes: 19 additions & 10 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cosmic_text::{Buffer, Metrics};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;

use crate::{Font, TextLayoutInfo, TextRoot};
use crate::{Font, TextLayoutInfo, TextRoot, TextSpanComponent};
pub use cosmic_text::{
self, FamilyOwned as FontFamily, Stretch as FontStretch, Style as FontStyle,
Weight as FontWeight,
Expand Down Expand Up @@ -286,15 +286,18 @@ pub trait TextBuilderExt {
/// If no spans are provided then a default text entity will be spawned.
///
/// Returns an [`EntityCommands`] for the root entity.
fn spawn_text_block<R: TextRoot>(&mut self, spans: Vec<(String, TextStyle)>) -> EntityCommands;
fn spawn_text_block<R: TextRoot>(
&mut self,
spans: impl IntoIterator<Item = (String, TextStyle)>,
) -> EntityCommands;
}

impl TextBuilderExt for Commands<'_, '_> {
fn spawn_text_block<R: TextRoot>(
&mut self,
mut spans: Vec<(String, TextStyle)>,
spans: impl IntoIterator<Item = (String, TextStyle)>,
) -> EntityCommands {
let mut spans = spans.drain(..);
let mut spans = spans.into_iter();

// Root of the block.
let first = spans.next().unwrap_or_default();
Expand All @@ -312,9 +315,9 @@ impl TextBuilderExt for Commands<'_, '_> {
impl TextBuilderExt for EntityCommands<'_> {
fn spawn_text_block<R: TextRoot>(
&mut self,
mut spans: Vec<(String, TextStyle)>,
spans: impl IntoIterator<Item = (String, TextStyle)>,
) -> EntityCommands {
let mut spans = spans.drain(..);
let mut spans = spans.into_iter();

// Root of the block.
let first = spans.next().unwrap_or_default();
Expand All @@ -332,13 +335,19 @@ impl TextBuilderExt for EntityCommands<'_> {
/// Provides convenience methods for adding text spans to a text block.
pub trait TextSpanBuilderExt {
/// Adds a flat list of spans as children of the current entity.
fn with_spans<S: Component>(&mut self, spans: Vec<(S, TextStyle)>) -> &mut Self;
fn with_spans<S: TextSpanComponent>(
&mut self,
spans: impl IntoIterator<Item = (String, TextStyle)>,
) -> &mut Self;
}

impl TextSpanBuilderExt for EntityCommands<'_> {
fn with_spans<S: Component>(&mut self, mut spans: Vec<(S, TextStyle)>) -> &mut Self {
for (span, style) in spans.drain(..) {
self.with_child((span, style));
fn with_spans<S: TextSpanComponent>(
&mut self,
spans: impl IntoIterator<Item = (String, TextStyle)>,
) -> &mut Self {
for (span, style) in spans.into_iter() {
self.with_child((S::from(span), style));
}
self
}
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::pipeline::CosmicFontSystem;
use crate::{
ComputedTextBlock, Font, FontAtlasSets, LineBreak, PositionedGlyph, SwashCache, TextBlock,
TextBounds, TextError, TextLayoutInfo, TextPipeline, TextReader, TextRoot, TextSpanAccess,
TextStyle, TextWriter, YAxisOrientation,
TextSpanComponent, TextStyle, TextWriter, YAxisOrientation,
};
use bevy_asset::Assets;
use bevy_color::LinearRgba;
Expand Down Expand Up @@ -175,6 +175,8 @@ impl TextSpan2d {
}
}

impl TextSpanComponent for TextSpan2d {}

impl TextSpanAccess for TextSpan2d {
fn read_span(&self) -> &str {
self.as_str()
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_text/src/text_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ pub trait TextSpanAccess: Component {
/// Helper trait for the root text component in a text block.
pub trait TextRoot: TextSpanAccess + From<String> {
/// Component type for spans of text blocks starting with the root component.
type Span: TextSpanAccess + From<String>;
type Span: TextSpanComponent;
}

/// Helper trait for the text span components in a text block.
pub trait TextSpanComponent: TextSpanAccess + From<String> {}

#[derive(Resource, Default)]
pub(crate) struct TextIterScratch {
stack: Vec<(&'static Children, usize)>,
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bevy_sprite::TextureAtlasLayout;
use bevy_text::{
scale_value, ComputedTextBlock, CosmicFontSystem, Font, FontAtlasSets, LineBreak, SwashCache,
TextBlock, TextBounds, TextError, TextLayoutInfo, TextMeasureInfo, TextPipeline, TextReader,
TextRoot, TextSpanAccess, TextStyle, TextWriter, YAxisOrientation,
TextRoot, TextSpanAccess, TextSpanComponent, TextStyle, TextWriter, YAxisOrientation,
};
use bevy_transform::components::Transform;
use bevy_utils::{tracing::error, Entry};
Expand Down Expand Up @@ -192,6 +192,8 @@ impl TextSpan {
}
}

impl TextSpanComponent for TextSpan {}

impl TextSpanAccess for TextSpan {
fn read_span(&self) -> &str {
self.as_str()
Expand Down
8 changes: 4 additions & 4 deletions examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy::{
color::palettes::css::*,
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
prelude::*,
text::TextBuilderExt,
text::TextSpanBuilderExt,
ui::widget::UiTextWriter,
window::PresentMode,
};
Expand Down Expand Up @@ -162,7 +162,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
},
TextChanges
))
.with_spans::<TextSpan>(vec![
.with_spans::<TextSpan>([
(
"\nThis text changes in the bottom right".into(),
TextStyle {
Expand Down Expand Up @@ -198,7 +198,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
},
),
(
TextSpan::empty(),
String::default(),
TextStyle {
font: font.clone(),
font_size: 21.0,
Expand All @@ -216,7 +216,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
},
),
(
TextSpan::empty(),
String::default(),
TextStyle {
font: font.clone(),
font_size: 21.0,
Expand Down

0 comments on commit 898708b

Please sign in to comment.