clang-tidy readability-function-cognitive-complexity

This commit is contained in:
2025-07-20 15:42:10 +02:00
parent cb4e4b450d
commit f2915aa4b4
6 changed files with 389 additions and 316 deletions

View File

@@ -15,16 +15,16 @@
#include "texture.h" // Para Texture
// Singleton
Notifier *Notifier::instance = nullptr;
Notifier* Notifier::instance = nullptr;
// Inicializa la instancia única del singleton
void Notifier::init(const std::string &icon_file, std::shared_ptr<Text> text) { Notifier::instance = new Notifier(icon_file, text); }
void Notifier::init(const std::string& icon_file, std::shared_ptr<Text> text) { Notifier::instance = new Notifier(icon_file, text); }
// Libera la instancia
void Notifier::destroy() { delete Notifier::instance; }
// Obtiene la instancia
auto Notifier::get() -> Notifier * { return Notifier::instance; }
auto Notifier::get() -> Notifier* { return Notifier::instance; }
// Constructor
Notifier::Notifier(std::string icon_file, std::shared_ptr<Text> text)
@@ -46,71 +46,108 @@ void Notifier::render() {
// Actualiza el estado de las notificaiones
void Notifier::update() {
for (int i = 0; i < (int)notifications_.size(); ++i) {
// Si la notificación anterior está "saliendo", no hagas nada
if (i > 0) {
if (notifications_[i - 1].state == NotificationStatus::RISING) {
break;
}
if (!shouldProcessNotification(i)) {
break;
}
notifications_[i].counter++;
// Hace sonar la notificación en el primer frame
if (notifications_[i].counter == 1) {
if (param.notification.sound) {
if (notifications_[i].state == NotificationStatus::RISING) {
// Reproduce el sonido de la notificación
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
}
}
}
// Comprueba los estados
if (notifications_[i].state == NotificationStatus::RISING) {
const float STEP = ((float)notifications_[i].counter / notifications_[i].travel_dist);
const int ALPHA = 255 * STEP;
if (param.notification.pos_v == NotifyPosition::TOP) {
notifications_[i].rect.y++;
} else {
notifications_[i].rect.y--;
}
notifications_[i].texture->setAlpha(ALPHA);
if (notifications_[i].rect.y == notifications_[i].y) {
notifications_[i].state = NotificationStatus::STAY;
notifications_[i].texture->setAlpha(255);
notifications_[i].counter = 0;
}
}
else if (notifications_[i].state == NotificationStatus::STAY) {
if (notifications_[i].counter == wait_time_) {
notifications_[i].state = NotificationStatus::VANISHING;
notifications_[i].counter = 0;
}
} else if (notifications_[i].state == NotificationStatus::VANISHING) {
const float STEP = (notifications_[i].counter / (float)notifications_[i].travel_dist);
const int ALPHA = 255 * (1 - STEP);
if (param.notification.pos_v == NotifyPosition::TOP) {
notifications_[i].rect.y--;
} else {
notifications_[i].rect.y++;
}
notifications_[i].texture->setAlpha(ALPHA);
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist) {
notifications_[i].state = NotificationStatus::FINISHED;
}
}
notifications_[i].sprite->setPosition(notifications_[i].rect);
processNotification(i);
}
clearFinishedNotifications();
}
bool Notifier::shouldProcessNotification(int index) const {
// Si la notificación anterior está "saliendo", no hagas nada
if (index > 0 && notifications_[index - 1].state == NotificationStatus::RISING) {
return false;
}
return true;
}
void Notifier::processNotification(int index) {
auto& notification = notifications_[index];
notification.counter++;
playNotificationSoundIfNeeded(notification);
updateNotificationState(index);
notification.sprite->setPosition(notification.rect);
}
void Notifier::playNotificationSoundIfNeeded(const Notification& notification) {
// Hace sonar la notificación en el primer frame
if (notification.counter == 1 &&
param.notification.sound &&
notification.state == NotificationStatus::RISING) {
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
}
}
void Notifier::updateNotificationState(int index) {
auto& notification = notifications_[index];
switch (notification.state) {
case NotificationStatus::RISING:
handleRisingState(index);
break;
case NotificationStatus::STAY:
handleStayState(index);
break;
case NotificationStatus::VANISHING:
handleVanishingState(index);
break;
default:
break;
}
}
void Notifier::handleRisingState(int index) {
auto& notification = notifications_[index];
const float step = (float)notification.counter / notification.travel_dist;
const int alpha = 255 * step;
moveNotificationVertically(notification, param.notification.pos_v == NotifyPosition::TOP ? 1 : -1);
notification.texture->setAlpha(alpha);
if (notification.rect.y == notification.y) {
transitionToStayState(index);
}
}
void Notifier::handleStayState(int index) {
auto& notification = notifications_[index];
if (notification.counter == wait_time_) {
notification.state = NotificationStatus::VANISHING;
notification.counter = 0;
}
}
void Notifier::handleVanishingState(int index) {
auto& notification = notifications_[index];
const float step = notification.counter / (float)notification.travel_dist;
const int alpha = 255 * (1 - step);
moveNotificationVertically(notification, param.notification.pos_v == NotifyPosition::TOP ? -1 : 1);
notification.texture->setAlpha(alpha);
if (notification.rect.y == notification.y - notification.travel_dist) {
notification.state = NotificationStatus::FINISHED;
}
}
void Notifier::moveNotificationVertically(Notification& notification, int direction) {
notification.rect.y += direction;
}
void Notifier::transitionToStayState(int index) {
auto& notification = notifications_[index];
notification.state = NotificationStatus::STAY;
notification.texture->setAlpha(255);
notification.counter = 0;
}
// Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications() {
for (int i = (int)notifications_.size() - 1; i >= 0; --i) {
@@ -120,7 +157,7 @@ void Notifier::clearFinishedNotifications() {
}
}
void Notifier::show(std::vector<std::string> texts, int icon, const std::string &code) {
void Notifier::show(std::vector<std::string> texts, int icon, const std::string& code) {
// Si no hay texto, acaba
if (texts.empty()) {
return;
@@ -132,12 +169,12 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
}
// Elimina las cadenas vacías
texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string &s) { return s.empty(); }),
texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string& s) { return s.empty(); }),
texts.end());
// Encuentra la cadena más larga
std::string longest;
for (const auto &text : texts) {
for (const auto& text : texts) {
if (text.length() > longest.length()) {
longest = text;
}
@@ -238,7 +275,7 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
// Escribe el texto de la notificación
const Color COLOR{255, 255, 255};
int iterator = 0;
for (const auto &text : texts) {
for (const auto& text : texts) {
text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + iterator * (text_->getCharacterSize() + 1), text, COLOR);
++iterator;
}
@@ -258,7 +295,7 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
// Finaliza y elimnina todas las notificaciones activas
void Notifier::clearAllNotifications() {
for (auto &notification : notifications_) {
for (auto& notification : notifications_) {
notification.state = NotificationStatus::FINISHED;
}
@@ -268,7 +305,7 @@ void Notifier::clearAllNotifications() {
// Obtiene los códigos de las notificaciones
auto Notifier::getCodes() -> std::vector<std::string> {
std::vector<std::string> codes;
for (const auto &notification : notifications_) {
for (const auto& notification : notifications_) {
codes.emplace_back(notification.code);
}
return codes;