diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 894289445ae89..84e13b6be3305 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -1,3 +1,5 @@ +use std::borrow::Borrow; + use bevy_asset::Handle; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_reflect::prelude::*; @@ -59,7 +61,7 @@ impl Text { /// ) // You can still add an alignment. /// .with_alignment(TextAlignment::Center); /// ``` - pub fn from_section(value: impl Into, style: TextStyle) -> Self { + pub fn from_section(value: impl Into, style: impl Borrow) -> Self { Self { sections: vec![TextSection::new(value, style)], ..default() @@ -123,10 +125,10 @@ pub struct TextSection { impl TextSection { /// Create a new [`TextSection`]. - pub fn new(value: impl Into, style: TextStyle) -> Self { + pub fn new(value: impl Into, style: impl Borrow) -> Self { Self { value: value.into(), - style, + style: style.borrow().to_owned(), } } @@ -189,6 +191,7 @@ impl Default for TextStyle { } } + /// Determines how lines will be broken when preventing text from running out of bounds. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] #[reflect(Serialize, Deserialize)] diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index ad783173d1309..06611a83a88bd 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -44,7 +44,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // Demonstrate changing translation commands.spawn(( Text2dBundle { - text: Text::from_section("translation", text_style.clone()) + text: Text::from_section("translation", &text_style) .with_alignment(text_alignment), ..default() }, @@ -53,7 +53,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // Demonstrate changing rotation commands.spawn(( Text2dBundle { - text: Text::from_section("rotation", text_style.clone()).with_alignment(text_alignment), + text: Text::from_section("rotation", &text_style).with_alignment(text_alignment), ..default() }, AnimateRotation, @@ -89,7 +89,7 @@ fn setup(mut commands: Commands, asset_server: Res) { text: Text { sections: vec![TextSection::new( "this text wraps in the box\n(Unicode linebreaks)", - slightly_smaller_text_style.clone(), + &slightly_smaller_text_style, )], alignment: TextAlignment::Left, linebreak_behavior: BreakLineOn::WordBoundary, @@ -121,7 +121,7 @@ fn setup(mut commands: Commands, asset_server: Res) { text: Text { sections: vec![TextSection::new( "this text wraps in the box\n(AnyCharacter linebreaks)", - slightly_smaller_text_style.clone(), + &slightly_smaller_text_style, )], alignment: TextAlignment::Left, linebreak_behavior: BreakLineOn::AnyCharacter,