Add Label builder

master
D4VID 1 month ago
parent 3738606742
commit 9801e5d6c8

@ -145,6 +145,85 @@ impl CircuitBoard {
return custom_data;
}
}
pub struct Label<'a> {
text: &'a str,
color: Vec<u8>,
size: Float,
mono: bool,
width: Int,
height: Int,
horizontal_align: HorizontalAlign,
vertical_align: VerticalAlign,
}
#[repr(i32)]
#[derive(Copy, Clone)]
pub enum HorizontalAlign {
Left = 0,
Middle = 1,
Right = 2,
}
#[repr(i32)]
#[derive(Copy, Clone)]
pub enum VerticalAlign {
Top = 0,
Middle = 1,
Bottom = 2,
}
impl<'a> Label<'a> {
pub fn new(text: &'a str) -> Self {
Self {
text,
color: vec![51, 51, 51],
size: 0.8,
mono: false,
width: 1,
height: 1,
horizontal_align: HorizontalAlign::Middle,
vertical_align: VerticalAlign::Middle,
}
}
pub fn mono(&mut self) -> &mut Self {
self.mono = true;
return self;
}
pub fn with_color(&mut self, r: u8, g: u8, b: u8) -> &mut Self {
self.color[0] = r;
self.color[1] = g;
self.color[2] = b;
return self;
}
pub fn with_size(&mut self, size: Float) -> &mut Self {
self.size = size;
return self;
}
pub fn with_dimensions(&mut self, width: Int, height: Int) -> &mut Self {
self.width = width;
self.height = height;
return self;
}
pub fn with_align(
&mut self,
horizontal: HorizontalAlign,
vertical: VerticalAlign,
) -> &mut Self {
self.horizontal_align = horizontal;
self.vertical_align = vertical;
return self;
}
pub fn custom_data(&self) -> Vec<u8> {
let mut custom_data = Vec::with_capacity(33);
custom_data.extend(&(self.horizontal_align as Int).to_le_bytes());
custom_data.extend(&self.color);
custom_data.extend(&self.size.to_le_bytes());
custom_data.extend(&[self.mono as u8]);
custom_data.extend(&(self.text.len() as Int).to_le_bytes());
custom_data.extend(self.text.as_bytes());
custom_data.extend(&self.width.to_le_bytes());
custom_data.extend(&self.height.to_le_bytes());
custom_data.extend(&(self.vertical_align as Int).to_le_bytes());
return custom_data;
}
}
impl<T: BinarySerializable> BinarySerializable for Vec<T> {
fn write_to<W: Write>(&self, writer: &mut W) -> io::Result<()> {

Loading…
Cancel
Save