Example usage for com.badlogic.gdx.graphics.g2d GlyphLayout setText

List of usage examples for com.badlogic.gdx.graphics.g2d GlyphLayout setText

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d GlyphLayout setText.

Prototype

public void setText(BitmapFont font, CharSequence str, int start, int end, Color color, float targetWidth,
        int halign, boolean wrap, String truncate) 

Source Link

Usage

From source file:spaceisnear.game.ui.TextField.java

@Override
public void paint(Batch batch) {
    if (checkKeys(keycode)) {
        keycode = 0;/*from   w  w w. jav a2  s.  co  m*/
    }
    ShapeRenderer renderer = getRenderer();
    focused = getStage().getKeyboardFocus() == this;
    int start = 0;
    int end = text.length();
    int startingXText = 0;
    //TODO
    GlyphLayout glyphLayout = new GlyphLayout(font, text);
    if (glyphLayout.width > getWidthWithoutPaddings()) {
        end = text.length();
        start = end - 1;
        glyphLayout.setText(font, text, start, end, textColor, getWidthWithoutPaddings(), 0, false, null);
        while (start > 0 && glyphLayout.width < getWidthWithoutPaddings()) {
            start--;
        }
        glyphLayout.setText(font, text, 0, start, textColor, getWidthWithoutPaddings(), 0, false, null);
        startingXText = (int) -glyphLayout.width;
    }
    //
    {
        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        renderer.setProjectionMatrix(batch.getProjectionMatrix());
        renderer.begin(ShapeRenderer.ShapeType.Filled);
        renderer.setColor(backgroundColor);
        renderer.rect(0, 0, getWidth(), getHeight());
        renderer.end();
        Gdx.gl.glDisable(GL20.GL_BLEND);
    }
    {
        renderer.begin(ShapeRenderer.ShapeType.Line);
        renderer.setColor(borderColor);
        renderer.rect(0, 0, getWidth() + 1, getHeight());
        renderer.end();
    }
    //cursor
    renderer.begin(ShapeRenderer.ShapeType.Line);
    font.setColor(Color.BLACK);
    if (focused) {
        glyphLayout.setText(font, text, 0, currentPosition, textColor, getWidthWithoutPaddings(), 0, false,
                null);
        final float x = 10 + glyphLayout.width + startingXText;
        renderer.line(x, 3, x, font.getLineHeight() + 2);
        renderer.line(x + 1, 3, x + 1, font.getLineHeight() + 2);
    }
    renderer.end();
    batch.begin();
    font.setColor(Color.BLACK);
    font.draw(batch, text.subSequence(start, end), WIDTH_PADDING + getX(), 3 + getY());
    batch.end();
}