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}.
025 * <p>
026 * {@link Scaling#fit} keeps the aspect ratio by scaling the world up to fit the screen, adding black bars (letterboxing) for the
027 * remaining space.
028 * <p>
029 * {@link Scaling#fill} keeps the aspect ratio by scaling the world up to take the whole screen (some of the world may be off
030 * screen).
031 * <p>
032 * {@link Scaling#stretch} does not keep the aspect ratio, the world is scaled to take the whole screen.
033 * <p>
034 * {@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
035 * may be off screen).
036 * @author Daniel Holderbaum
037 * @author Nathan Sweet
038 * Created by Tommy Ettinger on 4/16/2016.
039 */
040public class ShrinkPartViewport extends ScalingViewport {
041    public float barWidth;
042    /** Creates a new viewport using a new {@link OrthographicCamera}. */
043    public ShrinkPartViewport (float worldWidth, float worldHeight, float barWidth) {
044        this(worldWidth, worldHeight, barWidth, new OrthographicCamera());
045    }
046
047    public ShrinkPartViewport (float worldWidth, float worldHeight, float barWidth, Camera camera) {
048        super(Scaling.stretch, worldWidth, worldHeight, camera);
049        this.barWidth = barWidth;
050    }
051
052    @Override
053    public void update (int screenWidth, int screenHeight, boolean centerCamera) {
054        Vector2 scaled = Scaling.stretch.apply(getWorldWidth(), getWorldHeight(), screenWidth - barWidth * 2, screenHeight);
055        int viewportWidth = Math.round(scaled.x);
056        int viewportHeight = Math.round(scaled.y);
057
058        // Center.
059        setScreenBounds((screenWidth - viewportWidth) / 2, (screenHeight - viewportHeight) / 2, viewportWidth, viewportHeight);
060
061        apply(true);
062    }
063
064    public Scaling getScaling () {
065        return Scaling.stretch;
066    }
067}