| 1 | package io.github.some_example_name; | |
| 2 | ||
| 3 | import com.badlogic.gdx.Gdx; | |
| 4 | import com.badlogic.gdx.Screen; | |
| 5 | import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
| 6 | import com.badlogic.gdx.scenes.scene2d.Actor; | |
| 7 | import com.badlogic.gdx.scenes.scene2d.InputEvent; | |
| 8 | import com.badlogic.gdx.scenes.scene2d.Stage; | |
| 9 | import com.badlogic.gdx.scenes.scene2d.ui.*; | |
| 10 | import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; | |
| 11 | import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; | |
| 12 | import com.badlogic.gdx.utils.ScreenUtils; | |
| 13 | import com.badlogic.gdx.utils.viewport.ScreenViewport; | |
| 14 | ||
| 15 | /** | |
| 16 | * Settings screen allowing the player to adjust game volume. | |
| 17 | */ | |
| 18 | public class SettingsScreen implements Screen { | |
| 19 | ||
| 20 | private static float volume = 0.5f; // shared game volume | |
| 21 | ||
| 22 | private Stage stage; | |
| 23 | private Skin skin; | |
| 24 | private SpriteBatch batch; | |
| 25 | ||
| 26 | private final MyGame game; | |
| 27 | ||
| 28 | public SettingsScreen(MyGame game) { | |
| 29 | this.game = game; | |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * Global access to volume level. | |
| 34 | */ | |
| 35 | public static float getNoise() { | |
| 36 | return volume; | |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 40 | * Sets the global volume level. | |
| 41 | */ | |
| 42 | public static void setNoise(float value) { | |
| 43 | volume = value; | |
| 44 | } | |
| 45 | ||
| 46 | @Override | |
| 47 | public void show() { | |
| 48 | batch = new SpriteBatch(); | |
| 49 | stage = new Stage(new ScreenViewport()); | |
| 50 | Gdx.input.setInputProcessor(stage); | |
| 51 | ||
| 52 | skin = new Skin(Gdx.files.internal("uiskin.json")); | |
| 53 | ||
| 54 | Label titleLabel = new Label("Settings", skin); | |
| 55 | titleLabel.setFontScale(1.5f); | |
| 56 | ||
| 57 | Label volumeLabel = new Label("Volume", skin); | |
| 58 | ||
| 59 | Slider volumeSlider = new Slider(0.01f, 1f, 0.01f, false, skin); | |
| 60 | volumeSlider.setValue(volume); | |
| 61 | ||
| 62 | volumeSlider.addListener(new ChangeListener() { | |
| 63 | @Override | |
| 64 | public void changed(ChangeEvent event, Actor actor) { | |
| 65 |
1
1. changed : removed call to io/github/some_example_name/SettingsScreen::setNoise → NO_COVERAGE |
setNoise(volumeSlider.getValue()); |
| 66 |
1
1. changed : removed call to com/badlogic/gdx/audio/Music::setVolume → NO_COVERAGE |
MyGame.music.setVolume(getNoise()); |
| 67 | } | |
| 68 | }); | |
| 69 | ||
| 70 | TextButton backButton = new TextButton("Back to Menu", skin); | |
| 71 | backButton.addListener(new ClickListener() { | |
| 72 | @Override | |
| 73 | public void clicked(InputEvent event, float x, float y) { | |
| 74 |
1
1. clicked : removed call to io/github/some_example_name/MyGame::setScreen → NO_COVERAGE |
game.setScreen(new MenuScreen(game)); |
| 75 | } | |
| 76 | }); | |
| 77 | ||
| 78 | Table table = new Table(); | |
| 79 | table.setFillParent(true); | |
| 80 | table.center(); | |
| 81 | ||
| 82 | table.add(titleLabel).padBottom(40).row(); | |
| 83 | table.add(volumeLabel).padBottom(10).row(); | |
| 84 | table.add(volumeSlider).width(300).padBottom(30).row(); | |
| 85 | table.add(backButton).width(200); | |
| 86 | ||
| 87 | stage.addActor(table); | |
| 88 | } | |
| 89 | ||
| 90 | @Override | |
| 91 | public void render(float delta) { | |
| 92 | ScreenUtils.clear(0, 0, 0, 1); | |
| 93 | stage.act(delta); | |
| 94 | stage.draw(); | |
| 95 | } | |
| 96 | ||
| 97 | @Override public void resize(int width, int height) { | |
| 98 | stage.getViewport().update(width, height, true); | |
| 99 | } | |
| 100 | ||
| 101 | @Override public void pause() {} | |
| 102 | @Override public void resume() {} | |
| 103 | @Override public void hide() {} | |
| 104 | ||
| 105 | @Override | |
| 106 | public void dispose() { | |
| 107 | stage.dispose(); | |
| 108 | skin.dispose(); | |
| 109 | batch.dispose(); | |
| 110 | } | |
| 111 | } | |
Mutations | ||
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 74 |
1.1 |