This commit is contained in:
2026-04-17 22:20:37 +02:00
parent 513eacf356
commit 20b9a95619
38 changed files with 310 additions and 622 deletions

View File

@@ -2,6 +2,7 @@
#include <algorithm> // for max, min
#include <fstream> // for char_traits, basic_ifstream, basic_istream
#include <numeric> // for accumulate
#include <sstream> // for basic_stringstream
#include "core/audio/jail_audio.hpp" // for JA_LoadSound, JA_PlaySound, JA_DeleteSound
@@ -11,7 +12,10 @@
#include "core/resources/resource_helper.h"
// Constructor
Menu::Menu(SDL_Renderer *renderer, std::string file) {
Menu::Menu(SDL_Renderer *renderer, const std::string &file)
: colorGreyed{128, 128, 128},
font_png(""),
font_txt("") {
// Copia punteros
this->renderer = renderer;
@@ -21,7 +25,6 @@ Menu::Menu(SDL_Renderer *renderer, std::string file) {
soundCancel = nullptr;
// Inicializa variables
name = "";
selector.index = 0;
selector.previousIndex = 0;
itemSelected = MENU_NO_OPTION;
@@ -38,10 +41,7 @@ Menu::Menu(SDL_Renderer *renderer, std::string file) {
centerX = 0;
centerY = 0;
widestItem = 0;
colorGreyed = {128, 128, 128};
defaultActionWhenCancel = 0;
font_png = "";
font_txt = "";
// Selector
selector.originY = 0;
@@ -63,7 +63,7 @@ Menu::Menu(SDL_Renderer *renderer, std::string file) {
// Inicializa las variables desde un fichero. Si no se pasa fichero, el
// llamante (p.ej. Resource::preloadAll) usará loadFromBytes después —
// y ese método ya llama a setSelectorItemColors() y reset() al final.
if (file != "") {
if (!file.empty()) {
load(file);
setSelectorItemColors();
reset();
@@ -99,24 +99,24 @@ bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
while (std::getline(file, line)) {
if (line == "[item]") {
item_t item;
item.label = "";
item.hPaddingDown = 1;
item.selectable = true;
item.greyed = false;
item.linkedDown = false;
item.visible = true;
item.line = false;
item_t newItem;
newItem.label = "";
newItem.hPaddingDown = 1;
newItem.selectable = true;
newItem.greyed = false;
newItem.linkedDown = false;
newItem.visible = true;
newItem.line = false;
do {
std::getline(file, line);
int pos = line.find("=");
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
if (!setItem(&newItem, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
success = false;
}
} while (line != "[/item]");
addItem(item);
addItem(newItem);
} else {
int pos = line.find("=");
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
@@ -137,7 +137,7 @@ bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
}
// Carga la configuración del menu desde un archivo de texto
bool Menu::load(std::string file_path) {
bool Menu::load(const std::string &file_path) {
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
std::ifstream file(file_path);
if (!file.good()) {
@@ -158,7 +158,7 @@ bool Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &n
}
// Asigna variables a partir de dos cadenas
bool Menu::setItem(item_t *item, std::string var, std::string value) {
bool Menu::setItem(item_t *item, const std::string &var, const std::string &value) {
// Indicador de éxito en la asignación
bool success = true;
@@ -201,7 +201,7 @@ bool Menu::setItem(item_t *item, std::string var, std::string value) {
}
// Asigna variables a partir de dos cadenas
bool Menu::setVars(std::string var, std::string value) {
bool Menu::setVars(const std::string &var, const std::string &value) {
// Indicador de éxito en la asignación
bool success = true;
@@ -319,7 +319,7 @@ bool Menu::setVars(std::string var, std::string value) {
}
// Carga los ficheros de audio
void Menu::loadAudioFile(std::string file, int sound) {
void Menu::loadAudioFile(const std::string &file, int sound) {
switch (sound) {
case SOUND_ACCEPT:
soundAccept = JA_LoadSound(file.c_str());
@@ -339,7 +339,7 @@ void Menu::loadAudioFile(std::string file, int sound) {
}
// Obtiene el nombre del menu
std::string Menu::getName() {
const std::string &Menu::getName() const {
return name;
}
@@ -430,14 +430,8 @@ void Menu::setSelectorPos(int index) {
// Obtiene la anchura del elemento más ancho del menu
int Menu::getWidestItem() {
int result = 0;
// Obtenemos la anchura del item mas ancho
for (auto &i : item) {
result = std::max(result, i.rect.w);
}
return result;
return std::accumulate(item.begin(), item.end(), 0,
[](int acc, const item_t &i) { return std::max(acc, i.rect.w); });
}
// Deja el menu apuntando al primer elemento
@@ -474,7 +468,7 @@ void Menu::reorganize() {
}
// Deja el menu apuntando al siguiente elemento
bool Menu::increaseSelectorIndex() {
void Menu::increaseSelectorIndex() {
// Guarda el indice actual antes de modificarlo
selector.previousIndex = selector.index;
@@ -499,12 +493,10 @@ bool Menu::increaseSelectorIndex() {
if (selector.incH != 0) {
selector.resizing = true;
}
return true;
}
// Deja el menu apuntando al elemento anterior
bool Menu::decreaseSelectorIndex() {
void Menu::decreaseSelectorIndex() {
// Guarda el indice actual antes de modificarlo
selector.previousIndex = selector.index;
@@ -538,8 +530,6 @@ bool Menu::decreaseSelectorIndex() {
if (selector.incH != 0) {
selector.resizing = true;
}
return true;
}
// Actualiza la logica del menu
@@ -752,7 +742,7 @@ void Menu::addItem(item_t temp) {
}
// Cambia el texto de un item
void Menu::setItemCaption(int index, std::string text) {
void Menu::setItemCaption(int index, const std::string &text) {
item[index].label = text;
item[index].rect.w = this->text->lenght(item[index].label);
item[index].rect.h = this->text->getCharacterSize();
@@ -767,18 +757,16 @@ void Menu::setDefaultActionWhenCancel(int item) {
// Gestiona la entrada de teclado y mando durante el menu
void Menu::checkInput() {
if (Input::get()->checkInput(input_up, REPEAT_FALSE)) {
if (decreaseSelectorIndex()) {
if (soundMove) {
JA_PlaySound(soundMove);
}
decreaseSelectorIndex();
if (soundMove) {
JA_PlaySound(soundMove);
}
}
if (Input::get()->checkInput(input_down, REPEAT_FALSE)) {
if (increaseSelectorIndex()) {
if (soundMove) {
JA_PlaySound(soundMove);
}
increaseSelectorIndex();
if (soundMove) {
JA_PlaySound(soundMove);
}
}
@@ -804,12 +792,8 @@ int Menu::findWidth() {
// Calcula el alto del menu
int Menu::findHeight() {
int height = 0;
// Obtenemos la altura de la suma de alturas de los items
for (auto &i : item) {
height += i.rect.h + i.hPaddingDown;
}
const int height = std::accumulate(item.begin(), item.end(), 0,
[](int acc, const item_t &i) { return acc + i.rect.h + i.hPaddingDown; });
return height - item.back().hPaddingDown;
}
@@ -853,7 +837,7 @@ int Menu::getSelectorHeight(int value) {
}
// Establece el nombre del menu
void Menu::setName(std::string name) {
void Menu::setName(const std::string &name) {
this->name = name;
}
@@ -869,7 +853,7 @@ void Menu::setBackgroundType(int value) {
}
// Establece la fuente de texto que se utilizará
void Menu::setText(std::string font_png, std::string font_txt) {
void Menu::setText(const std::string &font_png, const std::string &font_txt) {
if (!text) {
text = new Text(Asset::get()->get(font_png), Asset::get()->get(font_txt), renderer);
}