From 6aebb28f375cfb80378d3801e102d95ab437ecec Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Wed, 7 Dec 2022 08:37:16 +0100 Subject: [PATCH] Actualizado menu.cpp --- source/common/menu.cpp | 82 +++++++++++++++++++++--------------------- source/common/menu.h | 2 +- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/source/common/menu.cpp b/source/common/menu.cpp index 4d9a92d..67ea0f0 100644 --- a/source/common/menu.cpp +++ b/source/common/menu.cpp @@ -1,4 +1,6 @@ +#include "../const.h" #include "menu.h" +#include // Constructor Menu::Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file) @@ -106,7 +108,7 @@ bool Menu::load(std::string file_path) if (file.good()) { // Procesa el fichero linea a linea - printf("Reading file %s\n", filename.c_str()); + std::cout << "Reading file " << filename.c_str() << std::endl; while (std::getline(file, line)) { if (line == "[item]") @@ -129,7 +131,7 @@ bool Menu::load(std::string file_path) // Procesa las dos subcadenas if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) { - printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); + std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; success = false; } @@ -146,7 +148,7 @@ bool Menu::load(std::string file_path) // Procesa las dos subcadenas if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) { - printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); + std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; success = false; } @@ -160,13 +162,13 @@ bool Menu::load(std::string file_path) } // Cierra el fichero - printf("Closing file %s\n", filename.c_str()); + std::cout << "Closing file " << filename.c_str() << std::endl; file.close(); } // El fichero no se puede abrir else { - printf("Warning: Unable to open %s file\n", filename.c_str()); + std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; success = false; } @@ -207,7 +209,7 @@ bool Menu::setItem(item_t *item, std::string var, std::string value) else if ((var == "") || (var == "[/item]")) { } - + else { success = false; @@ -404,7 +406,7 @@ void Menu::updateSelector() selector.moving = false; } } - + else if (selector.despY < 0) // Va hacia arriba { if (selector.y < selector.targetY) // Ha llegado al destino @@ -433,7 +435,7 @@ void Menu::updateSelector() selector.resizing = false; } } - + else if (selector.incH < 0) // Decrece { if (selector.h < selector.targetH) // Ha llegado al destino @@ -457,7 +459,7 @@ void Menu::setSelectorPos(int index) if (index < (int)item.size()) { selector.index = index; - selector.rect.y = selector.y = selector.originY = selector.targetY = item.at(selector.index).rect.y; + selector.rect.y = selector.y = selector.originY = selector.targetY = item[selector.index].rect.y; selector.rect.w = rectBG.rect.w; selector.rect.x = rectBG.rect.x; selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index); @@ -485,13 +487,13 @@ void Menu::reset() { itemSelected = MENU_NO_OPTION; selector.index = 0; - selector.originY = selector.targetY = selector.y = item.at(0).rect.y; - selector.originH = selector.targetH = item.at(0).rect.h; + selector.originY = selector.targetY = selector.y = item[0].rect.y; + selector.originH = selector.targetH = item[0].rect.h; selector.moving = false; selector.resizing = false; // Si el primer elemento no es seleccionable, incrementa el selector - if (!item.at(selector.index).selectable) + if (!item[selector.index].selectable) { increaseSelectorIndex(); setSelectorPos(selector.index); @@ -523,18 +525,18 @@ void Menu::reorganize() bool Menu::increaseSelectorIndex() { // Obten las coordenadas del elemento actual - selector.y = selector.originY = item.at(selector.index).rect.y; + selector.y = selector.originY = item[selector.index].rect.y; selector.h = selector.originH = getSelectorHeight(selector.index); // Calcula cual es el siguiente elemento ++selector.index %= item.size(); - while (!item.at(selector.index).selectable) + while (!item[selector.index].selectable) { ++selector.index %= item.size(); } // Establece las coordenadas y altura de destino - selector.targetY = item.at(selector.index).rect.y; + selector.targetY = item[selector.index].rect.y; selector.despY = (selector.targetY - selector.originY) / selector.numJumps; selector.targetH = getSelectorHeight(selector.index); @@ -553,7 +555,7 @@ bool Menu::increaseSelectorIndex() bool Menu::decreaseSelectorIndex() { // Obten las coordenadas del elemento actual - selector.y = selector.originY = item.at(selector.index).rect.y; + selector.y = selector.originY = item[selector.index].rect.y; selector.h = selector.originH = getSelectorHeight(selector.index); // Calcula cual es el siguiente elemento @@ -566,7 +568,7 @@ bool Menu::decreaseSelectorIndex() selector.index--; } - while (!item.at(selector.index).selectable) + while (!item[selector.index].selectable) { if (selector.index == 0) { @@ -579,7 +581,7 @@ bool Menu::decreaseSelectorIndex() } // Establece las coordenadas y altura de destino - selector.targetY = item.at(selector.index).rect.y; + selector.targetY = item[selector.index].rect.y; selector.despY = (selector.targetY - selector.originY) / selector.numJumps; selector.targetH = getSelectorHeight(selector.index); @@ -629,29 +631,29 @@ void Menu::render() if (i == selector.index) { const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b}; - text->writeColored(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label, color); + text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color); } - else if (item.at(i).selectable) + else if (item[i].selectable) { - text->write(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label); + text->write(item[i].rect.x, item[i].rect.y, item[i].label); } - else if (item.at(i).greyed) + else if (item[i].greyed) { - text->writeColored(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label, colorGreyed); + text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, colorGreyed); } - + else { // No seleccionable - if ((item.at(i).linkedUp) && (i == selector.index + 1)) + if ((item[i].linkedUp) && (i == selector.index + 1)) { const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b}; - text->writeColored(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label, color); + text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color); } else // No enlazado con el de arriba { - text->write(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label); + text->write(item[i].rect.x, item[i].rect.y, item[i].label); } } } @@ -821,7 +823,7 @@ void Menu::addItem(std::string text, int hPaddingDown, bool selectable, bool gre if (item.size() > 1) { - if (item.at(item.size() - 2).linkedDown) + if (item[item.size() - 2].linkedDown) { item.back().linkedUp = true; } @@ -834,9 +836,9 @@ void Menu::addItem(std::string text, int hPaddingDown, bool selectable, bool gre // Cambia el texto de un item void Menu::setItemCaption(int index, std::string text) { - item.at(index).label = text; - item.at(index).rect.w = this->text->lenght(item.at(index).label); - item.at(index).rect.h = this->text->getCharacterSize(); + item[index].label = text; + item[index].rect.w = this->text->lenght(item[index].label); + item[index].rect.h = this->text->getCharacterSize(); reorganize(); } @@ -913,42 +915,42 @@ int Menu::findHeight() // Recoloca los elementos del menu en el eje Y void Menu::replaceElementsOnY() { - item.at(0).rect.y = y; + item[0].rect.y = y; for (int i = 1; i < (int)item.size(); i++) { - item.at(i).rect.y = item.at(i - 1).rect.y + item.at(i - 1).rect.h + item.at(i - 1).hPaddingDown; + item[i].rect.y = item[i - 1].rect.y + item[i - 1].rect.h + item[i - 1].hPaddingDown; } } // Establece el estado seleccionable de un item void Menu::setSelectable(int index, bool value) { - item.at(index).selectable = value; + item[index].selectable = value; } // Establece el estado agrisado de un item void Menu::setGreyed(int index, bool value) { - item.at(index).greyed = value; + item[index].greyed = value; } // Establece el estado de enlace de un item void Menu::setLinkedDown(int index, bool value) { - item.at(index).linkedDown = value; + item[index].linkedDown = value; } // Calcula la altura del selector int Menu::getSelectorHeight(int value) { - if (item.at(value).linkedDown) + if (item[value].linkedDown) { - return item.at(value).rect.h + item.at(value).hPaddingDown + item.at(value + 1).rect.h; + return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h; } else { - return item.at(value).rect.h; + return item[value].rect.h; } } @@ -976,6 +978,6 @@ void Menu::setText(std::string font_png, std::string font_txt) { if (!text) { - text = new Text(font_png, font_txt, renderer); + text = new Text(asset->get(font_png), asset->get(font_txt), renderer); } } \ No newline at end of file diff --git a/source/common/menu.h b/source/common/menu.h index 5534b65..bedac0f 100644 --- a/source/common/menu.h +++ b/source/common/menu.h @@ -70,9 +70,9 @@ private: // Objetos y punteros SDL_Renderer *renderer; // Puntero al renderizador de la ventana + Asset *asset; // Objeto para gestionar los ficheros de recursos Text *text; // Texto para poder escribir los items del menu Input *input; // Gestor de eventos de entrada de teclado o gamepad - Asset *asset; // Objeto para gestionar los ficheros de recursos // Variables std::string name; // Nombre del menu