pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
@@ -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 ¤t_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;
|
||||
}
|
||||
Reference in New Issue
Block a user