Skip to content

Commit

Permalink
Make ScaledLayout find automatic scale factor
Browse files Browse the repository at this point in the history
  • Loading branch information
KovalevAndrey committed Oct 12, 2023
1 parent 57ee894 commit 6f614c9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Entry.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.bumble.puzzyx.model

import androidx.compose.runtime.Immutable

@Immutable
enum class Puzzle(
val imagesDir: String,
val columns: Int,
Expand Down

0 comments on commit 6f614c9

Please sign in to comment.