75 lines
2.7 KiB
C++
75 lines
2.7 KiB
C++
#include "ui/MenuLayout.h"
|
|
#include "ui/UIConstants.h"
|
|
#include <cmath>
|
|
#include <array>
|
|
|
|
namespace ui {
|
|
|
|
std::array<SDL_FRect, 5> computeMenuButtonRects(const MenuLayoutParams& p) {
|
|
const float LOGICAL_W = static_cast<float>(p.logicalW);
|
|
const float LOGICAL_H = static_cast<float>(p.logicalH);
|
|
float contentOffsetX = (p.winW - LOGICAL_W * p.logicalScale) * 0.5f / p.logicalScale;
|
|
float contentOffsetY = (p.winH - LOGICAL_H * p.logicalScale) * 0.5f / p.logicalScale;
|
|
|
|
// Cockpit HUD layout (matches main_screen art):
|
|
// - A big centered PLAY button
|
|
// - A second row of 4 smaller buttons: LEVEL / OPTIONS / HELP / EXIT
|
|
const float marginX = std::max(24.0f, LOGICAL_W * 0.03f);
|
|
const float marginBottom = std::max(26.0f, LOGICAL_H * 0.03f);
|
|
const float availableW = std::max(120.0f, LOGICAL_W - marginX * 2.0f);
|
|
|
|
float playW = std::min(230.0f, availableW * 0.27f);
|
|
float playH = 35.0f;
|
|
float smallW = std::min(220.0f, availableW * 0.23f);
|
|
float smallH = 34.0f;
|
|
float smallSpacing = 28.0f;
|
|
|
|
// Scale down for narrow windows so nothing goes offscreen.
|
|
float smallTotal = smallW * 4.0f + smallSpacing * 3.0f;
|
|
if (smallTotal > availableW) {
|
|
float s = availableW / smallTotal;
|
|
smallW *= s;
|
|
smallH *= s;
|
|
smallSpacing *= s;
|
|
playW = std::min(playW, availableW);
|
|
playH *= std::max(0.75f, s);
|
|
}
|
|
|
|
float centerX = LOGICAL_W * 0.5f + contentOffsetX;
|
|
float bottomY = LOGICAL_H + contentOffsetY - marginBottom;
|
|
float smallCY = bottomY - smallH * 0.5f;
|
|
float playCY = smallCY - smallH * 0.5f - 16.0f - playH * 0.5f;
|
|
|
|
std::array<SDL_FRect, MENU_BTN_COUNT> rects{};
|
|
rects[0] = SDL_FRect{ centerX - playW * 0.5f, playCY - playH * 0.5f, playW, playH };
|
|
|
|
float rowW = smallW * 4.0f + smallSpacing * 3.0f;
|
|
float left = centerX - rowW * 0.5f;
|
|
float minLeft = contentOffsetX + marginX;
|
|
float maxRight = contentOffsetX + LOGICAL_W - marginX;
|
|
if (left < minLeft) left = minLeft;
|
|
if (left + rowW > maxRight) left = std::max(minLeft, maxRight - rowW);
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
float x = left + i * (smallW + smallSpacing);
|
|
rects[i + 1] = SDL_FRect{ x, smallCY - smallH * 0.5f, smallW, smallH };
|
|
}
|
|
return rects;
|
|
}
|
|
|
|
int hitTestMenuButtons(const MenuLayoutParams& p, float localX, float localY) {
|
|
auto rects = computeMenuButtonRects(p);
|
|
for (int i = 0; i < MENU_BTN_COUNT; ++i) {
|
|
const auto &r = rects[i];
|
|
if (localX >= r.x && localX <= r.x + r.w && localY >= r.y && localY <= r.y + r.h)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
SDL_FRect settingsButtonRect(const MenuLayoutParams& p) {
|
|
return SDL_FRect{SETTINGS_BTN_X, SETTINGS_BTN_Y, SETTINGS_BTN_W, SETTINGS_BTN_H};
|
|
}
|
|
|
|
} // namespace ui
|