Compare commits
3 Commits
5f52560ab5
...
483a7e020b
| Author | SHA1 | Date | |
|---|---|---|---|
| 483a7e020b | |||
| 37a7a9eccb | |||
| 9b7da383ea |
@@ -43,40 +43,40 @@ Audio::~Audio() { JA_Quit(); }
|
||||
// Reproduce la música
|
||||
void Audio::playMusic(const std::string &name, const int loop)
|
||||
{
|
||||
music_name_ = name;
|
||||
music_loop_ = loop;
|
||||
music_.name = name;
|
||||
music_.loop = loop;
|
||||
|
||||
if (enabled_ && music_enabled_)
|
||||
{
|
||||
if (JA_GetMusicState() == JA_MUSIC_STOPPED)
|
||||
if (music_enabled_ && music_.state != MusicState::PLAYING)
|
||||
{
|
||||
JA_PlayMusic(Resource::get()->getMusic(name), loop);
|
||||
}
|
||||
music_.state = MusicState::PLAYING;
|
||||
}
|
||||
}
|
||||
|
||||
// Pausa la música
|
||||
void Audio::pauseMusic()
|
||||
{
|
||||
if (enabled_ && music_enabled_)
|
||||
if (music_enabled_ && music_.state == MusicState::PLAYING)
|
||||
{
|
||||
JA_PauseMusic();
|
||||
music_.state = MusicState::PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
// Detiene la música
|
||||
void Audio::stopMusic()
|
||||
{
|
||||
if (enabled_ && music_enabled_)
|
||||
if (music_enabled_)
|
||||
{
|
||||
JA_StopMusic();
|
||||
music_.state = MusicState::STOPPED;
|
||||
}
|
||||
}
|
||||
|
||||
// Reproduce un sonido
|
||||
void Audio::playSound(const std::string &name)
|
||||
{
|
||||
if (enabled_ && sound_enabled_)
|
||||
if (sound_enabled_)
|
||||
{
|
||||
JA_PlaySound(Resource::get()->getSound(name));
|
||||
}
|
||||
@@ -85,7 +85,7 @@ void Audio::playSound(const std::string &name)
|
||||
// Detiene todos los sonidos
|
||||
void Audio::stopAllSounds()
|
||||
{
|
||||
if (enabled_ && sound_enabled_)
|
||||
if (sound_enabled_)
|
||||
{
|
||||
JA_StopChannel(-1);
|
||||
}
|
||||
@@ -94,7 +94,7 @@ void Audio::stopAllSounds()
|
||||
// Realiza un fundido de salida de la música
|
||||
void Audio::fadeOutMusic(int milliseconds)
|
||||
{
|
||||
if (enabled_ && music_enabled_)
|
||||
if (music_enabled_)
|
||||
{
|
||||
JA_FadeOutMusic(milliseconds);
|
||||
}
|
||||
@@ -103,7 +103,7 @@ void Audio::fadeOutMusic(int milliseconds)
|
||||
// Establece el volumen de los sonidos
|
||||
void Audio::setSoundVolume(int sound_volume)
|
||||
{
|
||||
if (enabled_ && sound_enabled_)
|
||||
if (sound_enabled_)
|
||||
{
|
||||
sound_volume = std::clamp(sound_volume, 0, 100);
|
||||
const float CONVERTED_VOLUME = (sound_volume / 100.0f) * (options.audio.volume / 100.0f);
|
||||
@@ -114,7 +114,7 @@ void Audio::setSoundVolume(int sound_volume)
|
||||
// Establece el volumen de la música
|
||||
void Audio::setMusicVolume(int music_volume)
|
||||
{
|
||||
if (enabled_ && music_enabled_)
|
||||
if (music_enabled_)
|
||||
{
|
||||
music_volume = std::clamp(music_volume, 0, 100);
|
||||
const float CONVERTED_VOLUME = (music_volume / 100.0f) * (options.audio.volume / 100.0f);
|
||||
@@ -127,13 +127,23 @@ void Audio::applySettings()
|
||||
{
|
||||
setSoundVolume(options.audio.sound.volume);
|
||||
setMusicVolume(options.audio.music.volume);
|
||||
enable(options.audio.enabled);
|
||||
}
|
||||
|
||||
bool const PREVIOUS_ENABLED = enabled_; // Guarda el estado actual
|
||||
enable(options.audio.enabled); // Modifica enabled_
|
||||
// Establecer estado general
|
||||
void Audio::enable(bool value)
|
||||
{
|
||||
enabled_ = value;
|
||||
|
||||
// Solo ejecuta la acción si el estado ha cambiado
|
||||
if (PREVIOUS_ENABLED != enabled_)
|
||||
switch (enabled_)
|
||||
{
|
||||
enabled_ ? JA_PlayMusic(Resource::get()->getMusic(music_name_), music_loop_) : JA_StopMusic();
|
||||
case true:
|
||||
setSoundVolume(options.audio.sound.volume);
|
||||
setMusicVolume(options.audio.music.volume);
|
||||
break;
|
||||
case false:
|
||||
setSoundVolume(0);
|
||||
setMusicVolume(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,7 @@ public:
|
||||
void stopAllSounds(); // Detener todos los sonidos
|
||||
|
||||
// --- Configuración General ---
|
||||
void enable() { enabled_ = true; } // Habilitar audio
|
||||
void disable() { enabled_ = false; } // Deshabilitar audio
|
||||
void enable(bool value) { enabled_ = value; } // Establecer estado general
|
||||
void enable(bool value); // Establecer estado general
|
||||
void toggleEnabled() { enabled_ = !enabled_; } // Alternar estado general
|
||||
void applySettings(); // Aplica la configuración
|
||||
|
||||
@@ -45,15 +43,34 @@ public:
|
||||
void setMusicVolume(int volume); // Ajustar volumen de música
|
||||
|
||||
private:
|
||||
enum class MusicState
|
||||
{
|
||||
PLAYING,
|
||||
PAUSED,
|
||||
STOPPED,
|
||||
};
|
||||
|
||||
struct Music
|
||||
{
|
||||
MusicState state; // Estado actual de la música (reproduciendo, detenido, en pausa)
|
||||
std::string name; // Última pista de música reproducida
|
||||
bool loop; // Indica si la última pista de música se debe reproducir en bucle
|
||||
|
||||
// Constructor para inicializar la música con valores predeterminados
|
||||
Music() : state(MusicState::STOPPED), name(""), loop(false) {}
|
||||
|
||||
// Constructor para inicializar con valores específicos
|
||||
Music(MusicState initState, const std::string &initName, bool initLoop)
|
||||
: state(initState), name(initName), loop(initLoop) {}
|
||||
};
|
||||
|
||||
Music music_;
|
||||
|
||||
// --- Variables de Estado ---
|
||||
bool enabled_ = true; // Estado general del audio
|
||||
bool sound_enabled_ = true; // Estado de los efectos de sonido
|
||||
bool music_enabled_ = true; // Estado de la música
|
||||
|
||||
// -- Variables de backup ---
|
||||
std::string music_name_; // Última pista de música que se ha reproducido
|
||||
bool music_loop_; // Parametros de la última pista de música reproducida
|
||||
|
||||
// --- Patrón Singleton ---
|
||||
Audio(); // Constructor privado
|
||||
~Audio(); // Destructor privado
|
||||
|
||||
@@ -486,7 +486,7 @@ void Resource::updateLoadingProgress(std::string name)
|
||||
updateProgressBar();
|
||||
renderProgress();
|
||||
checkEvents();
|
||||
SDL_Delay(100);
|
||||
//SDL_Delay(100);
|
||||
}
|
||||
|
||||
// Inicializa los rectangulos que definen la barra de progreso
|
||||
|
||||
@@ -67,6 +67,9 @@ void ServiceMenu::render()
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255);
|
||||
SDL_RenderRect(Screen::get()->getRenderer(), &rect_);
|
||||
|
||||
// Si está animando el resize, no pintar el contenido
|
||||
//if (resizing_) return;
|
||||
|
||||
// TITULO
|
||||
y += title_padding_;
|
||||
title_text_->writeDX(TEXT_COLOR | TEXT_CENTER, param.game.game_area.center_x, y, lang::getText("[SERVICE_MENU] TITLE"), -4, title_color_);
|
||||
@@ -101,6 +104,11 @@ void ServiceMenu::render()
|
||||
// Actualiza el estado del menú de servicio (colores, animaciones, etc.)
|
||||
void ServiceMenu::update()
|
||||
{
|
||||
if (resizing_) {
|
||||
updateResizeAnimation();
|
||||
// No actualizar colores ni animaciones mientras se redimensiona
|
||||
return;
|
||||
}
|
||||
if (enabled_)
|
||||
{
|
||||
updateCounter();
|
||||
@@ -138,21 +146,59 @@ void ServiceMenu::setAnchors()
|
||||
void ServiceMenu::setOptionsPosition()
|
||||
{
|
||||
resize();
|
||||
options_y_ = rect_.y + upper_height_ + lower_padding_;
|
||||
//options_y_ = rect_.y + upper_height_ + lower_padding_;
|
||||
|
||||
SDL_FRect new_rect = {
|
||||
(param.game.width - width_) / 2,
|
||||
(param.game.height - height_) / 2,
|
||||
static_cast<float>(width_),
|
||||
static_cast<float>(height_)};
|
||||
options_y_ = new_rect.y + upper_height_ + lower_padding_;
|
||||
|
||||
}
|
||||
|
||||
// Cambia el tamaño de la ventana de menu
|
||||
void ServiceMenu::resize()
|
||||
{
|
||||
// Usa el ancho precalculado para el grupo actual
|
||||
int menu_width = getMenuWidthForGroup(current_settings_group_);
|
||||
width_ = menu_width;
|
||||
lower_height_ = ((display_options_.size() - 1) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2);
|
||||
|
||||
width_ = 240;
|
||||
height_ = upper_height_ + lower_height_;
|
||||
rect_ = {
|
||||
SDL_FRect new_rect = {
|
||||
(param.game.width - width_) / 2,
|
||||
(param.game.height - height_) / 2,
|
||||
static_cast<float>(width_),
|
||||
static_cast<float>(height_)};
|
||||
|
||||
// Si el rect actual es diferente al nuevo, inicia animación
|
||||
if (rect_.x != new_rect.x || rect_.y != new_rect.y || rect_.w != new_rect.w || rect_.h != new_rect.h) {
|
||||
rect_anim_from_ = rect_;
|
||||
rect_anim_to_ = new_rect;
|
||||
resize_anim_step_ = 0;
|
||||
resizing_ = true;
|
||||
} else {
|
||||
rect_ = new_rect;
|
||||
resizing_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceMenu::updateResizeAnimation()
|
||||
{
|
||||
if (!resizing_) return;
|
||||
++resize_anim_step_;
|
||||
float t = static_cast<float>(resize_anim_step_) / resize_anim_steps_;
|
||||
if (t >= 1.0f) {
|
||||
rect_ = rect_anim_to_;
|
||||
resizing_ = false;
|
||||
return;
|
||||
}
|
||||
// EaseOutQuad
|
||||
float ease = 1 - (1 - t) * (1 - t);
|
||||
rect_.x = rect_anim_from_.x + (rect_anim_to_.x - rect_anim_from_.x) * ease;
|
||||
rect_.y = rect_anim_from_.y + (rect_anim_to_.y - rect_anim_from_.y) * ease;
|
||||
rect_.w = rect_anim_from_.w + (rect_anim_to_.w - rect_anim_from_.w) * ease;
|
||||
rect_.h = rect_anim_from_.h + (rect_anim_to_.h - rect_anim_from_.h) * ease;
|
||||
}
|
||||
|
||||
// Actualiza el contador interno para animaciones o efectos visuales
|
||||
@@ -335,6 +381,9 @@ void ServiceMenu::initializeOptions()
|
||||
options_.emplace_back(lang::getText("[SERVICE_MENU] AUDIO"), SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::AUDIO);
|
||||
options_.emplace_back(lang::getText("[SERVICE_MENU] GAME"), SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::GAME);
|
||||
options_.emplace_back(lang::getText("[SERVICE_MENU] SYSTEM"), SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::SYSTEM);
|
||||
|
||||
// Al terminar de inicializar las opciones, recalcula los anchos de menú
|
||||
precalculateMenuWidths();
|
||||
}
|
||||
|
||||
// Devuelve las opciones del grupo como pares (nombre, valor)
|
||||
@@ -391,6 +440,8 @@ void ServiceMenu::updateMenu(SettingsGroup group)
|
||||
AdjustListValues();
|
||||
option_pairs_ = getOptionPairs(group);
|
||||
display_options_ = getOptionsByGroup(group);
|
||||
// Recalcula el ancho del menú al cambiar de grupo
|
||||
resize();
|
||||
}
|
||||
|
||||
// Reinicia el menú al estado inicial (grupo principal y opción seleccionada)
|
||||
@@ -463,3 +514,47 @@ void ServiceMenu::AdjustListValues()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceMenu::precalculateMenuWidths()
|
||||
{
|
||||
// Inicializa todos los anchos al mínimo
|
||||
for (int &w : group_menu_widths_) w = MIN_WIDTH_;
|
||||
|
||||
// Para cada grupo
|
||||
for (int group = 0; group < 5; ++group) {
|
||||
SettingsGroup sg = static_cast<SettingsGroup>(group);
|
||||
int max_width = MIN_WIDTH_;
|
||||
for (const auto &option : options_) {
|
||||
if (option.group != sg) continue;
|
||||
int option_width = element_text_->lenght(option.caption);
|
||||
int value_width = 0;
|
||||
// Calcular el valor más ancho posible para esta opción
|
||||
switch (option.type) {
|
||||
case ValueType::BOOL:
|
||||
value_width = std::max(element_text_->lenght(lang::getText("[SERVICE_MENU] ON")),
|
||||
element_text_->lenght(lang::getText("[SERVICE_MENU] OFF")));
|
||||
break;
|
||||
case ValueType::INT:
|
||||
// Considera el valor máximo y mínimo como strings
|
||||
value_width = std::max(
|
||||
element_text_->lenght(std::to_string(option.min_value)),
|
||||
element_text_->lenght(std::to_string(option.max_value)));
|
||||
break;
|
||||
case ValueType::LIST:
|
||||
for (const auto &val : option.value_list)
|
||||
value_width = std::max(value_width, element_text_->lenght(val));
|
||||
break;
|
||||
default:
|
||||
value_width = 0;
|
||||
}
|
||||
int total_width = option_width + MIN_GAP_OPTION_VALUE_ + value_width;
|
||||
max_width = std::max(max_width, total_width);
|
||||
}
|
||||
group_menu_widths_[group] = max_width;
|
||||
}
|
||||
}
|
||||
|
||||
int ServiceMenu::getMenuWidthForGroup(SettingsGroup group) const
|
||||
{
|
||||
return group_menu_widths_[static_cast<int>(group)];
|
||||
}
|
||||
@@ -35,13 +35,18 @@ public:
|
||||
// --- 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 int OPTIONS_HORIZONTAL_PADDING_ = 20; // Relleno horizontal de las opciones
|
||||
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
|
||||
@@ -201,6 +206,16 @@ private:
|
||||
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
|
||||
@@ -225,6 +240,14 @@ private:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user