mirror of
https://github.com/bevyengine/bevy
synced 2025-01-24 19:05:17 +00:00
9eefd7c022
# 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.
172 lines
5 KiB
Rust
172 lines
5 KiB
Rust
use bevy_asset::Handle;
|
|
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
|
|
use bevy_reflect::{prelude::*, FromReflect};
|
|
use bevy_render::color::Color;
|
|
use bevy_utils::default;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::Font;
|
|
|
|
#[derive(Component, Debug, Clone, Reflect)]
|
|
#[reflect(Component, Default)]
|
|
pub struct Text {
|
|
pub sections: Vec<TextSection>,
|
|
/// The text's internal alignment.
|
|
/// Should not affect its position within a container.
|
|
pub alignment: TextAlignment,
|
|
}
|
|
|
|
impl Default for Text {
|
|
fn default() -> Self {
|
|
Self {
|
|
sections: Default::default(),
|
|
alignment: TextAlignment::Left,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Text {
|
|
/// Constructs a [`Text`] with a single section.
|
|
///
|
|
/// ```
|
|
/// # use bevy_asset::Handle;
|
|
/// # use bevy_render::color::Color;
|
|
/// # use bevy_text::{Font, Text, TextStyle, TextAlignment};
|
|
/// #
|
|
/// # let font_handle: Handle<Font> = Default::default();
|
|
/// #
|
|
/// // Basic usage.
|
|
/// let hello_world = Text::from_section(
|
|
/// // Accepts a String or any type that converts into a String, such as &str.
|
|
/// "hello world!",
|
|
/// TextStyle {
|
|
/// font: font_handle.clone(),
|
|
/// font_size: 60.0,
|
|
/// color: Color::WHITE,
|
|
/// },
|
|
/// );
|
|
///
|
|
/// let hello_bevy = Text::from_section(
|
|
/// "hello bevy!",
|
|
/// TextStyle {
|
|
/// font: font_handle,
|
|
/// font_size: 60.0,
|
|
/// color: Color::WHITE,
|
|
/// },
|
|
/// ) // You can still add an alignment.
|
|
/// .with_alignment(TextAlignment::Center);
|
|
/// ```
|
|
pub fn from_section(value: impl Into<String>, style: TextStyle) -> Self {
|
|
Self {
|
|
sections: vec![TextSection::new(value, style)],
|
|
..default()
|
|
}
|
|
}
|
|
|
|
/// Constructs a [`Text`] from a list of sections.
|
|
///
|
|
/// ```
|
|
/// # use bevy_asset::Handle;
|
|
/// # use bevy_render::color::Color;
|
|
/// # use bevy_text::{Font, Text, TextStyle, TextSection};
|
|
/// #
|
|
/// # let font_handle: Handle<Font> = Default::default();
|
|
/// #
|
|
/// let hello_world = Text::from_sections([
|
|
/// TextSection::new(
|
|
/// "Hello, ",
|
|
/// TextStyle {
|
|
/// font: font_handle.clone(),
|
|
/// font_size: 60.0,
|
|
/// color: Color::BLUE,
|
|
/// },
|
|
/// ),
|
|
/// TextSection::new(
|
|
/// "World!",
|
|
/// TextStyle {
|
|
/// font: font_handle,
|
|
/// font_size: 60.0,
|
|
/// color: Color::RED,
|
|
/// },
|
|
/// ),
|
|
/// ]);
|
|
/// ```
|
|
pub fn from_sections(sections: impl IntoIterator<Item = TextSection>) -> Self {
|
|
Self {
|
|
sections: sections.into_iter().collect(),
|
|
..default()
|
|
}
|
|
}
|
|
|
|
/// Returns this [`Text`] with a new [`TextAlignment`].
|
|
pub const fn with_alignment(mut self, alignment: TextAlignment) -> Self {
|
|
self.alignment = alignment;
|
|
self
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, FromReflect, Reflect)]
|
|
pub struct TextSection {
|
|
pub value: String,
|
|
pub style: TextStyle,
|
|
}
|
|
|
|
impl TextSection {
|
|
/// Create a new [`TextSection`].
|
|
pub fn new(value: impl Into<String>, style: TextStyle) -> Self {
|
|
Self {
|
|
value: value.into(),
|
|
style,
|
|
}
|
|
}
|
|
|
|
/// Create an empty [`TextSection`] from a style. Useful when the value will be set dynamically.
|
|
pub const fn from_style(style: TextStyle) -> Self {
|
|
Self {
|
|
value: String::new(),
|
|
style,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Describes horizontal alignment preference for positioning & bounds.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
|
|
#[reflect(Serialize, Deserialize)]
|
|
pub enum TextAlignment {
|
|
/// Leftmost character is immediately to the right of the render position.<br/>
|
|
/// Bounds start from the render position and advance rightwards.
|
|
Left,
|
|
/// Leftmost & rightmost characters are equidistant to the render position.<br/>
|
|
/// Bounds start from the render position and advance equally left & right.
|
|
Center,
|
|
/// Rightmost character is immediately to the left of the render position.<br/>
|
|
/// Bounds start from the render position and advance leftwards.
|
|
Right,
|
|
}
|
|
|
|
impl From<TextAlignment> for glyph_brush_layout::HorizontalAlign {
|
|
fn from(val: TextAlignment) -> Self {
|
|
match val {
|
|
TextAlignment::Left => glyph_brush_layout::HorizontalAlign::Left,
|
|
TextAlignment::Center => glyph_brush_layout::HorizontalAlign::Center,
|
|
TextAlignment::Right => glyph_brush_layout::HorizontalAlign::Right,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Reflect, FromReflect)]
|
|
pub struct TextStyle {
|
|
pub font: Handle<Font>,
|
|
pub font_size: f32,
|
|
pub color: Color,
|
|
}
|
|
|
|
impl Default for TextStyle {
|
|
fn default() -> Self {
|
|
Self {
|
|
font: Default::default(),
|
|
font_size: 12.0,
|
|
color: Color::WHITE,
|
|
}
|
|
}
|
|
}
|