• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Input.Keys;
21 import com.badlogic.gdx.graphics.GL20;
22 import com.badlogic.gdx.graphics.Texture;
23 import com.badlogic.gdx.graphics.g2d.TextureRegion;
24 import com.badlogic.gdx.scenes.scene2d.Actor;
25 import com.badlogic.gdx.scenes.scene2d.Stage;
26 import com.badlogic.gdx.scenes.scene2d.ui.Button;
27 import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
28 import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
29 import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
30 import com.badlogic.gdx.scenes.scene2d.ui.Image;
31 import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
32 import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
33 import com.badlogic.gdx.scenes.scene2d.ui.Label;
34 import com.badlogic.gdx.scenes.scene2d.ui.List;
35 import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
36 import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
37 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
38 import com.badlogic.gdx.scenes.scene2d.ui.Slider;
39 import com.badlogic.gdx.scenes.scene2d.ui.SplitPane;
40 import com.badlogic.gdx.scenes.scene2d.ui.Table;
41 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
42 import com.badlogic.gdx.scenes.scene2d.ui.TextField;
43 import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
44 import com.badlogic.gdx.scenes.scene2d.ui.TextTooltip;
45 import com.badlogic.gdx.scenes.scene2d.ui.Tooltip;
46 import com.badlogic.gdx.scenes.scene2d.ui.Window;
47 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
48 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
49 import com.badlogic.gdx.tests.utils.GdxTest;
50 import com.badlogic.gdx.utils.Align;
51 import com.badlogic.gdx.utils.viewport.ScreenViewport;
52 
53 public class UITest extends GdxTest {
54 	Object[] listEntries = {"This is a list entry1", "And another one1", "The meaning of life1", "Is hard to come by1",
55 		"This is a list entry2", "And another one2", "The meaning of life2", "Is hard to come by2", "This is a list entry3",
56 		"And another one3", "The meaning of life3", "Is hard to come by3", "This is a list entry4", "And another one4",
57 		"The meaning of life4", "Is hard to come by4", "This is a list entry5", "And another one5", "The meaning of life5",
58 		"Is hard to come by5"};
59 
60 	Skin skin;
61 	Stage stage;
62 	Texture texture1;
63 	Texture texture2;
64 	Label fpsLabel;
65 
66 	@Override
create()67 	public void create () {
68 		skin = new Skin(Gdx.files.internal("data/uiskin.json"));
69 		texture1 = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));
70 		texture2 = new Texture(Gdx.files.internal("data/badlogic.jpg"));
71 		TextureRegion image = new TextureRegion(texture1);
72 		TextureRegion imageFlipped = new TextureRegion(image);
73 		imageFlipped.flip(true, true);
74 		TextureRegion image2 = new TextureRegion(texture2);
75 		// stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false, new PolygonSpriteBatch());
76 		stage = new Stage(new ScreenViewport());
77 		Gdx.input.setInputProcessor(stage);
78 
79 		// Group.debug = true;
80 
81 		ImageButtonStyle style = new ImageButtonStyle(skin.get(ButtonStyle.class));
82 		style.imageUp = new TextureRegionDrawable(image);
83 		style.imageDown = new TextureRegionDrawable(imageFlipped);
84 		ImageButton iconButton = new ImageButton(style);
85 
86 		Button buttonMulti = new TextButton("Multi\nLine\nToggle", skin, "toggle");
87 		Button imgButton = new Button(new Image(image), skin);
88 		Button imgToggleButton = new Button(new Image(image), skin, "toggle");
89 
90 		Label myLabel = new Label("this is some text.", skin);
91 		myLabel.setWrap(true);
92 
93 		Table t = new Table();
94 		t.row();
95 		t.add(myLabel);
96 
97 		t.layout();
98 
99 		final CheckBox checkBox = new CheckBox(" Continuous rendering", skin);
100 		checkBox.setChecked(true);
101 		final Slider slider = new Slider(0, 10, 1, false, skin);
102 		slider.setAnimateDuration(0.3f);
103 		TextField textfield = new TextField("", skin);
104 		textfield.setMessageText("Click here!");
105 		textfield.setAlignment(Align.center);
106 		final SelectBox selectBox = new SelectBox(skin);
107 		selectBox.addListener(new ChangeListener() {
108 			public void changed (ChangeEvent event, Actor actor) {
109 				System.out.println(selectBox.getSelected());
110 			}
111 		});
112 		selectBox.setItems("Android1", "Windows1 long text in item", "Linux1", "OSX1", "Android2", "Windows2", "Linux2", "OSX2",
113 			"Android3", "Windows3", "Linux3", "OSX3", "Android4", "Windows4", "Linux4", "OSX4", "Android5", "Windows5", "Linux5",
114 			"OSX5", "Android6", "Windows6", "Linux6", "OSX6", "Android7", "Windows7", "Linux7", "OSX7");
115 		selectBox.setSelected("Linux6");
116 		Image imageActor = new Image(image2);
117 		ScrollPane scrollPane = new ScrollPane(imageActor);
118 		List list = new List(skin);
119 		list.setItems(listEntries);
120 		list.getSelection().setMultiple(true);
121 		list.getSelection().setRequired(false);
122 		// list.getSelection().setToggle(true);
123 		ScrollPane scrollPane2 = new ScrollPane(list, skin);
124 		scrollPane2.setFlickScroll(false);
125 		SplitPane splitPane = new SplitPane(scrollPane, scrollPane2, false, skin, "default-horizontal");
126 		fpsLabel = new Label("fps:", skin);
127 
128 		// configures an example of a TextField in password mode.
129 		final Label passwordLabel = new Label("Textfield in password mode: ", skin);
130 		final TextField passwordTextField = new TextField("", skin);
131 		passwordTextField.setMessageText("password");
132 		passwordTextField.setPasswordCharacter('*');
133 		passwordTextField.setPasswordMode(true);
134 
135 		buttonMulti.addListener(new TextTooltip("This is a tooltip! This is a tooltip! This is a tooltip! This is a tooltip! This is a tooltip! This is a tooltip!", skin));
136 		Table tooltipTable = new Table(skin);
137 		tooltipTable.pad(10).background("default-round");
138 		tooltipTable.add(new TextButton("Fancy tooltip!", skin));
139 		imgButton.addListener(new Tooltip(tooltipTable));
140 
141 		// window.debug();
142 		Window window = new Window("Dialog", skin);
143 		window.getTitleTable().add(new TextButton("X", skin)).height(window.getPadTop());
144 		window.setPosition(0, 0);
145 		window.defaults().spaceBottom(10);
146 		window.row().fill().expandX();
147 		window.add(iconButton);
148 		window.add(buttonMulti);
149 		window.add(imgButton);
150 		window.add(imgToggleButton);
151 		window.row();
152 		window.add(checkBox);
153 		window.add(slider).minWidth(100).fillX().colspan(3);
154 		window.row();
155 		window.add(selectBox).maxWidth(100);
156 		window.add(textfield).minWidth(100).expandX().fillX().colspan(3);
157 		window.row();
158 		window.add(splitPane).fill().expand().colspan(4).maxHeight(200);
159 		window.row();
160 		window.add(passwordLabel).colspan(2);
161 		window.add(passwordTextField).minWidth(100).expandX().fillX().colspan(2);
162 		window.row();
163 		window.add(fpsLabel).colspan(4);
164 		window.pack();
165 
166 		// stage.addActor(new Button("Behind Window", skin));
167 		stage.addActor(window);
168 
169 		textfield.setTextFieldListener(new TextFieldListener() {
170 			public void keyTyped (TextField textField, char key) {
171 				if (key == '\n') textField.getOnscreenKeyboard().show(false);
172 			}
173 		});
174 
175 		slider.addListener(new ChangeListener() {
176 			public void changed (ChangeEvent event, Actor actor) {
177 				Gdx.app.log("UITest", "slider: " + slider.getValue());
178 			}
179 		});
180 
181 		iconButton.addListener(new ChangeListener() {
182 			public void changed (ChangeEvent event, Actor actor) {
183 				new Dialog("Some Dialog", skin, "dialog") {
184 					protected void result (Object object) {
185 						System.out.println("Chosen: " + object);
186 					}
187 				}.text("Are you enjoying this demo?").button("Yes", true).button("No", false).key(Keys.ENTER, true)
188 					.key(Keys.ESCAPE, false).show(stage);
189 			}
190 		});
191 
192 		checkBox.addListener(new ChangeListener() {
193 			public void changed (ChangeEvent event, Actor actor) {
194 				Gdx.graphics.setContinuousRendering(checkBox.isChecked());
195 			}
196 		});
197 	}
198 
199 	@Override
render()200 	public void render () {
201 		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
202 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
203 
204 		fpsLabel.setText("fps: " + Gdx.graphics.getFramesPerSecond());
205 
206 		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
207 		stage.draw();
208 	}
209 
210 	@Override
resize(int width, int height)211 	public void resize (int width, int height) {
212 		stage.getViewport().update(width, height, true);
213 	}
214 
215 	@Override
dispose()216 	public void dispose () {
217 		stage.dispose();
218 		skin.dispose();
219 		texture1.dispose();
220 		texture2.dispose();
221 	}
222 }
223