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

View File

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