Skip to content

Commit 66f495c

Browse files
author
Brian Merchant
committed
Cleaning up NodeBundle, and some slight UI module re-organization (bevyengine#6473)
# Objective `NodeBundle` contains an `image` field, which can be misleading, because if you do supply an image there, nothing will be shown to screen. You need to use an `ImageBundle` instead. ## Solution * `image` (`UiImage`) field is removed from `NodeBundle`, * extraction stage queries now make an optional query for `UiImage`, if one is not found, use the image handle that is used as a default by `UiImage`: https://github.com/bevyengine/bevy/blob/c019a60b39c5683656025bc9d24a02744aa59dea/crates/bevy_ui/src/ui_node.rs#L464 * touching up docs for `NodeBundle` to help guide what `NodeBundle` should be used for * renamed `entity.rs` to `node_bundle.rs` as that gives more of a hint regarding the module's purpose * separating `camera_config` stuff from the pre-made UI node bundles so that `node_bundle.rs` makes more sense as a module name.
1 parent 5ae9475 commit 66f495c

File tree

5 files changed

+62
-47
lines changed

5 files changed

+62
-47
lines changed

crates/bevy_ui/src/camera_config.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Configuration for cameras related to UI.
2+
3+
use bevy_ecs::component::Component;
4+
use bevy_ecs::prelude::With;
5+
use bevy_ecs::query::QueryItem;
6+
use bevy_render::camera::Camera;
7+
use bevy_render::extract_component::ExtractComponent;
8+
9+
/// Configuration for cameras related to UI.
10+
///
11+
/// When a [`Camera`] doesn't have the [`UiCameraConfig`] component,
12+
/// it will display the UI by default.
13+
///
14+
/// [`Camera`]: bevy_render::camera::Camera
15+
#[derive(Component, Clone)]
16+
pub struct UiCameraConfig {
17+
/// Whether to output UI to this camera view.
18+
///
19+
/// When a `Camera` doesn't have the [`UiCameraConfig`] component,
20+
/// it will display the UI by default.
21+
pub show_ui: bool,
22+
}
23+
24+
impl Default for UiCameraConfig {
25+
fn default() -> Self {
26+
Self { show_ui: true }
27+
}
28+
}
29+
30+
impl ExtractComponent for UiCameraConfig {
31+
type Query = &'static Self;
32+
type Filter = With<Camera>;
33+
34+
fn extract_component(item: QueryItem<'_, Self::Query>) -> Self {
35+
item.clone()
36+
}
37+
}

crates/bevy_ui/src/focus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{entity::UiCameraConfig, CalculatedClip, Node, UiStack};
1+
use crate::{camera_config::UiCameraConfig, CalculatedClip, Node, UiStack};
22
use bevy_ecs::{
33
entity::Entity,
44
prelude::Component,

crates/bevy_ui/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This crate contains Bevy's UI system, which can be used to create UI for both 2D and 3D games
22
//! # Basic usage
3-
//! Spawn UI elements with [`entity::ButtonBundle`], [`entity::ImageBundle`], [`entity::TextBundle`] and [`entity::NodeBundle`]
3+
//! Spawn UI elements with [`node_bundles::ButtonBundle`], [`node_bundles::ImageBundle`], [`node_bundles::TextBundle`] and [`node_bundles::NodeBundle`]
44
//! This UI is laid out with the Flexbox paradigm (see <https://cssreference.io/flexbox/>)
55
mod flex;
66
mod focus;
@@ -9,7 +9,8 @@ mod render;
99
mod stack;
1010
mod ui_node;
1111

12-
pub mod entity;
12+
pub mod camera_config;
13+
pub mod node_bundles;
1314
pub mod update;
1415
pub mod widget;
1516

@@ -23,7 +24,10 @@ pub use ui_node::*;
2324
#[doc(hidden)]
2425
pub mod prelude {
2526
#[doc(hidden)]
26-
pub use crate::{entity::*, geometry::*, ui_node::*, widget::Button, Interaction, UiScale};
27+
pub use crate::{
28+
camera_config::*, geometry::*, node_bundles::*, ui_node::*, widget::Button, Interaction,
29+
UiScale,
30+
};
2731
}
2832

2933
use bevy_app::prelude::*;

crates/bevy_ui/src/entity.rs renamed to crates/bevy_ui/src/node_bundles.rs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
//! This module contains the bundles used in Bevy's UI
1+
//! This module contains basic node bundles used to build UIs
22
33
use crate::{
44
widget::{Button, ImageMode},
55
BackgroundColor, CalculatedSize, FocusPolicy, Interaction, Node, Style, UiImage, ZIndex,
66
};
7-
use bevy_ecs::{
8-
bundle::Bundle,
9-
prelude::{Component, With},
10-
query::QueryItem,
11-
};
7+
use bevy_ecs::bundle::Bundle;
128
use bevy_render::{
13-
camera::Camera,
14-
extract_component::ExtractComponent,
159
prelude::{Color, ComputedVisibility},
1610
view::Visibility,
1711
};
1812
use bevy_text::{Text, TextAlignment, TextSection, TextStyle};
1913
use bevy_transform::prelude::{GlobalTransform, Transform};
2014

2115
/// The basic UI node
16+
///
17+
/// Useful as a container for a variety of child nodes.
2218
#[derive(Bundle, Clone, Debug)]
2319
pub struct NodeBundle {
2420
/// Describes the size of the node
@@ -27,8 +23,6 @@ pub struct NodeBundle {
2723
pub style: Style,
2824
/// The background color, which serves as a "fill" for this node
2925
pub background_color: BackgroundColor,
30-
/// Describes the image of the node
31-
pub image: UiImage,
3226
/// Whether this node should block interaction with lower nodes
3327
pub focus_policy: FocusPolicy,
3428
/// The transform of the node
@@ -56,7 +50,6 @@ impl Default for NodeBundle {
5650
background_color: Color::NONE.into(),
5751
node: Default::default(),
5852
style: Default::default(),
59-
image: Default::default(),
6053
focus_policy: Default::default(),
6154
transform: Default::default(),
6255
global_transform: Default::default(),
@@ -241,32 +234,3 @@ impl Default for ButtonBundle {
241234
}
242235
}
243236
}
244-
/// Configuration for cameras related to UI.
245-
///
246-
/// When a [`Camera`] doesn't have the [`UiCameraConfig`] component,
247-
/// it will display the UI by default.
248-
///
249-
/// [`Camera`]: bevy_render::camera::Camera
250-
#[derive(Component, Clone)]
251-
pub struct UiCameraConfig {
252-
/// Whether to output UI to this camera view.
253-
///
254-
/// When a `Camera` doesn't have the [`UiCameraConfig`] component,
255-
/// it will display the UI by default.
256-
pub show_ui: bool,
257-
}
258-
259-
impl Default for UiCameraConfig {
260-
fn default() -> Self {
261-
Self { show_ui: true }
262-
}
263-
}
264-
265-
impl ExtractComponent for UiCameraConfig {
266-
type Query = &'static Self;
267-
type Filter = With<Camera>;
268-
269-
fn extract_component(item: QueryItem<'_, Self::Query>) -> Self {
270-
item.clone()
271-
}
272-
}

crates/bevy_ui/src/render/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use bevy_asset::{load_internal_asset, AssetEvent, Assets, Handle, HandleUntyped}
1111
use bevy_ecs::prelude::*;
1212
use bevy_math::{Mat4, Rect, UVec4, Vec2, Vec3, Vec4Swizzles};
1313
use bevy_reflect::TypeUuid;
14+
use bevy_render::texture::DEFAULT_IMAGE_HANDLE;
1415
use bevy_render::{
1516
camera::Camera,
1617
color::Color,
@@ -209,7 +210,7 @@ pub fn extract_uinodes(
209210
&Node,
210211
&GlobalTransform,
211212
&BackgroundColor,
212-
&UiImage,
213+
Option<&UiImage>,
213214
&ComputedVisibility,
214215
Option<&CalculatedClip>,
215216
)>,
@@ -218,11 +219,19 @@ pub fn extract_uinodes(
218219
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
219220
extracted_uinodes.uinodes.clear();
220221
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
221-
if let Ok((uinode, transform, color, image, visibility, clip)) = uinode_query.get(*entity) {
222+
if let Ok((uinode, transform, color, maybe_image, visibility, clip)) =
223+
uinode_query.get(*entity)
224+
{
222225
if !visibility.is_visible() {
223226
continue;
224227
}
225-
let image = image.0.clone_weak();
228+
229+
let image = if let Some(image) = maybe_image {
230+
image.0.clone_weak()
231+
} else {
232+
DEFAULT_IMAGE_HANDLE.typed().clone_weak()
233+
};
234+
226235
// Skip loading images
227236
if !images.contains(&image) {
228237
continue;
@@ -231,6 +240,7 @@ pub fn extract_uinodes(
231240
if color.0.a() == 0.0 {
232241
continue;
233242
}
243+
234244
extracted_uinodes.uinodes.push(ExtractedUiNode {
235245
stack_index,
236246
transform: transform.compute_matrix(),

0 commit comments

Comments
 (0)