1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.GL20; 21 import com.badlogic.gdx.scenes.scene2d.Actor; 22 import com.badlogic.gdx.scenes.scene2d.InputEvent; 23 import com.badlogic.gdx.scenes.scene2d.InputListener; 24 import com.badlogic.gdx.scenes.scene2d.Stage; 25 import com.badlogic.gdx.scenes.scene2d.ui.Label; 26 import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 27 import com.badlogic.gdx.scenes.scene2d.ui.Skin; 28 import com.badlogic.gdx.scenes.scene2d.ui.Slider; 29 import com.badlogic.gdx.scenes.scene2d.ui.Table; 30 import com.badlogic.gdx.scenes.scene2d.ui.TextButton; 31 import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; 32 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; 33 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 34 import com.badlogic.gdx.tests.utils.GdxTest; 35 36 public class ScrollPaneTest extends GdxTest { 37 private Stage stage; 38 private Table container; 39 create()40 public void create () { 41 stage = new Stage(); 42 Skin skin = new Skin(Gdx.files.internal("data/uiskin.json")); 43 Gdx.input.setInputProcessor(stage); 44 45 // Gdx.graphics.setVSync(false); 46 47 container = new Table(); 48 stage.addActor(container); 49 container.setFillParent(true); 50 51 Table table = new Table(); 52 // table.debug(); 53 54 final ScrollPane scroll = new ScrollPane(table, skin); 55 56 InputListener stopTouchDown = new InputListener() { 57 public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 58 event.stop(); 59 return false; 60 } 61 }; 62 63 table.pad(10).defaults().expandX().space(4); 64 for (int i = 0; i < 100; i++) { 65 table.row(); 66 table.add(new Label(i + "uno", skin)).expandX().fillX(); 67 68 TextButton button = new TextButton(i + "dos", skin); 69 table.add(button); 70 button.addListener(new ClickListener() { 71 public void clicked (InputEvent event, float x, float y) { 72 System.out.println("click " + x + ", " + y); 73 } 74 }); 75 76 Slider slider = new Slider(0, 100, 1, false, skin); 77 slider.addListener(stopTouchDown); // Stops touchDown events from propagating to the FlickScrollPane. 78 table.add(slider); 79 80 table.add(new Label(i + "tres long0 long1 long2 long3 long4 long5 long6 long7 long8 long9 long10 long11 long12", skin)); 81 } 82 83 final TextButton flickButton = new TextButton("Flick Scroll", skin.get("toggle", TextButtonStyle.class)); 84 flickButton.setChecked(true); 85 flickButton.addListener(new ChangeListener() { 86 public void changed (ChangeEvent event, Actor actor) { 87 scroll.setFlickScroll(flickButton.isChecked()); 88 } 89 }); 90 91 final TextButton fadeButton = new TextButton("Fade Scrollbars", skin.get("toggle", TextButtonStyle.class)); 92 fadeButton.setChecked(true); 93 fadeButton.addListener(new ChangeListener() { 94 public void changed (ChangeEvent event, Actor actor) { 95 scroll.setFadeScrollBars(fadeButton.isChecked()); 96 } 97 }); 98 99 final TextButton smoothButton = new TextButton("Smooth Scrolling", skin.get("toggle", TextButtonStyle.class)); 100 smoothButton.setChecked(true); 101 smoothButton.addListener(new ChangeListener() { 102 public void changed (ChangeEvent event, Actor actor) { 103 scroll.setSmoothScrolling(smoothButton.isChecked()); 104 } 105 }); 106 107 final TextButton onTopButton = new TextButton("Scrollbars On Top", skin.get("toggle", TextButtonStyle.class)); 108 onTopButton.addListener(new ChangeListener() { 109 public void changed (ChangeEvent event, Actor actor) { 110 scroll.setScrollbarsOnTop(onTopButton.isChecked()); 111 } 112 }); 113 114 container.add(scroll).expand().fill().colspan(4); 115 container.row().space(10).padBottom(10); 116 container.add(flickButton).right().expandX(); 117 container.add(onTopButton); 118 container.add(smoothButton); 119 container.add(fadeButton).left().expandX(); 120 } 121 render()122 public void render () { 123 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 124 stage.act(Gdx.graphics.getDeltaTime()); 125 stage.draw(); 126 } 127 resize(int width, int height)128 public void resize (int width, int height) { 129 stage.getViewport().update(width, height, true); 130 131 // Gdx.gl.glViewport(100, 100, width - 200, height - 200); 132 // stage.setViewport(800, 600, false, 100, 100, width - 200, height - 200); 133 } 134 dispose()135 public void dispose () { 136 stage.dispose(); 137 } 138 needsGL20()139 public boolean needsGL20 () { 140 return false; 141 } 142 } 143