public
Last active

  • Download Gist
TestMenu.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.