What is targetWidth in glyphLayout

931 Views Asked by At

What does parameter targetWidth exactly means here and what it has to do with wrap parameter?

public GlyphLayout (BitmapFont font, CharSequence str, Color color, float targetWidth, int halign, boolean wrap) {
    setText(font, str, color, targetWidth, halign, wrap);
}
2

There are 2 best solutions below

4
AAryan On

From doc :

targetWidth : The width used for alignment, line wrapping, and truncation. May be zero if those features are not used.

wrap : If true then (a word or unit of text) to be carried over to a new line automatically as the margin is reached, or to fit around embedded features.

If false, the text will only wrap where it contains newlines (\n).

EDIT

Test code : Practical demonstration, how targetWidth and wrap works in GlyphLayout constructor.

public class MyGdxGame extends ApplicationAdapter {

    private GlyphLayout glyphLayout[];
    private BitmapFont bitmapFont;
    private float targetWidth=250;
    private ShapeRenderer shapeRenderer;
    private SpriteBatch spriteBatch;
    private float xPos[]={450,450,450,30,450,880};
    private float yPos[]={550,480,410,340,340,340};

    @Override
    public void create () {
        spriteBatch=new SpriteBatch();
        shapeRenderer=new ShapeRenderer();
        shapeRenderer.setAutoShapeType(true);
        bitmapFont=new BitmapFont(Gdx.files.internal("skin/poet.fnt"));

        glyphLayout=new GlyphLayout[6];
        glyphLayout[0]=new GlyphLayout(bitmapFont, "LOADING SCREENsssssssssssssssssssssssssssssssssssss", Color.BLACK, targetWidth, Align.left, false);
        glyphLayout[1]=new GlyphLayout(bitmapFont, "LOADING SCREENsssssssssssssssssssssssssssssssssssss", Color.BLACK, targetWidth, Align.right, false);
        glyphLayout[2]=new GlyphLayout(bitmapFont, "LOADING SCREENsssssssssssssssssssssssssssssssssssss", Color.BLACK, targetWidth, Align.center, false);
        glyphLayout[3]=new GlyphLayout(bitmapFont, "LOADING SCREENsssssssssssssssssssssssssssssssssssss", Color.BLACK, targetWidth, Align.left, true);
        glyphLayout[4]=new GlyphLayout(bitmapFont, "LOADING SCREENsssssssssssssssssssssssssssssssssssss", Color.BLACK, targetWidth, Align.right, true);
        glyphLayout[5]=new GlyphLayout(bitmapFont, "LOADING SCREENsssssssssssssssssssssssssssssssssssss", Color.BLACK, targetWidth, Align.center, true);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        spriteBatch.begin();
        for (int i=0;i<glyphLayout.length;i++) {
            bitmapFont.draw(spriteBatch, glyphLayout[i], xPos[i], yPos[i]);
        }
        spriteBatch.end();

        shapeRenderer.begin();
        shapeRenderer.setColor(Color.BLUE);
        shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
        for (int i=0;i<glyphLayout.length;i++) {
                shapeRenderer.rectLine(xPos[i], yPos[i], xPos[i] + targetWidth, yPos[i], 3f);
        }
        shapeRenderer.end();
    }

    @Override
    public void dispose () {
        bitmapFont.dispose();
        shapeRenderer.dispose();
        spriteBatch.dispose();
    }
}

Output :

enter image description here

0
TheChubbyPanda On

Target width is the lenient maximum width that the text will be before it is wrapped, truncated or otherwise altered from being a single line.

e.g. Consider a string of width 500 (when drawn). With a target width of 250, it will be wrapped at ~250 into 2 lines (if wrap is true).