Files
coffee_crisis_arcade_edition/source/global_inputs.cpp
Sergio 41e3fd1d8d iwyu
clang-tidy
clang-format
2025-08-10 22:01:18 +02:00

221 lines
7.3 KiB
C++

#include "global_inputs.h"
#include <functional> // Para function
#include <memory> // Para allocator, shared_ptr
#include <string> // Para operator+, char_traits, string, to_string
#include <utility> // Para pair
#include <vector> // Para vector
#include "audio.h" // Para Audio
#include "input.h" // Para Input
#include "input_types.h" // Para InputAction
#include "lang.h" // Para getText, getLangFile, getLangName, getNextLangCode, loadFromFile
#include "options.h" // Para Video, video, Settings, settings, Audio, audio, Window, window
#include "screen.h" // Para Screen
#include "section.hpp" // Para Name, name, Options, options, AttractMode, attract_mode
#include "ui/notifier.h" // Para Notifier
#include "ui/service_menu.h" // Para ServiceMenu
#include "utils.h" // Para boolToOnOff
namespace GlobalInputs {
// Termina
void quit() {
const std::string CODE = "QUIT";
if (Notifier::get()->checkCode(CODE)) {
// Si la notificación de salir está activa, cambia de sección
Section::name = Section::Name::QUIT;
Section::options = Section::Options::NONE;
} else {
// Si la notificación de salir no está activa, muestra la notificación
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 01"), std::string()}, -1, CODE);
}
}
// Reinicia
void reset() {
const std::string CODE = "RESET";
if (Notifier::get()->checkCode(CODE)) {
Section::name = Section::Name::RESET;
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 15")});
} else {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 03"), std::string()}, -1, CODE);
}
}
// Activa o desactiva el audio
void toggleAudio() {
Options::audio.enabled = !Options::audio.enabled;
Audio::get()->enable(Options::audio.enabled);
Notifier::get()->show({"Audio " + boolToOnOff(Options::audio.enabled)});
}
// Cambia el modo de escalado entero
void toggleIntegerScale() {
Screen::get()->toggleIntegerScale();
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 12") + " " + boolToOnOff(Options::video.integer_scale)});
}
// Activa / desactiva el vsync
void toggleVSync() {
Screen::get()->toggleVSync();
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 14") + " " + boolToOnOff(Options::video.vsync)});
}
// Activa o desactiva los shaders
void toggleShaders() {
Screen::get()->toggleShaders();
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 13") + " " + boolToOnOff(Options::video.shaders)});
}
// Cambia al siguiente idioma
void setNextLang() {
const std::string CODE = "LANG";
const auto NEXT_LANG_CODE = Lang::getNextLangCode(Options::settings.language);
const auto NEXT_LANG_NAME = Lang::getLangName(NEXT_LANG_CODE);
if (Notifier::get()->checkCode(CODE)) {
// Si la notificación de cambiar idioma está activa, cambia de de idioma
Options::settings.language = NEXT_LANG_CODE;
Lang::loadFromFile(Lang::getLangFile(NEXT_LANG_CODE));
Section::name = Section::Name::RESET;
Section::options = Section::Options::RELOAD;
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 05") + NEXT_LANG_NAME});
} else {
// Si la notificación de cambiar idioma no está activa, muestra la notificación
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 04") + NEXT_LANG_NAME, std::string()}, -1, CODE);
}
}
// Cambia el modo de disparo
void toggleFireMode() {
Options::settings.autofire = !Options::settings.autofire;
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 08") + " " + boolToOnOff(Options::settings.autofire)});
}
// Salta una sección del juego
void skipSection() {
switch (Section::name) {
case Section::Name::INTRO:
Audio::get()->stopMusic();
/* Continua en el case de abajo */
case Section::Name::LOGO:
case Section::Name::HI_SCORE_TABLE:
case Section::Name::INSTRUCTIONS: {
Section::name = Section::Name::TITLE;
Section::options = Section::Options::TITLE_1;
Section::attract_mode = Section::AttractMode::TITLE_TO_DEMO;
break;
}
default:
break;
}
}
// Activa el menu de servicio
void toggleServiceMenu() {
ServiceMenu::get()->toggle();
}
// Cambia el modo de pantalla completa
void toggleFullscreen() {
Screen::get()->toggleFullscreen();
const std::string MODE = Options::video.fullscreen ? Lang::getText("[NOTIFICATIONS] 11") : Lang::getText("[NOTIFICATIONS] 10");
Notifier::get()->show({MODE});
}
// Reduce el tamaño de la ventana
void decWindowSize() {
if (Screen::get()->decWindowSize()) {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.zoom)});
}
}
// Aumenta el tamaño de la ventana
void incWindowSize() {
if (Screen::get()->incWindowSize()) {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.zoom)});
}
}
// Comprueba el boton de servicio
auto checkServiceButton() -> bool {
// Teclado
if (Input::get()->checkAction(Input::Action::SERVICE, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
toggleServiceMenu();
return true;
}
// Mandos
for (auto gamepad : Input::get()->getGamepads()) {
if (Input::get()->checkAction(Input::Action::SERVICE, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
toggleServiceMenu();
return true;
}
}
return false;
}
// Comprueba las entradas para elementos del sistema
auto checkSystemInputs() -> bool {
using Action = Input::Action;
static const std::vector<std::pair<Action, std::function<void()>>> ACTIONS = {
{Action::WINDOW_FULLSCREEN, toggleFullscreen},
{Action::WINDOW_DEC_SIZE, decWindowSize},
{Action::WINDOW_INC_SIZE, incWindowSize},
{Action::EXIT, quit},
{Action::RESET, reset},
{Action::TOGGLE_AUDIO, toggleAudio},
{Action::TOGGLE_AUTO_FIRE, toggleFireMode},
{Action::CHANGE_LANG, setNextLang},
{Action::TOGGLE_VIDEO_SHADERS, toggleShaders},
{Action::TOGGLE_VIDEO_INTEGER_SCALE, toggleIntegerScale},
{Action::TOGGLE_VIDEO_VSYNC, toggleVSync},
#ifdef _DEBUG
{Action::SHOW_INFO, [] { Screen::get()->toggleDebugInfo(); }},
#endif
};
for (const auto& [action, func] : ACTIONS) {
if (Input::get()->checkAction(action, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
func();
return true;
}
}
return false;
}
// Comprueba el resto de entradas
auto checkOtherInputs() -> bool {
// Saltar sección
if ((Input::get()->checkAnyButton()) && !ServiceMenu::get()->isEnabled()) {
skipSection();
return true;
}
return false;
}
// Comprueba las entradas del Menu de Servicio
inline auto checkServiceMenuInputs() -> bool {
return ServiceMenu::get()->checkInput();
}
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
auto check() -> bool {
if (checkServiceButton()) {
return true;
}
if (checkServiceMenuInputs()) {
return true;
}
if (checkSystemInputs()) {
return true;
}
if (checkOtherInputs()) {
return true;
}
return false;
}
} // namespace GlobalInputs