Search code examples
androidjsonbuttonlibgdxscene2d

Is it possible to use two fonts in TextButtonStyle in LibGDX using JSON?


I want to use JSON to set up my button in LibGDX. I have two fonts (carterone_red.fnt, carterone_green.fnt). When the button is down, it use carterone_red font and when the button is up, it turns carterone_green.fnt. Is it possible?

The following code is not working.

Thanks, Vincent

 {
    com.badlogic.gdx.graphics.g2d.BitmapFont:{
    carterone-font: { file: fonts/carterone_green.fnt }
    carterone-font2: { file: fonts/carterone_red.fnt }
    },
    com.badlogic.gdx.graphics.Color: {
        green: { a: 1, b: 0, g: 1, r: 0 },
        white: { a: 1, b: 1, g: 1, r: 1 },
        red: { a: 1, b: 0, g: 0, r: 1 },
        black: { a: 1, b: 0, g: 0, r: 0 }
    },
    com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
        carterone: { down: button2,font: carterone2-font, up: button1, font: carterone-font, fontColor: white }  
    }
    }

Solution

  • If I understand your question right then you want your button to be in green font if it is up and in red font if it is down. Then the easiest approach I see is:

    1. create one BitmapFont, which should be your "carterone" font, but in white (!) color
    2. then in your skin json file you can set the attributes fontColor & downFontColor to set red & green color accordingly.

    ==> in the end your skin.json will look more or less like this:

    {
    com.badlogic.gdx.graphics.g2d.BitmapFont:{
        carterone-font: { file: fonts/carterone_white.fnt }
    },
    com.badlogic.gdx.graphics.Color: {
        green: { a: 1, b: 0, g: 1, r: 0 },
        white: { a: 1, b: 1, g: 1, r: 1 },
        red: { a: 1, b: 0, g: 0, r: 1 },
        black: { a: 1, b: 0, g: 0, r: 0 }
    },
    com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
        carterone: { down: button2, up: button1, fontColor: green, downFontColor: red }  
    }
    }