From 35de5e608e9441a1d8e01a6fc381ff1b2e943b72 Mon Sep 17 00:00:00 2001 From: Ycy Date: Tue, 26 Sep 2023 02:59:29 +0800 Subject: [PATCH] register `TextLayoutInfo` and `TextFlags` type. (#9919) derive `Reflect` to `GlyphAtlasInfo`,`PositionedGlyph` and `TextLayoutInfo`. # Objective - I need reflection gets all components of the `TextBundle` and `clone_value` it ## Solution - registry it --- crates/bevy_text/src/font_atlas_set.rs | 3 ++- crates/bevy_text/src/glyph_brush.rs | 3 ++- crates/bevy_text/src/pipeline.rs | 6 +++++- crates/bevy_ui/src/lib.rs | 8 ++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/bevy_text/src/font_atlas_set.rs b/crates/bevy_text/src/font_atlas_set.rs index f0ca798d89..730d978c14 100644 --- a/crates/bevy_text/src/font_atlas_set.rs +++ b/crates/bevy_text/src/font_atlas_set.rs @@ -4,6 +4,7 @@ use bevy_asset::{AssetEvent, AssetId}; use bevy_asset::{Assets, Handle}; use bevy_ecs::prelude::*; use bevy_math::Vec2; +use bevy_reflect::Reflect; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; use bevy_utils::FloatOrd; @@ -40,7 +41,7 @@ pub struct FontAtlasSet { font_atlases: HashMap>, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect)] pub struct GlyphAtlasInfo { pub texture_atlas: Handle, pub glyph_index: usize, diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index fe1e2d7203..9aab58ee97 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -1,6 +1,7 @@ use ab_glyph::{Font as _, FontArc, Glyph, PxScaleFont, ScaleFont as _}; use bevy_asset::{AssetId, Assets}; use bevy_math::{Rect, Vec2}; +use bevy_reflect::Reflect; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; use bevy_utils::tracing::warn; @@ -158,7 +159,7 @@ impl GlyphBrush { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect)] pub struct PositionedGlyph { pub position: Vec2, pub size: Vec2, diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index 6a83a44647..a93aa546f9 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -6,8 +6,11 @@ use crate::{ use ab_glyph::PxScale; use bevy_asset::{AssetId, Assets, Handle}; use bevy_ecs::component::Component; +use bevy_ecs::prelude::ReflectComponent; use bevy_ecs::system::Resource; use bevy_math::Vec2; +use bevy_reflect::prelude::ReflectDefault; +use bevy_reflect::Reflect; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; use bevy_utils::HashMap; @@ -22,7 +25,8 @@ pub struct TextPipeline { /// Render information for a corresponding [`Text`](crate::Text) component. /// /// Contains scaled glyphs and their size. Generated via [`TextPipeline::queue_text`]. -#[derive(Component, Clone, Default, Debug)] +#[derive(Component, Clone, Default, Debug, Reflect)] +#[reflect(Component, Default)] pub struct TextLayoutInfo { pub glyphs: Vec, pub logical_size: Vec2, diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index ae9f9f4188..1cee4a52a1 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -14,6 +14,8 @@ pub mod widget; use bevy_derive::{Deref, DerefMut}; use bevy_reflect::Reflect; #[cfg(feature = "bevy_text")] +use bevy_text::TextLayoutInfo; +#[cfg(feature = "bevy_text")] mod accessibility; mod focus; mod geometry; @@ -40,6 +42,8 @@ pub mod prelude { } use crate::prelude::UiCameraConfig; +#[cfg(feature = "bevy_text")] +use crate::widget::TextFlags; use bevy_app::prelude::*; use bevy_asset::Assets; use bevy_ecs::prelude::*; @@ -126,6 +130,10 @@ impl Plugin for UiPlugin { PreUpdate, ui_focus_system.in_set(UiSystem::Focus).after(InputSystem), ); + + #[cfg(feature = "bevy_text")] + app.register_type::() + .register_type::(); // add these systems to front because these must run before transform update systems #[cfg(feature = "bevy_text")] app.add_systems(