Search in sources :

Example 1 with GroupLayer

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

the class SubImageTest method fragment.

protected void fragment(String source, Image image, float ox, float oy) {
    float hw = image.width() / 2f, hh = image.height() / 2f;
    Image ul = image.subImage(0, 0, hw, hh);
    Image ur = image.subImage(hw, 0, hw, hh);
    Image ll = image.subImage(0, hh, hw, hh);
    Image lr = image.subImage(hw, hh, hw, hh);
    Image ctr = image.subImage(hw / 2, hh / 2, hw, hh);
    float dx = hw + 10, dy = hh + 10;
    GroupLayer group = graphics().createGroupLayer();
    group.addAt(graphics().createImageLayer(ul), 0, 0);
    group.addAt(graphics().createImageLayer(ur), dx, 0);
    group.addAt(graphics().createImageLayer(ll), 0, dy);
    group.addAt(graphics().createImageLayer(lr), dx, dy);
    group.addAt(graphics().createImageLayer(ctr), dx / 2, 2 * dy);
    float xoff = image.width() + 20;
    group.addAt(scaleLayer(graphics().createImageLayer(ul), 2), xoff, 0);
    group.addAt(scaleLayer(graphics().createImageLayer(ur), 2), xoff + 2 * dx, 0);
    group.addAt(scaleLayer(graphics().createImageLayer(ll), 2), xoff, 2 * dy);
    group.addAt(scaleLayer(graphics().createImageLayer(lr), 2), xoff + 2 * dx, 2 * dy);
    graphics().rootLayer().addAt(group, ox, oy);
    addDescrip(source + " split into subimages, and scaled", ox, oy + image.height() * 2 + 25, 3 * image.width() + 40);
}
Also used : CanvasImage(playn.core.CanvasImage) SurfaceImage(playn.core.SurfaceImage) Image(playn.core.Image) GroupLayer(playn.core.GroupLayer)

Example 2 with GroupLayer

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

the class AlphaLayerTest method init.

@Override
public void init() {
    final GroupLayer rootLayer = graphics().rootLayer();
    final float fullWidth = 6 * width, fullHeight = 3 * height;
    // add a half white, half blue background
    SurfaceImage bg = graphics().createSurface(fullWidth, fullHeight);
    bg.surface().setFillColor(Color.rgb(255, 255, 255));
    bg.surface().fillRect(0, 0, fullWidth, fullHeight);
    bg.surface().setFillColor(Color.rgb(0, 0, 255));
    bg.surface().fillRect(0, 2 * height, fullWidth, height);
    rootLayer.add(graphics().createImageLayer(bg));
    addDescrip("all layers contained in group layer with a=0.5\n" + "thus, fully composited a=0.25", offset, fullHeight + 5, fullWidth);
    // add a 50% transparent group layer
    final GroupLayer groupLayer = graphics().createGroupLayer();
    groupLayer.setAlpha(0.5f);
    rootLayer.add(groupLayer);
    assets().getImage("images/alphalayertest.png").addCallback(new Callback<Image>() {

        @Override
        public void onSuccess(Image image) {
            // add the layers over the white background
            float x = offset;
            groupLayer.addAt(graphics().createImageLayer(image).setAlpha(0.5f), x, offset);
            addDescrip("image\nimg layer a=0.5", x, offset + height, width);
            x += width;
            SurfaceImage surf1 = graphics().createSurface(image.width(), image.height());
            surf1.surface().setAlpha(0.5f).drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(surf1), x, offset);
            addDescrip("surface a=0.5\nimg layer a=1", x, offset + height, width);
            x += width;
            SurfaceImage surf2 = graphics().createSurface(image.width(), image.height());
            surf2.surface().drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(surf2).setAlpha(0.5f), x, offset);
            addDescrip("surface a=1\nimg layer a=0.5", x, offset + height, width);
            x += width;
            CanvasImage canvas1 = graphics().createImage(image.width(), image.height());
            canvas1.canvas().drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(canvas1).setAlpha(0.5f), x, offset);
            addDescrip("canvas a=1\nimg layer a=0.5", x, offset + height, width);
            x += width;
            CanvasImage canvas2 = graphics().createImage(image.width(), image.height());
            canvas2.canvas().setAlpha(0.5f).drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(canvas2), x, offset);
            addDescrip("canvas a=0.5\nimg layer a=1", x, offset + height, width);
            x += width;
            // add the same layers over the blue background
            x = offset;
            groupLayer.addAt(graphics().createImageLayer(image).setAlpha(0.5f), x, offset + 2 * height);
            x += width;
            SurfaceImage surf1b = graphics().createSurface(image.width(), image.height());
            surf1b.surface().setAlpha(0.5f).drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(surf1b), x, offset + 2 * height);
            x += width;
            SurfaceImage surf2b = graphics().createSurface(image.width(), image.height());
            surf2b.surface().drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(surf2b).setAlpha(0.5f), x, offset + 2 * height);
            x += width;
            CanvasImage canvas1b = graphics().createImage(image.width(), image.height());
            canvas1b.canvas().drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(canvas1b).setAlpha(0.5f), x, offset + 2 * height);
            x += width;
            CanvasImage canvas2b = graphics().createImage(image.width(), image.height());
            canvas2b.canvas().setAlpha(0.5f).drawImage(image, 0, 0);
            groupLayer.addAt(graphics().createImageLayer(canvas2b), x, offset + 2 * height);
            // add some copies of the image at 1, 0.5, 0.25 and 0.125 alpha
            x = offset + width;
            for (float alpha : new float[] { 1, 1 / 2f, 1 / 4f, 1 / 8f }) {
                float y = fullHeight + 50;
                rootLayer.addAt(graphics().createImageLayer(image).setAlpha(alpha), x, y);
                addDescrip("image a=" + alpha, x, y + height / 2, width / 2);
                x += width;
            }
        }

        @Override
        public void onFailure(Throwable err) {
            log().error("Error loading image", err);
        }
    });
    // add ground truth of 25% opaque image
    assets().getImage("images/alphalayertest_expected.png").addCallback(new Callback<Image>() {

        @Override
        public void onSuccess(Image image) {
            rootLayer.addAt(graphics().createImageLayer(image), 5 * width, 0);
            addDescrip("ground truth", 5 * width, offset + height, width);
        }

        @Override
        public void onFailure(Throwable err) {
            log().error("Error loading image", err);
        }
    });
}
Also used : CanvasImage(playn.core.CanvasImage) CanvasImage(playn.core.CanvasImage) SurfaceImage(playn.core.SurfaceImage) Image(playn.core.Image) GroupLayer(playn.core.GroupLayer) SurfaceImage(playn.core.SurfaceImage)

Example 3 with GroupLayer

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

the class DepthTest method init.

@Override
public void init() {
    GroupLayer rootLayer = graphics().rootLayer();
    CanvasImage image = graphics().createImage(250, 25);
    image.canvas().drawText(rootLayer.getClass().getName(), 0, 15);
    ImageLayer info = graphics().createImageLayer(image);
    info.setTranslation(5, 5);
    rootLayer.add(info);
    int[] depths = { 0, -1, 1, 3, 2, -4, -3, 4, -2 };
    int[] fills = { 0xFF99CCFF, 0xFFFFFF33, 0xFF9933FF, 0xFF999999, 0xFFFF0033, 0xFF00CC00, 0xFFFF9900, 0xFF0066FF, 0x0FFCC6666 };
    int width = 200, height = 200;
    for (int ii = 0; ii < depths.length; ii++) {
        int depth = depths[ii];
        image = graphics().createImage(width, height);
        image.canvas().setFillColor(fills[ii]);
        image.canvas().fillRect(0, 0, width, height);
        image.canvas().setFillColor(0xFF000000);
        image.canvas().drawText(depth + "/" + ii, 5, 15);
        ImageLayer layer = graphics().createImageLayer(image);
        layer.setDepth(depth);
        layer.setTranslation(225 - 50 * depth, 125 + 25 * depth);
        rootLayer.add(layer);
    }
}
Also used : CanvasImage(playn.core.CanvasImage) ImageLayer(playn.core.ImageLayer) GroupLayer(playn.core.GroupLayer)

Example 4 with GroupLayer

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

the class SurfaceDrawLayerTest method addTests.

protected void addTests(Image orange) {
    float owidth = orange.width(), oheight = orange.height();
    float twidth = 3 * owidth, theight = oheight;
    // create a small subgraph of a couple of image layers
    final GroupLayer group = graphics().createGroupLayer();
    group.addAt(graphics().createImageLayer(orange), owidth / 2, 0);
    rotator = graphics().createImageLayer(orange);
    group.addAt(rotator.setOrigin(owidth / 2, oheight / 2), 2 * owidth, oheight / 2);
    rotator.setRotation(FloatMath.PI / 2);
    // add the subgraph to the scene directly
    final float gap = 25;
    float x = gap, y = gap;
    graphics().rootLayer().addAt(group, x, y);
    addDescrip("Added to rootLayer", x, y + theight + gap, twidth);
    x += twidth + gap;
    // render the subgraph in an immediate layer, with a few variants
    graphics().rootLayer().addAt(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            // since the group layer has a +gap+gap offset, we adjust for that here
            surf.translate(-gap, -gap).drawLayer(group);
        }
    }), x, y);
    addDescrip("ImmediateLayer drawLayer", x, y + theight + gap, twidth);
    x += twidth + gap;
    graphics().rootLayer().addAt(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.translate(-gap, -gap).drawLayer(group);
        }
    }).setTint(0xFFDD0000), x, y);
    addDescrip("ImmediateLayer drawLayer (with tint)", x, y + theight + gap, twidth);
    x += twidth + gap;
    graphics().rootLayer().addAt(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.translate(-gap, -gap).drawLayer(group);
        }
    }).setShader(createSepiaShader()), x, y);
    addDescrip("ImmediateLayer drawLayer (with sepia shader)", x, y + theight + gap, twidth);
    x += twidth + gap;
    // lastly create a surface image and render the layer thereinto; this one won't animate
    SurfaceImage image = graphics().createSurface(twidth, theight);
    image.surface().translate(-gap, -gap).drawLayer(group);
    graphics().rootLayer().addAt(graphics().createImageLayer(image), x, y);
    addDescrip("SurfaceImage drawLayer (won't animate)", x, y + theight + gap, twidth);
    x += twidth + gap;
}
Also used : ImmediateLayer(playn.core.ImmediateLayer) GroupLayer(playn.core.GroupLayer) SurfaceImage(playn.core.SurfaceImage) Surface(playn.core.Surface)

Example 5 with GroupLayer

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

the class SurfaceTest method addTests.

protected void addTests(final Image orange, Image tile) {
    final Pattern pattern = tile.toPattern();
    // make samples big enough to force a buffer size increase
    final int samples = 128, hsamples = samples / 2;
    final float[] verts = new float[(samples + 1) * 4];
    final int[] indices = new int[samples * 6];
    tessellateCurve(0, 40 * (float) Math.PI, verts, indices, new F() {

        public float apply(float x) {
            return (float) Math.sin(x / 20) * 50;
        }
    });
    float ygap = 20, ypos = 10;
    // draw some wide lines
    ypos = ygap + addTest(10, ypos, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            drawLine(surf, 0, 0, 50, 50, 15);
            drawLine(surf, 70, 50, 120, 0, 10);
            drawLine(surf, 0, 70, 120, 120, 10);
        }
    }, 120, 120, "drawLine with width");
    ypos = ygap + addTest(20, ypos, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFF0000FF).fillRect(0, 0, 100, 25);
            // these two alpha fills should look the same
            surf.setFillColor(0x80FF0000).fillRect(0, 0, 50, 25);
            surf.setAlpha(0.5f).setFillColor(0xFFFF0000).fillRect(50, 0, 50, 25).setAlpha(1f);
        }
    }, 100, 25, "left and right half both same color");
    ypos = ygap + addTest(20, ypos, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFF0000FF).fillRect(0, 0, 100, 50);
            surf.setAlpha(0.5f);
            surf.drawImage(orange, 55, 5);
            surf.fillRect(0, 50, 50, 50);
            surf.drawImage(orange, 55, 55);
            surf.setAlpha(1f);
        }
    }, 100, 100, "fillRect and drawImage at 50% alpha");
    ypos = 10;
    ypos = ygap + addTest(160, ypos, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            // fill some shapes with patterns
            surf.setFillPattern(pattern).fillRect(10, 0, 100, 100);
            // use same fill pattern for the triangles
            surf.translate(0, 160);
            // render a sliding window of half of our triangles to test the slice rendering
            surf.fillTriangles(verts, offset * 4, (hsamples + 1) * 4, indices, offset * 6, hsamples * 6, offset * 2);
            offset += doff;
            if (offset == 0)
                doff = 1;
            else if (offset == hsamples)
                doff = -1;
        }

        private int offset = 0, doff = 1;
    }, 120, 210, "ImmediateLayer patterned fillRect, fillTriangles");
    SurfaceImage patted = graphics().createSurface(100, 100);
    patted.surface().setFillPattern(pattern).fillRect(0, 0, 100, 100);
    ypos = ygap + addTest(170, ypos, graphics().createImageLayer(patted), "SurfaceImage patterned fillRect");
    ypos = 10;
    // fill a patterned quad in a clipped group layer
    final int twidth = 150, theight = 75;
    GroupLayer group = graphics().createGroupLayer();
    ypos = ygap + addTest(315, 10, group, twidth, theight, "Clipped pattern should not exceed grey rectangle");
    group.add(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFFCCCCCC).fillRect(0, 0, twidth, theight);
        }
    }));
    group.add(graphics().createImmediateLayer(twidth, theight, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillPattern(pattern).fillRect(-10, -10, twidth + 20, theight + 20);
        }
    }));
    // draw some randomly jiggling dots inside a bounded region
    dotBox = new Rectangle(315, ypos, 200, 100);
    ypos = ygap + addTest(dotBox.x, dotBox.y, new ImmediateLayer.Renderer() {

        public void render(Surface surf) {
            surf.setFillColor(0xFFCCCCCC).fillRect(0, 0, dotBox.width, dotBox.height);
        }
    }, dotBox.width, dotBox.height, "Randomly positioned SurfaceImages");
    for (int ii = 0; ii < 10; ii++) {
        SurfaceImage dot = graphics().createSurface(10, 10);
        dot.surface().setFillColor(0xFFFF0000);
        dot.surface().fillRect(0, 0, 5, 5);
        dot.surface().fillRect(5, 5, 5, 5);
        dot.surface().setFillColor(0xFF0000FF);
        dot.surface().fillRect(5, 0, 5, 5);
        dot.surface().fillRect(0, 5, 5, 5);
        ImageLayer dotl = graphics().createImageLayer(dot);
        dotl.setTranslation(dotBox.x + random() * (dotBox.width - 10), dotBox.y + random() * (dotBox.height - 10));
        dots.add(dotl);
        // System.err.println("Created dot at " + dotl.transform());
        graphics().rootLayer().add(dotl);
    }
    // add a surface layer that is updated on every call to paint (a bad practice, but one that
    // should actually work)
    paintUpped = graphics().createSurface(100, 100);
    ypos = ygap + addTest(315, ypos, graphics().createImageLayer(paintUpped), "SurfaceImage updated in paint()");
}
Also used : Pattern(playn.core.Pattern) ImmediateLayer(playn.core.ImmediateLayer) Rectangle(pythagoras.f.Rectangle) ImageLayer(playn.core.ImageLayer) SurfaceImage(playn.core.SurfaceImage) Surface(playn.core.Surface) 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