use crate::layout::Rect;
use crate::render::Cell;
#[derive(Clone, Debug)]
pub struct OverlayCell {
pub x: u16,
pub y: u16,
pub cell: Cell,
}
#[derive(Clone, Debug)]
pub struct OverlayEntry {
pub z_index: i16,
pub area: Rect,
pub cells: Vec<OverlayCell>,
}
impl OverlayEntry {
pub fn new(z_index: i16, area: Rect) -> Self {
Self {
z_index,
area,
cells: Vec::new(),
}
}
pub fn push(&mut self, x: u16, y: u16, cell: Cell) {
self.cells.push(OverlayCell { x, y, cell });
}
}
#[derive(Default, Debug)]
pub struct OverlayQueue {
entries: Vec<OverlayEntry>,
}
impl OverlayQueue {
pub fn new() -> Self {
Self {
entries: Vec::new(),
}
}
pub fn push(&mut self, entry: OverlayEntry) {
self.entries.push(entry);
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn drain_sorted(&mut self) -> Vec<OverlayEntry> {
let mut entries: Vec<OverlayEntry> = self.entries.drain(..).collect();
entries.sort_by_key(|e| e.z_index);
entries
}
pub fn render_to(&mut self, buffer: &mut crate::render::Buffer) {
let entries = self.drain_sorted();
for entry in entries {
for oc in &entry.cells {
let abs_x = entry.area.x.saturating_add(oc.x);
let abs_y = entry.area.y.saturating_add(oc.y);
buffer.set(abs_x, abs_y, oc.cell);
}
}
}
}