Skip to content

Commit

Permalink
Implement callback to keep track of new and dead particle systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Dion Segijn committed May 31, 2017
1 parent 2ab9515 commit 341a2f0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
20 changes: 8 additions & 12 deletions app/src/main/java/nl/dionsegijn/konfettidemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import nl.dionsegijn.konfettidemo.interfaces.OnConfigurationChangedListener
import nl.dionsegijn.konfettidemo.interfaces.OnSimpleTabSelectedListener



/**
* Created by dionsegijn on 3/25/17.
*/
class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {

lateinit var bottomSheetBehavior: BottomSheetBehavior<View>
Expand Down Expand Up @@ -82,7 +78,7 @@ class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {
}

fun streamFromTop(config: Configuration, colors: IntArray) {
if(!canIHaveMoreConfetti()) return
if (!canIHaveMoreConfetti()) return
viewKonfetti.build()
.addColors(*colors)
.setDirection(0.0, 359.0)
Expand All @@ -96,7 +92,7 @@ class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {
}

fun burstFromCenter(config: Configuration, colors: IntArray) {
if(!canIHaveMoreConfetti()) return
if (!canIHaveMoreConfetti()) return
viewKonfetti.build()
.addColors(*colors)
.setDirection(0.0, 359.0)
Expand All @@ -116,7 +112,7 @@ class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {
fun velocityTest() {
viewKonfetti.setOnTouchListener { _, event ->
val modeEnabled = viewConfigurationControls.configuration.active.type == Configuration.TYPE_DRAG_AND_SHOOT
when(event.action) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
startX = event.x
startY = event.y
Expand All @@ -129,10 +125,10 @@ class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {

val length = Math.sqrt((dx * dx) + (dy * dy).toDouble())
speed = (length / 100).toInt()
if(speed > 10) speed = 0
if (speed > 10) speed = 0
}
MotionEvent.ACTION_UP -> {
if(!modeEnabled || !canIHaveMoreConfetti()) return@setOnTouchListener false
if (!modeEnabled || !canIHaveMoreConfetti()) return@setOnTouchListener false
val colors = viewConfigurationControls.configuration.active.colors.map { color(it) }.toIntArray()
viewKonfetti.build()
.addColors(*colors)
Expand Down Expand Up @@ -162,7 +158,7 @@ class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {
textViewInstructions.alpha = alpha
viewIllustration.alpha = alpha
}
valueAnimator.addListener(object: AnimatorListenerAdapter() {
valueAnimator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
textViewInstructions.setText(selected.instructions)
viewIllustration.setImageDrawable(ContextCompat.getDrawable(applicationContext, selected.vector))
Expand All @@ -185,10 +181,10 @@ class MainActivity : AppCompatActivity(), OnConfigurationChangedListener {
* there is no nice way of limiting the resources foreach particle system.
*/
fun canIHaveMoreConfetti(): Boolean {
if(viewConfigurationControls.configuration.maxParticleSystemsAlive
if (viewConfigurationControls.configuration.maxParticleSystemsAlive
== ConfigurationManager.PARTICLE_SYSTEMS_INFINITE) {
return true
} else if(viewKonfetti.systems.size <= 6) {
} else if (viewKonfetti.systems.size <= 6) {
return true
}
return false
Expand Down
26 changes: 22 additions & 4 deletions konfetti/src/main/java/nl/dionsegijn/konfetti/KonfettiView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.View
import nl.dionsegijn.konfetti.listeners.OnParticleSystemUpdateListener

/**
* Created by dionsegijn on 3/25/17.
Expand All @@ -17,8 +18,21 @@ class KonfettiView : View {
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

/**
* Active particle systems
*/
val systems: MutableList<ParticleSystem> = mutableListOf()
var timer: TimerIntegration = TimerIntegration()

/**
* Keeping track of the delta time between frame rendering
*/
internal var timer: TimerIntegration = TimerIntegration()

/**
* [OnParticleSystemUpdateListener] listener to notify when a new particle system
* starts rendering and when a particle system stopped rendering
*/
var onParticleSystemUpdateListener: OnParticleSystemUpdateListener? = null

fun build(): ParticleSystem {
return ParticleSystem(this)
Expand All @@ -29,9 +43,12 @@ class KonfettiView : View {

val deltaTime = timer.getDeltaTime()
for (i in systems.size - 1 downTo 0) {
val konfetti = systems[i]
konfetti.emitter.render(canvas, deltaTime)
if (konfetti.doneEmitting()) systems.removeAt(i)
val particleSystem = systems[i]
particleSystem.emitter.render(canvas, deltaTime)
if (particleSystem.doneEmitting()) {
systems.removeAt(i)
onParticleSystemUpdateListener?.onParticleSystemEnded(this, particleSystem, systems.size)
}
}

if (systems.size != 0) {
Expand All @@ -43,6 +60,7 @@ class KonfettiView : View {

fun start(particleSystem: ParticleSystem) {
systems.add(particleSystem)
onParticleSystemUpdateListener?.onParticleSystemStarted(this, particleSystem, systems.size)
invalidate()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.support.annotation.ColorInt
import nl.dionsegijn.konfetti.emitters.BurstEmitter
import nl.dionsegijn.konfetti.emitters.Emitter
import nl.dionsegijn.konfetti.emitters.StreamEmitter
import nl.dionsegijn.konfetti.listeners.OnParticleSystemUpdateListener
import nl.dionsegijn.konfetti.models.ConfettiConfig
import nl.dionsegijn.konfetti.models.LocationModule
import nl.dionsegijn.konfetti.models.Shape
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nl.dionsegijn.konfetti.listeners

import nl.dionsegijn.konfetti.KonfettiView
import nl.dionsegijn.konfetti.ParticleSystem

/**
* Created by dionsegijn on 5/31/17.
*/
interface OnParticleSystemUpdateListener {
fun onParticleSystemStarted(view: KonfettiView, system: ParticleSystem, activeSystems: Int)
fun onParticleSystemEnded(view: KonfettiView, system: ParticleSystem, activeSystems: Int)
}

0 comments on commit 341a2f0

Please sign in to comment.