feat(scene/main_menu): handle key down holding

This commit is contained in:
2025-10-01 11:07:02 +07:00
parent b1cbef4dd8
commit 65880853ac

View File

@ -3,6 +3,8 @@ package org.vibecoders.moongazer.scenes;
import static org.vibecoders.moongazer.Constants.*; import static org.vibecoders.moongazer.Constants.*;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import org.vibecoders.moongazer.Game; import org.vibecoders.moongazer.Game;
import org.vibecoders.moongazer.State; import org.vibecoders.moongazer.State;
@ -24,6 +26,7 @@ public class MainMenu extends Scene {
private FileHandle videoFileHandle; private FileHandle videoFileHandle;
private Texture titleTexture; private Texture titleTexture;
private UITextButton[] buttons; private UITextButton[] buttons;
private HashMap<Integer, Long> currentKeyDown = new HashMap<>();
private float titleY, titleX, titleWidth, titleHeight; private float titleY, titleX, titleWidth, titleHeight;
private boolean videoPrepared = false; private boolean videoPrepared = false;
private int currentChoice = -1; private int currentChoice = -1;
@ -129,6 +132,40 @@ public class MainMenu extends Scene {
root.addListener(new InputListener() { root.addListener(new InputListener() {
@Override @Override
public boolean keyDown(InputEvent event, int keycode) { public boolean keyDown(InputEvent event, int keycode) {
sKeyDown(event, keycode);
currentKeyDown.put(keycode, System.currentTimeMillis());
return true;
}
});
root.addListener(new InputListener() {
@Override
public boolean keyUp(InputEvent event, int keycode) {
currentKeyDown.remove(keycode);
return true;
}
});
}
private void startVideoOnce() {
if (videoPlayer == null || videoPrepared)
return;
if (videoFileHandle == null || !videoFileHandle.exists())
return;
if (game.transition == null && game.state != State.MAIN_MENU)
return;
videoPlayer.setLooping(true);
videoPlayer.play();
videoPrepared = true;
}
/**
* The actual key down handler. ((s)cene key down)
*
* @param event not used for now, can be null
* @param keycode the keycode of the pressed key
*/
public void sKeyDown(InputEvent event, int keycode) {
log.trace("Key pressed: {}", keycode); log.trace("Key pressed: {}", keycode);
switch (keycode) { switch (keycode) {
case Input.Keys.UP: case Input.Keys.UP:
@ -156,25 +193,19 @@ public class MainMenu extends Scene {
} }
} }
} }
return true;
}
});
}
private void startVideoOnce() {
if (videoPlayer == null || videoPrepared)
return;
if (videoFileHandle == null || !videoFileHandle.exists())
return;
if (game.transition == null && game.state != State.MAIN_MENU)
return;
videoPlayer.setLooping(true);
videoPlayer.play();
videoPrepared = true;
} }
@Override @Override
public void render(SpriteBatch batch) { public void render(SpriteBatch batch) {
// SDL way of handling key input XD
for (Map.Entry<Integer, Long> entry : currentKeyDown.entrySet()) {
Integer keyCode = entry.getKey();
Long timeStamp = entry.getValue();
if (System.currentTimeMillis() - timeStamp > 100) {
sKeyDown(null, keyCode);
currentKeyDown.put(keyCode, System.currentTimeMillis());
}
}
startVideoOnce(); startVideoOnce();
videoPlayer.update(); videoPlayer.update();
Texture videoTexture = videoPlayer.getTexture(); Texture videoTexture = videoPlayer.getTexture();