• 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.utils.viewport;
18 
19 import com.badlogic.gdx.graphics.Camera;
20 import com.badlogic.gdx.graphics.OrthographicCamera;
21 
22 /** A viewport where the world size is based on the size of the screen. By default 1 world unit == 1 screen pixel, but this ratio
23  * can be {@link #setUnitsPerPixel(float) changed}.
24  * @author Daniel Holderbaum
25  * @author Nathan Sweet */
26 public class ScreenViewport extends Viewport {
27 	private float unitsPerPixel = 1;
28 
29 	/** Creates a new viewport using a new {@link OrthographicCamera}. */
ScreenViewport()30 	public ScreenViewport () {
31 		this(new OrthographicCamera());
32 	}
33 
ScreenViewport(Camera camera)34 	public ScreenViewport (Camera camera) {
35 		setCamera(camera);
36 	}
37 
38 	@Override
update(int screenWidth, int screenHeight, boolean centerCamera)39 	public void update (int screenWidth, int screenHeight, boolean centerCamera) {
40 		setScreenBounds(0, 0, screenWidth, screenHeight);
41 		setWorldSize(screenWidth * unitsPerPixel, screenHeight * unitsPerPixel);
42 		apply(centerCamera);
43 	}
44 
getUnitsPerPixel()45 	public float getUnitsPerPixel () {
46 		return unitsPerPixel;
47 	}
48 
49 	/** Sets the number of pixels for each world unit. Eg, a scale of 2.5 means there are 2.5 world units for every 1 screen pixel.
50 	 * Default is 1. */
setUnitsPerPixel(float unitsPerPixel)51 	public void setUnitsPerPixel (float unitsPerPixel) {
52 		this.unitsPerPixel = unitsPerPixel;
53 	}
54 }
55