Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dermetfan.someLibgdxTests.screens;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.scenes.scene2d.InputEvent;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
- import com.badlogic.gdx.scenes.scene2d.ui.Table;
- import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
- import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
- import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
- public class HorizontalScrollPaneWithButtonsTest implements Screen {
- private Stage stage;
- @Override
- public void render(float delta) {
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- stage.act(delta);
- stage.draw();
- }
- @Override
- public void resize(int width, int height) {
- stage.setViewport(width, height, false);
- }
- @Override
- public void show() {
- Gdx.graphics.setDisplayMode(480, 360, false); // just resizing the window to something to show the effect of the ScrollPane (you don't need this at all)
- stage = new Stage();
- Gdx.input.setInputProcessor(stage);
- // create the buttons and their listener
- final TextButton[] buttons = new TextButton[10]; // final because the buttonListener enforces it
- // this is going to handle all the input events from the buttons
- ClickListener buttonListener = new ClickListener() {
- @Override
- public void clicked(InputEvent event, float x, float y) {
- if(event.getListenerActor() == buttons[0]) { // could use a switch statement on event.getListenerActor() here if compiling to java 1.7
- // button 1 callback
- } else if(event.getListenerActor() == buttons[1]) {
- // button 2 callback
- } // you get the idea, add the other callbacks yourself
- }
- };
- TextButtonStyle tbs = new TextButtonStyle(); // just some ugly TextButtonStyle, it has no background
- tbs.font = new BitmapFont();
- tbs.font.setScale(10); // just for the eye and to make the buttons bigger to have something for the ScrollPane to scroll
- tbs.fontColor = Color.WHITE;
- tbs.overFontColor = Color.YELLOW;
- tbs.downFontColor = Color.RED;
- for(byte b = 0; b < buttons.length; b++) { // initialize all the buttons
- buttons[b] = new TextButton("" + (b + 1), tbs);
- buttons[b].addListener(buttonListener);
- }
- // put the buttons in a table to position them side by side
- Table table = new Table();
- for(TextButton button : buttons)
- table.add(button).pad(25); // also add some padding for the eye
- // finally put everything in a ScrollPane
- ScrollPane scrollPane = new ScrollPane(table); // A ScrollPane doesn't take multiple Actors, but just one that is scrolls. That's why we need a table to put the buttons in.
- scrollPane.setFillParent(true); // should be self-explanatory
- scrollPane.setScrollingDisabled(false, true); // only scroll horizontally
- // well obviously put it all on the stage
- stage.addActor(scrollPane);
- }
- @Override
- public void hide() {
- dispose();
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- stage.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement