Browse Source

Tweaks, fix pause

pull/633/head
Nikolay Igotti 4 years ago
parent
commit
b9d7d28997
  1. 8
      examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Game.kt
  2. 2
      examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Piece.kt
  3. 2
      examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/fallingBalls.kt
  4. 15
      examples/falling_balls_with_web/src/jsMain/kotlin/androidx/compose/web/with-web/App.kt
  5. 6
      examples/falling_balls_with_web/src/jvmMain/kotlin/App.kt

8
examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Game.kt

@ -42,17 +42,17 @@ abstract class Game {
var numBlocks by mutableStateOf(5)
fun isInBoundaries(pieceData: PieceData): Boolean = pieceData.position < size.height
fun isInBoundaries(pieceData: PieceData) = pieceData.position < size.height
abstract fun saveTime()
abstract fun now(): Long
fun togglePause() {
paused = !paused
saveTime()
previousTime = Long.MAX_VALUE
}
fun start() {
saveTime()
previousTime = now()
startTime = previousTime
clicked = 0
started = true

2
examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/piece.kt → examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Piece.kt

@ -16,7 +16,7 @@ import jetbrains.compose.common.shapes.CircleShape
import org.jetbrains.compose.common.demo.position
@Composable
fun piece(index: Int, piece: PieceData) {
fun Piece(index: Int, piece: PieceData) {
val boxSize = 40.dp
Box(
Modifier

2
examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/fallingBalls.kt

@ -86,7 +86,7 @@ fun fallingBalls(game: Game) {
}
) {
game.pieces.forEachIndexed { index, piece ->
piece(index, piece)
Piece(index, piece)
}
}
}

15
examples/falling_balls_with_web/src/jsMain/kotlin/androidx/compose/web/with-web/App.kt

@ -11,25 +11,20 @@ import androidx.compose.web.css.Style
import org.jetbrains.compose.web.ui.Styles
class JsGame : Game() {
override fun saveTime() {
previousTime = window.performance.now().toLong()
}
override fun now() = window.performance.now().toLong()
}
fun main() {
val root = document.getElementById("root") as HTMLElement
renderComposable(
root = root
) {
renderComposable(root = root) {
Style(Styles)
fallingBalls(
remember {
JsGame()?.apply {
val game = remember {
JsGame().apply {
width = root.offsetWidth
height = root.offsetHeight
}
}
)
fallingBalls(game)
}
}

6
examples/falling_balls_with_web/src/jvmMain/kotlin/App.kt

@ -7,16 +7,14 @@ import org.jetbrains.compose.demo.falling.Game
import androidx.compose.runtime.remember
class JvmGame : Game() {
override fun saveTime() {
previousTime = System.nanoTime()
}
override fun now() = System.nanoTime()
}
fun main() {
Window(title = "Demo", size = IntSize(600, 400)) {
fallingBalls(
remember {
JvmGame()?.apply {
JvmGame().apply {
width = 600
height = 400
}

Loading…
Cancel
Save