use crate::prelude::*;
pub struct Label<'a, ID, COL: PixelColor> {
region: &'a Region<ID>,
text: &'a str,
font: OptionFont<'a>,
align: HorizontalAlign,
color: OptionColor<COL>,
}
impl<'a, ID: WidgetId, COL: PixelColor> Label<'a, ID, COL> {
pub const fn new(region: &'a Region<ID>, text: &'a str) -> Label<'a, ID, COL> {
Label {
region,
text,
font: OptionFont::none(),
align: HorizontalAlign::Left,
color: OptionColor::none(),
}
}
pub const fn with_font(mut self, font: UiFont<'a>) -> Self {
self.font.set_font(font);
self
}
pub const fn with_align(mut self, align: HorizontalAlign) -> Self {
self.align = align;
self
}
pub const fn with_color(mut self, color: COL) -> Self {
self.color.set_color(color);
self
}
}
impl<DRAW: DrawTarget<Color = COL>, ID: WidgetId, COL: PixelColor> Widget<DRAW, COL>
for Label<'_, ID, COL>
{
fn draw(&mut self, ui: &mut Ui<DRAW, COL>) -> GuiResult<Response> {
let widget_state = ui.get_widget_state(self.region.id())?;
if widget_state.compare_set(RenderStatus::Rendered) {
return Ok(Response::Idle);
}
let area = self.region.rectangle();
let font = self.font.font(ui.style());
let color = self.color.text_color(ui.style());
let mut text = matrix_utils::make_text(self.text, font, color);
matrix_utils::text_align_translate(&mut text, &area, self.align);
ui.clear_area(&area)?;
ui.draw(&text)?;
Ok(Response::Idle)
}
}