Coverage Summary for Class: LeaderboardScreen (io.github.unisim.ui)
| Class |
Method, %
|
Branch, %
|
Line, %
|
| LeaderboardScreen |
0%
(0/8)
|
0%
(0/2)
|
0%
(0/48)
|
| LeaderboardScreen$1 |
0%
(0/2)
|
0%
(0/2)
|
| Total |
0%
(0/10)
|
0%
(0/2)
|
0%
(0/50)
|
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.GL20;
import com.badlogic.gdx.graphics.Texture;
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.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import io.github.unisim.GameState;
import io.github.unisim.Leaderboard;
import io.github.unisim.Score;
import java.util.List;
public class LeaderboardScreen implements Screen {
private Stage stage;
private Table table;
private Skin altskin = GameState.altSkin;
private TextButton backButton;
private InputMultiplexer inputMultiplexer = new InputMultiplexer();
private Leaderboard leaderboard;
public LeaderboardScreen() {
leaderboard = new Leaderboard();
stage = new Stage();
table = new Table();
// Create an Image for the background
Image backgroundImage = new Image(new Texture(Gdx.files.internal("ui/leaderboardbackground.png")));
backgroundImage.setFillParent(true); // Fill the screen with image
stage.addActor(backgroundImage); // Add the background to the stage
// Display leaderboard title
Label title = new Label("Leaderboard", altskin);
title.setFontScale(1.3f);
title.setPosition(Gdx.graphics.getWidth() / 2 - 150,
Gdx.graphics.getHeight() - 70);
stage.addActor(title);
// Back button
backButton = new TextButton("Back", altskin);
backButton.addListener(new ClickListener() {
@Override
public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
// Go back to previous screen
GameState.popScreen();
}
});
// Fetch the top scores
List<Score> topScores = leaderboard.getTopScores(5);
int yPosition = Gdx.graphics.getHeight() - 150;
int count = 1;
// Render each score on the screen
for (Score score : topScores) {
Label playerNameLabel = new Label(count + ". " + score.getPlayerName() + ":", altskin);
Label scoreLabel = new Label("" + score.getScore(), altskin);
playerNameLabel.setFontScale(0.8f);
scoreLabel.setFontScale(0.8f);
playerNameLabel.setPosition(Gdx.graphics.getWidth() / 2 - 350, yPosition);
scoreLabel.setPosition(Gdx.graphics.getWidth() / 2 + 250, yPosition);
stage.addActor(playerNameLabel);
stage.addActor(scoreLabel);
count++;
yPosition -= 100; // Move down for the next entry
}
// Add UI elements to stage
table.setFillParent(true);
table.center().center();
table.pad(700, 100, 100, 100);
table.add(backButton).center().width(150).height(40).padBottom(10);
stage.addActor(table);
inputMultiplexer.addProcessor(GameState.fullscreenInputProcessor);
inputMultiplexer.addProcessor(stage);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
// Clear the screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
@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();
}
}