• 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.graphics.Color;
21 import com.badlogic.gdx.graphics.GL20;
22 import com.badlogic.gdx.graphics.Pixmap;
23 import com.badlogic.gdx.graphics.Pixmap.Format;
24 import com.badlogic.gdx.graphics.Texture;
25 import com.badlogic.gdx.graphics.g2d.BitmapFont;
26 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
27 import com.badlogic.gdx.scenes.scene2d.Actor;
28 import com.badlogic.gdx.scenes.scene2d.Stage;
29 import com.badlogic.gdx.scenes.scene2d.ui.Image;
30 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
31 import com.badlogic.gdx.scenes.scene2d.ui.Table;
32 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
33 import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
34 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
35 import com.badlogic.gdx.tests.utils.GdxTest;
36 
37 public class UISimpleTest extends GdxTest {
38 	Skin skin;
39 	Stage stage;
40 	SpriteBatch batch;
41 
42 	@Override
create()43 	public void create () {
44 		batch = new SpriteBatch();
45 		stage = new Stage();
46 		Gdx.input.setInputProcessor(stage);
47 
48 		// A skin can be loaded via JSON or defined programmatically, either is fine. Using a skin is optional but strongly
49 		// recommended solely for the convenience of getting a texture, region, etc as a drawable, tinted drawable, etc.
50 		skin = new Skin();
51 
52 		// Generate a 1x1 white texture and store it in the skin named "white".
53 		Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
54 		pixmap.setColor(Color.WHITE);
55 		pixmap.fill();
56 		skin.add("white", new Texture(pixmap));
57 
58 		// Store the default libgdx font under the name "default".
59 		skin.add("default", new BitmapFont());
60 
61 		// Configure a TextButtonStyle and name it "default". Skin resources are stored by type, so this doesn't overwrite the font.
62 		TextButtonStyle textButtonStyle = new TextButtonStyle();
63 		textButtonStyle.up = skin.newDrawable("white", Color.DARK_GRAY);
64 		textButtonStyle.down = skin.newDrawable("white", Color.DARK_GRAY);
65 		textButtonStyle.checked = skin.newDrawable("white", Color.BLUE);
66 		textButtonStyle.over = skin.newDrawable("white", Color.LIGHT_GRAY);
67 		textButtonStyle.font = skin.getFont("default");
68 		skin.add("default", textButtonStyle);
69 
70 		// Create a table that fills the screen. Everything else will go inside this table.
71 		Table table = new Table();
72 		table.setFillParent(true);
73 		stage.addActor(table);
74 
75 		// Create a button with the "default" TextButtonStyle. A 3rd parameter can be used to specify a name other than "default".
76 		final TextButton button = new TextButton("Click me!", skin);
77 		table.add(button);
78 
79 		// Add a listener to the button. ChangeListener is fired when the button's checked state changes, eg when clicked,
80 		// Button#setChecked() is called, via a key press, etc. If the event.cancel() is called, the checked state will be reverted.
81 		// ClickListener could have been used, but would only fire when clicked. Also, canceling a ClickListener event won't
82 		// revert the checked state.
83 		button.addListener(new ChangeListener() {
84 			public void changed (ChangeEvent event, Actor actor) {
85 				System.out.println("Clicked! Is checked: " + button.isChecked());
86 				button.setText("Good job!");
87 			}
88 		});
89 
90 		// Add an image actor. Have to set the size, else it would be the size of the drawable (which is the 1x1 texture).
91 		table.add(new Image(skin.newDrawable("white", Color.RED))).size(64);
92 	}
93 
94 	@Override
render()95 	public void render () {
96 		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
97 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
98 		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
99 		stage.draw();
100 	}
101 
102 	@Override
resize(int width, int height)103 	public void resize (int width, int height) {
104 		stage.getViewport().update(width, height, true);
105 	}
106 
107 	@Override
dispose()108 	public void dispose () {
109 		stage.dispose();
110 		skin.dispose();
111 	}
112 }
113