use crate::layout::content::BuilderState;
use crate::sugarloaf::graphics::GraphicOverlay;
use smallvec::SmallVec;
#[derive(Debug, Clone)]
pub enum ContentData {
Text(BuilderState),
Rect {
x: f32,
y: f32,
width: f32,
height: f32,
color: [f32; 4],
depth: f32,
},
RoundedRect {
x: f32,
y: f32,
width: f32,
height: f32,
color: [f32; 4],
depth: f32,
border_radius: f32,
},
Line {
x1: f32,
y1: f32,
x2: f32,
y2: f32,
width: f32,
color: [f32; 4],
depth: f32,
},
Triangle {
points: [(f32, f32); 3],
color: [f32; 4],
depth: f32,
},
Polygon {
points: SmallVec<[(f32, f32); 8]>,
color: [f32; 4],
depth: f32,
},
Arc {
center_x: f32,
center_y: f32,
radius: f32,
start_angle: f32,
end_angle: f32,
stroke_width: f32,
color: [f32; 4],
depth: f32,
},
Image {
x: f32,
y: f32,
width: f32,
height: f32,
color: [f32; 4],
coords: [f32; 4],
depth: f32,
atlas_layer: i32,
},
}
#[derive(Debug, Clone)]
pub struct ContentRenderData {
pub position: [f32; 2],
pub depth: f32,
pub order: u8,
pub hidden: bool,
pub needs_repaint: bool,
pub should_remove: bool,
pub transient: bool,
pub use_grid_cell_size: bool,
pub bounds: Option<[f32; 4]>,
}
impl Default for ContentRenderData {
fn default() -> Self {
Self {
position: [0.0, 0.0],
depth: 0.0,
order: 0,
hidden: false,
needs_repaint: true, should_remove: false,
transient: false,
use_grid_cell_size: true, bounds: None,
}
}
}
impl ContentRenderData {
pub fn set_position(&mut self, x: f32, y: f32) {
if self.position[0] != x || self.position[1] != y {
self.position = [x, y];
self.needs_repaint = true;
}
}
pub fn set_hidden(&mut self, hidden: bool) {
if self.hidden != hidden {
self.hidden = hidden;
self.needs_repaint = true;
}
}
pub fn set_bounds(&mut self, bounds: Option<[f32; 4]>) {
if self.bounds != bounds {
self.bounds = bounds;
self.needs_repaint = true;
}
}
pub fn mark_for_removal(&mut self) {
self.should_remove = true;
}
pub fn clear_repaint_flag(&mut self) {
self.needs_repaint = false;
}
}
#[derive(Debug, Clone)]
pub struct ContentState {
pub data: ContentData,
pub render_data: ContentRenderData,
pub image_overlays: SmallVec<[GraphicOverlay; 4]>,
}
impl ContentState {
pub fn new(data: ContentData) -> Self {
Self {
data,
render_data: ContentRenderData::default(),
image_overlays: SmallVec::new(),
}
}
pub fn is_text(&self) -> bool {
matches!(self.data, ContentData::Text(_))
}
pub fn as_text(&self) -> Option<&BuilderState> {
match &self.data {
ContentData::Text(state) => Some(state),
_ => None,
}
}
pub fn as_text_mut(&mut self) -> Option<&mut BuilderState> {
match &mut self.data {
ContentData::Text(state) => Some(state),
_ => None,
}
}
}