From fbb7c6764265bbcd1b77ad417e2a79cdf3d0f35e Mon Sep 17 00:00:00 2001 From: Andreas Weibye <13300393+Weibye@users.noreply.github.com> Date: Thu, 19 Jan 2023 21:14:09 +0100 Subject: [PATCH] Fracture style into multiple components --- crates/bevy_ui/src/flex/convert.rs | 83 ++-- crates/bevy_ui/src/flex/mod.rs | 41 +- crates/bevy_ui/src/focus.rs | 2 +- crates/bevy_ui/src/geometry.rs | 115 ++--- crates/bevy_ui/src/layout_components.rs | 561 +++++++++++++++++++++++ crates/bevy_ui/src/lib.rs | 20 +- crates/bevy_ui/src/node_bundles.rs | 160 ++++--- crates/bevy_ui/src/ui_node.rs | 344 +------------- crates/bevy_ui/src/update.rs | 23 +- crates/bevy_ui/src/widget/text.rs | 26 +- examples/3d/bloom.rs | 19 +- examples/ecs/state.rs | 27 +- examples/games/alien_cake_addict.rs | 42 +- examples/games/breakout.rs | 19 +- examples/games/contributors.rs | 33 +- examples/games/game_menu.rs | 288 +++++++----- examples/ios/src/lib.rs | 24 +- examples/scene/scene.rs | 13 +- examples/stress_tests/bevymark.rs | 35 +- examples/stress_tests/many_buttons.rs | 41 +- examples/tools/gamepad_viewer.rs | 23 +- examples/ui/button.rs | 27 +- examples/ui/font_atlas_debug.rs | 39 +- examples/ui/relative_cursor_position.rs | 15 +- examples/ui/text.rs | 71 +-- examples/ui/text_debug.rs | 170 +++---- examples/ui/transparency_ui.rs | 58 +-- examples/ui/ui.rs | 218 ++++----- examples/ui/ui_scaling.rs | 38 +- examples/ui/window_fallthrough.rs | 39 +- examples/ui/z_index.rs | 171 ++++--- examples/window/low_power.rs | 66 +-- examples/window/scale_factor_override.rs | 24 +- examples/window/window_resizing.rs | 24 +- 34 files changed, 1652 insertions(+), 1247 deletions(-) create mode 100644 crates/bevy_ui/src/layout_components.rs diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index c60feb97cd591..26ec0a06e3625 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -1,7 +1,14 @@ use crate::{ - AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent, - PositionType, Size, Style, UiRect, Val, + layout_components::{ + flex::{ + AlignContent, AlignItems, AlignSelf, Direction, FlexLayoutQueryItem, JustifyContent, + Wrap, + }, + Position, + }, + Display, }; +use crate::{Size, UiRect, Val}; pub fn from_rect( scale_factor: f64, @@ -32,28 +39,28 @@ pub fn from_val_size( } } -pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { +pub fn from_flex_layout(scale_factor: f64, value: FlexLayoutQueryItem<'_>) -> taffy::style::Style { taffy::style::Style { - display: value.display.into(), - position_type: value.position_type.into(), - flex_direction: value.flex_direction.into(), - flex_wrap: value.flex_wrap.into(), - align_items: value.align_items.into(), - align_self: value.align_self.into(), - align_content: value.align_content.into(), - justify_content: value.justify_content.into(), - position: from_rect(scale_factor, value.position), - margin: from_rect(scale_factor, value.margin), - padding: from_rect(scale_factor, value.padding), - border: from_rect(scale_factor, value.border), - flex_grow: value.flex_grow, - flex_shrink: value.flex_shrink, - flex_basis: from_val(scale_factor, value.flex_basis), - size: from_val_size(scale_factor, value.size), - min_size: from_val_size(scale_factor, value.min_size), - max_size: from_val_size(scale_factor, value.max_size), - aspect_ratio: value.aspect_ratio, - gap: from_val_size(scale_factor, value.gap), + display: (value.control.display).into(), + position_type: (value.control.position).into(), + flex_direction: value.layout.direction.into(), + flex_wrap: value.layout.wrap.into(), + align_items: value.layout.align_items.into(), + align_self: value.child_layout.align_self.into(), + align_content: value.layout.align_content.into(), + justify_content: value.layout.justify_content.into(), + position: from_rect(scale_factor, value.control.inset.0), + margin: from_rect(scale_factor, value.spacing.margin), + padding: from_rect(scale_factor, value.spacing.padding), + border: from_rect(scale_factor, value.spacing.border), + flex_grow: value.child_layout.grow, + flex_shrink: value.child_layout.shrink, + flex_basis: from_val(scale_factor, value.child_layout.basis), + size: from_val_size(scale_factor, value.size_constraints.suggested), + min_size: from_val_size(scale_factor, value.size_constraints.min), + max_size: from_val_size(scale_factor, value.size_constraints.max), + aspect_ratio: value.size_constraints.aspect_ratio, + gap: from_val_size(scale_factor, value.layout.gap), } } @@ -122,13 +129,13 @@ impl From for taffy::style::Display { } } -impl From for taffy::style::FlexDirection { - fn from(value: FlexDirection) -> Self { +impl From for taffy::style::FlexDirection { + fn from(value: Direction) -> Self { match value { - FlexDirection::Row => taffy::style::FlexDirection::Row, - FlexDirection::Column => taffy::style::FlexDirection::Column, - FlexDirection::RowReverse => taffy::style::FlexDirection::RowReverse, - FlexDirection::ColumnReverse => taffy::style::FlexDirection::ColumnReverse, + Direction::Row => taffy::style::FlexDirection::Row, + Direction::Column => taffy::style::FlexDirection::Column, + Direction::RowReverse => taffy::style::FlexDirection::RowReverse, + Direction::ColumnReverse => taffy::style::FlexDirection::ColumnReverse, } } } @@ -146,21 +153,21 @@ impl From for taffy::style::JustifyContent { } } -impl From for taffy::style::PositionType { - fn from(value: PositionType) -> Self { +impl From for taffy::style::PositionType { + fn from(value: Position) -> Self { match value { - PositionType::Relative => taffy::style::PositionType::Relative, - PositionType::Absolute => taffy::style::PositionType::Absolute, + Position::Relative => taffy::style::PositionType::Relative, + Position::Absolute => taffy::style::PositionType::Absolute, } } } -impl From for taffy::style::FlexWrap { - fn from(value: FlexWrap) -> Self { +impl From for taffy::style::FlexWrap { + fn from(value: Wrap) -> Self { match value { - FlexWrap::NoWrap => taffy::style::FlexWrap::NoWrap, - FlexWrap::Wrap => taffy::style::FlexWrap::Wrap, - FlexWrap::WrapReverse => taffy::style::FlexWrap::WrapReverse, + Wrap::NoWrap => taffy::style::FlexWrap::NoWrap, + Wrap::Wrap => taffy::style::FlexWrap::Wrap, + Wrap::WrapReverse => taffy::style::FlexWrap::WrapReverse, } } } diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index 8f058dd5887ae..3c336f5fdf998 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -1,6 +1,9 @@ mod convert; -use crate::{CalculatedSize, Node, Style, UiScale}; +use crate::{ + layout_components::flex::{FlexLayoutChanged, FlexLayoutQuery, FlexLayoutQueryItem}, + CalculatedSize, Node, UiScale, +}; use bevy_ecs::{ change_detection::DetectChanges, entity::Entity, @@ -60,10 +63,17 @@ impl Default for FlexSurface { } impl FlexSurface { - pub fn upsert_node(&mut self, entity: Entity, style: &Style, scale_factor: f64) { + /// Inserts the node into the hashmap if they don't already exist, + /// or update the contents if they already to exist. + pub fn upsert_node( + &mut self, + entity: Entity, + layout: FlexLayoutQueryItem<'_>, + scale_factor: f64, + ) { let mut added = false; let taffy = &mut self.taffy; - let taffy_style = convert::from_style(scale_factor, style); + let taffy_style = convert::from_flex_layout(scale_factor, layout); let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| { added = true; taffy.new_leaf(taffy_style).unwrap() @@ -77,12 +87,12 @@ impl FlexSurface { pub fn upsert_leaf( &mut self, entity: Entity, - style: &Style, + layout: FlexLayoutQueryItem<'_>, calculated_size: CalculatedSize, scale_factor: f64, ) { let taffy = &mut self.taffy; - let taffy_style = convert::from_style(scale_factor, style); + let taffy_style = convert::from_flex_layout(scale_factor, layout); let measure = taffy::node::MeasureFunc::Boxed(Box::new( move |constraints: Size>, _available: Size| { let mut size = convert::from_f32_size(scale_factor, calculated_size.size); @@ -227,10 +237,13 @@ pub fn flex_node_system( mut scale_factor_events: EventReader, mut flex_surface: ResMut, root_node_query: Query, Without)>, - node_query: Query<(Entity, &Style, Option<&CalculatedSize>), (With, Changed