1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
//! Text is a simple widget that allows you to print text on screen. See [`Text`] docs for more info and
//! examples.
#![warn(missing_docs)]
use crate::{
brush::Brush,
core::{algebra::Vector2, color::Color, pool::Handle},
define_constructor,
draw::DrawingContext,
formatted_text::{FormattedText, FormattedTextBuilder, WrapMode},
message::{MessageDirection, UiMessage},
ttf::SharedFont,
widget::{Widget, WidgetBuilder},
BuildContext, Control, HorizontalAlignment, UiNode, UserInterface, VerticalAlignment,
};
use std::{
any::{Any, TypeId},
cell::RefCell,
ops::{Deref, DerefMut},
};
/// Possible messages that can be used to alternate [`Text`] widget state at runtime.
#[derive(Debug, Clone, PartialEq)]
pub enum TextMessage {
/// Used to set new text of the widget.
Text(String),
/// Used to set new text wrapping mode of the widget. See [Text](Text#text-alignment-and-word-wrapping) for usage
/// examples.
Wrap(WrapMode),
/// Used to set new font of the widget. See [Text](Text#fonts-and_colors) for usage examples.
Font(SharedFont),
/// Used to set new vertical alignment of the widget. See [Text](Text#text-alignment-and-word-wrapping) for usage
/// examples.
VerticalAlignment(VerticalAlignment),
/// Used to set new horizontal alignment of the widget. See [Text](Text#text-alignment-and-word-wrapping) for usage
/// examples.
HorizontalAlignment(HorizontalAlignment),
/// Used to enable/disable shadow casting of the widget. See [Text](Text#shadows) for usage examples.
Shadow(bool),
/// Used to set new dilation factor of the shadows. See [Text](Text#shadows) for usage examples.
ShadowDilation(f32),
/// Used to set new brush that will be used to draw the shadows. See [Text](Text#shadows) for usage examples.
ShadowBrush(Brush),
/// Used to set how much the shadows will be offset from the widget. See [Text](Text#shadows) for usage examples.
ShadowOffset(Vector2<f32>),
}
impl TextMessage {
define_constructor!(
/// Creates new [`TextMessage::Text`] message.
TextMessage:Text => fn text(String), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::Wrap`] message.
TextMessage:Wrap => fn wrap(WrapMode), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::Font`] message.
TextMessage:Font => fn font(SharedFont), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::VerticalAlignment`] message.
TextMessage:VerticalAlignment => fn vertical_alignment(VerticalAlignment), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::HorizontalAlignment`] message.
TextMessage:HorizontalAlignment => fn horizontal_alignment(HorizontalAlignment), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::Shadow`] message.
TextMessage:Shadow => fn shadow(bool), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::ShadowDilation`] message.
TextMessage:ShadowDilation => fn shadow_dilation(f32), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::ShadowBrush`] message.
TextMessage:ShadowBrush => fn shadow_brush(Brush), layout: false
);
define_constructor!(
/// Creates new [`TextMessage::ShadowOffset`] message.
TextMessage:ShadowOffset => fn shadow_offset(Vector2<f32>), layout: false
);
}
/// Text is a simple widget that allows you to print text on screen. It has various options like word wrapping, text
/// alignment, and so on.
///
/// ## How to create
///
/// An instance of the [`Text`] widget could be created like so:
///
/// ```rust
/// # use fyrox_ui::{
/// # core::pool::Handle,
/// # text::TextBuilder, widget::WidgetBuilder, UiNode, UserInterface
/// # };
/// fn create_text(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// TextBuilder::new(WidgetBuilder::new())
/// .with_text(text)
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// ## Text alignment and word wrapping
///
/// There are various text alignment options for both vertical and horizontal axes. Typical alignment values are:
/// [`HorizontalAlignment::Left`], [`HorizontalAlignment::Center`], [`HorizontalAlignment::Right`] for horizontal axis,
/// and [`VerticalAlignment::Top`], [`VerticalAlignment::Center`], [`VerticalAlignment::Bottom`] for vertical axis. An
/// instance of centered text could be created like so:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// # core::pool::Handle,
/// # text::TextBuilder, widget::WidgetBuilder, HorizontalAlignment, UiNode, UserInterface,
/// # VerticalAlignment,
/// # };
/// fn create_centered_text(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// TextBuilder::new(WidgetBuilder::new())
/// .with_horizontal_text_alignment(HorizontalAlignment::Center)
/// .with_vertical_text_alignment(VerticalAlignment::Center)
/// .with_text(text)
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// What's the difference between widget's alignment and text-specific? Widget's alignment operates on a bounding rectangle
/// of the text and text-specific alignment operates on line-basis. This means that if you set [`HorizontalAlignment::Center`]
/// as widget's alignment, your text lines won't be centered, instead they'll be aligned at the left and the entire text block
/// will be aligned at center.
///
/// Long text is usually needs to wrap on available bounds, there are three possible options for word wrapping:
/// [`WrapMode::NoWrap`], [`WrapMode::Letter`], [`WrapMode::Word`]. An instance of text with word-based wrapping could
/// be created like so:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// # core::pool::Handle,
/// # formatted_text::WrapMode, text::TextBuilder, widget::WidgetBuilder, UiNode,
/// # UserInterface,
/// # };
/// fn create_text_with_word_wrap(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// TextBuilder::new(WidgetBuilder::new())
/// .with_wrap(WrapMode::Word)
/// .with_text(text)
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// ## Background
///
/// If you need to have a text with some background, you should use [`crate::border::Border`] widget as a parent widget of your
/// text. **Caveat:** [`WidgetBuilder::with_background`] is ignored for [`Text`] widget!
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// # core::{color::Color, pool::Handle},
/// # border::BorderBuilder, brush::Brush, text::TextBuilder, widget::WidgetBuilder, UiNode,
/// # UserInterface,
/// # };
/// #
/// fn create_text_with_background(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// let text_widget =
/// TextBuilder::new(WidgetBuilder::new().with_foreground(Brush::Solid(Color::RED)))
/// .with_text(text)
/// .build(&mut ui.build_ctx());
/// BorderBuilder::new(
/// WidgetBuilder::new()
/// .with_child(text_widget) // <-- Text is now a child of the border
/// .with_background(Brush::Solid(Color::opaque(50, 50, 50))),
/// )
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// Keep in mind that now the text widget is a child widget of the border, so if you need to position the text, you should
/// position the border, not the text.
///
/// ## Fonts and colors
///
/// To set a color of the text just use [`WidgetBuilder::with_foreground`] while building the text instance:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// # core::{color::Color, pool::Handle},
/// # brush::Brush, text::TextBuilder, widget::WidgetBuilder, UiNode, UserInterface
/// # };
/// fn create_text(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
/// TextBuilder::new(WidgetBuilder::new().with_foreground(Brush::Solid(Color::RED)))
/// .with_text(text)
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// By default, text is created with default font, however it is possible to set any custom font:
///
/// ```rust
/// # use fyrox_ui::{
/// # core::{futures::executor::block_on, pool::Handle},
/// # text::TextBuilder,
/// # ttf::{Font, SharedFont},
/// # widget::WidgetBuilder,
/// # UiNode, UserInterface,
/// # };
///
/// fn load_font() -> SharedFont {
/// // Choose desired character set, default is Basic Latin + Latin Supplement.
/// // Character set is a set of ranges with Unicode code points.
/// let character_set = Font::default_char_set();
///
/// // Normally `block_on` should be avoided by using async.
/// let font = block_on(Font::from_file(
/// "path/to/your/font.ttf",
/// 24.0,
/// character_set,
/// ))
/// .unwrap();
///
/// SharedFont::new(font)
/// }
///
/// fn create_text(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// TextBuilder::new(WidgetBuilder::new())
/// .with_font(load_font())
/// .with_text(text)
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// Please refer to [`crate::ttf::Font`] chapter to learn more about fonts.
///
/// ### Font size
///
/// There is no way to change font size without changing the entire font used by Text, it is known issue and there is
/// [tracking issue](https://github.com/FyroxEngine/Fyrox/issues/74) for that. Check [`crate::ttf::Font`] docs to learn how
/// to create fonts.
///
/// ## Shadows
///
/// Text widget supports shadows effect to add contrast to your text, which could be useful to make text readable independent
/// on the background colors. This effect could be used for subtitles. Shadows are pretty easy to add, all you need to do
/// is to enable them, setup desired thickness, offset and brush (solid color or gradient).
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// # core::{algebra::Vector2, color::Color, pool::Handle},
/// # brush::Brush, text::TextBuilder, widget::WidgetBuilder, UiNode, UserInterface
/// # };
/// #
/// fn create_red_text_with_black_shadows(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
/// TextBuilder::new(WidgetBuilder::new().with_foreground(Brush::Solid(Color::RED)))
/// .with_text(text)
/// // Enable shadows.
/// .with_shadow(true)
/// // Black shadows.
/// .with_shadow_brush(Brush::Solid(Color::BLACK))
/// // 1px thick.
/// .with_shadow_dilation(1.0)
/// // Offset the shadow slightly to the right-bottom.
/// .with_shadow_offset(Vector2::new(1.0, 1.0))
/// .build(&mut ui.build_ctx())
/// }
/// ```
///
/// ## Messages
///
/// Text widget can accept the following list of messages at runtime (respective constructors are name with small letter -
/// `TextMessage::Text -> TextMessage::text(widget_handle, direction, text)`):
///
/// - [`TextMessage::Text`] - sets new text for a `Text` widget.
/// - [`TextMessage::Wrap`] - sets new [wrapping mode](Text#text-alignment-and-word-wrapping).
/// - [`TextMessage::Font`] - sets new [font](Text#fonts-and-colors)
/// - [`TextMessage::VerticalAlignment`] and `TextMessage::HorizontalAlignment` sets
/// [vertical and horizontal](Text#text-alignment-and-word-wrapping) text alignment respectively.
/// - [`TextMessage::Shadow`] - enables or disables [shadow casting](Text#shadows)
/// - [`TextMessage::ShadowDilation`] - sets "thickness" of the shadows under the tex.
/// - [`TextMessage::ShadowBrush`] - sets shadow brush (allows you to change color and even make shadow with color gradients).
/// - [`TextMessage::ShadowOffset`] - sets offset of the shadows.
///
/// An example of changing text at runtime could be something like this:
///
/// ```rust
/// # use fyrox_ui::{
/// # core::pool::Handle,
/// # message::{MessageDirection},
/// # UiNode, UserInterface,
/// # text::TextMessage
/// # };
/// fn request_change_text(ui: &UserInterface, text_widget_handle: Handle<UiNode>, text: &str) {
/// ui.send_message(TextMessage::text(
/// text_widget_handle,
/// MessageDirection::ToWidget,
/// text.to_owned(),
/// ))
/// }
/// ```
///
/// Please keep in mind, that like any other situation when you "changing" something via messages, you should remember
/// that the change is **not** immediate.
#[derive(Clone)]
pub struct Text {
/// Base widget of the Text widget.
pub widget: Widget,
/// [`FormattedText`] instance that is used to layout text and generate drawing commands.
pub formatted_text: RefCell<FormattedText>,
}
crate::define_widget_deref!(Text);
impl Control for Text {
fn query_component(&self, type_id: TypeId) -> Option<&dyn Any> {
if type_id == TypeId::of::<Self>() {
Some(self)
} else {
None
}
}
fn measure_override(&self, _: &UserInterface, available_size: Vector2<f32>) -> Vector2<f32> {
self.formatted_text
.borrow_mut()
.set_constraint(available_size)
.set_brush(self.widget.foreground())
.build()
}
fn draw(&self, drawing_context: &mut DrawingContext) {
let bounds = self.widget.bounding_rect();
drawing_context.draw_text(
self.clip_bounds(),
bounds.position,
&self.formatted_text.borrow(),
);
}
fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
self.widget.handle_routed_message(ui, message);
if message.destination() == self.handle() {
if let Some(msg) = message.data::<TextMessage>() {
let mut text_ref = self.formatted_text.borrow_mut();
match msg {
TextMessage::Text(text) => {
text_ref.set_text(text);
drop(text_ref);
self.invalidate_layout();
}
&TextMessage::Wrap(wrap) => {
if text_ref.wrap_mode() != wrap {
text_ref.set_wrap(wrap);
drop(text_ref);
self.invalidate_layout();
}
}
TextMessage::Font(font) => {
if &text_ref.get_font() != font {
text_ref.set_font(font.clone());
drop(text_ref);
self.invalidate_layout();
}
}
&TextMessage::HorizontalAlignment(horizontal_alignment) => {
if text_ref.horizontal_alignment() != horizontal_alignment {
text_ref.set_horizontal_alignment(horizontal_alignment);
drop(text_ref);
self.invalidate_layout();
}
}
&TextMessage::VerticalAlignment(vertical_alignment) => {
if text_ref.vertical_alignment() != vertical_alignment {
text_ref.set_vertical_alignment(vertical_alignment);
drop(text_ref);
self.invalidate_layout();
}
}
&TextMessage::Shadow(shadow) => {
if text_ref.shadow != shadow {
text_ref.set_shadow(shadow);
drop(text_ref);
self.invalidate_layout();
}
}
TextMessage::ShadowBrush(brush) => {
if &text_ref.shadow_brush != brush {
text_ref.set_shadow_brush(brush.clone());
drop(text_ref);
self.invalidate_layout();
}
}
&TextMessage::ShadowDilation(dilation) => {
if text_ref.shadow_dilation != dilation {
text_ref.set_shadow_dilation(dilation);
drop(text_ref);
self.invalidate_layout();
}
}
&TextMessage::ShadowOffset(offset) => {
if text_ref.shadow_offset != offset {
text_ref.set_shadow_offset(offset);
drop(text_ref);
self.invalidate_layout();
}
}
}
}
}
}
}
impl Text {
/// Returns current text wrapping mode of the widget.
pub fn wrap_mode(&self) -> WrapMode {
self.formatted_text.borrow().wrap_mode()
}
/// Returns current text of the widget.
pub fn text(&self) -> String {
self.formatted_text.borrow().text()
}
/// Returns current font of the widget.
pub fn font(&self) -> SharedFont {
self.formatted_text.borrow().get_font()
}
/// Returns current vertical alignment of the widget.
pub fn vertical_alignment(&self) -> VerticalAlignment {
self.formatted_text.borrow().vertical_alignment()
}
/// Returns current horizontal alignment of the widget.
pub fn horizontal_alignment(&self) -> HorizontalAlignment {
self.formatted_text.borrow().horizontal_alignment()
}
}
/// TextBuilder is used to create instances of [`Text`] widget and register them in the user interface.
pub struct TextBuilder {
widget_builder: WidgetBuilder,
text: Option<String>,
font: Option<SharedFont>,
vertical_text_alignment: VerticalAlignment,
horizontal_text_alignment: HorizontalAlignment,
wrap: WrapMode,
shadow: bool,
shadow_brush: Brush,
shadow_dilation: f32,
shadow_offset: Vector2<f32>,
}
impl TextBuilder {
/// Creates new [`TextBuilder`] instance using the provided base widget builder.
pub fn new(widget_builder: WidgetBuilder) -> Self {
Self {
widget_builder,
text: None,
font: None,
vertical_text_alignment: VerticalAlignment::Top,
horizontal_text_alignment: HorizontalAlignment::Left,
wrap: WrapMode::NoWrap,
shadow: false,
shadow_brush: Brush::Solid(Color::BLACK),
shadow_dilation: 1.0,
shadow_offset: Vector2::new(1.0, 1.0),
}
}
/// Sets the desired text of the widget.
pub fn with_text<P: AsRef<str>>(mut self, text: P) -> Self {
self.text = Some(text.as_ref().to_owned());
self
}
/// Sets the desired font of the widget.
pub fn with_font(mut self, font: SharedFont) -> Self {
self.font = Some(font);
self
}
/// Sets the desired font of the widget using font wrapped in [`Option`].
pub fn with_opt_font(mut self, font: Option<SharedFont>) -> Self {
self.font = font;
self
}
/// Sets the desired vertical alignment of the widget.
pub fn with_vertical_text_alignment(mut self, valign: VerticalAlignment) -> Self {
self.vertical_text_alignment = valign;
self
}
/// Sets the desired horizontal alignment of the widget.
pub fn with_horizontal_text_alignment(mut self, halign: HorizontalAlignment) -> Self {
self.horizontal_text_alignment = halign;
self
}
/// Sets the desired word wrapping mode of the widget.
pub fn with_wrap(mut self, wrap: WrapMode) -> Self {
self.wrap = wrap;
self
}
/// Whether the shadow enabled or not.
pub fn with_shadow(mut self, shadow: bool) -> Self {
self.shadow = shadow;
self
}
/// Sets desired shadow brush. It will be used to render the shadow.
pub fn with_shadow_brush(mut self, brush: Brush) -> Self {
self.shadow_brush = brush;
self
}
/// Sets desired shadow dilation in units. Keep in mind that the dilation is absolute,
/// not percentage-based.
pub fn with_shadow_dilation(mut self, thickness: f32) -> Self {
self.shadow_dilation = thickness;
self
}
/// Sets desired shadow offset in units.
pub fn with_shadow_offset(mut self, offset: Vector2<f32>) -> Self {
self.shadow_offset = offset;
self
}
/// Finishes text widget creation and registers it in the user interface, returning its handle to you.
pub fn build(mut self, ui: &mut BuildContext) -> Handle<UiNode> {
let font = if let Some(font) = self.font {
font
} else {
ui.default_font()
};
if self.widget_builder.foreground.is_none() {
self.widget_builder.foreground = Some(Brush::Solid(Color::opaque(220, 220, 220)));
}
let text = Text {
widget: self.widget_builder.build(),
formatted_text: RefCell::new(
FormattedTextBuilder::new(font)
.with_text(self.text.unwrap_or_default())
.with_vertical_alignment(self.vertical_text_alignment)
.with_horizontal_alignment(self.horizontal_text_alignment)
.with_wrap(self.wrap)
.with_shadow(self.shadow)
.with_shadow_brush(self.shadow_brush)
.with_shadow_dilation(self.shadow_dilation)
.with_shadow_offset(self.shadow_offset)
.build(),
),
};
ui.add_node(UiNode::new(text))
}
}