diff --git a/examples/custom_shader.rs b/examples/custom_shader.rs index ac7e380775c85ffc8c2623023a57cff4e378913f..c2f68a924512ca5417f68dcd2c5bf8bd780ab768 100644 --- a/examples/custom_shader.rs +++ b/examples/custom_shader.rs @@ -28,6 +28,8 @@ fn startup( let my_material = MyUIMaterial {}; let my_material_handle = materials.add(my_material); + let my_material_handle1 = my_material_handle.clone(); + let my_material_handle2 = my_material_handle.clone(); let mut widget_context = KayakRootContext::new(camera_entity); widget_context.add_plugin(KayakWidgetsContextPlugin); @@ -36,8 +38,9 @@ fn startup( <KayakAppBundle> <TextWidgetBundle styles={KStyle { + position_type: KPositionType::SelfDirected.into(), material: MaterialHandle::new(move |commands, entity| { - commands.entity(entity).insert(my_material_handle.clone_weak()); + commands.entity(entity).insert(my_material_handle1.clone_weak()); }).into(), ..Default::default() }} @@ -47,6 +50,22 @@ fn startup( ..Default::default() }} /> + <TextWidgetBundle + styles={KStyle { + position_type: KPositionType::SelfDirected.into(), + left: Units::Pixels(20.0).into(), + top: Units::Pixels(5.0).into(), + material: MaterialHandle::new(move |commands, entity| { + commands.entity(entity).insert(my_material_handle2.clone_weak()); + }).into(), + ..Default::default() + }} + text={TextProps { + content: "Hello World!".into(), + size: 20.0, + ..Default::default() + }} + /> </KayakAppBundle> }; diff --git a/src/context.rs b/src/context.rs index 61612160a982173adb610a5642af223bead37aa9..14459695d4962419d09bcdaa4bf4572323454355 100644 --- a/src/context.rs +++ b/src/context.rs @@ -741,16 +741,6 @@ fn update_widgets( unique_ids_parents: &Arc<RwLock<HashMap<Entity, Entity>>>, ) { for entity in widgets.iter() { - // A small hack to add parents to widgets - // let mut command_queue = CommandQueue::default(); - // { - // let mut commands = Commands::new(&mut command_queue, &world); - // if let Some(mut entity_commands) = commands.get_entity(entity.0) { - // entity_commands.set_parent(camera_entity); - // } - // } - // command_queue.apply(world); - if let Some(entity_ref) = world.get_entity(entity.0) { if let Some(widget_type) = entity_ref.get::<WidgetName>() { let widget_context = KayakWidgetContext::new( @@ -816,6 +806,38 @@ fn update_widgets( let mut despawn_list = Vec::default(); 'outer: for (_index, changed_entity, parent, changes) in diff.changes.iter() { + // If a tree node goes from A to B we need to know and delete the descendants. + if let Ok(previous_entities) = cloned_widget_entities.read() { + if let Some(previous_entity) = + previous_entities.get(&changed_entity.0) + { + if let (Some(entity_ref), Some(prev_entity_ref)) = ( + world.get_entity(changed_entity.0), + world.get_entity(*previous_entity), + ) { + if let (Some(widget_name), Some(prev_widget_name)) = ( + entity_ref.get::<WidgetName>(), + prev_entity_ref.get::<WidgetName>(), + ) { + if widget_name != prev_widget_name { + // Do something + for child in tree.down_iter_at(*changed_entity, false) { + if let Some(parent) = tree.parent(child) { + // Due to a bug in bevy we need to remove the parent manually otherwise we'll panic later. + if let Some(mut entity_mut) = world.get_entity_mut(child.0) { + entity_mut.remove_parent(); + } + despawn_list.push((parent.0, child.0)); + if let Ok(mut order_tree) = order_tree.try_write() { + order_tree.remove(child); + } + } + } + } + } + } + } + } if changes.iter().any(|change| *change == Change::Inserted) { if let Some(mut entity_commands) = world.get_entity_mut(changed_entity.0)