Add visible return/save buttons to high scores pages

This commit is contained in:
Matt Low 2020-01-22 19:42:49 +04:00
parent 1989b4557b
commit 5645b4bcf5
4 changed files with 82 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -5,11 +5,14 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.me.pacman.Constants;
import com.me.pacman.FontRenderer;
import com.me.pacman.Score;
import com.me.pacman.PacDude;
import static com.me.pacman.state.HighScoresState.BUTTON;
public class HighScoreEntryState extends State {
private Texture background;
@ -21,6 +24,8 @@ public class HighScoreEntryState extends State {
private boolean flash;
private float elapsed;
private boolean buttonPressed;
public HighScoreEntryState(PacDude game, Score score) {
super(game);
this.score = score;
@ -58,6 +63,9 @@ public class HighScoreEntryState extends State {
Score score = scores[i];
game.fontRenderer.draw(game.batch, String.format("%-8d%s", score.score, score.scorer), (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, (17 - i) * 9);
}
game.fontRenderer.setColor(buttonPressed ? Color.BLUE : Color.WHITE);
game.fontRenderer.draw(game.batch, "save", 12 * Constants.TILE_SIZE, 5 * Constants.TILE_SIZE - 1);
}
@Override
@ -74,6 +82,12 @@ public class HighScoreEntryState extends State {
Gdx.input.setInputProcessor(null);
}
private void saveScoreAndReturn() {
score.scorer = String.valueOf(name);
game.highScores.addScore(score);
game.setNextState(new HighScoresState(game));
}
private final class Controller extends InputAdapter {
@Override
@ -110,15 +124,39 @@ public class HighScoreEntryState extends State {
flash = true;
break;
case Input.Keys.ENTER:
score.scorer = String.valueOf(name);
game.highScores.addScore(score);
game.setNextState(new HighScoresState(game));
case Input.Keys.BACK:
case Input.Keys.ESCAPE:
saveScoreAndReturn();
break;
}
elapsed = 0f;
return super.keyDown(keycode);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY));
if (BUTTON.contains(coords)) {
buttonPressed = true;
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (!buttonPressed) {
return true;
}
buttonPressed = false;
Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY));
if (BUTTON.contains(coords)) {
saveScoreAndReturn();
}
return true;
}
}
}

View File

@ -5,15 +5,25 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.me.pacman.Constants;
import com.me.pacman.PacDude;
import com.me.pacman.Score;
public class HighScoresState extends State {
public static final MenuState.BoundingBox BUTTON = new MenuState.BoundingBox(
11 * Constants.TILE_SIZE,
5 * Constants.TILE_SIZE,
17 * Constants.TILE_SIZE,
6 * Constants.TILE_SIZE);
private Texture background;
private boolean buttonPressed = false;
public HighScoresState(PacDude game) {
super(game);
}
@ -40,6 +50,9 @@ public class HighScoresState extends State {
Score score = scores[i];
game.fontRenderer.draw(game.batch, String.format("%-8d%s", score.score, score.scorer), (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, (18 - i) * 9);
}
game.fontRenderer.setColor(buttonPressed ? Color.BLUE : Color.WHITE);
game.fontRenderer.draw(game.batch, "return", 11 * Constants.TILE_SIZE, 5 * Constants.TILE_SIZE - 1);
}
@Override
@ -56,6 +69,7 @@ public class HighScoresState extends State {
public boolean keyDown(int keycode) {
switch(keycode) {
case Input.Keys.ENTER:
case Input.Keys.BACK:
case Input.Keys.ESCAPE:
game.setNextState(new MenuState(game));
break;
@ -63,6 +77,30 @@ public class HighScoresState extends State {
return super.keyDown(keycode);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY));
if (BUTTON.contains(coords)) {
buttonPressed = true;
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (!buttonPressed) {
return true;
}
buttonPressed = false;
Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY));
if (BUTTON.contains(coords)) {
game.setNextState(new MenuState(game));
}
return true;
}
}
}

View File

@ -19,8 +19,8 @@ public class MenuState extends LevelState {
public static final int NEW_GAME = 0;
public static final int HIGH_SCORES = 1;
private BoundingBox NEW_GAME_BOX = new BoundingBox(88, 66, 136, 84);
private BoundingBox HIGH_SCORE_BOX = new BoundingBox(88, 42, 136, 60);
private static final BoundingBox NEW_GAME_BOX = new BoundingBox(88, 66, 136, 84);
private static final BoundingBox HIGH_SCORE_BOX = new BoundingBox(88, 42, 136, 60);
private int selectedOption;
@ -39,6 +39,7 @@ public class MenuState extends LevelState {
switch(Gdx.app.getType()) {
case Android:
case iOS:
selectedOption = -1;
break;
default: