Add a horizontal band on the pause menu

This commit is contained in:
Lonami Exo 2017-01-28 18:01:46 +01:00
parent daef6d047e
commit 9eaba524bd
8 changed files with 309 additions and 24 deletions

View file

@ -0,0 +1,75 @@
package io.github.lonamiwebs.klooni.actors;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align;
import io.github.lonamiwebs.klooni.game.GameLayout;
import io.github.lonamiwebs.klooni.game.Scorer;
// Score and pause menu band actually
public class Band extends Actor {
private final Scorer scorer;
private final ShapeRenderer shapeRenderer; // To draw the horizontal "Band"
private final Color bandColor;
public final Rectangle scoreBounds;
public final Rectangle infoBounds;
public final Label infoLabel;
public final Label scoreLabel;
public Band(final GameLayout layout, final Scorer aScorer, final Color aBandColor) {
scorer = aScorer;
bandColor = aBandColor;
shapeRenderer = new ShapeRenderer(20); // Only 20 vertex are required, maybe less
Label.LabelStyle scoreStyle = new Label.LabelStyle();
scoreStyle.font = new BitmapFont(Gdx.files.internal("font/geosans-light.fnt"));
scoreLabel = new Label("", scoreStyle);
scoreLabel.setAlignment(Align.center);
infoLabel = new Label("pause menu", scoreStyle);
infoLabel.setAlignment(Align.center);
scoreBounds = new Rectangle();
infoBounds = new Rectangle();
layout.update(this);
}
public void setGameOver() {
infoLabel.setText("no moves left");
}
@Override
public void draw(Batch batch, float parentAlpha) {
// We need to end (thus flush) the batch or things will mess up!
batch.end();
float w = getWidth();
float h = getHeight();
// TODO This is not the best way to apply the transformation, but, oh well
float x = getParent().getX();
float y = getParent().getY();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(bandColor);
shapeRenderer.rect(x + getX(), y + getY(), w, h);
shapeRenderer.end();
batch.begin();
scoreLabel.setBounds(x + scoreBounds.x, y + scoreBounds.y, scoreBounds.width, scoreBounds.height);
scoreLabel.setText(Integer.toString(scorer.getCurrentScore()));
scoreLabel.draw(batch, parentAlpha);
infoLabel.setBounds(x + infoBounds.x, y + infoBounds.y, infoBounds.width, infoBounds.height);
infoLabel.draw(batch, parentAlpha);
}
}

View file

@ -4,6 +4,8 @@ package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;
import io.github.lonamiwebs.klooni.actors.Band;
// Helper class to calculate the size of each element
//
// TODO In a future, perhaps this could handle landscape mode differently
@ -71,4 +73,20 @@ public class GameLayout {
marginWidth, 0f,
availableWidth, pieceHolderHeight);
}
public void update(Band band) {
final Rectangle area = new Rectangle(
0, pieceHolderHeight + boardHeight,
screenWidth, scoreHeight);
band.setBounds(area.x, area.y, area.width, area.height);
// Let the band have the following shape:
// 10% (100) padding
// 35% (90%) score label
// 10% (55%) padding
// 35% (45%) info label
// 10% (10%) padding
band.scoreBounds.set(area.x, area.y + area.height * 0.55f, area.width, area.height * 0.35f);
band.infoBounds.set(area.x, area.y + area.height * 0.10f, area.width, area.height * 0.35f);
}
}

View file

@ -41,8 +41,11 @@ public class Scorer {
scoreStyle.font = new BitmapFont(Gdx.files.internal("font/geosans-light.fnt"));
currentScoreLabel = new Label("0", scoreStyle);
currentScoreLabel.setColor(Color.GOLD);
currentScoreLabel.setAlignment(Align.right);
maxScoreLabel = new Label(Integer.toString(maxScore), scoreStyle);
maxScoreLabel.setColor(new Color(0x65D681FF));
layout.update(this);
}
@ -60,6 +63,10 @@ public class Scorer {
newRecord = currentScore > maxScore;
}
public int getCurrentScore() {
return currentScore;
}
public void saveScore() {
if (newRecord) {
prefs.putInteger("maxScore", currentScore);
@ -84,7 +91,7 @@ public class Scorer {
currentScoreLabel.setText(Integer.toString(MathUtils.round(shownScore)));
}
batch.setColor(Color.WHITE);
batch.setColor(Color.GOLD);
batch.draw(cupTexture, cupArea.x, cupArea.y, cupArea.width, cupArea.height);
currentScoreLabel.draw(batch, 1f);
maxScoreLabel.draw(batch, 1f);

View file

@ -39,7 +39,7 @@ public class GameScreen implements Screen, InputProcessor {
board = new Board(layout, 10);
holder = new PieceHolder(layout, 3);
pauseMenu = new PauseMenuStage(game, scorer);
pauseMenu = new PauseMenuStage(layout, game, scorer);
}
private boolean isGameOver() {
@ -115,7 +115,7 @@ public class GameScreen implements Screen, InputProcessor {
@Override
public boolean keyUp(int keycode) {
if (keycode == Input.Keys.P) // Pause
pauseMenu.show();
pauseMenu.show(false);
return false;
}
@ -139,7 +139,7 @@ public class GameScreen implements Screen, InputProcessor {
// After the piece was put, check if it's game over
if (isGameOver()) {
pauseMenu.show();
pauseMenu.show(true);
}
return true;
} else {

View file

@ -16,6 +16,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.actors.Band;
import io.github.lonamiwebs.klooni.game.GameLayout;
import io.github.lonamiwebs.klooni.game.Scorer;
public class PauseMenuStage extends Stage {
@ -24,15 +26,21 @@ public class PauseMenuStage extends Stage {
private boolean shown;
private boolean hiding;
private Scorer scorer;
private final Band band;
private final Scorer scorer;
public PauseMenuStage(final Klooni game, final Scorer aScorer) {
public PauseMenuStage(final GameLayout layout, final Klooni game, final Scorer aScorer) {
scorer = aScorer;
Table table = new Table();
table.setFillParent(true);
addActor(table);
// Current and maximum score band.
// Do not add it to the table not to over-complicate things.
band = new Band(layout, scorer, Color.SKY);
addActor(band);
// Home screen button
ImageButton.ImageButtonStyle homeStyle = new ImageButton.ImageButtonStyle(
game.skin.newDrawable("button_up", Color.FIREBRICK),
@ -94,7 +102,7 @@ public class PauseMenuStage extends Stage {
});
}
void show() {
void show(final boolean gameOver) {
scorer.saveScore();
lastInputProcessor = Gdx.input.getInputProcessor();
@ -102,6 +110,9 @@ public class PauseMenuStage extends Stage {
shown = true;
hiding = false;
if (gameOver)
band.setGameOver();
addAction(Actions.moveTo(0, Gdx.graphics.getHeight()));
addAction(Actions.moveTo(0, 0, 0.75f, Interpolation.swingOut));
}
@ -135,7 +146,6 @@ public class PauseMenuStage extends Stage {
// Draw an overlay rectangle with not all the opacity
if (shown) {
ShapeRenderer shapeRenderer = new ShapeRenderer(20);
Gdx.gl.glEnable(GL20.GL_BLEND);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(1f, 1f, 1f, 0.3f);