From 341a2f070641876f3fb19eeee6b54062cf213e4d Mon Sep 17 00:00:00 2001 From: Dion Segijn Date: Wed, 31 May 2017 20:30:18 +0200 Subject: [PATCH] Implement callback to keep track of new and dead particle systems Fixes #8 --- .../dionsegijn/konfettidemo/MainActivity.kt | 20 ++++++-------- .../nl/dionsegijn/konfetti/KonfettiView.kt | 26 ++++++++++++++++--- .../nl/dionsegijn/konfetti/ParticleSystem.kt | 1 + .../OnParticleSystemUpdateListener.kt | 12 +++++++++ 4 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 konfetti/src/main/java/nl/dionsegijn/konfetti/listeners/OnParticleSystemUpdateListener.kt diff --git a/app/src/main/java/nl/dionsegijn/konfettidemo/MainActivity.kt b/app/src/main/java/nl/dionsegijn/konfettidemo/MainActivity.kt index 7396c66e..27ec950f 100644 --- a/app/src/main/java/nl/dionsegijn/konfettidemo/MainActivity.kt +++ b/app/src/main/java/nl/dionsegijn/konfettidemo/MainActivity.kt @@ -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 @@ -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) @@ -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) @@ -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 @@ -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) @@ -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)) @@ -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 diff --git a/konfetti/src/main/java/nl/dionsegijn/konfetti/KonfettiView.kt b/konfetti/src/main/java/nl/dionsegijn/konfetti/KonfettiView.kt index 55cce69f..89200f0d 100644 --- a/konfetti/src/main/java/nl/dionsegijn/konfetti/KonfettiView.kt +++ b/konfetti/src/main/java/nl/dionsegijn/konfetti/KonfettiView.kt @@ -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. @@ -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 = 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) @@ -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) { @@ -43,6 +60,7 @@ class KonfettiView : View { fun start(particleSystem: ParticleSystem) { systems.add(particleSystem) + onParticleSystemUpdateListener?.onParticleSystemStarted(this, particleSystem, systems.size) invalidate() } diff --git a/konfetti/src/main/java/nl/dionsegijn/konfetti/ParticleSystem.kt b/konfetti/src/main/java/nl/dionsegijn/konfetti/ParticleSystem.kt index 7df16826..380df92c 100644 --- a/konfetti/src/main/java/nl/dionsegijn/konfetti/ParticleSystem.kt +++ b/konfetti/src/main/java/nl/dionsegijn/konfetti/ParticleSystem.kt @@ -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 diff --git a/konfetti/src/main/java/nl/dionsegijn/konfetti/listeners/OnParticleSystemUpdateListener.kt b/konfetti/src/main/java/nl/dionsegijn/konfetti/listeners/OnParticleSystemUpdateListener.kt new file mode 100644 index 00000000..061eec01 --- /dev/null +++ b/konfetti/src/main/java/nl/dionsegijn/konfetti/listeners/OnParticleSystemUpdateListener.kt @@ -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) +}