Skip to content

Commit

Permalink
Merge pull request #322 from DanielMartinus/update-spotless
Browse files Browse the repository at this point in the history
Update spotless
  • Loading branch information
DanielMartinus authored Dec 29, 2023
2 parents 0425c22 + 1a1373f commit 1ebaf94
Show file tree
Hide file tree
Showing 41 changed files with 520 additions and 396 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.{kt,kts}]
ktlint_function_naming_ignore_when_annotated_with=Composable
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
classpath("com.android.tools.build:gradle:8.0.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Constants.kotlinVersion}")
classpath("com.github.dcendents:android-maven-gradle-plugin:2.1")
classpath("com.diffplug.spotless:spotless-plugin-gradle:5.14.2")
classpath("com.diffplug.spotless:spotless-plugin-gradle:6.23.3")
classpath("io.github.gradle-nexus:publish-plugin:1.3.0")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.7.0")
}
Expand Down
4 changes: 2 additions & 2 deletions konfetti/compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ apply(from = "../../scripts/publish-module.gradle.kts")

spotless {
kotlin {
ktlint("0.37.2")
ktlint("1.1.0")
target("src/**/*.kt")
}
java {
removeUnusedImports()
googleJavaFormat("1.5")
googleJavaFormat("1.15.0")
target("**/*.java")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ import nl.dionsegijn.konfetti.xml.image.ImageStore
* `size` and must vertically/horizontally center their asset if it does not have an equal width
* and height.
*/
fun Shape.draw(drawScope: DrawScope, particle: Particle, imageResource: ImageBitmap? = null, imageStore: ImageStore) {
fun Shape.draw(
drawScope: DrawScope,
particle: Particle,
imageResource: ImageBitmap? = null,
imageStore: ImageStore,
) {
when (this) {
Circle -> {
val offsetMiddle = particle.width / 2
drawScope.drawCircle(
color = Color(particle.color),
center = Offset(particle.x + offsetMiddle, particle.y + offsetMiddle),
radius = particle.width / 2
radius = particle.width / 2,
)
}
Square -> {
Expand All @@ -48,7 +53,7 @@ fun Shape.draw(drawScope: DrawScope, particle: Particle, imageResource: ImageBit
drawScope.drawRect(
color = Color(particle.color),
topLeft = Offset(particle.x, particle.y),
size = Size(size, height)
size = Size(size, height),
)
}
is DrawableShape -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import nl.dionsegijn.konfetti.xml.image.ImageStore
fun KonfettiView(
modifier: Modifier = Modifier,
parties: List<Party>,
updateListener: OnParticleSystemUpdateListener? = null
updateListener: OnParticleSystemUpdateListener? = null,
) {

lateinit var partySystems: List<PartySystem>

/**
Expand All @@ -51,63 +50,67 @@ fun KonfettiView(
val imageStore = remember { ImageStore() }

LaunchedEffect(Unit) {
partySystems = parties.map {
PartySystem(
party = storeImages(it, imageStore),
pixelDensity = Resources.getSystem().displayMetrics.density
)
}
partySystems =
parties.map {
PartySystem(
party = storeImages(it, imageStore),
pixelDensity = Resources.getSystem().displayMetrics.density,
)
}
while (true) {
withFrameMillis { frameMs ->
// Calculate time between frames, fallback to 0 when previous frame doesn't exist
val deltaMs = if (frameTime.value > 0) (frameMs - frameTime.value) else 0
frameTime.value = frameMs

particles.value = partySystems.map { particleSystem ->
particles.value =
partySystems.map { particleSystem ->

val totalTimeRunning = getTotalTimeRunning(particleSystem.createdAt)
// Do not start particleSystem yet if totalTimeRunning is below delay
if (totalTimeRunning < particleSystem.party.delay) return@map listOf()
val totalTimeRunning = getTotalTimeRunning(particleSystem.createdAt)
// Do not start particleSystem yet if totalTimeRunning is below delay
if (totalTimeRunning < particleSystem.party.delay) return@map listOf()

if (particleSystem.isDoneEmitting()) {
updateListener?.onParticleSystemEnded(
system = particleSystem,
activeSystems = partySystems.count { !it.isDoneEmitting() }
)
}
if (particleSystem.isDoneEmitting()) {
updateListener?.onParticleSystemEnded(
system = particleSystem,
activeSystems = partySystems.count { !it.isDoneEmitting() },
)
}

particleSystem.render(deltaMs.div(1000f), drawArea.value)
}.flatten()
particleSystem.render(deltaMs.div(1000f), drawArea.value)
}.flatten()
}
}
}

Canvas(
modifier = modifier
.onGloballyPositioned {
drawArea.value =
CoreRectImpl(0f, 0f, it.size.width.toFloat(), it.size.height.toFloat())
},
modifier =
modifier
.onGloballyPositioned {
drawArea.value =
CoreRectImpl(0f, 0f, it.size.width.toFloat(), it.size.height.toFloat())
},
onDraw = {
particles.value.forEach { particle ->
withTransform({
rotate(
degrees = particle.rotation,
pivot = Offset(
x = particle.x + (particle.width / 2),
y = particle.y + (particle.height / 2)
)
pivot =
Offset(
x = particle.x + (particle.width / 2),
y = particle.y + (particle.height / 2),
),
)
scale(
scaleX = particle.scaleX,
scaleY = 1f,
pivot = Offset(particle.x + (particle.width / 2), particle.y)
pivot = Offset(particle.x + (particle.width / 2), particle.y),
)
}) {
particle.shape.draw(drawScope = this, particle = particle, imageStore = imageStore)
}
}
}
},
)
}

Expand All @@ -118,16 +121,20 @@ fun KonfettiView(
* @param party The Party object containing the shapes to be transformed.
* @return A new Party object with the transformed shapes.
*/
fun storeImages(party: Party, imageStore: ImageStore): Party {
val transformedShapes = party.shapes.map { shape ->
when (shape) {
is Shape.DrawableShape -> {
val referenceImage = drawableToReferenceImage(shape.image as DrawableImage, imageStore)
shape.copy(image = referenceImage)
fun storeImages(
party: Party,
imageStore: ImageStore,
): Party {
val transformedShapes =
party.shapes.map { shape ->
when (shape) {
is Shape.DrawableShape -> {
val referenceImage = drawableToReferenceImage(shape.image as DrawableImage, imageStore)
shape.copy(image = referenceImage)
}
else -> shape
}
else -> shape
}
}
return party.copy(shapes = transformedShapes)
}

Expand All @@ -137,7 +144,10 @@ fun storeImages(party: Party, imageStore: ImageStore): Party {
* @param drawableImage The DrawableImage to be converted.
* @return A ReferenceImage with the same dimensions as the DrawableImage and a reference to the stored Drawable.
*/
fun drawableToReferenceImage(drawableImage: DrawableImage, imageStore: ImageStore): ReferenceImage {
fun drawableToReferenceImage(
drawableImage: DrawableImage,
imageStore: ImageStore,
): ReferenceImage {
val id = imageStore.storeImage(drawableImage.drawable)
return ReferenceImage(id, drawableImage.width, drawableImage.height)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package nl.dionsegijn.konfetti.compose
import nl.dionsegijn.konfetti.core.PartySystem

interface OnParticleSystemUpdateListener {
fun onParticleSystemEnded(system: PartySystem, activeSystems: Int)
fun onParticleSystemEnded(
system: PartySystem,
activeSystems: Int,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import nl.dionsegijn.konfetti.core.models.CoreImage
data class DrawableImage(
val drawable: Drawable,
override val width: Int,
override val height: Int
override val height: Int,
) : CoreImage
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import android.graphics.drawable.Drawable
import nl.dionsegijn.konfetti.core.models.Shape

object ImageUtil {

@JvmStatic
fun loadDrawable(
drawable: Drawable,
tint: Boolean = true,
applyAlpha: Boolean = true
applyAlpha: Boolean = true,
): Shape.DrawableShape {
val width = drawable.intrinsicWidth
val height = drawable.intrinsicHeight
Expand Down
13 changes: 7 additions & 6 deletions konfetti/core/src/main/java/nl/dionsegijn/konfetti/core/Party.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import nl.dionsegijn.konfetti.core.models.Size
* it will slowly fade out. If set to falls it will instantly disappear from the screen.
* @property position the point where the confetti will spawn relative to the canvas. Use absolute
* coordinates with [Position.Absolute] or relative coordinates between 0.0 and 1.0 using [Position.Relative].
* Spawn confetti on random positions using [Position.between].
* Spawn confetti on random positions using [Position.Between].
* @property delay the amount of milliseconds to wait before the rendering of the confetti starts
* @property rotation enable the 3D rotation of a Confetti. See [Rotation] class for the configuration
* options. Easily enable or disable it using [Rotation].enabled() or [Rotation].disabled() and
Expand All @@ -52,7 +52,7 @@ data class Party(
val position: Position = Position.Relative(0.5, 0.5),
val delay: Int = 0,
val rotation: Rotation = Rotation(),
val emitter: EmitterConfig
val emitter: EmitterConfig,
)

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ sealed class Position {
* @property y the y coordinate in pixels
*/
data class Absolute(val x: Float, val y: Float) : Position() {
fun between(value: Absolute): Position = between(this, value)
fun between(value: Absolute): Position = Between(this, value)
}

/**
Expand All @@ -105,7 +105,7 @@ sealed class Position {
* @property y the relative y coordinate as a double
*/
data class Relative(val x: Double, val y: Double) : Position() {
fun between(value: Relative): Position = between(this, value)
fun between(value: Relative): Position = Between(this, value)
}

/**
Expand All @@ -114,7 +114,7 @@ sealed class Position {
* Example: Relative(0.0, 0.0).between(Relative(1.0, 0.0))
* This will spawn confetti from the full width and top of the view
*/
internal data class between(val min: Position, val max: Position) : Position()
internal data class Between(val min: Position, val max: Position) : Position()
}

/**
Expand All @@ -132,10 +132,11 @@ data class Rotation(
val speed: Float = 1f,
val variance: Float = 0.5f,
val multiplier2D: Float = 8f,
val multiplier3D: Float = 1.5f
val multiplier3D: Float = 1.5f,
) {
companion object {
fun enabled() = Rotation(enabled = true)

fun disabled() = Rotation(enabled = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import nl.dionsegijn.konfetti.core.models.Size
* See [Party] for documentation on the configuration settings
*/
class PartyFactory(val emitter: EmitterConfig) {

private var party: Party = Party(emitter = emitter)

fun angle(angle: Int): PartyFactory {
Expand All @@ -27,7 +26,10 @@ class PartyFactory(val emitter: EmitterConfig) {
return this
}

fun setSpeedBetween(minSpeed: Float, maxSpeed: Float): PartyFactory {
fun setSpeedBetween(
minSpeed: Float,
maxSpeed: Float,
): PartyFactory {
party = party.copy(speed = minSpeed, maxSpeed = maxSpeed)
return this
}
Expand All @@ -42,28 +44,47 @@ class PartyFactory(val emitter: EmitterConfig) {
return this
}

fun position(x: Float, y: Float): PartyFactory {
fun position(
x: Float,
y: Float,
): PartyFactory {
party = party.copy(position = Position.Absolute(x, y))
return this
}

fun position(minX: Float, minY: Float, maxX: Float, maxY: Float): PartyFactory {
party = party.copy(
position = Position.Absolute(minX, minY)
.between(Position.Absolute(maxX, maxY))
)
fun position(
minX: Float,
minY: Float,
maxX: Float,
maxY: Float,
): PartyFactory {
party =
party.copy(
position =
Position.Absolute(minX, minY)
.between(Position.Absolute(maxX, maxY)),
)
return this
}

fun position(x: Double, y: Double): PartyFactory {
fun position(
x: Double,
y: Double,
): PartyFactory {
party = party.copy(position = Position.Relative(x, y))
return this
}

fun position(minX: Double, minY: Double, maxX: Double, maxY: Double): PartyFactory {
party = party.copy(
position = Position.Relative(minX, minY).between(Position.Relative(maxX, maxY))
)
fun position(
minX: Double,
minY: Double,
maxX: Double,
maxY: Double,
): PartyFactory {
party =
party.copy(
position = Position.Relative(minX, minY).between(Position.Relative(maxX, maxY)),
)
return this
}

Expand Down
Loading

0 comments on commit 1ebaf94

Please sign in to comment.