Skip to content

Commit 76557be

Browse files
committed
Collecting Display, Position, Inset, Overflow into LayoutControl
1 parent 0830bb4 commit 76557be

File tree

13 files changed

+219
-177
lines changed

13 files changed

+219
-177
lines changed

crates/bevy_ui/src/flex/convert.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
},
77
Position,
88
},
9-
prelude::FlexContainer,
9+
Display,
1010
};
1111
use crate::{Size, UiRect, Val};
1212

@@ -41,15 +41,15 @@ pub fn from_val_size(
4141

4242
pub fn from_flex_layout(scale_factor: f64, value: FlexLayoutQueryItem<'_>) -> taffy::style::Style {
4343
taffy::style::Style {
44-
display: (*value.layout).into(),
45-
position_type: (*value.position).into(),
44+
display: (value.control.display).into(),
45+
position_type: (value.control.position).into(),
4646
flex_direction: value.layout.direction.into(),
4747
flex_wrap: value.layout.wrap.into(),
4848
align_items: value.layout.align_items.into(),
4949
align_self: value.child_layout.align_self.into(),
5050
align_content: value.layout.align_content.into(),
5151
justify_content: value.layout.justify_content.into(),
52-
position: from_rect(scale_factor, value.offset.0),
52+
position: from_rect(scale_factor, value.control.inset.0),
5353
margin: from_rect(scale_factor, value.spacing.margin),
5454
padding: from_rect(scale_factor, value.spacing.padding),
5555
border: from_rect(scale_factor, value.spacing.border),
@@ -120,11 +120,11 @@ impl From<AlignContent> for taffy::style::AlignContent {
120120
}
121121
}
122122

123-
impl From<FlexContainer> for taffy::style::Display {
124-
fn from(value: FlexContainer) -> Self {
125-
match value.hide_from_layout {
126-
false => taffy::style::Display::Flex,
127-
true => taffy::style::Display::None,
123+
impl From<Display> for taffy::style::Display {
124+
fn from(value: Display) -> Self {
125+
match value {
126+
Display::Flex => taffy::style::Display::Flex,
127+
Display::None => taffy::style::Display::None,
128128
}
129129
}
130130
}

crates/bevy_ui/src/layout_components.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ use bevy_ecs::prelude::Component;
55
use bevy_reflect::prelude::*;
66
use serde::{Deserialize, Serialize};
77

8+
/// Grouping of core control parameter of the layout for this node.
9+
#[derive(Component, Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize, Reflect)]
10+
#[reflect(PartialEq, Serialize, Deserialize)]
11+
pub struct LayoutControl {
12+
/// Defines how the node will parttake in the layouting.
13+
pub display: Display,
14+
/// The strategy used to position this node.
15+
pub position: Position,
16+
/// The inset of this UI node, relative to its default position
17+
pub inset: Inset,
18+
/// The behavior in case the node overflows its allocated space
19+
pub overflow: Overflow,
20+
}
21+
822
/// Defines how the node will parttake in the layouting.
923
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)]
1024
#[reflect(PartialEq, Serialize, Deserialize)]
@@ -21,9 +35,7 @@ pub enum Display {
2135
}
2236

2337
/// The strategy used to position this node.
24-
#[derive(
25-
Component, Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect,
26-
)]
38+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)]
2739
#[reflect_value(PartialEq, Serialize, Deserialize)]
2840
pub enum Position {
2941
/// Positioned in relation to its parent, while considering all sibling-nodes that also have the [`Position::Relative`] value.
@@ -45,17 +57,7 @@ pub struct Gap(pub Size);
4557
/// Layouting is performed by the Flexbox layout algorithm where the value of [`Inset`] is considered.
4658
/// To check the final position of a UI element, read its [`Transform`](bevy_transform::components::Transform) component.
4759
#[derive(
48-
Component,
49-
Deref,
50-
DerefMut,
51-
Copy,
52-
Clone,
53-
PartialEq,
54-
Debug,
55-
Default,
56-
Serialize,
57-
Deserialize,
58-
Reflect,
60+
Deref, DerefMut, Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize, Reflect,
5961
)]
6062
#[reflect_value(PartialEq, Serialize, Deserialize)]
6163
pub struct Inset(pub UiRect);
@@ -240,9 +242,7 @@ impl Spacing {
240242
}
241243

242244
/// Whether to show or hide overflowing items
243-
#[derive(
244-
Component, Copy, Clone, PartialEq, Eq, Debug, Default, Reflect, Serialize, Deserialize,
245-
)]
245+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Reflect, Serialize, Deserialize)]
246246
#[reflect_value(PartialEq, Serialize, Deserialize)]
247247
pub enum Overflow {
248248
/// Show overflowing items
@@ -264,31 +264,24 @@ pub mod flex {
264264
/// See [`FlexLayoutChanged`] when attempting to use this as a query filter.
265265
#[derive(WorldQuery)]
266266
pub struct FlexLayoutQuery {
267+
pub control: &'static LayoutControl,
267268
/// Defines how this node's layout should be.
268269
pub layout: &'static FlexContainer,
269270
/// Defines how this node should behave as a child of a node.
270271
pub child_layout: &'static FlexItem,
271-
/// The position of this UI node
272-
pub offset: &'static Inset,
273-
/// Whether the node should be absolute or relatively positioned
274-
pub position: &'static Position,
275272
/// The constraints on the size of this node
276273
pub size_constraints: &'static SizeConstraints,
277274
/// The margin, padding and border of the UI node
278275
pub spacing: &'static Spacing,
279-
/// The behavior in case the node overflows its allocated space
280-
pub overflow: &'static Overflow,
281276
}
282277

283278
/// A type alias for when any of the components in a [`FlexLayoutQuery`] have changed.
284279
pub type FlexLayoutChanged = Or<(
280+
Changed<LayoutControl>,
285281
Changed<FlexContainer>,
286282
Changed<FlexItem>,
287-
Changed<Inset>,
288-
Changed<Position>,
289283
Changed<SizeConstraints>,
290284
Changed<Spacing>,
291-
Changed<Overflow>,
292285
)>;
293286

294287
/// The flexbox-specific layout configuration of a UI node
@@ -312,10 +305,6 @@ pub mod flex {
312305
pub justify_content: JustifyContent,
313306
/// Controls how the content wraps
314307
pub wrap: Wrap,
315-
/// If true will hide this node from the layouting.
316-
///
317-
/// Same as specifying in css `Display: None;`
318-
pub hide_from_layout: bool,
319308
/// The size of the gutters between the rows and columns of the flexbox layout
320309
pub gap: Gap,
321310
}

crates/bevy_ui/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl Plugin for UiPlugin {
9191
.register_type::<AlignItems>()
9292
.register_type::<AlignSelf>()
9393
.register_type::<CalculatedSize>()
94+
.register_type::<LayoutControl>()
9495
.register_type::<FlexContainer>()
9596
.register_type::<FlexItem>()
9697
.register_type::<Gap>()

crates/bevy_ui/src/node_bundles.rs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! This module contains basic node bundles used to build UIs
22
33
use crate::{
4-
layout_components::{Inset, Overflow, Position, SizeConstraints, Spacing},
4+
layout_components::{Inset, Position, SizeConstraints, Spacing},
55
prelude::{FlexContainer, FlexItem},
66
widget::Button,
7-
BackgroundColor, CalculatedSize, FocusPolicy, Interaction, Node, UiImage, ZIndex,
7+
BackgroundColor, CalculatedSize, FocusPolicy, Interaction, LayoutControl, Node, UiImage,
8+
ZIndex,
89
};
910
use bevy_ecs::bundle::Bundle;
1011
use bevy_render::{
@@ -21,20 +22,18 @@ use bevy_transform::prelude::{GlobalTransform, Transform};
2122
pub struct NodeBundle {
2223
/// Describes the size of the node
2324
pub node: Node,
25+
/// Core controls for layouting of this node.
26+
///
27+
/// See: [`Display`](crate::Display), [`Position`](crate::Position), [`Inset`](crate::Inset), [`Overflow`](crate::Overflow).
28+
pub control: LayoutControl,
2429
/// Defines how this node's layout should be.
2530
pub layout: FlexContainer,
2631
/// Defines how this node should behave as a child of a node.
2732
pub child_layout: FlexItem,
28-
/// The inset of this UI node, relative to its default position
29-
pub inset: Inset,
30-
/// Whether the node should be absolute or relatively positioned
31-
pub position: Position,
3233
/// The constraints on the size of this node
3334
pub size_constraints: SizeConstraints,
3435
/// The margin, padding and border of the UI node
3536
pub spacing: Spacing,
36-
/// The behavior in case the node overflows its allocated space
37-
pub overflow: Overflow,
3837
/// The background color, which serves as a "fill" for this node
3938
pub background_color: BackgroundColor,
4039
/// Whether this node should block interaction with lower nodes
@@ -44,14 +43,14 @@ pub struct NodeBundle {
4443
/// This field is automatically managed by the UI layout system.
4544
/// To alter the position of this entity, use the properties of layouting components.
4645
///
47-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
46+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
4847
pub transform: Transform,
4948
/// The global transform of the node
5049
///
5150
/// This field is automatically managed by the UI layout system.
5251
/// To alter the position of this entity, use the properties of layouting components.
5352
///
54-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
53+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
5554
pub global_transform: GlobalTransform,
5655
/// Describes the visibility properties of the node
5756
pub visibility: Visibility,
@@ -64,15 +63,13 @@ pub struct NodeBundle {
6463
impl Default for NodeBundle {
6564
fn default() -> Self {
6665
NodeBundle {
67-
// Transparent background
6866
node: Default::default(),
67+
control: Default::default(),
6968
layout: Default::default(),
7069
child_layout: Default::default(),
71-
inset: Default::default(),
72-
position: Default::default(),
7370
size_constraints: Default::default(),
7471
spacing: Default::default(),
75-
overflow: Default::default(),
72+
// Transparent background
7673
background_color: Color::NONE.into(),
7774
focus_policy: Default::default(),
7875
transform: Default::default(),
@@ -89,20 +86,18 @@ impl Default for NodeBundle {
8986
pub struct ImageBundle {
9087
/// Describes the size of the node
9188
pub node: Node,
89+
/// Core controls for layouting of this node.
90+
///
91+
/// See: [`Display`](crate::Display), [`Position`](crate::Position), [`Inset`](crate::Inset), [`Overflow`](crate::Overflow).
92+
pub control: LayoutControl,
9293
/// Defines how this node's layout should be.
9394
pub layout: FlexContainer,
9495
/// Defines how this node should behave as a child of a node.
9596
pub child_layout: FlexItem,
96-
/// The inset of this UI node, relative to its default position
97-
pub inset: Inset,
98-
/// Whether the node should be absolute or relatively positioned
99-
pub position: Position,
10097
/// The constraints on the size of this node
10198
pub size_constraints: SizeConstraints,
10299
/// The margin, padding and border of the UI node
103100
pub spacing: Spacing,
104-
/// The behavior in case the node overflows its allocated space
105-
pub overflow: Overflow,
106101
/// The calculated size based on the given image
107102
pub calculated_size: CalculatedSize,
108103
/// The background color, which serves as a "fill" for this node
@@ -118,14 +113,14 @@ pub struct ImageBundle {
118113
/// This field is automatically managed by the UI layout system.
119114
/// To alter the position of this entity, use the properties of layouting components.
120115
///
121-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
116+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
122117
pub transform: Transform,
123118
/// The global transform of the node
124119
///
125120
/// This field is automatically managed by the UI layout system.
126121
/// To alter the position of this entity, use the properties of layouting components.
127122
///
128-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
123+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
129124
pub global_transform: GlobalTransform,
130125
/// Describes the visibility properties of the node
131126
pub visibility: Visibility,
@@ -140,20 +135,18 @@ pub struct ImageBundle {
140135
pub struct TextBundle {
141136
/// Describes the size of the node
142137
pub node: Node,
138+
/// Core controls for layouting of this node.
139+
///
140+
/// See: [`Display`](crate::Display), [`Position`](crate::Position), [`Inset`](crate::Inset), [`Overflow`](crate::Overflow).
141+
pub control: LayoutControl,
143142
/// Defines how this node's layout should be.
144143
pub layout: FlexContainer,
145144
/// Defines how this node should behave as a child of a node.
146145
pub child_layout: FlexItem,
147-
/// The inset of this UI node, relative to its default position
148-
pub inset: Inset,
149-
/// Whether the node should be absolute or relatively positioned
150-
pub position: Position,
151146
/// The constraints on the size of this node
152147
pub size_constraints: SizeConstraints,
153148
/// The margin, padding and border of the UI node
154149
pub spacing: Spacing,
155-
/// The behavior in case the node overflows its allocated space
156-
pub overflow: Overflow,
157150
/// Contains the text of the node
158151
pub text: Text,
159152
/// The calculated size based on the given image
@@ -165,14 +158,14 @@ pub struct TextBundle {
165158
/// This field is automatically managed by the UI layout system.
166159
/// To alter the position of this entity, use the properties of layouting components.
167160
///
168-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
161+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
169162
pub transform: Transform,
170163
/// The global transform of the node
171164
///
172165
/// This field is automatically managed by the UI layout system.
173166
/// To alter the position of this entity, use the properties of layouting components.
174167
///
175-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
168+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
176169
pub global_transform: GlobalTransform,
177170
/// Describes the visibility properties of the node
178171
pub visibility: Visibility,
@@ -235,13 +228,13 @@ impl TextBundle {
235228

236229
/// Returns this [`TextBundle`] with a new [`Position`].
237230
pub const fn with_position(mut self, position: Position) -> Self {
238-
self.position = position;
231+
self.control.position = position;
239232
self
240233
}
241234

242235
/// Returns this [`TextBundle`] with a new [`Inset`].
243236
pub const fn with_inset(mut self, inset: Inset) -> Self {
244-
self.inset = inset;
237+
self.control.inset = inset;
245238
self
246239
}
247240
}
@@ -254,16 +247,14 @@ impl Default for TextBundle {
254247
node: Default::default(),
255248
calculated_size: Default::default(),
256249
layout: Default::default(),
250+
control: Default::default(),
257251
child_layout: Default::default(),
258-
position: Default::default(),
259252
size_constraints: Default::default(),
260-
overflow: Default::default(),
261253
transform: Default::default(),
262254
global_transform: Default::default(),
263255
visibility: Default::default(),
264256
computed_visibility: Default::default(),
265257
z_index: Default::default(),
266-
inset: Default::default(),
267258
spacing: Default::default(),
268259
}
269260
}
@@ -276,20 +267,18 @@ pub struct ButtonBundle {
276267
pub node: Node,
277268
/// Marker component that signals this node is a button
278269
pub button: Button,
270+
/// Core controls for layouting of this node.
271+
///
272+
/// See: [`Display`](crate::Display), [`Position`](crate::Position), [`Inset`](crate::Inset), [`Overflow`](crate::Overflow).
273+
pub control: LayoutControl,
279274
/// Defines how this node's layout should be.
280275
pub layout: FlexContainer,
281276
/// Defines how this node should behave as a child of a node.
282277
pub child_layout: FlexItem,
283-
/// The inset of this UI node, relative to its default position
284-
pub inset: Inset,
285-
/// Whether the node should be absolute or relatively positioned
286-
pub position: Position,
287278
/// The constraints on the size of this node
288279
pub size_constraints: SizeConstraints,
289280
/// The margin, padding and border of the UI node
290281
pub spacing: Spacing,
291-
/// The behavior in case the node overflows its allocated space
292-
pub overflow: Overflow,
293282
/// Describes whether and how the button has been interacted with by the input
294283
pub interaction: Interaction,
295284
/// Whether this node should block interaction with lower nodes
@@ -305,14 +294,14 @@ pub struct ButtonBundle {
305294
/// This field is automatically managed by the UI layout system.
306295
/// To alter the position of this entity, use the properties of layouting components.
307296
///
308-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
297+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
309298
pub transform: Transform,
310299
/// The global transform of the node
311300
///
312301
/// This field is automatically managed by the UI layout system.
313302
/// To alter the position of this entity, use the properties of layouting components.
314303
///
315-
/// See: [`FlexContainer`], [`FlexItem`], [`Inset`], [`Position`], [`SizeConstraints`], [`Spacing`], [`Overflow`].
304+
/// See: [`LayoutControl`], [`FlexContainer`], [`FlexItem`], [`SizeConstraints`], [`Spacing`].
316305
pub global_transform: GlobalTransform,
317306
/// Describes the visibility properties of the node
318307
pub visibility: Visibility,

0 commit comments

Comments
 (0)