r/interpreter/grid/display.rs
1//! Grid display list — records the sequence of viewport and drawing operations.
2//!
3//! The display list is the grid equivalent of a "scene graph". It records
4//! push/pop viewport operations and grob draws in order, so the entire
5//! scene can be replayed through any renderer (SVG, egui, PDF, etc.).
6
7use super::grob::GrobId;
8use super::viewport::Viewport;
9
10// region: DisplayItem
11
12/// A single item in the display list.
13#[derive(Clone, Debug)]
14pub enum DisplayItem {
15 /// Push a viewport onto the viewport stack.
16 PushViewport(Box<Viewport>),
17 /// Pop the current viewport (return to parent).
18 PopViewport,
19 /// Draw a grob (referenced by its store ID).
20 Draw(GrobId),
21}
22
23// endregion
24
25// region: DisplayList
26
27/// An ordered list of display items that records the full scene.
28///
29/// Can be replayed through a `GridRenderer` to produce output.
30#[derive(Clone, Debug)]
31pub struct DisplayList {
32 items: Vec<DisplayItem>,
33}
34
35impl DisplayList {
36 /// Create a new empty display list.
37 pub fn new() -> Self {
38 DisplayList { items: Vec::new() }
39 }
40
41 /// Record a display item.
42 pub fn record(&mut self, item: DisplayItem) {
43 self.items.push(item);
44 }
45
46 /// Clear all recorded items.
47 pub fn clear(&mut self) {
48 self.items.clear();
49 }
50
51 /// Return a slice of all recorded items.
52 pub fn items(&self) -> &[DisplayItem] {
53 &self.items
54 }
55
56 /// Return the number of recorded items.
57 pub fn len(&self) -> usize {
58 self.items.len()
59 }
60
61 /// Whether the display list is empty.
62 pub fn is_empty(&self) -> bool {
63 self.items.is_empty()
64 }
65}
66
67impl Default for DisplayList {
68 fn default() -> Self {
69 Self::new()
70 }
71}
72
73// endregion