Files
coffee_crisis_arcade_edition/source/service_menu.h

262 lines
13 KiB
C++

#pragma once
#include <vector>
#include <utility>
#include <string>
#include <memory>
#include <SDL3/SDL.h>
#include "utils.h"
#include "lang.h"
#include "options.h"
class Text;
class ServiceMenu
{
public:
// --- Métodos de Singleton ---
static void init(); // Inicializa la instancia única de ServiceMenu
static void destroy(); // Libera la instancia única de ServiceMenu
static ServiceMenu *get(); // Devuelve el puntero a la instancia única
// --- Métodos principales ---
void toggle(); // Muestra u oculta el menú de servicio
void render(); // Dibuja el menú de servicio en pantalla
void update(); // Actualiza el estado del menú de servicio
void reset(); // Reinicia el menú al estado inicial
// --- Métodos de control de navegación ---
void setSelectorUp(); // Mueve el selector hacia arriba
void setSelectorDown(); // Mueve el selector hacia abajo
void adjustOption(bool adjust_up); // Ajusta el valor de la opción seleccionada
void selectOption(); // Selecciona la opción actual
void moveBack(); // Vuelve al grupo de opciones anterior
// --- Getters ---
bool isEnabled() const { return enabled_; } // Indica si el menú de servicio está activo
// --- Métodos para animación de resize ---
void setResizeAnimationSteps(int steps) { resize_anim_steps_ = steps; }
private:
// --- Tipos internos ---
using OptionPairs = std::vector<std::pair<std::string, std::string>>;
// --- Constantes ---
static constexpr const char *MENU_SOUND_ = "clock.wav"; // Sonido al navegar por el menú
static constexpr size_t OPTIONS_HORIZONTAL_PADDING_ = 20; // Relleno horizontal de las opciones
static constexpr size_t MIN_WIDTH_ = 240; // Anchura mínima del menu
static constexpr size_t MIN_GAP_OPTION_VALUE_ = 20; // Espacio mínimo entre una opción y su valor
// --- Enumeraciones internas ---
enum class Aspect
{
ASPECT1, // Fondo opaco y proyecta sombra
ASPECT2 // Fondo translúcido
};
enum class SettingsGroup
{
VIDEO, // Configuraciones de vídeo
AUDIO, // Opciones de audio
GAME, // Opciones de juego
SYSTEM, // Opciones del sistema
MAIN // Menú principal
};
enum class OptionBehavior
{
ADJUST, // Modificable con izquierda/derecha
SELECT // Activable con ENTER
};
enum class ValueType
{
BOOL, // Valor booleano
INT, // Valor entero
LIST, // Lista de valores
FOLDER, // Referencia a otro grupo
NONE // Sin valor asociado
};
enum class GroupAlignment
{
CENTERED, // Opciones centradas
LEFT // Opciones alineadas a la izquierda
};
// --- Estructura de opción del menú ---
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
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)
std::vector<std::string> value_list; // Lista de valores posibles (solo aplicable si type == LIST)
size_t list_index; // Índice del valor seleccionado dentro de value_list
SettingsGroup target_group; // Grupo al que hace referencia la opción si es de tipo FOLDER
// Constructor para opciones de tipo BOOL, NONE, 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), list_index(0), target_group(SettingsGroup::SYSTEM) {}
// Constructor para opciones de tipo INT
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), list_index(0), target_group(SettingsGroup::SYSTEM) {}
// Constructor para opciones de tipo LIST
OptionEntry(std::string cap, SettingsGroup grp, OptionBehavior beh, void *var, std::vector<std::string> values)
: caption(cap), group(grp), behavior(beh), linked_variable(var), type(ValueType::LIST),
min_value(0), max_value(0), step_value(0), value_list(values), list_index(0), target_group(SettingsGroup::SYSTEM) {}
// Constructor para opciones de tipo FOLDER
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), list_index(0), target_group(tgtGrp) {}
// Método para modificar el valor de la opción
void adjustValue(bool adjust_up)
{
if (linked_variable)
{
if (type == ValueType::INT)
{
int &value = *(static_cast<int *>(linked_variable));
int newValue = adjust_up ? value + step_value : value - step_value;
value = std::clamp(newValue, min_value, max_value);
}
else if (type == ValueType::BOOL)
{
bool &value = *(static_cast<bool *>(linked_variable));
value = !value;
}
else if (type == ValueType::LIST && !value_list.empty())
{
list_index = adjust_up ? (list_index + 1) % value_list.size()
: (list_index - 1 + value_list.size()) % value_list.size();
// Idioma
if (linked_variable == &options.pending_changes.new_language)
{
options.pending_changes.new_language = lang::getCodeFromName(value_list[list_index]);
options.pending_changes.has_pending_changes = true;
}
// Dificultad
if (linked_variable == &options.pending_changes.new_difficulty)
{
// options.pending_changes.new_difficulty =
options.pending_changes.has_pending_changes = true;
}
}
}
}
// Método para obtener el valor como string
std::string getValueAsString() const
{
switch (type)
{
case ValueType::BOOL:
return (*(static_cast<bool *>(linked_variable))) ? lang::getText("[SERVICE_MENU] ON") : lang::getText("[SERVICE_MENU] OFF");
case ValueType::INT:
return std::to_string(*(static_cast<int *>(linked_variable)));
case ValueType::LIST:
return value_list.empty() ? "" : value_list[list_index];
default:
return "";
}
}
};
// --- Variables internas ---
bool enabled_ = false; // Indica si el menú de servicio está activo
SDL_FRect rect_; // Rectángulo que define el área del menú de servicio
std::shared_ptr<Text> element_text_; // Objeto para escribir el texto de los elementos
std::shared_ptr<Text> title_text_; // Objeto para escribir el texto del título
size_t selected_ = 0; // Índice del 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_; // Opciones actualmente mostradas en pantalla (punteros)
OptionPairs option_pairs_; // Opciones actuales del menú (filtradas por grupo)
SettingsGroup current_settings_group_; // Grupo de opciones actualmente activo
SettingsGroup previous_settings_group_; // Grupo de opciones anterior
Aspect aspect_ = Aspect::ASPECT1; // Estilo visual del menú
// --- Variables de aspecto ---
Color bg_color_ = SERV_MENU_BG_COLOR; // Color de fondo
Color title_color_ = SERV_MENU_TITLE_COLOR; // Color del título del menú
Color text_color_ = SERV_MENU_TEXT_COLOR; // Color del texto de los elementos
Color selected_color_ = SERV_MENU_SELECTED_COLOR; // Color del elemento seleccionado
size_t width_; // Ancho del menú
size_t height_; // Alto del menú
size_t options_height_; // Altura de cada elemento del menu
size_t options_padding_; // Espaciado vertical alrededor de cada elemento del menu
size_t options_y_; // Posicion del primer elemento del menu
size_t title_height_; // Altura del texto de titulo del menu
size_t title_padding_; // Espaciado vertical alrededor del titulo
size_t upper_height_; // Altura de la parte de arriba del menu: la del titulo
size_t lower_height_; // Altira de la parte baja del menu: la que tiene las opciones
size_t lower_padding_; // Espaciado vertical mínimo entre los bordes y el contenido de la zona inferior
size_t options_width_; // Anchura de la opcion + valor más larga
// --- Variables para animación de resize ---
SDL_FRect rect_anim_from_{}; // Estado inicial de la animación
SDL_FRect rect_anim_to_{}; // Estado objetivo de la animación
int resize_anim_step_ = 0; // Paso actual de la animación
int resize_anim_steps_ = 8; // Total de pasos de la animación
bool resizing_ = false; // Si está animando el resize
int group_menu_widths_[5];
// --- Métodos internos: Anclaje y aspecto ---
void setAnchors(); // Establece el valor de las variables de anclaje
Color getSelectedColor() const; // Devuelve el color del elemento seleccionado
void setOptionsPosition(); // Establce la posición donde empezar a escribir las opciones del menu
void resize(); // Cambia el tamaño de la ventana de menu
// --- Métodos internos: Gestión de opciones ---
void initializeOptions(); // Crea todas las opciones del menú de servicio
OptionPairs getOptionPairs(SettingsGroup group) const; // Devuelve las opciones como pares de strings para un grupo
std::vector<OptionEntry *> getOptionsByGroup(SettingsGroup group); // Devuelve punteros a las opciones de un grupo
// --- Métodos internos: Lógica de menú ---
void applySettings(SettingsGroup group); // Aplica la configuración de un grupo
void updateMenu(SettingsGroup group); // Actualiza las opciones mostradas según el grupo
void AdjustListValues(); // Ajusta los valores de las opciones tipo lista
// --- Métodos internos: Utilidades ---
void updateCounter(); // Actualiza el contador interno
int calculateMenuHeight() const; // Calcula la altura del menú
int findLargestGroupSize() const; // Devuelve el tamaño del grupo más grande
GroupAlignment getGroupAlignment(SettingsGroup group) const; // Devuelve la alineación del grupo
OptionEntry *getOptionEntryByCaption(const std::string &caption); // Devuelve un puntero a OptionEntry a partir del caption
// --- Métodos internos: Animación de resize ---
void updateResizeAnimation();
// --- Métodos internos: Cálculo de anchos ---
void precalculateMenuWidths();
int getMenuWidthForGroup(SettingsGroup group) const;
// --- Patrón Singleton ---
ServiceMenu(); // Constructor privado
~ServiceMenu() = default; // Destructor privado
ServiceMenu(const ServiceMenu &) = delete; // Evita la copia
ServiceMenu &operator=(const ServiceMenu &) = delete; // Evita la asignación
// --- Instancia Singleton ---
static ServiceMenu *instance_; // Instancia única del menú de servicio
// --- Método para reproducir el sonido del menú ---
void playMenuSound(); // Reproduce el sonido del menú
};