whiskers.git / android / app / src / main / kotlin / app / whiskers / art / GreyCat.kt
GreyCat.kt34 lines · 1.4 KB · raw
1package app.whiskers.art
2
3import androidx.compose.ui.geometry.Offset
4import androidx.compose.ui.geometry.Size
5import androidx.compose.ui.graphics.Color
6import androidx.compose.ui.graphics.Path
7import androidx.compose.ui.graphics.drawscope.DrawScope
8import app.whiskers.CatState
9import app.whiskers.MouthShape
10import kotlin.math.abs
11
12/**
13 * Draws the grey cat on the grid the caller has set up (the same one the ginger cat is drawn
14 * on). The cat is `crates/whiskers-art`'s data (GreyScenes.kt, generated); this only plays it at
15 * [t] seconds with the eyes where [state] says.
16 */
17internal fun DrawScope.paintGrey(state: CatState, t: Float, mouth: MouthShape?) {
18    for (q in flatten(GreyScenes.scene(state.mood, mouth), t, state.gazeX, state.gazeY)) {
19        val color = Color(0xFF000000.toInt() or q.rgb).copy(alpha = q.alpha.coerceIn(0f, 1f))
20        if (q.isAxisAligned()) {
21            // 0.02 of overlap hides the hairline AA leaves between touching rects.
22            drawRect(color, Offset(q.xs[0], q.ys[0]), Size(q.xs[2] - q.xs[0] + 0.02f, q.ys[2] - q.ys[0] + 0.02f))
23        } else {
24            val path = Path().apply {
25                moveTo(q.xs[0], q.ys[0])
26                for (i in 1..3) lineTo(q.xs[i], q.ys[i])
27                close()
28            }
29            drawPath(path, color)
30        }
31    }
32}
33
34private fun Quad.isAxisAligned() = abs(ys[0] - ys[1]) < 1e-4f && abs(xs[0] - xs[3]) < 1e-4f