//
// Triple Play - utilities for use in PlayN-based games
// Copyright (c) 2011-2013, Three Rings Design, Inc. - All rights reserved.
// http://github.com/threerings/tripleplay/blob/master/LICENSE

package tripleplay.demo.anim;

import playn.core.CanvasImage;
import playn.core.Font;
import playn.core.ImageLayer;
import playn.core.Pointer;
import static playn.core.PlayN.*;

import tripleplay.anim.AnimGroup;
import tripleplay.anim.Animation;
import tripleplay.demo.DemoScreen;
import tripleplay.ui.Group;
import tripleplay.util.StyledText;
import tripleplay.util.TextStyle;

public class AnimDemo extends DemoScreen
{
    @Override protected String name () {
        return "Anims";
    }
    @Override protected String title () {
        return "Various Animations";
    }

    @Override protected Group createIface () {
        // demo a repeating animation
        CanvasImage image = graphics().createImage(100, 100);
        image.canvas().setFillColor(0xFFFFCC99).fillCircle(50, 50, 50);
        ImageLayer circle = graphics().createImageLayer(image);

        float width = graphics().width();
        anim.addAt(layer, circle, 50, 100).then().
            repeat(circle).tweenX(circle).to(width-150).in(1000).easeInOut().then().
            tweenX(circle).to(50).in(1000).easeInOut();

        // demo the shake animation
        final ImageLayer click = StyledText.span("Click to Shake", STYLE).toLayer();
        click.addListener(new Pointer.Adapter() {
            @Override public void onPointerStart (Pointer.Event event) {
                anim.shake(click).bounds(-3, 3, -3, 0).cycleTime(25, 25).in(1000);
            }
        });
        layer.addAt(click, (width-click.width())/2, 275);

        // demo animation groups
        CanvasImage ball = graphics().createImage(40, 40);
        ball.canvas().setFillColor(0xFF99CCFF).fillCircle(20, 20, 20);
        ImageLayer[] balls = new ImageLayer[6];
        for (int ii = 0; ii < balls.length; ii++) {
            layer.addAt(balls[ii] = graphics().createImageLayer(ball), 170+ii*50, 350);
        }
        anim.repeat(layer).add(dropBalls(balls, 0, 1)).then().
            add(dropBalls(balls, 1, 2)).then().
            add(dropBalls(balls, 3, 3));

        return null;
    }

    protected Animation dropBalls (ImageLayer[] balls, int offset, int count) {
        float startY = 350;
        AnimGroup group = new AnimGroup();
        for (int ii = 0; ii < count; ii++) {
            ImageLayer ball = balls[ii+offset];
            group.tweenY(ball).to(startY+100).in(1000).easeIn().then().
                tweenY(ball).to(startY).in(1000).easeOut();
        }
        return group.toAnim();
    }

    protected static final TextStyle STYLE = new TextStyle().
        withFont(graphics().createFont("Helvetica", Font.Style.PLAIN, 48));
}

@@ -223,16 +223,48 @@
 .anim;%0A%0A
+import pythagoras.f.FloatMath;%0A%0A
 import p
@@ -286,16 +286,16 @@
 sImage;%0A
-
 import p
@@ -363,24 +363,54 @@
 re.Pointer;%0A
+import playn.core.util.Clock;%0A
 import stati
@@ -500,16 +500,49 @@
 mation;%0A
+import tripleplay.anim.Animator;%0A
 import t
@@ -709,16 +709,128 @@
 creen%0A%7B%0A
+    @Override public void paint (Clock clock) %7B%0A        super.paint(clock);%0A        _banim.paint(clock);%0A    %7D%0A%0A
     @Ove
@@ -2419,24 +2419,1013 @@
 s, 3, 3));%0A%0A
+        // test barrier delay%0A        CanvasImage sqimg = graphics().createImage(50, 50);%0A        sqimg.canvas().setFillColor(0xFF99CCFF).fillRect(0, 0, 50, 50);%0A        final ImageLayer square = graphics().createImageLayer(sqimg);%0A        square.setOrigin(25, 25);%0A        layer.addAt(square, 50, 300);%0A        square.addListener(new Pointer.Adapter() %7B%0A            @Override public void onPointerStart (Pointer.Event event) %7B%0A                square.setInteractive(false);%0A                _banim.tweenXY(square).to(50, 350);%0A                _banim.delay(250).then().tweenRotation(square).to(FloatMath.PI).in(500);%0A                _banim.addBarrier(1000);%0A                _banim.tweenXY(square).to(50, 300);%0A                _banim.delay(250).then().tweenRotation(square).to(0).in(500);%0A                _banim.addBarrier();%0A                _banim.action(new Runnable() %7B%0A                    public void run () %7B square.setInteractive(true); %7D%0A                %7D);%0A            %7D%0A        %7D);%0A%0A
         retu
@@ -3857,23 +3857,125 @@
 Anim();%0A
-
     %7D%0A%0A
+    // a separate animator used for testing barriers%0A    protected Animator _banim = new Animator();%0A%0A
     prot

Add animation barriers demo/test.