From 0a88bb5598b8bfabf104e035fafac1eb84d1304c Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 24 Aug 2023 15:00:42 +0100 Subject: [PATCH] Added marker components: * ExtractUiNode * ExtractUiNodeBorder * ExtractUiNodeText * ExtractUiNodeAtlasImage These are added to the relevant UI node bundles. The UI's extraction functions now have query filters that allow selective extraction of UI node entities: * `extract_uinode` has filter `With` * `extract_uinode_borders` has filter `With` * `extract_text_uinodes` has filter `With` * `extract_atlas_uinodes` has filter `With` --- crates/bevy_ui/src/node_bundles.rs | 25 ++++++++++++++++- crates/bevy_ui/src/render/mod.rs | 44 ++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/crates/bevy_ui/src/node_bundles.rs b/crates/bevy_ui/src/node_bundles.rs index 84f7940fe1734..2eeca10af26e2 100644 --- a/crates/bevy_ui/src/node_bundles.rs +++ b/crates/bevy_ui/src/node_bundles.rs @@ -4,7 +4,8 @@ use crate::widget::TextFlags; use crate::{ widget::{Button, UiImageSize}, - BackgroundColor, BorderColor, ContentSize, FocusPolicy, Interaction, Node, Style, UiImage, + BackgroundColor, BorderColor, ContentSize, ExtractUiNode, ExtractUiNodeAtlasImage, + ExtractUiNodeBorder, ExtractUiNodeText, FocusPolicy, Interaction, Node, Style, UiImage, UiTextureAtlasImage, ZIndex, }; use bevy_asset::Handle; @@ -50,6 +51,10 @@ pub struct NodeBundle { pub computed_visibility: ComputedVisibility, /// Indicates the depth at which the node should appear in the UI pub z_index: ZIndex, + /// Extract with [`extract_uinodes`](`crate::render::extract_uinodes`) + pub extract_uinode: ExtractUiNode, + /// Extract with [`extract_uinode_borders`](`crate::render::extract_uinode_borders`) + pub extract_border: ExtractUiNodeBorder, } impl Default for NodeBundle { @@ -66,6 +71,8 @@ impl Default for NodeBundle { visibility: Default::default(), computed_visibility: Default::default(), z_index: Default::default(), + extract_uinode: ExtractUiNode, + extract_border: ExtractUiNodeBorder, } } } @@ -108,6 +115,8 @@ pub struct ImageBundle { pub computed_visibility: ComputedVisibility, /// Indicates the depth at which the node should appear in the UI pub z_index: ZIndex, + /// Extract with [`extract_uinodes`](`crate::render::extract_uinodes`) + pub extract_uinode: ExtractUiNode, } /// A UI node that is a texture atlas sprite @@ -150,6 +159,8 @@ pub struct AtlasImageBundle { pub computed_visibility: ComputedVisibility, /// Indicates the depth at which the node should appear in the UI pub z_index: ZIndex, + /// Extract with [`extract_uinodes`](`crate::render::extract_uinodes`) + pub extract_uinode: ExtractUiNodeAtlasImage, } #[cfg(feature = "bevy_text")] @@ -189,6 +200,10 @@ pub struct TextBundle { pub z_index: ZIndex, /// The background color that will fill the containing node pub background_color: BackgroundColor, + /// Extract with [`extract_uinodes`](`crate::render::extract_uinodes`) + pub extract_uinode: ExtractUiNode, + /// Extract with [`extract_text_uinodes`](`crate::render::extract_text_uinodes`) + pub extract_text: ExtractUiNodeText, } #[cfg(feature = "bevy_text")] @@ -209,6 +224,8 @@ impl Default for TextBundle { visibility: Default::default(), computed_visibility: Default::default(), z_index: Default::default(), + extract_uinode: ExtractUiNode, + extract_text: ExtractUiNodeText, } } } @@ -299,6 +316,10 @@ pub struct ButtonBundle { pub computed_visibility: ComputedVisibility, /// Indicates the depth at which the node should appear in the UI pub z_index: ZIndex, + /// Extract with [`extract_uinodes`](`crate::render::extract_uinodes`) + pub extract_node: ExtractUiNode, + /// Extract with [`extract_uinode_borders`](`crate::render::extract_uinode_borders`) + pub extract_border: ExtractUiNodeBorder, } impl Default for ButtonBundle { @@ -317,6 +338,8 @@ impl Default for ButtonBundle { visibility: Default::default(), computed_visibility: Default::default(), z_index: Default::default(), + extract_node: ExtractUiNode, + extract_border: ExtractUiNodeBorder, } } } diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index c2e82a7440671..f151adf1b99d6 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -3,6 +3,7 @@ mod render_pass; use bevy_core_pipeline::{core_2d::Camera2d, core_3d::Camera3d}; use bevy_hierarchy::Parent; +use bevy_reflect::std_traits::ReflectDefault; use bevy_render::{ExtractSchedule, Render}; use bevy_window::{PrimaryWindow, Window}; pub use pipeline::*; @@ -17,7 +18,7 @@ use bevy_app::prelude::*; use bevy_asset::{load_internal_asset, AssetEvent, Assets, Handle, HandleUntyped}; use bevy_ecs::prelude::*; use bevy_math::{Mat4, Rect, URect, UVec4, Vec2, Vec3, Vec4Swizzles}; -use bevy_reflect::TypeUuid; +use bevy_reflect::{Reflect, TypeUuid}; use bevy_render::texture::DEFAULT_IMAGE_HANDLE; use bevy_render::{ camera::Camera, @@ -164,6 +165,22 @@ pub struct ExtractedUiNode { pub flip_y: bool, } +#[derive(Component, Default, Debug, Copy, Clone, Reflect)] +#[reflect(Component, Default)] +pub struct ExtractUiNode; + +#[derive(Component, Default, Debug, Copy, Clone, Reflect)] +#[reflect(Component, Default)] +pub struct ExtractUiNodeBorder; + +#[derive(Component, Default, Debug, Copy, Clone, Reflect)] +#[reflect(Component, Default)] +pub struct ExtractUiNodeText; + +#[derive(Component, Default, Debug, Copy, Clone, Reflect)] +#[reflect(Component, Default)] +pub struct ExtractUiNodeAtlasImage; + #[derive(Resource, Default)] pub struct ExtractedUiNodes { pub uinodes: Vec, @@ -185,7 +202,7 @@ pub fn extract_atlas_uinodes( &Handle, &UiTextureAtlasImage, ), - Without, + (With, Without), >, >, ) { @@ -273,7 +290,7 @@ pub fn extract_uinode_borders( &ComputedVisibility, Option<&CalculatedClip>, ), - Without, + (With, Without), >, >, node_query: Extract>, @@ -390,7 +407,7 @@ pub fn extract_uinodes( &ComputedVisibility, Option<&CalculatedClip>, ), - Without, + (With, Without), >, >, ) { @@ -512,14 +529,17 @@ pub fn extract_text_uinodes( ui_stack: Extract>, ui_scale: Extract>, uinode_query: Extract< - Query<( - &Node, - &GlobalTransform, - &Text, - &TextLayoutInfo, - &ComputedVisibility, - Option<&CalculatedClip>, - )>, + Query< + ( + &Node, + &GlobalTransform, + &Text, + &TextLayoutInfo, + &ComputedVisibility, + Option<&CalculatedClip>, + ), + With, + >, >, ) { // TODO: Support window-independent UI scale: https://github.com/bevyengine/bevy/issues/5621