# Objective - As part of the migration process we need to a) see the end effect of the migration on user ergonomics b) check for serious perf regressions c) actually migrate the code - To accomplish this, I'm going to attempt to migrate all of the remaining user-facing usages of `LegacyColor` in one PR, being careful to keep a clean commit history. - Fixes #12056. ## Solution I've chosen to use the polymorphic `Color` type as our standard user-facing API. - [x] Migrate `bevy_gizmos`. - [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs - [x] Migrate sprites - [x] Migrate UI - [x] Migrate `ColorMaterial` - [x] Migrate `MaterialMesh2D` - [x] Migrate fog - [x] Migrate lights - [x] Migrate StandardMaterial - [x] Migrate wireframes - [x] Migrate clear color - [x] Migrate text - [x] Migrate gltf loader - [x] Register color types for reflection - [x] Remove `LegacyColor` - [x] Make sure CI passes Incidental improvements to ease migration: - added `Color::srgba_u8`, `Color::srgba_from_array` and friends - added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the `Alpha` trait - add and immediately deprecate (lol) `Color::rgb` and friends in favor of more explicit and consistent `Color::srgb` - standardized on white and black for most example text colors - added vector field traits to `LinearRgba`: ~~`Add`, `Sub`, `AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications and divisions do not scale alpha. `Add` and `Sub` have been cut from this PR. - added `LinearRgba` and `Srgba` `RED/GREEN/BLUE` - added `LinearRgba_to_f32_array` and `LinearRgba::to_u32` ## Migration Guide Bevy's color types have changed! Wherever you used a `bevy::render::Color`, a `bevy::color::Color` is used instead. These are quite similar! Both are enums storing a color in a specific color space (or to be more precise, using a specific color model). However, each of the different color models now has its own type. TODO... - `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`, `Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`, `Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`. - `Color::set_a` and `Color::a` is now `Color::set_alpha` and `Color::alpha`. These are part of the `Alpha` trait in `bevy_color`. - `Color::is_fully_transparent` is now part of the `Alpha` trait in `bevy_color` - `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for `g`, `b` `h`, `s` and `l` have been removed due to causing silent relatively expensive conversions. Convert your `Color` into the desired color space, perform your operations there, and then convert it back into a polymorphic `Color` enum. - `Color::hex` is now `Srgba::hex`. Call `.into` or construct a `Color::Srgba` variant manually to convert it. - `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`, `ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now store a `LinearRgba`, rather than a polymorphic `Color` - `Color::rgb_linear` and `Color::rgba_linear` are now `Color::linear_rgb` and `Color::linear_rgba` - The various CSS color constants are no longer stored directly on `Color`. Instead, they're defined in the `Srgba` color space, and accessed via `bevy::color::palettes::css`. Call `.into()` on them to convert them into a `Color` for quick debugging use, and consider using the much prettier `tailwind` palette for prototyping. - The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with the standard naming. - Vector field arithmetic operations on `Color` (add, subtract, multiply and divide by a f32) have been removed. Instead, convert your colors into `LinearRgba` space, and perform your operations explicitly there. This is particularly relevant when working with emissive or HDR colors, whose color channel values are routinely outside of the ordinary 0 to 1 range. - `Color::as_linear_rgba_f32` has been removed. Call `LinearRgba::to_f32_array` instead, converting if needed. - `Color::as_linear_rgba_u32` has been removed. Call `LinearRgba::to_u32` instead, converting if needed. - Several other color conversion methods to transform LCH or HSL colors into float arrays or `Vec` types have been removed. Please reimplement these externally or open a PR to re-add them if you found them particularly useful. - Various methods on `Color` such as `rgb` or `hsl` to convert the color into a specific color space have been removed. Convert into `LinearRgba`, then to the color space of your choice. - Various implicitly-converting color value methods on `Color` such as `r`, `g`, `b` or `h` have been removed. Please convert it into the color space of your choice, then check these properties. - `Color` no longer implements `AsBindGroup`. Store a `LinearRgba` internally instead to avoid conversion costs. --------- Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com> Co-authored-by: Afonso Lage <lage.afonso@gmail.com> Co-authored-by: Rob Parrett <robparrett@gmail.com> Co-authored-by: Zachary Harrold <zac@harrold.com.au>
409 lines
16 KiB
Rust
409 lines
16 KiB
Rust
//! This module contains basic node bundles used to build UIs
|
|
|
|
#[cfg(feature = "bevy_text")]
|
|
use crate::widget::TextFlags;
|
|
use crate::{
|
|
widget::{Button, UiImageSize},
|
|
BackgroundColor, BorderColor, ContentSize, FocusPolicy, Interaction, Node, Style, UiImage,
|
|
UiMaterial, ZIndex,
|
|
};
|
|
use bevy_asset::Handle;
|
|
use bevy_color::Color;
|
|
use bevy_ecs::bundle::Bundle;
|
|
use bevy_render::view::{InheritedVisibility, ViewVisibility, Visibility};
|
|
use bevy_sprite::TextureAtlas;
|
|
#[cfg(feature = "bevy_text")]
|
|
use bevy_text::{BreakLineOn, JustifyText, Text, TextLayoutInfo, TextSection, TextStyle};
|
|
use bevy_transform::prelude::{GlobalTransform, Transform};
|
|
|
|
/// The basic UI node.
|
|
///
|
|
/// Contains the [`Node`] component and other components required to make a container.
|
|
///
|
|
/// See [`node_bundles`](crate::node_bundles) for more specialized bundles like [`TextBundle`].
|
|
#[derive(Bundle, Clone, Debug)]
|
|
pub struct NodeBundle {
|
|
/// Describes the logical size of the node
|
|
pub node: Node,
|
|
/// Styles which control the layout (size and position) of the node and it's children
|
|
/// In some cases these styles also affect how the node drawn/painted.
|
|
pub style: Style,
|
|
/// The background color, which serves as a "fill" for this node
|
|
pub background_color: BackgroundColor,
|
|
/// The color of the Node's border
|
|
pub border_color: BorderColor,
|
|
/// Whether this node should block interaction with lower nodes
|
|
pub focus_policy: FocusPolicy,
|
|
/// The transform of the node
|
|
///
|
|
/// This component is automatically managed by the UI layout system.
|
|
/// To alter the position of the `NodeBundle`, use the properties of the [`Style`] component.
|
|
pub transform: Transform,
|
|
/// The global transform of the node
|
|
///
|
|
/// This component is automatically updated by the [`TransformPropagate`](`bevy_transform::TransformSystem::TransformPropagate`) systems.
|
|
/// To alter the position of the `NodeBundle`, use the properties of the [`Style`] component.
|
|
pub global_transform: GlobalTransform,
|
|
/// Describes the visibility properties of the node
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Indicates the depth at which the node should appear in the UI
|
|
pub z_index: ZIndex,
|
|
}
|
|
|
|
impl Default for NodeBundle {
|
|
fn default() -> Self {
|
|
NodeBundle {
|
|
// Transparent background
|
|
background_color: Color::NONE.into(),
|
|
border_color: Color::NONE.into(),
|
|
node: Default::default(),
|
|
style: Default::default(),
|
|
focus_policy: Default::default(),
|
|
transform: Default::default(),
|
|
global_transform: Default::default(),
|
|
visibility: Default::default(),
|
|
inherited_visibility: Default::default(),
|
|
view_visibility: Default::default(),
|
|
z_index: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A UI node that is an image
|
|
///
|
|
/// # Extra behaviours
|
|
///
|
|
/// You may add the following components to enable additional behaviours
|
|
/// - [`ImageScaleMode`](bevy_sprite::ImageScaleMode) to enable either slicing or tiling of the texture
|
|
#[derive(Bundle, Debug, Default)]
|
|
pub struct ImageBundle {
|
|
/// Describes the logical size of the node
|
|
pub node: Node,
|
|
/// Styles which control the layout (size and position) of the node and it's children
|
|
/// In some cases these styles also affect how the node drawn/painted.
|
|
pub style: Style,
|
|
/// The calculated size based on the given image
|
|
pub calculated_size: ContentSize,
|
|
/// The background color, which serves as a "fill" for this node
|
|
///
|
|
/// Combines with `UiImage` to tint the provided image.
|
|
pub background_color: BackgroundColor,
|
|
/// The image of the node
|
|
pub image: UiImage,
|
|
/// The size of the image in pixels
|
|
///
|
|
/// This component is set automatically
|
|
pub image_size: UiImageSize,
|
|
/// Whether this node should block interaction with lower nodes
|
|
pub focus_policy: FocusPolicy,
|
|
/// The transform of the node
|
|
///
|
|
/// This component is automatically managed by the UI layout system.
|
|
/// To alter the position of the `ImageBundle`, use the properties of the [`Style`] component.
|
|
pub transform: Transform,
|
|
/// The global transform of the node
|
|
///
|
|
/// This component is automatically updated by the [`TransformPropagate`](`bevy_transform::TransformSystem::TransformPropagate`) systems.
|
|
pub global_transform: GlobalTransform,
|
|
/// Describes the visibility properties of the node
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Indicates the depth at which the node should appear in the UI
|
|
pub z_index: ZIndex,
|
|
}
|
|
|
|
/// A UI node that is a texture atlas sprite
|
|
///
|
|
/// This bundle is identical to [`ImageBundle`] with an additional [`TextureAtlas`] component.
|
|
#[derive(Bundle, Debug, Default)]
|
|
pub struct AtlasImageBundle {
|
|
/// Describes the logical size of the node
|
|
pub node: Node,
|
|
/// Styles which control the layout (size and position) of the node and it's children
|
|
/// In some cases these styles also affect how the node drawn/painted.
|
|
pub style: Style,
|
|
/// The calculated size based on the given image
|
|
pub calculated_size: ContentSize,
|
|
/// The background color, which serves as a "fill" for this node
|
|
///
|
|
/// Combines with `UiImage` to tint the provided image.
|
|
pub background_color: BackgroundColor,
|
|
/// The image of the node
|
|
pub image: UiImage,
|
|
/// A handle to the texture atlas to use for this Ui Node
|
|
pub texture_atlas: TextureAtlas,
|
|
/// Whether this node should block interaction with lower nodes
|
|
pub focus_policy: FocusPolicy,
|
|
/// The size of the image in pixels
|
|
///
|
|
/// This component is set automatically
|
|
pub image_size: UiImageSize,
|
|
/// The transform of the node
|
|
///
|
|
/// This component is automatically managed by the UI layout system.
|
|
/// To alter the position of the `AtlasImageBundle`, use the properties of the [`Style`] component.
|
|
pub transform: Transform,
|
|
/// The global transform of the node
|
|
///
|
|
/// This component is automatically updated by the [`TransformPropagate`](`bevy_transform::TransformSystem::TransformPropagate`) systems.
|
|
pub global_transform: GlobalTransform,
|
|
/// Describes the visibility properties of the node
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Indicates the depth at which the node should appear in the UI
|
|
pub z_index: ZIndex,
|
|
}
|
|
|
|
#[cfg(feature = "bevy_text")]
|
|
/// A UI node that is text
|
|
///
|
|
/// The positioning of this node is controlled by the UI layout system. If you need manual control,
|
|
/// use [`Text2dBundle`](bevy_text::Text2dBundle).
|
|
#[derive(Bundle, Debug)]
|
|
pub struct TextBundle {
|
|
/// Describes the logical size of the node
|
|
pub node: Node,
|
|
/// Styles which control the layout (size and position) of the node and it's children
|
|
/// In some cases these styles also affect how the node drawn/painted.
|
|
pub style: Style,
|
|
/// Contains the text of the node
|
|
pub text: Text,
|
|
/// Text layout information
|
|
pub text_layout_info: TextLayoutInfo,
|
|
/// Text system flags
|
|
pub text_flags: TextFlags,
|
|
/// The calculated size based on the given image
|
|
pub calculated_size: ContentSize,
|
|
/// Whether this node should block interaction with lower nodes
|
|
pub focus_policy: FocusPolicy,
|
|
/// The transform of the node
|
|
///
|
|
/// This component is automatically managed by the UI layout system.
|
|
/// To alter the position of the `TextBundle`, use the properties of the [`Style`] component.
|
|
pub transform: Transform,
|
|
/// The global transform of the node
|
|
///
|
|
/// This component is automatically updated by the [`TransformPropagate`](`bevy_transform::TransformSystem::TransformPropagate`) systems.
|
|
pub global_transform: GlobalTransform,
|
|
/// Describes the visibility properties of the node
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Indicates the depth at which the node should appear in the UI
|
|
pub z_index: ZIndex,
|
|
/// The background color that will fill the containing node
|
|
pub background_color: BackgroundColor,
|
|
}
|
|
|
|
#[cfg(feature = "bevy_text")]
|
|
impl Default for TextBundle {
|
|
fn default() -> Self {
|
|
Self {
|
|
text: Default::default(),
|
|
text_layout_info: Default::default(),
|
|
text_flags: Default::default(),
|
|
calculated_size: Default::default(),
|
|
node: Default::default(),
|
|
style: Default::default(),
|
|
focus_policy: Default::default(),
|
|
transform: Default::default(),
|
|
global_transform: Default::default(),
|
|
visibility: Default::default(),
|
|
inherited_visibility: Default::default(),
|
|
view_visibility: Default::default(),
|
|
z_index: Default::default(),
|
|
// Transparent background
|
|
background_color: BackgroundColor(Color::NONE),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "bevy_text")]
|
|
impl TextBundle {
|
|
/// Create a [`TextBundle`] from a single section.
|
|
///
|
|
/// See [`Text::from_section`] for usage.
|
|
pub fn from_section(value: impl Into<String>, style: TextStyle) -> Self {
|
|
Self {
|
|
text: Text::from_section(value, style),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Create a [`TextBundle`] from a list of sections.
|
|
///
|
|
/// See [`Text::from_sections`] for usage.
|
|
pub fn from_sections(sections: impl IntoIterator<Item = TextSection>) -> Self {
|
|
Self {
|
|
text: Text::from_sections(sections),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Returns this [`TextBundle`] with a new [`JustifyText`] on [`Text`].
|
|
pub const fn with_text_justify(mut self, justify: JustifyText) -> Self {
|
|
self.text.justify = justify;
|
|
self
|
|
}
|
|
|
|
/// Returns this [`TextBundle`] with a new [`Style`].
|
|
pub fn with_style(mut self, style: Style) -> Self {
|
|
self.style = style;
|
|
self
|
|
}
|
|
|
|
/// Returns this [`TextBundle`] with a new [`BackgroundColor`].
|
|
pub const fn with_background_color(mut self, color: Color) -> Self {
|
|
self.background_color = BackgroundColor(color);
|
|
self
|
|
}
|
|
|
|
/// Returns this [`TextBundle`] with soft wrapping disabled.
|
|
/// Hard wrapping, where text contains an explicit linebreak such as the escape sequence `\n`, will still occur.
|
|
pub const fn with_no_wrap(mut self) -> Self {
|
|
self.text.linebreak_behavior = BreakLineOn::NoWrap;
|
|
self
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "bevy_text")]
|
|
impl<I> From<I> for TextBundle
|
|
where
|
|
I: Into<TextSection>,
|
|
{
|
|
fn from(value: I) -> Self {
|
|
Self::from_sections(vec![value.into()])
|
|
}
|
|
}
|
|
|
|
/// A UI node that is a button
|
|
///
|
|
/// # Extra behaviours
|
|
///
|
|
/// You may add the following components to enable additional behaviours
|
|
/// - [`ImageScaleMode`](bevy_sprite::ImageScaleMode) to enable either slicing or tiling of the texture
|
|
#[derive(Bundle, Clone, Debug)]
|
|
pub struct ButtonBundle {
|
|
/// Describes the logical size of the node
|
|
pub node: Node,
|
|
/// Marker component that signals this node is a button
|
|
pub button: Button,
|
|
/// Styles which control the layout (size and position) of the node and it's children
|
|
/// In some cases these styles also affect how the node drawn/painted.
|
|
pub style: Style,
|
|
/// Describes whether and how the button has been interacted with by the input
|
|
pub interaction: Interaction,
|
|
/// Whether this node should block interaction with lower nodes
|
|
pub focus_policy: FocusPolicy,
|
|
/// The background color, which serves as a "fill" for this node
|
|
///
|
|
/// When combined with `UiImage`, tints the provided image.
|
|
pub background_color: BackgroundColor,
|
|
/// The color of the Node's border
|
|
pub border_color: BorderColor,
|
|
/// The image of the node
|
|
pub image: UiImage,
|
|
/// The transform of the node
|
|
///
|
|
/// This component is automatically managed by the UI layout system.
|
|
/// To alter the position of the `ButtonBundle`, use the properties of the [`Style`] component.
|
|
pub transform: Transform,
|
|
/// The global transform of the node
|
|
///
|
|
/// This component is automatically updated by the [`TransformPropagate`](`bevy_transform::TransformSystem::TransformPropagate`) systems.
|
|
pub global_transform: GlobalTransform,
|
|
/// Describes the visibility properties of the node
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Indicates the depth at which the node should appear in the UI
|
|
pub z_index: ZIndex,
|
|
}
|
|
|
|
impl Default for ButtonBundle {
|
|
fn default() -> Self {
|
|
Self {
|
|
focus_policy: FocusPolicy::Block,
|
|
node: Default::default(),
|
|
button: Default::default(),
|
|
style: Default::default(),
|
|
border_color: BorderColor(Color::NONE),
|
|
interaction: Default::default(),
|
|
background_color: Default::default(),
|
|
image: Default::default(),
|
|
transform: Default::default(),
|
|
global_transform: Default::default(),
|
|
visibility: Default::default(),
|
|
inherited_visibility: Default::default(),
|
|
view_visibility: Default::default(),
|
|
z_index: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A UI node that is rendered using a [`UiMaterial`]
|
|
///
|
|
/// Adding a `BackgroundColor` component to an entity with this bundle will ignore the custom
|
|
/// material and use the background color instead.
|
|
#[derive(Bundle, Clone, Debug)]
|
|
pub struct MaterialNodeBundle<M: UiMaterial> {
|
|
/// Describes the logical size of the node
|
|
pub node: Node,
|
|
/// Styles which control the layout (size and position) of the node and it's children
|
|
/// In some cases these styles also affect how the node drawn/painted.
|
|
pub style: Style,
|
|
/// The [`UiMaterial`] used to render the node.
|
|
pub material: Handle<M>,
|
|
/// Whether this node should block interaction with lower nodes
|
|
pub focus_policy: FocusPolicy,
|
|
/// The transform of the node
|
|
///
|
|
/// This field is automatically managed by the UI layout system.
|
|
/// To alter the position of the `NodeBundle`, use the properties of the [`Style`] component.
|
|
pub transform: Transform,
|
|
/// The global transform of the node
|
|
///
|
|
/// This field is automatically managed by the UI layout system.
|
|
/// To alter the position of the `NodeBundle`, use the properties of the [`Style`] component.
|
|
pub global_transform: GlobalTransform,
|
|
/// Describes the visibility properties of the node
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Indicates the depth at which the node should appear in the UI
|
|
pub z_index: ZIndex,
|
|
}
|
|
|
|
impl<M: UiMaterial> Default for MaterialNodeBundle<M> {
|
|
fn default() -> Self {
|
|
Self {
|
|
node: Default::default(),
|
|
style: Default::default(),
|
|
material: Default::default(),
|
|
focus_policy: Default::default(),
|
|
transform: Default::default(),
|
|
global_transform: Default::default(),
|
|
visibility: Default::default(),
|
|
inherited_visibility: Default::default(),
|
|
view_visibility: Default::default(),
|
|
z_index: Default::default(),
|
|
}
|
|
}
|
|
}
|