====== 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 [[https://libgdx.com/wiki/graphics/2d/scene2d/scene2d-ui|biblioteca Scene2D UI]]. {{tag> #Dam #DamMp08 #DamMp08Uf3 #DamMp08Uf03 #DamMp08Uf3 Android mobile java }} Per afegir la funcionalitat de widgets standard com botons, textview, etc. caldrà: * Afegir un //skin// amb arxius de imatges i fonts. * Utilitzar l'objecte ''Stage'' que ens renderitzarà l'escena amb els //widgets//. Podeu descarregar els [[https://github.com/libgdx/libgdx-skins/tree/master/skins/visui/assets|arxius (assets) de skin bàsic que es poden trobar aquí]]: * default.fnt * font-small.fnt * uiskin.atlas * uiskin.json * uiskin.png I després podem provar aquest codi amb un botó i un textView: 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(); } }