Skip to content

Instantly share code, notes, and snippets.

data class SquareRenderProperties(
val position: Position,
val isHighlighted: Boolean,
val clickable: Boolean,
val onClick: () -> Unit,
val isPossibleMoveWithoutCapture: Boolean,
val isPossibleCapture: Boolean,
val boardProperties: BoardRenderProperties
)
override fun dataPointAt(position: Position, state: GameSnapshotState): Datapoint? =
valueAt(position, state)?.let { value ->
Datapoint(
value = value,
label = null,
colorScale = when (value) {
0 -> Color.Red.copy(alpha = 0.35f) to Color.Unspecified
else -> Color.Unspecified to Color.Unspecified
},
)
@Parcelize
object KnightsMoveCount : DatasetVisualisation {
override val name = R.string.viz_knight_move_count
override val minValue: Int = 2
override val maxValue: Int = 8
override fun dataPointAt(position: Position, state: GameSnapshotState): Datapoint {
object DatasetVisualiser : SquareDecoration {
@Composable
override fun render(properties: SquareRenderProperties) {
ActiveDatasetVisualisation.current.let { viz ->
val datapoint = viz.dataPointAt(
properties.position,
properties.boardProperties.toState
)
interface DatasetVisualisation : Parcelable {
// String resource
val name: Int
// Global minimum value
val minValue: Int
// Global maximum value
val maxValue: Int
val offset = remember { Animatable(currentOffset ?: targetOffset, Offset.VectorConverter) }
LaunchedEffect(targetOffset) {
offset.animateTo(targetOffset, tween(100, easing = LinearEasing))
}
LaunchedEffect(isFlipped) {
offset.snapTo(targetOffset)
}
val offset = remember { Animatable(currentOffset ?: targetOffset, Offset.VectorConverter) }
LaunchedEffect(targetOffset) {
offset.animateTo(targetOffset, tween(100, easing = LinearEasing))
}
Piece(
piece = piece,
modifier = offset.value.toModifier()
)
val fromPosition = properties.fromState.board.find(piece)?.position
val currentOffset = fromPosition
?.toCoordinate(properties.isFlipped)
?.toOffset(properties.squareSize)
val targetOffset = toPosition
.toCoordinate(properties.isFlipped)
.toOffset(properties.squareSize)
BoxWithConstraints(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
) {
val squareSize = maxWidth / 8
}
private fun Modifier.pill(): Modifier =
this.background(
color = someColour,
shape = RoundedCornerShape(6.dp)
)