#![allow(non_snake_case)]
use crate::config::Config;
use crate::error::CustomError;
use circle::Circle;
use image::{ImageBuffer, Rgba};
use line::Line;
use rectangle::Rectangle;
use std::error::Error;
use text::Text;
mod circle;
mod line;
mod rectangle;
mod text;
pub trait ComponentTrait {
fn draw(
&self,
config: Config,
buffer: &mut ImageBuffer<Rgba<u8>, Vec<u8>>,
) -> Result<(), Box<dyn Error>>;
}
pub enum Component {
Circle {
cx: u32,
cy: u32,
r: u32,
color: Rgba<u8>,
},
Rectangle {
h: u32,
w: u32,
x: u32,
y: u32,
color: Rgba<u8>,
},
Line {
x1: u32,
y1: u32,
x2: u32,
y2: u32,
color: Rgba<u8>,
},
Text {
x: u32,
y: u32,
size: u32,
text: &'static str,
color: Rgba<u8>,
border: Option<(Rgba<u8>, u32)>,
},
}
pub struct Components;
impl Components {
pub fn Circle(cx: u32, cy: u32, r: u32, color: Rgba<u8>) -> Component {
Component::Circle { cx, cy, r, color }
}
pub fn Rectangle(h: u32, w: u32, x: u32, y: u32, color: Rgba<u8>) -> Component {
Component::Rectangle { h, w, x, y, color }
}
pub fn Line(x1: u32, y1: u32, x2: u32, y2: u32, color: Rgba<u8>) -> Component {
Component::Line {
x1,
y1,
x2,
y2,
color,
}
}
pub fn Text(
x: u32,
y: u32,
size: u32,
text: &'static str,
color: Rgba<u8>,
border: Option<(Rgba<u8>, u32)>,
) -> Component {
Component::Text {
x,
y,
size,
text,
color,
border,
}
}
}
impl ComponentTrait for Component {
fn draw(
&self,
config: Config,
buffer: &mut ImageBuffer<Rgba<u8>, Vec<u8>>,
) -> Result<(), Box<dyn Error>> {
match *self {
Component::Circle { cx, cy, r, color } => {
let circle = Circle::new(cx, cy, r, color);
circle.draw(config, buffer)
}
Component::Rectangle { h, w, x, y, color } => {
let rectangle = Rectangle::new(h, w, x, y, color);
rectangle.draw(config, buffer)
}
Component::Line {
x1,
y1,
x2,
y2,
color,
} => {
let line = Line::new(x1, y1, x2, y2, color);
line.draw(config, buffer)
}
Component::Text {
x,
y,
size,
text,
color,
border,
} => {
let text = Text::new(x, y, size, text, color, border);
text.draw(config, buffer)
}
}
}
}