bevy_text/bounds.rs
1use bevy_ecs::{component::Component, reflect::ReflectComponent};
2use bevy_math::Vec2;
3use bevy_reflect::{std_traits::ReflectDefault, Reflect};
4
5/// The maximum width and height of text. The text will wrap according to the specified size.
6///
7/// Characters out of the bounds after wrapping will be truncated. Text is aligned according to the
8/// specified [`Justify`](crate::text::Justify).
9///
10/// Note: only characters that are completely out of the bounds will be truncated, so this is not a
11/// reliable limit if it is necessary to contain the text strictly in the bounds. Currently this
12/// component is mainly useful for text wrapping only.
13#[derive(Component, Copy, Clone, Debug, Reflect)]
14#[reflect(Component, Default, Debug, Clone)]
15pub struct TextBounds {
16 /// The maximum width of text in logical pixels.
17 /// If `None`, the width is unbounded.
18 pub width: Option<f32>,
19 /// The maximum height of text in logical pixels.
20 /// If `None`, the height is unbounded.
21 pub height: Option<f32>,
22}
23
24impl Default for TextBounds {
25 #[inline]
26 fn default() -> Self {
27 Self::UNBOUNDED
28 }
29}
30
31impl TextBounds {
32 /// Unbounded text will not be truncated or wrapped.
33 pub const UNBOUNDED: Self = Self {
34 width: None,
35 height: None,
36 };
37
38 /// Creates a new `TextBounds`, bounded with the specified width and height values.
39 #[inline]
40 pub const fn new(width: f32, height: f32) -> Self {
41 Self {
42 width: Some(width),
43 height: Some(height),
44 }
45 }
46
47 /// Creates a new `TextBounds`, bounded with the specified width value and unbounded on height.
48 #[inline]
49 pub const fn new_horizontal(width: f32) -> Self {
50 Self {
51 width: Some(width),
52 height: None,
53 }
54 }
55
56 /// Creates a new `TextBounds`, bounded with the specified height value and unbounded on width.
57 #[inline]
58 pub const fn new_vertical(height: f32) -> Self {
59 Self {
60 width: None,
61 height: Some(height),
62 }
63 }
64}
65
66impl From<Vec2> for TextBounds {
67 #[inline]
68 fn from(v: Vec2) -> Self {
69 Self::new(v.x, v.y)
70 }
71}