console_ui_engine_null/ui_components/
label.rs1use std::any::Any;
2
3use crate::buffer::{SizedBuffer, StyledChar};
4use crate::ui_components::Content;
5use crate::ui_element::UiElement;
6
7ui_component_struct!(
8pub struct Label {
9 pub position: (u16, u16),
10 content: Content,
11});
12
13impl Label {
14 pub fn new(name: &'static str, content: Content, position: (u16, u16)) -> Label {
15 Label {
16 name,
17 focused: false,
18 position,
19 content,
20 }
21 }
22
23 pub fn get_content(&self) -> &Content {
24 &self.content
25 }
26
27 pub fn get_content_mut(&mut self) -> &mut Content {
28 &mut self.content
29 }
30
31 pub fn replace_content(&mut self, content: Content) {
32 self.content = content;
33 }
34}
35
36pub fn render_line(buffer: &mut SizedBuffer, content: &Content, position: (u16, u16)) {
37 let mut x_offset = 0;
38 match content {
39 Content::Plain(content, style) => {
40 for c in content.chars() {
41 let mut sc = StyledChar::from_char(c);
42 if let Some(style) = style {
43 sc.style = style.clone();
44 }
45 buffer.set_pixel(&sc, position.0 + x_offset, position.1);
46 x_offset += 1;
47 }
48 },
49 Content::RichText(content) => {
50 for c in content {
51 buffer.set_pixel(&c, position.0 + x_offset, position.1);
52 x_offset += 1;
53 }
54 },
55 };
56}
57
58impl UiElement for Label {
59 fn render(&self, buffer: &mut SizedBuffer) {
60 render_line(buffer, &self.content, self.position);
61 }
62
63 fn is_clicked(&self, x: u16, y: u16) -> bool {
64 x >= self.position.0 && x < self.position.0 + self.content.len() as u16
65 && y == self.position.1
66 }
67
68 ui_component_impl!();
69}