devpage_design/tiles/
mod.rs1use 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
17pub const TILE: u32 = 256;
19
20pub 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
26pub 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#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
40pub struct Level(pub i64);
41
42impl Level {
43 pub fn of(scale: f64) -> Level {
45 Level((scale * 1024.0).round().max(1.0) as i64)
46 }
47
48 pub fn scale(self) -> f64 {
50 self.0 as f64 / 1024.0
51 }
52}
53
54#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
57pub struct Key {
58 pub theme: Theme,
60 pub level: Level,
62 pub col: u32,
64 pub row: u32,
66}
67
68impl Key {
69 pub fn rect(&self, origin: (f64, f64)) -> KRect {
72 square(origin, self.level.scale(), self.col, self.row)
73 }
74
75 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
85pub trait Tiled: Copy + Eq + std::hash::Hash {
88 fn key(&self) -> Key;
90}
91
92impl Tiled for Key {
93 fn key(&self) -> Key {
94 *self
95 }
96}
97
98#[derive(Clone, Copy, Debug)]
101pub struct View {
102 pub width: f64,
104 pub height: f64,
106 pub zoom: f64,
108 pub origin: (f64, f64),
110}
111
112impl View {
113 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 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;