Files
coffee_crisis_arcade_edition/source/service_menu.h
Sergio Valor f1c576beef ja navega pels menus en ServiceMenu
Ja aplica les opcions de video de ServiceMenu
2025-06-05 14:06:42 +02:00

168 lines
6.9 KiB
C++

#pragma once
#include <vector>
#include <utility>
#include <string>
#include <memory>
#include <SDL3/SDL.h>
#include "utils.h"
class Text;
class ServiceMenu
{
public:
// --- Métodos de singleton ---
static void init(); // Inicializa el objeto ServiceMenu
static void destroy(); // Libera el objeto ServiceMenu
static ServiceMenu *get(); // Obtiene el puntero al objeto ServiceMenu
// -- Métodos ---
void toggle();
void render();
void update();
// --- Métodos de control ---
void setSelectorUp();
void setSelectorDown();
void setSelectorRight();
void setSelectorLeft();
void acceptSelection();
void moveBack();
// --- Getters ---
bool isEnabled() const { return enabled_; }
private:
using OptionPairs = std::vector<std::pair<std::string, std::string>>;
enum class SettingsGroup
{
VIDEO, // Configuraciones relacionadas con la calidad y resolución de imagen
AUDIO, // Opciones de sonido y volumen
GAME, // Ajustes de jugabilidad y mecánicas
SYSTEM, // Preferencias generales y configuraciones del sistema
MAIN // Raíz
};
enum class OptionBehavior
{
ADJUST, // Modificable con izquierda/derecha
SELECT // Activable con ENTER
};
enum class ValueType
{
BOOL,
INT,
FOLDER,
NONE
};
struct OptionEntry
{
std::string caption; // Texto visible en el menú
SettingsGroup group; // Categoría de la opción
OptionBehavior behavior; // Cómo se interactúa con la opción
void *linked_variable; // Puntero a la variable que controla la opción
ValueType type; // Tipo de la variable (bool, int, folder, none)
int min_value; // Valor mínimo (solo aplicable si type == INT)
int max_value; // Valor máximo (solo aplicable si type == INT)
int step_value; // Incremento al modificar la opción (solo aplicable si type == INT)
SettingsGroup target_group; // Grupo al que hace referencia la opción si es de tipo FOLDER
// Constructor para opciones de tipo BOOL, NONE o FOLDER
OptionEntry(std::string cap, SettingsGroup grp, OptionBehavior beh, void *var, ValueType t)
: caption(cap), group(grp), behavior(beh), linked_variable(var), type(t),
min_value(0), max_value(0), step_value(0), target_group(SettingsGroup::SYSTEM) {}
// Constructor para opciones de tipo INT con valores mínimos, máximos e incremento
OptionEntry(std::string cap, SettingsGroup grp, OptionBehavior beh, void *var, ValueType t, int min, int max, int step)
: caption(cap), group(grp), behavior(beh), linked_variable(var), type(t),
min_value(min), max_value(max), step_value(step), target_group(SettingsGroup::SYSTEM) {}
// Constructor para opciones de tipo FOLDER que referencian otro grupo
OptionEntry(std::string cap, SettingsGroup grp, OptionBehavior beh, SettingsGroup tgtGrp)
: caption(cap), group(grp), behavior(beh), linked_variable(nullptr), type(ValueType::FOLDER),
min_value(0), max_value(0), step_value(0), target_group(tgtGrp) {}
// Método para modificar el valor
void adjustValue(bool increase)
{
if (linked_variable)
{
if (type == ValueType::INT)
{
int &value = *(static_cast<int *>(linked_variable));
int newValue = increase ? value + step_value : value - step_value;
// Asegurar que el nuevo valor se mantenga dentro de los límites
value = std::clamp(newValue, min_value, max_value);
}
else if (type == ValueType::BOOL)
{
bool &value = *(static_cast<bool *>(linked_variable));
value = !value; // Invierte el valor booleano
}
}
}
// Método para obtener el valor como string
std::string getValueAsString() const
{
switch (type)
{
case ValueType::BOOL:
return (*(static_cast<bool *>(linked_variable))) ? "ON" : "OFF";
case ValueType::INT:
return std::to_string(*(static_cast<int *>(linked_variable)));
default:
return "";
}
}
};
// -- Variables internas ---
bool enabled_ = false; // Indica si el menú de servicio está activo
SDL_FRect rect_; // Rectangulo para definir el area del menú de servicio
std::shared_ptr<Text> element_text_; // Objeto para escribir texto;
std::shared_ptr<Text> title_text_; // Objeto para escribir texto;
size_t selected_ = 0; // Elemento del menú seleccionado
Uint32 counter_ = 0; // Contador interno
std::vector<OptionEntry> options_; // Listado con todas las opciones del menú de servicio
std::vector<OptionEntry> display_options_; // Listado con todas las opciones del menú de servicio que se estan mostrando
OptionPairs option_pairs_; // Listado con las opciones de menu actuales (filtradas por grupo)
SettingsGroup current_settings_group_; // Grupo actual
SettingsGroup previous_settings_group_; // Grupo anterior
// -- Aspecto --
Color bg_color_ = SERV_MENU_BG_COLOR; // Color de fondo
Color title_color_ = SERV_MENU_TITLE_COLOR; // Color del título del menu
Color text_color_ = SERV_MENU_TEXT_COLOR; // Color para el texto de los elementos
Color selected_color_ = SERV_MENU_SELECTED_COLOR; // Color para el elemento seleccionado
int width_; // Ancho del menú
int height_; // Alto del menu
int line_height_; // Espacio entre elementos del menu
// -- Métodos internos ---
void setAnchors(); // Establece el valor de las variables de anclaje
void updateCounter(); // Actualiza el contador interno
Color getSelectedColor(); // Devuelve el color del elemento seleccionado
void initializeOptions(); // Crea todas las opciones del menú de servicio
OptionPairs getOptionPairs(SettingsGroup group) const;
std::vector<OptionEntry> getOptionsByGroup(SettingsGroup group) const;
void applySettings(SettingsGroup group);
void updateMenu(SettingsGroup group);
void reset();
// --- Patrón Singleton ---
ServiceMenu(); // Constructor privado
~ServiceMenu() = default; // Destructor privado
ServiceMenu(const ServiceMenu &) = delete; // Evitar copia
ServiceMenu &operator=(const ServiceMenu &) = delete; // Evitar asignación
// --- Singleton ---
static ServiceMenu *instance_;
};