Example usage for com.badlogic.gdx.utils.viewport FitViewport FitViewport

List of usage examples for com.badlogic.gdx.utils.viewport FitViewport FitViewport

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils.viewport FitViewport FitViewport.

Prototype

public FitViewport(float worldWidth, float worldHeight) 

Source Link

Document

Creates a new viewport using a new OrthographicCamera .

Usage

From source file:es.danirod.jddprototype.game.MenuScreen.java

License:Open Source License

public MenuScreen(final MainGame game) {
    super(game);/*from   ww w. j  a va2s  .  c  om*/

    // Create a new stage, as usual.
    stage = new Stage(new FitViewport(640, 360));

    // Load the skin file. The skin file contains information about the skins. It can be
    // passed to any widget in Scene2D UI to set the style. It just works, amazing.
    skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

    // For instance, here you see that I create a new button by telling the label of the
    // button as well as the skin file. The background image for the button is in the skin
    // file.
    play = new TextButton("Play", skin);
    credits = new TextButton("Credits", skin);

    // Also, create an image. Images are actors that only display some texture. Useful if you
    // want to display a texture in a Scene2D based screen but you don't want to rewrite code.
    logo = new Image(game.getManager().get("logo.png", Texture.class));

    // Add capture listeners. Capture listeners have one method, changed, that is executed
    // when the button is pressed or when the user interacts somehow with the widget. They are
    // cool because they let you execute some code when you press them.
    play.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            // Take me to the game screen!
            game.setScreen(game.gameScreen);
        }
    });

    credits.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            game.setScreen(game.creditsScreen);
        }
    });

    // Now I position things on screen. Sorry for making this the hardest part of this screen.
    // I position things on the screen so that they look centered. This is why I make the
    // buttons the same size.
    logo.setPosition(440 - logo.getWidth() / 2, 320 - logo.getHeight());
    play.setSize(200, 80);
    credits.setSize(200, 80);
    play.setPosition(40, 140);
    credits.setPosition(40, 40);

    // Do not forget to add actors to the stage or we wouldn't see anything.
    stage.addActor(play);
    stage.addActor(logo);
    stage.addActor(credits);
}

From source file:es.danirod.jddprototype.game.vista.GameOverScreen.java

License:Open Source License

public GameOverScreen(final es.danirod.jddprototype.game.controlador.MainGame game) {
    super(game);//from w ww. jav a  2  s  .  c om

    // Create a new stage, as usual.
    stage = new Stage(new FitViewport(640, 360));

    // Load the skin file. The skin file contains information about the skins. It can be
    // passed to any widget in Scene2D UI to set the style. It just works, amazing.
    skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

    // For instance, here you see that I create a new button by telling the label of the
    // button as well as the skin file. The background image for the button is in the skin
    // file.
    retry = new TextButton("Intentar de nuevo", skin);
    menu = new TextButton("Menu", skin);

    // Also, create an image. Images are actors that only display some texture. Useful if you
    // want to display a texture in a Scene2D based screen but you don't want to rewrite code.
    gameover = new Image(game.getManager().get("gameover.png", Texture.class));

    // Add capture listeners. Capture listeners have one method, changed, that is executed
    // when the button is pressed or when the user interacts somehow with the widget. They are
    // cool because they let you execute some code when you press them.
    retry.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {

            // hay que calcular el checkpoint donde debe aparecer segun la posicion donde murio
            if (es.danirod.jddprototype.game.modelo.VariablesGlobales.dificultad == 0) { // facil, todos los checkpoints
                if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[2])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 0;
                else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[4])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 1;
                else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[6])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 2;
                else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[8])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 3;
                else
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 4;
            } else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.dificultad == 1) { // medio, solo checkpoints 1 y 3
                if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[2])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 0;
                else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[4])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 1;
                else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[6])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 1;
                else if (es.danirod.jddprototype.game.modelo.VariablesGlobales.posicion_muerte < es.danirod.jddprototype.game.modelo.Constants.POSICION_JUGADOR[8])
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 3;
                else
                    es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 3;
            } else { // dificil, sin checkpoints
                es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 0;
            }

            // Here I go to the game screen again.
            game.setScreen(game.gameScreen);
        }
    });

    menu.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            // se reinician variables
            es.danirod.jddprototype.game.modelo.VariablesGlobales.muertes = 0;
            es.danirod.jddprototype.game.modelo.VariablesGlobales.saltos = 0;
            es.danirod.jddprototype.game.modelo.VariablesGlobales.checkpoint = 0;
            // And here I go to the menu screen.
            game.setScreen(game.menuScreen);
        }
    });

    // Now I position things on screen. Sorry for making this the hardest part of this screen.
    // I position things on the screen so that they look centered. This is why I make the
    // buttons the same size.
    gameover.setPosition(320 - gameover.getWidth() / 2, 320 - gameover.getHeight());
    retry.setSize(200, 80);
    menu.setSize(200, 80);
    retry.setPosition(60, 50);
    menu.setPosition(380, 50);

    // Do not forget to add actors to the stage or we wouldn't see anything.
    stage.addActor(retry);
    stage.addActor(gameover);
    stage.addActor(menu);
}

From source file:com.tumblr.oddlydrawn.nahlc.screens.LicenseScreen.java

License:Apache License

public LicenseScreen(Game g) {
    game = g;/*  w w  w  .  ja v a2 s  .c om*/
    stage = new Stage(new FitViewport(WIDTH, HEIGHT));
    skin = new Skin();
    assets = new Assets();
    assets.initMainMenu();
    Gdx.input.setInputProcessor(stage);

    FileHandle handle;
    handle = Gdx.files.internal(LICENSE_PATH);
    licenseString = handle.readString();

    Table table = new Table();
    table.setFillParent(true);
    stage.addActor(table);

    skin.add("default", new BitmapFont(Gdx.files.internal(FONT_PATH)));

    LabelStyle labelStyle = new LabelStyle();
    labelStyle.font = skin.getFont("default");
    skin.add("default", labelStyle);

    ScrollPaneStyle scrollPaneStyle = new ScrollPaneStyle();
    skin.add("default", scrollPaneStyle);

    TextButtonStyle textButtonStyle = new TextButtonStyle();
    textButtonStyle.font = skin.getFont("default");
    textButtonStyle.up = new NinePatchDrawable(assets.getBoxPatch());
    skin.add("default", textButtonStyle);

    Label license = new Label(licenseString, skin);
    ScrollPane scrollPane = new ScrollPane(license, skin);
    scrollPane.setFlickScroll(true);
    table.add(scrollPane);
    table.row();

    TextButton backButton = new TextButton("Back", skin);
    table.add(backButton).padTop(TABLE_PAD);
    table.padTop(TABLE_PAD);
    table.padBottom(TABLE_PAD);
    table.row();

    backButton.addListener(new ChangeListener() {
        public void changed(ChangeEvent event, Actor actor) {
            dispose();
            game.setScreen(new MainMenuScreen(game));
        }
    });
}

From source file:es.danirod.jddprototype.game.vista.MenuScreen.java

License:Open Source License

public MenuScreen(final es.danirod.jddprototype.game.controlador.MainGame game) {
    super(game);//from   w  w w.jav  a 2 s  .  c om

    // Create a new stage, as usual.
    stage = new Stage(new FitViewport(640, 360));

    // Load the skin file. The skin file contains information about the skins. It can be
    // passed to any widget in Scene2D UI to set the style. It just works, amazing.
    skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

    // For instance, here you see that I create a new button by telling the label of the
    // button as well as the skin file. The background image for the button is in the skin
    // file.
    play = new TextButton("Jugar", skin);
    opciones = new TextButton("Opciones", skin);
    credits = new TextButton("Creditos", skin);
    salir = new TextButton("Salir", skin);

    // Also, create an image. Images are actors that only display some texture. Useful if you
    // want to display a texture in a Scene2D based screen but you don't want to rewrite code.
    logo = new Image(game.getManager().get("logo.png", Texture.class));

    // Add capture listeners. Capture listeners have one method, changed, that is executed
    // when the button is pressed or when the user interacts somehow with the widget. They are
    // cool because they let you execute some code when you press them.
    play.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            // Take me to the game screen!
            game.setScreen(game.gameScreen);
        }
    });

    opciones.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            game.setScreen(game.pantallaOpciones);
        }
    });

    credits.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            game.setScreen(game.creditsScreen);
        }
    });

    salir.addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.app.exit();
        }
    });

    // Now I position things on screen. Sorry for making this the hardest part of this screen.
    // I position things on the screen so that they look centered. This is why I make the
    // buttons the same size.
    logo.setPosition(440 - logo.getWidth() / 2, 290 - logo.getHeight());
    play.setSize(200, 50);
    opciones.setSize(200, 50);
    credits.setSize(200, 50);
    salir.setSize(200, 50);
    play.setPosition(40, 252.5f);
    opciones.setPosition(40, 187.5f);
    credits.setPosition(40, 122.5f);
    salir.setPosition(40, 57.5f);

    // Do not forget to add actors to the stage or we wouldn't see anything.
    stage.addActor(play);
    stage.addActor(opciones);
    stage.addActor(logo);
    stage.addActor(credits);
    stage.addActor(salir);
}

From source file:me.scarlet.undertailor.engine.Environment.java

License:Open Source License

public Environment(Undertailor tailor, String name) {
    this.name = name;
    this.events = new EventHelper(this);
    this.manager = tailor.getEnvironmentManager();
    this.scheduler = new Scheduler(this);
    this.overworld = new OverworldController(tailor.getRenderer(), this, new FitViewport(640, 480));
    this.ui = new UIController(this, tailor.getRenderer());
    this.destroyed = false;
}

From source file:me.scarlet.undertailor.manager.EnvironmentManager.java

License:Open Source License

public Viewport generateViewport() {
    switch (currentViewportType) {
    case STRETCH:
        return new StretchViewport(0F, 0F);
    case FIT:/*w  w w . j  a  v  a  2 s  .com*/
        return new FitViewport(0F, 0F);
    default:
        return null;
    }
}

From source file:com.exovum.test.animation.AnimatorMenuScreen.java

License:Creative Commons License

public AnimatorMenuScreen(final Game game) {
    this.batch = new SpriteBatch();
    this.game = game;
    screen = this;

    menuBackground = new Texture(Gdx.files.internal("beach-ocean-sea-bg/transparent-png/full_background.png"));
    menuMusic = Gdx.audio.newMusic(Gdx.files.internal("Carpe Diem.mp3"));

    stage = new Stage(new FitViewport(800, 480));
    Gdx.input.setInputProcessor(stage);//from   ww  w.j ava 2s .  c  o  m
    // Setup the UI skin. Pass the TextureAtlas too so it can find the default settings.
    TextureAtlas skinAtlas = new TextureAtlas(Gdx.files.internal("uiskin.atlas"));
    skin = new Skin(Gdx.files.internal("uiskin.json"), skinAtlas);

    mainTable = new Table(skin);
    //mainTable.defaults().expand().fill().padBottom(4f).padTop(4f);
    mainTable.setFillParent(true);

    // Add the title of the game at the top of the MenuScreen
    titleTable = new Table(skin);
    final Label titleLabel = new Label("Kordan Jirby", skin, "title");
    titleLabel.setColor(Color.FIREBRICK);
    //titleLabel.setStyle(new Label.LabelStyle(titleFont, Color.FIREBRICK));
    titleLabel.setAlignment(Align.center, Align.center);
    titleLabel.setPosition(stage.getWidth() / 2, stage.getHeight());
    //titleLabel.setFontScale(2.0f);
    titleTable.defaults().expand().fill().padBottom(4f).padTop(4f);

    titleTable.add(titleLabel);
    titleTable.padBottom(10f);

    // Bottom/Base Table: left-child holds buttonTable for menu buttons,
    //                    right-child holds some text info ie instructions or credits
    baseTable = new Table(skin);
    baseTable.defaults().expand().fill().padBottom(10f).padTop(10f);

    // Add the buttons for the user to press: play, help, credits, exit
    TextButton playButton = new TextButton("Play Game", skin, "small-font");
    TextButton helpButton = new TextButton("Instructions", skin, "small-font");
    TextButton creditsButton = new TextButton("Credits", skin, "small-font");
    TextButton exitButton = new TextButton("Exit", skin, "small-font");

    Table buttonTable = new Table(skin);
    // Add the button table as the left-child to baseTable
    baseTable.add(buttonTable);
    //menuTable.setBackground("console2");
    // Set the color of the BACKGROUND on the buttons
    Color buttonColor = Color.SKY;

    Gdx.app.log("AnimatorMenuScreen", "Color RGB SKY: " + Color.SKY.toString());
    playButton.setColor(buttonColor);
    helpButton.setColor(buttonColor);
    creditsButton.setColor(buttonColor);
    exitButton.setColor(buttonColor);
    // Set the color of the TEXT on the buttons
    //buttonColor = new Color(0.845f, 0.845f, 0.845f, 1);
    buttonColor = new Color(0.91f, 0.91f, 0.91f, 1);
    playButton.getLabel().setColor(buttonColor);
    helpButton.getLabel().setColor(buttonColor);
    creditsButton.getLabel().setColor(buttonColor);
    exitButton.getLabel().setColor(buttonColor);

    buttonTable.defaults().expand().fill().padBottom(8f).padTop(2f);
    buttonTable.add(playButton).width(180f).height(60f).row();
    buttonTable.add(helpButton).width(180f).height(60f).row();
    buttonTable.add(creditsButton).width(180f).height(60f).row();
    buttonTable.add(exitButton).width(180f).height(60f);
    buttonTable.padTop(30f).padBottom(30f);
    buttonTable.left();

    /*
    Temporary removal of infoTable
    It was not easy for me to add hidden/new text and for the layouts to update correctly.
    Changing plan to create a new Screen for Credits (and instructions too).
     */
    infoTable = new Table(skin); // = new Label("", skin, "small-font");
    infoTable.setVisible(false);
    infoTable.defaults().expand().fill().padBottom(8f).padTop(2f);
    //baseTable.add(infoTable);

    // Add title table at the top of mainTable
    mainTable.add(titleTable).row();
    // Add baseTable containing buttonTable to the next row of mainTable
    //mainTable.add(buttonTable);
    mainTable.add(baseTable);
    // Add mainTable to the stage
    stage.addActor(mainTable);

    // Event Listeners for the menu buttons
    playButton.addListener(new ChangeListener() {
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.app.log("AnimatorMenuScreen", "Pressed playButton");
            //game.setScreen(new AnimatorGameScreen(game, screen));
            ((Game) Gdx.app.getApplicationListener()).setScreen(new AnimatorGameScreen(game, screen));
        }
    });
    helpButton.addListener(new ChangeListener() {
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.app.log("AnimatorMenuScreen", "Pressed helpButton");
            //game.setScreen(new InstructionsScreen(game, screen));
            ((Game) Gdx.app.getApplicationListener()).setScreen(new InstructionsScreen(game, screen));
        }
    });
    creditsButton.addListener(new ChangeListener() {
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.app.log("AnimatorMenuScreen", "Pressed creditsButton");
            //game.setScreen(new CreditsScreen(game, screen));
            ((Game) Gdx.app.getApplicationListener()).setScreen(new CreditsScreen(game, screen));
            /*
            *  Attempt at adding text next to the buttons.
            *  Status: Unsuccessful. Updating the layout once adding the credits text
            *           does not update as easily as I hoped.
            *          Changing plan to use different Screens for Credits & Instructions.
            // If the table is visible and already showing credits, then 'minimize' infoTable
            if(infoTable.isVisible() && infoTable.getName() != null && infoTable.getName().equals("Credits")) {
            Gdx.app.log("AnimatorMenuScreen", "Hide the credits menu");
            infoTable.setVisible(false);
            infoTable.clearChildren();
            } else {
            Gdx.app.log("AnimatorMenuScreen", "Display the credits text");
            // Otherwise, make the infoTable visible and set text to the credits
            infoTable.setVisible(true);
            infoTable.clearChildren();
            //infoTable.center();
            Label musicLabel = new Label("Music\n" + "Pixel Peeker Polka - slower Kevin MacLeod (incompetech.com)\n" +
                    "Licensed under Creative Commons: By Attribution 3.0 License\n" +
                    "http://creativecommons.org/licenses/by/3.0/",
                    skin, "small-font");
            musicLabel.setColor(Color.BLACK);
            //infoTable.addActor(musicLabel);
                    
            infoTable.addActor(musicLabel);
            infoTable.padLeft(20f);
                    
            //musicLabel.setPosition(stage.getWidth() / 2 + 50, stage.getHeight() / 2 + 100, Align.right);
            infoTable.defaults().expand().fill().padBottom(8f).padTop(2f);
            musicLabel.setWidth(200f);
            //musicLabel.setWrap(true);
            musicLabel.setAlignment(Align.center);
                    
            //musicLabel.setFillParent(true);
            // "Invalidates this actor's layout, causing layout() to happen next time
            // validate() is called
            infoTable.invalidate();
            baseTable.invalidate();
            infoTable.center();
                    
            //infoTable.setFillParent(true);
                    
            //titleTable.add(musicLabel);
            }
            infoTable.setName("Credits");
            */
        }
    });
    exitButton.addListener(new ChangeListener() {
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.app.log("AnimatorMenuScreen", "Pressed exitButton - exiting application");
            Gdx.app.exit();
        }
    });

}

From source file:es.danirod.jumpdontdie.game.GameScreen.java

License:Open Source License

/**
 * Create the screen. Since this constructor cannot be invoked before libGDX is fully started,
 * it is safe to do critical code here such as loading assets and setting up the stage.
 * @param game//www.  ja v  a 2  s  .c o  m
 */
public GameScreen(MainGame game) {
    super(game);

    // Create a new Scene2D stage for displaying things.
    stage = new Stage(new FitViewport(640, 360));
    position = new Vector3(stage.getCamera().position);

    // Create a new Box2D world for managing things.
    world = new World(new Vector2(0, -10), true);
    world.setContactListener(new GameContactListener());

    // Get the sound effect references that will play during the game.
    jumpSound = game.getManager().get("audio/jump.ogg");
    dieSound = game.getManager().get("audio/die.ogg");
    backgroundMusic = game.getManager().get("audio/song.ogg");
}

From source file:es.danirod.jddprototype.game.GameScreen.java

License:Open Source License

/**
 * Create the screen. Since this constructor cannot be invoked before libGDX is fully started,
 * it is safe to do critical code here such as loading assets and setting up the stage.
 * @param game//  w w w  .  j ava  2s . c o  m
 */
public GameScreen(es.danirod.jddprototype.game.MainGame game) {
    super(game);

    // Create a new Scene2D stage for displaying things.
    stage = new Stage(new FitViewport(640, 360));
    position = new Vector3(stage.getCamera().position);

    // Create a new Box2D world for managing things.
    world = new World(new Vector2(0, -10), true);
    world.setContactListener(new GameContactListener());

    // Get the sound effect references that will play during the game.
    jumpSound = game.getManager().get("audio/jump.ogg");
    dieSound = game.getManager().get("audio/die.ogg");
    backgroundMusic = game.getManager().get("audio/song.ogg");
}

From source file:es.danirod.rectball.screens.AbstractScreen.java

License:Open Source License

@Override
public void show() {
    if (stage == null) {
        boolean landscape = Gdx.graphics.getWidth() > Gdx.graphics.getHeight();
        float ar = landscape ? (float) Gdx.graphics.getWidth() / Gdx.graphics.getHeight()
                : (float) Gdx.graphics.getHeight() / Gdx.graphics.getWidth();
        float width = (float) VIEWPORT_WIDTH;
        float height = landscape ? width : width * ar;
        Viewport viewport = new FitViewport(width, height);
        stage = new Stage(viewport);
    }//from  w ww .  j ava 2  s . co  m

    if (table == null) {
        table = new Table();
        table.setFillParent(true);
        table.pad(STAGE_PADDING);
        stage.addActor(table);
    } else {
        table.clear();
    }
    setUpInterface(table);

    Gdx.input.setCatchBackKey(true);
    if (handleBack) {
        InputMultiplexer multiplexer = new InputMultiplexer();
        multiplexer.addProcessor(new BackButtonInputProcessor(game));
        multiplexer.addProcessor(stage);
        Gdx.input.setInputProcessor(multiplexer);
    } else {
        Gdx.input.setInputProcessor(stage);
    }
}