mirror of
https://github.com/bevyengine/bevy
synced 2025-02-17 22:48:38 +00:00
make TextLayoutInfo a Component (#4460)
# Objective Make `TextLayoutInfo` more accessible as a component, rather than internal to `TextPipeline`. I am working on a plugin that manipulates these and there is no (mutable) access to them right now. ## Solution This changes `TextPipeline::queue_text` to return `TextLayoutInfo`'s rather than storing them in a map internally. `text2d_system` and `text_system` now take the returned `TextLayoutInfo` and store it as a component of the entity. I considered adding an accessor to `TextPipeline` (e.g. `get_glyphs_mut`) but this seems like it might be a little faster, and also has the added benefit of cleaning itself up when entities are removed. Right now nothing is ever removed from the glyphs map. ## Changelog Removed `DefaultTextPipeline`. `TextPipeline` no longer has a generic key type. `TextPipeline::queue_text` returns `TextLayoutInfo` directly. ## Migration Guide This might break a third-party crate? I could restore the orginal TextPipeline API as a wrapper around what's in this PR.
This commit is contained in:
parent
1914696a24
commit
bd68ba1c3c
5 changed files with 137 additions and 149 deletions
|
@ -28,13 +28,11 @@ pub mod prelude {
|
||||||
|
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
use bevy_asset::AddAsset;
|
use bevy_asset::AddAsset;
|
||||||
use bevy_ecs::{entity::Entity, schedule::ParallelSystemDescriptorCoercion};
|
use bevy_ecs::schedule::ParallelSystemDescriptorCoercion;
|
||||||
use bevy_render::{RenderApp, RenderStage};
|
use bevy_render::{RenderApp, RenderStage};
|
||||||
use bevy_sprite::SpriteSystem;
|
use bevy_sprite::SpriteSystem;
|
||||||
use bevy_window::ModifiesWindows;
|
use bevy_window::ModifiesWindows;
|
||||||
|
|
||||||
pub type DefaultTextPipeline = TextPipeline<Entity>;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct TextPlugin;
|
pub struct TextPlugin;
|
||||||
|
|
||||||
|
@ -48,7 +46,7 @@ impl Plugin for TextPlugin {
|
||||||
.register_type::<VerticalAlign>()
|
.register_type::<VerticalAlign>()
|
||||||
.register_type::<HorizontalAlign>()
|
.register_type::<HorizontalAlign>()
|
||||||
.init_asset_loader::<FontLoader>()
|
.init_asset_loader::<FontLoader>()
|
||||||
.insert_resource(DefaultTextPipeline::default())
|
.insert_resource(TextPipeline::default())
|
||||||
.add_system_to_stage(
|
.add_system_to_stage(
|
||||||
CoreStage::PostUpdate,
|
CoreStage::PostUpdate,
|
||||||
update_text2d_layout.after(ModifiesWindows),
|
update_text2d_layout.after(ModifiesWindows),
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
use ab_glyph::{PxScale, ScaleFont};
|
use ab_glyph::{PxScale, ScaleFont};
|
||||||
use bevy_asset::{Assets, Handle, HandleId};
|
use bevy_asset::{Assets, Handle, HandleId};
|
||||||
|
use bevy_ecs::component::Component;
|
||||||
use bevy_ecs::system::Resource;
|
use bevy_ecs::system::Resource;
|
||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
use bevy_render::texture::Image;
|
use bevy_render::texture::Image;
|
||||||
|
@ -15,29 +14,22 @@ use crate::{
|
||||||
TextAlignment, TextSection,
|
TextAlignment, TextSection,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Default, Resource)]
|
||||||
pub struct TextPipeline<ID> {
|
pub struct TextPipeline {
|
||||||
brush: GlyphBrush,
|
brush: GlyphBrush,
|
||||||
glyph_map: HashMap<ID, TextLayoutInfo>,
|
|
||||||
map_font_id: HashMap<HandleId, FontId>,
|
map_font_id: HashMap<HandleId, FontId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ID> Default for TextPipeline<ID> {
|
/// Render information for a corresponding [`Text`](crate::Text) component.
|
||||||
fn default() -> Self {
|
///
|
||||||
TextPipeline {
|
/// Contains scaled glyphs and their size. Generated via [`TextPipeline::queue_text`].
|
||||||
brush: GlyphBrush::default(),
|
#[derive(Component, Clone, Default, Debug)]
|
||||||
glyph_map: Default::default(),
|
|
||||||
map_font_id: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TextLayoutInfo {
|
pub struct TextLayoutInfo {
|
||||||
pub glyphs: Vec<PositionedGlyph>,
|
pub glyphs: Vec<PositionedGlyph>,
|
||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ID: Hash + Eq> TextPipeline<ID> {
|
impl TextPipeline {
|
||||||
pub fn get_or_insert_font_id(&mut self, handle: &Handle<Font>, font: &Font) -> FontId {
|
pub fn get_or_insert_font_id(&mut self, handle: &Handle<Font>, font: &Font) -> FontId {
|
||||||
let brush = &mut self.brush;
|
let brush = &mut self.brush;
|
||||||
*self
|
*self
|
||||||
|
@ -46,14 +38,9 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
||||||
.or_insert_with(|| brush.add_font(handle.clone(), font.font.clone()))
|
.or_insert_with(|| brush.add_font(handle.clone(), font.font.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_glyphs(&self, id: &ID) -> Option<&TextLayoutInfo> {
|
|
||||||
self.glyph_map.get(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn queue_text(
|
pub fn queue_text(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: ID,
|
|
||||||
fonts: &Assets<Font>,
|
fonts: &Assets<Font>,
|
||||||
sections: &[TextSection],
|
sections: &[TextSection],
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
|
@ -62,7 +49,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
||||||
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
|
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
|
||||||
texture_atlases: &mut Assets<TextureAtlas>,
|
texture_atlases: &mut Assets<TextureAtlas>,
|
||||||
textures: &mut Assets<Image>,
|
textures: &mut Assets<Image>,
|
||||||
) -> Result<(), TextError> {
|
) -> Result<TextLayoutInfo, TextError> {
|
||||||
let mut scaled_fonts = Vec::new();
|
let mut scaled_fonts = Vec::new();
|
||||||
let sections = sections
|
let sections = sections
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -90,14 +77,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
||||||
.compute_glyphs(§ions, bounds, text_alignment)?;
|
.compute_glyphs(§ions, bounds, text_alignment)?;
|
||||||
|
|
||||||
if section_glyphs.is_empty() {
|
if section_glyphs.is_empty() {
|
||||||
self.glyph_map.insert(
|
return Ok(TextLayoutInfo::default());
|
||||||
id,
|
|
||||||
TextLayoutInfo {
|
|
||||||
glyphs: Vec::new(),
|
|
||||||
size: Vec2::ZERO,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut min_x: f32 = std::f32::MAX;
|
let mut min_x: f32 = std::f32::MAX;
|
||||||
|
@ -125,8 +105,6 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
||||||
textures,
|
textures,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
self.glyph_map.insert(id, TextLayoutInfo { glyphs, size });
|
Ok(TextLayoutInfo { glyphs, size })
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use bevy_ecs::{
|
||||||
event::EventReader,
|
event::EventReader,
|
||||||
query::Changed,
|
query::Changed,
|
||||||
reflect::ReflectComponent,
|
reflect::ReflectComponent,
|
||||||
system::{Local, Query, Res, ResMut},
|
system::{Commands, Local, Query, Res, ResMut},
|
||||||
};
|
};
|
||||||
use bevy_math::{Vec2, Vec3};
|
use bevy_math::{Vec2, Vec3};
|
||||||
use bevy_reflect::Reflect;
|
use bevy_reflect::Reflect;
|
||||||
|
@ -22,7 +22,8 @@ use bevy_utils::HashSet;
|
||||||
use bevy_window::{WindowId, WindowScaleFactorChanged, Windows};
|
use bevy_window::{WindowId, WindowScaleFactorChanged, Windows};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
DefaultTextPipeline, Font, FontAtlasSet, HorizontalAlign, Text, TextError, VerticalAlign,
|
Font, FontAtlasSet, HorizontalAlign, Text, TextError, TextLayoutInfo, TextPipeline,
|
||||||
|
VerticalAlign,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The calculated size of text drawn in 2D scene.
|
/// The calculated size of text drawn in 2D scene.
|
||||||
|
@ -69,13 +70,13 @@ pub struct Text2dBundle {
|
||||||
pub fn extract_text2d_sprite(
|
pub fn extract_text2d_sprite(
|
||||||
mut extracted_sprites: ResMut<ExtractedSprites>,
|
mut extracted_sprites: ResMut<ExtractedSprites>,
|
||||||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
||||||
text_pipeline: Extract<Res<DefaultTextPipeline>>,
|
|
||||||
windows: Extract<Res<Windows>>,
|
windows: Extract<Res<Windows>>,
|
||||||
text2d_query: Extract<
|
text2d_query: Extract<
|
||||||
Query<(
|
Query<(
|
||||||
Entity,
|
Entity,
|
||||||
&ComputedVisibility,
|
&ComputedVisibility,
|
||||||
&Text,
|
&Text,
|
||||||
|
&TextLayoutInfo,
|
||||||
&GlobalTransform,
|
&GlobalTransform,
|
||||||
&Text2dSize,
|
&Text2dSize,
|
||||||
)>,
|
)>,
|
||||||
|
@ -83,15 +84,15 @@ pub fn extract_text2d_sprite(
|
||||||
) {
|
) {
|
||||||
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
|
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
|
||||||
|
|
||||||
for (entity, computed_visibility, text, text_transform, calculated_size) in text2d_query.iter()
|
for (entity, computed_visibility, text, text_layout_info, text_transform, calculated_size) in
|
||||||
|
text2d_query.iter()
|
||||||
{
|
{
|
||||||
if !computed_visibility.is_visible() {
|
if !computed_visibility.is_visible() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let (width, height) = (calculated_size.size.x, calculated_size.size.y);
|
let (width, height) = (calculated_size.size.x, calculated_size.size.y);
|
||||||
|
|
||||||
if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
|
let text_glyphs = &text_layout_info.glyphs;
|
||||||
let text_glyphs = &text_layout.glyphs;
|
|
||||||
let alignment_offset = match text.alignment.vertical {
|
let alignment_offset = match text.alignment.vertical {
|
||||||
VerticalAlign::Top => Vec3::new(0.0, -height, 0.0),
|
VerticalAlign::Top => Vec3::new(0.0, -height, 0.0),
|
||||||
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
|
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
|
||||||
|
@ -140,13 +141,13 @@ pub fn extract_text2d_sprite(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the layout and size information whenever the text or style is changed.
|
/// Updates the layout and size information whenever the text or style is changed.
|
||||||
/// This information is computed by the `TextPipeline` on insertion, then stored.
|
/// This information is computed by the `TextPipeline` on insertion, then stored.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn update_text2d_layout(
|
pub fn update_text2d_layout(
|
||||||
|
mut commands: Commands,
|
||||||
// Text items which should be reprocessed again, generally when the font hasn't loaded yet.
|
// Text items which should be reprocessed again, generally when the font hasn't loaded yet.
|
||||||
mut queue: Local<HashSet<Entity>>,
|
mut queue: Local<HashSet<Entity>>,
|
||||||
mut textures: ResMut<Assets<Image>>,
|
mut textures: ResMut<Assets<Image>>,
|
||||||
|
@ -155,20 +156,23 @@ pub fn update_text2d_layout(
|
||||||
mut scale_factor_changed: EventReader<WindowScaleFactorChanged>,
|
mut scale_factor_changed: EventReader<WindowScaleFactorChanged>,
|
||||||
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
||||||
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
|
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
|
||||||
mut text_pipeline: ResMut<DefaultTextPipeline>,
|
mut text_pipeline: ResMut<TextPipeline>,
|
||||||
mut text_query: Query<(
|
mut text_query: Query<(
|
||||||
Entity,
|
Entity,
|
||||||
Changed<Text>,
|
Changed<Text>,
|
||||||
&Text,
|
&Text,
|
||||||
Option<&Text2dBounds>,
|
Option<&Text2dBounds>,
|
||||||
&mut Text2dSize,
|
&mut Text2dSize,
|
||||||
|
Option<&mut TextLayoutInfo>,
|
||||||
)>,
|
)>,
|
||||||
) {
|
) {
|
||||||
// We need to consume the entire iterator, hence `last`
|
// We need to consume the entire iterator, hence `last`
|
||||||
let factor_changed = scale_factor_changed.iter().last().is_some();
|
let factor_changed = scale_factor_changed.iter().last().is_some();
|
||||||
let scale_factor = windows.scale_factor(WindowId::primary());
|
let scale_factor = windows.scale_factor(WindowId::primary());
|
||||||
|
|
||||||
for (entity, text_changed, text, maybe_bounds, mut calculated_size) in &mut text_query {
|
for (entity, text_changed, text, maybe_bounds, mut calculated_size, text_layout_info) in
|
||||||
|
&mut text_query
|
||||||
|
{
|
||||||
if factor_changed || text_changed || queue.remove(&entity) {
|
if factor_changed || text_changed || queue.remove(&entity) {
|
||||||
let text_bounds = match maybe_bounds {
|
let text_bounds = match maybe_bounds {
|
||||||
Some(bounds) => Vec2::new(
|
Some(bounds) => Vec2::new(
|
||||||
|
@ -178,7 +182,6 @@ pub fn update_text2d_layout(
|
||||||
None => Vec2::new(f32::MAX, f32::MAX),
|
None => Vec2::new(f32::MAX, f32::MAX),
|
||||||
};
|
};
|
||||||
match text_pipeline.queue_text(
|
match text_pipeline.queue_text(
|
||||||
entity,
|
|
||||||
&fonts,
|
&fonts,
|
||||||
&text.sections,
|
&text.sections,
|
||||||
scale_factor,
|
scale_factor,
|
||||||
|
@ -196,14 +199,17 @@ pub fn update_text2d_layout(
|
||||||
Err(e @ TextError::FailedToAddGlyph(_)) => {
|
Err(e @ TextError::FailedToAddGlyph(_)) => {
|
||||||
panic!("Fatal error when processing text: {}.", e);
|
panic!("Fatal error when processing text: {}.", e);
|
||||||
}
|
}
|
||||||
Ok(()) => {
|
Ok(info) => {
|
||||||
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
|
|
||||||
"Failed to get glyphs from the pipeline that have just been computed",
|
|
||||||
);
|
|
||||||
calculated_size.size = Vec2::new(
|
calculated_size.size = Vec2::new(
|
||||||
scale_value(text_layout_info.size.x, 1. / scale_factor),
|
scale_value(info.size.x, 1. / scale_factor),
|
||||||
scale_value(text_layout_info.size.y, 1. / scale_factor),
|
scale_value(info.size.y, 1. / scale_factor),
|
||||||
);
|
);
|
||||||
|
match text_layout_info {
|
||||||
|
Some(mut t) => *t = info,
|
||||||
|
None => {
|
||||||
|
commands.entity(entity).insert(info);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ use bevy_render::{
|
||||||
Extract, RenderApp, RenderStage,
|
Extract, RenderApp, RenderStage,
|
||||||
};
|
};
|
||||||
use bevy_sprite::{SpriteAssetEvents, TextureAtlas};
|
use bevy_sprite::{SpriteAssetEvents, TextureAtlas};
|
||||||
use bevy_text::{DefaultTextPipeline, Text};
|
use bevy_text::{Text, TextLayoutInfo};
|
||||||
use bevy_transform::components::GlobalTransform;
|
use bevy_transform::components::GlobalTransform;
|
||||||
use bevy_utils::FloatOrd;
|
use bevy_utils::FloatOrd;
|
||||||
use bevy_utils::HashMap;
|
use bevy_utils::HashMap;
|
||||||
|
@ -272,21 +272,21 @@ pub fn extract_default_ui_camera_view<T: Component>(
|
||||||
pub fn extract_text_uinodes(
|
pub fn extract_text_uinodes(
|
||||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
||||||
text_pipeline: Extract<Res<DefaultTextPipeline>>,
|
|
||||||
windows: Extract<Res<Windows>>,
|
windows: Extract<Res<Windows>>,
|
||||||
uinode_query: Extract<
|
uinode_query: Extract<
|
||||||
Query<(
|
Query<(
|
||||||
Entity,
|
|
||||||
&Node,
|
&Node,
|
||||||
&GlobalTransform,
|
&GlobalTransform,
|
||||||
&Text,
|
&Text,
|
||||||
|
&TextLayoutInfo,
|
||||||
&ComputedVisibility,
|
&ComputedVisibility,
|
||||||
Option<&CalculatedClip>,
|
Option<&CalculatedClip>,
|
||||||
)>,
|
)>,
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
|
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
|
||||||
for (entity, uinode, global_transform, text, visibility, clip) in uinode_query.iter() {
|
for (uinode, global_transform, text, text_layout_info, visibility, clip) in uinode_query.iter()
|
||||||
|
{
|
||||||
if !visibility.is_visible() {
|
if !visibility.is_visible() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -294,8 +294,7 @@ pub fn extract_text_uinodes(
|
||||||
if uinode.size == Vec2::ZERO {
|
if uinode.size == Vec2::ZERO {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
|
let text_glyphs = &text_layout_info.glyphs;
|
||||||
let text_glyphs = &text_layout.glyphs;
|
|
||||||
let alignment_offset = (uinode.size / -2.0).extend(0.0);
|
let alignment_offset = (uinode.size / -2.0).extend(0.0);
|
||||||
|
|
||||||
let mut color = Color::WHITE;
|
let mut color = Color::WHITE;
|
||||||
|
@ -333,7 +332,6 @@ pub fn extract_text_uinodes(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
|
@ -3,12 +3,12 @@ use bevy_asset::Assets;
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
query::{Changed, Or, With},
|
query::{Changed, Or, With},
|
||||||
system::{Local, ParamSet, Query, Res, ResMut},
|
system::{Commands, Local, ParamSet, Query, Res, ResMut},
|
||||||
};
|
};
|
||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
use bevy_render::texture::Image;
|
use bevy_render::texture::Image;
|
||||||
use bevy_sprite::TextureAtlas;
|
use bevy_sprite::TextureAtlas;
|
||||||
use bevy_text::{DefaultTextPipeline, Font, FontAtlasSet, Text, TextError};
|
use bevy_text::{Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline};
|
||||||
use bevy_window::Windows;
|
use bevy_window::Windows;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
|
@ -38,6 +38,7 @@ pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f6
|
||||||
/// This information is computed by the `TextPipeline` on insertion, then stored.
|
/// This information is computed by the `TextPipeline` on insertion, then stored.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn text_system(
|
pub fn text_system(
|
||||||
|
mut commands: Commands,
|
||||||
mut queued_text: Local<QueuedText>,
|
mut queued_text: Local<QueuedText>,
|
||||||
mut last_scale_factor: Local<f64>,
|
mut last_scale_factor: Local<f64>,
|
||||||
mut textures: ResMut<Assets<Image>>,
|
mut textures: ResMut<Assets<Image>>,
|
||||||
|
@ -46,11 +47,16 @@ pub fn text_system(
|
||||||
ui_scale: Res<UiScale>,
|
ui_scale: Res<UiScale>,
|
||||||
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
||||||
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
|
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
|
||||||
mut text_pipeline: ResMut<DefaultTextPipeline>,
|
mut text_pipeline: ResMut<TextPipeline>,
|
||||||
mut text_queries: ParamSet<(
|
mut text_queries: ParamSet<(
|
||||||
Query<Entity, Or<(Changed<Text>, Changed<Style>)>>,
|
Query<Entity, Or<(Changed<Text>, Changed<Style>)>>,
|
||||||
Query<Entity, (With<Text>, With<Style>)>,
|
Query<Entity, (With<Text>, With<Style>)>,
|
||||||
Query<(&Text, &Style, &mut CalculatedSize)>,
|
Query<(
|
||||||
|
&Text,
|
||||||
|
&Style,
|
||||||
|
&mut CalculatedSize,
|
||||||
|
Option<&mut TextLayoutInfo>,
|
||||||
|
)>,
|
||||||
)>,
|
)>,
|
||||||
) {
|
) {
|
||||||
// TODO: This should support window-independent scale settings.
|
// TODO: This should support window-independent scale settings.
|
||||||
|
@ -85,7 +91,7 @@ pub fn text_system(
|
||||||
let mut new_queue = Vec::new();
|
let mut new_queue = Vec::new();
|
||||||
let mut query = text_queries.p2();
|
let mut query = text_queries.p2();
|
||||||
for entity in queued_text.entities.drain(..) {
|
for entity in queued_text.entities.drain(..) {
|
||||||
if let Ok((text, style, mut calculated_size)) = query.get_mut(entity) {
|
if let Ok((text, style, mut calculated_size, text_layout_info)) = query.get_mut(entity) {
|
||||||
let node_size = Vec2::new(
|
let node_size = Vec2::new(
|
||||||
text_constraint(
|
text_constraint(
|
||||||
style.min_size.width,
|
style.min_size.width,
|
||||||
|
@ -102,7 +108,6 @@ pub fn text_system(
|
||||||
);
|
);
|
||||||
|
|
||||||
match text_pipeline.queue_text(
|
match text_pipeline.queue_text(
|
||||||
entity,
|
|
||||||
&fonts,
|
&fonts,
|
||||||
&text.sections,
|
&text.sections,
|
||||||
scale_factor,
|
scale_factor,
|
||||||
|
@ -120,14 +125,17 @@ pub fn text_system(
|
||||||
Err(e @ TextError::FailedToAddGlyph(_)) => {
|
Err(e @ TextError::FailedToAddGlyph(_)) => {
|
||||||
panic!("Fatal error when processing text: {}.", e);
|
panic!("Fatal error when processing text: {}.", e);
|
||||||
}
|
}
|
||||||
Ok(()) => {
|
Ok(info) => {
|
||||||
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
|
|
||||||
"Failed to get glyphs from the pipeline that have just been computed",
|
|
||||||
);
|
|
||||||
calculated_size.size = Size {
|
calculated_size.size = Size {
|
||||||
width: Val::Px(scale_value(text_layout_info.size.x, inv_scale_factor)),
|
width: Val::Px(scale_value(info.size.x, inv_scale_factor)),
|
||||||
height: Val::Px(scale_value(text_layout_info.size.y, inv_scale_factor)),
|
height: Val::Px(scale_value(info.size.y, inv_scale_factor)),
|
||||||
};
|
};
|
||||||
|
match text_layout_info {
|
||||||
|
Some(mut t) => *t = info,
|
||||||
|
None => {
|
||||||
|
commands.entity(entity).insert(info);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue