Skip to content

Commit fbb7c67

Browse files
committed
Fracture style into multiple components
1 parent 519f6f4 commit fbb7c67

34 files changed

+1652
-1247
lines changed

crates/bevy_ui/src/flex/convert.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
use crate::{
2-
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
3-
PositionType, Size, Style, UiRect, Val,
2+
layout_components::{
3+
flex::{
4+
AlignContent, AlignItems, AlignSelf, Direction, FlexLayoutQueryItem, JustifyContent,
5+
Wrap,
6+
},
7+
Position,
8+
},
9+
Display,
410
};
11+
use crate::{Size, UiRect, Val};
512

613
pub fn from_rect(
714
scale_factor: f64,
@@ -32,28 +39,28 @@ pub fn from_val_size(
3239
}
3340
}
3441

35-
pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
42+
pub fn from_flex_layout(scale_factor: f64, value: FlexLayoutQueryItem<'_>) -> taffy::style::Style {
3643
taffy::style::Style {
37-
display: value.display.into(),
38-
position_type: value.position_type.into(),
39-
flex_direction: value.flex_direction.into(),
40-
flex_wrap: value.flex_wrap.into(),
41-
align_items: value.align_items.into(),
42-
align_self: value.align_self.into(),
43-
align_content: value.align_content.into(),
44-
justify_content: value.justify_content.into(),
45-
position: from_rect(scale_factor, value.position),
46-
margin: from_rect(scale_factor, value.margin),
47-
padding: from_rect(scale_factor, value.padding),
48-
border: from_rect(scale_factor, value.border),
49-
flex_grow: value.flex_grow,
50-
flex_shrink: value.flex_shrink,
51-
flex_basis: from_val(scale_factor, value.flex_basis),
52-
size: from_val_size(scale_factor, value.size),
53-
min_size: from_val_size(scale_factor, value.min_size),
54-
max_size: from_val_size(scale_factor, value.max_size),
55-
aspect_ratio: value.aspect_ratio,
56-
gap: from_val_size(scale_factor, value.gap),
44+
display: (value.control.display).into(),
45+
position_type: (value.control.position).into(),
46+
flex_direction: value.layout.direction.into(),
47+
flex_wrap: value.layout.wrap.into(),
48+
align_items: value.layout.align_items.into(),
49+
align_self: value.child_layout.align_self.into(),
50+
align_content: value.layout.align_content.into(),
51+
justify_content: value.layout.justify_content.into(),
52+
position: from_rect(scale_factor, value.control.inset.0),
53+
margin: from_rect(scale_factor, value.spacing.margin),
54+
padding: from_rect(scale_factor, value.spacing.padding),
55+
border: from_rect(scale_factor, value.spacing.border),
56+
flex_grow: value.child_layout.grow,
57+
flex_shrink: value.child_layout.shrink,
58+
flex_basis: from_val(scale_factor, value.child_layout.basis),
59+
size: from_val_size(scale_factor, value.size_constraints.suggested),
60+
min_size: from_val_size(scale_factor, value.size_constraints.min),
61+
max_size: from_val_size(scale_factor, value.size_constraints.max),
62+
aspect_ratio: value.size_constraints.aspect_ratio,
63+
gap: from_val_size(scale_factor, value.layout.gap),
5764
}
5865
}
5966

@@ -122,13 +129,13 @@ impl From<Display> for taffy::style::Display {
122129
}
123130
}
124131

125-
impl From<FlexDirection> for taffy::style::FlexDirection {
126-
fn from(value: FlexDirection) -> Self {
132+
impl From<Direction> for taffy::style::FlexDirection {
133+
fn from(value: Direction) -> Self {
127134
match value {
128-
FlexDirection::Row => taffy::style::FlexDirection::Row,
129-
FlexDirection::Column => taffy::style::FlexDirection::Column,
130-
FlexDirection::RowReverse => taffy::style::FlexDirection::RowReverse,
131-
FlexDirection::ColumnReverse => taffy::style::FlexDirection::ColumnReverse,
135+
Direction::Row => taffy::style::FlexDirection::Row,
136+
Direction::Column => taffy::style::FlexDirection::Column,
137+
Direction::RowReverse => taffy::style::FlexDirection::RowReverse,
138+
Direction::ColumnReverse => taffy::style::FlexDirection::ColumnReverse,
132139
}
133140
}
134141
}
@@ -146,21 +153,21 @@ impl From<JustifyContent> for taffy::style::JustifyContent {
146153
}
147154
}
148155

149-
impl From<PositionType> for taffy::style::PositionType {
150-
fn from(value: PositionType) -> Self {
156+
impl From<Position> for taffy::style::PositionType {
157+
fn from(value: Position) -> Self {
151158
match value {
152-
PositionType::Relative => taffy::style::PositionType::Relative,
153-
PositionType::Absolute => taffy::style::PositionType::Absolute,
159+
Position::Relative => taffy::style::PositionType::Relative,
160+
Position::Absolute => taffy::style::PositionType::Absolute,
154161
}
155162
}
156163
}
157164

158-
impl From<FlexWrap> for taffy::style::FlexWrap {
159-
fn from(value: FlexWrap) -> Self {
165+
impl From<Wrap> for taffy::style::FlexWrap {
166+
fn from(value: Wrap) -> Self {
160167
match value {
161-
FlexWrap::NoWrap => taffy::style::FlexWrap::NoWrap,
162-
FlexWrap::Wrap => taffy::style::FlexWrap::Wrap,
163-
FlexWrap::WrapReverse => taffy::style::FlexWrap::WrapReverse,
168+
Wrap::NoWrap => taffy::style::FlexWrap::NoWrap,
169+
Wrap::Wrap => taffy::style::FlexWrap::Wrap,
170+
Wrap::WrapReverse => taffy::style::FlexWrap::WrapReverse,
164171
}
165172
}
166173
}

crates/bevy_ui/src/flex/mod.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod convert;
22

3-
use crate::{CalculatedSize, Node, Style, UiScale};
3+
use crate::{
4+
layout_components::flex::{FlexLayoutChanged, FlexLayoutQuery, FlexLayoutQueryItem},
5+
CalculatedSize, Node, UiScale,
6+
};
47
use bevy_ecs::{
58
change_detection::DetectChanges,
69
entity::Entity,
@@ -60,10 +63,17 @@ impl Default for FlexSurface {
6063
}
6164

6265
impl FlexSurface {
63-
pub fn upsert_node(&mut self, entity: Entity, style: &Style, scale_factor: f64) {
66+
/// Inserts the node into the hashmap if they don't already exist,
67+
/// or update the contents if they already to exist.
68+
pub fn upsert_node(
69+
&mut self,
70+
entity: Entity,
71+
layout: FlexLayoutQueryItem<'_>,
72+
scale_factor: f64,
73+
) {
6474
let mut added = false;
6575
let taffy = &mut self.taffy;
66-
let taffy_style = convert::from_style(scale_factor, style);
76+
let taffy_style = convert::from_flex_layout(scale_factor, layout);
6777
let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| {
6878
added = true;
6979
taffy.new_leaf(taffy_style).unwrap()
@@ -77,12 +87,12 @@ impl FlexSurface {
7787
pub fn upsert_leaf(
7888
&mut self,
7989
entity: Entity,
80-
style: &Style,
90+
layout: FlexLayoutQueryItem<'_>,
8191
calculated_size: CalculatedSize,
8292
scale_factor: f64,
8393
) {
8494
let taffy = &mut self.taffy;
85-
let taffy_style = convert::from_style(scale_factor, style);
95+
let taffy_style = convert::from_flex_layout(scale_factor, layout);
8696
let measure = taffy::node::MeasureFunc::Boxed(Box::new(
8797
move |constraints: Size<Option<f32>>, _available: Size<AvailableSpace>| {
8898
let mut size = convert::from_f32_size(scale_factor, calculated_size.size);
@@ -227,10 +237,13 @@ pub fn flex_node_system(
227237
mut scale_factor_events: EventReader<WindowScaleFactorChanged>,
228238
mut flex_surface: ResMut<FlexSurface>,
229239
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
230-
node_query: Query<(Entity, &Style, Option<&CalculatedSize>), (With<Node>, Changed<Style>)>,
231-
full_node_query: Query<(Entity, &Style, Option<&CalculatedSize>), With<Node>>,
240+
node_query: Query<
241+
(Entity, FlexLayoutQuery, Option<&CalculatedSize>),
242+
(With<Node>, FlexLayoutChanged),
243+
>,
244+
full_node_query: Query<(Entity, FlexLayoutQuery, Option<&CalculatedSize>), With<Node>>,
232245
changed_size_query: Query<
233-
(Entity, &Style, &CalculatedSize),
246+
(Entity, FlexLayoutQuery, &CalculatedSize),
234247
(With<Node>, Changed<CalculatedSize>),
235248
>,
236249
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
@@ -257,15 +270,15 @@ pub fn flex_node_system(
257270
fn update_changed<F: ReadOnlyWorldQuery>(
258271
flex_surface: &mut FlexSurface,
259272
scaling_factor: f64,
260-
query: Query<(Entity, &Style, Option<&CalculatedSize>), F>,
273+
query: Query<(Entity, FlexLayoutQuery, Option<&CalculatedSize>), F>,
261274
) {
262275
// update changed nodes
263-
for (entity, style, calculated_size) in &query {
276+
for (entity, layout, calculated_size) in &query {
264277
// TODO: remove node from old hierarchy if its root has changed
265278
if let Some(calculated_size) = calculated_size {
266-
flex_surface.upsert_leaf(entity, style, *calculated_size, scaling_factor);
279+
flex_surface.upsert_leaf(entity, layout, *calculated_size, scaling_factor);
267280
} else {
268-
flex_surface.upsert_node(entity, style, scaling_factor);
281+
flex_surface.upsert_node(entity, layout, scaling_factor);
269282
}
270283
}
271284
}
@@ -276,8 +289,8 @@ pub fn flex_node_system(
276289
update_changed(&mut flex_surface, scale_factor, node_query);
277290
}
278291

279-
for (entity, style, calculated_size) in &changed_size_query {
280-
flex_surface.upsert_leaf(entity, style, *calculated_size, scale_factor);
292+
for (entity, layout, calculated_size) in &changed_size_query {
293+
flex_surface.upsert_leaf(entity, layout, *calculated_size, scale_factor);
281294
}
282295

283296
// clean up removed nodes

crates/bevy_ui/src/focus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use smallvec::SmallVec;
3030
/// This ensures that hidden UI nodes are not interactable,
3131
/// and do not end up stuck in an active state if hidden at the wrong time.
3232
///
33-
/// Note that you can also control the visibility of a node using the [`Display`](crate::ui_node::Display) property,
33+
/// Note that you can also control the visibility of a node using the [`Display`](crate::layout_components::Display) property,
3434
/// which fully collapses it during layout calculations.
3535
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
3636
#[reflect(Component, Serialize, Deserialize, PartialEq)]

crates/bevy_ui/src/geometry.rs

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::Val;
22
use bevy_reflect::Reflect;
3+
use serde::{Deserialize, Serialize};
34
use std::ops::{Div, DivAssign, Mul, MulAssign};
45

5-
/// A type which is commonly used to define positions, margins, paddings and borders.
6+
/// A type which is commonly used throughout UI and layouting, commonly used to define inset, margins, paddings and borders.
67
///
78
/// # Examples
89
///
@@ -65,61 +66,17 @@ use std::ops::{Div, DivAssign, Mul, MulAssign};
6566
/// right values of the position because the size of the UI element is already explicitly specified.
6667
///
6768
/// ```
68-
/// # use bevy_ui::{UiRect, Size, Val, Style};
69+
/// # use bevy_ui::{UiRect, Size, Val, SizeConstraints, Inset};
6970
/// # use bevy_utils::default;
7071
/// #
71-
/// let style = Style {
72-
/// position: UiRect { // Defining all four sides
73-
/// left: Val::Px(100.0),
74-
/// right: Val::Px(200.0),
75-
/// top: Val::Px(300.0),
76-
/// bottom: Val::Px(400.0),
77-
/// },
78-
/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size
79-
/// ..default()
80-
/// };
81-
/// ```
82-
///
83-
/// ## Margin
84-
///
85-
/// A margin is used to create space around UI elements, outside of any defined borders.
86-
///
87-
/// ```
88-
/// # use bevy_ui::{UiRect, Val};
89-
/// #
90-
/// let margin = UiRect::all(Val::Auto); // Centers the UI element
91-
/// ```
92-
///
93-
/// ## Padding
94-
///
95-
/// A padding is used to create space around UI elements, inside of any defined borders.
96-
///
97-
/// ```
98-
/// # use bevy_ui::{UiRect, Val};
99-
/// #
100-
/// let padding = UiRect {
101-
/// left: Val::Px(10.0),
102-
/// right: Val::Px(20.0),
103-
/// top: Val::Px(30.0),
104-
/// bottom: Val::Px(40.0),
105-
/// };
106-
/// ```
107-
///
108-
/// ## Borders
109-
///
110-
/// A border is used to define the width of the border of a UI element.
72+
/// let inset = Inset(UiRect {
73+
/// left: Val::Px(100.0),
74+
/// right: Val::Px(200.0),
75+
/// top: Val::Px(300.0),
76+
/// bottom: Val::Px(400.0),
77+
/// });
11178
///
112-
/// ```
113-
/// # use bevy_ui::{UiRect, Val};
114-
/// #
115-
/// let border = UiRect {
116-
/// left: Val::Px(10.0),
117-
/// right: Val::Px(20.0),
118-
/// top: Val::Px(30.0),
119-
/// bottom: Val::Px(40.0),
120-
/// };
121-
/// ```
122-
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
79+
#[derive(Copy, Clone, PartialEq, Debug, Reflect, Serialize, Deserialize)]
12380
#[reflect(PartialEq)]
12481
pub struct UiRect {
12582
/// The value corresponding to the left side of the UI rect.
@@ -133,13 +90,6 @@ pub struct UiRect {
13390
}
13491

13592
impl UiRect {
136-
pub const DEFAULT: Self = Self {
137-
left: Val::DEFAULT,
138-
right: Val::DEFAULT,
139-
top: Val::DEFAULT,
140-
bottom: Val::DEFAULT,
141-
};
142-
14393
/// Creates a new [`UiRect`] from the values specified.
14494
///
14595
/// # Example
@@ -318,6 +268,15 @@ impl UiRect {
318268
..Default::default()
319269
}
320270
}
271+
272+
/// Alias of [`UiRect::UNDEFINED`]
273+
pub const DEFAULT: UiRect = UiRect::UNDEFINED;
274+
275+
/// Creates a [`UiRect`] where all sides are [`Val::Undefined`]
276+
pub const UNDEFINED: UiRect = UiRect::all(Val::Undefined);
277+
278+
/// Creates a [`UiRect`] where all sides are [`Val::Auto`]
279+
pub const AUTO: UiRect = UiRect::all(Val::Auto);
321280
}
322281

323282
impl Default for UiRect {
@@ -329,7 +288,7 @@ impl Default for UiRect {
329288
/// A 2-dimensional area defined by a width and height.
330289
///
331290
/// It is commonly used to define the size of a text or UI element.
332-
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
291+
#[derive(Copy, Clone, PartialEq, Debug, Reflect, Serialize, Deserialize)]
333292
#[reflect(PartialEq)]
334293
pub struct Size {
335294
/// The width of the 2-dimensional area.
@@ -339,11 +298,6 @@ pub struct Size {
339298
}
340299

341300
impl Size {
342-
pub const DEFAULT: Self = Self {
343-
width: Val::DEFAULT,
344-
height: Val::DEFAULT,
345-
};
346-
347301
/// Creates a new [`Size`] from a width and a height.
348302
///
349303
/// # Example
@@ -360,17 +314,42 @@ impl Size {
360314
Size { width, height }
361315
}
362316

363-
/// Creates a Size where both values are [`Val::Auto`].
317+
/// Creates a new [`Size`] where both values are given in [`Val::Px`].
318+
pub const fn px(width: f32, height: f32) -> Self {
319+
Size {
320+
width: Val::Px(width),
321+
height: Val::Px(height),
322+
}
323+
}
324+
325+
/// Creates a new [`Size`] where both values are given in [`Val::Percent`].
326+
pub const fn percent(width: f32, height: f32) -> Self {
327+
Size {
328+
width: Val::Percent(width),
329+
height: Val::Percent(height),
330+
}
331+
}
332+
333+
/// Alias of [`Size::UNDEFINED`]
334+
pub const DEFAULT: Size = Size::UNDEFINED;
335+
336+
/// Creates a new [`Size`] where both values are [`Val::Auto`].
364337
pub const AUTO: Size = Size {
365338
width: Val::Auto,
366339
height: Val::Auto,
367340
};
368341

369-
/// Creates a Size where both values are [`Val::Undefined`].
342+
/// Creates a new [`Size`] where both values are [`Val::Undefined`].
370343
pub const UNDEFINED: Size = Size {
371344
width: Val::Undefined,
372345
height: Val::Undefined,
373346
};
347+
348+
/// Creates a new [`Size`] where both values are 100 percent.
349+
pub const FULL: Size = Size {
350+
width: Val::FULL,
351+
height: Val::FULL,
352+
};
374353
}
375354

376355
impl Default for Size {

0 commit comments

Comments
 (0)