Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class TestMenu implements ApplicationListener, GameContext {
public interface ButtonListener {
public void clicked(GameContext context);
}
public enum ButtonActions implements ButtonListener {
PLAY("Play Game") {
@Override
public void clicked(GameContext context) {
context.enterGame();
}
},
EXIT("Exit") {
@Override
public void clicked(GameContext context) {
context.exit();
}
};
public final String text;
ButtonActions(String text) {
this.text = text;
}
}
Stage stage;
Skin skin;
Table menuRoot;
@Override
public void create() {
//set up a new stage
stage = new Stage();
// Requires the following skin/font files to be placed in "assets/data"
// of your Android project
//https://raw.github.com/libgdx/libgdx/master/tests/gdx-tests-android/assets/data/uiskin.json
//https://raw.github.com/libgdx/libgdx/master/tests/gdx-tests-android/assets/data/uiskin.atlas
//https://raw.github.com/libgdx/libgdx/master/tests/gdx-tests-android/assets/data/uiskin.png
//https://raw.github.com/libgdx/libgdx/master/tests/gdx-tests-android/assets/data/default.fnt
//https://raw.github.com/libgdx/libgdx/master/tests/gdx-tests-android/assets/data/default.png
// Presumably your game would use a custom GUI skin eventually ...
//create a basic GUI skin
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
//make sure input goes through our stage
Gdx.input.setInputProcessor(stage);
//initialize our menu GUI
setupGUI();
//load the menu state
enterMenu();
}
public void setupGUI() {
//let's say your game implements GameContext
final GameContext context = this;
//the "root" container; which fills the screen
menuRoot = new Table(skin);
menuRoot.setFillParent(true);
Table table = new Table(skin);
//add each button to the menu
final int BUTTON_WIDTH = 100;
for (final ButtonActions a : ButtonActions.values()) {
//create a new text button.. we could use ImageButton instead
TextButton btn = new TextButton(a.text, skin);
//add a listener
btn.addListener(new ClickListener() {
public void clicked(InputEvent e, float x, float y) {
a.clicked(context);
}
});
//add the button with a fixed width
table.add(btn).width(BUTTON_WIDTH);
//then move down a row
table.row();
}
//ad the button group to the root
menuRoot.add(table);
}
@Override
public void resize(int width, int height) {
//resize the stage to the new window size
stage.setViewport(width, height, false);
}
@Override
public void render() {
//clear the screen
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//update and draw the stage UI
stage.act();
stage.draw();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
//gameContext interface methods...
@Override
public void exit() {
Gdx.app.exit();
}
@Override
public void enterGame() {
//clears the stage and adds the game GUI
stage.clear();
//TODO: add game actors...
}
@Override
public void enterMenu() {
//clears the stage and adds the menu GUI
stage.clear();
stage.addActor(menuRoot);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.