Android Open Source - killingspree Options Screen






From Project

Back to project page killingspree.

License

The source code is released under:

MIT License

If you think the Android project killingspree listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sillygames.killingSpree.screens;
/*from  w  ww  . j a v a2 s .  co  m*/
import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Input.TextInputListener;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.sillygames.killingSpree.KillingSpree;
import com.sillygames.killingSpree.controls.InputController;
import com.sillygames.killingSpree.helpers.MyButton;

public class OptionsScreen extends AbstractScreen {

    private BitmapFont font;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private FitViewport viewport;
    private ArrayList<MyButton> buttons;
    private MyButton currentButton;
    private MyButton backButton;
    private MyButton changeNameButton;
    private MyButton setControlsScaleButton;
    private boolean buttonPressed;
    
    public OptionsScreen(KillingSpree game) {
        super(game);
        show();
    }

    @Override
    public void render(float delta) {
        renderButtons(delta);
        processInput();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height);
        camera.update();
    }

    @Override
    public void show() {
        font = game.getFont(170);
        batch = new SpriteBatch();
        camera = new OrthographicCamera();
        viewport = new FitViewport(1280, 720, camera);
        camera.setToOrtho(false, 1280, 720);
        buttons = new ArrayList<MyButton>();
        addAllButtons();
        buttonPressed = false;
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        batch.dispose();
        font.dispose();
    }
    
    public void processInput() {
        currentButton = currentButton.process();
        if (InputController.instance.buttonA()){
            processButton();
            return;
        }
        if(Gdx.input.isTouched()) {
            processTouched();
        }
    }
    
    private void processButton() {
        if (buttonPressed)
            return;
        if (currentButton == changeNameButton) {
            final Preferences prefs = Gdx.app.getPreferences("profile");
            buttonPressed = true;
            Gdx.input.getTextInput(new TextInputListener() {
                
                @Override
                public void input(String text) {
                    prefs.putString("name", text);
                    prefs.flush();
                    buttonPressed = false;
                }
                
                @Override
                public void canceled() {
                    buttonPressed = false;
                }
            }, "Enter your name", prefs.getString("name"));
        } else if (currentButton == setControlsScaleButton) {
            final Preferences prefs = Gdx.app.getPreferences("settings");
            buttonPressed = true;
            Gdx.input.getTextInput(new TextInputListener() {
                
                @Override
                public void input(String text) {
                    try {
                        int scaling = Integer.parseInt(text);
                        prefs.putInteger("scaling", scaling);
                        prefs.flush();
                    } catch (Exception e) {
                        game.platformServices.toast("Please enter a valid integer");
                    }
                    buttonPressed = false;
                }
                
                @Override
                public void canceled() {
                    buttonPressed = false;
                }
            }, "Enter scaling percentage for onscreen controls",
            Integer.toString(prefs.getInteger("scaling")));
        } else if (currentButton == backButton) {
            game.setScreen(new MainMenuScreen(game));
        }
    }

    private void processTouched() {
        Vector2 touchVector = viewport.unproject(new Vector2(Gdx.input.getX(),
                Gdx.input.getY()));
        for (MyButton button: buttons) {
            if(button.isPressed(touchVector, font)) {
                if (currentButton == button) {
                    processButton();
                    return;
                }
                currentButton.setActive(false);
                currentButton = button;
                currentButton.setActive(true);
                renderButtons(0.01f);
                return;
            }
        }
    }
    
    public MyButton addButton(String text, float x, float y) {
        MyButton button = new MyButton(text, x, y);
        buttons.add(button);
        return button;
    }
    
    private void renderButtons(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        for (MyButton button: buttons) {
            button.render(batch, font, delta);
        }
        batch.end();
    }
    
    private void addAllButtons() {
        changeNameButton = addButton("change name", 100, 650);
        setControlsScaleButton  = addButton("set buttons scale", 100, 450);
        backButton = addButton("back", 100, 250);
        changeNameButton.setActive(true);
        currentButton = changeNameButton;
        setControlsScaleButton.setNorth(changeNameButton);
        backButton.setNorth(setControlsScaleButton);
    }
    
}




Java Source Code List

com.sillygames.killingSpree.AndroidLauncher.java
com.sillygames.killingSpree.KillingSpree.java
com.sillygames.killingSpree.PlatformServices.java
com.sillygames.killingSpree.categories.EnemyCategory.java
com.sillygames.killingSpree.categories.ExplodingWeaponCategory.java
com.sillygames.killingSpree.categories.LivingCategory.java
com.sillygames.killingSpree.categories.NonExplodingWeaponCategory.java
com.sillygames.killingSpree.clientEntities.ClientArrow.java
com.sillygames.killingSpree.clientEntities.ClientBlob.java
com.sillygames.killingSpree.clientEntities.ClientBomb.java
com.sillygames.killingSpree.clientEntities.ClientBullet.java
com.sillygames.killingSpree.clientEntities.ClientEntity.java
com.sillygames.killingSpree.clientEntities.ClientFly.java
com.sillygames.killingSpree.clientEntities.ClientFrog.java
com.sillygames.killingSpree.clientEntities.ClientPlayer.java
com.sillygames.killingSpree.clientEntities.ClientTestPlayer.java
com.sillygames.killingSpree.controls.InputController.java
com.sillygames.killingSpree.controls.onScreenControls.java
com.sillygames.killingSpree.desktop.DesktopLauncher.java
com.sillygames.killingSpree.desktop.DesktopServices.java
com.sillygames.killingSpree.helpers.EntityUtils.java
com.sillygames.killingSpree.helpers.Event.java
com.sillygames.killingSpree.helpers.MyButton.java
com.sillygames.killingSpree.helpers.MyConnection.java
com.sillygames.killingSpree.helpers.Utils.java
com.sillygames.killingSpree.managers.LevelLoader.java
com.sillygames.killingSpree.managers.WorldBodyUtils.java
com.sillygames.killingSpree.managers.WorldManager.java
com.sillygames.killingSpree.managers.WorldRenderer.java
com.sillygames.killingSpree.managers.physics.Body.java
com.sillygames.killingSpree.managers.physics.CollisionProcessor.java
com.sillygames.killingSpree.managers.physics.Ray.java
com.sillygames.killingSpree.managers.physics.WorldDebugRenderer.java
com.sillygames.killingSpree.managers.physics.World.java
com.sillygames.killingSpree.networking.ControlsSender.java
com.sillygames.killingSpree.networking.NetworkRegisterer.java
com.sillygames.killingSpree.networking.StateProcessor.java
com.sillygames.killingSpree.networking.messages.AudioMessage.java
com.sillygames.killingSpree.networking.messages.ClientDetailsMessage.java
com.sillygames.killingSpree.networking.messages.ConnectMessage.java
com.sillygames.killingSpree.networking.messages.ControlsMessage.java
com.sillygames.killingSpree.networking.messages.EntityState.java
com.sillygames.killingSpree.networking.messages.GameStateMessage.java
com.sillygames.killingSpree.networking.messages.PlayerNamesMessage.java
com.sillygames.killingSpree.networking.messages.ServerStatusMessage.java
com.sillygames.killingSpree.pool.AssetLoader.java
com.sillygames.killingSpree.pool.EntityPool.java
com.sillygames.killingSpree.pool.MessageObjectPool.java
com.sillygames.killingSpree.pool.Pool.java
com.sillygames.killingSpree.pool.Poolable.java
com.sillygames.killingSpree.renderers.HUDRenderer.java
com.sillygames.killingSpree.screens.AbstractScreen.java
com.sillygames.killingSpree.screens.ClientDiscoveryScreen.java
com.sillygames.killingSpree.screens.GameScreen.java
com.sillygames.killingSpree.screens.LobbyScreen.java
com.sillygames.killingSpree.screens.MainMenuScreen.java
com.sillygames.killingSpree.screens.OptionsScreen.java
com.sillygames.killingSpree.screens.SplashScreen.java
com.sillygames.killingSpree.screens.settings.Constants.java
com.sillygames.killingSpree.serverEntities.ServerArrow.java
com.sillygames.killingSpree.serverEntities.ServerBlob.java
com.sillygames.killingSpree.serverEntities.ServerBomb.java
com.sillygames.killingSpree.serverEntities.ServerBullet.java
com.sillygames.killingSpree.serverEntities.ServerEntity.java
com.sillygames.killingSpree.serverEntities.ServerFly.java
com.sillygames.killingSpree.serverEntities.ServerFrog.java
com.sillygames.killingSpree.serverEntities.ServerPlayer.java
com.sillygames.killingSpree.sound.SFXPlayer.java