varies coses i detallets

This commit is contained in:
2026-04-16 18:46:58 +02:00
parent fe41919e1e
commit 6394e9afab
17 changed files with 513 additions and 156 deletions

View File

@@ -1,10 +1,13 @@
#include "core/input/gamepad.hpp"
#include <cstdio>
#include <string>
#include "core/input/key_config.hpp"
#include "core/jail/jinput.hpp"
#include "core/locale/locale.hpp"
#include "core/rendering/menu.hpp"
#include "game/options.hpp"
#include "core/rendering/overlay.hpp"
namespace Gamepad {
@@ -19,11 +22,23 @@ namespace Gamepad {
static bool prev_down_ = false;
static bool prev_left_ = false;
static bool prev_right_ = false;
static bool prev_a_ = false;
static bool prev_b_ = false;
static bool prev_south_ = false;
static bool prev_east_ = false;
static bool prev_west_ = false;
static bool prev_north_ = false;
static bool prev_start_ = false;
static bool prev_back_ = false;
static void notify(const char* name, const char* status_key) {
std::string msg = (name && *name) ? name : "Gamepad";
msg += ' ';
msg += Locale::get(status_key);
Overlay::showNotification(msg.c_str(), 2.5F);
}
static void notifyConnected(const char* name) { notify(name, "notifications.gamepad_connected"); }
static void notifyDisconnected(const char* name) { notify(name, "notifications.gamepad_disconnected"); }
static void openFirstGamepad() {
int count = 0;
SDL_JoystickID* ids = SDL_GetGamepads(&count);
@@ -70,12 +85,16 @@ namespace Gamepad {
pad_ = SDL_OpenGamepad(event.gdevice.which);
if (pad_) {
pad_id_ = event.gdevice.which;
SDL_Log("Gamepad connectat: %s", SDL_GetGamepadName(pad_));
const char* name = SDL_GetGamepadName(pad_);
SDL_Log("Gamepad connectat: %s", name ? name : "");
notifyConnected(name);
}
}
} else if (event.type == SDL_EVENT_GAMEPAD_REMOVED) {
if (pad_ && event.gdevice.which == pad_id_) {
SDL_Log("Gamepad desconnectat");
const char* name = SDL_GetGamepadName(pad_);
std::string saved_name = name ? name : "";
SDL_Log("Gamepad desconnectat: %s", saved_name.c_str());
SDL_CloseGamepad(pad_);
pad_ = nullptr;
pad_id_ = 0;
@@ -84,6 +103,7 @@ namespace Gamepad {
JI_SetVirtualKey(SDL_SCANCODE_DOWN, JI_VSRC_GAMEPAD, false);
JI_SetVirtualKey(SDL_SCANCODE_LEFT, JI_VSRC_GAMEPAD, false);
JI_SetVirtualKey(SDL_SCANCODE_RIGHT, JI_VSRC_GAMEPAD, false);
notifyDisconnected(saved_name.c_str());
}
}
}
@@ -125,16 +145,18 @@ namespace Gamepad {
bool lt = dlt || slt;
bool rt = drt || srt;
// Botons
bool a = SDL_GetGamepadButton(pad_, SDL_GAMEPAD_BUTTON_SOUTH); // A/Cross
bool b = SDL_GetGamepadButton(pad_, SDL_GAMEPAD_BUTTON_EAST); // B/Circle
// 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);
// Start → obre/tanca menú (flanc)
if (start && !prev_start_) pushKey(Options::keys_gui.menu_toggle);
// Back → ESC (flanc)
if (back && !prev_back_) pushKey(SDL_SCANCODE_ESCAPE);
// 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"));
if (Menu::isOpen()) {
// Navegació del menú per flanc
@@ -142,8 +164,9 @@ namespace Gamepad {
if (dn && !prev_down_) pushKey(SDL_SCANCODE_DOWN);
if (lt && !prev_left_) pushKey(SDL_SCANCODE_LEFT);
if (rt && !prev_right_) pushKey(SDL_SCANCODE_RIGHT);
if (a && !prev_a_) pushKey(SDL_SCANCODE_RETURN);
if (b && !prev_b_) pushKey(SDL_SCANCODE_BACKSPACE);
// 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_VSRC_GAMEPAD, false);
@@ -156,16 +179,21 @@ namespace Gamepad {
JI_SetVirtualKey(SDL_SCANCODE_DOWN, JI_VSRC_GAMEPAD, dn);
JI_SetVirtualKey(SDL_SCANCODE_LEFT, JI_VSRC_GAMEPAD, lt);
JI_SetVirtualKey(SDL_SCANCODE_RIGHT, JI_VSRC_GAMEPAD, rt);
// Botó A al joc: emet Enter per avançar seqüències (JI_AnyKey)
if (a && !prev_a_) pushKey(SDL_SCANCODE_RETURN);
// 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);
}
}
prev_up_ = up;
prev_down_ = dn;
prev_left_ = lt;
prev_right_ = rt;
prev_a_ = a;
prev_b_ = b;
prev_south_ = south;
prev_east_ = east;
prev_west_ = west;
prev_north_ = north;
prev_start_ = start;
prev_back_ = back;
}