Skip to content

Commit 21dbf59

Browse files
authored
Move text2d into bevy_sprite (#20594)
# Objective Fixes #19531 ## Solution Move the `text2d` module from `bevy_text` into `bevy_sprite` Move the `text2d` rendering module from `bevy_ui_render` to `bevy_sprite_render`. Remove the `bevy_sprite`, `bevy_window`, `bevy_camera` and `bevy_transform` dependencies from `bevy_text`. ## Testing ``` cargo run --example text2d ``` ## Showcase
1 parent 66cf812 commit 21dbf59

File tree

15 files changed

+79
-109
lines changed

15 files changed

+79
-109
lines changed

crates/bevy_sprite/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev" }
2626
bevy_transform = { path = "../bevy_transform", version = "0.17.0-dev" }
2727
bevy_window = { path = "../bevy_window", version = "0.17.0-dev", optional = true }
2828
bevy_derive = { path = "../bevy_derive", version = "0.17.0-dev" }
29+
bevy_text = { path = "../bevy_text", version = "0.17.0-dev" }
2930

3031
# other
3132
radsort = "0.1"
3233
tracing = { version = "0.1", default-features = false, features = ["std"] }
3334
wgpu-types = { version = "26", default-features = false }
3435

36+
[dev-dependencies]
37+
approx = "0.5.1"
38+
bevy_window = { path = "../bevy_window", version = "0.17.0-dev" }
39+
3540
[lints]
3641
workspace = true
3742

crates/bevy_sprite/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern crate alloc;
1313
#[cfg(feature = "bevy_sprite_picking_backend")]
1414
mod picking_backend;
1515
mod sprite;
16+
mod text2d;
1617
mod texture_slice;
1718

1819
/// The sprite prelude.
@@ -27,20 +28,26 @@ pub mod prelude {
2728
#[doc(hidden)]
2829
pub use crate::{
2930
sprite::{Sprite, SpriteImageMode},
31+
text2d::{Text2d, Text2dReader, Text2dWriter},
3032
texture_slice::{BorderRect, SliceScaleMode, TextureSlice, TextureSlicer},
3133
ScalingMode,
3234
};
3335
}
3436

37+
use bevy_app::AnimationSystems;
3538
use bevy_asset::Assets;
3639
use bevy_camera::{
3740
primitives::{Aabb, MeshAabb},
38-
visibility::{NoFrustumCulling, VisibilitySystems},
41+
visibility::NoFrustumCulling,
3942
};
43+
use bevy_camera::{visibility::VisibilitySystems, CameraUpdateSystems};
4044
use bevy_mesh::{Mesh, Mesh2d};
45+
use bevy_text::detect_text_needs_rerender;
46+
use bevy_text::Text2dUpdateSystems;
4147
#[cfg(feature = "bevy_sprite_picking_backend")]
4248
pub use picking_backend::*;
4349
pub use sprite::*;
50+
pub use text2d::*;
4451
pub use texture_slice::*;
4552

4653
use bevy_app::prelude::*;
@@ -72,6 +79,24 @@ impl Plugin for SpritePlugin {
7279
calculate_bounds_2d.in_set(VisibilitySystems::CalculateBounds),
7380
);
7481

82+
app.add_systems(
83+
PostUpdate,
84+
(
85+
detect_text_needs_rerender::<Text2d>,
86+
update_text2d_layout
87+
// Potential conflict: `Assets<Image>`
88+
// In practice, they run independently since `bevy_render::camera_update_system`
89+
// will only ever observe its own render target, and `update_text2d_layout`
90+
// will never modify a pre-existing `Image` asset.
91+
.ambiguous_with(CameraUpdateSystems)
92+
.after(bevy_text::remove_dropped_font_atlas_sets),
93+
calculate_bounds_text2d.in_set(VisibilitySystems::CalculateBounds),
94+
)
95+
.chain()
96+
.in_set(Text2dUpdateSystems)
97+
.after(AnimationSystems),
98+
);
99+
75100
#[cfg(feature = "bevy_sprite_picking_backend")]
76101
app.add_plugins(SpritePickingPlugin);
77102
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::pipeline::CosmicFontSystem;
2-
use crate::{
3-
ComputedTextBlock, Font, FontAtlasSets, LineBreak, SwashCache, TextBounds, TextColor,
4-
TextError, TextFont, TextLayout, TextLayoutInfo, TextPipeline, TextReader, TextRoot,
1+
use bevy_text::{
2+
ComputedTextBlock, CosmicFontSystem, Font, FontAtlasSets, LineBreak, SwashCache, TextBounds,
3+
TextColor, TextError, TextFont, TextLayout, TextLayoutInfo, TextPipeline, TextReader, TextRoot,
54
TextSpanAccess, TextWriter,
65
};
76

7+
use crate::{Anchor, Sprite};
88
use bevy_asset::Assets;
99
use bevy_camera::primitives::Aabb;
1010
use bevy_camera::visibility::{self, NoFrustumCulling, Visibility, VisibilityClass};
@@ -22,7 +22,6 @@ use bevy_ecs::{
2222
use bevy_image::prelude::*;
2323
use bevy_math::{Vec2, Vec3};
2424
use bevy_reflect::{prelude::ReflectDefault, Reflect};
25-
use bevy_sprite::{Anchor, Sprite};
2625
use bevy_transform::components::Transform;
2726
use bevy_window::{PrimaryWindow, Window};
2827

@@ -32,7 +31,7 @@ use bevy_window::{PrimaryWindow, Window};
3231
/// [Example usage.](https://github.com/bevyengine/bevy/blob/latest/examples/2d/text2d.rs)
3332
///
3433
/// The string in this component is the first 'text span' in a hierarchy of text spans that are collected into
35-
/// a [`ComputedTextBlock`]. See [`TextSpan`](crate::TextSpan) for the component used by children of entities with [`Text2d`].
34+
/// a [`ComputedTextBlock`]. See `TextSpan` for the component used by children of entities with [`Text2d`].
3635
///
3736
/// With `Text2d` the `justify` field of [`TextLayout`] only affects the internal alignment of a block of text and not its
3837
/// relative position, which is controlled by the [`Anchor`] component.
@@ -44,7 +43,8 @@ use bevy_window::{PrimaryWindow, Window};
4443
/// # use bevy_color::Color;
4544
/// # use bevy_color::palettes::basic::BLUE;
4645
/// # use bevy_ecs::world::World;
47-
/// # use bevy_text::{Font, Justify, Text2d, TextLayout, TextFont, TextColor, TextSpan};
46+
/// # use bevy_text::{Font, Justify, TextLayout, TextFont, TextColor, TextSpan};
47+
/// # use bevy_sprite::Text2d;
4848
/// #
4949
/// # let font_handle: Handle<Font> = Default::default();
5050
/// # let mut world = World::default();
@@ -278,8 +278,7 @@ mod tests {
278278
use bevy_app::{App, Update};
279279
use bevy_asset::{load_internal_binary_asset, Handle};
280280
use bevy_ecs::schedule::IntoScheduleConfigs;
281-
282-
use crate::{detect_text_needs_rerender, TextIterScratch};
281+
use bevy_text::{detect_text_needs_rerender, TextIterScratch};
283282

284283
use super::*;
285284

@@ -310,7 +309,7 @@ mod tests {
310309
load_internal_binary_asset!(
311310
app,
312311
Handle::default(),
313-
"FiraMono-subset.ttf",
312+
"../../bevy_text/src/FiraMono-subset.ttf",
314313
|bytes: &[u8], _path: String| { Font::try_from_bytes(bytes.to_vec()).unwrap() }
315314
);
316315

crates/bevy_sprite_render/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ bevy_mesh = { path = "../bevy_mesh", version = "0.17.0-dev" }
2525
bevy_math = { path = "../bevy_math", version = "0.17.0-dev" }
2626
bevy_shader = { path = "../bevy_shader", version = "0.17.0-dev" }
2727
bevy_sprite = { path = "../bevy_sprite", version = "0.17.0-dev" }
28+
bevy_text = { path = "../bevy_text", version = "0.17.0-dev" }
2829
bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev" }
2930
bevy_render = { path = "../bevy_render", version = "0.17.0-dev" }
3031
bevy_transform = { path = "../bevy_transform", version = "0.17.0-dev" }
@@ -33,6 +34,7 @@ bevy_derive = { path = "../bevy_derive", version = "0.17.0-dev" }
3334
bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-features = false, features = [
3435
"std",
3536
] }
37+
bevy_window = { path = "../bevy_window", version = "0.17.0-dev" }
3638

3739
# other
3840
bytemuck = { version = "1", features = ["derive", "must_cast"] }

crates/bevy_sprite_render/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern crate alloc;
1212

1313
mod mesh2d;
1414
mod render;
15+
mod text2d;
1516
mod texture_slice;
1617
mod tilemap_chunk;
1718

@@ -42,6 +43,8 @@ use bevy_render::{
4243
};
4344
use bevy_sprite::Sprite;
4445

46+
use crate::text2d::extract_text2d_sprite;
47+
4548
/// Adds support for 2D sprite rendering.
4649
#[derive(Default)]
4750
pub struct SpriteRenderingPlugin;
@@ -100,6 +103,7 @@ impl Plugin for SpriteRenderingPlugin {
100103
(
101104
extract_sprites.in_set(SpriteSystems::ExtractSprites),
102105
extract_sprite_events,
106+
extract_text2d_sprite.after(SpriteSystems::ExtractSprites),
103107
),
104108
)
105109
.add_systems(

crates/bevy_ui_render/src/text2d.rs renamed to crates/bevy_sprite_render/src/text2d/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::{
2+
ExtractedSlice, ExtractedSlices, ExtractedSprite, ExtractedSpriteKind, ExtractedSprites,
3+
};
14
use bevy_asset::{AssetId, Assets};
25
use bevy_camera::visibility::ViewVisibility;
36
use bevy_color::LinearRgba;
@@ -10,13 +13,9 @@ use bevy_image::prelude::*;
1013
use bevy_math::Vec2;
1114
use bevy_render::sync_world::TemporaryRenderEntity;
1215
use bevy_render::Extract;
13-
use bevy_sprite::Anchor;
14-
use bevy_sprite_render::{
15-
ExtractedSlice, ExtractedSlices, ExtractedSprite, ExtractedSpriteKind, ExtractedSprites,
16-
};
16+
use bevy_sprite::{Anchor, Text2dShadow};
1717
use bevy_text::{
18-
ComputedTextBlock, PositionedGlyph, Text2dShadow, TextBackgroundColor, TextBounds, TextColor,
19-
TextLayoutInfo,
18+
ComputedTextBlock, PositionedGlyph, TextBackgroundColor, TextBounds, TextColor, TextLayoutInfo,
2019
};
2120
use bevy_transform::prelude::GlobalTransform;
2221
use bevy_window::{PrimaryWindow, Window};

crates/bevy_text/Cargo.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ bevy_image = { path = "../bevy_image", version = "0.17.0-dev" }
2222
bevy_log = { path = "../bevy_log", version = "0.17.0-dev" }
2323
bevy_math = { path = "../bevy_math", version = "0.17.0-dev" }
2424
bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev" }
25-
bevy_camera = { path = "../bevy_camera", version = "0.17.0-dev" }
26-
bevy_sprite = { path = "../bevy_sprite", version = "0.17.0-dev" }
27-
bevy_transform = { path = "../bevy_transform", version = "0.17.0-dev" }
28-
bevy_window = { path = "../bevy_window", version = "0.17.0-dev" }
2925
bevy_utils = { path = "../bevy_utils", version = "0.17.0-dev" }
3026
bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-features = false, features = [
3127
"std",
@@ -41,9 +37,6 @@ smallvec = { version = "1", default-features = false }
4137
sys-locale = "0.3.0"
4238
tracing = { version = "0.1", default-features = false, features = ["std"] }
4339

44-
[dev-dependencies]
45-
approx = "0.5.1"
46-
4740
[lints]
4841
workspace = true
4942

crates/bevy_text/src/lib.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! Note that text measurement is only relevant in a UI context.
2121
//!
2222
//! With the actual text bounds defined, the `bevy_ui::widget::text::text_system` system (in a UI context)
23-
//! or [`text2d::update_text2d_layout`] system (in a 2d world space context)
23+
//! or `bevy_sprite::text2d::update_text2d_layout` system (in a 2d world space context)
2424
//! passes it into [`TextPipeline::queue_text`], which:
2525
//!
2626
//! 1. updates a [`Buffer`](cosmic_text::Buffer) from the [`TextSpan`]s, generating new [`FontAtlasSet`]s if necessary.
@@ -40,10 +40,8 @@ mod font_loader;
4040
mod glyph;
4141
mod pipeline;
4242
mod text;
43-
mod text2d;
4443
mod text_access;
4544

46-
use bevy_camera::{visibility::VisibilitySystems, CameraUpdateSystems};
4745
pub use bounds::*;
4846
pub use error::*;
4947
pub use font::*;
@@ -53,7 +51,6 @@ pub use font_loader::*;
5351
pub use glyph::*;
5452
pub use pipeline::*;
5553
pub use text::*;
56-
pub use text2d::*;
5754
pub use text_access::*;
5855

5956
/// The text prelude.
@@ -62,12 +59,11 @@ pub use text_access::*;
6259
pub mod prelude {
6360
#[doc(hidden)]
6461
pub use crate::{
65-
Font, Justify, LineBreak, Text2d, Text2dReader, Text2dWriter, TextColor, TextError,
66-
TextFont, TextLayout, TextSpan,
62+
Font, Justify, LineBreak, TextColor, TextError, TextFont, TextLayout, TextSpan,
6763
};
6864
}
6965

70-
use bevy_app::{prelude::*, AnimationSystems};
66+
use bevy_app::prelude::*;
7167
use bevy_asset::{AssetApp, AssetEventSystems};
7268
use bevy_ecs::prelude::*;
7369

@@ -101,20 +97,7 @@ impl Plugin for TextPlugin {
10197
.init_resource::<TextIterScratch>()
10298
.add_systems(
10399
PostUpdate,
104-
(
105-
remove_dropped_font_atlas_sets.before(AssetEventSystems),
106-
detect_text_needs_rerender::<Text2d>,
107-
update_text2d_layout
108-
// Potential conflict: `Assets<Image>`
109-
// In practice, they run independently since `bevy_render::camera_update_system`
110-
// will only ever observe its own render target, and `update_text2d_layout`
111-
// will never modify a pre-existing `Image` asset.
112-
.ambiguous_with(CameraUpdateSystems),
113-
calculate_bounds_text2d.in_set(VisibilitySystems::CalculateBounds),
114-
)
115-
.chain()
116-
.in_set(Text2dUpdateSystems)
117-
.after(AnimationSystems),
100+
remove_dropped_font_atlas_sets.before(AssetEventSystems),
118101
)
119102
.add_systems(Last, trim_cosmic_cache);
120103

crates/bevy_text/src/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct FontFaceInfo {
6464
pub family_name: Arc<str>,
6565
}
6666

67-
/// The `TextPipeline` is used to layout and render text blocks (see `Text`/[`Text2d`](crate::Text2d)).
67+
/// The `TextPipeline` is used to layout and render text blocks (see `Text`/`Text2d`).
6868
///
6969
/// See the [crate-level documentation](crate) for more information.
7070
#[derive(Default, Resource)]

crates/bevy_text/src/text.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Default for ComputedTextBlock {
109109
/// spans associated with a text block are collected into [`ComputedTextBlock`] for layout, and then inserted
110110
/// to [`TextLayoutInfo`] for rendering.
111111
///
112-
/// See [`Text2d`](crate::Text2d) for the core component of 2d text, and `Text` in `bevy_ui` for UI text.
112+
/// See `Text2d` in `bevy_sprite` for the core component of 2d text, and `Text` in `bevy_ui` for UI text.
113113
#[derive(Component, Debug, Copy, Clone, Default, Reflect)]
114114
#[reflect(Component, Default, Debug, Clone)]
115115
#[require(ComputedTextBlock, TextLayoutInfo)]
@@ -170,46 +170,6 @@ impl TextLayout {
170170
/// with `TextSpan` extend this text by appending their content to the parent's text in sequence to
171171
/// form a [`ComputedTextBlock`]. The parent's [`TextLayout`] determines the layout of the block
172172
/// but each node has its own [`TextFont`] and [`TextColor`].
173-
///
174-
/// ```
175-
/// # use bevy_asset::Handle;
176-
/// # use bevy_color::Color;
177-
/// # use bevy_color::palettes::basic::{BLUE, GREEN, RED};
178-
/// # use bevy_ecs::{children, spawn::SpawnRelated, world::World};
179-
/// # use bevy_text::{Font, Justify, Text2d, TextColor, TextLayout, TextFont, TextSpan};
180-
///
181-
/// # let font_handle: Handle<Font> = Default::default();
182-
/// # let mut world = World::default();
183-
/// #
184-
/// world.spawn((
185-
/// // `Text` or `Text2d` is needed.
186-
/// Text2d::new("Bevy\n"),
187-
/// // Layout of the entire block of text.
188-
/// TextLayout::new_with_justify(Justify::Center),
189-
/// // TextFont of this node. Won't apply to children.
190-
/// TextFont::from_font_size(50.0),
191-
/// // TextColor of this node. Won't apply to children.
192-
/// TextColor(BLUE.into()),
193-
/// // Children must be `TextSpan`, not `Text` or `Text2d`.
194-
/// children![
195-
/// (
196-
/// TextSpan::new("Bevy\n"),
197-
/// TextFont::from_font_size(40.0),
198-
/// TextColor(RED.into()),
199-
/// ),
200-
/// (
201-
/// TextSpan::new("Bevy\n"),
202-
/// TextFont::from_font_size(30.0),
203-
/// // Default TextColor will be inserted because TextSpan requires it.
204-
/// ),
205-
/// (
206-
/// TextSpan::new("Bevy"),
207-
/// TextColor(GREEN.into()),
208-
/// // Default TextFont will be inserted because TextSpan requires it.
209-
/// )
210-
/// ],
211-
/// ));
212-
/// ```
213173
#[derive(Component, Debug, Default, Clone, Deref, DerefMut, Reflect)]
214174
#[reflect(Component, Default, Debug, Clone)]
215175
#[require(TextFont, TextColor)]
@@ -500,7 +460,7 @@ pub enum FontSmoothing {
500460

501461
/// System that detects changes to text blocks and sets `ComputedTextBlock::should_rerender`.
502462
///
503-
/// Generic over the root text component and text span component. For example, [`Text2d`](crate::Text2d)/[`TextSpan`] for
463+
/// Generic over the root text component and text span component. For example, `Text2d`/[`TextSpan`] for
504464
/// 2d or `Text`/[`TextSpan`] for UI.
505465
pub fn detect_text_needs_rerender<Root: Component>(
506466
changed_roots: Query<

0 commit comments

Comments
 (0)