Files
coffee_crisis_arcade_edition/source/define_buttons.cpp

174 lines
4.7 KiB
C++

#include "define_buttons.h"
#include <utility> // Para move
#include "input.h" // Para Input, InputType
#include "lang.h" // Para getText
#include "options.h" // Para OptionsController, Options, options
#include "param.h" // Para Param, param, ParamGame, ParamTitle
#include "section.h" // Para Name, Options, name, options
#include "text.h" // Para Text
// Constructor
DefineButtons::DefineButtons(std::unique_ptr<Text> text_)
: input_(Input::get()),
text_(std::move(text_))
{
// Inicializa variables
x_ = param.game.width / 2;
y_ = param.title.press_start_position;
buttons_.emplace_back(lang::getText(95), InputType::FIRE_LEFT, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(96), InputType::FIRE_CENTER, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(97), InputType::FIRE_RIGHT, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(99), InputType::EXIT, SDL_CONTROLLER_BUTTON_INVALID);
for (int i = 0; i < input_->getNumControllers(); ++i)
{
controller_names_.emplace_back(input_->getControllerName(i));
}
}
// Dibuja el objeto en pantalla
void DefineButtons::render()
{
if (enabled_)
{
text_->writeCentered(x_, y_ - 10, lang::getText(100) + std::to_string(options.controller[index_controller_].player_id));
text_->writeCentered(x_, y_, controller_names_[index_controller_]);
text_->writeCentered(x_, y_ + 10, buttons_[index_button_].label);
}
}
// Comprueba el botón que se ha pulsado
void DefineButtons::doControllerButtonDown(SDL_ControllerButtonEvent &event)
{
size_t i = input_->getJoyIndex(event.which);
// Solo pillamos botones del mando que toca
if (i != index_controller_)
{
return;
}
const auto button = static_cast<SDL_GameControllerButton>(event.button);
if (checkButtonNotInUse(button))
{
buttons_[index_button_].button = button;
incIndexButton();
}
}
// Asigna los botones definidos al input_
void DefineButtons::bindButtons()
{
for (size_t i = 0; i < buttons_.size(); ++i)
{
input_->bindGameControllerButton(index_controller_, buttons_[i].input, buttons_[i].button);
}
}
// Comprueba los eventos
void DefineButtons::checkEvents()
{
if (enabled_)
{
SDL_Event event;
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
{
section::name = section::Name::QUIT;
section::options = section::Options::QUIT_WITH_KEYBOARD;
break;
}
case SDL_CONTROLLERBUTTONDOWN:
{
doControllerButtonDown(event.cbutton);
break;
}
default:
break;
}
}
}
}
// Habilita el objeto
bool DefineButtons::enable(int index)
{
if (index < input_->getNumControllers())
{
enabled_ = true;
index_controller_ = index;
index_button_ = 0;
return true;
}
return false;
}
// Comprueba si está habilitado
bool DefineButtons::isEnabled()
{
return enabled_;
}
// Incrementa el indice de los botones
void DefineButtons::incIndexButton()
{
index_button_++;
// Comprueba si ha finalizado
if (index_button_ == buttons_.size())
{
// Asigna los botones definidos al input_
bindButtons();
// Guarda los cambios en las opciones
saveBindingsToOptions();
// input_->allActive(index_controller_);
// Reinicia variables
index_button_ = 0;
index_controller_ = 0;
enabled_ = false;
}
}
// Guarda los cambios en las opciones
void DefineButtons::saveBindingsToOptions()
{
// Modifica las opciones para colocar los valores asignados
options.controller[index_controller_].name = input_->getControllerName(index_controller_);
for (int j = 0; j < (int)options.controller[index_controller_].inputs.size(); ++j)
{
options.controller[index_controller_].buttons[j] = input_->getControllerBinding(index_controller_, options.controller[index_controller_].inputs[j]);
}
}
// Intercambia los jugadores asignados a los dos primeros mandos
void DefineButtons::swapControllers()
{
std::swap(options.controller[0].player_id, options.controller[1].player_id);
}
// Comprueba que un botón no esté ya asignado
bool DefineButtons::checkButtonNotInUse(SDL_GameControllerButton button)
{
for (const auto &b : buttons_)
{
if (b.button == button)
{
return false;
}
}
return true;
}