Search in sources :

Example 6 with GroupLayer

use of playn.core.GroupLayer in project playn by threerings.

the class ClippedGroupTest method init.

@Override
public void init() {
    GroupLayer rootLayer = graphics().rootLayer();
    final CanvasImage img = graphics().createImage(100, 50);
    img.canvas().setFillGradient(graphics().createLinearGradient(0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }));
    img.canvas().fillRoundRect(0, 0, 100, 50, 10);
    // create an immediate layer that draws the boundaries of our clipped group layers
    rootLayer.add(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            // draw the border of our various clipped groups
            surf.setFillColor(0xFF000000);
            outline(surf, g1);
            outline(surf, g2);
            outline(surf, g3);
            outline(surf, g4);
            outline(surf, g5);
        }

        protected void outline(Surface surf, Layer.HasSize ly) {
            drawRect(surf, ly.tx() - ly.originX(), ly.ty() - ly.originY(), ly.width(), ly.height());
        }

        protected void drawRect(Surface surf, float x, float y, float w, float h) {
            float left = x - 1, top = y - 1, right = x + w + 2, bot = y + h + 2;
            surf.drawLine(left, top, right, top, 1);
            surf.drawLine(right, top, right, bot, 1);
            surf.drawLine(left, top, left, bot, 1);
            surf.drawLine(left, bot, right, bot, 1);
        }
    }));
    // create a group layer with a static clip, and a rotating image inside
    g1 = graphics().createGroupLayer(100, 100);
    // test the origin not being at zero/zero
    g1.setOrigin(50, 0);
    i1 = graphics().createImageLayer(img);
    i1.setOrigin(i1.width() / 2, i1.height() / 2);
    g1.addAt(i1, 50, 50);
    rootLayer.addAt(g1, 75, 25);
    // static image inside and animated clipped width
    g2 = graphics().createGroupLayer(100, 100);
    g2.setOrigin(50, 50);
    g2.addAt(graphics().createImageLayer(img), (100 - img.width()) / 2, (100 - img.height()) / 2);
    rootLayer.addAt(g2, 200, 75);
    // nest a group layer inside with an animated origin
    inner = graphics().createGroupLayer();
    inner.addAt(graphics().createImageLayer(img), (100 - img.width()) / 2, (100 - img.height()) / 2);
    g3 = graphics().createGroupLayer(100, 100);
    g3.add(inner);
    rootLayer.addAt(g3, 275, 25);
    // create a group layer with a static clip, and a rotating surface image inside
    g4 = graphics().createGroupLayer(100, 100);
    SurfaceImage si = graphics().createSurface(100, 50);
    si.surface().setFillColor(0xFF99CCFF).fillRect(0, 0, 100, 50);
    s1 = graphics().createImageLayer(si);
    s1.setOrigin(s1.width() / 2, s1.height() / 2);
    g4.addAt(s1, 50, 50);
    rootLayer.addAt(g4, 400, 25);
    // put a large clipped group inside a small one
    g5Inner = graphics().createGroupLayer(150, 150);
    g5Inner.addAt(graphics().createImageLayer(img).setScale(2), -img.width(), -img.height());
    g5Inner.addAt(graphics().createImageLayer(img).setScale(2), -img.width(), img.height());
    g5Inner.addAt(graphics().createImageLayer(img).setScale(2), img.width(), -img.height());
    g5Inner.addAt(graphics().createImageLayer(img).setScale(2), img.width(), img.height());
    g5 = graphics().createGroupLayer(100, 100);
    g5.addAt(g5Inner, -25, -25);
    rootLayer.addAt(g5, 525, 25);
}
Also used : CanvasImage(playn.core.CanvasImage) GroupLayer(playn.core.GroupLayer) SurfaceImage(playn.core.SurfaceImage) Surface(playn.core.Surface)

Example 7 with GroupLayer

use of playn.core.GroupLayer in project playn by threerings.

the class ImmediateTest method init.

@Override
public void init() {
    GroupLayer rootLayer = graphics().rootLayer();
    final CanvasImage circle = graphics().createImage(100, 100);
    circle.canvas().setFillColor(0xFFCC99FF);
    circle.canvas().fillCircle(50, 50, 50);
    final CanvasImage sausage = graphics().createImage(100, 50);
    sausage.canvas().setFillGradient(graphics().createLinearGradient(0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }));
    sausage.canvas().fillRoundRect(0, 0, 100, 50, 10);
    // add an unclipped layer which will draw our background and outlines
    rootLayer.add(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFFFFCC99);
            surf.fillRect(0, 0, graphics().width(), graphics().height());
            // fill a rect that will be covered except for one pixel by the clipped immediate layers
            surf.setFillColor(0xFF000000);
            surf.fillRect(29, 29, 202, 202);
            surf.fillRect(259, 29, 102, 102);
            surf.fillRect(259, 159, 102, 102);
        }
    }));
    // add a clipped layer that will clip a fill and image draw
    ImmediateLayer ilayer = graphics().createImmediateLayer(200, 200, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            // this fill should be clipped to our bounds
            surf.setFillColor(0xFF99CCFF);
            surf.fillRect(-50, -50, 300, 300);
            // and this image should be clipped to our bounds
            surf.drawImage(circle, 125, -25);
        }
    });
    // adjust the origin to ensure that is accounted for in the clipping
    ilayer.setOrigin(100, 100);
    rootLayer.addAt(ilayer, 130, 130);
    // add a clipped layer that draws an image through a rotation transform
    rootLayer.addAt(graphics().createImmediateLayer(100, 100, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFF99CCFF);
            surf.fillRect(0, 0, 100, 100);
            surf.translate(50, 50);
            surf.rotate(rotation);
            surf.translate(-50, -50);
            surf.drawImage(sausage, 0, 25);
        }
    }), 260, 30);
    // add a clipped layer that draws an image through a translation transform
    rootLayer.addAt(graphics().createImmediateLayer(100, 100, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFF99CCFF);
            surf.fillRect(0, 0, 100, 100);
            surf.translate(FloatMath.sin(elapsed) * 50, FloatMath.cos(elapsed) * 50 + 25);
            surf.drawImage(sausage, 0, 0);
        }
    }), 260, 160);
}
Also used : CanvasImage(playn.core.CanvasImage) ImmediateLayer(playn.core.ImmediateLayer) GroupLayer(playn.core.GroupLayer) Surface(playn.core.Surface)

Example 8 with GroupLayer

use of playn.core.GroupLayer in project playn by threerings.

the class MouseWheelTest method init.

@Override
public void init() {
    GroupLayer slider = graphics().createGroupLayer();
    CanvasImage image = graphics().createImage(WIDTH + 10, HEIGHT);
    image.canvas().setFillColor(0xff808080);
    image.canvas().fillRect(0, 0, WIDTH + 10, HEIGHT);
    ImageLayer bg = graphics().createImageLayer(image);
    slider.add(bg);
    image = graphics().createImage(WIDTH, HWIDTH);
    image.canvas().setFillColor(0xffffffff);
    image.canvas().fillRect(0, 0, WIDTH, HWIDTH);
    image.canvas().setStrokeColor(0xff000000);
    image.canvas().drawLine(0, HWIDTH / 2, WIDTH, HWIDTH / 2);
    image.canvas().setStrokeColor(0xffff0000);
    image.canvas().strokeRect(0, 0, WIDTH - 1, HWIDTH - 1);
    slider.add(il = graphics().createImageLayer(image));
    il.setOrigin(0, HWIDTH / 2);
    il.setTranslation(0, HEIGHT / 2);
    il.setDepth(1);
    graphics().rootLayer().add(slider);
    slider.setTranslation(25, 25);
    bg.addListener(new Mouse.LayerAdapter() {

        @Override
        public void onMouseWheelScroll(WheelEvent event) {
            float y = il.ty() + event.velocity();
            y = Math.max(0, Math.min(y, HEIGHT));
            il.setTranslation(0, y);
        }
    });
}
Also used : CanvasImage(playn.core.CanvasImage) WheelEvent(playn.core.Mouse.WheelEvent) Mouse(playn.core.Mouse) ImageLayer(playn.core.ImageLayer) GroupLayer(playn.core.GroupLayer)

Aggregations

GroupLayer (playn.core.GroupLayer)8 CanvasImage (playn.core.CanvasImage)6 SurfaceImage (playn.core.SurfaceImage)5 Surface (playn.core.Surface)4 ImageLayer (playn.core.ImageLayer)3 ImmediateLayer (playn.core.ImmediateLayer)3 Image (playn.core.Image)2 Mouse (playn.core.Mouse)1 WheelEvent (playn.core.Mouse.WheelEvent)1 Pattern (playn.core.Pattern)1 Rectangle (pythagoras.f.Rectangle)1