pasaeta loca de clang-format (despres m'arrepentiré pero bueno)

This commit is contained in:
2025-07-18 20:01:13 +02:00
parent 734c220fb0
commit dabba41179
112 changed files with 22361 additions and 26474 deletions

View File

@@ -1,23 +1,22 @@
#pragma once
#include <string>
#include <vector>
#include <algorithm> // para std::clamp
#include <functional>
#include <memory>
#include <algorithm> // para std::clamp
#include "ui/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
#include <string>
#include <vector>
#include "lang.h" // Para las traducciones
#include "options.h" // Para acceder a las variables de configuración
#include "section.h" // Para las acciones como Quit o Reset
#include "text.h" // Para poder calcular el ancho del texto
#include "ui/service_menu.h" // Necesitamos las enums como SettingsGroup
// --- Interfaz Base para todas las Opciones del Menú ---
class MenuOption
{
public:
enum class Behavior
{
class MenuOption {
public:
enum class Behavior {
ADJUST,
SELECT
};
@@ -41,7 +40,7 @@ public:
// 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:
protected:
std::string caption_;
ServiceMenu::SettingsGroup group_;
bool hidden_;
@@ -49,52 +48,44 @@ protected:
// --- Clases Derivadas ---
class BoolOption : public MenuOption
{
public:
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
{
std::string getValueAsString() const override {
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_;
}
int getMaxValueWidth(Text *text_renderer) const override
{
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:
private:
bool *linked_variable_;
};
class IntOption : public MenuOption
{
public:
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
{
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 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_)
{
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);
}
@@ -102,34 +93,26 @@ public:
return max_width;
}
private:
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)
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)
{
list_index_(0) {
sync();
}
void sync()
{
void sync() {
std::string current_value = getter_();
for (size_t i = 0; i < value_list_.size(); ++i)
{
if (value_list_[i] == current_value)
{
for (size_t i = 0; i < value_list_.size(); ++i) {
if (value_list_[i] == current_value) {
list_index_ = i;
return;
}
@@ -137,12 +120,10 @@ public:
}
Behavior getBehavior() const override { return Behavior::ADJUST; }
std::string getValueAsString() const override
{
std::string getValueAsString() const override {
return value_list_.empty() ? "" : value_list_[list_index_];
}
void adjustValue(bool adjust_up) override
{
void adjustValue(bool adjust_up) override {
if (value_list_.empty())
return;
size_t size = value_list_.size();
@@ -150,49 +131,44 @@ public:
: (list_index_ + size - 1) % size;
setter_(value_list_[list_index_]);
}
int getMaxValueWidth(Text *text_renderer) const override
{
int getMaxValueWidth(Text *text_renderer) const override {
int max_w = 0;
for (const auto &val : value_list_)
{
for (const auto &val : value_list_) {
max_w = std::max(max_w, text_renderer->lenght(val, -2));
}
return max_w;
}
private:
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:
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:
private:
ServiceMenu::SettingsGroup target_group_;
};
class ActionOption : public MenuOption
{
public:
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
{
void executeAction() override {
if (action_)
action_();
}
private:
private:
std::function<void()> action_;
};

View File

@@ -1,22 +1,20 @@
#include "menu_renderer.h"
#include <algorithm> // Para max
#include <string> // Para basic_string
#include <utility> // Para pair, move
#include <algorithm> // Para max
#include <string> // Para basic_string
#include <utility> // Para pair, move
#include "menu_option.h" // Para MenuOption
#include "param.h" // Para Param, param, ParamServiceMenu, ParamGame
#include "screen.h" // Para Screen
#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR
#include "menu_option.h" // Para MenuOption
#include "param.h" // Para Param, param, ParamServiceMenu, ParamGame
#include "screen.h" // Para Screen
#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR
MenuRenderer::MenuRenderer(const ServiceMenu *menu_state, 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)
{
void MenuRenderer::render(const ServiceMenu *menu_state) {
// Dibuja la sombra
if (param.service_menu.drop_shadow)
{
if (param.service_menu.drop_shadow) {
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);
@@ -44,59 +42,48 @@ void MenuRenderer::render(const ServiceMenu *menu_state)
// Dibuja las opciones
y = options_y_;
const auto &option_pairs = menu_state->getOptionPairs();
for (size_t i = 0; i < option_pairs.size(); ++i)
{
for (size_t i = 0; i < option_pairs.size(); ++i) {
const bool is_selected = (i == menu_state->getSelectedIndex());
const Color &current_color = is_selected ? param.service_menu.selected_color : param.service_menu.text_color;
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT)
{
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
{
} 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_)
{
void MenuRenderer::update(const ServiceMenu *menu_state) {
if (resizing_) {
updateResizeAnimation();
}
updateColorCounter();
param.service_menu.selected_color = getAnimatedSelectedColor();
}
void MenuRenderer::onLayoutChanged(const ServiceMenu *menu_state)
{
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::setLayout(const ServiceMenu *menu_state)
{
void MenuRenderer::setLayout(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);
setSize(menu_state);
}
void MenuRenderer::setAnchors(const ServiceMenu *menu_state)
{
void MenuRenderer::setAnchors(const ServiceMenu *menu_state) {
size_t max_entries = 0;
for (int i = 0; i < 5; ++i)
{
for (int i = 0; i < 5; ++i) {
size_t count = menu_state->countOptionsInGroup(static_cast<ServiceMenu::SettingsGroup>(i));
if (count > max_entries)
{
if (count > max_entries) {
max_entries = count;
}
}
@@ -113,8 +100,7 @@ void MenuRenderer::setAnchors(const ServiceMenu *menu_state)
height_ = upper_height_ + lower_height_;
}
SDL_FRect MenuRenderer::calculateNewRect(const ServiceMenu *menu_state)
{
SDL_FRect MenuRenderer::calculateNewRect(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);
@@ -123,40 +109,33 @@ SDL_FRect MenuRenderer::calculateNewRect(const ServiceMenu *menu_state)
return {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, (float)width_, (float)height_};
}
void MenuRenderer::resize(const ServiceMenu *menu_state)
{
void MenuRenderer::resize(const ServiceMenu *menu_state) {
SDL_FRect new_rect = calculateNewRect(menu_state);
if (rect_.x != new_rect.x || rect_.y != new_rect.y || rect_.w != new_rect.w || rect_.h != new_rect.h)
{
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
{
} else {
rect_ = setRect(new_rect);
resizing_ = false;
}
options_y_ = new_rect.y + upper_height_ + lower_padding_;
}
void MenuRenderer::setSize(const ServiceMenu *menu_state)
{
void MenuRenderer::setSize(const ServiceMenu *menu_state) {
rect_ = setRect(calculateNewRect(menu_state));
resizing_ = false;
options_y_ = rect_.y + upper_height_ + lower_padding_;
}
void MenuRenderer::updateResizeAnimation()
{
void MenuRenderer::updateResizeAnimation() {
if (!resizing_)
return;
++resize_anim_step_;
float t = static_cast<float>(resize_anim_step_) / resize_anim_steps_;
if (t >= 1.0f)
{
if (t >= 1.0f) {
rect_ = setRect(rect_anim_to_);
resizing_ = false;
return;
@@ -164,64 +143,54 @@ void MenuRenderer::updateResizeAnimation()
float ease = 1 - (1 - t) * (1 - t);
SDL_FRect rect =
{rect_anim_from_.x + (rect_anim_to_.x - rect_anim_from_.x) * ease,
rect_anim_from_.y + (rect_anim_to_.y - rect_anim_from_.y) * ease,
rect_anim_from_.w + (rect_anim_to_.w - rect_anim_from_.w) * ease,
rect_anim_from_.h + (rect_anim_to_.h - rect_anim_from_.h) * ease};
rect_anim_from_.y + (rect_anim_to_.y - rect_anim_from_.y) * ease,
rect_anim_from_.w + (rect_anim_to_.w - rect_anim_from_.w) * ease,
rect_anim_from_.h + (rect_anim_to_.h - rect_anim_from_.h) * ease};
rect_ = setRect(rect);
}
void MenuRenderer::precalculateMenuWidths(const std::vector<std::unique_ptr<MenuOption>> &all_options, const ServiceMenu *menu_state)
{
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)
{
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)
{
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)
{
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)
{
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
{
int MenuRenderer::getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const {
return group_menu_widths_[static_cast<int>(group)];
}
void MenuRenderer::updateColorCounter()
{
void MenuRenderer::updateColorCounter() {
static Uint64 lastUpdate = SDL_GetTicks();
Uint64 currentTicks = SDL_GetTicks();
if (currentTicks - lastUpdate >= 50)
{
if (currentTicks - lastUpdate >= 50) {
color_counter_++;
lastUpdate = currentTicks;
}
}
Color MenuRenderer::getAnimatedSelectedColor()
{
Color MenuRenderer::getAnimatedSelectedColor() {
static auto colorCycle = generateMirroredCycle(param.service_menu.selected_color, ColorCycleStyle::HueWave);
return colorCycle.at(color_counter_ % colorCycle.size());
}
SDL_FRect MenuRenderer::setRect(SDL_FRect rect)
{
SDL_FRect MenuRenderer::setRect(SDL_FRect rect) {
border_rect_ = {rect.x - 1, rect.y + 1, rect.w + 2, rect.h - 2};
return rect;
}

View File

@@ -1,20 +1,20 @@
#pragma once
#include <SDL3/SDL.h> // Para SDL_FRect, Uint32
#include <stddef.h> // Para size_t
#include <memory> // Para shared_ptr, unique_ptr
#include <vector> // Para vector
#include <SDL3/SDL.h> // Para SDL_FRect, Uint32
#include <stddef.h> // Para size_t
#include "ui/service_menu.h" // Para ServiceMenu
#include "utils.h" // Para Color
#include <memory> // Para shared_ptr, unique_ptr
#include <vector> // Para vector
#include "ui/service_menu.h" // Para ServiceMenu
#include "utils.h" // Para Color
class MenuOption;
// Forward declarations
class Text;
class MenuRenderer
{
public:
class MenuRenderer {
public:
MenuRenderer(const ServiceMenu *menu_state, std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text);
// Métodos principales de la vista
@@ -28,7 +28,7 @@ public:
// Getters
const SDL_FRect &getRect() const { return rect_; }
private:
private:
// --- Referencias a los renderizadores de texto ---
std::shared_ptr<Text> element_text_;
std::shared_ptr<Text> title_text_;

View File

@@ -1,16 +1,16 @@
#include "ui/service_menu.h"
#include "audio.h" // Para Audio
#include "lang.h" // Para getText, getCodeFromName, getNameFromCode
#include "menu_option.h" // Para MenuOption, BoolOption, ActionOption, Int...
#include "menu_renderer.h" // Para MenuRenderer
#include "options.h" // Para PendingChanges, VideoOptions, pending_cha...
#include "param.h" // Para Param, param, ParamGame, ParamServiceMenu
#include "resource.h" // Para Resource
#include "screen.h" // Para Screen
#include "section.h" // Para Name, name, Options, options
#include "ui/ui_message.h" // Para UIMessage
#include "utils.h" // Para Zone
#include "audio.h" // Para Audio
#include "lang.h" // Para getText, getCodeFromName, getNameFromCode
#include "menu_option.h" // Para MenuOption, BoolOption, ActionOption, Int...
#include "menu_renderer.h" // Para MenuRenderer
#include "options.h" // Para PendingChanges, VideoOptions, pending_cha...
#include "param.h" // Para Param, param, ParamGame, ParamServiceMenu
#include "resource.h" // Para Resource
#include "screen.h" // Para Screen
#include "section.h" // Para Name, name, Options, options
#include "ui/ui_message.h" // Para UIMessage
#include "utils.h" // Para Zone
// Singleton
ServiceMenu *ServiceMenu::instance_ = nullptr;
@@ -21,8 +21,7 @@ ServiceMenu *ServiceMenu::get() { return ServiceMenu::instance_; }
// Constructor
ServiceMenu::ServiceMenu()
: current_settings_group_(SettingsGroup::MAIN),
previous_settings_group_(current_settings_group_)
{
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");
@@ -32,18 +31,15 @@ ServiceMenu::ServiceMenu()
reset();
}
void ServiceMenu::toggle()
{
void ServiceMenu::toggle() {
playBackSound();
enabled_ = !enabled_;
if (!enabled_)
{
if (!enabled_) {
reset();
}
}
void ServiceMenu::render()
{
void ServiceMenu::render() {
if (!enabled_)
return;
renderer_->render(this);
@@ -55,89 +51,76 @@ void ServiceMenu::render()
restart_message_ui_->render();
}
void ServiceMenu::update()
{
void ServiceMenu::update() {
if (!enabled_)
return;
renderer_->update(this);
bool now_pending = Options::pending_changes.has_pending_changes;
if (now_pending != last_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()
{
void ServiceMenu::reset() {
selected_ = 0;
main_menu_selected_ = 0;
current_settings_group_ = SettingsGroup::MAIN;
previous_settings_group_ = SettingsGroup::MAIN;
initializeOptions();
updateMenu();
renderer_->setLayout(this); // Notifica al renderer para que calcule el layout inicial
renderer_->setLayout(this); // Notifica al renderer para que calcule el layout inicial
}
// --- Lógica de Navegación ---
void ServiceMenu::setSelectorUp()
{
void ServiceMenu::setSelectorUp() {
if (display_options_.empty())
return;
selected_ = (selected_ > 0) ? selected_ - 1 : display_options_.size() - 1;
playMoveSound();
}
void ServiceMenu::setSelectorDown()
{
void ServiceMenu::setSelectorDown() {
if (display_options_.empty())
return;
selected_ = (selected_ + 1) % display_options_.size();
playMoveSound();
}
void ServiceMenu::adjustOption(bool adjust_up)
{
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)
{
if (selected_option->getBehavior() == MenuOption::Behavior::ADJUST) {
selected_option->adjustValue(adjust_up);
applySettings();
playAdjustSound();
}
}
void ServiceMenu::selectOption()
{
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))
{
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)
{
} else if (selected_option->getBehavior() == MenuOption::Behavior::SELECT) {
selected_option->executeAction();
}
playSelectSound();
}
void ServiceMenu::moveBack()
{
void ServiceMenu::moveBack() {
playBackSound();
if (current_settings_group_ == SettingsGroup::MAIN)
{
if (current_settings_group_ == SettingsGroup::MAIN) {
enabled_ = false;
return;
}
@@ -148,30 +131,24 @@ void ServiceMenu::moveBack()
// --- Lógica Interna ---
void ServiceMenu::updateDisplayOptions()
{
void ServiceMenu::updateDisplayOptions() {
display_options_.clear();
for (auto &option : options_)
{
if (option->getGroup() == current_settings_group_ && !option->isHidden())
{
for (auto &option : options_) {
if (option->getGroup() == current_settings_group_ && !option->isHidden()) {
display_options_.push_back(option.get());
}
}
updateOptionPairs();
}
void ServiceMenu::updateOptionPairs()
{
void ServiceMenu::updateOptionPairs() {
option_pairs_.clear();
for (const auto &option : display_options_)
{
for (const auto &option : display_options_) {
option_pairs_.emplace_back(option->getCaption(), option->getValueAsString());
}
}
void ServiceMenu::updateMenu()
{
void ServiceMenu::updateMenu() {
title_ = settingsGroupToString(current_settings_group_);
AdjustListValues();
@@ -182,8 +159,7 @@ void ServiceMenu::updateMenu()
renderer_->onLayoutChanged(this);
}
void ServiceMenu::applySettings()
{
void ServiceMenu::applySettings() {
if (current_settings_group_ == SettingsGroup::VIDEO)
applyVideoSettings();
if (current_settings_group_ == SettingsGroup::AUDIO)
@@ -195,28 +171,22 @@ void ServiceMenu::applySettings()
updateOptionPairs();
}
void ServiceMenu::applyVideoSettings()
{
void ServiceMenu::applyVideoSettings() {
Screen::get()->applySettings();
setHiddenOptions();
}
void ServiceMenu::applyAudioSettings()
{
void ServiceMenu::applyAudioSettings() {
Audio::get()->applySettings();
}
void ServiceMenu::applySettingsSettings()
{
void ServiceMenu::applySettingsSettings() {
setHiddenOptions();
}
MenuOption *ServiceMenu::getOptionByCaption(const std::string &caption) const
{
for (const auto &option : options_)
{
if (option->getCaption() == caption)
{
MenuOption *ServiceMenu::getOptionByCaption(const std::string &caption) const {
for (const auto &option : options_) {
if (option->getCaption() == caption) {
return option.get();
}
}
@@ -225,26 +195,21 @@ MenuOption *ServiceMenu::getOptionByCaption(const std::string &caption) const
// --- 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;
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 ServiceMenu::countOptionsInGroup(SettingsGroup group) const {
size_t count = 0;
for (const auto &option : options_)
{
if (option->getGroup() == group && !option->isHidden())
{
for (const auto &option : options_) {
if (option->getGroup() == group && !option->isHidden()) {
count++;
}
}
@@ -252,8 +217,7 @@ size_t ServiceMenu::countOptionsInGroup(SettingsGroup group) const
}
// Inicializa todas las opciones del menú
void ServiceMenu::initializeOptions()
{
void ServiceMenu::initializeOptions() {
options_.clear();
// --- Video ---
@@ -271,21 +235,14 @@ void ServiceMenu::initializeOptions()
// --- 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<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));
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));
@@ -297,12 +254,9 @@ void ServiceMenu::initializeOptions()
}
// Sincroniza los valores de las opciones tipo lista
void ServiceMenu::AdjustListValues()
{
for (auto &option : options_)
{
if (auto list_option = dynamic_cast<ListOption *>(option.get()))
{
void ServiceMenu::AdjustListValues() {
for (auto &option : options_) {
if (auto list_option = dynamic_cast<ListOption *>(option.get())) {
list_option->sync();
}
}
@@ -315,28 +269,25 @@ void ServiceMenu::playSelectSound() { Audio::get()->playSound("service_menu_sele
void ServiceMenu::playBackSound() { Audio::get()->playSound("service_menu_select.wav", Audio::Group::INTERFACE); }
// 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");
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");
}
}
// Establece el estado de oculto de ciertas opciones
void ServiceMenu::setHiddenOptions()
{
void ServiceMenu::setHiddenOptions() {
{
auto option = getOptionByCaption(Lang::getText("[SERVICE_MENU] WINDOW_SIZE"));
if (option)
@@ -349,5 +300,5 @@ void ServiceMenu::setHiddenOptions()
option->setHidden(!Options::settings.shutdown_enabled);
}
updateMenu(); // El menú debe refrescarse si algo se oculta
updateMenu(); // El menú debe refrescarse si algo se oculta
}

View File

@@ -1,21 +1,20 @@
#pragma once
#include <stddef.h> // Para size_t
#include <stddef.h> // Para size_t
#include <memory> // Para unique_ptr
#include <string> // Para basic_string, string
#include <utility> // Para pair
#include <vector> // Para vector
#include "ui_message.h" // Para UIMessage
#include "ui_message.h" // Para UIMessage
class MenuOption;
class MenuRenderer; // <-- Nuevo
class MenuRenderer; // <-- Nuevo
class ServiceMenu
{
public:
enum class SettingsGroup
{
class ServiceMenu {
public:
enum class SettingsGroup {
VIDEO,
AUDIO,
SETTINGS,
@@ -23,8 +22,7 @@ public:
MAIN
};
enum class GroupAlignment
{
enum class GroupAlignment {
CENTERED,
LEFT
};
@@ -61,7 +59,7 @@ public:
const std::vector<std::pair<std::string, std::string>> &getOptionPairs() const { return option_pairs_; }
size_t countOptionsInGroup(SettingsGroup group) const;
private:
private:
// --- Lógica de estado del menú (Modelo) ---
bool enabled_ = false;
std::vector<std::unique_ptr<MenuOption>> options_;

View File

@@ -1,21 +1,20 @@
#include "ui_message.h"
#include <cmath> // Para pow
#include <cmath> // Para pow
#include "text.h" // Para TEXT_CENTER, TEXT_COLOR, Text
#include "text.h" // Para TEXT_CENTER, TEXT_COLOR, Text
// 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()
{
void UIMessage::show() {
if (visible_ && target_y_ == 0.0f)
return; // Ya está visible y quieto
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
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;
@@ -23,47 +22,39 @@ void UIMessage::show()
}
// Oculta el mensaje con animación de salida hacia arriba
void UIMessage::hide()
{
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
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_)
{
void UIMessage::update() {
if (animating_) {
updateAnimation();
}
}
// Interpola la posición vertical del mensaje usando ease out cubic
void UIMessage::updateAnimation()
{
void UIMessage::updateAnimation() {
anim_step_++;
float t = static_cast<float>(anim_step_) / ANIMATION_STEPS;
if (target_y_ > start_y_)
{
if (target_y_ > start_y_) {
// Animación de entrada (ease out cubic)
t = 1 - pow(1 - t, 3);
}
else
{
} 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)
{
if (anim_step_ >= ANIMATION_STEPS) {
y_offset_ = target_y_;
animating_ = false;
if (target_y_ < 0.0f)
@@ -72,10 +63,8 @@ void UIMessage::updateAnimation()
}
// Dibuja el mensaje en pantalla si está visible
void UIMessage::render()
{
if (visible_)
{
void UIMessage::render() {
if (visible_) {
text_renderer_->writeDX(
TEXT_COLOR | TEXT_CENTER,
base_x_,
@@ -87,14 +76,12 @@ void UIMessage::render()
}
// Devuelve true si el mensaje está visible actualmente
bool UIMessage::isVisible() const
{
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)
{
void UIMessage::setPosition(float new_base_x, float new_base_y) {
base_x_ = new_base_x;
base_y_ = new_base_y;
}

View File

@@ -1,16 +1,15 @@
#pragma once
#include <memory> // Para shared_ptr
#include <string> // Para string, basic_string
#include <memory> // Para shared_ptr
#include <string> // Para string, basic_string
#include "utils.h" // Para Color
#include "utils.h" // Para Color
class Text;
// Clase para mostrar mensajes animados en la interfaz de usuario
class UIMessage
{
public:
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);
@@ -32,25 +31,25 @@ public:
// 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:
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
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)
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
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();