Skip to main content

grafix_toolbox/gui/elements/
label.rs

1use super::*;
2
3#[derive(Default, Debug)]
4pub struct Label {
5	offset: Vec2,
6	size: Vec2,
7	scale: f32,
8	text: Str,
9}
10impl Label {
11	pub fn draw<'s: 'l, 'l>(&'s mut self, r: &mut RenderLock<'l>, t: &'l Theme, Surf { pos, size }: Surf, text: &str) {
12		let (s, font) = (self, &t.font);
13
14		if *s.text != *text || s.size != size {
15			let (offset, scale) = u::fit_line(text, font, t.font_size, size);
16			*s = Self { offset, size, scale, text: text.into() };
17		}
18		let Self { offset, scale, .. } = *s;
19
20		r.draw(Rect { pos, size, color: t.fg });
21
22		r.draw(Text { pos: pos.sum(offset), color: t.text, scale, text, font });
23	}
24}
25
26impl<'s: 'l, 'l> Lock::Label<'s, 'l, '_> {
27	pub fn draw(self, g: impl Into<Surf>, te: impl AsRef<str>) {
28		let Self { s, r, t } = self;
29		s.draw(r, t, g.into(), te.as_ref())
30	}
31}