Skip to content

Commit

Permalink
Fix examples for the latest compose-jb version
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousNikhil committed Jan 28, 2022
1 parent 1e45d4b commit 3e52f7b
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/angularmotion/Rotation.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.angularmotion

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
Expand All @@ -13,6 +14,7 @@ import math.constrain
import math.map
import math.toOffSet

@OptIn(ExperimentalComposeUiApi::class)
fun rotateRectangle() = k5 {

var angle = 0f
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/elye/danceYarnAuto.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.elye

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
Expand All @@ -13,6 +14,7 @@ import java.lang.Float.max
import java.lang.Float.min
import kotlin.math.abs

@OptIn(ExperimentalComposeUiApi::class)
fun danceYarnAuto() = k5 {
val loop = 10000
val mouseVector = Vector2D()
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/elye/danceYarnMouse.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.elye

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
Expand All @@ -9,6 +10,7 @@ import k5
import math.Vector2D
import math.noise3D

@OptIn(ExperimentalComposeUiApi::class)
fun danceYarnMouse() = k5 {
val mouseVector = Vector2D()
show(
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/elye/landscapeInspection.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.elye

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
Expand All @@ -9,6 +10,7 @@ import k5
import math.Vector2D
import math.noise2D

@OptIn(ExperimentalComposeUiApi::class)
fun landscapeInspection() {
val loop = 1000
val slices = 250
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/elye/simplegame.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.elye

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerMoveFilter
Expand All @@ -14,6 +15,7 @@ import math.toOffSet

private const val BALL_RADIUS = 30f

@OptIn(ExperimentalComposeUiApi::class)
fun simpleGame() = k5 {
var velocity = 5f
val vehicleY = dimensFloat.height - 250f
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/examples/forces/Ball.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ data class Ball(
var x: Float,
var y: Float,
var m: Float,
var ballMassFactor: Float,
var coeffOfDrag: Float,
var playgroundSize: Size
) {

Expand All @@ -33,7 +35,7 @@ data class Ball(
val velocity = Vector2D(0f, 0f)
val acceleration = Vector2D(0f, 0f)

val mass = m
val mass = m * ballMassFactor

// Create the radius of the object based on the mass. The more the mass, the more the size of the ball
val radius = sqrt(mass) * 10
Expand Down Expand Up @@ -103,7 +105,7 @@ data class Ball(
drag.multiply(-1f)

// Coefficient of drag - depends on medium
val coefficientOfDrag = 0.3f
val coefficientOfDrag = coeffOfDrag

// magSq() returns magnitude square
val speedSq = velocity.magSq()
Expand Down
71 changes: 56 additions & 15 deletions src/main/kotlin/examples/forces/BouncingBall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,43 @@ package examples.forces

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Checkbox
import androidx.compose.material.Slider
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.unit.dp
import k5
import math.Vector2D
import math.multiply
import kotlin.random.Random
import math.random

@OptIn(ExperimentalFoundationApi::class)
fun bouncingBall() = k5 {

var enableWaterDrag by mutableStateOf(false)
var enableFriction by mutableStateOf(false)
var gravity by mutableStateOf(0.2f)
var ballMassFactor by mutableStateOf(1f)
var coeffOfDrag by mutableStateOf(0.3f)

val playgroundSize = dimensFloat

val balls = List(5) {
Ball(
Random.nextInt(2, playgroundSize.width.toInt()).toFloat(),
(2f..dimensFloat.width).random(),
6f,
Random.nextInt(8, 50).toFloat(),
m = (8f..50f).random(),
ballMassFactor = ballMassFactor,
coeffOfDrag = coeffOfDrag,
playgroundSize
)
}
Expand All @@ -39,39 +57,62 @@ fun bouncingBall() = k5 {
)
}

show(
Modifier.combinedClickable(
showWithControls(
modifier = Modifier.combinedClickable(
onClick = {
/**
* On mouse click, apply wind force in right direction
*/
val wind = Vector2D(50f, 0f)
balls.forEach { it.applyForce(wind) }
}
)
) { scope ->
),
controls = {
Row(modifier = Modifier.padding(4.dp)) {
Checkbox(checked = enableWaterDrag, onCheckedChange = { enableWaterDrag = it })
Text("Enable water drag")
}
Row(modifier = Modifier.padding(4.dp)) {
Checkbox(checked = enableFriction, onCheckedChange = { enableFriction = it })
Text("Enable friction force")
}

Text(modifier = Modifier.padding(top = 4.dp), text = "Gravity")
Slider(value = gravity, onValueChange = { gravity = it })

Text(text = "Coefficient of Drag")
Slider(value = coeffOfDrag, onValueChange = { coeffOfDrag = it })

Text(text = "Ball's mass")
Slider(value = ballMassFactor, onValueChange = { ballMassFactor = it })
}
) { scope ->
// Apply gravity force every time on ball
val gravity = Vector2D(0f, 0.2f)
val gravityVector = Vector2D(0f, gravity)

/* Uncomment to apply drag force
/*
* This will create some sort of water and air medium. When ball will hit the water
* it'll experience drag force
*/
// showWater(scope)
if (enableWaterDrag) {
showWater(scope)
}

balls.forEach { ball ->
val weight = gravity.multiply(ball.mass)
val weight = gravityVector.multiply(ball.mass)
ball.applyForce(weight)

/* Uncomment to apply friction force*/
// ball.friction()
if (enableFriction) {
ball.friction()
}

/* Uncomment to apply drag force
/*
* This will create some sort of water and air medium. When ball will hit the water
* it'll experience drag force
*/
// showDrag(ball)
if (enableWaterDrag) {
showDrag(ball)
}

ball.update()
ball.edges()
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/forces/GravitationalAttraction.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.forces

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
Expand Down Expand Up @@ -71,6 +72,7 @@ data class Attractor(val x: Float, val y: Float, val m: Float) {
}
}

@OptIn(ExperimentalComposeUiApi::class)
fun gravitationalPull() = k5 {

val moons = List(15) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/kinematics/SimpleMover.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.kinematics

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -66,6 +67,7 @@ data class Mover(
}
}

@OptIn(ExperimentalComposeUiApi::class)
fun simpleMover() = k5 {

// Create a mover object with initial position
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/mathematics/ribbon.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples.mathematics

import androidx.compose.foundation.background
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
Expand All @@ -10,6 +11,7 @@ import math.map
import kotlin.math.cos
import kotlin.math.sin

@OptIn(ExperimentalComposeUiApi::class)
fun showRibbon() = k5 {

var r = 50f
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/simulations/Chains.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.simulations

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -43,6 +44,7 @@ class Spring(
}
}

@OptIn(ExperimentalComposeUiApi::class)
fun chainLoop() = k5 {
val dimens = dimensFloat
val l1 = Spring(0f, dimens.width / 2)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/simulations/recursive-tree.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples.simulations

import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
Expand All @@ -15,6 +16,7 @@ import k5
* Inspired from https://github.com/V9vek/Fractal-Trees
*/
// Implementation logic may differ
@OptIn(ExperimentalComposeUiApi::class)
fun showRecursiveTree() = k5 {

noLoop()
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/examples/simulations/wavemaker.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package examples.simulations

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
Expand All @@ -9,6 +10,7 @@ import androidx.compose.ui.input.pointer.pointerMoveFilter
import k5
import math.map

@OptIn(ExperimentalComposeUiApi::class)
fun showWaveMaker() = k5 {

var angle = 0f
Expand Down

0 comments on commit 3e52f7b

Please sign in to comment.