From 188abdfabddc9cf28c081516196bb48345b8033e Mon Sep 17 00:00:00 2001 From: Igor Demin Date: Tue, 11 Oct 2022 19:54:25 +0200 Subject: [PATCH] Refactor visual-effects and notepad --- .../src/main/kotlin/common/AppResources.kt | 1 + .../visual-effects/src/main/kotlin/HappyNY.kt | 40 +++++++++---------- .../src/main/kotlin/WaveEffect.kt | 40 ++++++++++++------- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/examples/notepad/src/main/kotlin/common/AppResources.kt b/examples/notepad/src/main/kotlin/common/AppResources.kt index a1fd633023..865e53e040 100644 --- a/examples/notepad/src/main/kotlin/common/AppResources.kt +++ b/examples/notepad/src/main/kotlin/common/AppResources.kt @@ -34,5 +34,6 @@ fun rememberVectorPainter(image: ImageVector, tintColor: Color) = name = image.name, tintColor = tintColor, tintBlendMode = image.tintBlendMode, + autoMirror = false, content = { _, _ -> RenderVectorGroup(group = image.root) } ) diff --git a/examples/visual-effects/src/main/kotlin/HappyNY.kt b/examples/visual-effects/src/main/kotlin/HappyNY.kt index 74bd53ddc8..ca1d797055 100644 --- a/examples/visual-effects/src/main/kotlin/HappyNY.kt +++ b/examples/visual-effects/src/main/kotlin/HappyNY.kt @@ -23,11 +23,11 @@ import java.lang.Math.random import kotlin.math.* import kotlin.random.Random -val width = 1200 -val height = 800 -val snowCount = 80 -val starCount = 60 -val rocketPartsCount = 30 +const val width = 1200 +const val height = 800 +const val snowCount = 80 +const val starCount = 60 +const val rocketPartsCount = 30 data class SnowFlake( var x: Dp, @@ -42,14 +42,14 @@ data class SnowFlake( data class Star(val x: Dp, val y: Dp, val color: Color, val size: Dp) -val HNYString = "Happy New Year!" +const val HNYString = "Happy New Year!" class DoubleRocket(val particle: Particle) { - val STATE_ROCKET = 0 - val STATE_SMALL_ROCKETS = 1 + private val STATE_ROCKET = 0 + private val STATE_SMALL_ROCKETS = 1 var state = STATE_ROCKET var rockets: Array = emptyArray() - fun checkState(time: Long) { + private fun checkState(time: Long) { if (particle.vy > -3.0 && state == STATE_ROCKET) { explode(time) } @@ -69,7 +69,7 @@ class DoubleRocket(val particle: Particle) { } } - fun reset() { + private fun reset() { if (particle.vx < 0) return //to stop drawing after the second rocket. This could be commented out state = STATE_ROCKET particle.x = if (particle.vx > 0) width - 0.0 else 0.0 @@ -78,9 +78,9 @@ class DoubleRocket(val particle: Particle) { particle.vy = -12.5 } - fun explode(time: Long) { + private fun explode(time: Long) { val colors = arrayOf(Color(0xff, 0, 0), Color(192, 255, 192), Color(192, 212, 255)) - rockets = Array(7) { + rockets = Array(7) { val v = 1.2f + 1.0 * random() val angle = 2 * PI * random() Rocket( @@ -131,8 +131,8 @@ class Rocket(val particle: Particle, val color: Color, val startTime: Long = 0) } } - fun explode() { - parts = Array(rocketPartsCount) { + private fun explode() { + parts = Array(rocketPartsCount) { val v = 0.5f + 1.5 * random() val angle = 2 * PI * random() Particle(particle.x, particle.y, v * sin(angle) + particle.vx, v * cos(angle) + particle.vy, color, 1) @@ -202,7 +202,7 @@ val rocket = DoubleRocket(Particle(0.0, 1000.0, 2.1, -12.5, Color.White)) @Composable fun NYWindow(onCloseRequest: () -> Unit) { val windowState = remember { WindowState(width = width.dp, height = height.dp) } - Window(onCloseRequest = {}, undecorated = true, transparent = true, state = windowState) { + Window(onCloseRequest = onCloseRequest, undecorated = true, transparent = true, state = windowState) { NYContent() } } @@ -249,9 +249,7 @@ fun NYContent() { remember { prepareStarsAndSnowFlakes(stars, snowFlakes) } Surface( - modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp)) - .pointerMoveFilter(onMove = { false; }, - onEnter = { false; }, onExit = { false; }), + modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp)), color = Color.Black, shape = RoundedCornerShape(20.dp) ) { @@ -266,7 +264,7 @@ fun NYContent() { } if (!started) { //animation starts with delay, so there is some time to start recording - if (time - startTime > 7000000000 && time - startTime < 7100000000) println("ready!") + if (time - startTime in 7000000001..7099999999) println("ready!") if (time - startTime > 10000000000) { startTime = time //restarting timer started = true @@ -303,7 +301,7 @@ fun NYContent() { color = Color.White ) - if (started) { //delay to be able start recording + if (started) { //delay to be able to start recording //HNY var i = 0 val angle = (HNYString.length / 2 * 5) * -1.0f @@ -370,7 +368,7 @@ fun flickeringAlpha(time: Long): Float { val time = (time / 10000000) % 100 var result = 0.2f if (time > 75) { - result = result + 0.6f * ((time - 75) % 3) / 3 + result += 0.6f * ((time - 75) % 3) / 3 } return result } diff --git a/examples/visual-effects/src/main/kotlin/WaveEffect.kt b/examples/visual-effects/src/main/kotlin/WaveEffect.kt index c311f12519..1f09d139e9 100644 --- a/examples/visual-effects/src/main/kotlin/WaveEffect.kt +++ b/examples/visual-effects/src/main/kotlin/WaveEffect.kt @@ -10,6 +10,8 @@ import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.draw.* import androidx.compose.ui.graphics.Color +import androidx.compose.ui.input.pointer.PointerEventType +import androidx.compose.ui.input.pointer.onPointerEvent import androidx.compose.ui.input.pointer.pointerMoveFilter import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp @@ -20,7 +22,7 @@ import kotlin.math.* @Composable fun WaveEffect(onCloseRequest: () -> Unit, showControls: Boolean) { - var windowState = remember { WindowState(width = 1200.dp, height = 800.dp) } + val windowState = remember { WindowState(width = 1200.dp, height = 800.dp) } Window(onCloseRequest = {}, undecorated = true, transparent = true, state = windowState) { Grid() } @@ -70,8 +72,16 @@ fun Grid() { Surface( modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp)) - .pointerMoveFilter(onMove = { mouseX = it.x.toInt(); mouseY = it.y.toInt(); false; }, - onEnter = { State.mouseUsed = true; false; }, onExit = { State.mouseUsed = false; false; }), + .onPointerEvent(PointerEventType.Move) { + mouseX = it.changes.first().position.x.toInt() + mouseY = it.changes.first().position.y.toInt() + } + .onPointerEvent(PointerEventType.Enter) { + State.mouseUsed = true + } + .onPointerEvent(PointerEventType.Exit) { + State.mouseUsed = false + }, color = Color(0, 0, 0), shape = RoundedCornerShape(20.dp) ) { @@ -80,13 +90,13 @@ fun Grid() { var y = 10 // initial position val shift = 25 var evenRow = false - var pointerOffsetX = (centerX / 2) - var pointerOffsety = (centerY / 2) + val pointerOffsetX = (centerX / 2) + val pointerOffsety = (centerY / 2) while (y < 790) { x = if (evenRow) 10 + shift else 10 while (x < 1190) { - var size: Int = size(x, y, pointerOffsetX, pointerOffsety) - var color = boxColor(x, y, time, pointerOffsetX, pointerOffsety) + val size: Int = size(x, y, pointerOffsetX, pointerOffsety) + val color = boxColor(x, y, time, pointerOffsetX, pointerOffsety) Dot(size, Modifier.offset(x.dp, y.dp), color, time) x += shift * 2 } @@ -111,19 +121,19 @@ fun Grid() { fun HighPanel(mouseX: Int, mouseY: Int) { Text( "Compose", - androidx.compose.ui.Modifier.offset(270.dp, 600.dp).scale(7.0f).alpha(alpha(mouseX, mouseY, 270, 700)), + Modifier.offset(270.dp, 600.dp).scale(7.0f).alpha(alpha(mouseX, mouseY, 270, 700)), color = colorMouse(mouseX, mouseY, 270, 700), fontWeight = FontWeight.Bold ) Text( "Multiplatform", - androidx.compose.ui.Modifier.offset(350.dp, 700.dp).scale(7.0f).alpha(alpha(mouseX, mouseY, 550, 800)), + Modifier.offset(350.dp, 700.dp).scale(7.0f).alpha(alpha(mouseX, mouseY, 550, 800)), color = colorMouse(mouseX, mouseY, 550, 800), fontWeight = FontWeight.Bold ) Text( "1.0", - androidx.compose.ui.Modifier.offset(800.dp, 700.dp).scale(7.0f).alpha(alpha(mouseX, mouseY, 800, 800)), + Modifier.offset(800.dp, 700.dp).scale(7.0f).alpha(alpha(mouseX, mouseY, 800, 800)), color = colorMouse(mouseX, mouseY, 800, 800), fontWeight = FontWeight.Bold ) @@ -137,7 +147,7 @@ private fun alpha(mouseX: Int, mouseY: Int, x: Int, y: Int): Float { } private fun colorMouse(mouseX: Int, mouseY: Int, x: Int, y: Int): Color { - var d = distance(mouseX, mouseY, x, y) / 450 + val d = distance(mouseX, mouseY, x, y) / 450 val color1 = Color(0x6B, 0x57, 0xFF) val color2 = Color(0xFE, 0x28, 0x57) val color3 = Color(0xFD, 0xB6, 0x0D) @@ -178,7 +188,7 @@ private fun size(x: Int, y: Int, mouseX: Int, mouseY: Int): Int { var result = 5 if (y > 550 && x < 550) return result if (y > 650 && x < 900) return result - var distance2 = sqrt((x - mouseX) * (x - mouseX) + (y - mouseY) * (y - mouseY).toDouble()) / 200 + val distance2 = sqrt((x - mouseX) * (x - mouseX) + (y - mouseY) * (y - mouseY).toDouble()) / 200 val scale: Double = (if (distance2 < 1) { addSize * (1 - distance2) } else 0.toDouble()) @@ -205,13 +215,13 @@ private fun boxColor(x: Int, y: Int, time: Long, mouseX: Int, mouseY: Int): Colo var color = Color.White if (c1 <= 0) { - var d = c2 / (c2 + c3) + val d = c2 / (c2 + c3) color = balancedColor(d, color2, color3) } else if (c2 <= 0) { - var d = c3 / (c1 + c3) + val d = c3 / (c1 + c3) color = balancedColor(d, color3, color1) } else if (c3 <= 0) { - var d = c1 / (c1 + c2) + val d = c1 / (c1 + c2) color = balancedColor(d, color1, color2) }