Pasaeta de linters
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
#pragma once
|
||||
#include <external/json.hpp>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "external/json.hpp"
|
||||
#include "input_types.h" // Solo incluimos los tipos compartidos
|
||||
|
||||
struct GamepadConfig {
|
||||
@@ -12,9 +13,9 @@ struct GamepadConfig {
|
||||
std::string path; // Ruta física del dispositivo
|
||||
std::unordered_map<InputAction, SDL_GamepadButton> bindings; // Asociación acción-botón
|
||||
|
||||
GamepadConfig(const std::string& name, const std::string& path)
|
||||
: name(name),
|
||||
path(path),
|
||||
GamepadConfig(std::string name, std::string path)
|
||||
: name(std::move(name)),
|
||||
path(std::move(path)),
|
||||
bindings{
|
||||
{InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST},
|
||||
{InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH},
|
||||
@@ -33,28 +34,28 @@ using GamepadConfigs = std::vector<GamepadConfig>;
|
||||
class GamepadConfigManager {
|
||||
public:
|
||||
// Escribir vector de GamepadConfig a archivo JSON
|
||||
static bool writeToJson(const GamepadConfigs& configs, const std::string& filename) {
|
||||
static auto writeToJson(const GamepadConfigs& configs, const std::string& filename) -> bool {
|
||||
try {
|
||||
nlohmann::json j;
|
||||
j["gamepads"] = nlohmann::json::array();
|
||||
|
||||
for (const auto& config : configs) {
|
||||
nlohmann::json gamepadJson;
|
||||
gamepadJson["name"] = config.name;
|
||||
gamepadJson["path"] = config.path;
|
||||
gamepadJson["bindings"] = nlohmann::json::object();
|
||||
nlohmann::json gamepad_json;
|
||||
gamepad_json["name"] = config.name;
|
||||
gamepad_json["path"] = config.path;
|
||||
gamepad_json["bindings"] = nlohmann::json::object();
|
||||
|
||||
// Convertir bindings a JSON
|
||||
for (const auto& [action, button] : config.bindings) {
|
||||
auto actionIt = actionToString.find(action);
|
||||
auto buttonIt = buttonToString.find(button);
|
||||
auto action_it = ACTION_TO_STRING.find(action);
|
||||
auto button_it = BUTTON_TO_STRING.find(button);
|
||||
|
||||
if (actionIt != actionToString.end() && buttonIt != buttonToString.end()) {
|
||||
gamepadJson["bindings"][actionIt->second] = buttonIt->second;
|
||||
if (action_it != ACTION_TO_STRING.end() && button_it != BUTTON_TO_STRING.end()) {
|
||||
gamepad_json["bindings"][action_it->second] = button_it->second;
|
||||
}
|
||||
}
|
||||
|
||||
j["gamepads"].push_back(gamepadJson);
|
||||
j["gamepads"].push_back(gamepad_json);
|
||||
}
|
||||
|
||||
// Escribir al archivo
|
||||
@@ -74,7 +75,7 @@ class GamepadConfigManager {
|
||||
}
|
||||
|
||||
// Leer vector de GamepadConfig desde archivo JSON
|
||||
static bool readFromJson(GamepadConfigs& configs, const std::string& filename) {
|
||||
static auto readFromJson(GamepadConfigs& configs, const std::string& filename) -> bool {
|
||||
try {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
@@ -91,25 +92,25 @@ class GamepadConfigManager {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& gamepadJson : j["gamepads"]) {
|
||||
if (!gamepadJson.contains("name") || !gamepadJson.contains("bindings")) {
|
||||
for (const auto& gamepad_json : j["gamepads"]) {
|
||||
if (!gamepad_json.contains("name") || !gamepad_json.contains("bindings")) {
|
||||
continue; // Saltar configuraciones malformadas
|
||||
}
|
||||
|
||||
// Leer el campo path si existe, si no dejarlo vacío
|
||||
std::string path = gamepadJson.contains("path") ? gamepadJson["path"].get<std::string>() : "";
|
||||
GamepadConfig config(gamepadJson["name"], path);
|
||||
std::string path = gamepad_json.contains("path") ? gamepad_json["path"].get<std::string>() : "";
|
||||
GamepadConfig config(gamepad_json["name"], path);
|
||||
|
||||
// Limpiar bindings por defecto para cargar los del archivo
|
||||
config.bindings.clear();
|
||||
|
||||
// Cargar bindings desde JSON
|
||||
for (const auto& [actionStr, buttonStr] : gamepadJson["bindings"].items()) {
|
||||
auto actionIt = stringToAction.find(actionStr);
|
||||
auto buttonIt = stringToButton.find(buttonStr);
|
||||
for (const auto& [actionStr, buttonStr] : gamepad_json["bindings"].items()) {
|
||||
auto action_it = STRING_TO_ACTION.find(actionStr);
|
||||
auto button_it = STRING_TO_BUTTON.find(buttonStr);
|
||||
|
||||
if (actionIt != stringToAction.end() && buttonIt != stringToButton.end()) {
|
||||
config.bindings[actionIt->second] = buttonIt->second;
|
||||
if (action_it != STRING_TO_ACTION.end() && button_it != STRING_TO_BUTTON.end()) {
|
||||
config.bindings[action_it->second] = button_it->second;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +126,7 @@ class GamepadConfigManager {
|
||||
}
|
||||
|
||||
// Método auxiliar para verificar si un archivo existe
|
||||
static bool fileExists(const std::string& filename) {
|
||||
static auto fileExists(const std::string& filename) -> bool {
|
||||
std::ifstream file(filename);
|
||||
return file.good();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user