
# Objective Remove the `VerticalAlign` enum. Text's alignment field should only affect the text's internal text alignment, not its position. The only way to control a `TextBundle`'s position and bounds should be through the manipulation of the constraints in the `Style` components of the nodes in the Bevy UI's layout tree. `Text2dBundle` should have a separate `Anchor` component that sets its position relative to its transform. Related issues: #676, #1490, #5502, #5513, #5834, #6717, #6724, #6741, #6748 ## Changelog * Changed `TextAlignment` into an enum with `Left`, `Center`, and `Right` variants. * Removed the `HorizontalAlign` and `VerticalAlign` types. * Added an `Anchor` component to `Text2dBundle` * Added `Component` derive to `Anchor` * Use `f32::INFINITY` instead of `f32::MAX` to represent unbounded text in Text2dBounds ## Migration Guide The `alignment` field of `Text` now only affects the text's internal alignment. ### Change `TextAlignment` to TextAlignment` which is now an enum. Replace: * `TextAlignment::TOP_LEFT`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_LEFT` with `TextAlignment::Left` * `TextAlignment::TOP_CENTER`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_CENTER` with `TextAlignment::Center` * `TextAlignment::TOP_RIGHT`, `TextAlignment::CENTER_RIGHT`, `TextAlignment::BOTTOM_RIGHT` with `TextAlignment::Right` ### Changes for `Text2dBundle` `Text2dBundle` has a new field 'text_anchor' that takes an `Anchor` component that controls its position relative to its transform.
61 lines
2.0 KiB
Rust
61 lines
2.0 KiB
Rust
use bevy_ecs::component::Component;
|
|
use bevy_math::{Rect, Vec2};
|
|
use bevy_reflect::Reflect;
|
|
use bevy_render::color::Color;
|
|
|
|
#[derive(Component, Debug, Default, Clone, Reflect)]
|
|
#[repr(C)]
|
|
pub struct Sprite {
|
|
/// The sprite's color tint
|
|
pub color: Color,
|
|
/// Flip the sprite along the `X` axis
|
|
pub flip_x: bool,
|
|
/// Flip the sprite along the `Y` axis
|
|
pub flip_y: bool,
|
|
/// An optional custom size for the sprite that will be used when rendering, instead of the size
|
|
/// of the sprite's image
|
|
pub custom_size: Option<Vec2>,
|
|
/// An optional rectangle representing the region of the sprite's image to render, instead of
|
|
/// rendering the full image. This is an easy one-off alternative to using a texture atlas.
|
|
pub rect: Option<Rect>,
|
|
/// [`Anchor`] point of the sprite in the world
|
|
pub anchor: Anchor,
|
|
}
|
|
|
|
/// How a sprite is positioned relative to its [`Transform`](bevy_transform::components::Transform).
|
|
/// It defaults to `Anchor::Center`.
|
|
#[derive(Component, Debug, Clone, Default, Reflect)]
|
|
#[doc(alias = "pivot")]
|
|
pub enum Anchor {
|
|
#[default]
|
|
Center,
|
|
BottomLeft,
|
|
BottomCenter,
|
|
BottomRight,
|
|
CenterLeft,
|
|
CenterRight,
|
|
TopLeft,
|
|
TopCenter,
|
|
TopRight,
|
|
/// Custom anchor point. Top left is `(-0.5, 0.5)`, center is `(0.0, 0.0)`. The value will
|
|
/// be scaled with the sprite size.
|
|
Custom(Vec2),
|
|
}
|
|
|
|
impl Anchor {
|
|
pub fn as_vec(&self) -> Vec2 {
|
|
match self {
|
|
Anchor::Center => Vec2::ZERO,
|
|
Anchor::BottomLeft => Vec2::new(-0.5, -0.5),
|
|
Anchor::BottomCenter => Vec2::new(0.0, -0.5),
|
|
Anchor::BottomRight => Vec2::new(0.5, -0.5),
|
|
Anchor::CenterLeft => Vec2::new(-0.5, 0.0),
|
|
Anchor::CenterRight => Vec2::new(0.5, 0.0),
|
|
Anchor::TopLeft => Vec2::new(-0.5, 0.5),
|
|
Anchor::TopCenter => Vec2::new(0.0, 0.5),
|
|
Anchor::TopRight => Vec2::new(0.5, 0.5),
|
|
Anchor::Custom(point) => *point,
|
|
}
|
|
}
|
|
}
|