From 9c2a2668d243647cfe5bde818a2bf19cf388ecc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Maita?= <47983254+mnmaita@users.noreply.github.com> Date: Tue, 19 Nov 2024 02:30:39 +0100 Subject: [PATCH] Adds some helpful methods to TextFont (#16370) # Objective - Add methods to facilitate `TextFont` component creation and insertion. ## Solution - Added `from_font` and `from_font_size` which return a new `TextFont` with said attributes provided as parameters. - Added `with_font` and `with_font_size` which return an existing `TextFont` modifying said attributes with the values provided as parameters. ## Testing - CI Checks. - Tested methods locally by changing values and running the `text_debug` example. --- crates/bevy_text/src/text.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 13e464451b..0effb361aa 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -289,6 +289,28 @@ pub struct TextFont { } impl TextFont { + /// Returns a new [`TextFont`] with the specified font face handle. + pub fn from_font(font: Handle) -> Self { + Self::default().with_font(font) + } + + /// Returns a new [`TextFont`] with the specified font size. + pub fn from_font_size(font_size: f32) -> Self { + Self::default().with_font_size(font_size) + } + + /// Returns this [`TextFont`] with the specified font face handle. + pub fn with_font(mut self, font: Handle) -> Self { + self.font = font; + self + } + + /// Returns this [`TextFont`] with the specified font size. + pub const fn with_font_size(mut self, font_size: f32) -> Self { + self.font_size = font_size; + self + } + /// Returns this [`TextFont`] with the specified [`FontSmoothing`]. pub const fn with_font_smoothing(mut self, font_smoothing: FontSmoothing) -> Self { self.font_smoothing = font_smoothing;