Title: clang-tidy readability-function-cognitive-complexity
This commit is contained in:
@@ -108,160 +108,190 @@ void Title::render() {
|
||||
void Title::checkEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
#ifdef _DEBUG
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 1) {
|
||||
static Color color_ = param.title.bg_color;
|
||||
switch (event.key.key) {
|
||||
case SDLK_A:
|
||||
if (color_.r < 255) {
|
||||
++color_.r;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_Z:
|
||||
if (color_.r > 0) {
|
||||
--color_.r;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_S:
|
||||
if (color_.g < 255) {
|
||||
++color_.g;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_X:
|
||||
if (color_.g > 0) {
|
||||
--color_.g;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_D:
|
||||
if (color_.b < 255) {
|
||||
++color_.b;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_C:
|
||||
if (color_.b > 0) {
|
||||
--color_.b;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_F:
|
||||
if (color_.r < 255) {
|
||||
++color_.r;
|
||||
}
|
||||
if (color_.g < 255) {
|
||||
++color_.g;
|
||||
}
|
||||
if (color_.b < 255) {
|
||||
++color_.b;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_V:
|
||||
if (color_.r > 0) {
|
||||
--color_.r;
|
||||
}
|
||||
if (color_.g > 0) {
|
||||
--color_.g;
|
||||
}
|
||||
if (color_.b > 0) {
|
||||
--color_.b;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
counter_ = 0;
|
||||
tiled_bg_->setColor(color_);
|
||||
std::cout << "#"
|
||||
<< std::hex << std::setw(2) << std::setfill('0') << (int)color_.r
|
||||
<< std::setw(2) << std::setfill('0') << (int)color_.g
|
||||
<< std::setw(2) << std::setfill('0') << (int)color_.b
|
||||
<< std::endl;
|
||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||
handleKeyDownEvent(event);
|
||||
}
|
||||
#endif
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
|
||||
switch (event.key.key) {
|
||||
case SDLK_1: // Redefine los botones del mando #0
|
||||
define_buttons_->enable(0);
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_2: // Redefine los botones del mando #1
|
||||
define_buttons_->enable(1);
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_3: // Intercambia los mandos entre los dos jugadores
|
||||
swapControllers();
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_4: // Intercambia la asignación del teclado
|
||||
swapKeyboard();
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_5: // Muestra la asignación de mandos y teclado
|
||||
showControllers();
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GlobalEvents::check(event);
|
||||
define_buttons_->checkEvents(event);
|
||||
}
|
||||
}
|
||||
|
||||
void Title::handleKeyDownEvent(const SDL_Event& event) {
|
||||
bool isFirstPress = static_cast<int>(event.key.repeat) == 0;
|
||||
if (isFirstPress) {
|
||||
handleControlKeys(event.key.key);
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
bool isRepeat = static_cast<int>(event.key.repeat) == 1;
|
||||
if (isRepeat) {
|
||||
handleDebugColorKeys(event.key.key);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
void Title::handleDebugColorKeys(SDL_Keycode key) {
|
||||
static Color color_ = param.title.bg_color;
|
||||
|
||||
adjustColorComponent(key, color_);
|
||||
|
||||
counter_ = 0;
|
||||
tiled_bg_->setColor(color_);
|
||||
printColorValue(color_);
|
||||
}
|
||||
|
||||
void Title::adjustColorComponent(SDL_Keycode key, Color& color) {
|
||||
switch (key) {
|
||||
case SDLK_A: incrementColorComponent(color.r); break;
|
||||
case SDLK_Z: decrementColorComponent(color.r); break;
|
||||
case SDLK_S: incrementColorComponent(color.g); break;
|
||||
case SDLK_X: decrementColorComponent(color.g); break;
|
||||
case SDLK_D: incrementColorComponent(color.b); break;
|
||||
case SDLK_C: decrementColorComponent(color.b); break;
|
||||
case SDLK_F: incrementAllComponents(color); break;
|
||||
case SDLK_V: decrementAllComponents(color); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void Title::incrementColorComponent(uint8_t& component) {
|
||||
if (component < 255) {
|
||||
++component;
|
||||
}
|
||||
}
|
||||
|
||||
void Title::decrementColorComponent(uint8_t& component) {
|
||||
if (component > 0) {
|
||||
--component;
|
||||
}
|
||||
}
|
||||
|
||||
void Title::incrementAllComponents(Color& color) {
|
||||
incrementColorComponent(color.r);
|
||||
incrementColorComponent(color.g);
|
||||
incrementColorComponent(color.b);
|
||||
}
|
||||
|
||||
void Title::decrementAllComponents(Color& color) {
|
||||
decrementColorComponent(color.r);
|
||||
decrementColorComponent(color.g);
|
||||
decrementColorComponent(color.b);
|
||||
}
|
||||
|
||||
void Title::printColorValue(const Color& color) {
|
||||
std::cout << "#"
|
||||
<< std::hex << std::setw(2) << std::setfill('0') << (int)color.r
|
||||
<< std::setw(2) << std::setfill('0') << (int)color.g
|
||||
<< std::setw(2) << std::setfill('0') << (int)color.b
|
||||
<< std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
void Title::handleControlKeys(SDL_Keycode key) {
|
||||
switch (key) {
|
||||
case SDLK_1:
|
||||
define_buttons_->enable(0);
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_2:
|
||||
define_buttons_->enable(1);
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_3:
|
||||
swapControllers();
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_4:
|
||||
swapKeyboard();
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
case SDLK_5:
|
||||
showControllers();
|
||||
resetCounter();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Title::checkInput() {
|
||||
// Comprueba las entradas solo si no se estan definiendo los botones
|
||||
if (define_buttons_->isEnabled()) {
|
||||
if (shouldSkipInputCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Input::get()->update();
|
||||
|
||||
if (!ServiceMenu::get()->isEnabled()) {
|
||||
// Comprueba todos los métodos de control
|
||||
for (const auto &controller : Options::controllers) {
|
||||
// Boton START
|
||||
if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index)) {
|
||||
if ((state_ != TitleState::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP)) {
|
||||
if (controller.player_id == 1) {
|
||||
if (!player1_start_pressed_) {
|
||||
player1_start_pressed_ = true;
|
||||
getPlayer(1)->setPlayingState(PlayerState::TITLE_ANIMATION);
|
||||
setState(TitleState::START_HAS_BEEN_PRESSED);
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (controller.player_id == 2) {
|
||||
if (!player2_start_pressed_) {
|
||||
player2_start_pressed_ = true;
|
||||
getPlayer(2)->setPlayingState(PlayerState::TITLE_ANIMATION);
|
||||
setState(TitleState::START_HAS_BEEN_PRESSED);
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
processControllerInputs();
|
||||
}
|
||||
|
||||
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
|
||||
GlobalInputs::check();
|
||||
}
|
||||
|
||||
bool Title::shouldSkipInputCheck() const {
|
||||
return define_buttons_->isEnabled();
|
||||
}
|
||||
|
||||
void Title::processControllerInputs() {
|
||||
for (const auto &controller : Options::controllers) {
|
||||
if (isStartButtonPressed(controller)) {
|
||||
handleStartButtonPress(controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Title::isStartButtonPressed(const Options::GamepadOptions &controller) const {
|
||||
return Input::get()->checkInput(
|
||||
InputAction::START,
|
||||
INPUT_DO_NOT_ALLOW_REPEAT,
|
||||
controller.type,
|
||||
controller.index
|
||||
);
|
||||
}
|
||||
|
||||
void Title::handleStartButtonPress(const Options::GamepadOptions &controller) {
|
||||
if (!canProcessStartButton()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (controller.player_id == 1) {
|
||||
processPlayer1Start();
|
||||
} else if (controller.player_id == 2) {
|
||||
processPlayer2Start();
|
||||
}
|
||||
}
|
||||
|
||||
bool Title::canProcessStartButton() const {
|
||||
return (state_ != TitleState::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP);
|
||||
}
|
||||
|
||||
void Title::processPlayer1Start() {
|
||||
if (!player1_start_pressed_) {
|
||||
player1_start_pressed_ = true;
|
||||
activatePlayerAndSetState(1);
|
||||
}
|
||||
}
|
||||
|
||||
void Title::processPlayer2Start() {
|
||||
if (!player2_start_pressed_) {
|
||||
player2_start_pressed_ = true;
|
||||
activatePlayerAndSetState(2);
|
||||
}
|
||||
}
|
||||
|
||||
void Title::activatePlayerAndSetState(int player_id) {
|
||||
getPlayer(player_id)->setPlayingState(PlayerState::TITLE_ANIMATION);
|
||||
setState(TitleState::START_HAS_BEEN_PRESSED);
|
||||
counter_ = 0;
|
||||
}
|
||||
|
||||
// Bucle para el titulo del juego
|
||||
void Title::run() {
|
||||
while (Section::name == Section::Name::TITLE) {
|
||||
|
||||
Reference in New Issue
Block a user