fixed keybord selection for quit game play

This commit is contained in:
2025-11-22 11:14:55 +01:00
parent 0ffd801743
commit a257c5cd79
7 changed files with 39 additions and 12 deletions

View File

@ -24,6 +24,17 @@ void PlayingState::handleEvent(const SDL_Event& e) {
// We keep short-circuited input here; main still owns mouse UI
if (e.type == SDL_EVENT_KEY_DOWN && !e.key.repeat) {
if (!ctx.game) return;
auto setExitSelection = [&](int value) {
if (ctx.exitPopupSelectedButton) {
*ctx.exitPopupSelectedButton = value;
}
};
auto getExitSelection = [&]() -> int {
return ctx.exitPopupSelectedButton ? *ctx.exitPopupSelectedButton : 1;
};
// Pause toggle (P)
if (e.key.scancode == SDL_SCANCODE_P) {
bool paused = ctx.game->isPaused();
@ -35,20 +46,26 @@ void PlayingState::handleEvent(const SDL_Event& e) {
if (ctx.showExitConfirmPopup && *ctx.showExitConfirmPopup) {
// Navigate between YES (0) and NO (1) buttons
if (e.key.scancode == SDL_SCANCODE_LEFT || e.key.scancode == SDL_SCANCODE_UP) {
exitPopupSelectedButton = 0; // YES
setExitSelection(0);
return;
}
if (e.key.scancode == SDL_SCANCODE_RIGHT || e.key.scancode == SDL_SCANCODE_DOWN) {
exitPopupSelectedButton = 1; // NO
setExitSelection(1);
return;
}
// Activate selected button with Enter or Space
if (e.key.scancode == SDL_SCANCODE_RETURN || e.key.scancode == SDL_SCANCODE_KP_ENTER || e.key.scancode == SDL_SCANCODE_SPACE) {
const bool confirmExit = (getExitSelection() == 0);
*ctx.showExitConfirmPopup = false;
if (exitPopupSelectedButton == 0) {
if (confirmExit) {
// YES - Reset game and return to menu
ctx.game->reset(false);
if (ctx.startLevelSelection) {
ctx.game->reset(*ctx.startLevelSelection);
} else {
ctx.game->reset(0);
}
ctx.game->setPaused(false);
if (ctx.stateManager) ctx.stateManager->setState(AppState::Menu);
} else {
// NO - Just close popup and resume
@ -60,6 +77,7 @@ void PlayingState::handleEvent(const SDL_Event& e) {
if (e.key.scancode == SDL_SCANCODE_ESCAPE) {
*ctx.showExitConfirmPopup = false;
ctx.game->setPaused(false);
setExitSelection(1);
return;
}
// While modal is open, suppress other gameplay keys
@ -71,6 +89,7 @@ void PlayingState::handleEvent(const SDL_Event& e) {
if (ctx.showExitConfirmPopup) {
if (ctx.game) ctx.game->setPaused(true);
*ctx.showExitConfirmPopup = true;
setExitSelection(1); // Default to NO for safety
}
return;
}
@ -78,7 +97,7 @@ void PlayingState::handleEvent(const SDL_Event& e) {
// Tetris controls (only when not paused)
if (!ctx.game->isPaused()) {
// Rotation (still event-based for precise timing)
if (e.key.scancode == SDL_SCANCODE_UP || e.key.scancode == SDL_SCANCODE_W ||
if (e.key.scancode == SDL_SCANCODE_UP || e.key.scancode == SDL_SCANCODE_W ||
e.key.scancode == SDL_SCANCODE_Z) {
ctx.game->rotate(1); // Clockwise rotation
return;
@ -87,7 +106,7 @@ void PlayingState::handleEvent(const SDL_Event& e) {
ctx.game->rotate(-1); // Counter-clockwise rotation
return;
}
// Hard drop (space)
if (e.key.scancode == SDL_SCANCODE_SPACE) {
ctx.game->hardDrop();
@ -95,8 +114,8 @@ void PlayingState::handleEvent(const SDL_Event& e) {
}
}
}
// Note: Left/Right movement and soft drop are now handled by
// Note: Left/Right movement and soft drop are now handled by
// ApplicationManager's update handler for proper DAS/ARR timing
}