2023-11-03 22:33:01 +00:00
|
|
|
//! Demonstrates the use of [`UiMaterials`](UiMaterial) and how to change material values
|
|
|
|
|
2024-10-20 01:08:17 +00:00
|
|
|
use bevy::{prelude::*, reflect::TypePath, render::render_resource::*};
|
2023-11-03 22:33:01 +00:00
|
|
|
|
2024-06-12 14:16:01 +00:00
|
|
|
/// This example uses a shader source file from the assets subdirectory
|
2024-06-27 21:23:04 +00:00
|
|
|
const SHADER_ASSET_PATH: &str = "shaders/custom_ui_material.wgsl";
|
2024-06-12 14:16:01 +00:00
|
|
|
|
2023-11-03 22:33:01 +00:00
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_plugins(UiMaterialPlugin::<CustomUiMaterial>::default())
|
|
|
|
.add_systems(Startup, setup)
|
2024-06-27 21:23:04 +00:00
|
|
|
.add_systems(Update, animate)
|
2023-11-03 22:33:01 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2024-06-27 21:23:04 +00:00
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut ui_materials: ResMut<Assets<CustomUiMaterial>>,
|
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
) {
|
2023-11-03 22:33:01 +00:00
|
|
|
// Camera so we can see UI
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2023-11-03 22:33:01 +00:00
|
|
|
|
|
|
|
commands
|
Merge Style properties into Node. Use ComputedNode for computed properties. (#15975)
# Objective
Continue improving the user experience of our UI Node API in the
direction specified by [Bevy's Next Generation Scene / UI
System](https://github.com/bevyengine/bevy/discussions/14437)
## Solution
As specified in the document above, merge `Style` fields into `Node`,
and move "computed Node fields" into `ComputedNode` (I chose this name
over something like `ComputedNodeLayout` because it currently contains
more than just layout info. If we want to break this up / rename these
concepts, lets do that in a separate PR). `Style` has been removed.
This accomplishes a number of goals:
## Ergonomics wins
Specifying both `Node` and `Style` is now no longer required for
non-default styles
Before:
```rust
commands.spawn((
Node::default(),
Style {
width: Val::Px(100.),
..default()
},
));
```
After:
```rust
commands.spawn(Node {
width: Val::Px(100.),
..default()
});
```
## Conceptual clarity
`Style` was never a comprehensive "style sheet". It only defined "core"
style properties that all `Nodes` shared. Any "styled property" that
couldn't fit that mold had to be in a separate component. A "real" style
system would style properties _across_ components (`Node`, `Button`,
etc). We have plans to build a true style system (see the doc linked
above).
By moving the `Style` fields to `Node`, we fully embrace `Node` as the
driving concept and remove the "style system" confusion.
## Next Steps
* Consider identifying and splitting out "style properties that aren't
core to Node". This should not happen for Bevy 0.15.
---
## Migration Guide
Move any fields set on `Style` into `Node` and replace all `Style`
component usage with `Node`.
Before:
```rust
commands.spawn((
Node::default(),
Style {
width: Val::Px(100.),
..default()
},
));
```
After:
```rust
commands.spawn(Node {
width: Val::Px(100.),
..default()
});
```
For any usage of the "computed node properties" that used to live on
`Node`, use `ComputedNode` instead:
Before:
```rust
fn system(nodes: Query<&Node>) {
for node in &nodes {
let computed_size = node.size();
}
}
```
After:
```rust
fn system(computed_nodes: Query<&ComputedNode>) {
for computed_node in &computed_nodes {
let computed_size = computed_node.size();
}
}
```
2024-10-18 22:25:33 +00:00
|
|
|
.spawn(Node {
|
|
|
|
width: Val::Percent(100.0),
|
|
|
|
height: Val::Percent(100.0),
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
..default()
|
|
|
|
})
|
2023-11-03 22:33:01 +00:00
|
|
|
.with_children(|parent| {
|
2024-06-27 21:23:04 +00:00
|
|
|
let banner_scale_factor = 0.5;
|
Migrate UI bundles to required components (#15898)
# Objective
- Migrate UI bundles to required components, fixes #15889
## Solution
- deprecate `NodeBundle` in favor of `Node`
- deprecate `ImageBundle` in favor of `UiImage`
- deprecate `ButtonBundle` in favor of `Button`
## Testing
CI.
## Migration Guide
- Replace all uses of `NodeBundle` with `Node`. e.g.
```diff
commands
- .spawn(NodeBundle {
- style: Style {
+ .spawn((
+ Node::default(),
+ Style {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
- ..default()
- })
+ ))
```
- Replace all uses of `ButtonBundle` with `Button`. e.g.
```diff
.spawn((
- ButtonBundle {
- style: Style {
- width: Val::Px(w),
- height: Val::Px(h),
- // horizontally center child text
- justify_content: JustifyContent::Center,
- // vertically center child text
- align_items: AlignItems::Center,
- margin: UiRect::all(Val::Px(20.0)),
- ..default()
- },
- image: image.clone().into(),
+ Button,
+ Style {
+ width: Val::Px(w),
+ height: Val::Px(h),
+ // horizontally center child text
+ justify_content: JustifyContent::Center,
+ // vertically center child text
+ align_items: AlignItems::Center,
+ margin: UiRect::all(Val::Px(20.0)),
..default()
},
+ UiImage::from(image.clone()),
ImageScaleMode::Sliced(slicer.clone()),
))
```
- Replace all uses of `ImageBundle` with `UiImage`. e.g.
```diff
- commands.spawn(ImageBundle {
- image: UiImage {
+ commands.spawn((
+ UiImage {
texture: metering_mask,
..default()
},
- style: Style {
+ Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
- ..default()
- });
+ ));
```
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-17 21:11:02 +00:00
|
|
|
parent.spawn((
|
|
|
|
MaterialNode(ui_materials.add(CustomUiMaterial {
|
|
|
|
color: LinearRgba::WHITE.to_f32_array().into(),
|
|
|
|
slider: 0.5,
|
|
|
|
color_texture: asset_server.load("branding/banner.png"),
|
|
|
|
border_color: LinearRgba::WHITE.to_f32_array().into(),
|
|
|
|
})),
|
Merge Style properties into Node. Use ComputedNode for computed properties. (#15975)
# Objective
Continue improving the user experience of our UI Node API in the
direction specified by [Bevy's Next Generation Scene / UI
System](https://github.com/bevyengine/bevy/discussions/14437)
## Solution
As specified in the document above, merge `Style` fields into `Node`,
and move "computed Node fields" into `ComputedNode` (I chose this name
over something like `ComputedNodeLayout` because it currently contains
more than just layout info. If we want to break this up / rename these
concepts, lets do that in a separate PR). `Style` has been removed.
This accomplishes a number of goals:
## Ergonomics wins
Specifying both `Node` and `Style` is now no longer required for
non-default styles
Before:
```rust
commands.spawn((
Node::default(),
Style {
width: Val::Px(100.),
..default()
},
));
```
After:
```rust
commands.spawn(Node {
width: Val::Px(100.),
..default()
});
```
## Conceptual clarity
`Style` was never a comprehensive "style sheet". It only defined "core"
style properties that all `Nodes` shared. Any "styled property" that
couldn't fit that mold had to be in a separate component. A "real" style
system would style properties _across_ components (`Node`, `Button`,
etc). We have plans to build a true style system (see the doc linked
above).
By moving the `Style` fields to `Node`, we fully embrace `Node` as the
driving concept and remove the "style system" confusion.
## Next Steps
* Consider identifying and splitting out "style properties that aren't
core to Node". This should not happen for Bevy 0.15.
---
## Migration Guide
Move any fields set on `Style` into `Node` and replace all `Style`
component usage with `Node`.
Before:
```rust
commands.spawn((
Node::default(),
Style {
width: Val::Px(100.),
..default()
},
));
```
After:
```rust
commands.spawn(Node {
width: Val::Px(100.),
..default()
});
```
For any usage of the "computed node properties" that used to live on
`Node`, use `ComputedNode` instead:
Before:
```rust
fn system(nodes: Query<&Node>) {
for node in &nodes {
let computed_size = node.size();
}
}
```
After:
```rust
fn system(computed_nodes: Query<&ComputedNode>) {
for computed_node in &computed_nodes {
let computed_size = computed_node.size();
}
}
```
2024-10-18 22:25:33 +00:00
|
|
|
Node {
|
2023-11-03 22:33:01 +00:00
|
|
|
position_type: PositionType::Absolute,
|
2024-06-27 21:23:04 +00:00
|
|
|
width: Val::Px(905.0 * banner_scale_factor),
|
|
|
|
height: Val::Px(363.0 * banner_scale_factor),
|
2024-09-09 16:34:24 +00:00
|
|
|
border: UiRect::all(Val::Px(10.)),
|
2023-11-03 22:33:01 +00:00
|
|
|
..default()
|
|
|
|
},
|
Migrate UI bundles to required components (#15898)
# Objective
- Migrate UI bundles to required components, fixes #15889
## Solution
- deprecate `NodeBundle` in favor of `Node`
- deprecate `ImageBundle` in favor of `UiImage`
- deprecate `ButtonBundle` in favor of `Button`
## Testing
CI.
## Migration Guide
- Replace all uses of `NodeBundle` with `Node`. e.g.
```diff
commands
- .spawn(NodeBundle {
- style: Style {
+ .spawn((
+ Node::default(),
+ Style {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
- ..default()
- })
+ ))
```
- Replace all uses of `ButtonBundle` with `Button`. e.g.
```diff
.spawn((
- ButtonBundle {
- style: Style {
- width: Val::Px(w),
- height: Val::Px(h),
- // horizontally center child text
- justify_content: JustifyContent::Center,
- // vertically center child text
- align_items: AlignItems::Center,
- margin: UiRect::all(Val::Px(20.0)),
- ..default()
- },
- image: image.clone().into(),
+ Button,
+ Style {
+ width: Val::Px(w),
+ height: Val::Px(h),
+ // horizontally center child text
+ justify_content: JustifyContent::Center,
+ // vertically center child text
+ align_items: AlignItems::Center,
+ margin: UiRect::all(Val::Px(20.0)),
..default()
},
+ UiImage::from(image.clone()),
ImageScaleMode::Sliced(slicer.clone()),
))
```
- Replace all uses of `ImageBundle` with `UiImage`. e.g.
```diff
- commands.spawn(ImageBundle {
- image: UiImage {
+ commands.spawn((
+ UiImage {
texture: metering_mask,
..default()
},
- style: Style {
+ Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
- ..default()
- });
+ ));
```
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-17 21:11:02 +00:00
|
|
|
));
|
2023-11-03 22:33:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(AsBindGroup, Asset, TypePath, Debug, Clone)]
|
|
|
|
struct CustomUiMaterial {
|
2024-06-27 21:23:04 +00:00
|
|
|
/// Color multiplied with the image
|
2023-11-03 22:33:01 +00:00
|
|
|
#[uniform(0)]
|
|
|
|
color: Vec4,
|
2024-06-27 21:23:04 +00:00
|
|
|
/// Represents how much of the image is visible
|
|
|
|
/// Goes from 0 to 1
|
|
|
|
#[uniform(1)]
|
|
|
|
slider: f32,
|
|
|
|
/// Image used to represent the slider
|
|
|
|
#[texture(2)]
|
|
|
|
#[sampler(3)]
|
|
|
|
color_texture: Handle<Image>,
|
2024-09-09 16:34:24 +00:00
|
|
|
/// Color of the image's border
|
|
|
|
#[uniform(4)]
|
|
|
|
border_color: Vec4,
|
2023-11-03 22:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UiMaterial for CustomUiMaterial {
|
|
|
|
fn fragment_shader() -> ShaderRef {
|
2024-06-12 14:16:01 +00:00
|
|
|
SHADER_ASSET_PATH.into()
|
2023-11-03 22:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 21:23:04 +00:00
|
|
|
|
|
|
|
// Fills the slider slowly over 2 seconds and resets it
|
|
|
|
// Also updates the color of the image to a rainbow color
|
|
|
|
fn animate(
|
|
|
|
mut materials: ResMut<Assets<CustomUiMaterial>>,
|
Migrate UI bundles to required components (#15898)
# Objective
- Migrate UI bundles to required components, fixes #15889
## Solution
- deprecate `NodeBundle` in favor of `Node`
- deprecate `ImageBundle` in favor of `UiImage`
- deprecate `ButtonBundle` in favor of `Button`
## Testing
CI.
## Migration Guide
- Replace all uses of `NodeBundle` with `Node`. e.g.
```diff
commands
- .spawn(NodeBundle {
- style: Style {
+ .spawn((
+ Node::default(),
+ Style {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
- ..default()
- })
+ ))
```
- Replace all uses of `ButtonBundle` with `Button`. e.g.
```diff
.spawn((
- ButtonBundle {
- style: Style {
- width: Val::Px(w),
- height: Val::Px(h),
- // horizontally center child text
- justify_content: JustifyContent::Center,
- // vertically center child text
- align_items: AlignItems::Center,
- margin: UiRect::all(Val::Px(20.0)),
- ..default()
- },
- image: image.clone().into(),
+ Button,
+ Style {
+ width: Val::Px(w),
+ height: Val::Px(h),
+ // horizontally center child text
+ justify_content: JustifyContent::Center,
+ // vertically center child text
+ align_items: AlignItems::Center,
+ margin: UiRect::all(Val::Px(20.0)),
..default()
},
+ UiImage::from(image.clone()),
ImageScaleMode::Sliced(slicer.clone()),
))
```
- Replace all uses of `ImageBundle` with `UiImage`. e.g.
```diff
- commands.spawn(ImageBundle {
- image: UiImage {
+ commands.spawn((
+ UiImage {
texture: metering_mask,
..default()
},
- style: Style {
+ Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
- ..default()
- });
+ ));
```
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-17 21:11:02 +00:00
|
|
|
q: Query<&MaterialNode<CustomUiMaterial>>,
|
2024-06-27 21:23:04 +00:00
|
|
|
time: Res<Time>,
|
|
|
|
) {
|
|
|
|
let duration = 2.0;
|
|
|
|
for handle in &q {
|
|
|
|
if let Some(material) = materials.get_mut(handle) {
|
|
|
|
// rainbow color effect
|
2024-10-16 21:09:32 +00:00
|
|
|
let new_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 1., 0.5);
|
|
|
|
let border_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 0.75, 0.75);
|
2024-09-09 16:34:24 +00:00
|
|
|
material.color = new_color.to_linear().to_vec4();
|
2024-06-27 21:23:04 +00:00
|
|
|
material.slider =
|
2024-10-16 21:09:32 +00:00
|
|
|
((time.elapsed_secs() % (duration * 2.0)) - duration).abs() / duration;
|
2024-09-09 16:34:24 +00:00
|
|
|
material.border_color = border_color.to_linear().to_vec4();
|
2024-06-27 21:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|