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

text_system split #7779

Merged
merged 48 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3a09af9
changes:
ickshonpe Feb 17, 2023
b1712b8
cargo fmt
ickshonpe Feb 17, 2023
080aae7
fix lints
ickshonpe Feb 17, 2023
9c4c01d
cargo fmt
ickshonpe Feb 17, 2023
8934a6f
impl Measure for Fn
ickshonpe Feb 18, 2023
fc9b2d9
Changes:
ickshonpe Feb 19, 2023
f7d20ea
cargo fmt --all
ickshonpe Feb 19, 2023
13091a3
remove `text_constraint` function
ickshonpe Feb 21, 2023
cfdb649
Merge branch 'split-text-system' of https://github.com/ickshonpe/bevy…
ickshonpe Feb 21, 2023
621a117
Merge branch 'add-TextLayoutInfo-to-TextBundle' into split-text-system
ickshonpe Feb 21, 2023
3981c7f
add TextLayoutInfo to default TextBundle
ickshonpe Feb 21, 2023
b004072
changes:
ickshonpe Feb 21, 2023
af7b6f2
changes:
ickshonpe Feb 21, 2023
2280bb0
changes:
ickshonpe Feb 21, 2023
f510899
Clean up and bug fixes.
ickshonpe Feb 21, 2023
95d7c96
simplified text size code
ickshonpe Feb 21, 2023
91a05d2
fix derivable impls
ickshonpe Feb 21, 2023
1ee6233
added `compute_geometry` function to pipeline
ickshonpe Feb 27, 2023
5944e4b
Merge branch 'main' into split-text-system
ickshonpe Mar 14, 2023
540f51e
fixed remaining bugs from merge
ickshonpe Mar 14, 2023
ac267a9
Renamed `IntrinsicSize` back to `CalculatedSize`, too many changes in…
ickshonpe Mar 14, 2023
70f5422
Implemented one pass text `MeasureFunc`
ickshonpe Mar 14, 2023
1fe7ac7
update layout on screensize change
ickshonpe Mar 16, 2023
d919ac7
Merge branch 'main' into split-text-system
ickshonpe Mar 21, 2023
8a4c73d
Cleaned up unused code and fields.
ickshonpe Mar 21, 2023
5ae0d4d
Merge branch 'main' into split-text-system
ickshonpe Mar 21, 2023
711036b
cargo fmt --all
ickshonpe Mar 21, 2023
74afca0
Merge branch 'main' into split-text-system
ickshonpe Mar 31, 2023
941c018
changes:
ickshonpe Mar 31, 2023
89eb505
taffy dependency version to 0.3.10
ickshonpe Mar 31, 2023
ffb59a8
removed some unused code
ickshonpe Mar 31, 2023
cbb1d17
reuse section texts vec when computing min and max content size
ickshonpe Mar 31, 2023
8547ce2
changes:
ickshonpe Apr 14, 2023
76ea8a0
Merge branch 'main' into split-text-system
ickshonpe Apr 14, 2023
6c15af9
formatting
ickshonpe Apr 14, 2023
08aa11c
Added a constructor function to `TextMeasureInfo`
ickshonpe Apr 14, 2023
410a4ec
Added a comment explaining the font coordinate calculations.
ickshonpe Apr 14, 2023
a04e100
fix misnamed variable
ickshonpe Apr 14, 2023
af1f990
fix misnamed variables
ickshonpe Apr 14, 2023
10e2ddc
Merge branch 'split-text-system' of https://github.com/ickshonpe/bevy…
ickshonpe Apr 14, 2023
ee1d1f3
use `with_capacity` to construct the vecs with the correct length
ickshonpe Apr 14, 2023
8f1eb15
fixed extra space
ickshonpe Apr 14, 2023
2ec10d8
Update crates/bevy_ui/src/widget/image.rs
ickshonpe Apr 15, 2023
be4053c
added `Debug` derive to new types
ickshonpe Apr 15, 2023
7526fc6
Added comment to `text_layout_info` field of `Text2dBundle`
ickshonpe Apr 15, 2023
e33bc19
Update crates/bevy_text/src/pipeline.rs
ickshonpe Apr 15, 2023
68c6e98
fixed needless borrow
ickshonpe Apr 15, 2023
2779750
Merge branch 'split-text-system' of https://github.com/ickshonpe/bevy…
ickshonpe Apr 15, 2023
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
73 changes: 73 additions & 0 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,77 @@ impl TextPipeline {

Ok(TextLayoutInfo { glyphs, size })
}

// hacked up `queue_text` that returns the text size constraints, there should be a better solution
pub fn compute_size_constraints(
&mut self,
fonts: &Assets<Font>,
sections: &[TextSection],
scale_factor: f64,
text_alignment: TextAlignment,
linebreak_behaviour: BreakLineOn,
) -> Result<[Vec2; 2], TextError> {
let mut scaled_fonts = Vec::new();
let sections = sections
.iter()
.map(|section| {
let font = fonts
.get(&section.style.font)
.ok_or(TextError::NoSuchFont)?;
let font_id = self.get_or_insert_font_id(&section.style.font, font);
let font_size = scale_value(section.style.font_size, scale_factor);

let px_scale_font = ab_glyph::Font::into_scaled(font.font.clone(), font_size);
scaled_fonts.push(px_scale_font);

let section = SectionText {
font_id,
scale: PxScale::from(font_size),
text: &section.value,
};

Ok(section)
})
.collect::<Result<Vec<_>, _>>()?;

let result = [
Vec2::new(0.0, f32::INFINITY),
Vec2::new(f32::INFINITY, f32::INFINITY),
]
.map(|bounds| {
if let Ok(section_glyphs) =
self.brush
.compute_glyphs(&sections, bounds, text_alignment, linebreak_behaviour)
{
let mut min_x: f32 = std::f32::MAX;
let mut min_y: f32 = std::f32::MAX;
let mut max_x: f32 = std::f32::MIN;
let mut max_y: f32 = std::f32::MIN;

for sg in section_glyphs {
let scaled_font = &scaled_fonts[sg.section_index];
let glyph = &sg.glyph;
min_x = min_x.min(glyph.position.x);
min_y = min_y.min(glyph.position.y - scaled_font.ascent());
max_x = max_x.max(glyph.position.x + scaled_font.h_advance(glyph.id));
max_y = max_y.max(glyph.position.y - scaled_font.descent());
}

Vec2::new(max_x - min_x, max_y - min_y)
} else {
Vec2::ZERO
}
});
Ok(result)
}

fn compute_geometry(
&mut self,
fonts: &Assets<Font>,
sections: &[TextSection],
scale_factor: f64,
text_alignment: TextAlignment,
linebreak_behaviour: BreakLineOn,
) {
}
}
20 changes: 5 additions & 15 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_ecs::{
event::EventReader,
prelude::With,
reflect::ReflectComponent,
system::{Commands, Local, Query, Res, ResMut},
system::{Local, Query, Res, ResMut},
};
use bevy_math::{Vec2, Vec3};
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -72,6 +72,7 @@ pub struct Text2dBundle {
pub visibility: Visibility,
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering.
pub computed_visibility: ComputedVisibility,
pub text_layout_info: TextLayoutInfo,
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn extract_text2d_sprite(
Expand Down Expand Up @@ -153,7 +154,6 @@ pub fn extract_text2d_sprite(
/// It does not modify or observe existing ones.
#[allow(clippy::too_many_arguments)]
pub fn update_text2d_layout(
mut commands: Commands,
// Text items which should be reprocessed again, generally when the font hasn't loaded yet.
mut queue: Local<HashSet<Entity>>,
mut textures: ResMut<Assets<Image>>,
Expand All @@ -165,12 +165,7 @@ pub fn update_text2d_layout(
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
mut text_pipeline: ResMut<TextPipeline>,
mut text_query: Query<(
Entity,
Ref<Text>,
&Text2dBounds,
Option<&mut TextLayoutInfo>,
)>,
mut text_query: Query<(Entity, Ref<Text>, &Text2dBounds, &mut TextLayoutInfo)>,
) {
// We need to consume the entire iterator, hence `last`
let factor_changed = scale_factor_changed.iter().last().is_some();
Expand All @@ -181,7 +176,7 @@ pub fn update_text2d_layout(
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.0);

for (entity, text, bounds, text_layout_info) in &mut text_query {
for (entity, text, bounds, mut text_layout_info) in &mut text_query {
if factor_changed || text.is_changed() || queue.remove(&entity) {
let text_bounds = Vec2::new(
scale_value(bounds.size.x, scale_factor),
Expand Down Expand Up @@ -210,12 +205,7 @@ pub fn update_text2d_layout(
Err(e @ TextError::FailedToAddGlyph(_)) => {
panic!("Fatal error when processing text: {e}.");
}
Ok(info) => match text_layout_info {
Some(mut t) => *t = info,
None => {
commands.entity(entity).insert(info);
}
},
Ok(info) => *text_layout_info = info,
}
}
}
Expand Down
63 changes: 27 additions & 36 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod convert;

use crate::{CalculatedSize, Node, Style, UiScale};
use crate::{IntrinsicSize, Node, Style, UiScale};
use bevy_ecs::{
change_detection::DetectChanges,
entity::Entity,
Expand Down Expand Up @@ -76,44 +76,35 @@ impl FlexSurface {
&mut self,
entity: Entity,
style: &Style,
calculated_size: CalculatedSize,
calculated_size: &IntrinsicSize,
scale_factor: f64,
) {
let taffy = &mut self.taffy;
let taffy_style = convert::from_style(scale_factor, style);
let measure = taffy::node::MeasureFunc::Boxed(Box::new(
move |constraints: Size<Option<f32>>, _available: Size<AvailableSpace>| {
let mut size = Size {
width: (scale_factor * calculated_size.size.x as f64) as f32,
height: (scale_factor * calculated_size.size.y as f64) as f32,
};
match (constraints.width, constraints.height) {
(None, None) => {}
(Some(width), None) => {
if calculated_size.preserve_aspect_ratio {
size.height = width * size.height / size.width;
}
size.width = width;
}
(None, Some(height)) => {
if calculated_size.preserve_aspect_ratio {
size.width = height * size.width / size.height;
}
size.height = height;
}
(Some(width), Some(height)) => {
size.width = width;
size.height = height;
}
let measure = calculated_size.measure.dyn_clone();
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
let measure_func = taffy::node::MeasureFunc::Boxed(Box::new(
move |constraints: Size<Option<f32>>, available: Size<AvailableSpace>| {
let size = measure.measure(
constraints.width,
constraints.height,
available.width,
available.height,
);
taffy::geometry::Size {
width: size.x,
height: size.y,
}
size
},
));
if let Some(taffy_node) = self.entity_to_taffy.get(&entity) {
self.taffy.set_style(*taffy_node, taffy_style).unwrap();
self.taffy.set_measure(*taffy_node, Some(measure)).unwrap();
self.taffy
.set_measure(*taffy_node, Some(measure_func))
.unwrap();
} else {
let taffy_node = taffy.new_leaf_with_measure(taffy_style, measure).unwrap();
let taffy_node = taffy
.new_leaf_with_measure(taffy_style, measure_func)
.unwrap();
self.entity_to_taffy.insert(entity, taffy_node);
}
}
Expand Down Expand Up @@ -227,11 +218,11 @@ pub fn flex_node_system(
mut scale_factor_events: EventReader<WindowScaleFactorChanged>,
mut flex_surface: ResMut<FlexSurface>,
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
node_query: Query<(Entity, &Style, Option<&CalculatedSize>), (With<Node>, Changed<Style>)>,
full_node_query: Query<(Entity, &Style, Option<&CalculatedSize>), With<Node>>,
node_query: Query<(Entity, &Style, Option<&IntrinsicSize>), (With<Node>, Changed<Style>)>,
full_node_query: Query<(Entity, &Style, Option<&IntrinsicSize>), With<Node>>,
changed_size_query: Query<
(Entity, &Style, &CalculatedSize),
(With<Node>, Changed<CalculatedSize>),
(Entity, &Style, &IntrinsicSize),
(With<Node>, Changed<IntrinsicSize>),
>,
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
mut removed_children: RemovedComponents<Children>,
Expand All @@ -257,13 +248,13 @@ pub fn flex_node_system(
fn update_changed<F: ReadOnlyWorldQuery>(
flex_surface: &mut FlexSurface,
scaling_factor: f64,
query: Query<(Entity, &Style, Option<&CalculatedSize>), F>,
query: Query<(Entity, &Style, Option<&IntrinsicSize>), F>,
) {
// update changed nodes
for (entity, style, calculated_size) in &query {
// TODO: remove node from old hierarchy if its root has changed
if let Some(calculated_size) = calculated_size {
flex_surface.upsert_leaf(entity, style, *calculated_size, scaling_factor);
flex_surface.upsert_leaf(entity, style, calculated_size, scaling_factor);
} else {
flex_surface.upsert_node(entity, style, scaling_factor);
}
Expand All @@ -278,7 +269,7 @@ pub fn flex_node_system(
}

for (entity, style, calculated_size) in &changed_size_query {
flex_surface.upsert_leaf(entity, style, *calculated_size, scale_factor);
flex_surface.upsert_leaf(entity, style, calculated_size, scale_factor);
}

// clean up removed nodes
Expand Down
19 changes: 12 additions & 7 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod stack;
mod ui_node;

pub mod camera_config;
pub mod measurement;
pub mod node_bundles;
pub mod update;
pub mod widget;
Expand All @@ -18,18 +19,20 @@ use bevy_render::{camera::CameraUpdateSystem, extract_component::ExtractComponen
pub use flex::*;
pub use focus::*;
pub use geometry::*;
pub use measurement::*;
pub use render::*;
pub use ui_node::*;

#[doc(hidden)]
pub mod prelude {
#[doc(hidden)]
pub use crate::{
camera_config::*, geometry::*, node_bundles::*, ui_node::*, widget::Button, Interaction,
UiScale,
camera_config::*, geometry::*, measurement::IntrinsicSize, node_bundles::*, ui_node::*,
widget::Button, Interaction, UiScale,
};
}

use crate::prelude::UiCameraConfig;
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_input::InputSystem;
Expand All @@ -38,8 +41,6 @@ use stack::ui_stack_system;
pub use stack::UiStack;
use update::update_clipping_system;

use crate::prelude::UiCameraConfig;

/// The basic plugin for Bevy UI
#[derive(Default)]
pub struct UiPlugin;
Expand Down Expand Up @@ -80,7 +81,7 @@ impl Plugin for UiPlugin {
.register_type::<AlignContent>()
.register_type::<AlignItems>()
.register_type::<AlignSelf>()
.register_type::<CalculatedSize>()
.register_type::<IntrinsicSize>()
.register_type::<Direction>()
.register_type::<Display>()
.register_type::<FlexDirection>()
Expand All @@ -106,7 +107,7 @@ impl Plugin for UiPlugin {
.add_system(ui_focus_system.in_set(UiSystem::Focus).after(InputSystem))
// add these systems to front because these must run before transform update systems
.add_system(
widget::text_system
widget::measure_text_system
.in_base_set(CoreSet::PostUpdate)
.before(UiSystem::Flex)
// Potential conflict: `Assets<Image>`
Expand Down Expand Up @@ -140,8 +141,12 @@ impl Plugin for UiPlugin {
update_clipping_system
.after(TransformSystem::TransformPropagate)
.in_base_set(CoreSet::PostUpdate),
)
.add_system(
widget::text_system
.in_base_set(CoreSet::PostUpdate)
.after(UiSystem::Flex),
);

crate::render::build_ui_render(app);
}
}
Loading