0

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);
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65

2 Answers2

2

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

Michael Michailidis
  • 1,002
  • 1
  • 8
  • 21
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • layout = new GlyphLayout(font,"LOADING SCREEN", Color.BLACK,3, Align.center,true); when i increase target width from 1 to 2 nothing happens ,but from 1 to 3 whole text become verticle...so how increase or decrease in target width relates to wrap parameter. – Shubham Tripathi Jul 03 '18 at 18:04
  • @ShubhamTripathi please check my updated answer for practical pictorial explanation – Abhishek Aryan Jul 04 '18 at 07:56
  • if I increase targetWidth from 250 to 255 the result is still the same why is that...?and thanks for the effort you made.. – Shubham Tripathi Jul 05 '18 at 17:45
  • It depends(many cases), if you choose left align and unwrap then result will always constant in all variation of targetwidth. If text width is less than targetwidth then wrap not work because no new lines are required and so many cases when you get constant result inspite of different targetwidth value. – Abhishek Aryan Jul 05 '18 at 17:54
0

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).

TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37