use of playn.core.TextLayout in project playn by threerings.
the class TextTest method render.
protected void render(Canvas canvas, String strokeFill, TextBlock block, TextBlock.Align align, int color, float x, float y, boolean showBounds) {
float sy = y + block.bounds.y();
for (TextLayout layout : block.lines) {
float sx = x + block.bounds.x() + align.getX(layout.width(), block.bounds.width() - block.bounds.x());
if (showBounds) {
IRectangle lbounds = layout.bounds();
canvas.setStrokeColor(0xFFFFCCCC).setStrokeWidth(1);
canvas.strokeRect(sx + lbounds.x(), sy + lbounds.y(), lbounds.width(), lbounds.height());
}
if (strokeFill.equals("Fill")) {
canvas.setFillColor(color).fillText(layout, sx, sy);
} else {
canvas.setStrokeColor(color).strokeText(layout, sx, sy);
}
sy += layout.ascent() + layout.descent() + layout.leading();
}
}
use of playn.core.TextLayout 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);
}
use of playn.core.TextLayout in project playn by threerings.
the class ScaledTextTest method addInfo.
protected void addInfo(CanvasImage image, float cx, float y) {
TextLayout ilayout = graphics().layoutText(image.width() + "x" + image.height(), infoFormat);
CanvasImage iimage = graphics().createImage(ilayout.width(), ilayout.height());
iimage.canvas().setFillColor(0xFF000000).fillText(ilayout, 0, 0);
graphics().rootLayer().addAt(graphics().createImageLayer(iimage), cx - iimage.width() / 2, y);
}
use of playn.core.TextLayout 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);
}
use of playn.core.TextLayout in project playn by threerings.
the class TextTest method makeLabel.
protected Image makeLabel(String label) {
TextLayout layout = graphics().layoutText(label, new TextFormat());
CanvasImage image = graphics().createImage(layout.width(), layout.height());
image.canvas().setFillColor(0xFF000000);
image.canvas().fillText(layout, 0, 0);
return image;
}
Aggregations