001/****************************************************************************** 002 Copyright 2011 See AUTHORS file. 003 004 Licensed under the Apache License, Version 2.0 (the "License"); 005 you may not use this file except in compliance with the License. 006 You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, 012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 See the License for the specific language governing permissions and 014 limitations under the License. 015 */ 016package squidpony.squidgrid.gui.gdx; 017 018import com.badlogic.gdx.graphics.Camera; 019import com.badlogic.gdx.graphics.OrthographicCamera; 020import com.badlogic.gdx.math.Vector2; 021import com.badlogic.gdx.utils.Scaling; 022import com.badlogic.gdx.utils.viewport.ScalingViewport; 023 024/** A viewport that scales the world using {@link Scaling#stretch} on a sub-region of the screen. 025 * Does not keep the aspect ratio, the world is scaled to take up the requested region of the screen. 026 * @author Daniel Holderbaum 027 * @author Nathan Sweet 028 * Created by Tommy Ettinger on 4/16/2016. 029 */ 030public class ShrinkPartViewport extends ScalingViewport { 031 public float barWidth, barHeight; 032 /** Creates a new viewport using a new {@link OrthographicCamera}. */ 033 public ShrinkPartViewport (float worldWidth, float worldHeight, float barWidth) { 034 this(worldWidth, worldHeight, barWidth, new OrthographicCamera()); 035 } 036 public ShrinkPartViewport (float worldWidth, float worldHeight, float barWidth, float barHeight) { 037 this(worldWidth, worldHeight, barWidth, barHeight, new OrthographicCamera()); 038 } 039 040 public ShrinkPartViewport (float worldWidth, float worldHeight, float barWidth, Camera camera) { 041 this(worldWidth, worldHeight, barWidth, 0f, camera); 042 } 043 044 public ShrinkPartViewport (float worldWidth, float worldHeight, float barWidth, float barHeight, Camera camera) { 045 super(Scaling.stretch, worldWidth, worldHeight, camera); 046 this.barWidth = barWidth; 047 this.barHeight = barHeight; 048 } 049 050 @Override 051 public void update (int screenWidth, int screenHeight, boolean centerCamera) { 052 Vector2 scaled = Scaling.stretch.apply(getWorldWidth(), getWorldHeight(), 053 screenWidth - barWidth * 2, screenHeight - barHeight * 2); 054 int viewportWidth = Math.round(scaled.x); 055 int viewportHeight = Math.round(scaled.y); 056 // Center. 057 setScreenBounds((screenWidth - viewportWidth) >> 1, (screenHeight - viewportHeight) >> 1, viewportWidth, viewportHeight); 058 apply(true); 059 } 060 061 public Scaling getScaling () { 062 return Scaling.stretch; 063 } 064}