• 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;
21 import com.badlogic.gdx.InputAdapter;
22 import com.badlogic.gdx.InputMultiplexer;
23 import com.badlogic.gdx.graphics.Camera;
24 import com.badlogic.gdx.graphics.GL20;
25 import com.badlogic.gdx.scenes.scene2d.Stage;
26 import com.badlogic.gdx.scenes.scene2d.ui.Label;
27 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
28 import com.badlogic.gdx.scenes.scene2d.ui.Table;
29 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
30 import com.badlogic.gdx.tests.utils.GdxTest;
31 import com.badlogic.gdx.utils.Array;
32 import com.badlogic.gdx.utils.Scaling;
33 import com.badlogic.gdx.utils.viewport.ExtendViewport;
34 import com.badlogic.gdx.utils.viewport.FillViewport;
35 import com.badlogic.gdx.utils.viewport.FitViewport;
36 import com.badlogic.gdx.utils.viewport.ScalingViewport;
37 import com.badlogic.gdx.utils.viewport.ScreenViewport;
38 import com.badlogic.gdx.utils.viewport.StretchViewport;
39 import com.badlogic.gdx.utils.viewport.Viewport;
40 
41 /** Cycles viewports while rendering a stage with a root Table for the layout. */
42 public class ViewportTest1 extends GdxTest {
43 	Array<Viewport> viewports;
44 	Array<String> names;
45 	Stage stage;
46 	Label label;
47 
create()48 	public void create () {
49 		stage = new Stage();
50 		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
51 
52 		label = new Label("", skin);
53 
54 		Table root = new Table(skin);
55 		root.setFillParent(true);
56 		root.setBackground(skin.getDrawable("default-pane"));
57 		root.debug().defaults().space(6);
58 		root.add(new TextButton("Button 1", skin));
59 		root.add(new TextButton("Button 2", skin)).row();
60 		root.add("Press spacebar to change the viewport:").colspan(2).row();
61 		root.add(label).colspan(2);
62 		stage.addActor(root);
63 
64 		viewports = getViewports(stage.getCamera());
65 		names = getViewportNames();
66 
67 		stage.setViewport(viewports.first());
68 		label.setText(names.first());
69 
70 		Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {
71 			public boolean keyDown (int keycode) {
72 				if (keycode == Input.Keys.SPACE) {
73 					int index = (viewports.indexOf(stage.getViewport(), true) + 1) % viewports.size;
74 					label.setText(names.get(index));
75 					Viewport viewport = viewports.get(index);
76 					stage.setViewport(viewport);
77 					resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
78 				}
79 				return false;
80 			}
81 		}, stage));
82 	}
83 
render()84 	public void render () {
85 		stage.act();
86 
87 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
88 		stage.draw();
89 	}
90 
resize(int width, int height)91 	public void resize (int width, int height) {
92 		stage.getViewport().update(width, height, true);
93 	}
94 
dispose()95 	public void dispose () {
96 		stage.dispose();
97 	}
98 
getViewportNames()99 	static public Array<String> getViewportNames () {
100 		Array<String> names = new Array();
101 		names.add("StretchViewport");
102 		names.add("FillViewport");
103 		names.add("FitViewport");
104 		names.add("ExtendViewport: no max");
105 		names.add("ExtendViewport: max");
106 		names.add("ScreenViewport: 1:1");
107 		names.add("ScreenViewport: 0.75:1");
108 		names.add("ScalingViewport: none");
109 		return names;
110 	}
111 
getViewports(Camera camera)112 	static public Array<Viewport> getViewports (Camera camera) {
113 		int minWorldWidth = 640;
114 		int minWorldHeight = 480;
115 		int maxWorldWidth = 800;
116 		int maxWorldHeight = 480;
117 
118 		Array<Viewport> viewports = new Array();
119 		viewports.add(new StretchViewport(minWorldWidth, minWorldHeight, camera));
120 		viewports.add(new FillViewport(minWorldWidth, minWorldHeight, camera));
121 		viewports.add(new FitViewport(minWorldWidth, minWorldHeight, camera));
122 		viewports.add(new ExtendViewport(minWorldWidth, minWorldHeight, camera));
123 		viewports.add(new ExtendViewport(minWorldWidth, minWorldHeight, maxWorldWidth, maxWorldHeight, camera));
124 		viewports.add(new ScreenViewport(camera));
125 
126 		ScreenViewport screenViewport = new ScreenViewport(camera);
127 		screenViewport.setUnitsPerPixel(0.75f);
128 		viewports.add(screenViewport);
129 
130 		viewports.add(new ScalingViewport(Scaling.none, minWorldWidth, minWorldHeight, camera));
131 		return viewports;
132 	}
133 }
134