Advertisement
dermetfan

horizontal ScrollPane with buttons

Jun 17th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Screen;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.GL20;
  7. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  8. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  9. import com.badlogic.gdx.scenes.scene2d.Stage;
  10. import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
  11. import com.badlogic.gdx.scenes.scene2d.ui.Table;
  12. import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
  13. import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
  14. import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
  15.  
  16. public class HorizontalScrollPaneWithButtonsTest implements Screen {
  17.  
  18.     private Stage stage;
  19.  
  20.     @Override
  21.     public void render(float delta) {
  22.         Gdx.gl.glClearColor(0, 0, 0, 1);
  23.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  24.  
  25.         stage.act(delta);
  26.         stage.draw();
  27.     }
  28.  
  29.     @Override
  30.     public void resize(int width, int height) {
  31.         stage.setViewport(width, height, false);
  32.     }
  33.  
  34.     @Override
  35.     public void show() {
  36.         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)
  37.         stage = new Stage();
  38.  
  39.         Gdx.input.setInputProcessor(stage);
  40.  
  41.         // create the buttons and their listener
  42.         final TextButton[] buttons = new TextButton[10]; // final because the buttonListener enforces it
  43.  
  44.         // this is going to handle all the input events from the buttons
  45.         ClickListener buttonListener = new ClickListener() {
  46.  
  47.             @Override
  48.             public void clicked(InputEvent event, float x, float y) {
  49.                 if(event.getListenerActor() == buttons[0]) { // could use a switch statement on event.getListenerActor() here if compiling to java 1.7
  50.                     // button 1 callback
  51.                 } else if(event.getListenerActor() == buttons[1]) {
  52.                     // button 2 callback
  53.                 } // you get the idea, add the other callbacks yourself
  54.             }
  55.         };
  56.  
  57.         TextButtonStyle tbs = new TextButtonStyle(); // just some ugly TextButtonStyle, it has no background
  58.         tbs.font = new BitmapFont();
  59.         tbs.font.setScale(10); // just for the eye and to make the buttons bigger to have something for the ScrollPane to scroll
  60.         tbs.fontColor = Color.WHITE;
  61.         tbs.overFontColor = Color.YELLOW;
  62.         tbs.downFontColor = Color.RED;
  63.  
  64.         for(byte b = 0; b < buttons.length; b++) { // initialize all the buttons
  65.             buttons[b] = new TextButton("" + (b + 1), tbs);
  66.             buttons[b].addListener(buttonListener);
  67.         }
  68.  
  69.         // put the buttons in a table to position them side by side
  70.         Table table = new Table();
  71.         for(TextButton button : buttons)
  72.             table.add(button).pad(25); // also add some padding for the eye
  73.  
  74.         // finally put everything in a ScrollPane
  75.         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.
  76.         scrollPane.setFillParent(true); // should be self-explanatory
  77.         scrollPane.setScrollingDisabled(false, true); // only scroll horizontally
  78.  
  79.         // well obviously put it all on the stage
  80.         stage.addActor(scrollPane);
  81.     }
  82.  
  83.     @Override
  84.     public void hide() {
  85.         dispose();
  86.     }
  87.  
  88.     @Override
  89.     public void pause() {
  90.     }
  91.  
  92.     @Override
  93.     public void resume() {
  94.     }
  95.  
  96.     @Override
  97.     public void dispose() {
  98.         stage.dispose();
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement