Navigating MainMenu with arrow keys or controller
- by Phil Royer
I'm attempting to make my menu navigable with the arrow keys or via the d-pad on a controller. So Far I've had no luck.
The question is: Can someone walk me through how to make my current menu or any libgdx menu keyboard accessible?
I'm a bit noobish with some stuff and I come from a Javascript background.
Here's an example of what I'm trying to do:
http://dl.dropboxusercontent.com/u/39448/webgl/qb/qb.html
For a simple menu that you can just add a few buttons to and it run out of the box use this:
http://www.sadafnoor.com/blog/how-to-create-simple-menu-in-libgdx/
Or you can use my code but I use a lot of custom styles.
And here's an example of my code:
import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
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.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.project.game.tween.ActorAccessor;
public class MainMenu implements Screen {
private SpriteBatch batch;
private Sprite menuBG;
private Stage stage;
private TextureAtlas atlas;
private Skin skin;
private Table table;
private TweenManager tweenManager;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
menuBG.draw(batch);
batch.end();
//table.debug();
stage.act(delta);
stage.draw();
//Table.drawDebug(stage);
tweenManager.update(delta);
}
@Override
public void resize(int width, int height) {
menuBG.setSize(width, height);
stage.setViewport(width, height, false);
table.invalidateHierarchy();
}
@Override
public void resume() {
}
@Override
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
batch = new SpriteBatch();
atlas = new TextureAtlas("ui/atlas.pack");
skin = new Skin(Gdx.files.internal("ui/menuSkin.json"), atlas);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Set Background
Texture menuBackgroundTexture = new Texture("images/mainMenuBackground.png");
menuBG = new Sprite(menuBackgroundTexture);
menuBG.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Create Main Menu Buttons
// Button Play
TextButton buttonPlay = new TextButton("START", skin, "inactive");
buttonPlay.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener()).setScreen(new LevelMenu());
}
});
buttonPlay.addListener(new InputListener() {
public boolean keyDown (InputEvent event, int keycode) {
System.out.println("down");
return true;
}
});
buttonPlay.padBottom(12);
buttonPlay.padLeft(20);
buttonPlay.getLabel().setAlignment(Align.left);
// Button EXTRAS
TextButton buttonExtras = new TextButton("EXTRAS", skin, "inactive");
buttonExtras.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener()).setScreen(new ExtrasMenu());
}
});
buttonExtras.padBottom(12);
buttonExtras.padLeft(20);
buttonExtras.getLabel().setAlignment(Align.left);
// Button Credits
TextButton buttonCredits = new TextButton("CREDITS", skin, "inactive");
buttonCredits.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener()).setScreen(new Credits());
}
});
buttonCredits.padBottom(12);
buttonCredits.padLeft(20);
buttonCredits.getLabel().setAlignment(Align.left);
// Button Settings
TextButton buttonSettings = new TextButton("SETTINGS", skin, "inactive");
buttonSettings.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener()).setScreen(new Settings());
}
});
buttonSettings.padBottom(12);
buttonSettings.padLeft(20);
buttonSettings.getLabel().setAlignment(Align.left);
// Button Exit
TextButton buttonExit = new TextButton("EXIT", skin, "inactive");
buttonExit.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
buttonExit.padBottom(12);
buttonExit.padLeft(20);
buttonExit.getLabel().setAlignment(Align.left);
// Adding Heading-Buttons to the cue
table.add().width(190);
table.add().width((table.getWidth() / 10) * 3);
table.add().width((table.getWidth() / 10) * 5).height(140).spaceBottom(50);
table.add().width(190).row();
table.add().width(190);
table.add(buttonPlay).spaceBottom(20).width(460).height(110);
table.add().row();
table.add().width(190);
table.add(buttonExtras).spaceBottom(20).width(460).height(110);
table.add().row();
table.add().width(190);
table.add(buttonCredits).spaceBottom(20).width(460).height(110);
table.add().row();
table.add().width(190);
table.add(buttonSettings).spaceBottom(20).width(460).height(110);
table.add().row();
table.add().width(190);
table.add(buttonExit).width(460).height(110);
table.add().row();
stage.addActor(table);
// Animation Settings
tweenManager = new TweenManager();
Tween.registerAccessor(Actor.class, new ActorAccessor());
// Heading and Buttons Fade In
Timeline.createSequence().beginSequence()
.push(Tween.set(buttonPlay, ActorAccessor.ALPHA).target(0))
.push(Tween.set(buttonExtras, ActorAccessor.ALPHA).target(0))
.push(Tween.set(buttonCredits, ActorAccessor.ALPHA).target(0))
.push(Tween.set(buttonSettings, ActorAccessor.ALPHA).target(0))
.push(Tween.set(buttonExit, ActorAccessor.ALPHA).target(0))
.push(Tween.to(buttonPlay, ActorAccessor.ALPHA, .5f).target(1))
.push(Tween.to(buttonExtras, ActorAccessor.ALPHA, .5f).target(1))
.push(Tween.to(buttonCredits, ActorAccessor.ALPHA, .5f).target(1))
.push(Tween.to(buttonSettings, ActorAccessor.ALPHA, .5f).target(1))
.push(Tween.to(buttonExit, ActorAccessor.ALPHA, .5f).target(1))
.end().start(tweenManager);
tweenManager.update(Gdx.graphics.getDeltaTime());
}
public static Vector2 getStageLocation(Actor actor) {
return actor.localToStageCoordinates(new Vector2(0, 0));
}
@Override
public void dispose() {
stage.dispose();
atlas.dispose();
skin.dispose();
menuBG.getTexture().dispose();
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
}
}