Pages computed layout from SDL_GetWindowSize() (real window coordinates) while SDL_SetRenderLogicalPresentation() independently remapped every draw call into a 1920x1080 space. The two coordinate systems only agreed when the window happened to be exactly 1920x1080; at any other size, layout coordinates were transformed a second time, so content shrank/grew quadratically toward the top-left on resize or on non-1080p monitors. Commenting the call out masked the bug by making rendering 1:1 with the window, but then nothing scaled: fonts and margins stayed fixed-pixel while backgrounds cover-fit, and HiDPI displays drew at point resolution and relied on blurry compositor upsampling. Restore SDL_SetRenderLogicalPresentation(1920x1080, LETTERBOX) as the single source of scaling, and route all twelve layout sites through a new radiantwave::getLayoutSize() helper (src/logical_size.h) that returns the renderer's logical size, so pages always lay out against the design resolution regardless of window or monitor geometry. Add SDL_WINDOW_HIGH_PIXEL_DENSITY so HiDPI displays render at native pixel density instead of being upscaled by the OS. Cached per-page textures become resolution-independent since layout no longer depends on window size.
252 lines
7.9 KiB
C++
252 lines
7.9 KiB
C++
#include "license_key_page.h"
|
|
#include "app_state.h"
|
|
#include "db/db_keys.h"
|
|
#include "network/license_service.h"
|
|
#include "provisioning.h"
|
|
#include "validation.h"
|
|
#include "logical_size.h"
|
|
#include <SDL3/SDL.h>
|
|
|
|
void LicenseKeyPage::onPush(AppState& state)
|
|
{
|
|
state_ = &state;
|
|
TTF_Init();
|
|
createFonts();
|
|
|
|
storedValue_ = state.db.getKV(radiantwave::db::KV_LICENSE_KEY);
|
|
raw_ = radiantwave::validation::sanitizeLicenseKey(storedValue_);
|
|
statusMessage_ = "Enter the 16-character key (XXXX-XXXX-XXXX-XXXX), then press ENTER";
|
|
|
|
if (state.window) {
|
|
SDL_StartTextInput(state.window);
|
|
}
|
|
}
|
|
|
|
void LicenseKeyPage::onResume(AppState& state)
|
|
{
|
|
state_ = &state;
|
|
createFonts();
|
|
storedValue_ = state.db.getKV(radiantwave::db::KV_LICENSE_KEY);
|
|
raw_ = radiantwave::validation::sanitizeLicenseKey(storedValue_);
|
|
|
|
if (state.window) {
|
|
SDL_StartTextInput(state.window);
|
|
}
|
|
}
|
|
|
|
void LicenseKeyPage::onPause(AppState& state)
|
|
{
|
|
state.keybinds.unbindAll();
|
|
if (state.window) {
|
|
SDL_StopTextInput(state.window);
|
|
}
|
|
destroyFonts();
|
|
destroyTextures();
|
|
}
|
|
|
|
void LicenseKeyPage::onPop(AppState& state)
|
|
{
|
|
if (state.window) {
|
|
SDL_StopTextInput(state.window);
|
|
}
|
|
destroyFonts();
|
|
destroyTextures();
|
|
TTF_Quit();
|
|
}
|
|
|
|
void LicenseKeyPage::createFonts()
|
|
{
|
|
destroyFonts();
|
|
fontTitle_ = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 52);
|
|
fontHeader_ = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 28);
|
|
fontBody_ = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 32);
|
|
fontSmall_ = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 22);
|
|
}
|
|
|
|
void LicenseKeyPage::destroyFonts()
|
|
{
|
|
if (fontTitle_) { TTF_CloseFont(fontTitle_); fontTitle_ = nullptr; }
|
|
if (fontHeader_) { TTF_CloseFont(fontHeader_); fontHeader_ = nullptr; }
|
|
if (fontBody_) { TTF_CloseFont(fontBody_); fontBody_ = nullptr; }
|
|
if (fontSmall_) { TTF_CloseFont(fontSmall_); fontSmall_ = nullptr; }
|
|
}
|
|
|
|
void LicenseKeyPage::destroyTextures()
|
|
{
|
|
titleText_.destroy();
|
|
storedText_.destroy();
|
|
valueText_.destroy();
|
|
statusText_.destroy();
|
|
hintText_.destroy();
|
|
}
|
|
|
|
void LicenseKeyPage::submit()
|
|
{
|
|
if (raw_.size() != 16) {
|
|
statusMessage_ = "License key must be 16 characters (XXXX-XXXX-XXXX-XXXX)";
|
|
return;
|
|
}
|
|
|
|
std::string formatted = radiantwave::validation::formatLicenseKey(raw_);
|
|
if (!state_->db.setKV(radiantwave::db::KV_LICENSE_KEY, formatted)) {
|
|
statusMessage_ = "Failed to save license key";
|
|
return;
|
|
}
|
|
storedValue_ = formatted;
|
|
|
|
// Both the email address and the license key are required to validate.
|
|
// When the email is missing the key is saved but the check is skipped.
|
|
std::string errorMessage;
|
|
const radiantwave::license::Result result = radiantwave::license::validateStoredLicense(
|
|
state_->db, &errorMessage);
|
|
|
|
switch (result) {
|
|
case radiantwave::license::Result::Ok:
|
|
statusMessage_ = "License validated";
|
|
break;
|
|
case radiantwave::license::Result::NotConfigured:
|
|
statusMessage_ = "Saved - add your email address to validate the license";
|
|
break;
|
|
case radiantwave::license::Result::Invalid:
|
|
statusMessage_ = "License check failed: " +
|
|
(errorMessage.empty() ? "invalid license" : errorMessage);
|
|
break;
|
|
case radiantwave::license::Result::NetworkError:
|
|
statusMessage_ = "License saved, but the server could not be reached to validate";
|
|
break;
|
|
}
|
|
returnTimer_ = 1.0f;
|
|
}
|
|
|
|
void LicenseKeyPage::handleEvent(const SDL_Event& event)
|
|
{
|
|
if (!state_) return;
|
|
|
|
if (event.type == SDL_EVENT_TEXT_INPUT) {
|
|
std::string addition = radiantwave::validation::sanitizeLicenseKey(event.text.text);
|
|
if (raw_.size() + addition.size() > 16) {
|
|
addition.resize(16 - raw_.size());
|
|
}
|
|
raw_ += addition;
|
|
return;
|
|
}
|
|
|
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
|
SDL_Keycode key = event.key.key;
|
|
|
|
if (key == SDLK_BACKSPACE) {
|
|
if (!raw_.empty()) {
|
|
raw_.pop_back();
|
|
}
|
|
} else if (key == SDLK_RETURN || key == SDLK_KP_ENTER) {
|
|
submit();
|
|
} else if (key == SDLK_ESCAPE) {
|
|
exitPage(*state_);
|
|
}
|
|
}
|
|
}
|
|
|
|
void LicenseKeyPage::update(float dt)
|
|
{
|
|
if (returnTimer_ > 0.0f) {
|
|
returnTimer_ -= dt;
|
|
if (returnTimer_ <= 0.0f) {
|
|
returnTimer_ = -1.0f;
|
|
if (state_) {
|
|
exitPage(*state_);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// In the First-Run walkthrough a finished step moves on to the next missing
|
|
// prerequisite; otherwise the page returns to whatever pushed it (Settings).
|
|
void LicenseKeyPage::exitPage(AppState& state)
|
|
{
|
|
if (state.provisioningMode) {
|
|
radiantwave::provisioning::advance(state);
|
|
} else {
|
|
state.pages.pop(state);
|
|
}
|
|
}
|
|
|
|
void LicenseKeyPage::render(SDL_Renderer* renderer)
|
|
{
|
|
int winW, winH;
|
|
radiantwave::getLayoutSize(renderer, &winW, &winH);
|
|
float fw = static_cast<float>(winW);
|
|
float fh = static_cast<float>(winH);
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
|
SDL_SetRenderDrawColor(renderer, 20, 20, 30, 255);
|
|
SDL_FRect bgRect{0, 0, fw, fh};
|
|
SDL_RenderFillRect(renderer, &bgRect);
|
|
|
|
if (!fontTitle_ || !fontHeader_ || !fontBody_ || !fontSmall_) return;
|
|
|
|
SDL_Color white = {255, 255, 255, 255};
|
|
SDL_Color yellow = {255, 255, 0, 255};
|
|
SDL_Color red = {255, 80, 80, 255};
|
|
SDL_Color cyan = {100, 220, 255, 255};
|
|
SDL_Color gray = {180, 180, 180, 255};
|
|
|
|
float currentY = 30.0f;
|
|
|
|
// Input box (centered on screen); messages align to its left edge
|
|
float boxW = 700.0f;
|
|
float boxH = 70.0f;
|
|
float boxX = (fw - boxW) / 2.0f;
|
|
|
|
titleText_.ensure(renderer, fontTitle_, "License Key", white);
|
|
if (titleText_.texture) {
|
|
float x = (fw - titleText_.width) / 2.0f;
|
|
SDL_FRect dst{x, currentY, titleText_.width, titleText_.height};
|
|
SDL_RenderTexture(renderer, titleText_.texture, nullptr, &dst);
|
|
currentY += titleText_.height + 30.0f;
|
|
}
|
|
|
|
std::string storedStr = storedValue_.empty() ? "Not set" : storedValue_;
|
|
storedText_.ensure(renderer, fontHeader_, "Saved: " + storedStr,
|
|
storedValue_.empty() ? gray : yellow);
|
|
if (storedText_.texture) {
|
|
SDL_FRect dst{boxX, currentY, storedText_.width, storedText_.height};
|
|
SDL_RenderTexture(renderer, storedText_.texture, nullptr, &dst);
|
|
}
|
|
currentY += 50.0f;
|
|
|
|
SDL_SetRenderDrawColor(renderer, 35, 40, 60, 255);
|
|
SDL_FRect boxRect{boxX, currentY, boxW, boxH};
|
|
SDL_RenderFillRect(renderer, &boxRect);
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
SDL_RenderRect(renderer, &boxRect);
|
|
|
|
std::string display = radiantwave::validation::formatLicenseKey(raw_);
|
|
if (!raw_.empty() && raw_.size() < 16) {
|
|
display += "_";
|
|
}
|
|
valueText_.ensure(renderer, fontBody_, display, cyan);
|
|
if (valueText_.texture) {
|
|
SDL_FRect dst{boxX + 20.0f, currentY + (boxH - valueText_.height) / 2.0f, valueText_.width, valueText_.height};
|
|
SDL_RenderTexture(renderer, valueText_.texture, nullptr, &dst);
|
|
}
|
|
currentY += boxH + 30.0f;
|
|
|
|
// Status message (left-aligned with input box)
|
|
if (!statusMessage_.empty()) {
|
|
statusText_.ensure(renderer, fontHeader_, statusMessage_, gray);
|
|
if (statusText_.texture) {
|
|
SDL_FRect dst{boxX, currentY, statusText_.width, statusText_.height};
|
|
SDL_RenderTexture(renderer, statusText_.texture, nullptr, &dst);
|
|
}
|
|
currentY += 50.0f;
|
|
}
|
|
|
|
hintText_.ensure(renderer, fontSmall_,
|
|
"[Type] Enter key (dashes added automatically) [ENTER] Save [BACKSPACE] Delete [ESC] Back", gray);
|
|
if (hintText_.texture) {
|
|
float x = (fw - hintText_.width) / 2.0f;
|
|
SDL_FRect dst{x, fh - 40.0f, hintText_.width, hintText_.height};
|
|
SDL_RenderTexture(renderer, hintText_.texture, nullptr, &dst);
|
|
}
|
|
}
|