Retocando la clase menu

This commit is contained in:
2022-08-24 13:58:52 +02:00
parent 0749862af2
commit 1f4d094593
4 changed files with 182 additions and 77 deletions

View File

@@ -2,12 +2,15 @@
#include "menu.h"
// Constructor
Menu::Menu(SDL_Renderer *renderer, Text *text, Input *input, Asset *asset)
Menu::Menu(SDL_Renderer *renderer, Text *text, Input *input)
{
this->renderer = renderer;
this->text = text;
this->input = input;
this->asset = asset;
soundMove = nullptr;
soundAccept = nullptr;
soundCancel = nullptr;
}
Menu::~Menu()
@@ -15,21 +18,26 @@ Menu::~Menu()
renderer = nullptr;
text = nullptr;
input = nullptr;
asset = nullptr;
JA_DeleteSound(soundAccept);
JA_DeleteSound(soundCancel);
JA_DeleteSound(soundMove);
if (soundMove)
{
JA_DeleteSound(soundMove);
}
if (soundAccept)
{
JA_DeleteSound(soundAccept);
}
if (soundCancel)
{
JA_DeleteSound(soundCancel);
}
}
// Inicializador
void Menu::init(std::string name, int x, int y, int backgroundType)
{
// Sonidos
soundMove = JA_LoadSound(asset->get("sound_menu_move.wav").c_str());
soundAccept = JA_LoadSound(asset->get("sound_menu_select.wav").c_str());
soundCancel = JA_LoadSound(asset->get("sound_menu_cancel.wav").c_str());
// Inicia variables
this->name = name;
selector.index = 0;
@@ -66,6 +74,28 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
selector.a = 255;
}
// Carga los ficheros de audio
void Menu::loadAudioFile(std::string file, int sound)
{
switch (sound)
{
case SOUND_ACCEPT:
soundAccept = JA_LoadSound(file.c_str());
break;
case SOUND_CANCEL:
soundCancel = JA_LoadSound(file.c_str());
break;
case SOUND_MOVE:
soundMove = JA_LoadSound(file.c_str());
break;
default:
break;
}
}
// Obtiene el nombre del menu
std::string Menu::getName()
{
@@ -185,19 +215,26 @@ void Menu::reset()
void Menu::reorganize()
{
setRectSize();
if (isCenteredOnX)
{
centerMenuOnX(centerX);
}
if (isCenteredOnY)
{
centerMenuOnY(centerY);
}
if (areElementsCenteredOnX)
{
centerMenuElementsOnX();
}
}
// Deja el menu apuntando al siguiente elemento
bool Menu::increaseSelectorIndex()
{
bool success = false;
// Obten las coordenadas del elemento actual
selector.y = selector.originY = item[selector.index].rect.y;
selector.h = selector.originH = getSelectorHeight(selector.index);
@@ -208,30 +245,26 @@ bool Menu::increaseSelectorIndex()
{
++selector.index %= item.size();
}
success = true;
// Establece las coordenadas y altura de destino
if (success)
selector.targetY = item[selector.index].rect.y;
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
selector.targetH = getSelectorHeight(selector.index);
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
selector.moving = true;
if (selector.incH != 0)
{
selector.targetY = item[selector.index].rect.y;
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
selector.targetH = getSelectorHeight(selector.index);
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
selector.moving = true;
if (selector.incH != 0)
selector.resizing = true;
selector.resizing = true;
}
return success;
return true;
}
// Deja el menu apuntando al elemento anterior
bool Menu::decreaseSelectorIndex()
{
bool success = false;
// Obten las coordenadas del elemento actual
selector.y = selector.originY = item[selector.index].rect.y;
selector.h = selector.originH = getSelectorHeight(selector.index);
@@ -245,35 +278,39 @@ bool Menu::decreaseSelectorIndex()
{
selector.index--;
}
while (!item[selector.index].selectable)
{
if (selector.index == 0)
{
selector.index = item.size();
}
else
{
selector.index--;
}
}
success = true;
// Establece las coordenadas y altura de destino
if (success)
selector.targetY = item[selector.index].rect.y;
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
selector.targetH = getSelectorHeight(selector.index);
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
selector.moving = true;
if (selector.incH != 0)
{
selector.targetY = item[selector.index].rect.y;
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
selector.targetH = getSelectorHeight(selector.index);
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
selector.moving = true;
if (selector.incH != 0)
selector.resizing = true;
selector.resizing = true;
}
return success;
return true;
}
// Actualiza la logica del menu
void Menu::update()
{
checkInput();
updateSelector();
}
@@ -288,9 +325,9 @@ void Menu::render()
}
// Renderiza el rectangulo del selector
SDL_Rect temp = selector.rect;
temp.y--;
temp.h++;
const SDL_Rect temp = {selector.rect.x, selector.rect.y - 1, selector.rect.w, selector.rect.h + 1};
// temp.y--;
// temp.h++;
SDL_SetRenderDrawColor(renderer, selector.color.r, selector.color.g, selector.color.b, selector.a);
SDL_RenderFillRect(renderer, &temp);
@@ -317,7 +354,8 @@ void Menu::render()
{
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, colorGreyed);
}
else // No seleccionable
else
// No seleccionable
{
if ((item[i].linkedUp) && (i == selector.index + 1))
{
@@ -436,11 +474,15 @@ void Menu::addItem(std::string text, int hPaddingDown, bool selectable, bool gre
temp.linkedDown = linkedDown;
item.push_back(temp);
setItemCaption(item.size(), text);
setItemCaption(item.size() - 1, text);
if (item.size() > 0)
{
if (item[item.size() - 1].linkedDown)
{
item[item.size()].linkedUp = true;
}
}
centerX = x + (findWidth() / 2);
reorganize();
@@ -465,23 +507,43 @@ void Menu::setDefaultActionWhenCancel(int item)
void Menu::checkInput()
{
if (input->checkInput(INPUT_UP, REPEAT_FALSE))
{
if (decreaseSelectorIndex())
JA_PlaySound(soundMove);
{
if (soundMove)
{
JA_PlaySound(soundMove);
}
}
}
if (input->checkInput(INPUT_DOWN, REPEAT_FALSE))
{
if (increaseSelectorIndex())
JA_PlaySound(soundMove);
{
if (soundMove)
{
JA_PlaySound(soundMove);
}
}
}
if (input->checkInput(INPUT_ACCEPT, REPEAT_FALSE))
{
itemSelected = selector.index;
JA_PlaySound(soundAccept);
if (soundAccept)
{
JA_PlaySound(soundAccept);
}
}
if (input->checkInput(INPUT_CANCEL, REPEAT_FALSE))
{
itemSelected = defaultActionWhenCancel;
JA_PlaySound(soundCancel);
if (soundCancel)
{
JA_PlaySound(soundCancel);
}
}
}
@@ -511,7 +573,9 @@ void Menu::replaceElementsOnY()
item[0].rect.y = y;
for (int i = 1; i < item.size(); i++)
{
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
@@ -536,7 +600,11 @@ void Menu::setLinkedDown(int index, bool value)
int Menu::getSelectorHeight(int value)
{
if (item[value].linkedDown)
{
return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h;
}
else
{
return item[value].rect.h;
}
}