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.Stage; 23 import com.badlogic.gdx.scenes.scene2d.ui.Image; 24 import com.badlogic.gdx.scenes.scene2d.ui.Label; 25 import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 26 import com.badlogic.gdx.scenes.scene2d.ui.Skin; 27 import com.badlogic.gdx.scenes.scene2d.ui.Table; 28 import com.badlogic.gdx.scenes.scene2d.ui.TextButton; 29 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; 30 import com.badlogic.gdx.tests.utils.GdxTest; 31 import com.badlogic.gdx.utils.Array; 32 33 /** Test switch of scroll bars + knobs from right to left, and bottom to top */ 34 public class ScrollPaneScrollBarsTest extends GdxTest { 35 private Stage stage; 36 Array<ScrollPane> scrollPanes = new Array<ScrollPane>(); 37 boolean doFade = true; 38 boolean doOnTop = true; 39 private Table bottomLeft, bottomRight, topLeft, topRight, horizOnlyTop, horizOnlyBottom, vertOnlyLeft, vertOnlyRight; 40 create()41 public void create () { 42 float width = Gdx.graphics.getWidth(); 43 float height = Gdx.graphics.getHeight(); 44 float btnWidth = 200; 45 float btnHeight = 40; 46 stage = new Stage(); 47 Skin skin = new Skin(Gdx.files.internal("data/uiskin.json")); 48 Gdx.input.setInputProcessor(stage); 49 50 final TextButton fadeBtn = new TextButton("Fade: " + doFade, skin); 51 fadeBtn.setSize(btnWidth, btnHeight); 52 fadeBtn.setPosition(0, height - fadeBtn.getHeight()); 53 stage.addActor(fadeBtn); 54 fadeBtn.addListener(new ChangeListener() { 55 @Override 56 public void changed (ChangeEvent event, Actor actor) { 57 doFade = !doFade; 58 fadeBtn.setText("Fade: " + doFade); 59 for (ScrollPane pane : scrollPanes) { 60 pane.setFadeScrollBars(doFade); 61 } 62 } 63 }); 64 65 final TextButton onTopBtn = new TextButton("ScrollbarsOnTop: " + doOnTop, skin); 66 onTopBtn.setSize(btnWidth, btnHeight); 67 onTopBtn.setPosition(0 + fadeBtn.getWidth() + 20, height - onTopBtn.getHeight()); 68 stage.addActor(onTopBtn); 69 onTopBtn.addListener(new ChangeListener() { 70 @Override 71 public void changed (ChangeEvent event, Actor actor) { 72 doOnTop = !doOnTop; 73 onTopBtn.setText("ScrollbarOnTop: " + doOnTop); 74 onTopBtn.invalidate(); 75 for (ScrollPane pane : scrollPanes) { 76 pane.setScrollbarsOnTop(doOnTop); 77 } 78 } 79 }); 80 81 // Gdx.graphics.setVSync(false); 82 83 float gap = 8; 84 float x = gap; 85 float y = gap; 86 float contWidth = width / 2 - gap * 1.5f; 87 float contHeight = height / 4.5f - gap * 1.25f; 88 89 bottomLeft = new Table(); 90 bottomLeft.setPosition(x, y); 91 bottomLeft.setSize(contWidth, contHeight); 92 stage.addActor(bottomLeft); 93 94 bottomRight = new Table(); 95 bottomRight.setSize(contWidth, contHeight); 96 x = bottomLeft.getX() + bottomLeft.getWidth() + gap; 97 bottomRight.setPosition(x, y); 98 stage.addActor(bottomRight); 99 100 topLeft = new Table(); 101 topLeft.setSize(contWidth, contHeight); 102 x = bottomLeft.getX(); 103 y = bottomLeft.getY() + bottomLeft.getHeight() + gap; 104 topLeft.setPosition(x, y); 105 stage.addActor(topLeft); 106 107 topRight = new Table(); 108 topRight.setSize(contWidth, contHeight); 109 x = bottomRight.getX(); 110 y = topLeft.getY(); 111 topRight.setPosition(x, y); 112 stage.addActor(topRight); 113 114 horizOnlyTop = new Table(); 115 horizOnlyTop.setSize(contWidth, contHeight); 116 x = topRight.getX(); 117 y = topRight.getY() + topRight.getHeight() + gap; 118 horizOnlyTop.setPosition(x, y); 119 stage.addActor(horizOnlyTop); 120 121 horizOnlyBottom = new Table(); 122 horizOnlyBottom.setSize(contWidth, contHeight); 123 x = topLeft.getX(); 124 y = topLeft.getY() + topLeft.getHeight() + gap; 125 horizOnlyBottom.setPosition(x, y); 126 stage.addActor(horizOnlyBottom); 127 128 vertOnlyLeft = new Table(); 129 vertOnlyLeft.setSize(contWidth, contHeight); 130 x = horizOnlyBottom.getX(); 131 y = horizOnlyBottom.getY() + horizOnlyBottom.getHeight() + gap; 132 vertOnlyLeft.setPosition(x, y); 133 stage.addActor(vertOnlyLeft); 134 135 vertOnlyRight = new Table(); 136 vertOnlyRight.setSize(contWidth, contHeight); 137 x = horizOnlyTop.getX(); 138 y = horizOnlyTop.getY() + horizOnlyTop.getHeight() + gap; 139 vertOnlyRight.setPosition(x, y); 140 stage.addActor(vertOnlyRight); 141 142 Table bottomLeftTable = new Table(); 143 Table bottomRightTable = new Table(); 144 Table topLeftTable = new Table(); 145 Table topRightTable = new Table(); 146 Table horizOnlyTopTable = new Table(); 147 Table horizOnlyBottomTable = new Table(); 148 Table vertOnlyLeftTable = new Table(); 149 Table vertOnlyRightTable = new Table(); 150 151 final ScrollPane bottomLeftScroll = new ScrollPane(bottomLeftTable, skin); 152 bottomLeftScroll.setScrollBarPositions(true, false); 153 154 final ScrollPane bottomRightScroll = new ScrollPane(bottomRightTable, skin); 155 bottomRightScroll.setScrollBarPositions(true, true); 156 157 final ScrollPane topLeftScroll = new ScrollPane(topLeftTable, skin); 158 topLeftScroll.setScrollBarPositions(false, false); 159 160 final ScrollPane topRightScroll = new ScrollPane(topRightTable, skin); 161 topRightScroll.setScrollBarPositions(false, true); 162 163 final ScrollPane horizOnlyTopScroll = new ScrollPane(horizOnlyTopTable, skin); 164 horizOnlyTopScroll.setScrollBarPositions(false, true); 165 166 final ScrollPane horizOnlyBottomScroll = new ScrollPane(horizOnlyBottomTable, skin); 167 horizOnlyBottomScroll.setScrollBarPositions(true, true); 168 169 final ScrollPane vertOnlyLeftScroll = new ScrollPane(vertOnlyLeftTable, skin); 170 vertOnlyLeftScroll.setScrollBarPositions(true, false); 171 172 final ScrollPane vertOnlyRightScroll = new ScrollPane(vertOnlyRightTable, skin); 173 vertOnlyRightScroll.setScrollBarPositions(true, true); 174 175 ScrollPane[] panes = new ScrollPane[] {bottomLeftScroll, bottomRightScroll, topLeftScroll, topRightScroll, 176 horizOnlyTopScroll, horizOnlyBottomScroll, vertOnlyLeftScroll, vertOnlyRightScroll}; 177 for (ScrollPane pane : panes) { 178 scrollPanes.add(pane); 179 } 180 181 Table[] tables = new Table[] {bottomLeftTable, bottomRightTable, topLeftTable, topRightTable, horizOnlyTopTable, 182 horizOnlyBottomTable, vertOnlyLeftTable, vertOnlyRightTable}; 183 for (Table t : tables) 184 t.defaults().expandX().fillX(); 185 186 horizOnlyTopTable.add( 187 new Label("HORIZONTAL-ONLY-TOP verify HORIZONTAL scroll bar is on the TOP and properly aligned", skin)).row(); 188 horizOnlyTopTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 189 190 horizOnlyBottomTable.add( 191 new Label("HORIZONTAL-ONLY-BOTTOM verify HORIZONTAL scroll bar is on the BOTTOM and properly aligned", skin)).row(); 192 horizOnlyBottomTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 193 194 for (int i = 0; i < 12; i++) { 195 bottomLeftTable.add( 196 new Label(i + " BOTTOM-LEFT verify scroll bars are on the BOTTOM and the LEFT, and are properly aligned", skin)) 197 .row(); 198 bottomLeftTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 199 200 bottomRightTable.add( 201 new Label(i + " BOTTOM-RIGHT verify scroll bars are on the BOTTOM and the RIGHT, and are properly aligned", skin)) 202 .row(); 203 bottomRightTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 204 205 topLeftTable.add( 206 new Label(i + " TOP-LEFT verify scroll bars are on the TOP and the LEFT, and are properly aligned", skin)).row(); 207 topLeftTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 208 209 topRightTable.add( 210 new Label(i + " TOP-RIGHT verify scroll bars are on the TOP and the RIGHT, and are properly aligned", skin)).row(); 211 topRightTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 212 213 vertOnlyLeftTable.add(new Label("VERT-ONLY-LEFT", skin)).row(); 214 vertOnlyLeftTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 215 216 vertOnlyRightTable.add(new Label("VERT-ONLY-RIGHT", skin)).row(); 217 vertOnlyRightTable.add(new Image(skin.getDrawable("default-rect"))).height(20).row(); 218 } 219 220 bottomLeft.add(bottomLeftScroll).expand().fill().colspan(4); 221 bottomRight.add(bottomRightScroll).expand().fill().colspan(4); 222 topLeft.add(topLeftScroll).expand().fill().colspan(4); 223 topRight.add(topRightScroll).expand().fill().colspan(4); 224 horizOnlyTop.add(horizOnlyTopScroll).expand().fill().colspan(4); 225 horizOnlyBottom.add(horizOnlyBottomScroll).expand().fill().colspan(4); 226 vertOnlyLeft.add(vertOnlyLeftScroll).expand().fill().colspan(4); 227 vertOnlyRight.add(vertOnlyRightScroll).expand().fill().colspan(4); 228 229 for (ScrollPane pane : scrollPanes) { 230 pane.setFadeScrollBars(doFade); 231 pane.setScrollbarsOnTop(doOnTop); 232 } 233 } 234 render()235 public void render () { 236 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 237 stage.act(Gdx.graphics.getDeltaTime()); 238 stage.draw(); 239 } 240 resize(int width, int height)241 public void resize (int width, int height) { 242 stage.getViewport().update(width, height, true); 243 // Gdx.gl.glViewport(100, 100, width - 200, height - 200); 244 // stage.setViewport(800, 600, false, 100, 100, width - 200, height - 200); 245 } 246 dispose()247 public void dispose () { 248 stage.dispose(); 249 } 250 } 251