Skip to content

Commit

Permalink
Regular stars weren't rendered as expected after introducing Optimisi…
Browse files Browse the repository at this point in the history
…ngLayout
  • Loading branch information
mmartosdev committed Oct 17, 2023
1 parent a2d04c4 commit 0471aea
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
Expand All @@ -34,6 +35,7 @@ import com.bumble.puzzyx.model.Entry
import com.bumble.puzzyx.model.entries
import com.bumble.puzzyx.ui.appyx_dark
import kotlinx.coroutines.isActive
import org.jetbrains.skia.svg.SVGPreserveAspectRatio
import kotlin.math.max
import kotlin.math.roundToInt
import kotlin.random.Random
Expand All @@ -56,21 +58,23 @@ private data class Star(
val xCoord: Float = Random.nextDouble(-0.5, 0.5).toFloat(),
val yCoord: Float = Random.nextDouble(-0.5, 0.5).toFloat(),
val zCoord: Float,
val size: Modifier,
val sizeDp: Dp,
val aspectRatio: Float = 1f,
val type: StarType,
)

@Immutable
private sealed class StarType {
data class RegularType(val color: Color) : StarType() {
companion object {
val size: Modifier = Modifier.size(4.dp)
val sizeDp: Dp = 4.dp
}
}

data class EntryType(val entry: Entry) : StarType() {
companion object {
val size: Modifier = Modifier.fillMaxSize(0.15f).aspectRatio(1.5f)
val sizeDp: Dp = 260.dp
const val aspectRatio: Float = 1.5f
}
}

Expand Down Expand Up @@ -102,7 +106,7 @@ private data class StarField(
from = starFieldSpecs.zNewCoord.toDouble(),
until = starFieldSpecs.zFadeOutEnd.toDouble(),
).toFloat(),
size = StarType.RegularType.size,
sizeDp = StarType.RegularType.sizeDp,
type = StarType.RegularType(
color = Color(
red = Random.nextDouble(0.60, 0.66).toFloat(),
Expand All @@ -117,7 +121,8 @@ private data class StarField(
entries.reversed().mapIndexed { index, entry ->
Star(
zCoord = starFieldSpecs.zFadeInStart - index * starFieldSpecs.zOffset,
size = StarType.EntryType.size,
sizeDp = StarType.EntryType.sizeDp,
aspectRatio = StarType.EntryType.aspectRatio,
type = StarType.EntryType(entry = entry),
)
}
Expand Down Expand Up @@ -193,10 +198,11 @@ private fun StarFieldContent(
val alpha = starField.specs.calcAlpha(zPos)
if (alpha > 0f) {
OptimisingLayout(
optimalWidth = 200.dp,
optimalWidth = star.sizeDp,
modifier = Modifier
.scale(zPos)
.then(star.size)
.size(star.sizeDp)
.aspectRatio(star.aspectRatio)
.align(Alignment.Center)
.absoluteOffset {
IntOffset(
Expand Down

0 comments on commit 0471aea

Please sign in to comment.