Search in sources :

Example 1 with Callback

use of playn.core.util.Callback 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 2 with Callback

use of playn.core.util.Callback in project playn by threerings.

the class GetTextTest method init.

@Override
public void init() {
    String instructions = "Click one of the buttons below to display the text entry UI.";
    ImageLayer instLayer = graphics().createImageLayer(formatText(instructions, false));
    graphics().rootLayer().addAt(instLayer, 50, 50);
    String last = storage().getItem("last_text");
    if (last == null)
        last = "...";
    final ImageLayer outputLayer = graphics().createImageLayer(formatText(last, false));
    graphics().rootLayer().addAt(outputLayer, 50, 150);
    final Callback<String> onGotText = new Callback<String>() {

        public void onSuccess(String text) {
            outputLayer.setImage(formatText(text == null ? "canceled" : text, false));
            if (text != null)
                storage().setItem("last_text", text);
        }

        public void onFailure(Throwable cause) {
            outputLayer.setImage(formatText(cause.getMessage(), false));
        }
    };
    float x = 50;
    for (final Keyboard.TextType type : Keyboard.TextType.values()) {
        ImageLayer button = createButton(type.toString(), new Runnable() {

            public void run() {
                keyboard().getText(Keyboard.TextType.DEFAULT, "Enter " + type + " text:", "", onGotText);
            }
        });
        graphics().rootLayer().addAt(button, x, 100);
        x += button.width() + 10;
    }
}
Also used : Callback(playn.core.util.Callback) Keyboard(playn.core.Keyboard) ImageLayer(playn.core.ImageLayer)

Aggregations

ImageLayer (playn.core.ImageLayer)2 Callback (playn.core.util.Callback)2 Keyboard (playn.core.Keyboard)1 Event (playn.core.Pointer.Event)1 TextFormat (playn.core.TextFormat)1 TextLayout (playn.core.TextLayout)1 TextBlock (playn.core.util.TextBlock)1 IRectangle (pythagoras.f.IRectangle)1 Rectangle (pythagoras.f.Rectangle)1