From 6f614c954c61ec8d59fe6f38244c6e0a589b9539 Mon Sep 17 00:00:00 2001 From: andreykovalev Date: Thu, 12 Oct 2023 09:06:13 +0100 Subject: [PATCH] Make ScaledLayout find automatic scale factor --- .../com/bumble/puzzyx/composable/EntryCard.kt | 30 +++++++---- .../bumble/puzzyx/composable/ScaledLayout.kt | 50 ++++++++++++++++--- .../composable/StarFieldMessageBoard.kt | 30 +++++------ .../kotlin/com/bumble/puzzyx/model/Entry.kt | 2 + .../kotlin/com/bumble/puzzyx/model/Puzzle.kt | 3 ++ 5 files changed, 83 insertions(+), 32 deletions(-) diff --git a/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/EntryCard.kt b/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/EntryCard.kt index 88c7e8f5..05b0a231 100644 --- a/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/EntryCard.kt +++ b/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/EntryCard.kt @@ -2,6 +2,7 @@ package com.bumble.puzzyx.composable import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer @@ -99,16 +100,27 @@ fun EntryCardSmall( entry: Entry.Text, modifier: Modifier = Modifier, ) { - ScaledLayout( - modifier = modifier.padding(8.dp), - scale = 0.5f - ) { - Column( - modifier = Modifier - .fillMaxSize() + BoxWithConstraints { + val padding = remember(maxWidth) { + when { + maxWidth < 20.dp -> 0.dp + maxWidth < 40.dp -> 2.dp + maxWidth < 60.dp -> 4.dp + maxWidth < 80.dp -> 6.dp + else -> 8.dp + } + } + ScaledLayout( + modifier = modifier + .padding(padding), ) { - Text(entry.githubUserName) - Text(entry.message) + Column( + modifier = Modifier + .fillMaxSize() + ) { + Text(entry.githubUserName) + Text(entry.message) + } } } } diff --git a/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/ScaledLayout.kt b/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/ScaledLayout.kt index 2161502f..84859095 100644 --- a/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/ScaledLayout.kt +++ b/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/ScaledLayout.kt @@ -1,28 +1,62 @@ package com.bumble.puzzyx.composable import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.UiComposable import androidx.compose.ui.graphics.TransformOrigin import androidx.compose.ui.layout.Layout +import androidx.compose.ui.unit.Constraints.Companion.Infinity import kotlin.math.roundToInt +import kotlin.math.sqrt @Composable internal fun ScaledLayout( modifier: Modifier = Modifier, - scale: Float = 1f, content: @Composable @UiComposable () -> Unit ) { + var scale by remember { mutableStateOf(1f) } + + Layout( + content = content, + modifier = modifier + ) { measurables, constraints -> + val placeables = measurables.map { measurable -> + measurable.measure( + constraints.copy( + maxWidth = constraints.maxWidth, + maxHeight = Infinity + ) + ) + } + val measuredHeight = placeables.maxOf { it.measuredHeight } + val maxHeight = constraints.maxHeight.toFloat() + scale = if (measuredHeight > maxHeight) { + (measuredHeight / maxHeight) * 1.5f + } else { + 1f + } + + layout( + constraints.maxWidth, + constraints.maxHeight + ) { } + } + Layout( content = content, modifier = modifier ) { measurables, constraints -> - val scaledUpConstraints = constraints.copy( - maxWidth = (constraints.maxWidth / scale).roundToInt(), - maxHeight = (constraints.maxHeight / scale).roundToInt() - ) val placeables = measurables.map { measurable -> - measurable.measure(scaledUpConstraints) + measurable.measure( + constraints.copy( + maxWidth = (constraints.maxWidth * sqrt(scale)).roundToInt(), + maxHeight = (constraints.maxHeight * sqrt(scale)).roundToInt() + ) + ) } layout( @@ -34,8 +68,8 @@ internal fun ScaledLayout( x = 0, y = 0 ) { - scaleX = scale - scaleY = scale + scaleX = 1 / sqrt(scale) + scaleY = 1 / sqrt(scale) transformOrigin = TransformOrigin(0f, 0f) } } diff --git a/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/StarFieldMessageBoard.kt b/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/StarFieldMessageBoard.kt index adb5f968..8a4fa0ad 100644 --- a/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/StarFieldMessageBoard.kt +++ b/shared/src/commonMain/kotlin/com/bumble/puzzyx/composable/StarFieldMessageBoard.kt @@ -192,21 +192,21 @@ private fun StarFieldContent( val yPos = star.yCoord * zPos val alpha = starField.specs.calcAlpha(zPos) if (alpha > 0f) { - StarContent( - star.type, - modifier = Modifier - .scale(zPos) - .then(star.size) - .align(Alignment.Center) - .absoluteOffset { - IntOffset( - x = (size.width * xPos).roundToInt(), - y = (size.height * yPos).roundToInt(), - ) - } - .alpha(alpha) - .zIndex(zPos) - ) + ScaledLayout(modifier = Modifier + .scale(zPos) + .then(star.size) + .align(Alignment.Center) + .absoluteOffset { + IntOffset( + x = (size.width * xPos).roundToInt(), + y = (size.height * yPos).roundToInt(), + ) + } + .alpha(alpha) + .zIndex(zPos)) { + + StarContent(star.type) + } } } } diff --git a/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Entry.kt b/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Entry.kt index 26bbeca7..63716f93 100644 --- a/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Entry.kt +++ b/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Entry.kt @@ -1,8 +1,10 @@ package com.bumble.puzzyx.model import androidx.compose.runtime.Composable +import androidx.compose.runtime.Immutable import androidx.compose.ui.layout.ContentScale +@Immutable sealed class Entry { abstract val puzzle: Puzzle abstract val githubUserName: String diff --git a/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Puzzle.kt b/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Puzzle.kt index 10b7d6af..8ef8f79a 100644 --- a/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Puzzle.kt +++ b/shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Puzzle.kt @@ -1,5 +1,8 @@ package com.bumble.puzzyx.model +import androidx.compose.runtime.Immutable + +@Immutable enum class Puzzle( val imagesDir: String, val columns: Int,