Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions LeonidsLib/src/main/java/com/plattysoft/leonids/ParticleSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import com.plattysoft.leonids.emitters.Emitter;
import com.plattysoft.leonids.emitters.ViewEmitter;
import com.plattysoft.leonids.initializers.AccelerationInitializer;
import com.plattysoft.leonids.initializers.ParticleInitializer;
import com.plattysoft.leonids.initializers.RotationInitializer;
Expand Down Expand Up @@ -438,6 +440,13 @@ public ParticleSystem setFadeOut(long duration) {
return setFadeOut(duration, new LinearInterpolator());
}

/**
* @see #emitWithGravity(View, int, int, int)
*/
public void emitWithGravity (View emitter, int gravity, int particlesPerSecond, int emittingTime) {
emitWithGravity(new ViewEmitter(emitter), gravity, particlesPerSecond, emittingTime);
}

/**
* Starts emitting particles from a specific view. If at some point the number goes over the amount of particles availabe on create
* no new particles will be created
Expand All @@ -447,7 +456,7 @@ public ParticleSystem setFadeOut(long duration) {
* @param particlesPerSecond Number of particles per second that will be emited (evenly distributed)
* @param emittingTime time the emitter will be emitting particles
*/
public void emitWithGravity (View emitter, int gravity, int particlesPerSecond, int emittingTime) {
public void emitWithGravity (Emitter emitter, int gravity, int particlesPerSecond, int emittingTime) {
// Setup emitter
configureEmitter(emitter, gravity);
startEmitting(particlesPerSecond, emittingTime);
Expand Down Expand Up @@ -487,7 +496,7 @@ public void emit (View emitter, int particlesPerSecond) {
*/
public void emitWithGravity (View emitter, int gravity, int particlesPerSecond) {
// Setup emitter
configureEmitter(emitter, gravity);
configureEmitter(new ViewEmitter(emitter), gravity);
startEmitting(particlesPerSecond);
}

Expand Down Expand Up @@ -541,27 +550,45 @@ public void updateEmitPoint (int emitterX, int emitterY) {
}

public void updateEmitPoint (View emitter, int gravity) {
updateEmitPoint(new ViewEmitter(emitter), gravity);
}

public void updateEmitPoint (Emitter emitter, int gravity) {
configureEmitter(emitter, gravity);
}

/**
* @see #oneShot(Emitter, int)
*/
public void oneShot(View emitter, int numParticles) {
oneShot(new ViewEmitter(emitter), numParticles);
}

/**
* Launches particles in one Shot
*
* @param emitter View from which center the particles will be emited
* @param numParticles number of particles launched on the one shot
*/
public void oneShot(View emitter, int numParticles) {
public void oneShot(Emitter emitter, int numParticles) {
oneShot(emitter, numParticles, new LinearInterpolator());
}

/**
* @see #oneShot(Emitter, int, Interpolator)
*/
public void oneShot(View emitter, int numParticles, Interpolator interpolator) {
oneShot(new ViewEmitter(emitter), numParticles, interpolator);
}

/**
* Launches particles in one Shot using a special Interpolator
*
* @param emitter View from which center the particles will be emited
* @param numParticles number of particles launched on the one shot
* @param interpolator the interpolator for the time
*/
public void oneShot(View emitter, int numParticles, Interpolator interpolator) {
public void oneShot(Emitter emitter, int numParticles, Interpolator interpolator) {
configureEmitter(emitter, Gravity.CENTER);
mActivatedParticles = 0;
mEmittingTime = mTimeToLive;
Expand Down Expand Up @@ -609,47 +636,44 @@ public void onAnimationCancel(Animator animation) {
mAnimator.start();
}

private void configureEmitter(View emitter, int gravity) {
// It works with an emision range
int[] location = new int[2];
emitter.getLocationInWindow(location);
private void configureEmitter(Emitter emitter, int gravity) {

// Check horizontal gravity and set range
if (hasGravity(gravity, Gravity.LEFT)) {
mEmitterXMin = location[0] - mParentLocation[0];
mEmitterXMin = emitter.getX() - mParentLocation[0];
mEmitterXMax = mEmitterXMin;
}
else if (hasGravity(gravity, Gravity.RIGHT)) {
mEmitterXMin = location[0] + emitter.getWidth() - mParentLocation[0];
mEmitterXMin = emitter.getX() + emitter.getWidth() - mParentLocation[0];
mEmitterXMax = mEmitterXMin;
}
else if (hasGravity(gravity, Gravity.CENTER_HORIZONTAL)){
mEmitterXMin = location[0] + emitter.getWidth()/2 - mParentLocation[0];
mEmitterXMin = emitter.getX() + emitter.getWidth()/2 - mParentLocation[0];
mEmitterXMax = mEmitterXMin;
}
else {
// All the range
mEmitterXMin = location[0] - mParentLocation[0];
mEmitterXMax = location[0] + emitter.getWidth() - mParentLocation[0];
mEmitterXMin = emitter.getX() - mParentLocation[0];
mEmitterXMax = emitter.getX() + emitter.getWidth() - mParentLocation[0];
}

// Now, vertical gravity and range
if (hasGravity(gravity, Gravity.TOP)) {
mEmitterYMin = location[1] - mParentLocation[1];
mEmitterYMin = emitter.getY() - mParentLocation[1];
mEmitterYMax = mEmitterYMin;
}
else if (hasGravity(gravity, Gravity.BOTTOM)) {
mEmitterYMin = location[1] + emitter.getHeight() - mParentLocation[1];
mEmitterYMin = emitter.getY() + emitter.getHeight() - mParentLocation[1];
mEmitterYMax = mEmitterYMin;
}
else if (hasGravity(gravity, Gravity.CENTER_VERTICAL)){
mEmitterYMin = location[1] + emitter.getHeight()/2 - mParentLocation[1];
mEmitterYMin = emitter.getY() + emitter.getHeight()/2 - mParentLocation[1];
mEmitterYMax = mEmitterYMin;
}
else {
// All the range
mEmitterYMin = location[1] - mParentLocation[1];
mEmitterYMax = location[1] + emitter.getHeight() - mParentLocation[1];
mEmitterYMin = emitter.getY() - mParentLocation[1];
mEmitterYMax = emitter.getY() + emitter.getHeight() - mParentLocation[1];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.plattysoft.leonids.emitters;

/**
* Specifies an area with which particles should be emitted from.
*/
public interface Emitter {
int getX();
int getY();
int getWidth();
int getHeight();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.plattysoft.leonids.emitters;

/**
* Specifies an arbitrary rectangular area where particles can be emitted from.
* This will be relative to whatever parent view the emitter is attached to. So for example, if
* you specify an "x" of 10px for this rectangle, and the parent view is offset 100px from the
* edge of the screen, then the total offset will be 110px from the edge of the screen.
*/
public class RectEmitter implements Emitter {

private final int x;
private final int y;
private final int width;
private final int height;

public RectEmitter(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

@Override
public int getX() {
return x;
}

@Override
public int getY() {
return y;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.plattysoft.leonids.emitters;

import android.view.View;

/**
* Emit particles within the area occupied by a particular {@link View}.
*/
public class ViewEmitter implements Emitter {

private final int x;
private final int y;
private final int width;
private final int height;

public ViewEmitter(final View view) {
// It works with an emission range
int[] location = new int[2];
view.getLocationInWindow(location);

x = location[0];
y = location[1];
width = view.getWidth();
height = view.getHeight();
}

@Override
public int getX() {
return x;
}

@Override
public int getY() {
return y;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}
}