Skip to content
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

split up TextStyle #15857

Merged
merged 61 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
a6d1ac9
split up textstyle
ickshonpe Oct 11, 2024
f225ee8
Merge branch 'main' into text-style-split
ickshonpe Oct 11, 2024
300d378
update text_access
ickshonpe Oct 11, 2024
222efd3
updated some example
ickshonpe Oct 11, 2024
86cd1cb
Added `TextColor` to bevy_text prelude
ickshonpe Oct 11, 2024
b89d4fc
Updated `text` example
ickshonpe Oct 11, 2024
3a97382
updated examples
ickshonpe Oct 11, 2024
4c4bf5c
updated more examples
ickshonpe Oct 11, 2024
8858bac
updated more examples
ickshonpe Oct 11, 2024
662071f
updated `virtual_time` example
ickshonpe Oct 11, 2024
ebfe991
updated text_access, added color accessors
ickshonpe Oct 11, 2024
2356f9a
more examples
ickshonpe Oct 11, 2024
38f4ff3
update example
ickshonpe Oct 11, 2024
29a5627
updated example
ickshonpe Oct 11, 2024
41aa6c2
added `get_color` to `TextReader`
ickshonpe Oct 11, 2024
a26024f
Added BLACK and WHITE consts to TextColor
ickshonpe Oct 11, 2024
47bebed
added docstrings to new consts
ickshonpe Oct 11, 2024
ecce0e8
updated more examples
ickshonpe Oct 11, 2024
81c22e9
more examples
ickshonpe Oct 11, 2024
af8805a
Derived Copy, Deref and DerefMut for TextColor
ickshonpe Oct 11, 2024
0c3a03b
Updated fps_overlay
ickshonpe Oct 11, 2024
7159d6b
more examples
ickshonpe Oct 11, 2024
fd6b7b9
more examples
ickshonpe Oct 11, 2024
f453fcf
Require `TextColor` for `TextSpan`
ickshonpe Oct 11, 2024
5963080
example
ickshonpe Oct 11, 2024
0537233
fix fps_overlay color updates
ickshonpe Oct 11, 2024
e51cf65
fix fps_overlay again
ickshonpe Oct 11, 2024
d474caf
Updated more examples
ickshonpe Oct 11, 2024
4b75e38
more examples
ickshonpe Oct 11, 2024
fd2b81d
more examples
ickshonpe Oct 11, 2024
7b4474f
another example
ickshonpe Oct 11, 2024
985d208
example
ickshonpe Oct 11, 2024
c13dc88
more examples
ickshonpe Oct 11, 2024
26ed300
another example
ickshonpe Oct 11, 2024
26a8142
updated remaining UI examples
ickshonpe Oct 11, 2024
917f9a5
Migrated remaining examples
ickshonpe Oct 11, 2024
40ea18a
renamed `style` on `TextReader` to `font`, added `color` method
ickshonpe Oct 11, 2024
4880eb8
Fixed doc comment links
ickshonpe Oct 11, 2024
3369175
Fixed another doc comment link
ickshonpe Oct 11, 2024
791986d
Fixed more comments
ickshonpe Oct 11, 2024
385ced5
Removed trailing whitespaces
ickshonpe Oct 11, 2024
282b400
Fixed some more comments
ickshonpe Oct 11, 2024
b78acd1
Removed unneeded conversion
ickshonpe Oct 11, 2024
590ee96
Replaced missing text color in easing_functions example.
ickshonpe Oct 11, 2024
4dbaf9b
fixed text colors in bevymark example
ickshonpe Oct 11, 2024
0f2a4cd
fixed `TextSpan` doc example`
ickshonpe Oct 11, 2024
c14e50d
fixed doctests
ickshonpe Oct 11, 2024
d3d35f2
Merge branch 'text-style-split' of https://github.com/ickshonpe/bevy …
ickshonpe Oct 11, 2024
a39e416
Fixes for doc tests
ickshonpe Oct 11, 2024
13b1846
Merge branch 'main' into text-style-split
ickshonpe Oct 11, 2024
8c77e36
Fixed widget/text module's doc test
ickshonpe Oct 11, 2024
37fc79f
add a From Into<Color> for TextColor impl
ickshonpe Oct 12, 2024
5c0d007
Fixed namings in `pipeline`
ickshonpe Oct 12, 2024
da84131
another renaming
ickshonpe Oct 12, 2024
4796821
More renamings
ickshonpe Oct 12, 2024
bda4be2
More renamings
ickshonpe Oct 12, 2024
270b640
Merge branch 'main' into text-style-split
ickshonpe Oct 12, 2024
868b270
renaming to fit new api
ickshonpe Oct 12, 2024
bc9ae9a
More renamings
ickshonpe Oct 12, 2024
0833e1e
Removed `text_style` variable from example.
ickshonpe Oct 12, 2024
07da055
update example variable names
ickshonpe Oct 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions crates/bevy_dev_tools/src/fps_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bevy_ecs::{
};
use bevy_hierarchy::{BuildChildren, ChildBuild};
use bevy_render::view::Visibility;
use bevy_text::{Font, TextSpan, TextStyle};
use bevy_text::{Font, TextColor, TextFont, TextSpan};
use bevy_ui::{
node_bundles::NodeBundle,
widget::{Text, UiTextWriter},
Expand Down Expand Up @@ -62,20 +62,22 @@ impl Plugin for FpsOverlayPlugin {
#[derive(Resource, Clone)]
pub struct FpsOverlayConfig {
/// Configuration of text in the overlay.
pub text_config: TextStyle,
pub text_config: TextFont,
/// Color of text in the overlay.
pub text_color: Color,
/// Displays the FPS overlay if true.
pub enabled: bool,
}

impl Default for FpsOverlayConfig {
fn default() -> Self {
FpsOverlayConfig {
text_config: TextStyle {
text_config: TextFont {
font: Handle::<Font>::default(),
font_size: 32.0,
color: Color::WHITE,
..default()
},
text_color: Color::WHITE,
enabled: true,
}
}
Expand All @@ -102,6 +104,7 @@ fn setup(mut commands: Commands, overlay_config: Res<FpsOverlayConfig>) {
p.spawn((
Text::new("FPS: "),
overlay_config.text_config.clone(),
TextColor(overlay_config.text_color),
FpsText,
))
.with_child((TextSpan::default(), overlay_config.text_config.clone()));
Expand All @@ -128,9 +131,10 @@ fn customize_text(
mut writer: UiTextWriter,
) {
for entity in &query {
writer.for_each_style(entity, |mut style| {
*style = overlay_config.text_config.clone();
writer.for_each_font(entity, |mut font| {
*font = overlay_config.text_config.clone();
});
writer.for_each_color(entity, |mut color| color.0 = overlay_config.text_color);
Comment on lines -131 to +137
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep for_each_style and have it pass both the font and color as params?

Copy link
Contributor Author

@ickshonpe ickshonpe Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but I'm wondering if all these helper functions are even needed though. Users can just use for_each and ignore the fields that they don't need? We could return a struct with named fields instread of an optional tuple to make it more ergonomic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I believe that we should leave this helpers to a follow-up PR so we can discuss better what to do with them

}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_text/src/font_atlas_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub struct FontAtlasKey(pub u32, pub FontSmoothing);
/// A `FontAtlasSet` is an [`Asset`].
///
/// There is one `FontAtlasSet` for each font:
/// - When a [`Font`] is loaded as an asset and then used in [`TextStyle`](crate::TextStyle),
/// - When a [`Font`] is loaded as an asset and then used in [`TextFont`](crate::TextFont),
/// a `FontAtlasSet` asset is created from a weak handle to the `Font`.
/// - ~When a font is loaded as a system font, and then used in [`TextStyle`](crate::TextStyle),
/// - ~When a font is loaded as a system font, and then used in [`TextFont`](crate::TextFont),
/// a `FontAtlasSet` asset is created and stored with a strong handle to the `FontAtlasSet`.~
/// (*Note that system fonts are not currently supported by the `TextPipeline`.*)
///
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub use text_access::*;
pub mod prelude {
#[doc(hidden)]
pub use crate::{
Font, JustifyText, LineBreak, Text2d, TextError, TextLayout, TextReader2d, TextSpan,
TextStyle, TextWriter2d,
Font, JustifyText, LineBreak, Text2d, TextColor, TextError, TextFont, TextLayout,
TextReader2d, TextSpan, TextWriter2d,
};
}

Expand Down
48 changes: 26 additions & 22 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::sync::Arc;

use bevy_asset::{AssetId, Assets};
use bevy_color::Color;
use bevy_ecs::{
component::Component,
entity::Entity,
Expand All @@ -17,7 +18,7 @@ use cosmic_text::{Attrs, Buffer, Family, Metrics, Shaping, Wrap};

use crate::{
error::TextError, ComputedTextBlock, Font, FontAtlasSets, FontSmoothing, JustifyText,
LineBreak, PositionedGlyph, TextBounds, TextEntity, TextLayout, TextStyle, YAxisOrientation,
LineBreak, PositionedGlyph, TextBounds, TextEntity, TextFont, TextLayout, YAxisOrientation,
};

/// A wrapper resource around a [`cosmic_text::FontSystem`]
Expand Down Expand Up @@ -70,7 +71,7 @@ pub struct TextPipeline {
/// Buffered vec for collecting spans.
///
/// See [this dark magic](https://users.rust-lang.org/t/how-to-cache-a-vectors-capacity/94478/10).
spans_buffer: Vec<(usize, &'static str, &'static TextStyle, FontFaceInfo)>,
spans_buffer: Vec<(usize, &'static str, &'static TextFont, FontFaceInfo)>,
/// Buffered vec for collecting info for glyph assembly.
glyph_info: Vec<(AssetId<Font>, FontSmoothing)>,
}
Expand All @@ -83,7 +84,7 @@ impl TextPipeline {
pub fn update_buffer<'a>(
&mut self,
fonts: &Assets<Font>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextStyle)>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextFont, Color)>,
linebreak: LineBreak,
justify: JustifyText,
bounds: TextBounds,
Expand All @@ -96,22 +97,22 @@ impl TextPipeline {
// Collect span information into a vec. This is necessary because font loading requires mut access
// to FontSystem, which the cosmic-text Buffer also needs.
let mut font_size: f32 = 0.;
let mut spans: Vec<(usize, &str, &TextStyle, FontFaceInfo)> =
let mut spans: Vec<(usize, &str, &TextFont, FontFaceInfo, Color)> =
core::mem::take(&mut self.spans_buffer)
.into_iter()
.map(|_| -> (usize, &str, &TextStyle, FontFaceInfo) { unreachable!() })
.map(|_| -> (usize, &str, &TextFont, FontFaceInfo, Color) { unreachable!() })
.collect();

computed.entities.clear();

for (span_index, (entity, depth, span, style)) in text_spans.enumerate() {
for (span_index, (entity, depth, span, style, color)) in text_spans.enumerate() {
// Return early if a font is not loaded yet.
if !fonts.contains(style.font.id()) {
spans.clear();
self.spans_buffer = spans
.into_iter()
.map(
|_| -> (usize, &'static str, &'static TextStyle, FontFaceInfo) {
|_| -> (usize, &'static str, &'static TextFont, FontFaceInfo) {
unreachable!()
},
)
Expand All @@ -134,7 +135,7 @@ impl TextPipeline {
if scale_factor <= 0.0 || style.font_size <= 0.0 {
continue;
}
spans.push((span_index, span, style, face_info));
spans.push((span_index, span, style, face_info, color));
}

let line_height = font_size * 1.2;
Expand All @@ -151,12 +152,14 @@ impl TextPipeline {
// The section index is stored in the metadata of the spans, and could be used
// to look up the section the span came from and is not used internally
// in cosmic-text.
let spans_iter = spans.iter().map(|(span_index, span, style, font_info)| {
(
*span,
get_attrs(*span_index, style, font_info, scale_factor),
)
});
let spans_iter = spans
.iter()
.map(|(span_index, span, style, font_info, color)| {
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
(
*span,
get_attrs(*span_index, style, *color, font_info, scale_factor),
)
});

// Update the buffer.
let buffer = &mut computed.buffer;
Expand Down Expand Up @@ -186,7 +189,7 @@ impl TextPipeline {
spans.clear();
self.spans_buffer = spans
.into_iter()
.map(|_| -> (usize, &'static str, &'static TextStyle, FontFaceInfo) { unreachable!() })
.map(|_| -> (usize, &'static str, &'static TextFont, FontFaceInfo) { unreachable!() })
.collect();

Ok(())
Expand All @@ -201,7 +204,7 @@ impl TextPipeline {
&mut self,
layout_info: &mut TextLayoutInfo,
fonts: &Assets<Font>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextStyle)>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextFont, Color)>,
scale_factor: f64,
layout: &TextLayout,
bounds: TextBounds,
Expand All @@ -222,7 +225,7 @@ impl TextPipeline {
// Extract font ids from the iterator while traversing it.
let mut glyph_info = core::mem::take(&mut self.glyph_info);
glyph_info.clear();
let text_spans = text_spans.inspect(|(_, _, _, style)| {
let text_spans = text_spans.inspect(|(_, _, _, style, _)| {
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
glyph_info.push((style.font.id(), style.font_smoothing));
});

Expand Down Expand Up @@ -335,7 +338,7 @@ impl TextPipeline {
&mut self,
entity: Entity,
fonts: &Assets<Font>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextStyle)>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextFont, Color)>,
scale_factor: f64,
layout: &TextLayout,
computed: &mut ComputedTextBlock,
Expand Down Expand Up @@ -427,7 +430,7 @@ impl TextMeasureInfo {
}

fn load_font_to_fontdb(
style: &TextStyle,
style: &TextFont,
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
font_system: &mut cosmic_text::FontSystem,
map_handle_to_font_id: &mut HashMap<AssetId<Font>, (cosmic_text::fontdb::ID, Arc<str>)>,
fonts: &Assets<Font>,
Expand Down Expand Up @@ -461,10 +464,11 @@ fn load_font_to_fontdb(
}
}

/// Translates [`TextStyle`] to [`Attrs`].
/// Translates [`TextFont`] to [`Attrs`].
fn get_attrs<'a>(
span_index: usize,
style: &TextStyle,
style: &TextFont,
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
color: Color,
face_info: &'a FontFaceInfo,
scale_factor: f64,
) -> Attrs<'a> {
Expand All @@ -475,7 +479,7 @@ fn get_attrs<'a>(
.style(face_info.style)
.weight(face_info.weight)
.metrics(Metrics::relative(style.font_size, 1.2).scale(scale_factor as f32))
.color(cosmic_text::Color(style.color.to_linear().as_u32()));
.color(cosmic_text::Color(color.to_linear().as_u32()));
attrs
}

Expand Down
Loading