options: readability-function-cognitive-complexity
This commit is contained in:
@@ -12,6 +12,11 @@
|
|||||||
#include "lang.h" // Para Code
|
#include "lang.h" // Para Code
|
||||||
#include "utils.h" // Para boolToString, stringToBool, getFileName
|
#include "utils.h" // Para boolToString, stringToBool, getFileName
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <stdexcept> // Para excepciones de std::stoi
|
||||||
|
|
||||||
namespace Options {
|
namespace Options {
|
||||||
// --- Variables globales ---
|
// --- Variables globales ---
|
||||||
WindowOptions window; // Opciones de la ventana
|
WindowOptions window; // Opciones de la ventana
|
||||||
@@ -171,98 +176,103 @@ auto saveToFile() -> bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asigna variables a partir de dos cadenas
|
// Función auxiliar para analizar la configuración del mando y reducir duplicación
|
||||||
|
void parseAndSetController(const std::string &var, const std::string &value) {
|
||||||
|
// Lógica básica de análisis (puede hacerse más robusta)
|
||||||
|
size_t first_dot = var.find('.');
|
||||||
|
size_t second_dot = var.find('.', first_dot + 1);
|
||||||
|
|
||||||
|
int controller_index = std::stoi(var.substr(first_dot + 1, second_dot - first_dot - 1));
|
||||||
|
std::string setting_key = var.substr(second_dot + 1);
|
||||||
|
|
||||||
|
auto& controller = controllers.at(controller_index);
|
||||||
|
|
||||||
|
if (setting_key == "name") {
|
||||||
|
controller.name = value;
|
||||||
|
} else if (setting_key == "player") {
|
||||||
|
controller.player_id = std::clamp(std::stoi(value), 1, 2);
|
||||||
|
} else if (setting_key == "type") {
|
||||||
|
controller.type = static_cast<InputDevice>(std::stoi(value));
|
||||||
|
} else if (setting_key == "button.fire_left") {
|
||||||
|
controller.buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
||||||
|
} else if (setting_key == "button.fire_center") {
|
||||||
|
controller.buttons.at(1) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
||||||
|
} else if (setting_key == "button.fire_right") {
|
||||||
|
controller.buttons.at(2) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
||||||
|
} else if (setting_key == "button.start") {
|
||||||
|
controller.buttons.at(3) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
||||||
|
} else if (setting_key == "button.service") {
|
||||||
|
controller.buttons.at(4) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto set(const std::string &var, const std::string &value) -> bool {
|
auto set(const std::string &var, const std::string &value) -> bool {
|
||||||
// Indicador de éxito en la asignación
|
// Clausula de protección: ignora líneas vacías o comentarios
|
||||||
// Lineas vacias o que empiezan por comentario
|
|
||||||
if (var.empty() || var.starts_with("#")) {
|
if (var.empty() || var.starts_with("#")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opciones de ventana
|
// Un mapa estático asegura que se inicializa solo una vez
|
||||||
if (var == "window.zoom") {
|
static const std::map<std::string, std::function<void(const std::string&)>> settings_map = {
|
||||||
window.zoom = std::stoi(value);
|
// Ventana
|
||||||
}
|
{"window.zoom", [](const auto& val){ window.zoom = std::stoi(val); }},
|
||||||
|
// Vídeo
|
||||||
// Opciones de video
|
{"video.fullscreen", [](const auto& val){ video.fullscreen = stringToBool(val); }},
|
||||||
if (var == "video.fullscreen") {
|
{"video.scale_mode", [](const auto& val){ video.scale_mode = static_cast<SDL_ScaleMode>(std::stoi(val)); }},
|
||||||
video.fullscreen = stringToBool(value);
|
{"video.shaders", [](const auto& val){ video.shaders = stringToBool(val); }},
|
||||||
} else if (var == "video.scale_mode") {
|
{"video.integer_scale", [](const auto& val){ video.integer_scale = stringToBool(val); }},
|
||||||
video.scale_mode = static_cast<SDL_ScaleMode>(std::stoi(value));
|
{"video.vsync", [](const auto& val){ video.vsync = stringToBool(val); }},
|
||||||
} else if (var == "video.shaders") {
|
// Audio
|
||||||
video.shaders = stringToBool(value);
|
{"audio.enabled", [](const auto& val){ audio.enabled = stringToBool(val); }},
|
||||||
} else if (var == "video.integer_scale") {
|
{"audio.volume", [](const auto& val){ audio.volume = std::clamp(std::stoi(val), 0, 100); }},
|
||||||
video.integer_scale = stringToBool(value);
|
{"audio.music.enabled", [](const auto& val){ audio.music.enabled = stringToBool(val); }},
|
||||||
} else if (var == "video.vsync") {
|
{"audio.music.volume", [](const auto& val){ audio.music.volume = std::clamp(std::stoi(val), 0, 100); }},
|
||||||
video.vsync = stringToBool(value);
|
{"audio.sound.enabled", [](const auto& val){ audio.sound.enabled = stringToBool(val); }},
|
||||||
}
|
{"audio.sound.volume", [](const auto& val){ audio.sound.volume = std::clamp(std::stoi(val), 0, 100); }},
|
||||||
|
// Juego
|
||||||
// Opciones de audio
|
{"game.language", [](const auto& val){
|
||||||
else if (var == "audio.enabled") {
|
settings.language = static_cast<Lang::Code>(std::stoi(val));
|
||||||
audio.enabled = stringToBool(value);
|
|
||||||
} else if (var == "audio.volume") {
|
|
||||||
audio.volume = std::clamp(std::stoi(value), 0, 100);
|
|
||||||
} else if (var == "audio.music.enabled") {
|
|
||||||
audio.music.enabled = stringToBool(value);
|
|
||||||
} else if (var == "audio.music.volume") {
|
|
||||||
audio.music.volume = std::clamp(std::stoi(value), 0, 100);
|
|
||||||
} else if (var == "audio.sound.enabled") {
|
|
||||||
audio.sound.enabled = stringToBool(value);
|
|
||||||
} else if (var == "audio.sound.volume") {
|
|
||||||
audio.sound.volume = std::clamp(std::stoi(value), 0, 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Opciones de juego
|
|
||||||
else if (var == "game.language") {
|
|
||||||
settings.language = static_cast<Lang::Code>(std::stoi(value));
|
|
||||||
pending_changes.new_language = settings.language;
|
pending_changes.new_language = settings.language;
|
||||||
} else if (var == "game.difficulty") {
|
}},
|
||||||
settings.difficulty = static_cast<DifficultyCode>(std::stoi(value));
|
{"game.difficulty", [](const auto& val){
|
||||||
|
settings.difficulty = static_cast<DifficultyCode>(std::stoi(val));
|
||||||
pending_changes.new_difficulty = settings.difficulty;
|
pending_changes.new_difficulty = settings.difficulty;
|
||||||
} else if (var == "game.autofire") {
|
}},
|
||||||
settings.autofire = stringToBool(value);
|
{"game.autofire", [](const auto& val){ settings.autofire = stringToBool(val); }},
|
||||||
} else if (var == "game.shutdown_enabled") {
|
{"game.shutdown_enabled", [](const auto& val){ settings.shutdown_enabled = stringToBool(val); }}
|
||||||
settings.shutdown_enabled = stringToBool(value);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// Opciones de mandos
|
|
||||||
else if (var == "controller.0.name") {
|
|
||||||
controllers.at(0).name = value;
|
|
||||||
} else if (var == "controller.0.player") {
|
|
||||||
controllers.at(0).player_id = std::clamp(std::stoi(value), 1, 2);
|
|
||||||
} else if (var == "controller.0.type") {
|
|
||||||
controllers.at(0).type = static_cast<InputDevice>(std::stoi(value));
|
|
||||||
} else if (var == "controller.0.button.fire_left") {
|
|
||||||
controllers.at(0).buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.0.button.fire_center") {
|
|
||||||
controllers.at(0).buttons.at(1) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.0.button.fire_right") {
|
|
||||||
controllers.at(0).buttons.at(2) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.0.button.start") {
|
|
||||||
controllers.at(0).buttons.at(3) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.0.button.service") {
|
|
||||||
controllers.at(0).buttons.at(4) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.1.name") {
|
|
||||||
controllers.at(1).name = value;
|
|
||||||
} else if (var == "controller.1.player") {
|
|
||||||
controllers.at(1).player_id = std::clamp(std::stoi(value), 1, 2);
|
|
||||||
} else if (var == "controller.1.type") {
|
|
||||||
controllers.at(1).type = static_cast<InputDevice>(std::stoi(value));
|
|
||||||
} else if (var == "controller.1.button.fire_left") {
|
|
||||||
controllers.at(1).buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.1.button.fire_center") {
|
|
||||||
controllers.at(1).buttons.at(1) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.1.button.fire_right") {
|
|
||||||
controllers.at(1).buttons.at(2) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.1.button.start") {
|
|
||||||
controllers.at(1).buttons.at(3) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
} else if (var == "controller.1.button.service") {
|
|
||||||
controllers.at(1).buttons.at(4) = static_cast<SDL_GamepadButton>(std::stoi(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Maneja por separado la configuración general de los mandos
|
||||||
|
if (var.starts_with("controller.")) {
|
||||||
|
try {
|
||||||
|
parseAndSetController(var, value);
|
||||||
return true;
|
return true;
|
||||||
|
} catch (const std::out_of_range& e) {
|
||||||
|
// Error: por ejemplo, índice de mando fuera de rango
|
||||||
|
return false;
|
||||||
|
} catch (const std::invalid_argument& e) {
|
||||||
|
// Error: por ejemplo, fallo en std::stoi
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Busca el nombre de la variable en el mapa
|
||||||
|
if (auto it = settings_map.find(var); it != settings_map.end()) {
|
||||||
|
try {
|
||||||
|
// Ejecuta la función lambda asociada
|
||||||
|
it->second(value);
|
||||||
|
return true;
|
||||||
|
} catch (const std::invalid_argument& e) {
|
||||||
|
// Maneja casos donde std::stoi falla por entrada inválida
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si la clave no se encontró en el mapa ni en la lógica de mandos
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Asigna el teclado al jugador
|
// Asigna el teclado al jugador
|
||||||
void setKeyboardToPlayer(int player_id) {
|
void setKeyboardToPlayer(int player_id) {
|
||||||
for (auto &controller : controllers) {
|
for (auto &controller : controllers) {
|
||||||
|
|||||||
Reference in New Issue
Block a user