1
1
mod convert;
2
2
3
- use crate :: { CalculatedSize , Node , Style } ;
3
+ use crate :: { CalculatedSize , ControlNode , Node , Style } ;
4
4
use bevy_app:: EventReader ;
5
5
use bevy_ecs:: {
6
6
entity:: Entity ,
@@ -111,19 +111,62 @@ impl FlexSurface {
111
111
}
112
112
}
113
113
114
- pub fn update_children ( & mut self , entity : Entity , children : & Children ) {
114
+ pub fn update_children (
115
+ & mut self ,
116
+ entity : Entity ,
117
+ children : & Children ,
118
+ control_node_query : & mut Query < & mut ControlNode > ,
119
+ unfiltered_children_query : & Query < & Children > ,
120
+ ) {
115
121
let mut stretch_children = Vec :: with_capacity ( children. len ( ) ) ;
116
- for child in children. iter ( ) {
117
- if let Some ( stretch_node) = self . entity_to_stretch . get ( child) {
118
- stretch_children. push ( * stretch_node) ;
122
+ fn inner (
123
+ true_parent : Entity ,
124
+ child : Entity ,
125
+ control_node_query : & mut Query < & mut ControlNode > ,
126
+ unfiltered_children_query : & Query < & Children > ,
127
+ do_on_real : & mut impl FnMut ( Entity ) ,
128
+ ) {
129
+ if let Ok ( mut control_node) = control_node_query. get_mut ( child) {
130
+ control_node. true_parent = Some ( true_parent) ;
131
+ for & child in unfiltered_children_query
132
+ . get ( child)
133
+ . ok ( )
134
+ . into_iter ( )
135
+ . map ( |c| & * * c)
136
+ . flatten ( )
137
+ {
138
+ inner (
139
+ true_parent,
140
+ child,
141
+ control_node_query,
142
+ unfiltered_children_query,
143
+ do_on_real,
144
+ ) ;
145
+ }
119
146
} else {
120
- warn ! (
121
- "Unstyled child in a UI entity hierarchy. You are using an entity \
122
- without UI components as a child of an entity with UI components, results may be unexpected."
123
- ) ;
147
+ do_on_real ( child) ;
124
148
}
125
149
}
126
150
151
+ for & child in children. iter ( ) {
152
+ inner (
153
+ entity,
154
+ child,
155
+ control_node_query,
156
+ unfiltered_children_query,
157
+ & mut |e| {
158
+ if let Some ( stretch_node) = self . entity_to_stretch . get ( & e) {
159
+ stretch_children. push ( * stretch_node) ;
160
+ } else {
161
+ warn ! (
162
+ "Unstyled child in a UI entity hierarchy. You are using an entity \
163
+ without UI components as a child of an entity with UI components, results may be unexpected."
164
+ ) ;
165
+ }
166
+ } ,
167
+ ) ;
168
+ }
169
+
127
170
let stretch_node = self . entity_to_stretch . get ( & entity) . unwrap ( ) ;
128
171
self . stretch
129
172
. set_children ( * stretch_node, stretch_children)
@@ -207,7 +250,10 @@ pub fn flex_node_system(
207
250
( Entity , & Style , & CalculatedSize ) ,
208
251
( With < Node > , Changed < CalculatedSize > ) ,
209
252
> ,
210
- children_query : Query < ( Entity , & Children ) , ( With < Node > , Changed < Children > ) > ,
253
+ changed_children_query : Query < ( Entity , & Children ) , ( With < Node > , Changed < Children > ) > ,
254
+ unfiltered_children_query : Query < & Children > ,
255
+ mut control_node_query : Query < & mut ControlNode > ,
256
+ changed_cnc_query : Query < Entity , ( Changed < Children > , With < ControlNode > ) > ,
211
257
mut node_transform_query : Query < ( Entity , & mut Node , & mut Transform , Option < & Parent > ) > ,
212
258
) {
213
259
// update window root nodes
@@ -262,8 +308,28 @@ pub fn flex_node_system(
262
308
}
263
309
264
310
// update children
265
- for ( entity, children) in children_query. iter ( ) {
266
- flex_surface. update_children ( entity, children) ;
311
+ for ( entity, children) in changed_children_query. iter ( ) {
312
+ flex_surface. update_children (
313
+ entity,
314
+ children,
315
+ & mut control_node_query,
316
+ & unfiltered_children_query,
317
+ ) ;
318
+ }
319
+
320
+ for entity in changed_cnc_query. iter ( ) {
321
+ let true_parent = if let Some ( e) = control_node_query. get_mut ( entity) . unwrap ( ) . true_parent {
322
+ e
323
+ } else {
324
+ continue ;
325
+ } ;
326
+ let children = unfiltered_children_query. get ( true_parent) . unwrap ( ) ;
327
+ flex_surface. update_children (
328
+ true_parent,
329
+ children,
330
+ & mut control_node_query,
331
+ & unfiltered_children_query,
332
+ ) ;
267
333
}
268
334
269
335
// compute layouts
@@ -284,7 +350,11 @@ pub fn flex_node_system(
284
350
position. x = to_logical ( layout. location . x + layout. size . width / 2.0 ) ;
285
351
position. y = to_logical ( layout. location . y + layout. size . height / 2.0 ) ;
286
352
if let Some ( parent) = parent {
287
- if let Ok ( parent_layout) = flex_surface. get_layout ( parent. 0 ) {
353
+ let parent = control_node_query
354
+ . get_mut ( parent. 0 )
355
+ . map ( |cn| cn. true_parent . unwrap ( ) )
356
+ . unwrap_or ( parent. 0 ) ;
357
+ if let Ok ( parent_layout) = flex_surface. get_layout ( parent) {
288
358
position. x -= to_logical ( parent_layout. size . width / 2.0 ) ;
289
359
position. y -= to_logical ( parent_layout. size . height / 2.0 ) ;
290
360
}
0 commit comments