tab1React = TimeReact.every(10).edge(MouseReact.onButton1()).subscribe(new Procedure1<Integer>() {
@Override
public void invoke(Integer value) {
// create circle
final Circle circle = new Circle(5);
particule.addShape(circle);
// determine random velocity and angle constant in time
double velAngle = Math.random() * Math.PI * 2;
double velSpeed = Math.random() * 10;
final double velX = Math.cos(velAngle) * velSpeed;
final double velY = Math.sin(velAngle) * velSpeed;
final int mx = mouseX.getValue();
final int my = mouseY.getValue();
// create reactive counter every 25 fps
final Var<Integer> counter = TimeReact.framePerSecond(25).fold(0, new Function2<Integer, Integer, Integer>() {
@Override
public Integer invoke(Integer arg1, Integer arg2) {
return arg1 + 1;
}
}).map(new Function1<Integer, Integer>() {
@Override
public Integer invoke(Integer arg1) {
return arg1.intValue();
}
}).toVar(0);
// creation reactive operation between counter and constants to calculate x coordonate
final Var<Integer> x = counter.map(new Function1<Integer, Integer>() {
@Override
public Integer invoke(Integer value) {
return mx + (int) (velX * value);
}
});
// creation reactive operation between counter and constants to calculate y coordonate
final Var<Integer> y = counter.map(new Function1<Integer, Integer>() {
@Override
public Integer invoke(Integer value) {
return my + (int) (velY * value);
}
});
// creation reactive operation between counter and constants to calculate circle size
Signal<Integer> size = counter.map(new Function1<Integer, Integer>() {
@Override
public Integer invoke(Integer value) {
double factor = Math.sin((double) value / 10 - Math.PI / 4);
return 50 + (int) (50 * factor);
}
});
size.disposeWhen(new Predicate1<Integer>() {
@Override
public boolean invoke(Integer value) {
if (value == 1) {
particule.removeShape(circle);
x.dispose();
y.dispose();
return true;
}
return false;
}
});
// set reacts to component
circle.setX(x);
circle.setY(y);
circle.setSize(size);
}
});
}