mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
336fddb101
# Objective In Bevy 0.13, `BackgroundColor` simply tinted the image of any `UiImage`. This was confusing: in every other case (e.g. Text), this added a solid square behind the element. #11165 changed this, but removed `BackgroundColor` from `ImageBundle` to avoid confusion, since the semantic meaning had changed. However, this resulted in a serious UX downgrade / inconsistency, as this behavior was no longer part of the bundle (unlike for `TextBundle` or `NodeBundle`), leaving users with a relatively frustrating upgrade path. Additionally, adding both `BackgroundColor` and `UiImage` resulted in a bizarre effect, where the background color was seemingly ignored as it was covered by a solid white placeholder image. Fixes #13969. ## Solution Per @viridia's design: > - if you don't specify a background color, it's transparent. > - if you don't specify an image color, it's white (because it's a multiplier). > - if you don't specify an image, no image is drawn. > - if you specify both a background color and an image color, they are independent. > - the background color is drawn behind the image (in whatever pixels are transparent) As laid out by @benfrankel, this involves: 1. Changing the default `UiImage` to use a transparent texture but a pure white tint. 2. Adding `UiImage::solid_color` to quickly set placeholder images. 3. Changing the default `BorderColor` and `BackgroundColor` to transparent. 4. Removing the default overrides for these values in the other assorted UI bundles. 5. Adding `BackgroundColor` back to `ImageBundle` and `ButtonBundle`. 6. Adding a 1x1 `Image::transparent`, which can be accessed from `Assets<Image>` via the `TRANSPARENT_IMAGE_HANDLE` constant. Huge thanks to everyone who helped out with the design in the linked issue and [the Discord thread](https://discord.com/channels/691052431525675048/1255209923890118697/1255209999278280844): this was very much a joint design. @cart helped me figure out how to set the UiImage's default texture to a transparent 1x1 image, which is a much nicer fix. ## Testing I've checked the examples modified by this PR, and the `ui` example as well just to be sure. ## Migration Guide - `BackgroundColor` no longer tints the color of images in `ImageBundle` or `ButtonBundle`. Set `UiImage::color` to tint images instead. - The default texture for `UiImage` is now a transparent white square. Use `UiImage::solid_color` to quickly draw debug images. - The default value for `BackgroundColor` and `BorderColor` is now transparent. Set the color to white manually to return to previous behavior.
86 lines
3 KiB
Rust
86 lines
3 KiB
Rust
//! This example illustrates how to use `TextureAtlases` within ui
|
|
|
|
use bevy::{color::palettes::css::*, prelude::*, winit::WinitSettings};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(
|
|
// This sets image filtering to nearest
|
|
// This is done to prevent textures with low resolution (e.g. pixel art) from being blurred
|
|
// by linear filtering.
|
|
ImagePlugin::default_nearest(),
|
|
))
|
|
// Only run the app when there is user input. This will significantly reduce CPU/GPU use.
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, increment_atlas_index)
|
|
.run();
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
asset_server: Res<AssetServer>,
|
|
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
|
|
) {
|
|
// Camera
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
let text_style = TextStyle::default();
|
|
|
|
let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
|
|
let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
|
|
let texture_atlas_handle = texture_atlases.add(texture_atlas);
|
|
|
|
// root node
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
flex_direction: FlexDirection::Column,
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
row_gap: Val::Px(text_style.font_size * 2.),
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
ImageBundle {
|
|
style: Style {
|
|
width: Val::Px(256.),
|
|
height: Val::Px(256.),
|
|
..default()
|
|
},
|
|
image: UiImage::new(texture_handle),
|
|
background_color: BackgroundColor(ANTIQUE_WHITE.into()),
|
|
..default()
|
|
},
|
|
TextureAtlas::from(texture_atlas_handle),
|
|
Outline::new(Val::Px(8.0), Val::ZERO, CRIMSON.into()),
|
|
));
|
|
parent.spawn(TextBundle::from_sections([
|
|
TextSection::new("press ".to_string(), text_style.clone()),
|
|
TextSection::new(
|
|
"space".to_string(),
|
|
TextStyle {
|
|
color: YELLOW.into(),
|
|
..text_style.clone()
|
|
},
|
|
),
|
|
TextSection::new(" to advance frames".to_string(), text_style),
|
|
]));
|
|
});
|
|
}
|
|
|
|
fn increment_atlas_index(
|
|
mut atlas_images: Query<&mut TextureAtlas>,
|
|
keyboard: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
if keyboard.just_pressed(KeyCode::Space) {
|
|
for mut atlas_image in &mut atlas_images {
|
|
atlas_image.index = (atlas_image.index + 1) % 6;
|
|
}
|
|
}
|
|
}
|