coses de la ia
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <algorithm> // para std::clamp
|
#include <algorithm> // para std::clamp
|
||||||
#include "service_menu.h" // Necesitamos las enums como SettingsGroup
|
#include "service_menu.h" // Necesitamos las enums como SettingsGroup
|
||||||
#include "options.h" // Para acceder a las variables de configuración
|
#include "options.h" // Para acceder a las variables de configuración
|
||||||
#include "lang.h" // Para las traducciones
|
#include "lang.h" // Para las traducciones
|
||||||
@@ -12,25 +12,20 @@
|
|||||||
|
|
||||||
// --- Interfaz Base para todas las Opciones del Menú ---
|
// --- Interfaz Base para todas las Opciones del Menú ---
|
||||||
|
|
||||||
class MenuOption
|
class MenuOption {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
// Enum para el comportamiento, similar al anterior pero más integrado
|
// Enum para el comportamiento, similar al anterior pero más integrado
|
||||||
enum class Behavior
|
enum class Behavior { ADJUST, SELECT };
|
||||||
{
|
|
||||||
ADJUST,
|
|
||||||
SELECT
|
|
||||||
};
|
|
||||||
|
|
||||||
// Constructor base
|
// Constructor base
|
||||||
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
|
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
|
||||||
: caption_(std::move(caption)), group_(group), hidden_(hidden) {}
|
: caption_(std::move(caption)), group_(group), hidden_(hidden) {}
|
||||||
|
|
||||||
// Destructor virtual para permitir la destrucción polimórfica correcta
|
// Destructor virtual para permitir la destrucción polimórfica correcta
|
||||||
virtual ~MenuOption() = default;
|
virtual ~MenuOption() = default;
|
||||||
|
|
||||||
// Métodos comunes que todas las opciones deben tener
|
// Métodos comunes que todas las opciones deben tener
|
||||||
const std::string &getCaption() const { return caption_; }
|
const std::string& getCaption() const { return caption_; }
|
||||||
ServiceMenu::SettingsGroup getGroup() const { return group_; }
|
ServiceMenu::SettingsGroup getGroup() const { return group_; }
|
||||||
bool isHidden() const { return hidden_; }
|
bool isHidden() const { return hidden_; }
|
||||||
void setHidden(bool hidden) { hidden_ = hidden; }
|
void setHidden(bool hidden) { hidden_ = hidden; }
|
||||||
@@ -38,9 +33,9 @@ public:
|
|||||||
// Métodos virtuales que las clases derivadas implementarán según su naturaleza
|
// Métodos virtuales que las clases derivadas implementarán según su naturaleza
|
||||||
virtual Behavior getBehavior() const = 0;
|
virtual Behavior getBehavior() const = 0;
|
||||||
virtual std::string getValueAsString() const { return ""; }
|
virtual std::string getValueAsString() const { return ""; }
|
||||||
virtual void adjustValue(bool adjust_up) {} // Implementado por opciones ajustables
|
virtual void adjustValue(bool adjust_up) {} // Implementado por opciones ajustables
|
||||||
virtual ServiceMenu::SettingsGroup getTargetGroup() const { return ServiceMenu::SettingsGroup::MAIN; } // Implementado por FolderOption
|
virtual ServiceMenu::SettingsGroup getTargetGroup() const { return ServiceMenu::SettingsGroup::MAIN; } // Implementado por FolderOption
|
||||||
virtual void executeAction() {} // Implementado por ActionOption
|
virtual void executeAction() {} // Implementado por ActionOption
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string caption_;
|
std::string caption_;
|
||||||
@@ -48,71 +43,67 @@ protected:
|
|||||||
bool hidden_;
|
bool hidden_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// --- Clases Derivadas ---
|
// --- Clases Derivadas ---
|
||||||
|
|
||||||
// 1. Opción Booleana (On/Off)
|
// 1. Opción Booleana (On/Off) - Sin cambios
|
||||||
class BoolOption : public MenuOption
|
class BoolOption : public MenuOption {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
BoolOption(const std::string &cap, ServiceMenu::SettingsGroup grp, bool *var)
|
BoolOption(const std::string& cap, ServiceMenu::SettingsGroup grp, bool* var)
|
||||||
: MenuOption(cap, grp), linked_variable_(var) {}
|
: MenuOption(cap, grp), linked_variable_(var) {}
|
||||||
|
|
||||||
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
||||||
std::string getValueAsString() const override
|
std::string getValueAsString() const override {
|
||||||
{
|
|
||||||
return *linked_variable_ ? Lang::getText("[SERVICE_MENU] ON") : Lang::getText("[SERVICE_MENU] OFF");
|
return *linked_variable_ ? Lang::getText("[SERVICE_MENU] ON") : Lang::getText("[SERVICE_MENU] OFF");
|
||||||
}
|
}
|
||||||
void adjustValue(bool /*adjust_up*/) override
|
void adjustValue(bool /*adjust_up*/) override {
|
||||||
{
|
|
||||||
*linked_variable_ = !*linked_variable_;
|
*linked_variable_ = !*linked_variable_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool *linked_variable_;
|
bool* linked_variable_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 2. Opción de Entero (Volumen, Tamaño Ventana, etc.)
|
// 2. Opción de Entero (Volumen, Tamaño Ventana, etc.) - Sin cambios
|
||||||
class IntOption : public MenuOption
|
class IntOption : public MenuOption {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
IntOption(const std::string &cap, ServiceMenu::SettingsGroup grp, int *var, int min, int max, int step)
|
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) {}
|
: MenuOption(cap, grp), linked_variable_(var), min_value_(min), max_value_(max), step_value_(step) {}
|
||||||
|
|
||||||
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
||||||
std::string getValueAsString() const override { return std::to_string(*linked_variable_); }
|
std::string getValueAsString() const override { return std::to_string(*linked_variable_); }
|
||||||
void adjustValue(bool adjust_up) override
|
void adjustValue(bool adjust_up) override {
|
||||||
{
|
|
||||||
int newValue = *linked_variable_ + (adjust_up ? step_value_ : -step_value_);
|
int newValue = *linked_variable_ + (adjust_up ? step_value_ : -step_value_);
|
||||||
*linked_variable_ = std::clamp(newValue, min_value_, max_value_);
|
*linked_variable_ = std::clamp(newValue, min_value_, max_value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int *linked_variable_;
|
int* linked_variable_;
|
||||||
int min_value_, max_value_, step_value_;
|
int min_value_, max_value_, step_value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 3. Opción de Lista (Idioma, Dificultad)
|
// 3. Opción de Lista (Idioma, Dificultad) - *** CORREGIDO ***
|
||||||
class ListOption : public MenuOption
|
class ListOption : public MenuOption {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
ListOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::string *var, std::vector<std::string> values)
|
// El constructor ahora es más abstracto. Acepta funciones para obtener y establecer el valor.
|
||||||
: MenuOption(cap, grp), linked_variable_(var), value_list_(std::move(values)), list_index_(0) {}
|
ListOption(const std::string& cap, ServiceMenu::SettingsGroup grp,
|
||||||
|
std::vector<std::string> values,
|
||||||
void sync()
|
std::function<std::string()> current_value_getter,
|
||||||
{ // Sincroniza el índice con el valor actual de la variable
|
std::function<void(const std::string&)> new_value_setter)
|
||||||
for (size_t i = 0; i < value_list_.size(); ++i)
|
: MenuOption(cap, grp),
|
||||||
{
|
value_list_(std::move(values)),
|
||||||
std::string code;
|
getter_(std::move(current_value_getter)),
|
||||||
if (linked_variable_ == &Options::pending_changes.new_language)
|
setter_(std::move(new_value_setter)),
|
||||||
{
|
list_index_(0)
|
||||||
code = Lang::getCodeFromName(value_list_[i]);
|
{
|
||||||
}
|
sync(); // Sincroniza el índice con el valor actual al momento de la creación.
|
||||||
else if (linked_variable_ == &Options::pending_changes.new_difficulty)
|
}
|
||||||
{
|
|
||||||
code = Options::getDifficultyCodeFromName(value_list_[i]);
|
// Se mantiene pública en caso de que necesitemos resincronizar desde fuera.
|
||||||
}
|
void sync() {
|
||||||
if (code == *linked_variable_)
|
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;
|
list_index_ = i;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -120,62 +111,51 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
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
|
std::string getValueAsString() const override {
|
||||||
{
|
return value_list_.empty() ? "" : value_list_[list_index_];
|
||||||
if (value_list_.empty())
|
}
|
||||||
return;
|
|
||||||
|
void adjustValue(bool adjust_up) override {
|
||||||
|
if (value_list_.empty()) return;
|
||||||
size_t size = value_list_.size();
|
size_t size = value_list_.size();
|
||||||
list_index_ = (adjust_up) ? (list_index_ + 1) % size
|
list_index_ = (adjust_up) ? (list_index_ + 1) % size
|
||||||
: (list_index_ + size - 1) % size;
|
: (list_index_ + size - 1) % size;
|
||||||
|
|
||||||
// Actualiza la variable real y comprueba cambios pendientes
|
// Usa la función setter para actualizar la variable de configuración real.
|
||||||
if (linked_variable_ == &Options::pending_changes.new_language)
|
setter_(value_list_[list_index_]);
|
||||||
{
|
|
||||||
*linked_variable_ = Lang::getCodeFromName(value_list_[list_index_]);
|
|
||||||
}
|
|
||||||
else if (linked_variable_ == &Options::pending_changes.new_difficulty)
|
|
||||||
{
|
|
||||||
*linked_variable_ = Options::getDifficultyCodeFromName(value_list_[list_index_]);
|
|
||||||
}
|
|
||||||
Options::checkPendingChanges();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string *linked_variable_;
|
|
||||||
std::vector<std::string> value_list_;
|
std::vector<std::string> value_list_;
|
||||||
|
std::function<std::string()> getter_;
|
||||||
|
std::function<void(const std::string&)> setter_;
|
||||||
size_t list_index_;
|
size_t list_index_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 4. Opción Carpeta (Navega a otro sub-menú)
|
|
||||||
class FolderOption : public MenuOption
|
// 4. Opción Carpeta (Navega a otro sub-menú) - Sin cambios
|
||||||
{
|
class FolderOption : public MenuOption {
|
||||||
public:
|
public:
|
||||||
FolderOption(const std::string &cap, ServiceMenu::SettingsGroup grp, ServiceMenu::SettingsGroup target)
|
FolderOption(const std::string& cap, ServiceMenu::SettingsGroup grp, ServiceMenu::SettingsGroup target)
|
||||||
: MenuOption(cap, grp), target_group_(target) {}
|
: MenuOption(cap, grp), target_group_(target) {}
|
||||||
|
|
||||||
Behavior getBehavior() const override { return Behavior::SELECT; }
|
Behavior getBehavior() const override { return Behavior::SELECT; }
|
||||||
ServiceMenu::SettingsGroup getTargetGroup() const override { return target_group_; }
|
ServiceMenu::SettingsGroup getTargetGroup() const override { return target_group_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ServiceMenu::SettingsGroup target_group_;
|
ServiceMenu::SettingsGroup target_group_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 5. Opción de Acción (Ejecuta una función)
|
// 5. Opción de Acción (Ejecuta una función) - Sin cambios
|
||||||
class ActionOption : public MenuOption
|
class ActionOption : public MenuOption {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
// Usamos std::function para poder pasar cualquier función/lambda
|
ActionOption(const std::string& cap, ServiceMenu::SettingsGroup grp, std::function<void()> action, bool hidden = false)
|
||||||
ActionOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::function<void()> action, bool hidden = false)
|
|
||||||
: MenuOption(cap, grp, hidden), action_(std::move(action)) {}
|
: MenuOption(cap, grp, hidden), action_(std::move(action)) {}
|
||||||
|
|
||||||
Behavior getBehavior() const override { return Behavior::SELECT; }
|
Behavior getBehavior() const override { return Behavior::SELECT; }
|
||||||
void executeAction() override
|
void executeAction() override {
|
||||||
{
|
if (action_) action_();
|
||||||
if (action_)
|
|
||||||
action_();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::function<void()> action_;
|
std::function<void()> action_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -93,6 +93,18 @@ namespace Lang
|
|||||||
return languages[0].code;
|
return languages[0].code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Devuelve el nombre de un idioma a partir de un código
|
||||||
|
std::string getNameFromCode(Code code)
|
||||||
|
{
|
||||||
|
for (const auto& lang : languages)
|
||||||
|
{
|
||||||
|
if (lang.code == code)
|
||||||
|
return lang.name;
|
||||||
|
}
|
||||||
|
// Si no se encuentra, devuelve el nombre del primer idioma por defecto
|
||||||
|
return languages[0].name;
|
||||||
|
}
|
||||||
|
|
||||||
// Actualiza los nombres de los idiomas
|
// Actualiza los nombres de los idiomas
|
||||||
void updateLanguageNames()
|
void updateLanguageNames()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ namespace Lang
|
|||||||
// Devuelve el código de un idioma a partir de un nombre
|
// Devuelve el código de un idioma a partir de un nombre
|
||||||
Code getCodeFromName(const std::string& name);
|
Code getCodeFromName(const std::string& name);
|
||||||
|
|
||||||
|
// Devuelve el nombre de un idioma a partir de un código
|
||||||
|
std::string getNameFromCode(Code code);
|
||||||
|
|
||||||
// Actualiza los nombres de los idiomas
|
// Actualiza los nombres de los idiomas
|
||||||
void updateLanguageNames();
|
void updateLanguageNames();
|
||||||
|
|
||||||
|
|||||||
@@ -431,4 +431,15 @@ namespace Options
|
|||||||
// Si no se encuentra, devuelve el primero por defecto
|
// Si no se encuentra, devuelve el primero por defecto
|
||||||
return difficulties[0].code;
|
return difficulties[0].code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getDifficultyNameFromCode(DifficultyCode code)
|
||||||
|
{
|
||||||
|
for (const auto &difficulty : difficulties)
|
||||||
|
{
|
||||||
|
if (difficulty.code == code)
|
||||||
|
return difficulty.name;
|
||||||
|
}
|
||||||
|
// Si no se encuentra, devuelve el nombre del primero por defecto
|
||||||
|
return difficulties[0].name;
|
||||||
|
}
|
||||||
} // namespace Options
|
} // namespace Options
|
||||||
@@ -139,4 +139,5 @@ namespace Options
|
|||||||
void applyPendingChanges(); // Aplica los cambios pendientes copiando los valores a sus variables
|
void applyPendingChanges(); // Aplica los cambios pendientes copiando los valores a sus variables
|
||||||
void checkPendingChanges();
|
void checkPendingChanges();
|
||||||
DifficultyCode getDifficultyCodeFromName(const std::string& name);
|
DifficultyCode getDifficultyCodeFromName(const std::string& name);
|
||||||
|
std::string getDifficultyNameFromCode(DifficultyCode code);
|
||||||
} // namespace Options
|
} // namespace Options
|
||||||
@@ -48,10 +48,35 @@ void ServiceMenu::initializeOptions()
|
|||||||
|
|
||||||
// --- Settings ---
|
// --- Settings ---
|
||||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] AUTOFIRE"), SettingsGroup::SETTINGS, &Options::settings.autofire));
|
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] AUTOFIRE"), SettingsGroup::SETTINGS, &Options::settings.autofire));
|
||||||
options_.push_back(std::make_unique<ListOption>(Lang::getText("[SERVICE_MENU] LANGUAGE"), SettingsGroup::SETTINGS, &Options::pending_changes.new_language,
|
// Opción de Idioma
|
||||||
std::vector<std::string>{Lang::getText("[SERVICE_MENU] LANG_ES"), Lang::getText("[SERVICE_MENU] LANG_BA"), Lang::getText("[SERVICE_MENU] LANG_EN")}));
|
options_.push_back(std::make_unique<ListOption>(
|
||||||
options_.push_back(std::make_unique<ListOption>(Lang::getText("[SERVICE_MENU] DIFFICULTY"), SettingsGroup::SETTINGS, &Options::pending_changes.new_difficulty,
|
Lang::getText("[SERVICE_MENU] LANGUAGE"),
|
||||||
std::vector<std::string>{Lang::getText("[SERVICE_MENU] EASY"), Lang::getText("[SERVICE_MENU] NORMAL"), Lang::getText("[SERVICE_MENU] HARD")}));
|
SettingsGroup::SETTINGS,
|
||||||
|
std::vector<std::string>{Lang::getText("[SERVICE_MENU] LANG_ES"), Lang::getText("[SERVICE_MENU] LANG_BA"), Lang::getText("[SERVICE_MENU] LANG_EN")},
|
||||||
|
// Getter: Devuelve el nombre del idioma actual
|
||||||
|
[]()
|
||||||
|
{ return Lang::getNameFromCode(Options::pending_changes.new_language); },
|
||||||
|
// Setter: Establece el nuevo idioma a partir de su nombre
|
||||||
|
[](const std::string &val)
|
||||||
|
{
|
||||||
|
Options::pending_changes.new_language = Lang::getCodeFromName(val);
|
||||||
|
Options::checkPendingChanges();
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Opción de Dificultad
|
||||||
|
options_.push_back(std::make_unique<ListOption>(
|
||||||
|
Lang::getText("[SERVICE_MENU] DIFFICULTY"),
|
||||||
|
SettingsGroup::SETTINGS,
|
||||||
|
std::vector<std::string>{Lang::getText("[SERVICE_MENU] EASY"), Lang::getText("[SERVICE_MENU] NORMAL"), Lang::getText("[SERVICE_MENU] HARD")},
|
||||||
|
// Getter: Devuelve el nombre de la dificultad actual
|
||||||
|
[]()
|
||||||
|
{ return Options::getDifficultyNameFromCode(Options::pending_changes.new_difficulty); },
|
||||||
|
// Setter: Establece la nueva dificultad a partir de su nombre
|
||||||
|
[](const std::string &val)
|
||||||
|
{
|
||||||
|
Options::pending_changes.new_difficulty = Options::getDifficultyCodeFromName(val);
|
||||||
|
Options::checkPendingChanges();
|
||||||
|
}));
|
||||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] ENABLE_SHUTDOWN"), SettingsGroup::SETTINGS, &Options::settings.shutdown_enabled));
|
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] ENABLE_SHUTDOWN"), SettingsGroup::SETTINGS, &Options::settings.shutdown_enabled));
|
||||||
|
|
||||||
// --- System (con lambdas para las acciones) ---
|
// --- System (con lambdas para las acciones) ---
|
||||||
|
|||||||
Reference in New Issue
Block a user