working on class sections
This commit is contained in:
@@ -4,20 +4,26 @@
|
||||
#include "menu.h"
|
||||
|
||||
// Constructor
|
||||
Menu::Menu()
|
||||
Menu::Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList)
|
||||
{
|
||||
init("", 0, 0, 0, nullptr, nullptr, nullptr);
|
||||
mRenderer = renderer;
|
||||
mText = text;
|
||||
mInput = input;
|
||||
mFileList = fileList;
|
||||
}
|
||||
|
||||
Menu::~Menu()
|
||||
{
|
||||
mRenderer = nullptr;
|
||||
mText = nullptr;
|
||||
mInput = nullptr;
|
||||
}
|
||||
|
||||
// Inicializador
|
||||
void Menu::init(std::string name, int x, int y, int backgroundType, LTexture *texture, SDL_Renderer *renderer, Text *text)
|
||||
void Menu::init(std::string name, int x, int y, int backgroundType)
|
||||
{
|
||||
loadMedia();
|
||||
|
||||
// Inicia variables
|
||||
mName = name;
|
||||
mSelector.index = 0;
|
||||
@@ -42,8 +48,6 @@ void Menu::init(std::string name, int x, int y, int backgroundType, LTexture *te
|
||||
mSelector.b = 0;
|
||||
mSelector.a = 255;
|
||||
mBackgroundType = backgroundType;
|
||||
mText = text;
|
||||
mRenderer = renderer;
|
||||
mIsCentered = false;
|
||||
mAreElementsCentered = false;
|
||||
mCenter = x + ((SCREEN_WIDTH - x) / 2);
|
||||
@@ -82,6 +86,20 @@ void Menu::init(std::string name, int x, int y, int backgroundType, LTexture *te
|
||||
//moveSelectorSprite(mSelector.index);
|
||||
}
|
||||
|
||||
// Carga los recursos necesarios para la sección 'Title'
|
||||
bool Menu::loadMedia()
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Sonidos
|
||||
mSoundMove = JA_LoadSound(mFileList[17].c_str());
|
||||
mSoundAccept = JA_LoadSound(mFileList[18].c_str());
|
||||
mSoundCancel = JA_LoadSound(mFileList[16].c_str());
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Obtiene el nombre del menu
|
||||
std::string Menu::getName()
|
||||
{
|
||||
@@ -91,7 +109,9 @@ std::string Menu::getName()
|
||||
// Obtiene el valor de la variable
|
||||
Uint8 Menu::getItemSelected()
|
||||
{
|
||||
return mItemSelected;
|
||||
const int temp = mItemSelected;
|
||||
mItemSelected = MENU_NO_OPTION;
|
||||
return temp;
|
||||
}
|
||||
|
||||
// Mueve el grafico del selector al elemento seleccionado
|
||||
@@ -159,7 +179,7 @@ Uint16 Menu::getWidestItem()
|
||||
Uint16 result = 0;
|
||||
|
||||
// Obtenemos la anchura del item mas ancho
|
||||
for (Uint8 i = 0; i < mTotalItems; i++)
|
||||
for (int i = 0; i < mTotalItems; i++)
|
||||
{
|
||||
if (mItem[i].w > result)
|
||||
{
|
||||
@@ -232,30 +252,6 @@ bool Menu::decreaseSelectorIndex()
|
||||
return success;
|
||||
}
|
||||
|
||||
// Comprueba la entrada (teclado, gamepad) y actua en consecuencia
|
||||
bool Menu::checkInput(Uint8 input)
|
||||
{
|
||||
bool success = false;
|
||||
switch (input)
|
||||
{
|
||||
case INPUT_UP:
|
||||
success = decreaseSelectorIndex();
|
||||
//moveSelectorSprite(mSelector.index);
|
||||
break;
|
||||
case INPUT_DOWN:
|
||||
success = increaseSelectorIndex();
|
||||
//moveSelectorSprite(mSelector.index);
|
||||
break;
|
||||
case INPUT_ACCEPT:
|
||||
mItemSelected = mSelector.index;
|
||||
break;
|
||||
case INPUT_CANCEL:
|
||||
mItemSelected = mDefaultActionWhenCancel;
|
||||
break;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
// Actualiza la logica del menu
|
||||
void Menu::update()
|
||||
{
|
||||
@@ -280,11 +276,12 @@ void Menu::render()
|
||||
//mSelectorSprite.render();
|
||||
|
||||
// Renderitza el text
|
||||
for (Uint8 i = 0; i < mTotalItems; i++)
|
||||
for (int i = 0; i < mTotalItems; i++)
|
||||
{
|
||||
if (i == mSelector.index)
|
||||
{
|
||||
mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, mSelector.itemR, mSelector.itemG, mSelector.itemB);
|
||||
const color_t color = {mSelector.itemR, mSelector.itemG, mSelector.itemB};
|
||||
mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -378,7 +375,7 @@ void Menu::centerMenu(int value)
|
||||
mPosX = (value) - (mWidestItem / 2);
|
||||
|
||||
// Reposiciona los elementos del menu
|
||||
for (Uint8 i = 0; i < 10; i++)
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
mItem[i].x = mPosX;
|
||||
}
|
||||
@@ -397,7 +394,7 @@ void Menu::centerMenuElements()
|
||||
{
|
||||
mAreElementsCentered = true;
|
||||
|
||||
for (Uint8 i = 0; i < mTotalItems; i++)
|
||||
for (int i = 0; i < mTotalItems; i++)
|
||||
{
|
||||
//mItem[i].x = (mCenter - ((mText->lenght(mItem[i].label, 0)) / 2));
|
||||
mItem[i].x = (mCenter - (mItem[i].w / 2));
|
||||
@@ -453,3 +450,31 @@ void Menu::setDefaultActionWhenCancel(Uint8 item)
|
||||
{
|
||||
mDefaultActionWhenCancel = item;
|
||||
}
|
||||
|
||||
// Gestiona la entrada de teclado y mando durante el menu
|
||||
void Menu::checkInput()
|
||||
{
|
||||
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
|
||||
{
|
||||
if (decreaseSelectorIndex())
|
||||
JA_PlaySound(mSoundMove);
|
||||
}
|
||||
|
||||
if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
|
||||
{
|
||||
if (increaseSelectorIndex())
|
||||
JA_PlaySound(mSoundMove);
|
||||
}
|
||||
|
||||
if (mInput->checkInput(INPUT_ACCEPT, REPEAT_FALSE))
|
||||
{
|
||||
mItemSelected = mSelector.index;
|
||||
JA_PlaySound(mSoundAccept);
|
||||
}
|
||||
|
||||
if (mInput->checkInput(INPUT_CANCEL, REPEAT_FALSE))
|
||||
{
|
||||
mItemSelected = mDefaultActionWhenCancel;
|
||||
JA_PlaySound(mSoundCancel);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user