clang-tidy readability-function-cognitive-complexity

clang-format
This commit is contained in:
2025-07-20 16:12:27 +02:00
parent f2915aa4b4
commit 2620a76865
56 changed files with 2376 additions and 2295 deletions

View File

@@ -15,161 +15,161 @@
// --- Interfaz Base para todas las Opciones del Menú ---
class MenuOption {
public:
enum class Behavior {
ADJUST,
SELECT
};
public:
enum class Behavior {
ADJUST,
SELECT
};
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
: caption_(std::move(caption)), group_(group), hidden_(hidden) {}
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
: caption_(std::move(caption)), group_(group), hidden_(hidden) {}
virtual ~MenuOption() = default;
virtual ~MenuOption() = default;
[[nodiscard]] auto getCaption() const -> const std::string & { return caption_; }
[[nodiscard]] auto getGroup() const -> ServiceMenu::SettingsGroup { return group_; }
[[nodiscard]] auto isHidden() const -> bool { return hidden_; }
void setHidden(bool hidden) { hidden_ = hidden; }
[[nodiscard]] auto getCaption() const -> const std::string & { return caption_; }
[[nodiscard]] auto getGroup() const -> ServiceMenu::SettingsGroup { return group_; }
[[nodiscard]] auto isHidden() const -> bool { return hidden_; }
void setHidden(bool hidden) { hidden_ = hidden; }
[[nodiscard]] virtual auto getBehavior() const -> Behavior = 0;
[[nodiscard]] virtual auto getValueAsString() const -> std::string { return ""; }
virtual void adjustValue(bool adjust_up) {}
[[nodiscard]] virtual auto getTargetGroup() const -> ServiceMenu::SettingsGroup { return ServiceMenu::SettingsGroup::MAIN; }
virtual void executeAction() {}
[[nodiscard]] virtual auto getBehavior() const -> Behavior = 0;
[[nodiscard]] virtual auto getValueAsString() const -> std::string { return ""; }
virtual void adjustValue(bool adjust_up) {}
[[nodiscard]] virtual auto getTargetGroup() const -> ServiceMenu::SettingsGroup { return ServiceMenu::SettingsGroup::MAIN; }
virtual void executeAction() {}
// Método virtual para que cada opción calcule el ancho de su valor más largo.
virtual auto getMaxValueWidth(Text *text_renderer) const -> int { return 0; }
// Método virtual para que cada opción calcule el ancho de su valor más largo.
virtual auto getMaxValueWidth(Text *text_renderer) const -> int { return 0; }
protected:
std::string caption_;
ServiceMenu::SettingsGroup group_;
bool hidden_;
protected:
std::string caption_;
ServiceMenu::SettingsGroup group_;
bool hidden_;
};
// --- Clases Derivadas ---
class BoolOption : public MenuOption {
public:
BoolOption(const std::string &cap, ServiceMenu::SettingsGroup grp, bool *var)
: MenuOption(cap, grp), linked_variable_(var) {}
public:
BoolOption(const std::string &cap, ServiceMenu::SettingsGroup grp, bool *var)
: MenuOption(cap, grp), linked_variable_(var) {}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::ADJUST; }
[[nodiscard]] auto getValueAsString() const -> std::string override {
return *linked_variable_ ? Lang::getText("[SERVICE_MENU] ON") : Lang::getText("[SERVICE_MENU] OFF");
}
void adjustValue(bool /*adjust_up*/) override {
*linked_variable_ = !*linked_variable_;
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
return std::max(
text_renderer->lenght(Lang::getText("[SERVICE_MENU] ON"), -2),
text_renderer->lenght(Lang::getText("[SERVICE_MENU] OFF"), -2));
}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::ADJUST; }
[[nodiscard]] auto getValueAsString() const -> std::string override {
return *linked_variable_ ? Lang::getText("[SERVICE_MENU] ON") : Lang::getText("[SERVICE_MENU] OFF");
}
void adjustValue(bool /*adjust_up*/) override {
*linked_variable_ = !*linked_variable_;
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
return std::max(
text_renderer->lenght(Lang::getText("[SERVICE_MENU] ON"), -2),
text_renderer->lenght(Lang::getText("[SERVICE_MENU] OFF"), -2));
}
private:
bool *linked_variable_;
private:
bool *linked_variable_;
};
class IntOption : public MenuOption {
public:
IntOption(const std::string &cap, ServiceMenu::SettingsGroup grp, int *var, int min, int max, int step)
: MenuOption(cap, grp), linked_variable_(var), min_value_(min), max_value_(max), step_value_(step) {}
public:
IntOption(const std::string &cap, ServiceMenu::SettingsGroup grp, int *var, int min, int max, int step)
: MenuOption(cap, grp), linked_variable_(var), min_value_(min), max_value_(max), step_value_(step) {}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::ADJUST; }
[[nodiscard]] auto getValueAsString() const -> std::string override { return std::to_string(*linked_variable_); }
void adjustValue(bool adjust_up) override {
int new_value = *linked_variable_ + (adjust_up ? step_value_ : -step_value_);
*linked_variable_ = std::clamp(new_value, min_value_, max_value_);
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
int max_width = 0;
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::ADJUST; }
[[nodiscard]] auto getValueAsString() const -> std::string override { return std::to_string(*linked_variable_); }
void adjustValue(bool adjust_up) override {
int new_value = *linked_variable_ + (adjust_up ? step_value_ : -step_value_);
*linked_variable_ = std::clamp(new_value, min_value_, max_value_);
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
int max_width = 0;
// Iterar por todos los valores posibles en el rango
for (int value = min_value_; value <= max_value_; value += step_value_) {
int width = text_renderer->lenght(std::to_string(value), -2);
max_width = std::max(max_width, width);
// Iterar por todos los valores posibles en el rango
for (int value = min_value_; value <= max_value_; value += step_value_) {
int width = text_renderer->lenght(std::to_string(value), -2);
max_width = std::max(max_width, width);
}
return max_width;
}
return max_width;
}
private:
int *linked_variable_;
int min_value_, max_value_, step_value_;
private:
int *linked_variable_;
int min_value_, max_value_, step_value_;
};
class ListOption : public MenuOption {
public:
ListOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::vector<std::string> values, std::function<std::string()> current_value_getter, std::function<void(const std::string &)> new_value_setter)
: MenuOption(cap, grp),
value_list_(std::move(values)),
getter_(std::move(current_value_getter)),
setter_(std::move(new_value_setter)) {
sync();
}
public:
ListOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::vector<std::string> values, std::function<std::string()> current_value_getter, std::function<void(const std::string &)> new_value_setter)
: MenuOption(cap, grp),
value_list_(std::move(values)),
getter_(std::move(current_value_getter)),
setter_(std::move(new_value_setter)) {
sync();
}
void sync() {
std::string current_value = getter_();
for (size_t i = 0; i < value_list_.size(); ++i) {
if (value_list_[i] == current_value) {
list_index_ = i;
return;
void sync() {
std::string current_value = getter_();
for (size_t i = 0; i < value_list_.size(); ++i) {
if (value_list_[i] == current_value) {
list_index_ = i;
return;
}
}
}
}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::ADJUST; }
[[nodiscard]] auto getValueAsString() const -> std::string override {
return value_list_.empty() ? "" : value_list_[list_index_];
}
void adjustValue(bool adjust_up) override {
if (value_list_.empty()) {
return;
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::ADJUST; }
[[nodiscard]] auto getValueAsString() const -> std::string override {
return value_list_.empty() ? "" : value_list_[list_index_];
}
size_t size = value_list_.size();
list_index_ = (adjust_up) ? (list_index_ + 1) % size
: (list_index_ + size - 1) % size;
setter_(value_list_[list_index_]);
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
int max_w = 0;
for (const auto &val : value_list_) {
max_w = std::max(max_w, text_renderer->lenght(val, -2));
void adjustValue(bool adjust_up) override {
if (value_list_.empty()) {
return;
}
size_t size = value_list_.size();
list_index_ = (adjust_up) ? (list_index_ + 1) % size
: (list_index_ + size - 1) % size;
setter_(value_list_[list_index_]);
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
int max_w = 0;
for (const auto &val : value_list_) {
max_w = std::max(max_w, text_renderer->lenght(val, -2));
}
return max_w;
}
return max_w;
}
private:
std::vector<std::string> value_list_;
std::function<std::string()> getter_;
std::function<void(const std::string &)> setter_;
size_t list_index_{0};
private:
std::vector<std::string> value_list_;
std::function<std::string()> getter_;
std::function<void(const std::string &)> setter_;
size_t list_index_{0};
};
class FolderOption : public MenuOption {
public:
FolderOption(const std::string &cap, ServiceMenu::SettingsGroup grp, ServiceMenu::SettingsGroup target)
: MenuOption(cap, grp), target_group_(target) {}
public:
FolderOption(const std::string &cap, ServiceMenu::SettingsGroup grp, ServiceMenu::SettingsGroup target)
: MenuOption(cap, grp), target_group_(target) {}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::SELECT; }
[[nodiscard]] auto getTargetGroup() const -> ServiceMenu::SettingsGroup override { return target_group_; }
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::SELECT; }
[[nodiscard]] auto getTargetGroup() const -> ServiceMenu::SettingsGroup override { return target_group_; }
private:
ServiceMenu::SettingsGroup target_group_;
private:
ServiceMenu::SettingsGroup target_group_;
};
class ActionOption : public MenuOption {
public:
ActionOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::function<void()> action, bool hidden = false)
: MenuOption(cap, grp, hidden), action_(std::move(action)) {}
public:
ActionOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::function<void()> action, bool hidden = false)
: MenuOption(cap, grp, hidden), action_(std::move(action)) {}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::SELECT; }
void executeAction() override {
if (action_) {
action_();
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::SELECT; }
void executeAction() override {
if (action_) {
action_();
}
}
}
private:
std::function<void()> action_;
private:
std::function<void()> action_;
};

View File

@@ -12,98 +12,98 @@ class MenuOption;
class MenuRenderer; // <-- Nuevo
class ServiceMenu {
public:
enum class SettingsGroup {
VIDEO,
AUDIO,
SETTINGS,
SYSTEM,
MAIN
};
public:
enum class SettingsGroup {
VIDEO,
AUDIO,
SETTINGS,
SYSTEM,
MAIN
};
enum class GroupAlignment {
CENTERED,
LEFT
};
enum class GroupAlignment {
CENTERED,
LEFT
};
// --- Constantes públicas que el Renderer podría necesitar ---
static constexpr size_t OPTIONS_HORIZONTAL_PADDING = 20;
static constexpr size_t MIN_WIDTH = 240;
static constexpr size_t MIN_GAP_OPTION_VALUE = 30;
// --- Constantes públicas que el Renderer podría necesitar ---
static constexpr size_t OPTIONS_HORIZONTAL_PADDING = 20;
static constexpr size_t MIN_WIDTH = 240;
static constexpr size_t MIN_GAP_OPTION_VALUE = 30;
// --- Métodos de singleton ---
static void init();
static void destroy();
static auto get() -> ServiceMenu *;
ServiceMenu(const ServiceMenu &) = delete;
auto operator=(const ServiceMenu &) -> ServiceMenu & = delete;
// --- Métodos de singleton ---
static void init();
static void destroy();
static auto get() -> ServiceMenu *;
ServiceMenu(const ServiceMenu &) = delete;
auto operator=(const ServiceMenu &) -> ServiceMenu & = delete;
// --- Métodos principales ---
void toggle();
void render();
void update();
void reset();
// --- Métodos principales ---
void toggle();
void render();
void update();
void reset();
// --- Lógica de navegación ---
void setSelectorUp();
void setSelectorDown();
void adjustOption(bool adjust_up);
void selectOption();
void moveBack();
// --- Lógica de navegación ---
void setSelectorUp();
void setSelectorDown();
void adjustOption(bool adjust_up);
void selectOption();
void moveBack();
// --- Getters para que el Renderer pueda leer el estado ---
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; }
[[nodiscard]] auto getTitle() const -> const std::string & { return title_; }
[[nodiscard]] auto getCurrentGroup() const -> SettingsGroup { return current_settings_group_; }
[[nodiscard]] auto getCurrentGroupAlignment() const -> GroupAlignment;
[[nodiscard]] auto getDisplayOptions() const -> const std::vector<MenuOption *> & { return display_options_; }
[[nodiscard]] auto getAllOptions() const -> const std::vector<std::unique_ptr<MenuOption>> & { return options_; }
[[nodiscard]] auto getSelectedIndex() const -> size_t { return selected_; }
[[nodiscard]] auto getOptionPairs() const -> const std::vector<std::pair<std::string, std::string>> & { return option_pairs_; }
[[nodiscard]] auto countOptionsInGroup(SettingsGroup group) const -> size_t;
// --- Getters para que el Renderer pueda leer el estado ---
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; }
[[nodiscard]] auto getTitle() const -> const std::string & { return title_; }
[[nodiscard]] auto getCurrentGroup() const -> SettingsGroup { return current_settings_group_; }
[[nodiscard]] auto getCurrentGroupAlignment() const -> GroupAlignment;
[[nodiscard]] auto getDisplayOptions() const -> const std::vector<MenuOption *> & { return display_options_; }
[[nodiscard]] auto getAllOptions() const -> const std::vector<std::unique_ptr<MenuOption>> & { return options_; }
[[nodiscard]] auto getSelectedIndex() const -> size_t { return selected_; }
[[nodiscard]] auto getOptionPairs() const -> const std::vector<std::pair<std::string, std::string>> & { return option_pairs_; }
[[nodiscard]] auto countOptionsInGroup(SettingsGroup group) const -> size_t;
private:
// --- Lógica de estado del menú (Modelo) ---
bool enabled_ = false;
std::vector<std::unique_ptr<MenuOption>> options_;
std::vector<MenuOption *> display_options_;
std::vector<std::pair<std::string, std::string>> option_pairs_;
private:
// --- Lógica de estado del menú (Modelo) ---
bool enabled_ = false;
std::vector<std::unique_ptr<MenuOption>> options_;
std::vector<MenuOption *> display_options_;
std::vector<std::pair<std::string, std::string>> option_pairs_;
SettingsGroup current_settings_group_;
SettingsGroup previous_settings_group_;
std::string title_;
size_t selected_ = 0;
size_t main_menu_selected_ = 0;
SettingsGroup current_settings_group_;
SettingsGroup previous_settings_group_;
std::string title_;
size_t selected_ = 0;
size_t main_menu_selected_ = 0;
// --- Mensaje de reinicio ---
std::unique_ptr<UIMessage> restart_message_ui_;
bool last_pending_changes_ = false;
// --- Mensaje de reinicio ---
std::unique_ptr<UIMessage> restart_message_ui_;
bool last_pending_changes_ = false;
// --- La Vista ---
std::unique_ptr<MenuRenderer> renderer_;
// --- La Vista ---
std::unique_ptr<MenuRenderer> renderer_;
// --- Métodos de lógica interna ---
void updateDisplayOptions();
void updateOptionPairs();
void initializeOptions();
void updateMenu();
void applySettings();
void applyVideoSettings();
static void applyAudioSettings();
void applySettingsSettings();
[[nodiscard]] auto getOptionByCaption(const std::string &caption) const -> MenuOption *;
void adjustListValues();
static void playMoveSound();
static void playAdjustSound();
static void playSelectSound();
static void playBackSound();
[[nodiscard]] static auto settingsGroupToString(SettingsGroup group) -> std::string;
void setHiddenOptions();
// --- Métodos de lógica interna ---
void updateDisplayOptions();
void updateOptionPairs();
void initializeOptions();
void updateMenu();
void applySettings();
void applyVideoSettings();
static void applyAudioSettings();
void applySettingsSettings();
[[nodiscard]] auto getOptionByCaption(const std::string &caption) const -> MenuOption *;
void adjustListValues();
static void playMoveSound();
static void playAdjustSound();
static void playSelectSound();
static void playBackSound();
[[nodiscard]] static auto settingsGroupToString(SettingsGroup group) -> std::string;
void setHiddenOptions();
// --- Constructores y destructor privados (singleton) ---
ServiceMenu();
~ServiceMenu() = default;
// --- Instancia singleton ---
static ServiceMenu *instance;
// --- Constructores y destructor privados (singleton) ---
ServiceMenu();
~ServiceMenu() = default;
// --- Instancia singleton ---
static ServiceMenu *instance;
};

View File

@@ -15,7 +15,7 @@ void UIMessage::show() {
return; // Ya está visible y quieto
}
start_y_ = DESP; // Empieza 8 píxeles arriba de la posición base
start_y_ = DESP; // Empieza 8 píxeles arriba de la posición base
target_y_ = 0.0F; // La posición final es la base
y_offset_ = start_y_;
anim_step_ = 0;
@@ -30,7 +30,7 @@ void UIMessage::hide() {
}
start_y_ = y_offset_; // Comienza desde la posición actual
target_y_ = DESP; // Termina 8 píxeles arriba de la base
target_y_ = DESP; // Termina 8 píxeles arriba de la base
anim_step_ = 0;
animating_ = true;
}

View File

@@ -9,48 +9,48 @@ class Text;
// Clase para mostrar mensajes animados en la interfaz de usuario
class UIMessage {
public:
// Constructor: recibe el renderizador de texto, el mensaje y el color
UIMessage(std::shared_ptr<Text> text_renderer, std::string message_text, const Color &color);
public:
// Constructor: recibe el renderizador de texto, el mensaje y el color
UIMessage(std::shared_ptr<Text> text_renderer, std::string message_text, const Color &color);
// Muestra el mensaje con animación de entrada
void show();
// Muestra el mensaje con animación de entrada
void show();
// Oculta el mensaje con animación de salida
void hide();
// Oculta el mensaje con animación de salida
void hide();
// Actualiza el estado de la animación (debe llamarse cada frame)
void update();
// Actualiza el estado de la animación (debe llamarse cada frame)
void update();
// Dibuja el mensaje en pantalla si está visible
void render();
// Dibuja el mensaje en pantalla si está visible
void render();
// Indica si el mensaje está visible actualmente
[[nodiscard]] auto isVisible() const -> bool;
// Indica si el mensaje está visible actualmente
[[nodiscard]] auto isVisible() const -> bool;
// Permite actualizar la posición del mensaje (por ejemplo, si el menú se mueve)
void setPosition(float new_base_x, float new_base_y);
// Permite actualizar la posición del mensaje (por ejemplo, si el menú se mueve)
void setPosition(float new_base_x, float new_base_y);
private:
// --- Configuración ---
std::shared_ptr<Text> text_renderer_; // Renderizador de texto
std::string text_; // Texto del mensaje a mostrar
Color color_; // Color del texto
private:
// --- Configuración ---
std::shared_ptr<Text> text_renderer_; // Renderizador de texto
std::string text_; // Texto del mensaje a mostrar
Color color_; // Color del texto
// --- Estado ---
bool visible_ = false; // Indica si el mensaje está visible
bool animating_ = false; // Indica si el mensaje está en proceso de animación
float base_x_ = 0.0F; // Posición X base donde se muestra el mensaje
float base_y_ = 0.0F; // Posición Y base donde se muestra el mensaje
float y_offset_ = 0.0F; // Desplazamiento vertical actual del mensaje (para animación)
// --- Estado ---
bool visible_ = false; // Indica si el mensaje está visible
bool animating_ = false; // Indica si el mensaje está en proceso de animación
float base_x_ = 0.0F; // Posición X base donde se muestra el mensaje
float base_y_ = 0.0F; // Posición Y base donde se muestra el mensaje
float y_offset_ = 0.0F; // Desplazamiento vertical actual del mensaje (para animación)
// --- Animación ---
float start_y_ = 0.0F; // Posición Y inicial de la animación
float target_y_ = 0.0F; // Posición Y objetivo de la animación
int anim_step_ = 0; // Paso actual de la animación
static constexpr int ANIMATION_STEPS = 8; // Número total de pasos de la animación
static constexpr float DESP = -8.0F; // Distancia a desplazarse
// --- Animación ---
float start_y_ = 0.0F; // Posición Y inicial de la animación
float target_y_ = 0.0F; // Posición Y objetivo de la animación
int anim_step_ = 0; // Paso actual de la animación
static constexpr int ANIMATION_STEPS = 8; // Número total de pasos de la animación
static constexpr float DESP = -8.0F; // Distancia a desplazarse
// Actualiza la interpolación de la animación (ease out/in cubic)
void updateAnimation();
// Actualiza la interpolación de la animación (ease out/in cubic)
void updateAnimation();
};