Files
coffee_crisis_arcade_edition/source/global_inputs.cpp

437 lines
14 KiB
C++

#include "global_inputs.h"
#include <SDL3/SDL_render.h> // Para SDL_RendererLogicalPresentation, SDL_Se...
#include <string> // Para operator+, allocator, char_traits, string
#include <vector> // Para vector
#include "asset.h" // Para Asset
#include "input.h" // Para Input, InputAction, InputDeviceToUse
#include "audio.h" // Para JA_SetMusicVolume, JA_SetSoundVolume
#include "lang.h" // Para getText, Code, getNextLangCode, loadFro...
#include "notifier.h" // Para Notifier
#include "options.h" // Para Options, options, VideoOptions, GameOpt...
#include "param.h" // Para Param, ParamGame, param
#include "screen.h" // Para Screen
#include "section.h" // Para Name, name, Options, options, AttractMode
#include "service_menu.h" // Para ServiceMenu
#include "utils.h" // Para boolToOnOff
namespace globalInputs
{
// Termina
void quit(section::Options code)
{
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 = code;
}
else
{
// Si la notificación de salir no está activa, muestra la notificación
#ifdef ARCADE
const std::string TEXT = code == section::Options::QUIT_WITH_CONTROLLER ? lang::getText("[NOTIFICATIONS] 02") : lang::getText("[NOTIFICATIONS] 01");
Notifier::get()->show({TEXT, std::string()}, -1, CODE);
#else
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 01"), std::string()}, -1, CODE);
#endif
}
}
// 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.v_sync)});
}
// Activa o desactiva los shaders
void toggleShaders()
{
Screen::get()->toggleShaders();
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 13") + " " + boolToOnOff(options.video.shaders)});
}
// Obtiene una fichero a partir de un lang::Code
std::string getLangFile(lang::Code code)
{
switch (code)
{
case lang::Code::VALENCIAN:
return Asset::get()->get("ba_BA.json");
break;
case lang::Code::SPANISH:
return Asset::get()->get("es_ES.json");
break;
default:
return Asset::get()->get("en_UK.json");
break;
}
}
// Obtiene una cadena a partir de un lang::Code
std::string getLangName(lang::Code code)
{
switch (code)
{
case lang::Code::VALENCIAN:
return " \"ba_BA\"";
break;
case lang::Code::SPANISH:
return " \"es_ES\"";
break;
default:
return " \"en_UK\"";
break;
}
}
// Cambia el idioma
void changeLang()
{
const std::string CODE = "LANG";
if (Notifier::get()->checkCode(CODE))
{
options.game.language = lang::getNextLangCode(options.game.language);
lang::loadFromFile(getLangFile(static_cast<lang::Code>(options.game.language)));
section::name = section::Name::RESET;
section::options = section::Options::RELOAD;
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 05") + getLangName(options.game.language)});
}
else
{
const auto NEXT = lang::getNextLangCode(options.game.language);
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 04") + getLangName(NEXT), std::string()}, -1, CODE);
}
}
// Cambia el modo de disparo
void toggleFireMode()
{
options.game.autofire = !options.game.autofire;
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 08") + " " + boolToOnOff(options.game.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.size)});
}
}
// 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.size)});
}
}
// Comprueba el boton de servicio
void checkServiceButton()
{
// Teclado
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
toggleServiceMenu();
return;
}
// Mandos
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
toggleServiceMenu();
return;
}
}
}
}
// Comprueba las entradas del menú de servicio
void checkServiceInputs()
{
if (!ServiceMenu::get()->isEnabled())
return;
// Teclado
{
// Arriba
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
ServiceMenu::get()->setSelectorUp();
return;
}
// Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
ServiceMenu::get()->setSelectorDown();
return;
}
// Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
ServiceMenu::get()->adjustOption(true);
return;
}
// Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
ServiceMenu::get()->adjustOption(false);
return;
}
// Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
ServiceMenu::get()->selectOption();
return;
}
// Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
ServiceMenu::get()->moveBack();
return;
}
}
// Mandos
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
// Arriba
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
ServiceMenu::get()->setSelectorUp();
return;
}
// Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
ServiceMenu::get()->setSelectorDown();
return;
}
// Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
ServiceMenu::get()->adjustOption(true);
return;
}
// Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
ServiceMenu::get()->adjustOption(false);
return;
}
// Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
ServiceMenu::get()->selectOption();
return;
}
// Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
ServiceMenu::get()->moveBack();
return;
}
}
}
}
// Comprueba las entradas fuera del menú de servicio
void checkInputs()
{
// Teclado
{
// Comprueba el teclado para cambiar entre pantalla completa y ventana
if (Input::get()->checkInput(InputAction::WINDOW_FULLSCREEN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
Screen::get()->toggleFullscreen();
const std::string MODE = options.video.fullscreen ? lang::getText("[NOTIFICATIONS] 11") : lang::getText("[NOTIFICATIONS] 10");
Notifier::get()->show({MODE});
return;
}
// Comprueba el teclado para decrementar el tamaño de la ventana
if (Input::get()->checkInput(InputAction::WINDOW_DEC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
if (Screen::get()->decWindowSize())
{
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(options.window.size)});
}
return;
}
// Comprueba el teclado para incrementar el tamaño de la ventana
if (Input::get()->checkInput(InputAction::WINDOW_INC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
if (Screen::get()->incWindowSize())
{
Notifier::get()->show({lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(options.window.size)});
}
return;
}
// Salir
if (Input::get()->checkInput(InputAction::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
quit(section::Options::QUIT_WITH_KEYBOARD);
return;
}
// Saltar sección
if (Input::get()->checkAnyButtonPressed() && !ServiceMenu::get()->isEnabled())
{
skipSection();
return;
}
// Reset
if (Input::get()->checkInput(InputAction::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
reset();
return;
}
// Audio
if (Input::get()->checkInput(InputAction::TOGGLE_AUDIO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
toggleAudio();
return;
}
// Autofire
if (Input::get()->checkInput(InputAction::TOGGLE_AUTO_FIRE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
toggleFireMode();
return;
}
// Idioma
if (Input::get()->checkInput(InputAction::CHANGE_LANG, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
changeLang();
return;
}
// Shaders
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
toggleShaders();
return;
}
// Integer Scale
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_INTEGER_SCALE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
toggleIntegerScale();
return;
}
// VSync
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_VSYNC, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
toggleVSync();
return;
}
#ifdef DEBUG
// Debug info
if (Input::get()->checkInput(InputAction::SHOW_INFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
Screen::get()->toggleDebugInfo();
return;
}
#endif
}
}
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
void check()
{
checkServiceButton();
checkServiceInputs();
checkInputs();
}
}