Ersen Osman
Ersen Osman

Reputation: 7247

Libgdx, How to set Alpha on a Bitmap Font?

Today I would like to change the alpha of a bitmap font in LibGdx

I discovered that the BitmapFont class has a setColor method so I pass in the following. Note I do not want to change the colour so I set these these values to the same

 myBitmapFont.setColor(myColour.r,
                       myColour.g,
                       myColour.b,
                       MY_ALPHA)

And my alpha is

 MY_ALPHA= 0.1f

However, the alpha is not applying, anyone know what I am doing wrong here?

Thanks

Upvotes: 0

Views: 230

Answers (1)

Ersen Osman
Ersen Osman

Reputation: 7247

I realised my error. I forgot that I don't draw the BitmapFont directly but use the GlyphLayout instance to perform the text drawing (because GlyphLayout performs text measuring)

internal fun BitmapFont.drawFontInCenterOfContainer(batch: Batch, glyphLayout: GlyphLayout, containerWidth: Float, containerHeight: Float, offsetX: Float = 0f, offsetY: Float = 0f) {
    this.draw(
            batch,
            glyphLayout,
            offsetX + containerWidth / 2 - glyphLayout.width / 2,
            offsetY + containerHeight / 2 + glyphLayout.height / 2)
}

So even though I called setColor on the BitmapFont instance, It did not apply because I must also call setText on the GlyphLayout

 myBitmapFont.setColor(myColour.r,
                   myColour.g,
                   myColour.b,
                   MY_ALPHA)

glyphLayout.setText(myBitmapFont, myText)

And looking at the setText method of GlyphLayout

/** Calls {@link #setText(BitmapFont, CharSequence, int, int, Color, float, int, boolean, String) setText} with the whole
     * string, the font's current color, and no alignment or wrapping. */
    public void setText (BitmapFont font, CharSequence str) {
        setText(font, str, 0, str.length(), font.getColor(), 0, Align.left, false, null);
    }

It applies the BitmapFont colour

Upvotes: 1

Related Questions