Allow swiping for inputting high score name

This commit is contained in:
Matt Low 2020-01-22 20:50:30 +04:00
parent fc912863ad
commit ab3e4a177b
2 changed files with 82 additions and 30 deletions

View File

@ -9,9 +9,10 @@ public class FontRenderer {
public static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz0123456789!?()[]<>$&*:#^~-_/\\".toCharArray();
public static final int[] CHAR_TO_INDEX = new int[256];
private Color color = Color.WHITE;
private TextureRegion[][] sprites;
private final TextureRegion[][] sprites;
private Color color = Color.WHITE;
public FontRenderer(TextureRegion[][] sprites) {
this.sprites = sprites;
@ -31,8 +32,7 @@ public class FontRenderer {
continue;
}
int i = CHAR_TO_INDEX[c];
TextureRegion region = sprites[i / 8][i % 8];
batch.draw(region, x, y);
batch.draw(sprites[i / 8][i % 8], x, y);
x += 8;
}

View File

@ -10,6 +10,7 @@ import com.me.pacman.Constants;
import com.me.pacman.FontRenderer;
import com.me.pacman.Score;
import com.me.pacman.PacDude;
import com.me.pacman.entity.Direction;
import static com.me.pacman.state.HighScoresState.BUTTON;
@ -27,6 +28,8 @@ public class HighScoreEntryState extends State {
private boolean buttonPressed;
private Direction nextDirection;
public HighScoreEntryState(PacDude game, Score score, int position) {
super(game);
this.position = position;
@ -35,6 +38,7 @@ public class HighScoreEntryState extends State {
this.flash = false;
this.elapsed = 0f;
this.name = score.scorer.toCharArray();
this.nextDirection = null;
}
@Override
@ -82,6 +86,42 @@ public class HighScoreEntryState extends State {
elapsed = 0f;
flash = !flash;
}
if (nextDirection != null) {
switch (nextDirection) {
case UP:
int nextIndex = FontRenderer.CHAR_TO_INDEX[name[currLetter]] - 1;
if (nextIndex < 0) {
nextIndex = 25;
}
flash = false;
name[currLetter] = FontRenderer.CHARS[nextIndex];
break;
case DOWN:
nextIndex = FontRenderer.CHAR_TO_INDEX[name[currLetter]] + 1;
if (nextIndex > 25) {
nextIndex = 0;
}
flash = false;
name[currLetter] = FontRenderer.CHARS[nextIndex];
break;
case LEFT:
currLetter--;
if (currLetter < 0) {
currLetter = 2;
}
flash = true;
break;
case RIGHT:
currLetter++;
if (currLetter > 2) {
currLetter = 0;
}
flash = true;
break;
}
nextDirection = null;
}
}
@Override
@ -97,38 +137,23 @@ public class HighScoreEntryState extends State {
private final class Controller extends InputAdapter {
int downX;
int downY;
@Override
public boolean keyDown(int keycode) {
switch(keycode) {
case Input.Keys.DOWN:
int nextIndex = FontRenderer.CHAR_TO_INDEX[name[currLetter]] - 1;
if (nextIndex < 0) {
nextIndex = 25;
}
flash = false;
name[currLetter] = FontRenderer.CHARS[nextIndex];
nextDirection = Direction.DOWN;
break;
case Input.Keys.UP:
nextIndex = FontRenderer.CHAR_TO_INDEX[name[currLetter]] + 1;
if (nextIndex > 25) {
nextIndex = 0;
}
flash = false;
name[currLetter] = FontRenderer.CHARS[nextIndex];
nextDirection = Direction.UP;
break;
case Input.Keys.RIGHT:
currLetter++;
if (currLetter > 2) {
currLetter = 0;
}
flash = true;
nextDirection = Direction.RIGHT;
break;
case Input.Keys.LEFT:
currLetter--;
if (currLetter < 0) {
currLetter = 2;
}
flash = true;
nextDirection = Direction.LEFT;
break;
case Input.Keys.ENTER:
case Input.Keys.BACK:
@ -146,19 +171,46 @@ public class HighScoreEntryState extends State {
if (BUTTON.contains(coords)) {
buttonPressed = true;
}
downX = screenX;
downY = screenY;
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (!buttonPressed) {
if (buttonPressed) {
Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY));
if (BUTTON.contains(coords)) {
saveScoreAndReturn();
}
return true;
}
buttonPressed = false;
Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY));
if (BUTTON.contains(coords)) {
saveScoreAndReturn();
int relaX = screenX - downX;
int relaY = screenY - downY;
if (Math.abs(relaX) < 50 && Math.abs(relaY) < 50) {
// didn't move enough to consider a swipe
return true;
}
if (Math.abs(relaX) > Math.abs(relaY)) {
// x > y, so moving left-right
if (relaX > 0) {
nextDirection = Direction.RIGHT;
} else {
nextDirection = Direction.LEFT;
}
} else {
// else, moving up/down
int nextIndex;
if (relaY > 0) {
nextDirection = Direction.DOWN;
} else {
nextDirection = Direction.UP;
}
}
return true;