use crate::text::{Alignment, TextRenderer};
use crate::types::*;
pub struct Text<'a> {
renderer: &'a mut TextRenderer,
data: TextData,
}
pub(crate) struct TextData {
pub x: i32,
pub y: i32,
pub z: f32,
pub font_size: i32,
pub text: String,
pub alignment: Alignment,
pub max_line_width: Option<i32>,
pub color: (f32, f32, f32, f32),
pub rotation: (f32, f32, f32),
pub clip_area: Option<Rect>,
pub visible: bool,
}
impl<'a> Text<'a> {
pub(crate) fn new(
renderer: &'a mut TextRenderer,
text: String,
x: i32,
y: i32,
font_size: i32,
) -> Text<'a> {
Text {
renderer,
data: TextData {
text,
x,
y,
z: 0.0,
font_size,
alignment: Alignment::Left,
max_line_width: None,
clip_area: None,
color: (0.0, 0.0, 0.0, 1.0),
rotation: (0.0, 0.0, 0.0),
visible: true,
},
}
}
pub fn finish(&mut self) -> Option<Rect> {
self.renderer.draw_text(&self.data)
}
pub fn z(&mut self, z: f32) -> &mut Self {
self.data.z = z;
self
}
pub fn color(&mut self, (red, green, blue, alpha): (f32, f32, f32, f32)) -> &mut Self {
self.data.color = (red, green, blue, alpha);
self
}
pub fn alignment(&mut self, alignment: Alignment) -> &mut Self {
self.data.alignment = alignment;
self
}
pub fn max_width(&mut self, width: f32) -> &mut Self {
self.data.max_line_width = Some((width * self.renderer.dpi_factor) as i32);
self
}
pub fn clip_area<R: Into<Rect>>(&mut self, clip_area: R) -> &mut Self {
self.data.clip_area = Some(clip_area.into());
self
}
pub fn visibility(&mut self, visible: bool) -> &mut Self {
self.data.visible = visible;
self
}
pub fn rotation(&mut self, rotation: f32, pivot_x: f32, pivot_y: f32) -> &mut Self {
self.data.rotation = (rotation, pivot_x, pivot_y);
self
}
}