Search in sources :

Example 1 with TextFormat

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

the class TextTest method makeTextImage.

protected Image makeTextImage() {
    TextFormat format = new TextFormat(graphics().createFont(font.value(), style.value(), 24), true);
    float wrapWidth = wrap.value() == 0 ? Float.MAX_VALUE : graphics().width() * wrap.value() / 100;
    TextBlock block = new TextBlock(graphics().layoutText(sample, format, new TextWrap(wrapWidth)));
    float awidth = adjustWidth(block.bounds.width()), aheight = adjustHeight(block.bounds.height());
    float pad = TextBlock.pad();
    CanvasImage image = graphics().createImage(awidth + 2 * pad, aheight + 2 * pad);
    image.canvas().translate(pad, pad);
    image.canvas().setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, awidth, aheight);
    render(image.canvas(), block, align.value(), lineBounds.value());
    return image;
}
Also used : CanvasImage(playn.core.CanvasImage) TextFormat(playn.core.TextFormat) TextBlock(playn.core.util.TextBlock) TextWrap(playn.core.TextWrap)

Example 2 with TextFormat

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

the class TextTest method init.

@Override
public void init() {
    row = new Rectangle(5, 5, 0, 0);
    addToRow((style = new NToggle<Style>("Style", Style.PLAIN, Style.BOLD, Style.ITALIC, Style.BOLD_ITALIC)).layer);
    addToRow((draw = new NToggle<String>("Draw", "Fill", "Stroke")).layer);
    addToRow((effect = new NToggle<String>("Effect", "None", "ShadowUL", "ShadowLR", "Outline")).layer);
    addToRow((wrap = new NToggle<Integer>("Wrap", 0, 20, 50, 100)).layer);
    addToRow((align = new NToggle<TextBlock.Align>("Align", TextBlock.Align.LEFT, TextBlock.Align.CENTER, TextBlock.Align.RIGHT)).layer);
    addToRow((font = new NToggle<String>("Font", "Times New Roman", "Helvetica")).layer);
    class SetText extends Pointer.Adapter implements Callback<String> {

        final ImageLayer layer = graphics().createImageLayer(TestsGame.makeButtonImage("Set Text"));

        {
            layer.addListener(this);
        }

        @Override
        public void onPointerEnd(Event event) {
            keyboard().getText(TextType.DEFAULT, "Test text", sample.replace("\n", "\\n"), this);
        }

        public void onSuccess(String result) {
            if (result == null)
                return;
            // parse \n to allow testing line breaks
            sample = result.replace("\\n", "\n");
            update();
        }

        public void onFailure(Throwable cause) {
        }
    }
    addToRow(new SetText().layer);
    addToRow((lineBounds = new Toggle("Lines")).layer);
    // test laying out the empty string
    TextLayout layout = graphics().layoutText("", new TextFormat());
    ImageLayer empty = graphics().createImageLayer(makeLabel("Empty string size " + layout.width() + "x" + layout.height()));
    newRow();
    addToRow(empty);
    newRow();
    addToRow((text = graphics().createImageLayer(makeTextImage())));
}
Also used : Rectangle(pythagoras.f.Rectangle) IRectangle(pythagoras.f.IRectangle) ImageLayer(playn.core.ImageLayer) TextLayout(playn.core.TextLayout) Callback(playn.core.util.Callback) Event(playn.core.Pointer.Event) TextFormat(playn.core.TextFormat) TextBlock(playn.core.util.TextBlock)

Example 3 with TextFormat

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

the class PauseResumeTest method updateDisplay.

protected void updateDisplay() {
    StringBuffer buf = new StringBuffer();
    if (notifications.isEmpty()) {
        buf.append("No notifications. Pause and resume the game to generate some.");
    } else {
        buf.append("Notifications:\n");
        for (String note : notifications) buf.append(note).append("\n");
    }
    TextLayout layout = graphics().layoutText(buf.toString(), new TextFormat());
    CanvasImage image = graphics().createImage(layout.width(), layout.height());
    image.canvas().setFillColor(0xFF000000);
    image.canvas().fillText(layout, 0, 0);
    layer.setImage(image);
}
Also used : CanvasImage(playn.core.CanvasImage) TextFormat(playn.core.TextFormat) TextLayout(playn.core.TextLayout)

Example 4 with TextFormat

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

the class ScaledTextTest method init.

@Override
public void init() {
    String text = "The quick brown fox jumped over the lazy dog.";
    TextFormat format = new TextFormat().withFont(graphics().createFont("Helvetica", Font.Style.PLAIN, 18));
    TextBlock block = new TextBlock(graphics().layoutText(text, format, new TextWrap(100)));
    float x = 5;
    for (float scale : new float[] { 1f, 2f, 3f }) {
        float swidth = block.bounds.width() * scale, sheight = block.bounds.height() * scale;
        CanvasImage image = graphics().createImage(swidth, sheight);
        image.canvas().setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, swidth - 0.5f, sheight - 0.5f);
        image.canvas().scale(scale, scale);
        image.canvas().setFillColor(0xFF000000);
        block.fill(image.canvas(), TextBlock.Align.RIGHT, 0, 0);
        graphics().rootLayer().addAt(graphics().createImageLayer(image), x, 5);
        addInfo(image, x + swidth / 2, sheight + 10);
        x += swidth + 5;
    }
}
Also used : CanvasImage(playn.core.CanvasImage) TextFormat(playn.core.TextFormat) TextBlock(playn.core.util.TextBlock) TextWrap(playn.core.TextWrap)

Example 5 with TextFormat

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

the class CanvasTest method init.

@Override
public void init() {
    nextX = nextY = GAP;
    lastSecs = -1;
    addTestCanvas("radial fill gradient", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            canvas.setFillGradient(graphics().createRadialGradient(0, 0, 50, new int[] { 0xFFFF0000, 0xFF00FF00 }, new float[] { 0, 1 }));
            canvas.fillRect(0, 0, 100, 100);
        }
    });
    addTestCanvas("linear fill gradient", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            canvas.setFillGradient(graphics().createLinearGradient(0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }));
            canvas.fillRect(0, 0, 100, 100);
        }
    });
    addTestCanvas("image fill pattern", 100, 100, "images/tile.png", new ImageDrawer() {

        public void draw(Canvas canvas, Image tile) {
            canvas.setFillPattern(tile.toPattern());
            canvas.fillRect(0, 0, 100, 100);
        }
    });
    addTestCanvas("lines and circles", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            canvas.setFillColor(0xFF99CCFF);
            canvas.fillRect(0, 0, 100, 100);
            // draw a point and some lines
            canvas.setStrokeColor(0xFFFF0000);
            canvas.drawPoint(50, 50);
            canvas.drawLine(0, 25, 100, 25);
            canvas.drawLine(0, 75, 100, 75);
            canvas.drawLine(25, 0, 25, 100);
            canvas.drawLine(75, 0, 75, 100);
            // stroke and fill a circle
            canvas.strokeCircle(25, 75, 10);
            canvas.setFillColor(0xFF0000FF);
            canvas.fillCircle(75, 75, 10);
        }
    });
    addTestCanvas("image, subimage", 100, 100, "images/orange.png", new ImageDrawer() {

        public void draw(Canvas canvas, Image orange) {
            canvas.setFillColor(0xFF99CCFF);
            canvas.fillRect(0, 0, 100, 100);
            // draw an image normally, scaled, cropped, cropped and scaled, etc.
            float half = 37 / 2f;
            canvas.drawImage(orange, 10, 10);
            canvas.drawImage(orange, 55, 10, 37, 37, half, half, half, half);
            canvas.drawImage(orange, 10, 55, 37, 37, half, 0, half, half);
            canvas.drawImage(orange, 55, 55, 37, 37, half, half / 2, half, half);
        }
    });
    CanvasImage repimg = createCanvasImage(30, 30, new Drawer() {

        public void draw(Canvas canvas) {
            canvas.setFillColor(0xFF99CCFF);
            canvas.fillCircle(15, 15, 15);
            canvas.setStrokeColor(0xFF000000);
            canvas.strokeRect(0, 0, 30, 30);
        }
    });
    repimg.setRepeat(true, true);
    ImageLayer layer = graphics().createImageLayer(repimg);
    layer.setSize(100, 100);
    addTestLayer("ImageLayer repeat x/y", 100, 100, layer);
    timeImg = graphics().createImage(100, 100);
    addTestLayer("updated canvas", 100, 100, graphics().createImageLayer(timeImg));
    addTestCanvas("filled bezier path", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            // draw a rounded rect with bezier curves
            Path path = canvas.createPath();
            path.moveTo(10, 0);
            path.lineTo(90, 0);
            path.bezierTo(95, 0, 100, 5, 100, 10);
            path.lineTo(100, 90);
            path.bezierTo(100, 95, 95, 100, 90, 100);
            path.lineTo(10, 100);
            path.bezierTo(5, 100, 0, 95, 0, 90);
            path.lineTo(0, 10);
            path.bezierTo(0, 5, 5, 0, 10, 0);
            path.close();
            canvas.setFillGradient(graphics().createLinearGradient(0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }));
            canvas.fillPath(path);
        }
    });
    addTestCanvas("gradient round rect", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            // draw a rounded rect directly
            canvas.setFillGradient(graphics().createLinearGradient(0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }));
            canvas.fillRoundRect(0, 0, 100, 100, 10);
        }
    });
    addTestCanvas("gradient filled text", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            // draw a rounded rect directly
            canvas.setFillGradient(graphics().createLinearGradient(0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }));
            TextLayout capF = graphics().layoutText("F", new TextFormat().withFont(F_FONT.derive(96)));
            canvas.fillText(capF, 15, 5);
        }
    });
    addTestCanvas("nested round rect", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            // demonstrates a bug (now worked around) in Android round-rect drawing
            canvas.setFillColor(0xFFFFCC99);
            canvas.fillRoundRect(0, 0, 98.32f, 29.5f, 12f);
            canvas.setFillColor(0xFF99CCFF);
            canvas.fillRoundRect(3, 3, 92.32f, 23.5f, 9.5f);
        }
    });
    addTestCanvas("android fill/stroke bug", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            float dotRadius = 40;
            canvas.save();
            canvas.setFillGradient(graphics().createRadialGradient(100 / 3, 100 / 2.5f, dotRadius, new int[] { 0xFFFFFFFF, 0xFFCC66FF }, new float[] { 0f, 1f }));
            canvas.fillCircle(50, 50, dotRadius);
            canvas.restore();
            canvas.setStrokeColor(0xFF000000);
            canvas.setStrokeWidth(1.5f);
            canvas.strokeCircle(50, 50, dotRadius);
        }
    });
    addTestCanvas("transform test", 100, 100, new Drawer() {

        public void draw(Canvas canvas) {
            canvas.setFillColor(0xFFCCCCCC).fillRect(0, 0, 50, 50);
            canvas.setFillColor(0xFFCCCCCC).fillRect(50, 50, 50, 50);
            TextLayout capF = graphics().layoutText("F", new TextFormat().withFont(F_FONT));
            float theta = -FloatMath.PI / 4, tsin = FloatMath.sin(theta), tcos = FloatMath.cos(theta);
            canvas.setFillColor(0xFF000000).fillText(capF, 0, 0);
            canvas.transform(tcos, -tsin, tsin, tcos, 50, 50);
            canvas.setFillColor(0xFF000000).fillText(capF, 0, 0);
        }
    });
    addTestCanvas("round rect precision", 100, 100, new Drawer() {

        float bwid = 4;

        void outer(Canvas canvas, float y) {
            canvas.setFillColor(0xFF000000);
            canvas.fillRect(2, y, 94, 30);
        }

        void inner(Canvas canvas, float y) {
            canvas.setFillColor(0xFF555555);
            canvas.fillRect(2 + bwid, y + bwid, 94 - bwid * 2, 30 - bwid * 2);
        }

        void stroke(Canvas canvas, float y) {
            canvas.setStrokeColor(0xFF99CCFF);
            canvas.setStrokeWidth(bwid);
            canvas.strokeRoundRect(2 + bwid / 2, y + bwid / 2, 94 - bwid, 30 - bwid, 10);
        }

        public void draw(Canvas canvas) {
            float y = 1;
            outer(canvas, y);
            inner(canvas, y);
            stroke(canvas, y);
            y += 34;
            outer(canvas, y);
            stroke(canvas, y);
            inner(canvas, y);
            y += 34;
            stroke(canvas, y);
            outer(canvas, y);
            inner(canvas, y);
        }
    });
    Image tileimg = assets().getImage("images/tile.png");
    tileimg.setRepeat(true, true);
    addTestLayer("img layer anim setWidth", 100, 100, tileLayer = graphics().createImageLayer(tileimg));
    tileLayer.setSize(0, 100);
}
Also used : Path(playn.core.Path) CanvasImage(playn.core.CanvasImage) Canvas(playn.core.Canvas) ImageLayer(playn.core.ImageLayer) TextFormat(playn.core.TextFormat) CanvasImage(playn.core.CanvasImage) Image(playn.core.Image) TextLayout(playn.core.TextLayout)

Aggregations

TextFormat (playn.core.TextFormat)6 CanvasImage (playn.core.CanvasImage)5 TextLayout (playn.core.TextLayout)4 TextBlock (playn.core.util.TextBlock)3 ImageLayer (playn.core.ImageLayer)2 TextWrap (playn.core.TextWrap)2 Canvas (playn.core.Canvas)1 Image (playn.core.Image)1 Path (playn.core.Path)1 Event (playn.core.Pointer.Event)1 Callback (playn.core.util.Callback)1 IRectangle (pythagoras.f.IRectangle)1 Rectangle (pythagoras.f.Rectangle)1