Files
coffee_crisis_arcade_edition/source/global_inputs.cpp

439 lines
14 KiB
C++

#include "global_inputs.h"
#include <string> // Para basic_string, operator+, allocator, cha...
#include <vector> // Para vector
#include "asset.h" // Para Asset
#include "audio.h" // Para Audio
#include "input.h" // Para Input, INPUT_DO_NOT_ALLOW_REPEAT, Input...
#include "lang.h" // Para getText, Code, getNextLangCode, loadFro...
#include "notifier.h" // Para Notifier
#include "options.h" // Para SettingsOptions, settings, VideoOptions
#include "screen.h" // Para Screen
#include "section.h" // Para Name, name, Options, options, AttractMode
#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.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::settings.language = Lang::getNextLangCode(Options::settings.language);
Lang::loadFromFile(getLangFile(static_cast<Lang::Code>(Options::settings.language)));
Section::name = Section::Name::RESET;
Section::options = Section::Options::RELOAD;
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 05") + getLangName(Options::settings.language)});
}
else
{
const auto NEXT = Lang::getNextLangCode(Options::settings.language);
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 04") + getLangName(NEXT), 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.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
bool checkServiceButton()
{
// Teclado
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
toggleServiceMenu();
return true;
}
// Mandos
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
toggleServiceMenu();
return true;
}
}
}
return false;
}
// Comprueba las entradas del menú de servicio
bool checkServiceInputs()
{
if (!ServiceMenu::get()->isEnabled())
return false;
// Teclado
{
// Arriba
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
ServiceMenu::get()->setSelectorUp();
return true;
}
// Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
ServiceMenu::get()->setSelectorDown();
return true;
}
// Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
ServiceMenu::get()->adjustOption(true);
return true;
}
// Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
ServiceMenu::get()->adjustOption(false);
return true;
}
// Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
ServiceMenu::get()->selectOption();
return true;
}
// Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
ServiceMenu::get()->moveBack();
return true;
}
}
// Mandos
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
// Arriba
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
ServiceMenu::get()->setSelectorUp();
return true;
}
// Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
ServiceMenu::get()->setSelectorDown();
return true;
}
// Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
ServiceMenu::get()->adjustOption(true);
return true;
}
// Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
ServiceMenu::get()->adjustOption(false);
return true;
}
// Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
ServiceMenu::get()->selectOption();
return true;
}
// Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i))
{
ServiceMenu::get()->moveBack();
return true;
}
}
}
return false;
}
// Comprueba las entradas fuera del menú de servicio
bool checkInputs()
{
// Teclado
{
// Comprueba el teclado para cambiar entre pantalla completa y ventana
if (Input::get()->checkInput(InputAction::WINDOW_FULLSCREEN, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
Screen::get()->toggleFullscreen();
const std::string MODE = Options::video.fullscreen ? Lang::getText("[NOTIFICATIONS] 11") : Lang::getText("[NOTIFICATIONS] 10");
Notifier::get()->show({MODE});
return true;
}
// Comprueba el teclado para decrementar el tamaño de la ventana
if (Input::get()->checkInput(InputAction::WINDOW_DEC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
if (Screen::get()->decWindowSize())
{
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.size)});
}
return true;
}
// Comprueba el teclado para incrementar el tamaño de la ventana
if (Input::get()->checkInput(InputAction::WINDOW_INC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
if (Screen::get()->incWindowSize())
{
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.size)});
}
return true;
}
// Salir
if (Input::get()->checkInput(InputAction::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
quit();
return true;
}
// Saltar sección
if (Input::get()->checkAnyButton() && !ServiceMenu::get()->isEnabled())
{
skipSection();
return true;
}
// Reset
if (Input::get()->checkInput(InputAction::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
reset();
return true;
}
// Audio
if (Input::get()->checkInput(InputAction::TOGGLE_AUDIO, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
toggleAudio();
return true;
}
// Autofire
if (Input::get()->checkInput(InputAction::TOGGLE_AUTO_FIRE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
toggleFireMode();
return true;
}
// Idioma
if (Input::get()->checkInput(InputAction::CHANGE_LANG, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
changeLang();
return true;
}
// Shaders
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
toggleShaders();
return true;
}
// Integer Scale
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_INTEGER_SCALE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
toggleIntegerScale();
return true;
}
// VSync
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_VSYNC, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
toggleVSync();
return true;
}
#ifdef DEBUG
// Debug info
if (Input::get()->checkInput(InputAction::SHOW_INFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD))
{
Screen::get()->toggleDebugInfo();
return true;
}
#endif
}
return false;
}
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
bool check()
{
if (checkServiceButton())
return true;
if (checkServiceInputs())
return true;
if (checkInputs())
return true;
return false;
}
}