Created
June 29, 2021 16:32
-
-
Save sergenes/f509c9841c385c57cae136206432e1ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class CardsInDeckAnimation( | |
val paddingOffset: Float, | |
val count: Int = 3 | |
) { | |
private lateinit var scaleCards: List<MutableState<Float>> | |
private lateinit var offsetCards: List<MutableState<Float>> | |
private fun calculateScale(idx: Int): Float { | |
return 1f - idx * (1f / 10f) | |
} | |
private fun calculateOffset(idx: Int): Int { | |
return (paddingOffset * (idx + 1)).toInt() | |
} | |
@Composable | |
fun Init() { | |
scaleCards = remember { | |
List(count) { mutableStateOf(calculateScale(it)) } | |
} | |
offsetCards = remember { | |
List(count) { mutableStateOf(calculateOffset(it).toFloat()) } | |
} | |
} | |
fun scaleX(index: Int): Float { | |
return scaleCards[index].value | |
} | |
fun offset(index: Int): IntOffset { | |
return IntOffset(0, offsetCards[index].value.toInt()) | |
} | |
fun backToInitState() { | |
repeat(count) { | |
scaleCards[it].value = calculateScale(it) | |
offsetCards[it].value = calculateOffset(it).toFloat() | |
} | |
} | |
fun pushBackToTheFront() { | |
repeat(count) { | |
if (it == 0 || it == 1) { | |
scaleCards[it].value = calculateScale(0) | |
offsetCards[it].value = calculateOffset(0).toFloat() | |
} else { | |
scaleCards[it].value = calculateScale(it - 1) | |
offsetCards[it].value = calculateOffset(it - 1).toFloat() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment