use alloc::{collections::VecDeque, string::String};
use crate::{elements::flexbox::FlexBox, style::border};
#[derive(Debug, Clone)]
pub enum RenderCommand<Color>
where
Color: Default + Copy + PartialEq + crate::style::KaolinColor<Color>,
{
DrawRectangle {
id: String,
x: f64,
y: f64,
width: f64,
height: f64,
color: Color,
corner_radius: f32,
border: border::Border<Color>,
},
DrawText {
text: String,
x: f64,
y: f64,
font_id: u32,
font_size: f32,
color: Color,
},
}
impl<Color> PartialEq for RenderCommand<Color>
where
Color: Default + Copy + PartialEq + crate::style::KaolinColor<Color>,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
RenderCommand::DrawRectangle {
x,
y,
width,
height,
color,
corner_radius,
..
},
RenderCommand::DrawRectangle {
x: other_x,
y: other_y,
width: other_width,
height: other_height,
color: other_color,
corner_radius: other_corner_radius,
..
},
) => {
x == other_x
&& y == other_y
&& width == other_width
&& height == other_height
&& color == other_color
&& corner_radius == other_corner_radius
}
(
RenderCommand::DrawText {
text,
x,
y,
font_id,
font_size,
color,
},
RenderCommand::DrawText {
text: other_text,
x: other_x,
y: other_y,
font_id: other_font_id,
font_size: other_font_size,
color: other_color,
},
) => {
text == other_text
&& x == other_x
&& y == other_y
&& font_id == other_font_id
&& font_size == other_font_size
&& color == other_color
}
_ => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RenderCommands<Color>
where
Color: Default + Copy + PartialEq + crate::style::KaolinColor<Color>,
{
commands: VecDeque<RenderCommand<Color>>,
}
impl<Color> RenderCommands<Color>
where
Color: Default + Copy + PartialEq + crate::style::KaolinColor<Color>,
{
pub(crate) fn new(root: FlexBox<Color>) -> Self {
let children = root.children;
RenderCommands {
commands: children.render_nodes().collect::<VecDeque<_>>(),
}
}
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
pub fn len(&self) -> usize {
self.commands.len()
}
}
impl<Color> Iterator for RenderCommands<Color>
where
Color: Default + Copy + PartialEq + crate::style::KaolinColor<Color>,
{
type Item = RenderCommand<Color>;
fn next(&mut self) -> Option<Self::Item> {
self.commands.pop_front()
}
}