ServiceMenu: afegit polimorfisme per a les diferents opcions del menu
This commit is contained in:
@@ -9,33 +9,34 @@
|
||||
#include "options.h" // Para acceder a las variables de configuración
|
||||
#include "lang.h" // Para las traducciones
|
||||
#include "section.h" // Para las acciones como Quit o Reset
|
||||
#include "text.h" // Para poder calcular el ancho del texto
|
||||
|
||||
// --- Interfaz Base para todas las Opciones del Menú ---
|
||||
|
||||
class MenuOption {
|
||||
public:
|
||||
// Enum para el comportamiento, similar al anterior pero más integrado
|
||||
enum class Behavior { ADJUST, SELECT };
|
||||
|
||||
// Constructor base
|
||||
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
|
||||
: caption_(std::move(caption)), group_(group), hidden_(hidden) {}
|
||||
|
||||
// Destructor virtual para permitir la destrucción polimórfica correcta
|
||||
virtual ~MenuOption() = default;
|
||||
|
||||
// Métodos comunes que todas las opciones deben tener
|
||||
const std::string& getCaption() const { return caption_; }
|
||||
ServiceMenu::SettingsGroup getGroup() const { return group_; }
|
||||
bool isHidden() const { return hidden_; }
|
||||
void setHidden(bool hidden) { hidden_ = hidden; }
|
||||
|
||||
// Métodos virtuales que las clases derivadas implementarán según su naturaleza
|
||||
virtual Behavior getBehavior() const = 0;
|
||||
virtual std::string getValueAsString() const { return ""; }
|
||||
virtual void adjustValue(bool adjust_up) {} // Implementado por opciones ajustables
|
||||
virtual ServiceMenu::SettingsGroup getTargetGroup() const { return ServiceMenu::SettingsGroup::MAIN; } // Implementado por FolderOption
|
||||
virtual void executeAction() {} // Implementado por ActionOption
|
||||
virtual void adjustValue(bool adjust_up) {}
|
||||
virtual ServiceMenu::SettingsGroup getTargetGroup() const { return ServiceMenu::SettingsGroup::MAIN; }
|
||||
virtual void executeAction() {}
|
||||
|
||||
// --- AÑADIDO ---
|
||||
// Método virtual para que cada opción calcule el ancho de su valor más largo.
|
||||
virtual int getMaxValueWidth(Text* text_renderer) const { return 0; }
|
||||
// --- FIN AÑADIDO ---
|
||||
|
||||
protected:
|
||||
std::string caption_;
|
||||
@@ -46,7 +47,6 @@ protected:
|
||||
|
||||
// --- Clases Derivadas ---
|
||||
|
||||
// 1. Opción Booleana (On/Off) - Sin cambios
|
||||
class BoolOption : public MenuOption {
|
||||
public:
|
||||
BoolOption(const std::string& cap, ServiceMenu::SettingsGroup grp, bool* var)
|
||||
@@ -59,11 +59,18 @@ public:
|
||||
void adjustValue(bool /*adjust_up*/) override {
|
||||
*linked_variable_ = !*linked_variable_;
|
||||
}
|
||||
// --- AÑADIDO ---
|
||||
int getMaxValueWidth(Text* text_renderer) const override {
|
||||
return std::max(
|
||||
text_renderer->lenght(Lang::getText("[SERVICE_MENU] ON"), -2),
|
||||
text_renderer->lenght(Lang::getText("[SERVICE_MENU] OFF"), -2)
|
||||
);
|
||||
}
|
||||
// --- FIN AÑADIDO ---
|
||||
private:
|
||||
bool* linked_variable_;
|
||||
};
|
||||
|
||||
// 2. Opción de Entero (Volumen, Tamaño Ventana, etc.) - Sin cambios
|
||||
class IntOption : public MenuOption {
|
||||
public:
|
||||
IntOption(const std::string& cap, ServiceMenu::SettingsGroup grp, int* var, int min, int max, int step)
|
||||
@@ -75,15 +82,21 @@ public:
|
||||
int newValue = *linked_variable_ + (adjust_up ? step_value_ : -step_value_);
|
||||
*linked_variable_ = std::clamp(newValue, min_value_, max_value_);
|
||||
}
|
||||
// --- AÑADIDO ---
|
||||
int getMaxValueWidth(Text* text_renderer) const override {
|
||||
return std::max(
|
||||
text_renderer->lenght(std::to_string(min_value_), -2),
|
||||
text_renderer->lenght(std::to_string(max_value_), -2)
|
||||
);
|
||||
}
|
||||
// --- FIN AÑADIDO ---
|
||||
private:
|
||||
int* linked_variable_;
|
||||
int min_value_, max_value_, step_value_;
|
||||
};
|
||||
|
||||
// 3. Opción de Lista (Idioma, Dificultad) - *** CORREGIDO ***
|
||||
class ListOption : public MenuOption {
|
||||
public:
|
||||
// El constructor ahora es más abstracto. Acepta funciones para obtener y establecer el valor.
|
||||
ListOption(const std::string& cap, ServiceMenu::SettingsGroup grp,
|
||||
std::vector<std::string> values,
|
||||
std::function<std::string()> current_value_getter,
|
||||
@@ -94,15 +107,12 @@ public:
|
||||
setter_(std::move(new_value_setter)),
|
||||
list_index_(0)
|
||||
{
|
||||
sync(); // Sincroniza el índice con el valor actual al momento de la creación.
|
||||
sync();
|
||||
}
|
||||
|
||||
// Se mantiene pública en caso de que necesitemos resincronizar desde fuera.
|
||||
void sync() {
|
||||
std::string current_value = getter_();
|
||||
for (size_t i = 0; i < value_list_.size(); ++i) {
|
||||
// Asume que el getter devuelve un string que coincide con uno de los de la lista.
|
||||
// Para que esto funcione, necesitarás funciones como `Lang::getNameFromCode`.
|
||||
if (value_list_[i] == current_value) {
|
||||
list_index_ = i;
|
||||
return;
|
||||
@@ -111,21 +121,25 @@ public:
|
||||
}
|
||||
|
||||
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
||||
|
||||
std::string getValueAsString() const override {
|
||||
return value_list_.empty() ? "" : value_list_[list_index_];
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Usa la función setter para actualizar la variable de configuración real.
|
||||
setter_(value_list_[list_index_]);
|
||||
}
|
||||
|
||||
// --- AÑADIDO ---
|
||||
int getMaxValueWidth(Text* text_renderer) const 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;
|
||||
}
|
||||
// --- FIN AÑADIDO ---
|
||||
private:
|
||||
std::vector<std::string> value_list_;
|
||||
std::function<std::string()> getter_;
|
||||
@@ -133,8 +147,6 @@ private:
|
||||
size_t list_index_;
|
||||
};
|
||||
|
||||
|
||||
// 4. Opción Carpeta (Navega a otro sub-menú) - Sin cambios
|
||||
class FolderOption : public MenuOption {
|
||||
public:
|
||||
FolderOption(const std::string& cap, ServiceMenu::SettingsGroup grp, ServiceMenu::SettingsGroup target)
|
||||
@@ -146,7 +158,6 @@ private:
|
||||
ServiceMenu::SettingsGroup target_group_;
|
||||
};
|
||||
|
||||
// 5. Opción de Acción (Ejecuta una función) - Sin cambios
|
||||
class ActionOption : public MenuOption {
|
||||
public:
|
||||
ActionOption(const std::string& cap, ServiceMenu::SettingsGroup grp, std::function<void()> action, bool hidden = false)
|
||||
|
||||
Reference in New Issue
Block a user