Skip to main content

devpage_design/tiles/
mod.rs

1//! Tiles: squares of a drawing, 256 device pixels a side, painted at one
2//! scale in one theme — where each is in the design, the pyramid's levels,
3//! the cache that keeps them and marks the ones damage makes stale, and the
4//! frame plan a browser canvas carries out.
5
6use kurbo::Rect as KRect;
7
8use crate::display::List;
9use crate::scene::Theme;
10
11mod cache;
12mod plan;
13
14pub use cache::*;
15pub use plan::*;
16
17/// A tile's side, in device pixels.
18pub const TILE: u32 = 256;
19
20/// The design-coordinate square of tile `(col, row)` at `scale`, from the
21/// drawing's top-left.
22pub fn tile_rect(list: &List, scale: f64, col: u32, row: u32) -> KRect {
23    square((list.area.x as f64, list.area.y as f64), scale, col, row)
24}
25
26/// The design-coordinate square of tile `(col, row)` at `scale` of a drawing
27/// whose top-left is `origin`.
28pub fn square((x, y): (f64, f64), scale: f64, col: u32, row: u32) -> KRect {
29    let side = TILE as f64 / scale;
30    KRect::new(
31        x + col as f64 * side,
32        y + row as f64 * side,
33        x + (col + 1) as f64 * side,
34        y + (row + 1) as f64 * side,
35    )
36}
37
38/// A scale tiles are painted at, to a 1/1024th: a level of the pyramid.
39#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
40pub struct Level(pub i64);
41
42impl Level {
43    /// The level a scale is painted at.
44    pub fn of(scale: f64) -> Level {
45        Level((scale * 1024.0).round().max(1.0) as i64)
46    }
47
48    /// The scale the level paints at.
49    pub fn scale(self) -> f64 {
50        self.0 as f64 / 1024.0
51    }
52}
53
54/// A tile: its theme, its level, and its column and row from the drawing's
55/// top-left.
56#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
57pub struct Key {
58    /// The theme the tile is painted in.
59    pub theme: Theme,
60    /// The scale the tile is painted at.
61    pub level: Level,
62    /// The tile's column from the drawing's left, counting from 0.
63    pub col: u32,
64    /// The tile's row from the drawing's top, counting from 0.
65    pub row: u32,
66}
67
68impl Key {
69    /// Its square in the design's coordinates, the drawing's top-left at
70    /// `origin`.
71    pub fn rect(&self, origin: (f64, f64)) -> KRect {
72        square(origin, self.level.scale(), self.col, self.row)
73    }
74
75    /// Whether its square, the drawing's top-left at `origin`, overlaps one
76    /// of `rects`, in the design's coordinates.
77    pub fn overlaps(&self, origin: (f64, f64), rects: &[KRect]) -> bool {
78        let r = self.rect(origin);
79        rects
80            .iter()
81            .any(|d| d.x0 < r.x1 && d.x1 > r.x0 && d.y0 < r.y1 && d.y1 > r.y0)
82    }
83}
84
85/// What a cache keys its tiles by: a tile's [`Key`], with whatever else
86/// tells the cache's tiles apart — a server's design, say.
87pub trait Tiled: Copy + Eq + std::hash::Hash {
88    /// The tile's [`Key`], without what else tells it apart.
89    fn key(&self) -> Key;
90}
91
92impl Tiled for Key {
93    fn key(&self) -> Key {
94        *self
95    }
96}
97
98/// A view of a drawing: `width` × `height` device pixels showing the
99/// drawing scaled by `zoom`, its top-left at `origin`, a whole device pixel.
100#[derive(Clone, Copy, Debug)]
101pub struct View {
102    /// The view's width, in device pixels.
103    pub width: f64,
104    /// The view's height, in device pixels.
105    pub height: f64,
106    /// The scale the drawing is shown at.
107    pub zoom: f64,
108    /// Where the drawing's top-left lands in the view, in device pixels.
109    pub origin: (f64, f64),
110}
111
112impl View {
113    /// The tiles at `level` the view shows, as columns and rows of the
114    /// drawing `area` × `level`, nearest the view's middle first.
115    pub fn tiles(&self, area: KRect, level: Level) -> Vec<(u32, u32)> {
116        let side = TILE as f64;
117        let scale = level.scale();
118        let (ox, oy) = self.origin;
119        let count = |v: f64| (v * scale / side).ceil() as i64;
120        let (cols, rows) = (count(area.width()), count(area.height()));
121        let span = |o: f64, n: f64, count: i64| {
122            let first = ((-o) / side).floor().max(0.0) as i64;
123            let last = (((n - o) / side).ceil() as i64).min(count);
124            first..last.max(first)
125        };
126        let (cs, rs) = (span(ox, self.width, cols), span(oy, self.height, rows));
127        let mut out: Vec<(u32, u32)> = rs
128            .flat_map(|r| cs.clone().map(move |c| (c as u32, r as u32)))
129            .collect();
130        let (mx, my) = (
131            (self.width / 2.0 - ox) / side,
132            (self.height / 2.0 - oy) / side,
133        );
134        let d = |p: &(u32, u32)| (p.0 as f64 + 0.5 - mx).powi(2) + (p.1 as f64 + 0.5 - my).powi(2);
135        out.sort_by(|a, b| d(a).total_cmp(&d(b)));
136        out
137    }
138
139    /// Where a tile at any level lands in the view, in device pixels —
140    /// `(x0, y0, x1, y1)` — or `None` when the view doesn't show it.
141    pub fn place(&self, area: KRect, key: &Key) -> Option<KRect> {
142        let rect = square((area.x0, area.y0), key.level.scale(), key.col, key.row);
143        let (ox, oy) = self.origin;
144        let to = |x: f64, y: f64| {
145            (
146                ox + (x - area.x0) * self.zoom,
147                oy + (y - area.y0) * self.zoom,
148            )
149        };
150        let (x0, y0) = to(rect.x0, rect.y0);
151        let (x1, y1) = to(rect.x1, rect.y1);
152        (x1 >= 0.0 && y1 >= 0.0 && x0 <= self.width && y0 <= self.height)
153            .then(|| KRect::new(x0, y0, x1, y1))
154    }
155}
156
157#[cfg(test)]
158mod tests;