minor fix

This commit is contained in:
2025-11-22 10:59:47 +01:00
parent ec2bb1bb1e
commit 0ffd801743
8 changed files with 105 additions and 32 deletions

View File

@ -2,6 +2,7 @@
#include "persistence/Scores.h"
#include "graphics/Font.h"
#include "../core/GlobalState.h"
#include "../core/state/StateManager.h"
#include "../audio/Audio.h"
#include <SDL3/SDL.h>
#include <cstdio>
@ -27,8 +28,37 @@ void MenuState::onExit() {
}
void MenuState::handleEvent(const SDL_Event& e) {
// Key-specific handling (allow main to handle global keys)
(void)e;
// Keyboard navigation for menu buttons
if (e.type == SDL_EVENT_KEY_DOWN && !e.key.repeat) {
switch (e.key.scancode) {
case SDL_SCANCODE_LEFT:
case SDL_SCANCODE_UP:
selectedButton = 0; // PLAY
break;
case SDL_SCANCODE_RIGHT:
case SDL_SCANCODE_DOWN:
selectedButton = 1; // LEVEL
break;
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_KP_ENTER:
case SDL_SCANCODE_SPACE:
// Activate selected button
if (selectedButton == 0) {
// PLAY button - transition to Playing state
if (ctx.stateManager) {
ctx.stateManager->setState(AppState::Playing);
}
} else {
// LEVEL button - transition to LevelSelector state
if (ctx.stateManager) {
ctx.stateManager->setState(AppState::LevelSelector);
}
}
break;
default:
break;
}
}
}
void MenuState::update(double frameMs) {
@ -175,8 +205,16 @@ void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logi
int startLevel = ctx.startLevelSelection ? *ctx.startLevelSelection : 0;
std::snprintf(levelBtnText, sizeof(levelBtnText), "LEVEL %d", startLevel);
// Draw simple styled buttons (replicating menu_drawMenuButton)
auto drawMenuButtonLocal = [&](SDL_Renderer* r, FontAtlas& font, float cx, float cy, float w, float h, const std::string& label, SDL_Color bg, SDL_Color border){
auto drawMenuButtonLocal = [&](SDL_Renderer* r, FontAtlas& font, float cx, float cy, float w, float h, const std::string& label, SDL_Color bg, SDL_Color border, bool selected){
float x = cx - w/2; float y = cy - h/2;
// If selected, draw a glow effect
if (selected) {
SDL_SetRenderDrawColor(r, 255, 220, 0, 100);
SDL_FRect glow{ x-10, y-10, w+20, h+20 };
SDL_RenderFillRect(r, &glow);
}
SDL_SetRenderDrawColor(r, border.r, border.g, border.b, border.a);
SDL_FRect br{ x-6, y-6, w+12, h+12 }; SDL_RenderFillRect(r, &br);
SDL_SetRenderDrawColor(r, 255,255,255,255); SDL_FRect br2{ x-4, y-4, w+8, h+8 }; SDL_RenderFillRect(r, &br2);
@ -185,10 +223,10 @@ void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logi
font.draw(r, tx+2, ty+2, label, textScale, SDL_Color{0,0,0,180});
font.draw(r, tx, ty, label, textScale, SDL_Color{255,255,255,255});
};
drawMenuButtonLocal(renderer, *ctx.pixelFont, btnX - btnW * 0.6f, btnY, btnW, btnH, std::string("PLAY"), SDL_Color{60,180,80,255}, SDL_Color{30,120,40,255});
drawMenuButtonLocal(renderer, *ctx.pixelFont, btnX - btnW * 0.6f, btnY, btnW, btnH, std::string("PLAY"), SDL_Color{60,180,80,255}, SDL_Color{30,120,40,255}, selectedButton == 0);
{
}
drawMenuButtonLocal(renderer, *ctx.pixelFont, btnX + btnW * 0.6f, btnY, btnW, btnH, std::string(levelBtnText), SDL_Color{40,140,240,255}, SDL_Color{20,100,200,255});
drawMenuButtonLocal(renderer, *ctx.pixelFont, btnX + btnW * 0.6f, btnY, btnW, btnH, std::string(levelBtnText), SDL_Color{40,140,240,255}, SDL_Color{20,100,200,255}, selectedButton == 1);
{
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "MenuState::render after draw LEVEL button\n"); fclose(f); }
}