refactor: extreure helpers per reduir complexitat cognitiva (tidy net)

This commit is contained in:
2026-05-16 16:13:57 +02:00
parent b984e6041e
commit e1bc1b597f
31 changed files with 1145 additions and 1332 deletions
+73 -84
View File
@@ -195,97 +195,86 @@ namespace Gamepad {
SDL_PushEvent(&e);
}
// Estat agregat d'un frame: D-pad i stick combinats, més botons frontals.
struct PadState {
bool up;
bool down;
bool left;
bool right;
bool south;
bool east;
bool west;
bool north;
bool start;
bool back;
};
static auto readPadState() -> PadState {
const Sint16 LX = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX);
const Sint16 LY = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY);
return PadState{
.up = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP) || LY < -STICK_DEADZONE,
.down = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN) || LY > STICK_DEADZONE,
.left = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT) || LX < -STICK_DEADZONE,
.right = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT) || LX > STICK_DEADZONE,
.south = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_SOUTH),
.east = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_EAST),
.west = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_WEST),
.north = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_NORTH),
.start = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_START),
.back = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_BACK),
};
}
static void handleMenuNavigation(const PadState& s) {
if (s.up && !prev_up) { pushKey(SDL_SCANCODE_UP); }
if (s.down && !prev_down) { pushKey(SDL_SCANCODE_DOWN); }
if (s.left && !prev_left) { pushKey(SDL_SCANCODE_LEFT); }
if (s.right && !prev_right) { pushKey(SDL_SCANCODE_RIGHT); }
if (s.east && !prev_east) { pushKey(SDL_SCANCODE_RETURN); }
if (s.south && !prev_south) { pushKey(SDL_SCANCODE_BACKSPACE); }
// Mentre el menú està obert, el joc no ha de rebre moviment.
Ji::setVirtualKey(SDL_SCANCODE_UP, Ji::VirtualSource::GAMEPAD, false);
Ji::setVirtualKey(SDL_SCANCODE_DOWN, Ji::VirtualSource::GAMEPAD, false);
Ji::setVirtualKey(SDL_SCANCODE_LEFT, Ji::VirtualSource::GAMEPAD, false);
Ji::setVirtualKey(SDL_SCANCODE_RIGHT, Ji::VirtualSource::GAMEPAD, false);
}
static void handleGameInput(const PadState& s) {
Ji::setVirtualKey(SDL_SCANCODE_UP, Ji::VirtualSource::GAMEPAD, s.up);
Ji::setVirtualKey(SDL_SCANCODE_DOWN, Ji::VirtualSource::GAMEPAD, s.down);
Ji::setVirtualKey(SDL_SCANCODE_LEFT, Ji::VirtualSource::GAMEPAD, s.left);
Ji::setVirtualKey(SDL_SCANCODE_RIGHT, Ji::VirtualSource::GAMEPAD, s.right);
const bool ANY_FRONT_EDGE = (s.south && !prev_south) || (s.east && !prev_east) ||
(s.west && !prev_west) || (s.north && !prev_north);
if (ANY_FRONT_EDGE) {
pushKey(SDL_SCANCODE_RETURN);
}
}
void update() {
if (pad == nullptr) {
return;
}
// D-pad
bool dup = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP);
bool ddn = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
bool dlt = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
bool drt = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
// Stick esquerre amb dead-zone
Sint16 lx = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX);
Sint16 ly = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY);
bool sup = ly < -STICK_DEADZONE;
bool sdn = ly > STICK_DEADZONE;
bool slt = lx < -STICK_DEADZONE;
bool srt = lx > STICK_DEADZONE;
bool up = dup || sup;
bool dn = ddn || sdn;
bool lt = dlt || slt;
bool rt = drt || srt;
// Botons frontals (layout SDL: SOUTH=A/Cross, EAST=B/Circle, WEST=X/Square, NORTH=Y/Triangle)
bool south = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_SOUTH);
bool east = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_EAST);
bool west = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_WEST);
bool north = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_NORTH);
bool start = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_START);
bool back = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_BACK);
// Select (Back) → obre/tanca menú de servei (flanc)
if (back && !prev_back) {
pushKey(KeyConfig::scancode("menu_toggle"));
}
// Start → pausa (flanc)
if (start && !prev_start) {
pushKey(KeyConfig::scancode("pause_toggle"));
}
const PadState S = readPadState();
// Flancs globals: Select i Start sempre operen.
if (S.back && !prev_back) { pushKey(KeyConfig::scancode("menu_toggle")); }
if (S.start && !prev_start) { pushKey(KeyConfig::scancode("pause_toggle")); }
if (Menu::isOpen()) {
// Navegació del menú per flanc
if (up && !prev_up) {
pushKey(SDL_SCANCODE_UP);
}
if (dn && !prev_down) {
pushKey(SDL_SCANCODE_DOWN);
}
if (lt && !prev_left) {
pushKey(SDL_SCANCODE_LEFT);
}
if (rt && !prev_right) {
pushKey(SDL_SCANCODE_RIGHT);
}
// EAST accepta, SOUTH cancela / endarrere
if (east && !prev_east) {
pushKey(SDL_SCANCODE_RETURN);
}
if (south && !prev_south) {
pushKey(SDL_SCANCODE_BACKSPACE);
}
// Assegura que el joc no rep tecles de moviment mentre el menú està obert
Ji::setVirtualKey(SDL_SCANCODE_UP, Ji::VirtualSource::GAMEPAD, false);
Ji::setVirtualKey(SDL_SCANCODE_DOWN, Ji::VirtualSource::GAMEPAD, false);
Ji::setVirtualKey(SDL_SCANCODE_LEFT, Ji::VirtualSource::GAMEPAD, false);
Ji::setVirtualKey(SDL_SCANCODE_RIGHT, Ji::VirtualSource::GAMEPAD, false);
handleMenuNavigation(S);
} else {
// Moviment al joc — level-triggered (polling)
Ji::setVirtualKey(SDL_SCANCODE_UP, Ji::VirtualSource::GAMEPAD, up);
Ji::setVirtualKey(SDL_SCANCODE_DOWN, Ji::VirtualSource::GAMEPAD, dn);
Ji::setVirtualKey(SDL_SCANCODE_LEFT, Ji::VirtualSource::GAMEPAD, lt);
Ji::setVirtualKey(SDL_SCANCODE_RIGHT, Ji::VirtualSource::GAMEPAD, rt);
// Qualsevol dels 4 botons frontals avança escenes (Ji::anyKey via Enter sintètic)
if ((south && !prev_south) || (east && !prev_east) ||
(west && !prev_west) || (north && !prev_north)) {
pushKey(SDL_SCANCODE_RETURN);
}
handleGameInput(S);
}
prev_up = up;
prev_down = dn;
prev_left = lt;
prev_right = rt;
prev_south = south;
prev_east = east;
prev_west = west;
prev_north = north;
prev_start = start;
prev_back = back;
prev_up = S.up;
prev_down = S.down;
prev_left = S.left;
prev_right = S.right;
prev_south = S.south;
prev_east = S.east;
prev_west = S.west;
prev_north = S.north;
prev_start = S.start;
prev_back = S.back;
}
} // namespace Gamepad
+32 -91
View File
@@ -1,6 +1,7 @@
#include "core/input/global_inputs.hpp"
#include <cstdio>
#include <functional>
#include <string>
#include "core/input/key_config.hpp"
@@ -23,131 +24,71 @@ namespace GlobalInputs {
static bool texture_filter_prev = false;
static bool render_info_prev = false;
// Patró comú: lectura amb detecció de flanc + acumulació al flag "consumed".
// `on_press` només s'executa al flanc puja; `prev` es manté actualitzat.
static auto edgeTrigger(const char* key_id, bool& prev, const std::function<void()>& on_press) -> bool {
const bool PRESSED = Ji::keyPressed(KeyConfig::scancode(key_id));
if (PRESSED && !prev) {
on_press();
}
prev = PRESSED;
return PRESSED;
}
auto handle() -> bool {
bool consumed = false;
// F1 — Reduir zoom
bool dec_zoom = Ji::keyPressed(KeyConfig::scancode("dec_zoom"));
if (dec_zoom && !dec_zoom_prev) {
consumed |= edgeTrigger("dec_zoom", dec_zoom_prev, [] {
Screen::get()->decZoom();
char msg[32];
snprintf(msg, sizeof(msg), Locale::get("notifications.zoom_fmt"), Screen::get()->getZoom());
Overlay::showNotification(msg);
}
if (dec_zoom) {
consumed = true;
}
dec_zoom_prev = dec_zoom;
// F2 — Augmentar zoom
bool inc_zoom = Ji::keyPressed(KeyConfig::scancode("inc_zoom"));
if (inc_zoom && !inc_zoom_prev) {
});
consumed |= edgeTrigger("inc_zoom", inc_zoom_prev, [] {
Screen::get()->incZoom();
char msg[32];
snprintf(msg, sizeof(msg), Locale::get("notifications.zoom_fmt"), Screen::get()->getZoom());
Overlay::showNotification(msg);
}
if (inc_zoom) {
consumed = true;
}
inc_zoom_prev = inc_zoom;
// F3 — Toggle pantalla completa
bool fullscreen = Ji::keyPressed(KeyConfig::scancode("fullscreen"));
if (fullscreen && !fullscreen_prev) {
});
consumed |= edgeTrigger("fullscreen", fullscreen_prev, [] {
Screen::get()->toggleFullscreen();
Overlay::showNotification(Screen::get()->isFullscreen() ? Locale::get("notifications.fullscreen") : Locale::get("notifications.windowed"));
}
if (fullscreen) {
consumed = true;
}
fullscreen_prev = fullscreen;
// F4 — Toggle shaders
bool shader = Ji::keyPressed(KeyConfig::scancode("toggle_shader"));
if (shader && !shader_prev) {
});
consumed |= edgeTrigger("toggle_shader", shader_prev, [] {
Screen::get()->toggleShaders();
Overlay::showNotification(Options::video.shader_enabled ? Locale::get("notifications.shader_on") : Locale::get("notifications.shader_off"));
}
if (shader) {
consumed = true;
}
shader_prev = shader;
// F5 — Toggle aspect ratio 4:3
bool aspect = Ji::keyPressed(KeyConfig::scancode("toggle_aspect_ratio"));
if (aspect && !aspect_prev) {
});
consumed |= edgeTrigger("toggle_aspect_ratio", aspect_prev, [] {
Screen::get()->toggleAspectRatio();
Overlay::showNotification(Options::video.aspect_ratio_4_3 ? Locale::get("notifications.aspect_43") : Locale::get("notifications.aspect_square"));
}
if (aspect) {
consumed = true;
}
aspect_prev = aspect;
// F6 — Toggle supersampling
bool ss = Ji::keyPressed(KeyConfig::scancode("toggle_supersampling"));
if (ss && !ss_prev) {
});
consumed |= edgeTrigger("toggle_supersampling", ss_prev, [] {
if (Screen::get()->toggleSupersampling()) {
Overlay::showNotification(Options::video.supersampling ? Locale::get("notifications.ss_on") : Locale::get("notifications.ss_off"));
}
}
if (ss) {
consumed = true;
}
ss_prev = ss;
// F7 — Canviar tipus de shader (PostFX ↔ CrtPi)
bool next_shader = Ji::keyPressed(KeyConfig::scancode("next_shader"));
if (next_shader && !next_shader_prev) {
});
consumed |= edgeTrigger("next_shader", next_shader_prev, [] {
if (Screen::get()->nextShaderType()) {
char msg[64];
snprintf(msg, sizeof(msg), "%s: %s", Screen::get()->getActiveShaderName(), Screen::get()->getCurrentPresetName());
Overlay::showNotification(msg);
}
}
if (next_shader) {
consumed = true;
}
next_shader_prev = next_shader;
// F8 — Pròxim preset del shader actiu
bool next_preset = Ji::keyPressed(KeyConfig::scancode("next_shader_preset"));
if (next_preset && !next_preset_prev) {
});
consumed |= edgeTrigger("next_shader_preset", next_preset_prev, [] {
if (Screen::get()->nextPreset()) {
char msg[64];
snprintf(msg, sizeof(msg), Locale::get("notifications.preset_fmt"), Screen::get()->getCurrentPresetName());
Overlay::showNotification(msg);
}
}
if (next_preset) {
consumed = true;
}
next_preset_prev = next_preset;
// F9 — Cicla filtre de textura (NEAREST ↔ LINEAR), sempre aplicat
bool texture_filter = Ji::keyPressed(KeyConfig::scancode("cycle_texture_filter"));
if (texture_filter && !texture_filter_prev) {
});
consumed |= edgeTrigger("cycle_texture_filter", texture_filter_prev, [] {
Screen::get()->cycleTextureFilter(+1);
Overlay::showNotification(Options::video.texture_filter == Options::TextureFilter::LINEAR
? Locale::get("notifications.filter_linear")
: Locale::get("notifications.filter_nearest"));
}
if (texture_filter) {
consumed = true;
}
texture_filter_prev = texture_filter;
// F10 — Toggle render info (FPS, driver, shader)
bool render_info = Ji::keyPressed(KeyConfig::scancode("toggle_render_info"));
if (render_info && !render_info_prev) {
});
consumed |= edgeTrigger("toggle_render_info", render_info_prev, [] {
Overlay::toggleRenderInfo();
}
if (render_info) {
consumed = true;
}
render_info_prev = render_info;
});
return consumed;
}