Scoop API

scoop:scoop.ui:0.1.0

src/paint/grid.scoop

Selected production source · src/paint/grid.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.math.*
6internal struct PaintTileIndex(val x: Long, val y: Long): Equals<PaintTileIndex>, Hash {
7 public override operator fun equals(other: PaintTileIndex): Boolean =
8 x == other.x && y == other.y
9 public override fun hash(): Long = x.hash() xor (y.hash() shl 1L)
12internal struct PaintTileRange(val left: Long, val top: Long, val right: Long, val bottom: Long) {
13 fun contains(index: PaintTileIndex): Boolean =
14 index.x >= left && index.x < right && index.y >= top && index.y < bottom
17/** Device-sized cells anchored to finite content, independent of scrolling translation. */
18internal struct PaintTileGrid(
19 val scaleX: Double,
20 val scaleY: Double,
21 val tileSize: Int,
22 val overscan: Int,
23 val paddingX: Int,
24 val paddingY: Int,
25 val firstColumn: Long,
26 val firstRow: Long,
27 val columns: Long,
28 val rows: Long
29) {
30 val pixelWidth: Int get() = (tileSize.toLong() + 2L * paddingX.toLong()).toInt()
31 val pixelHeight: Int get() = (tileSize.toLong() + 2L * paddingY.toLong()).toInt()
32 fun core(index: PaintTileIndex): Rect {
33 val x = (firstColumn + index.x).toDouble() * tileSize.toDouble() / scaleX
34 val y = (firstRow + index.y).toDouble() * tileSize.toDouble() / scaleY
35 return Rect(x, y, tileSize.toDouble() / scaleX, tileSize.toDouble() / scaleY)
36 }
37 fun range(bounds: Rect, clip: Rect): PaintTileRange? {
38 val left = max(bounds.x, clip.x)
39 val top = max(bounds.y, clip.y)
40 val right = min(bounds.x + bounds.width, clip.x + clip.width)
41 val bottom = min(bounds.y + bounds.height, clip.y + clip.height)
42 if (left >= right || top >= bottom)
43 return None
44 val extra = overscan.toLong()
45 return Some(
46 PaintTileRange(
47 max(0L, floor(left * scaleX / tileSize.toDouble()).toLong() - firstColumn - extra),
48 max(0L, floor(top * scaleY / tileSize.toDouble()).toLong() - firstRow - extra),
49 min(
50 columns,
51 ceil(right * scaleX / tileSize.toDouble()).toLong() - firstColumn + extra
52 ),
53 min(rows, ceil(bottom * scaleY / tileSize.toDouble()).toLong() - firstRow + extra)
54 )
55 )
56 }
57 fun representable(range: PaintTileRange): Boolean {
58 for (
59 index in [
60 PaintTileIndex(range.left, range.top),
61 PaintTileIndex(range.right - 1L, range.bottom - 1L)
62 ]
63 ) {
64 val rect = core(index)
65 if (
66 !rect.x.toFloat().isFinite() ||
67 !rect.y.toFloat().isFinite() ||
68 !(rect.x + rect.width).toFloat().isFinite() ||
69 !(rect.y + rect.height).toFloat().isFinite() ||
70 (rect.x + rect.width).toFloat() <=
71 rect.x.toFloat() ||
72 (rect.y + rect.height).toFloat() <=
73 rect.y.toFloat()
74 )
75 return false
76 }
77 return true
78 }
81internal fun paintTileGrid(
82 transform: Transform,
83 input: PaintLayerInput,
84 tileSize: Int,
85 overscan: Int
86): PaintTileGrid? {
87 if (transform.b != 0.0 || transform.c != 0.0 || transform.a <= 0.0 || transform.d <= 0.0)
88 return None
89 val x = max(1.0, ceil(input.samplingOutset * transform.a))
90 val y = max(1.0, ceil(input.samplingOutset * transform.d))
91 val width = tileSize.toDouble() + 2.0 * x
92 val height = tileSize.toDouble() + 2.0 * y
93 if (
94 !width.isFinite() ||
95 !height.isFinite() ||
96 width >
97 Int.MAX_VALUE.toDouble() ||
98 height >
99 Int.MAX_VALUE.toDouble() ||
100 width *
101 height >
102 (Long.MAX_VALUE / 4L).toDouble()
103 )
104 return None
105 val left = input.bounds.x * transform.a
106 val top = input.bounds.y * transform.d
107 val right = (input.bounds.x + input.bounds.width) * transform.a
108 val bottom = (input.bounds.y + input.bounds.height) * transform.d
109 // Above the exact integer range, adjacent device pixels cannot define a reliable grid.
110 val exactPixels = 9007199254740991.0
111 for (coordinate in [left, top, right, bottom])
112 if (!coordinate.isFinite() || abs(coordinate) > exactPixels)
113 return None
114 val cellX = tileSize.toDouble() / transform.a
115 val cellY = tileSize.toDouble() / transform.d
116 if (
117 !cellX.toFloat().isFinite() ||
118 !cellY.toFloat().isFinite() ||
119 cellX.toFloat() <=
120 0.0 ||
121 cellY.toFloat() <=
122 0.0
123 )
124 return None
125 val firstColumn = floor(left / tileSize.toDouble()).toLong()
126 val firstRow = floor(top / tileSize.toDouble()).toLong()
127 return Some(
128 PaintTileGrid(
129 transform.a,
130 transform.d,
131 tileSize,
132 overscan,
133 x.toInt(),
134 y.toInt(),
135 firstColumn,
136 firstRow,
137 ceil(right / tileSize.toDouble()).toLong() - firstColumn,
138 ceil(bottom / tileSize.toDouble()).toLong() - firstRow
139 )
140 )