Coverage Summary for Class: SettingsScreen (io.github.unisim.ui)
| Class |
Method, %
|
Branch, %
|
Line, %
|
| SettingsScreen |
0%
(0/10)
|
0%
(0/4)
|
0%
(0/75)
|
| SettingsScreen$1 |
0%
(0/2)
|
0%
(0/3)
|
| SettingsScreen$2 |
0%
(0/2)
|
0%
(0/3)
|
| SettingsScreen$3 |
0%
(0/2)
|
0%
(0/2)
|
0%
(0/4)
|
| Total |
0%
(0/16)
|
0%
(0/6)
|
0%
(0/85)
|
package io.github.unisim.ui;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.ScreenUtils;
import io.github.unisim.GameState;
import io.github.unisim.Settings.Difficulty;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
/**
* The settings screen that allows the player to adjust the volume.
*/
public class SettingsScreen implements Screen {
private Stage stage;
private Table table;
private Skin altskin = GameState.altSkin;
private Slider volumeSlider;
private TextButton backButton;
private Label difficultyLabel;
private String[] difficulties = { "Easy", "Medium", "Hard" };
private int currentDifficultyIndex = 1;
public static int difficultyVariable;
private InputMultiplexer inputMultiplexer = new InputMultiplexer();
/**
* Create a new Settings screen and draw the initial UI layout.
*/
public SettingsScreen() {
stage = new Stage();
table = new Table();
difficultyLabel = new Label(difficulties[currentDifficultyIndex], altskin);
difficultyLabel.setFontScale(1.0f);
TextButton leftArrow = new TextButton("<", altskin);
TextButton rightArrow = new TextButton(">", altskin);
// Create an Image for the background
Image backgroundImage = new Image(new Texture(Gdx.files.internal("ui/settingsbackground.png")));
backgroundImage.setFillParent(true); // Fill the screen with image
stage.addActor(backgroundImage); // Add the background to the stage
// Display settings title
Label title = new Label("Settings", altskin);
title.setFontScale(1.3f);
title.setPosition(Gdx.graphics.getWidth() / 2 - 100,
Gdx.graphics.getHeight() - 150);
stage.addActor(title);
// Display volume label
Label volumeLabel = new Label("Difficulty", altskin);
volumeLabel.setFontScale(0.8f);
volumeLabel.setPosition(Gdx.graphics.getWidth() / 2 - 280,
Gdx.graphics.getHeight() - 235);
stage.addActor(volumeLabel);
// Display difficulty label
Label diffLabel = new Label("Volume", altskin);
diffLabel.setFontScale(0.8f);
diffLabel.setPosition(Gdx.graphics.getWidth() / 2 - 240,
Gdx.graphics.getHeight() - 405);
stage.addActor(diffLabel);
leftArrow.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
currentDifficultyIndex = (currentDifficultyIndex - 1 + difficulties.length) % difficulties.length;
updateDifficulty();
// confirmRestart();
}
});
rightArrow.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
// Increase difficulty index
currentDifficultyIndex = (currentDifficultyIndex + 1) % difficulties.length;
updateDifficulty();
// confirmRestart();
}
});
// Volume slider
volumeSlider = new Slider(0.0f, 1.0f, 0.01f, false, altskin);
volumeSlider.setValue(GameState.musicManager.getVolume()); // Set current volume
volumeSlider.setSize(200, 50);
volumeSlider.addListener(event -> {
// Adjust the game volume based on slider value
GameState.musicManager.setVolume(volumeSlider.getValue());
return false;
});
// Difficulty Section
Table difficultyTable = new Table();
difficultyTable.padTop(200);
difficultyTable.add(leftArrow).center().width(50).height(40).padRight(20); // Left arrow
difficultyTable.add(difficultyLabel).center().width(130).height(40); // Center the difficulty label
difficultyTable.add(rightArrow).center().width(50).height(40).padLeft(20); // Right arrow
table.add(difficultyTable).center(); // Add the nested difficulty table to the main table
table.row();
// Back button
backButton = new TextButton("Back", altskin);
backButton.setSize(200, 60);
backButton.addListener(new ClickListener() {
@Override
public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
// Go back to the start menu
if (GameState.currentScreen instanceof GameScreen) {
GameState.musicManager.resumeMusic(); // Resume game music
}
GameState.popScreen();
}
});
// Add UI elements to stage
table.setFillParent(true);
table.center().center();
table.pad(30, 100, 100, 100);
table.add(volumeSlider).center().width(250).height(40).padTop(130);
table.row();
table.add(backButton).center().width(150).height(40).padBottom(10).padTop(190);
stage.addActor(table);
inputMultiplexer.addProcessor(GameState.fullscreenInputProcessor);
inputMultiplexer.addProcessor(stage);
}
@Override
public void show() {
Difficulty diff = GameState.settings.getDifficulty();
if (diff == Difficulty.EASY) {
currentDifficultyIndex = 0;
} else if (diff == Difficulty.MEDIUM) {
currentDifficultyIndex = 1;
} else {
currentDifficultyIndex = 2;
}
updateDifficulty();
volumeSlider.setValue(GameState.musicManager.getVolume());
}
@Override
public void render(float delta) {
// Clear the screen
ScreenUtils.clear(GameState.UISecondaryColour);
// Draw the stage containing the volume slider and buttons
stage.act(delta);
stage.draw();
}
public void updateDifficulty() {
// Update the difficulty label
difficultyLabel.setText(difficulties[currentDifficultyIndex]);
// Update the GameState difficulty
GameState.settings.setDifficulty(Difficulty.values()[currentDifficultyIndex]);
// GameState.restartGame();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void pause() {
}
@Override
public void resume() {
Gdx.input.setInputProcessor(inputMultiplexer);
}
@Override
public void hide() {
}
@Override
public void dispose() {
stage.dispose();
altskin.dispose();
}
}