1use super::*;
2
3pub struct Text<F: std::borrow::Borrow<Font>, T: AsRef<str>> {
5 pub font: F,
6 pub text: T,
7 pub color: Rgba<f32>,
8 pub into_unit_transform: mat3<f32>,
9 pub transform: mat3<f32>,
10 pub true_transform: mat3<f32>, }
12
13impl<F: std::borrow::Borrow<Font>, T: AsRef<str>> Text<F, T> {
14 pub fn unit(font: F, text: T, color: Rgba<f32>) -> Self {
15 if let Some(aabb) = font
16 .borrow()
17 .measure(text.as_ref(), vec2::splat(TextAlign::LEFT))
18 {
19 let aspect = aabb.width() / aabb.height();
20 Self {
21 font,
22 text,
23 color,
24 into_unit_transform: (mat3::translate(aabb.center())
25 * mat3::scale(aabb.size() / 2.0))
26 .inverse(),
27 transform: mat3::scale(vec2(aspect, 1.0)),
28 true_transform: mat3::translate(vec2(-aspect, -1.0)) * mat3::scale_uniform(4.0),
29 }
30 } else {
31 Self {
32 font,
33 text,
34 color,
35 into_unit_transform: mat3::identity(),
36 transform: mat3::scale_uniform(0.0),
37 true_transform: mat3::identity(),
38 }
39 }
40 }
41}
42
43impl<F: std::borrow::Borrow<Font>, T: AsRef<str>> Transform2d<f32> for Text<F, T> {
44 fn bounding_quad(&self) -> batbox_lapp::Quad<f32> {
45 batbox_lapp::Quad {
46 transform: self.transform,
47 }
48 }
49 fn apply_transform(&mut self, transform: mat3<f32>) {
50 self.transform = transform * self.transform;
51 self.true_transform = transform * self.true_transform;
52 }
53}
54
55impl<F: std::borrow::Borrow<Font>, T: AsRef<str>> Draw2d for Text<F, T> {
56 fn draw2d_transformed(
57 &self,
58 _: &Helper,
59 framebuffer: &mut ugli::Framebuffer,
60 camera: &dyn AbstractCamera2d,
61 transform: mat3<f32>,
62 ) {
63 self.font.borrow().draw(
64 framebuffer,
65 camera,
66 self.text.as_ref(),
67 vec2::splat(TextAlign::LEFT),
68 transform * self.transform * self.into_unit_transform,
69 self.color,
70 );
71 }
72}