use crate::platform::Cx;
use crate::platform::event::Event;
use crate::platform::area::Area;
use crate::draw::Cx2d;
use crate::draw::draw_list_2d::DrawList2d;
use crate::draw::text::{DrawText, TextStyle, TextAlign};
use crate::draw::color::Color;
use crate::draw::math::Vec2;
use crate::widgets::widget::{Widget, DrawStep};
use crate::widgets::theme::Theme;
#[derive(Clone, Debug)]
pub struct Label {
pub text: String,
pub draw_list: DrawList2d,
pub area: Area,
pub draw_text: DrawText,
pub padding: Vec2,
}
impl Label {
pub fn new(cx: &mut Cx, text: &str) -> Self {
let theme = Theme::default();
Self {
text: text.to_string(),
draw_list: DrawList2d::new(cx),
area: cx.create_area(),
draw_text: DrawText::new()
.with_text(text)
.with_style(theme.default_text_style),
padding: Vec2::new(theme.spacing_small, theme.spacing_small),
}
}
pub fn with_text_style(mut self, style: TextStyle) -> Self {
self.draw_text = self.draw_text.with_style(style);
self
}
pub fn with_color(mut self, color: Color) -> Self {
self.draw_text = self.draw_text.with_color(color);
self
}
pub fn with_font_size(mut self, font_size: f32) -> Self {
self.draw_text = self.draw_text.with_font_size(font_size);
self
}
pub fn with_align(mut self, align: TextAlign) -> Self {
self.draw_text = self.draw_text.with_align(align);
self
}
pub fn with_padding(mut self, padding: Vec2) -> Self {
self.padding = padding;
self
}
}
impl Widget for Label {
fn handle_event(&mut self, _cx: &mut Cx, _event: &Event) {
}
fn draw(&mut self, cx: &mut Cx2d) -> DrawStep {
self.draw_list.begin(cx);
let text_size = Vec2::new(100.0, 20.0); let label_size = Vec2::new(
text_size.x + self.padding.x * 2.0,
text_size.y + self.padding.y * 2.0,
);
if let Some(rect) = cx.add_turtle_item(label_size) {
self.draw_text.draw(cx, self.draw_list.id(), &rect, &Default::default());
cx.set_area_rect(self.area, rect.x(), rect.y(), rect.width(), rect.height());
cx.set_area_draw_list(self.area, self.draw_list.id());
}
self.draw_list.end(cx);
DrawStep::done()
}
}