Skip to content

Commit 9aef71b

Browse files
Replace Handle<M: UiMaterial> component with UiMaterialHandle wrapper (#15740)
# Objective - Closes #15720 ## Solution Wrap the handle in a new wrapper component: `UiMaterialHandle` It's not possible to match the naming convention of `MeshMaterial3d/2d` here with the trait already being called `UiMaterial` Should we consider renaming to `Material3d/2dHandle` and `Mesh3d/2d` to `Mesh3d/2dHandle`? - This shouldn't have any merge conflicts with #15591 ## Testing Tested the `ui_material` example ## Migration Guide Let's defer the migration guide to the required component port. I just want to yeet the `Component` impl on `Handle` in the meantime :)
1 parent aa626e4 commit 9aef71b

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

crates/bevy_ui/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub mod prelude {
5050
pub use {
5151
crate::{
5252
geometry::*, node_bundles::*, ui_material::*, ui_node::*, widget::Button,
53-
widget::Label, Interaction, UiMaterialPlugin, UiScale,
53+
widget::Label, Interaction, UiMaterialHandle, UiMaterialPlugin, UiScale,
5454
},
5555
// `bevy_sprite` re-exports for texture slicing
5656
bevy_sprite::{BorderRect, ImageScaleMode, SliceScaleMode, TextureSlicer},

crates/bevy_ui/src/node_bundles.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use crate::{
44
widget::{Button, UiImageSize},
55
BackgroundColor, BorderColor, BorderRadius, ContentSize, FocusPolicy, Interaction, Node,
6-
ScrollPosition, Style, UiImage, UiMaterial, ZIndex,
6+
ScrollPosition, Style, UiImage, UiMaterial, UiMaterialHandle, ZIndex,
77
};
8-
use bevy_asset::Handle;
98
use bevy_ecs::bundle::Bundle;
109
use bevy_render::view::{InheritedVisibility, ViewVisibility, Visibility};
1110
use bevy_transform::prelude::{GlobalTransform, Transform};
@@ -294,7 +293,7 @@ pub struct MaterialNodeBundle<M: UiMaterial> {
294293
/// In some cases these styles also affect how the node drawn/painted.
295294
pub style: Style,
296295
/// The [`UiMaterial`] used to render the node.
297-
pub material: Handle<M>,
296+
pub material: UiMaterialHandle<M>,
298297
/// Whether this node should block interaction with lower nodes
299298
pub focus_policy: FocusPolicy,
300299
/// The transform of the node

crates/bevy_ui/src/render/ui_material_pipeline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060
Shader::from_wgsl
6161
);
6262
app.init_asset::<M>().add_plugins((
63-
ExtractComponentPlugin::<Handle<M>>::extract_visible(),
63+
ExtractComponentPlugin::<UiMaterialHandle<M>>::extract_visible(),
6464
RenderAssetPlugin::<PreparedUiMaterial<M>>::default(),
6565
));
6666

@@ -364,7 +364,7 @@ pub fn extract_ui_material_nodes<M: UiMaterial>(
364364
(
365365
&Node,
366366
&GlobalTransform,
367-
&Handle<M>,
367+
&UiMaterialHandle<M>,
368368
&ViewVisibility,
369369
Option<&CalculatedClip>,
370370
Option<&TargetCamera>,

crates/bevy_ui/src/ui_material.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use core::hash::Hash;
22

3-
use bevy_asset::Asset;
4-
use bevy_render::render_resource::{AsBindGroup, RenderPipelineDescriptor, ShaderRef};
3+
use bevy_asset::{Asset, AssetId, Handle};
4+
use bevy_derive::{Deref, DerefMut};
5+
use bevy_ecs::{component::Component, reflect::ReflectComponent};
6+
use bevy_reflect::{prelude::ReflectDefault, Reflect};
7+
use bevy_render::{
8+
extract_component::ExtractComponent,
9+
render_resource::{AsBindGroup, RenderPipelineDescriptor, ShaderRef},
10+
};
511

612
/// Materials are used alongside [`UiMaterialPlugin`](crate::UiMaterialPlugin) and [`MaterialNodeBundle`](crate::prelude::MaterialNodeBundle)
713
/// to spawn entities that are rendered with a specific [`UiMaterial`] type. They serve as an easy to use high level
@@ -56,10 +62,10 @@ use bevy_render::render_resource::{AsBindGroup, RenderPipelineDescriptor, Shader
5662
/// width: Val::Percent(100.0),
5763
/// ..Default::default()
5864
/// },
59-
/// material: materials.add(CustomMaterial {
65+
/// material: UiMaterialHandle(materials.add(CustomMaterial {
6066
/// color: LinearRgba::RED,
6167
/// color_texture: asset_server.load("some_image.png"),
62-
/// }),
68+
/// })),
6369
/// ..Default::default()
6470
/// });
6571
/// }
@@ -145,3 +151,31 @@ where
145151
self.bind_group_data.hash(state);
146152
}
147153
}
154+
155+
#[derive(Component, Clone, Debug, Deref, DerefMut, Reflect, PartialEq, Eq, ExtractComponent)]
156+
#[reflect(Component, Default)]
157+
pub struct UiMaterialHandle<M: UiMaterial>(pub Handle<M>);
158+
159+
impl<M: UiMaterial> Default for UiMaterialHandle<M> {
160+
fn default() -> Self {
161+
Self(Handle::default())
162+
}
163+
}
164+
165+
impl<M: UiMaterial> From<Handle<M>> for UiMaterialHandle<M> {
166+
fn from(handle: Handle<M>) -> Self {
167+
Self(handle)
168+
}
169+
}
170+
171+
impl<M: UiMaterial> From<UiMaterialHandle<M>> for AssetId<M> {
172+
fn from(material: UiMaterialHandle<M>) -> Self {
173+
material.id()
174+
}
175+
}
176+
177+
impl<M: UiMaterial> From<&UiMaterialHandle<M>> for AssetId<M> {
178+
fn from(material: &UiMaterialHandle<M>) -> Self {
179+
material.id()
180+
}
181+
}

examples/ui/ui_material.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ fn setup(
4343
border: UiRect::all(Val::Px(10.)),
4444
..default()
4545
},
46-
material: ui_materials.add(CustomUiMaterial {
46+
material: UiMaterialHandle(ui_materials.add(CustomUiMaterial {
4747
color: LinearRgba::WHITE.to_f32_array().into(),
4848
slider: 0.5,
4949
color_texture: asset_server.load("branding/banner.png"),
5050
border_color: LinearRgba::WHITE.to_f32_array().into(),
51-
}),
51+
})),
5252
..default()
5353
});
5454
});
@@ -82,7 +82,7 @@ impl UiMaterial for CustomUiMaterial {
8282
// Also updates the color of the image to a rainbow color
8383
fn animate(
8484
mut materials: ResMut<Assets<CustomUiMaterial>>,
85-
q: Query<&Handle<CustomUiMaterial>>,
85+
q: Query<&UiMaterialHandle<CustomUiMaterial>>,
8686
time: Res<Time>,
8787
) {
8888
let duration = 2.0;

0 commit comments

Comments
 (0)