#include "define_buttons.h" #include // for move #include "input.h" // for Input, InputType #include "lang.h" // for getText #include "options.h" // for options #include "param.h" // for param #include "section.h" // for Name, Options, name, options #include "text.h" // for Text #include "utils.h" // for OptionsController, Options, Param, ParamGame // Constructor DefineButtons::DefineButtons(std::unique_ptr text_) : text_(std::move(text_)) { // Copia punteros a los objetos input_ = Input::get(); // Inicializa variables enabled_ = false; x_ = param.game.width / 2; y_ = param.title.press_start_position; index_controller_ = 0; index_button_ = 0; buttons_.clear(); DefineButtonsButton button; button.label = lang::getText(95); button.input = InputType::FIRE_LEFT; button.button = SDL_CONTROLLER_BUTTON_X; buttons_.push_back(button); button.label = lang::getText(96); button.input = InputType::FIRE_CENTER; button.button = SDL_CONTROLLER_BUTTON_Y; buttons_.push_back(button); button.label = lang::getText(97); button.input = InputType::FIRE_RIGHT; button.button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER; buttons_.push_back(button); button.label = lang::getText(98); button.input = InputType::START; button.button = SDL_CONTROLLER_BUTTON_START; buttons_.push_back(button); button.label = lang::getText(99); button.input = InputType::EXIT; button.button = SDL_CONTROLLER_BUTTON_BACK; buttons_.push_back(button); for (int i = 0; i < input_->getNumControllers(); ++i) { controller_names_.push_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) { int i = input_->getJoyIndex(event->which); // Solo pillamos botones del mando que toca if (i != index_controller_) { return; } buttons_[index_button_].button = (SDL_GameControllerButton)event->button; incIndexButton(); } // Asigna los botones definidos al input_ void DefineButtons::bindButtons() { for (int i = 0; i < (int)buttons_.size(); ++i) { input_->bindGameControllerButton(index_controller_, buttons_[i].input, buttons_[i].button); } } // Comprueba las entradas void DefineButtons::checkInput() { 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_ == (int)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() { const int temp = options.controller[0].player_id; options.controller[0].player_id = options.controller[1].player_id; options.controller[1].player_id = temp; }