Home | History | Annotate | Download | only in viewport
      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.Gdx;
     20 import com.badlogic.gdx.graphics.Camera;
     21 import com.badlogic.gdx.graphics.OrthographicCamera;
     22 import com.badlogic.gdx.math.Vector2;
     23 import com.badlogic.gdx.utils.Scaling;
     24 
     25 /** A viewport that scales the world using {@link Scaling}.
     26  * <p>
     27  * {@link Scaling#fit} keeps the aspect ratio by scaling the world up to fit the screen, adding black bars (letterboxing) for the
     28  * remaining space.
     29  * <p>
     30  * {@link Scaling#fill} keeps the aspect ratio by scaling the world up to take the whole screen (some of the world may be off
     31  * screen).
     32  * <p>
     33  * {@link Scaling#stretch} does not keep the aspect ratio, the world is scaled to take the whole screen.
     34  * <p>
     35  * {@link Scaling#none} keeps the aspect ratio by using a fixed size world (the world may not fill the screen or some of the world
     36  * may be off screen).
     37  * @author Daniel Holderbaum
     38  * @author Nathan Sweet */
     39 public class ScalingViewport extends Viewport {
     40 	private Scaling scaling;
     41 
     42 	/** Creates a new viewport using a new {@link OrthographicCamera}. */
     43 	public ScalingViewport (Scaling scaling, float worldWidth, float worldHeight) {
     44 		this(scaling, worldWidth, worldHeight, new OrthographicCamera());
     45 	}
     46 
     47 	public ScalingViewport (Scaling scaling, float worldWidth, float worldHeight, Camera camera) {
     48 		this.scaling = scaling;
     49 		setWorldSize(worldWidth, worldHeight);
     50 		setCamera(camera);
     51 	}
     52 
     53 	@Override
     54 	public void update (int screenWidth, int screenHeight, boolean centerCamera) {
     55 		Vector2 scaled = scaling.apply(getWorldWidth(), getWorldHeight(), screenWidth, screenHeight);
     56 		int viewportWidth = Math.round(scaled.x);
     57 		int viewportHeight = Math.round(scaled.y);
     58 
     59 		// Center.
     60 		setScreenBounds((screenWidth - viewportWidth) / 2, (screenHeight - viewportHeight) / 2, viewportWidth, viewportHeight);
     61 
     62 		apply(centerCamera);
     63 	}
     64 
     65 	public Scaling getScaling () {
     66 		return scaling;
     67 	}
     68 
     69 	public void setScaling (Scaling scaling) {
     70 		this.scaling = scaling;
     71 	}
     72 }
     73