Browse Source

Refactor visual-effects and notepad

release/1.2 v1.2.0
Igor Demin 2 years ago
parent
commit
188abdfabd
  1. 1
      examples/notepad/src/main/kotlin/common/AppResources.kt
  2. 40
      examples/visual-effects/src/main/kotlin/HappyNY.kt
  3. 40
      examples/visual-effects/src/main/kotlin/WaveEffect.kt

1
examples/notepad/src/main/kotlin/common/AppResources.kt

@ -34,5 +34,6 @@ fun rememberVectorPainter(image: ImageVector, tintColor: Color) =
name = image.name, name = image.name,
tintColor = tintColor, tintColor = tintColor,
tintBlendMode = image.tintBlendMode, tintBlendMode = image.tintBlendMode,
autoMirror = false,
content = { _, _ -> RenderVectorGroup(group = image.root) } content = { _, _ -> RenderVectorGroup(group = image.root) }
) )

40
examples/visual-effects/src/main/kotlin/HappyNY.kt

@ -23,11 +23,11 @@ import java.lang.Math.random
import kotlin.math.* import kotlin.math.*
import kotlin.random.Random import kotlin.random.Random
val width = 1200 const val width = 1200
val height = 800 const val height = 800
val snowCount = 80 const val snowCount = 80
val starCount = 60 const val starCount = 60
val rocketPartsCount = 30 const val rocketPartsCount = 30
data class SnowFlake( data class SnowFlake(
var x: Dp, 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) 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) { class DoubleRocket(val particle: Particle) {
val STATE_ROCKET = 0 private val STATE_ROCKET = 0
val STATE_SMALL_ROCKETS = 1 private val STATE_SMALL_ROCKETS = 1
var state = STATE_ROCKET var state = STATE_ROCKET
var rockets: Array<Rocket> = emptyArray() var rockets: Array<Rocket> = emptyArray()
fun checkState(time: Long) { private fun checkState(time: Long) {
if (particle.vy > -3.0 && state == STATE_ROCKET) { if (particle.vy > -3.0 && state == STATE_ROCKET) {
explode(time) 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 if (particle.vx < 0) return //to stop drawing after the second rocket. This could be commented out
state = STATE_ROCKET state = STATE_ROCKET
particle.x = if (particle.vx > 0) width - 0.0 else 0.0 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 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)) val colors = arrayOf(Color(0xff, 0, 0), Color(192, 255, 192), Color(192, 212, 255))
rockets = Array<Rocket>(7) { rockets = Array(7) {
val v = 1.2f + 1.0 * random() val v = 1.2f + 1.0 * random()
val angle = 2 * PI * random() val angle = 2 * PI * random()
Rocket( Rocket(
@ -131,8 +131,8 @@ class Rocket(val particle: Particle, val color: Color, val startTime: Long = 0)
} }
} }
fun explode() { private fun explode() {
parts = Array<Particle>(rocketPartsCount) { parts = Array(rocketPartsCount) {
val v = 0.5f + 1.5 * random() val v = 0.5f + 1.5 * random()
val angle = 2 * PI * random() val angle = 2 * PI * random()
Particle(particle.x, particle.y, v * sin(angle) + particle.vx, v * cos(angle) + particle.vy, color, 1) 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 @Composable
fun NYWindow(onCloseRequest: () -> Unit) { fun NYWindow(onCloseRequest: () -> Unit) {
val windowState = remember { WindowState(width = width.dp, height = height.dp) } 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() NYContent()
} }
} }
@ -249,9 +249,7 @@ fun NYContent() {
remember { prepareStarsAndSnowFlakes(stars, snowFlakes) } remember { prepareStarsAndSnowFlakes(stars, snowFlakes) }
Surface( Surface(
modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp)) modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp)),
.pointerMoveFilter(onMove = { false; },
onEnter = { false; }, onExit = { false; }),
color = Color.Black, color = Color.Black,
shape = RoundedCornerShape(20.dp) 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 (!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) { if (time - startTime > 10000000000) {
startTime = time //restarting timer startTime = time //restarting timer
started = true started = true
@ -303,7 +301,7 @@ fun NYContent() {
color = Color.White color = Color.White
) )
if (started) { //delay to be able start recording if (started) { //delay to be able to start recording
//HNY //HNY
var i = 0 var i = 0
val angle = (HNYString.length / 2 * 5) * -1.0f val angle = (HNYString.length / 2 * 5) * -1.0f
@ -370,7 +368,7 @@ fun flickeringAlpha(time: Long): Float {
val time = (time / 10000000) % 100 val time = (time / 10000000) % 100
var result = 0.2f var result = 0.2f
if (time > 75) { if (time > 75) {
result = result + 0.6f * ((time - 75) % 3) / 3 result += 0.6f * ((time - 75) % 3) / 3
} }
return result return result
} }

40
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.Modifier
import androidx.compose.ui.draw.* import androidx.compose.ui.draw.*
import androidx.compose.ui.graphics.Color 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.input.pointer.pointerMoveFilter
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -20,7 +22,7 @@ import kotlin.math.*
@Composable @Composable
fun WaveEffect(onCloseRequest: () -> Unit, showControls: Boolean) { 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) { Window(onCloseRequest = {}, undecorated = true, transparent = true, state = windowState) {
Grid() Grid()
} }
@ -70,8 +72,16 @@ fun Grid() {
Surface( Surface(
modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp)) modifier = Modifier.fillMaxSize().padding(5.dp).shadow(3.dp, RoundedCornerShape(20.dp))
.pointerMoveFilter(onMove = { mouseX = it.x.toInt(); mouseY = it.y.toInt(); false; }, .onPointerEvent(PointerEventType.Move) {
onEnter = { State.mouseUsed = true; false; }, onExit = { State.mouseUsed = false; false; }), 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), color = Color(0, 0, 0),
shape = RoundedCornerShape(20.dp) shape = RoundedCornerShape(20.dp)
) { ) {
@ -80,13 +90,13 @@ fun Grid() {
var y = 10 // initial position var y = 10 // initial position
val shift = 25 val shift = 25
var evenRow = false var evenRow = false
var pointerOffsetX = (centerX / 2) val pointerOffsetX = (centerX / 2)
var pointerOffsety = (centerY / 2) val pointerOffsety = (centerY / 2)
while (y < 790) { while (y < 790) {
x = if (evenRow) 10 + shift else 10 x = if (evenRow) 10 + shift else 10
while (x < 1190) { while (x < 1190) {
var size: Int = size(x, y, pointerOffsetX, pointerOffsety) val size: Int = size(x, y, pointerOffsetX, pointerOffsety)
var color = boxColor(x, y, time, pointerOffsetX, pointerOffsety) val color = boxColor(x, y, time, pointerOffsetX, pointerOffsety)
Dot(size, Modifier.offset(x.dp, y.dp), color, time) Dot(size, Modifier.offset(x.dp, y.dp), color, time)
x += shift * 2 x += shift * 2
} }
@ -111,19 +121,19 @@ fun Grid() {
fun HighPanel(mouseX: Int, mouseY: Int) { fun HighPanel(mouseX: Int, mouseY: Int) {
Text( Text(
"Compose", "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), color = colorMouse(mouseX, mouseY, 270, 700),
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
"Multiplatform", "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), color = colorMouse(mouseX, mouseY, 550, 800),
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
"1.0", "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), color = colorMouse(mouseX, mouseY, 800, 800),
fontWeight = FontWeight.Bold 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 { 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 color1 = Color(0x6B, 0x57, 0xFF)
val color2 = Color(0xFE, 0x28, 0x57) val color2 = Color(0xFE, 0x28, 0x57)
val color3 = Color(0xFD, 0xB6, 0x0D) 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 var result = 5
if (y > 550 && x < 550) return result if (y > 550 && x < 550) return result
if (y > 650 && x < 900) 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) { val scale: Double = (if (distance2 < 1) {
addSize * (1 - distance2) addSize * (1 - distance2)
} else 0.toDouble()) } 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 var color = Color.White
if (c1 <= 0) { if (c1 <= 0) {
var d = c2 / (c2 + c3) val d = c2 / (c2 + c3)
color = balancedColor(d, color2, color3) color = balancedColor(d, color2, color3)
} else if (c2 <= 0) { } else if (c2 <= 0) {
var d = c3 / (c1 + c3) val d = c3 / (c1 + c3)
color = balancedColor(d, color3, color1) color = balancedColor(d, color3, color1)
} else if (c3 <= 0) { } else if (c3 <= 0) {
var d = c1 / (c1 + c2) val d = c1 / (c1 + c2)
color = balancedColor(d, color1, color2) color = balancedColor(d, color1, color2)
} }

Loading…
Cancel
Save