From bb9afd0f06c91aeff1271b90349e4f98a45ed363 Mon Sep 17 00:00:00 2001 From: Ellen Spertus Date: Sat, 13 Aug 2022 19:48:28 -0700 Subject: [PATCH] Update Kotlin and Compose versions --- build.gradle.kts | 17 ++++--- .../com/arkivanov/composnake/DefaultGame.kt | 11 +++-- .../kotlin/com/arkivanov/composnake/Main.kt | 44 ++++++++++++++----- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index dad4f1d..7b44f5a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,8 +3,8 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.4.31" - id("org.jetbrains.compose") version "0.3.2" + kotlin("jvm") version "1.6.10" + id("org.jetbrains.compose") version "1.1.0" } group = "me.aivanov" @@ -20,11 +20,18 @@ dependencies { } tasks.withType() { - kotlinOptions.jvmTarget = "11" + kotlinOptions.jvmTarget = "17" } -kotlin.sourceSets.all { - languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn") +subprojects { + val compilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn") + tasks.withType { + kotlinOptions.freeCompilerArgs += compilerArgs + } + + tasks.withType { + kotlinOptions.freeCompilerArgs = compilerArgs + } } compose.desktop { diff --git a/src/main/kotlin/com/arkivanov/composnake/DefaultGame.kt b/src/main/kotlin/com/arkivanov/composnake/DefaultGame.kt index 948d293..d5af68b 100644 --- a/src/main/kotlin/com/arkivanov/composnake/DefaultGame.kt +++ b/src/main/kotlin/com/arkivanov/composnake/DefaultGame.kt @@ -5,15 +5,14 @@ import androidx.compose.runtime.State import androidx.compose.runtime.mutableStateOf class DefaultGame : Game { + val WIDTH = 16 + val HEIGHT = WIDTH private val _board: MutableState private val randomPointGenerator: RandomPointGenerator init { - val width = 16 - val height = 16 - - val cy = height / 2 + val cy = HEIGHT / 2 val snake = setOf( @@ -25,8 +24,8 @@ class DefaultGame : Game { ) val grid = - List(height) { y -> - List(width) { x -> + List(HEIGHT) { y -> + List(WIDTH) { x -> Point(x = x, y = y) } } diff --git a/src/main/kotlin/com/arkivanov/composnake/Main.kt b/src/main/kotlin/com/arkivanov/composnake/Main.kt index 5417e51..7c6144e 100644 --- a/src/main/kotlin/com/arkivanov/composnake/Main.kt +++ b/src/main/kotlin/com/arkivanov/composnake/Main.kt @@ -1,11 +1,8 @@ package com.arkivanov.composnake -import androidx.compose.desktop.Window import androidx.compose.foundation.ExperimentalFoundationApi -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.* import androidx.compose.material.Checkbox import androidx.compose.material.CheckboxDefaults import androidx.compose.material.MaterialTheme @@ -15,30 +12,44 @@ import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment +import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusModifier import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.focusTarget +import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.key.Key import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.onKeyEvent +import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.IntSize +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Window +import androidx.compose.ui.window.WindowState +import androidx.compose.ui.window.application import kotlinx.coroutines.delay import kotlinx.coroutines.isActive -fun main() { +val rowWidth = 400.dp +val rowHeight = rowWidth + +@OptIn(ExperimentalComposeUiApi::class) +fun main() = application { val game: Game = DefaultGame() val focusRequester = FocusRequester() + val delay = 2000L Window( - size = IntSize(width = 500, height = 500), + state = WindowState(size = DpSize.Unspecified), + onCloseRequest = ::exitApplication, title = "CompoSnake" ) { MaterialTheme { LaunchedEffect(Unit) { focusRequester.requestFocus() while (isActive) { - delay(200L) + delay(delay) game.step() } } @@ -46,7 +57,7 @@ fun main() { Box( modifier = Modifier .focusRequester(focusRequester) - .focusModifier() + .focusTarget() .onKeyEvent { when (it.key) { Key.DirectionLeft -> game.setDirection(Direction.LEFT).let { true } @@ -58,8 +69,12 @@ fun main() { } ) - Box( - modifier = Modifier.fillMaxSize(), + Box(modifier = Modifier + .width(rowWidth) + .height(rowHeight) + .focusRequester(focusRequester) + .focusTarget(), + // modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center ) { Board(game.board.value) @@ -81,7 +96,7 @@ private fun Board(board: Board) { Column { board.grid.forEachFast { row -> - Row { + Row(Modifier.width(rowWidth).padding(horizontal = 8.dp)) { row.forEachFast { cell -> when (cell) { board.food -> FoodCell() @@ -94,9 +109,12 @@ private fun Board(board: Board) { } } +val cellModifier = Modifier.width(24.dp).height(24.dp).padding(10.dp) + @Composable private fun FoodCell() { RadioButton( + modifier = cellModifier, selected = true, onClick = {}, colors = RadioButtonDefaults.colors(selectedColor = MaterialTheme.colors.primary) @@ -106,6 +124,7 @@ private fun FoodCell() { @Composable private fun EmptyCell() { Checkbox( + modifier = cellModifier, checked = false, onCheckedChange = {} ) @@ -115,6 +134,7 @@ private fun EmptyCell() { private fun SnakeCell(isHead: Boolean) { Checkbox( checked = true, + modifier = cellModifier, colors = CheckboxDefaults.colors( checkedColor = if (isHead) MaterialTheme.colors.primary else MaterialTheme.colors.secondary ),