modificada la estructura de fitxers en source
This commit is contained in:
197
source/ui/menu_option.h
Normal file
197
source/ui/menu_option.h
Normal file
@@ -0,0 +1,197 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <algorithm> // para std::clamp
|
||||
#include "service_menu.h" // Necesitamos las enums como SettingsGroup
|
||||
#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 class Behavior
|
||||
{
|
||||
ADJUST,
|
||||
SELECT
|
||||
};
|
||||
|
||||
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
|
||||
: caption_(std::move(caption)), group_(group), hidden_(hidden) {}
|
||||
|
||||
virtual ~MenuOption() = default;
|
||||
|
||||
const std::string &getCaption() const { return caption_; }
|
||||
ServiceMenu::SettingsGroup getGroup() const { return group_; }
|
||||
bool isHidden() const { return hidden_; }
|
||||
void setHidden(bool hidden) { hidden_ = hidden; }
|
||||
|
||||
virtual Behavior getBehavior() const = 0;
|
||||
virtual std::string getValueAsString() const { return ""; }
|
||||
virtual void adjustValue(bool adjust_up) {}
|
||||
virtual ServiceMenu::SettingsGroup getTargetGroup() const { return ServiceMenu::SettingsGroup::MAIN; }
|
||||
virtual void executeAction() {}
|
||||
|
||||
// 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; }
|
||||
|
||||
protected:
|
||||
std::string caption_;
|
||||
ServiceMenu::SettingsGroup group_;
|
||||
bool hidden_;
|
||||
};
|
||||
|
||||
// --- Clases Derivadas ---
|
||||
|
||||
class BoolOption : public MenuOption
|
||||
{
|
||||
public:
|
||||
BoolOption(const std::string &cap, ServiceMenu::SettingsGroup grp, bool *var)
|
||||
: MenuOption(cap, grp), linked_variable_(var) {}
|
||||
|
||||
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
||||
std::string getValueAsString() const override
|
||||
{
|
||||
return *linked_variable_ ? Lang::getText("[SERVICE_MENU] ON") : Lang::getText("[SERVICE_MENU] OFF");
|
||||
}
|
||||
void adjustValue(bool /*adjust_up*/) override
|
||||
{
|
||||
*linked_variable_ = !*linked_variable_;
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
private:
|
||||
bool *linked_variable_;
|
||||
};
|
||||
|
||||
class IntOption : public MenuOption
|
||||
{
|
||||
public:
|
||||
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) {}
|
||||
|
||||
Behavior getBehavior() const override { return Behavior::ADJUST; }
|
||||
std::string getValueAsString() const override { return std::to_string(*linked_variable_); }
|
||||
void adjustValue(bool adjust_up) override
|
||||
{
|
||||
int newValue = *linked_variable_ + (adjust_up ? step_value_ : -step_value_);
|
||||
*linked_variable_ = std::clamp(newValue, min_value_, max_value_);
|
||||
}
|
||||
int getMaxValueWidth(Text *text_renderer) const override
|
||||
{
|
||||
int max_width = 0;
|
||||
|
||||
// Iterar por todos los valores posibles en el rango
|
||||
for (int value = min_value_; value <= max_value_; value += step_value_)
|
||||
{
|
||||
int width = text_renderer->lenght(std::to_string(value), -2);
|
||||
max_width = std::max(max_width, width);
|
||||
}
|
||||
|
||||
return max_width;
|
||||
}
|
||||
|
||||
private:
|
||||
int *linked_variable_;
|
||||
int min_value_, max_value_, step_value_;
|
||||
};
|
||||
|
||||
class ListOption : public MenuOption
|
||||
{
|
||||
public:
|
||||
ListOption(const std::string &cap, ServiceMenu::SettingsGroup grp,
|
||||
std::vector<std::string> values,
|
||||
std::function<std::string()> current_value_getter,
|
||||
std::function<void(const std::string &)> new_value_setter)
|
||||
: MenuOption(cap, grp),
|
||||
value_list_(std::move(values)),
|
||||
getter_(std::move(current_value_getter)),
|
||||
setter_(std::move(new_value_setter)),
|
||||
list_index_(0)
|
||||
{
|
||||
sync();
|
||||
}
|
||||
|
||||
void sync()
|
||||
{
|
||||
std::string current_value = getter_();
|
||||
for (size_t i = 0; i < value_list_.size(); ++i)
|
||||
{
|
||||
if (value_list_[i] == current_value)
|
||||
{
|
||||
list_index_ = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
setter_(value_list_[list_index_]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
private:
|
||||
std::vector<std::string> value_list_;
|
||||
std::function<std::string()> getter_;
|
||||
std::function<void(const std::string &)> setter_;
|
||||
size_t list_index_;
|
||||
};
|
||||
|
||||
class FolderOption : public MenuOption
|
||||
{
|
||||
public:
|
||||
FolderOption(const std::string &cap, ServiceMenu::SettingsGroup grp, ServiceMenu::SettingsGroup target)
|
||||
: MenuOption(cap, grp), target_group_(target) {}
|
||||
|
||||
Behavior getBehavior() const override { return Behavior::SELECT; }
|
||||
ServiceMenu::SettingsGroup getTargetGroup() const override { return target_group_; }
|
||||
|
||||
private:
|
||||
ServiceMenu::SettingsGroup target_group_;
|
||||
};
|
||||
|
||||
class ActionOption : public MenuOption
|
||||
{
|
||||
public:
|
||||
ActionOption(const std::string &cap, ServiceMenu::SettingsGroup grp, std::function<void()> action, bool hidden = false)
|
||||
: MenuOption(cap, grp, hidden), action_(std::move(action)) {}
|
||||
|
||||
Behavior getBehavior() const override { return Behavior::SELECT; }
|
||||
void executeAction() override
|
||||
{
|
||||
if (action_)
|
||||
action_();
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<void()> action_;
|
||||
};
|
||||
200
source/ui/menu_renderer.cpp
Normal file
200
source/ui/menu_renderer.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#include "menu_renderer.h"
|
||||
#include "text.h"
|
||||
#include "menu_option.h" // Necesario para acceder a las opciones
|
||||
#include "screen.h" // Para param
|
||||
#include <array>
|
||||
|
||||
MenuRenderer::MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text)
|
||||
: element_text_(std::move(element_text)), title_text_(std::move(title_text))
|
||||
{
|
||||
}
|
||||
|
||||
void MenuRenderer::render(const ServiceMenu *menu_state)
|
||||
{
|
||||
// Dibuja la sombra
|
||||
if (menu_state->getAspect() == ServiceMenu::Aspect::ASPECT1)
|
||||
{
|
||||
SDL_FRect shadowRect = {rect_.x + 5, rect_.y + 5, rect_.w, rect_.h};
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 64);
|
||||
SDL_RenderFillRect(Screen::get()->getRenderer(), &shadowRect);
|
||||
}
|
||||
|
||||
// Dibuja el fondo
|
||||
const Uint8 ALPHA = menu_state->getAspect() == ServiceMenu::Aspect::ASPECT1 ? 255 : 255;
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), bg_color_.r, bg_color_.g, bg_color_.b, ALPHA);
|
||||
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect_);
|
||||
|
||||
// Dibuja el borde
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255);
|
||||
SDL_RenderRect(Screen::get()->getRenderer(), &rect_);
|
||||
|
||||
// Dibuja el título
|
||||
float y = rect_.y + title_padding_;
|
||||
title_text_->writeDX(TEXT_COLOR | TEXT_CENTER, param.game.game_area.center_x, y, menu_state->getTitle(), -4, title_color_);
|
||||
|
||||
// Dibuja la línea separadora
|
||||
y = rect_.y + upper_height_;
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255);
|
||||
SDL_RenderLine(Screen::get()->getRenderer(), rect_.x + ServiceMenu::OPTIONS_HORIZONTAL_PADDING_, y, rect_.x + rect_.w - ServiceMenu::OPTIONS_HORIZONTAL_PADDING_, y);
|
||||
|
||||
// Dibuja las opciones
|
||||
y = options_y_;
|
||||
const auto &option_pairs = menu_state->getOptionPairs();
|
||||
for (size_t i = 0; i < option_pairs.size(); ++i)
|
||||
{
|
||||
const bool is_selected = (i == menu_state->getSelectedIndex());
|
||||
const Color ¤t_color = is_selected ? selected_color_ : text_color_;
|
||||
|
||||
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT)
|
||||
{
|
||||
element_text_->writeColored(rect_.x + ServiceMenu::OPTIONS_HORIZONTAL_PADDING_, y, option_pairs.at(i).first, current_color, -2);
|
||||
const int X = rect_.x + rect_.w - ServiceMenu::OPTIONS_HORIZONTAL_PADDING_ - element_text_->lenght(option_pairs.at(i).second, -2);
|
||||
element_text_->writeColored(X, y, option_pairs.at(i).second, current_color, -2);
|
||||
}
|
||||
else
|
||||
{
|
||||
element_text_->writeDX(TEXT_CENTER | TEXT_COLOR, rect_.x + rect_.w / 2, y, option_pairs.at(i).first, -2, current_color);
|
||||
}
|
||||
y += options_height_ + options_padding_;
|
||||
}
|
||||
}
|
||||
|
||||
void MenuRenderer::update(const ServiceMenu *menu_state)
|
||||
{
|
||||
if (resizing_)
|
||||
{
|
||||
updateResizeAnimation();
|
||||
}
|
||||
updateColorCounter();
|
||||
selected_color_ = getAnimatedSelectedColor();
|
||||
}
|
||||
|
||||
void MenuRenderer::onLayoutChanged(const ServiceMenu *menu_state)
|
||||
{
|
||||
// Cuando la lógica del menú notifica un cambio, el renderer recalcula su layout
|
||||
precalculateMenuWidths(menu_state->getAllOptions(), menu_state);
|
||||
setAnchors(menu_state);
|
||||
resize(menu_state);
|
||||
}
|
||||
|
||||
void MenuRenderer::setAnchors(const ServiceMenu *menu_state)
|
||||
{
|
||||
size_t max_entries = 0;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
size_t count = menu_state->countOptionsInGroup(static_cast<ServiceMenu::SettingsGroup>(i));
|
||||
if (count > max_entries)
|
||||
{
|
||||
max_entries = count;
|
||||
}
|
||||
}
|
||||
|
||||
options_height_ = element_text_->getCharacterSize();
|
||||
options_padding_ = 5;
|
||||
title_height_ = title_text_->getCharacterSize();
|
||||
title_padding_ = title_height_ / 2;
|
||||
upper_height_ = (title_padding_ * 2) + title_height_;
|
||||
lower_padding_ = (options_padding_ * 3);
|
||||
lower_height_ = ((max_entries > 0 ? max_entries - 1 : 0) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2);
|
||||
|
||||
width_ = ServiceMenu::MIN_WIDTH_;
|
||||
height_ = upper_height_ + lower_height_;
|
||||
|
||||
rect_ = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, (float)width_, (float)height_};
|
||||
}
|
||||
|
||||
void MenuRenderer::resize(const ServiceMenu *menu_state)
|
||||
{
|
||||
width_ = getMenuWidthForGroup(menu_state->getCurrentGroup());
|
||||
const auto &display_options = menu_state->getDisplayOptions();
|
||||
lower_height_ = ((display_options.size() > 0 ? display_options.size() - 1 : 0) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2);
|
||||
height_ = upper_height_ + lower_height_;
|
||||
SDL_FRect new_rect = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, (float)width_, (float)height_};
|
||||
|
||||
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;
|
||||
}
|
||||
options_y_ = new_rect.y + upper_height_ + lower_padding_;
|
||||
}
|
||||
|
||||
void MenuRenderer::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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
void MenuRenderer::precalculateMenuWidths(const std::vector<std::unique_ptr<MenuOption>> &all_options, const ServiceMenu *menu_state)
|
||||
{
|
||||
for (int &w : group_menu_widths_)
|
||||
w = ServiceMenu::MIN_WIDTH_;
|
||||
for (int group = 0; group < 5; ++group)
|
||||
{
|
||||
auto sg = static_cast<ServiceMenu::SettingsGroup>(group);
|
||||
int max_option_width = 0;
|
||||
int max_value_width = 0;
|
||||
for (const auto &option : all_options)
|
||||
{
|
||||
if (option->getGroup() != sg)
|
||||
continue;
|
||||
max_option_width = std::max(max_option_width, element_text_->lenght(option->getCaption(), -2));
|
||||
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT)
|
||||
{
|
||||
max_value_width = std::max(max_value_width, option->getMaxValueWidth(element_text_.get()));
|
||||
}
|
||||
}
|
||||
size_t total_width = max_option_width + (ServiceMenu::OPTIONS_HORIZONTAL_PADDING_ * 2);
|
||||
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT)
|
||||
{
|
||||
total_width += ServiceMenu::MIN_GAP_OPTION_VALUE_ + max_value_width;
|
||||
}
|
||||
group_menu_widths_[group] = std::max((int)ServiceMenu::MIN_WIDTH_, (int)total_width);
|
||||
}
|
||||
}
|
||||
|
||||
int MenuRenderer::getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const
|
||||
{
|
||||
return group_menu_widths_[static_cast<int>(group)];
|
||||
}
|
||||
|
||||
void MenuRenderer::updateColorCounter()
|
||||
{
|
||||
static Uint64 lastUpdate = SDL_GetTicks();
|
||||
Uint64 currentTicks = SDL_GetTicks();
|
||||
if (currentTicks - lastUpdate >= 50)
|
||||
{
|
||||
color_counter_++;
|
||||
lastUpdate = currentTicks;
|
||||
}
|
||||
}
|
||||
|
||||
Color MenuRenderer::getAnimatedSelectedColor()
|
||||
{
|
||||
static const std::array<Color, 12> colors = {
|
||||
Color(0xFF, 0xFB, 0x8A), Color(0xFF, 0xE4, 0x5D), Color(0xFF, 0xD1, 0x3C),
|
||||
Color(0xFF, 0xBF, 0x23), Color(0xFF, 0xAA, 0x12), Color(0xE6, 0x9A, 0x08),
|
||||
Color(0xE6, 0x9A, 0x08), Color(0xFF, 0xAA, 0x12), Color(0xFF, 0xBF, 0x23),
|
||||
Color(0xFF, 0xD1, 0x3C), Color(0xFF, 0xE4, 0x5D), Color(0xFF, 0xFB, 0x8A)};
|
||||
return colors.at(color_counter_ % colors.size());
|
||||
}
|
||||
69
source/ui/menu_renderer.h
Normal file
69
source/ui/menu_renderer.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <SDL3/SDL.h>
|
||||
#include "service_menu.h" // Necesario para las enums y para acceder al estado del menú
|
||||
#include "utils.h" // Para Color
|
||||
|
||||
// Forward declarations
|
||||
class Text;
|
||||
class MenuOption;
|
||||
|
||||
class MenuRenderer {
|
||||
public:
|
||||
MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text);
|
||||
|
||||
// Métodos principales de la vista
|
||||
void render(const ServiceMenu* menu_state);
|
||||
void update(const ServiceMenu* menu_state);
|
||||
|
||||
// Método para notificar al renderer que el layout puede haber cambiado
|
||||
void onLayoutChanged(const ServiceMenu* menu_state);
|
||||
|
||||
// Getters
|
||||
const SDL_FRect& getRect() const { return rect_; }
|
||||
|
||||
private:
|
||||
// --- Referencias a los renderizadores de texto ---
|
||||
std::shared_ptr<Text> element_text_;
|
||||
std::shared_ptr<Text> title_text_;
|
||||
|
||||
// --- Variables de estado de la vista (layout y animación) ---
|
||||
SDL_FRect rect_{};
|
||||
Color bg_color_ = SERV_MENU_BG_COLOR;
|
||||
Color title_color_ = SERV_MENU_TITLE_COLOR;
|
||||
Color text_color_ = SERV_MENU_TEXT_COLOR;
|
||||
Color selected_color_ = SERV_MENU_SELECTED_COLOR;
|
||||
size_t width_ = 0;
|
||||
size_t height_ = 0;
|
||||
size_t options_height_ = 0;
|
||||
size_t options_padding_ = 0;
|
||||
size_t options_y_ = 0;
|
||||
size_t title_height_ = 0;
|
||||
size_t title_padding_ = 0;
|
||||
size_t upper_height_ = 0;
|
||||
size_t lower_height_ = 0;
|
||||
size_t lower_padding_ = 0;
|
||||
Uint32 color_counter_ = 0;
|
||||
|
||||
// --- Variables para animación de resize ---
|
||||
SDL_FRect rect_anim_from_{};
|
||||
SDL_FRect rect_anim_to_{};
|
||||
int resize_anim_step_ = 0;
|
||||
int resize_anim_steps_ = 8;
|
||||
bool resizing_ = false;
|
||||
|
||||
// --- Anchos precalculados ---
|
||||
int group_menu_widths_[5]{};
|
||||
|
||||
// --- Métodos privados de la vista ---
|
||||
void setAnchors(const ServiceMenu* menu_state);
|
||||
void resize(const ServiceMenu* menu_state);
|
||||
void updateResizeAnimation();
|
||||
void precalculateMenuWidths(const std::vector<std::unique_ptr<MenuOption>>& all_options, const ServiceMenu* menu_state);
|
||||
int getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const;
|
||||
Color getAnimatedSelectedColor();
|
||||
void updateColorCounter();
|
||||
};
|
||||
266
source/ui/service_menu.cpp
Normal file
266
source/ui/service_menu.cpp
Normal file
@@ -0,0 +1,266 @@
|
||||
#include "service_menu.h"
|
||||
#include "menu_renderer.h"
|
||||
#include "menu_option.h"
|
||||
#include "resource.h"
|
||||
#include "audio.h"
|
||||
#include "section.h"
|
||||
#include "screen.h"
|
||||
#include "asset.h"
|
||||
|
||||
// Singleton
|
||||
ServiceMenu* ServiceMenu::instance_ = nullptr;
|
||||
void ServiceMenu::init() { ServiceMenu::instance_ = new ServiceMenu(); }
|
||||
void ServiceMenu::destroy() { delete ServiceMenu::instance_; }
|
||||
ServiceMenu* ServiceMenu::get() { return ServiceMenu::instance_; }
|
||||
|
||||
// Constructor
|
||||
ServiceMenu::ServiceMenu()
|
||||
: current_settings_group_(SettingsGroup::MAIN),
|
||||
previous_settings_group_(current_settings_group_)
|
||||
{
|
||||
auto element_text = Resource::get()->getText("04b_25_flat");
|
||||
auto title_text = Resource::get()->getText("04b_25_flat_2x");
|
||||
|
||||
renderer_ = std::make_unique<MenuRenderer>(element_text, title_text);
|
||||
restart_message_ui_ = std::make_unique<UIMessage>(element_text, Lang::getText("[SERVICE_MENU] NEED_RESTART_MESSAGE"), SERV_MENU_TITLE_COLOR);
|
||||
|
||||
reset();
|
||||
}
|
||||
|
||||
void ServiceMenu::toggle() {
|
||||
enabled_ = !enabled_;
|
||||
if (!enabled_) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceMenu::render() {
|
||||
if (!enabled_) return;
|
||||
renderer_->render(this);
|
||||
|
||||
// El mensaje de reinicio se dibuja por encima, así que lo gestionamos aquí
|
||||
const float MSG_X = param.game.game_area.center_x;
|
||||
const float MSG_Y = renderer_->getRect().y + 39.0f;
|
||||
restart_message_ui_->setPosition(MSG_X, MSG_Y);
|
||||
restart_message_ui_->render();
|
||||
}
|
||||
|
||||
void ServiceMenu::update() {
|
||||
if (!enabled_) return;
|
||||
|
||||
renderer_->update(this);
|
||||
|
||||
bool now_pending = Options::pending_changes.has_pending_changes;
|
||||
if (now_pending != last_pending_changes_) {
|
||||
now_pending ? restart_message_ui_->show() : restart_message_ui_->hide();
|
||||
last_pending_changes_ = now_pending;
|
||||
}
|
||||
restart_message_ui_->update();
|
||||
}
|
||||
|
||||
void ServiceMenu::reset() {
|
||||
selected_ = 0;
|
||||
main_menu_selected_ = 0;
|
||||
current_settings_group_ = SettingsGroup::MAIN;
|
||||
previous_settings_group_ = SettingsGroup::MAIN;
|
||||
initializeOptions();
|
||||
updateMenu();
|
||||
renderer_->onLayoutChanged(this); // Notifica al renderer para que calcule el layout inicial
|
||||
}
|
||||
|
||||
// --- Lógica de Navegación ---
|
||||
void ServiceMenu::setSelectorUp() {
|
||||
if (display_options_.empty()) return;
|
||||
selected_ = (selected_ > 0) ? selected_ - 1 : display_options_.size() - 1;
|
||||
playMenuSound();
|
||||
}
|
||||
|
||||
void ServiceMenu::setSelectorDown() {
|
||||
if (display_options_.empty()) return;
|
||||
selected_ = (selected_ + 1) % display_options_.size();
|
||||
playMenuSound();
|
||||
}
|
||||
|
||||
void ServiceMenu::adjustOption(bool adjust_up) {
|
||||
if (display_options_.empty()) return;
|
||||
auto& selected_option = display_options_.at(selected_);
|
||||
if (selected_option->getBehavior() == MenuOption::Behavior::ADJUST) {
|
||||
selected_option->adjustValue(adjust_up);
|
||||
applySettings();
|
||||
playMenuSound();
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceMenu::selectOption() {
|
||||
if (display_options_.empty()) return;
|
||||
if (current_settings_group_ == SettingsGroup::MAIN) main_menu_selected_ = selected_;
|
||||
auto& selected_option = display_options_.at(selected_);
|
||||
if (auto folder = dynamic_cast<FolderOption*>(selected_option)) {
|
||||
previous_settings_group_ = current_settings_group_;
|
||||
current_settings_group_ = folder->getTargetGroup();
|
||||
selected_ = 0;
|
||||
updateMenu();
|
||||
} else if (selected_option->getBehavior() == MenuOption::Behavior::SELECT) {
|
||||
selected_option->executeAction();
|
||||
}
|
||||
playMenuSound();
|
||||
}
|
||||
|
||||
void ServiceMenu::moveBack() {
|
||||
if (current_settings_group_ == SettingsGroup::MAIN) {
|
||||
enabled_ = false;
|
||||
return;
|
||||
}
|
||||
current_settings_group_ = previous_settings_group_;
|
||||
selected_ = (current_settings_group_ == SettingsGroup::MAIN) ? main_menu_selected_ : 0;
|
||||
updateMenu();
|
||||
playMenuSound();
|
||||
}
|
||||
|
||||
// --- Lógica Interna ---
|
||||
|
||||
void ServiceMenu::updateMenu() {
|
||||
title_ = settingsGroupToString(current_settings_group_);
|
||||
AdjustListValues();
|
||||
|
||||
// Actualiza las opciones visibles
|
||||
display_options_.clear();
|
||||
for (auto& option : options_) {
|
||||
if (option->getGroup() == current_settings_group_ && !option->isHidden()) {
|
||||
display_options_.push_back(option.get());
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los pares de strings para el renderer
|
||||
option_pairs_.clear();
|
||||
for (const auto& option : display_options_) {
|
||||
option_pairs_.emplace_back(option->getCaption(), option->getValueAsString());
|
||||
}
|
||||
|
||||
// Notifica al renderer del cambio de layout
|
||||
renderer_->onLayoutChanged(this);
|
||||
}
|
||||
|
||||
void ServiceMenu::applySettings() {
|
||||
if (current_settings_group_ == SettingsGroup::VIDEO) Screen::get()->applySettings();
|
||||
if (current_settings_group_ == SettingsGroup::AUDIO) Audio::get()->applySettings();
|
||||
if (current_settings_group_ == SettingsGroup::SETTINGS) {
|
||||
for(auto& option : options_){
|
||||
if(option->getCaption() == Lang::getText("[SERVICE_MENU] SHUTDOWN]")){
|
||||
option->setHidden(!Options::settings.shutdown_enabled);
|
||||
updateMenu(); // El menú debe refrescarse si algo se oculta
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Refresca los pares de strings por si un valor cambió
|
||||
option_pairs_.clear();
|
||||
for (const auto& option : display_options_) {
|
||||
option_pairs_.emplace_back(option->getCaption(), option->getValueAsString());
|
||||
}
|
||||
}
|
||||
|
||||
// --- Getters y otros ---
|
||||
|
||||
ServiceMenu::GroupAlignment ServiceMenu::getCurrentGroupAlignment() const {
|
||||
switch (current_settings_group_) {
|
||||
case SettingsGroup::VIDEO:
|
||||
case SettingsGroup::AUDIO:
|
||||
case SettingsGroup::SETTINGS:
|
||||
return GroupAlignment::LEFT;
|
||||
default:
|
||||
return GroupAlignment::CENTERED;
|
||||
}
|
||||
}
|
||||
|
||||
size_t ServiceMenu::countOptionsInGroup(SettingsGroup group) const {
|
||||
size_t count = 0;
|
||||
for (const auto& option : options_) {
|
||||
if (option->getGroup() == group && !option->isHidden()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Inicializa todas las opciones del menú
|
||||
void ServiceMenu::initializeOptions()
|
||||
{
|
||||
options_.clear();
|
||||
|
||||
// --- Video ---
|
||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] FULLSCREEN"), SettingsGroup::VIDEO, &Options::video.fullscreen));
|
||||
options_.push_back(std::make_unique<IntOption>(Lang::getText("[SERVICE_MENU] WINDOW_SIZE"), SettingsGroup::VIDEO, &Options::window.size, 1, Options::window.max_size, 1));
|
||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] SHADERS"), SettingsGroup::VIDEO, &Options::video.shaders));
|
||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] VSYNC"), SettingsGroup::VIDEO, &Options::video.v_sync));
|
||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] INTEGER_SCALE"), SettingsGroup::VIDEO, &Options::video.integer_scale));
|
||||
|
||||
// --- Audio ---
|
||||
options_.push_back(std::make_unique<BoolOption>(Lang::getText("[SERVICE_MENU] AUDIO"), SettingsGroup::AUDIO, &Options::audio.enabled));
|
||||
options_.push_back(std::make_unique<IntOption>(Lang::getText("[SERVICE_MENU] MAIN_VOLUME"), SettingsGroup::AUDIO, &Options::audio.volume, 0, 100, 5));
|
||||
options_.push_back(std::make_unique<IntOption>(Lang::getText("[SERVICE_MENU] MUSIC_VOLUME"), SettingsGroup::AUDIO, &Options::audio.music.volume, 0, 100, 5));
|
||||
options_.push_back(std::make_unique<IntOption>(Lang::getText("[SERVICE_MENU] SFX_VOLUME"), SettingsGroup::AUDIO, &Options::audio.sound.volume, 0, 100, 5));
|
||||
|
||||
// --- Settings ---
|
||||
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, std::vector<std::string>{Lang::getText("[SERVICE_MENU] LANG_ES"), Lang::getText("[SERVICE_MENU] LANG_BA"), Lang::getText("[SERVICE_MENU] LANG_EN")}, []()
|
||||
{ return Lang::getNameFromCode(Options::pending_changes.new_language); }, [](const std::string &val)
|
||||
{ Options::pending_changes.new_language = Lang::getCodeFromName(val); Options::checkPendingChanges(); }));
|
||||
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")}, []()
|
||||
{ return Options::getDifficultyNameFromCode(Options::pending_changes.new_difficulty); }, [](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));
|
||||
|
||||
// --- System ---
|
||||
options_.push_back(std::make_unique<ActionOption>(Lang::getText("[SERVICE_MENU] RESET"), SettingsGroup::SYSTEM, [this]()
|
||||
{ Section::name = Section::Name::RESET; toggle(); }));
|
||||
options_.push_back(std::make_unique<ActionOption>(Lang::getText("[SERVICE_MENU] QUIT"), SettingsGroup::SYSTEM, []()
|
||||
{ Section::name = Section::Name::QUIT; Section::options = Section::Options::NONE; }));
|
||||
options_.push_back(std::make_unique<ActionOption>(Lang::getText("[SERVICE_MENU] SHUTDOWN"), SettingsGroup::SYSTEM, []()
|
||||
{ Section::name = Section::Name::QUIT; Section::options = Section::Options::SHUTDOWN; }, !Options::settings.shutdown_enabled));
|
||||
|
||||
// --- Main Menu ---
|
||||
options_.push_back(std::make_unique<FolderOption>(Lang::getText("[SERVICE_MENU] VIDEO"), SettingsGroup::MAIN, SettingsGroup::VIDEO));
|
||||
options_.push_back(std::make_unique<FolderOption>(Lang::getText("[SERVICE_MENU] AUDIO"), SettingsGroup::MAIN, SettingsGroup::AUDIO));
|
||||
options_.push_back(std::make_unique<FolderOption>(Lang::getText("[SERVICE_MENU] SETTINGS"), SettingsGroup::MAIN, SettingsGroup::SETTINGS));
|
||||
options_.push_back(std::make_unique<FolderOption>(Lang::getText("[SERVICE_MENU] SYSTEM"), SettingsGroup::MAIN, SettingsGroup::SYSTEM));
|
||||
|
||||
//precalculateMenuWidths();
|
||||
}
|
||||
|
||||
// Sincroniza los valores de las opciones tipo lista
|
||||
void ServiceMenu::AdjustListValues()
|
||||
{
|
||||
for (auto &option : options_)
|
||||
{
|
||||
if (auto list_option = dynamic_cast<ListOption *>(option.get()))
|
||||
{
|
||||
list_option->sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Reproduce el sonido de navegación del menú
|
||||
void ServiceMenu::playMenuSound() { Audio::get()->playSound("clock.wav"); }
|
||||
|
||||
// Devuelve el nombre del grupo como string para el título
|
||||
std::string ServiceMenu::settingsGroupToString(SettingsGroup group) const
|
||||
{
|
||||
switch (group)
|
||||
{
|
||||
case SettingsGroup::MAIN:
|
||||
return Lang::getText("[SERVICE_MENU] TITLE");
|
||||
case SettingsGroup::VIDEO:
|
||||
return Lang::getText("[SERVICE_MENU] VIDEO");
|
||||
case SettingsGroup::AUDIO:
|
||||
return Lang::getText("[SERVICE_MENU] AUDIO");
|
||||
case SettingsGroup::SETTINGS:
|
||||
return Lang::getText("[SERVICE_MENU] SETTINGS");
|
||||
case SettingsGroup::SYSTEM:
|
||||
return Lang::getText("[SERVICE_MENU] SYSTEM");
|
||||
default:
|
||||
return Lang::getText("[SERVICE_MENU] TITLE");
|
||||
}
|
||||
}
|
||||
|
||||
89
source/ui/service_menu.h
Normal file
89
source/ui/service_menu.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "utils.h"
|
||||
#include "ui_message.h"
|
||||
|
||||
// Forward Declarations
|
||||
class Text;
|
||||
class MenuOption;
|
||||
class MenuRenderer; // <-- Nuevo
|
||||
|
||||
class ServiceMenu {
|
||||
public:
|
||||
enum class Aspect { ASPECT1, ASPECT2 };
|
||||
enum class SettingsGroup { VIDEO, AUDIO, SETTINGS, SYSTEM, MAIN };
|
||||
enum class GroupAlignment { CENTERED, LEFT };
|
||||
|
||||
// --- Constantes públicas que el Renderer podría necesitar ---
|
||||
static constexpr size_t OPTIONS_HORIZONTAL_PADDING_ = 20;
|
||||
static constexpr size_t MIN_WIDTH_ = 240;
|
||||
static constexpr size_t MIN_GAP_OPTION_VALUE_ = 30;
|
||||
|
||||
static void init();
|
||||
static void destroy();
|
||||
static ServiceMenu* get();
|
||||
|
||||
void toggle();
|
||||
void render(); // Ahora solo delega
|
||||
void update(); // Ahora delega la parte visual
|
||||
void reset();
|
||||
|
||||
// --- Lógica de navegación ---
|
||||
void setSelectorUp();
|
||||
void setSelectorDown();
|
||||
void adjustOption(bool adjust_up);
|
||||
void selectOption();
|
||||
void moveBack();
|
||||
|
||||
// --- Getters para que el Renderer pueda leer el estado ---
|
||||
bool isEnabled() const { return enabled_; }
|
||||
const std::string& getTitle() const { return title_; }
|
||||
SettingsGroup getCurrentGroup() const { return current_settings_group_; }
|
||||
GroupAlignment getCurrentGroupAlignment() const;
|
||||
const std::vector<MenuOption*>& getDisplayOptions() const { return display_options_; }
|
||||
const std::vector<std::unique_ptr<MenuOption>>& getAllOptions() const { return options_; }
|
||||
size_t getSelectedIndex() const { return selected_; }
|
||||
const std::vector<std::pair<std::string, std::string>>& getOptionPairs() const { return option_pairs_; }
|
||||
size_t countOptionsInGroup(SettingsGroup group) const;
|
||||
Aspect getAspect() const { return aspect_; }
|
||||
|
||||
private:
|
||||
// --- Lógica de estado del menú (Modelo) ---
|
||||
bool enabled_ = false;
|
||||
std::vector<std::unique_ptr<MenuOption>> options_;
|
||||
std::vector<MenuOption*> display_options_;
|
||||
std::vector<std::pair<std::string, std::string>> option_pairs_;
|
||||
|
||||
SettingsGroup current_settings_group_;
|
||||
SettingsGroup previous_settings_group_;
|
||||
Aspect aspect_ = Aspect::ASPECT1;
|
||||
std::string title_;
|
||||
size_t selected_ = 0;
|
||||
size_t main_menu_selected_ = 0;
|
||||
|
||||
// --- Mensaje de reinicio ---
|
||||
std::unique_ptr<UIMessage> restart_message_ui_;
|
||||
bool last_pending_changes_ = false;
|
||||
|
||||
// --- La Vista ---
|
||||
std::unique_ptr<MenuRenderer> renderer_;
|
||||
|
||||
// --- Métodos de lógica interna ---
|
||||
void initializeOptions();
|
||||
void updateMenu();
|
||||
void applySettings();
|
||||
void AdjustListValues();
|
||||
void playMenuSound();
|
||||
std::string settingsGroupToString(SettingsGroup group) const;
|
||||
|
||||
// --- Singleton ---
|
||||
ServiceMenu();
|
||||
~ServiceMenu() = default;
|
||||
ServiceMenu(const ServiceMenu&) = delete;
|
||||
ServiceMenu& operator=(const ServiceMenu&) = delete;
|
||||
static ServiceMenu* instance_;
|
||||
};
|
||||
98
source/ui/ui_message.cpp
Normal file
98
source/ui/ui_message.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "ui_message.h"
|
||||
#include <cmath> // Para pow
|
||||
#include "screen.h" // Para param y TEXT_CENTER
|
||||
|
||||
// Constructor: inicializa el renderizador, el texto y el color del mensaje
|
||||
UIMessage::UIMessage(std::shared_ptr<Text> text_renderer, const std::string &message_text, const Color &color)
|
||||
: text_renderer_(text_renderer), text_(message_text), color_(color) {}
|
||||
|
||||
// Muestra el mensaje en la posición base_x, base_y con animación de entrada desde arriba
|
||||
void UIMessage::show()
|
||||
{
|
||||
if (visible_ && target_y_ == 0.0f)
|
||||
return; // Ya está visible y quieto
|
||||
|
||||
start_y_ = DESP_; // Empieza 8 píxeles arriba de la posición base
|
||||
target_y_ = 0.0f; // La posición final es la base
|
||||
y_offset_ = start_y_;
|
||||
anim_step_ = 0;
|
||||
animating_ = true;
|
||||
visible_ = true;
|
||||
}
|
||||
|
||||
// Oculta el mensaje con animación de salida hacia arriba
|
||||
void UIMessage::hide()
|
||||
{
|
||||
if (!visible_)
|
||||
return;
|
||||
|
||||
start_y_ = y_offset_; // Comienza desde la posición actual
|
||||
target_y_ = DESP_; // Termina 8 píxeles arriba de la base
|
||||
anim_step_ = 0;
|
||||
animating_ = true;
|
||||
}
|
||||
|
||||
// Actualiza el estado de la animación (debe llamarse cada frame)
|
||||
void UIMessage::update()
|
||||
{
|
||||
if (animating_)
|
||||
{
|
||||
updateAnimation();
|
||||
}
|
||||
}
|
||||
|
||||
// Interpola la posición vertical del mensaje usando ease out cubic
|
||||
void UIMessage::updateAnimation()
|
||||
{
|
||||
anim_step_++;
|
||||
float t = static_cast<float>(anim_step_) / ANIMATION_STEPS;
|
||||
|
||||
if (target_y_ > start_y_)
|
||||
{
|
||||
// Animación de entrada (ease out cubic)
|
||||
t = 1 - pow(1 - t, 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Animación de salida (ease in cubic)
|
||||
t = pow(t, 3);
|
||||
}
|
||||
|
||||
y_offset_ = start_y_ + (target_y_ - start_y_) * t;
|
||||
|
||||
if (anim_step_ >= ANIMATION_STEPS)
|
||||
{
|
||||
y_offset_ = target_y_;
|
||||
animating_ = false;
|
||||
if (target_y_ < 0.0f)
|
||||
visible_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el mensaje en pantalla si está visible
|
||||
void UIMessage::render()
|
||||
{
|
||||
if (visible_)
|
||||
{
|
||||
text_renderer_->writeDX(
|
||||
TEXT_COLOR | TEXT_CENTER,
|
||||
base_x_,
|
||||
base_y_ + y_offset_,
|
||||
text_,
|
||||
-2,
|
||||
color_);
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve true si el mensaje está visible actualmente
|
||||
bool UIMessage::isVisible() const
|
||||
{
|
||||
return visible_;
|
||||
}
|
||||
|
||||
// Permite actualizar la posición del mensaje (por ejemplo, si el menú se mueve)
|
||||
void UIMessage::setPosition(float new_base_x, float new_base_y)
|
||||
{
|
||||
base_x_ = new_base_x;
|
||||
base_y_ = new_base_y;
|
||||
}
|
||||
55
source/ui/ui_message.h
Normal file
55
source/ui/ui_message.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "text.h"
|
||||
#include "utils.h" // Para Color
|
||||
|
||||
// Clase para mostrar mensajes animados en la interfaz de usuario
|
||||
class UIMessage
|
||||
{
|
||||
public:
|
||||
// Constructor: recibe el renderizador de texto, el mensaje y el color
|
||||
UIMessage(std::shared_ptr<Text> text_renderer, const std::string &message_text, const Color &color);
|
||||
|
||||
// Muestra el mensaje con animación de entrada
|
||||
void show();
|
||||
|
||||
// Oculta el mensaje con animación de salida
|
||||
void hide();
|
||||
|
||||
// Actualiza el estado de la animación (debe llamarse cada frame)
|
||||
void update();
|
||||
|
||||
// Dibuja el mensaje en pantalla si está visible
|
||||
void render();
|
||||
|
||||
// Indica si el mensaje está visible actualmente
|
||||
bool isVisible() const;
|
||||
|
||||
// Permite actualizar la posición del mensaje (por ejemplo, si el menú se mueve)
|
||||
void setPosition(float new_base_x, float new_base_y);
|
||||
|
||||
private:
|
||||
// --- Configuración ---
|
||||
std::shared_ptr<Text> text_renderer_; // Renderizador de texto
|
||||
std::string text_; // Texto del mensaje a mostrar
|
||||
Color color_; // Color del texto
|
||||
|
||||
// --- Estado ---
|
||||
bool visible_ = false; // Indica si el mensaje está visible
|
||||
bool animating_ = false; // Indica si el mensaje está en proceso de animación
|
||||
float base_x_ = 0.0f; // Posición X base donde se muestra el mensaje
|
||||
float base_y_ = 0.0f; // Posición Y base donde se muestra el mensaje
|
||||
float y_offset_ = 0.0f; // Desplazamiento vertical actual del mensaje (para animación)
|
||||
|
||||
// --- Animación ---
|
||||
float start_y_ = 0.0f; // Posición Y inicial de la animación
|
||||
float target_y_ = 0.0f; // Posición Y objetivo de la animación
|
||||
int anim_step_ = 0; // Paso actual de la animación
|
||||
static constexpr int ANIMATION_STEPS = 8; // Número total de pasos de la animación
|
||||
static constexpr float DESP_ = -8.0f; // Distancia a desplazarse
|
||||
|
||||
// Actualiza la interpolación de la animación (ease out/in cubic)
|
||||
void updateAnimation();
|
||||
};
|
||||
Reference in New Issue
Block a user