libGDX: widgets clàssics

libGDX treballa principalment amb l'objecte SpriteBatch renderitzant bitmaps. Però disposa també d'alguns widgets «clàssics» com Button , TextView , etc. No són el cas d'ús més habitual, però per facilitar la seva creació existeix la biblioteca Scene2D UI.

, , , , , , ,

Per afegir la funcionalitat de widgets standard com botons, textview, etc. caldrà:

Podeu descarregar els arxius (assets) de skin bàsic que es poden trobar aquí:

I després podem provar aquest codi amb un botó i un textView:

Main.java
package com.myuser.myapp;
 
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
 
public class Main extends ApplicationAdapter {
    private Stage stage;
    private Skin skin;
 
    @Override
    public void create() {
        // Crear un Stage i un Skin
        stage = new Stage(new ScreenViewport());
        skin = new Skin(Gdx.files.internal("uiskin.json")); // Carregar un Skin per defecte
 
        // Crear un Label (TextView)
        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font = new BitmapFont(); // Font per defecte
        Label label = new Label("Hola, això és un TextView", labelStyle);
        label.setPosition(100, 400); // Posició del Label
 
        // Crear un Button
        Button button = new TextButton("Clica'm!", skin);
        button.setPosition(100, 300); // Posició del Button
        button.setSize(200, 50); // Mida del Button
 
        // Afegir els actors al Stage
        stage.addActor(label);
        stage.addActor(button);
 
        // Configurar l'Stage com a gestor d'entrada
        Gdx.input.setInputProcessor(stage);
    }
 
    @Override
    public void render() {
        // Netejar la pantalla
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
 
        // Actualitzar i dibuixar l'Stage
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }
 
    @Override
    public void dispose() {
        // Alliberar recursos
        stage.dispose();
        skin.dispose();
    }
}