use of playn.core.TextLayout in project playn by threerings.
the class PointerMouseTouchTest method createLabel.
protected ImageLayer createLabel(String text, GroupLayer parent, int fg, int bg, float x, float y, float padding) {
TextLayout layout = graphics().layoutText(text, baseFormat);
float twidth = layout.width() + padding * 2;
float theight = layout.height() + padding * 2;
CanvasImage image = graphics().createImage(twidth, theight);
if (bg != 0) {
image.canvas().setFillColor(bg);
image.canvas().fillRect(0, 0, twidth, theight);
}
image.canvas().setFillColor(fg);
image.canvas().fillText(layout, padding, padding);
ImageLayer imageLayer = graphics().createImageLayer(image);
imageLayer.setTranslation(x, y);
parent.add(imageLayer);
return imageLayer;
}
use of playn.core.TextLayout in project playn by threerings.
the class SoundTest method addAction.
protected void addAction(String action) {
_actions.add(0, action);
if (_actions.size() > 10)
_actions.subList(10, _actions.size()).clear();
_actionsImage.canvas().clear();
StringBuilder buf = new StringBuilder();
for (String a : _actions) {
if (buf.length() > 0)
buf.append("\n");
buf.append(a);
}
_actionsImage.canvas().setFillColor(0xFF000000);
float y = 0;
for (TextLayout layout : graphics().layoutText(buf.toString(), TEXT_FMT, new TextWrap(300))) {
_actionsImage.canvas().fillText(layout, 0, y);
y += layout.ascent() + layout.descent() + layout.leading();
}
}
use of playn.core.TextLayout in project playn by threerings.
the class Test method formatText.
protected static CanvasImage formatText(TextFormat format, String text, boolean border) {
TextLayout layout = graphics().layoutText(text, format);
float margin = border ? 10 : 0;
float width = layout.width() + 2 * margin, height = layout.height() + 2 * margin;
CanvasImage image = graphics().createImage(width, height);
image.canvas().setStrokeColor(0xFF000000);
image.canvas().setFillColor(0xFF000000);
image.canvas().fillText(layout, margin, margin);
if (border)
image.canvas().strokeRect(0, 0, width - 1, height - 1);
return image;
}
use of playn.core.TextLayout 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())));
}
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();
}
}
Aggregations