Created
December 24, 2014 09:53
-
-
Save Jackyjjc/9707dab983dd1d403bf5 to your computer and use it in GitHub Desktop.
scene2d List with keyboard navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input; | |
import com.badlogic.gdx.ScreenAdapter; | |
import com.badlogic.gdx.graphics.GL20; | |
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.List; | |
import com.badlogic.gdx.scenes.scene2d.ui.Skin; | |
import com.badlogic.gdx.scenes.scene2d.ui.Table; | |
import com.badlogic.gdx.utils.Array; | |
import com.badlogic.gdx.utils.viewport.ScreenViewport; | |
/** | |
* Created by Jacky on 24/12/2014. | |
*/ | |
public class ListScreen extends ScreenAdapter { | |
private Stage stage; | |
public ListScreen() { | |
stage = new Stage(new ScreenViewport()); | |
Table rootTable = new Table(); | |
rootTable.setFillParent(true); | |
stage.addActor(rootTable); | |
Array<String> listItems = new Array<String>(new String[] {"item1", "item2", "item3"}); | |
final List<String> listView = new List<String>(new Skin(Gdx.files.internal("skin/uiskin.json"))); | |
listView.setItems(listItems); | |
listView.addListener(new InputListener(){ | |
@Override | |
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { | |
listView.getStage().setKeyboardFocus(listView); | |
return false; | |
} | |
@Override | |
public boolean keyDown(InputEvent event, int keycode) { | |
boolean consumed = false; | |
int selectedIndex = listView.getSelectedIndex(); | |
switch (keycode) { | |
case Input.Keys.UP: { | |
listView.setSelectedIndex(Math.max(selectedIndex - 1, 0)); | |
} break; | |
case Input.Keys.DOWN: { | |
listView.setSelectedIndex(Math.min(selectedIndex + 1, listView.getItems().size - 1)); | |
} break; | |
} | |
return consumed; | |
} | |
}); | |
rootTable.add(listView); | |
} | |
@Override | |
public void show() { | |
Gdx.input.setInputProcessor(stage); | |
} | |
@Override | |
public void render(float delta) { | |
Gdx.gl.glClearColor(0f, 0f, 0f, 1f); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
stage.act(); | |
stage.draw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, also check out https://github.com/MrStahlfelge/gdx-controllerutils/wiki/Button-operable-Scene2d on this topic.