Reestructurados los ficheros en carpetas

This commit is contained in:
2022-10-20 16:31:22 +02:00
parent ce509c6aa6
commit 764463fa56
40 changed files with 50 additions and 50 deletions

195
source/menu.cpp Normal file
View File

@@ -0,0 +1,195 @@
#include "menu.h"
//Constructor
Menu::Menu()
{
init(0, 0, 0, MENU_BACKGROUND_SOLID);
}
//Inicializador
void Menu::init(int x, int y, int offset_sprite_selector, int backgroundType)
{
//Inicia variables
mSelectorIndex = 0;
mTotalItems = 0;
mItemSelected = MENU_NO_OPTION;
mPosX = x;
mPosY = y;
mRect.x = 0;
mRect.y = 0;
mRect.w = 0;
mRect.h = 0;
mRectR = 0;
mRectG = 0;
mRectB = 0;
mBackgroundType = backgroundType;
//Sprite con los graficos del selector
mSelectorSprite.setWidth(8);
mSelectorSprite.setHeight(8);
mSelectorSprite.setPosX(0);
mSelectorSprite.setPosY(0);
mSelectorSprite.setTexture(gMenuTexture);
mSelectorSprite.setSpriteClip(offset_sprite_selector, 0, mSelectorSprite.getWidth(), mSelectorSprite.getHeight());
//Elementos del menu
for (Uint8 i = 0; i < 10; i++)
{
mMenuItem[i].label = "";
mMenuItem[i].x = mPosX;
mMenuItem[i].y = mPosY + (i * (BLOCK + 2));
}
//Mueve el grafico del selector al elemento seleccionado
moveSelectorSprite(mSelectorIndex);
}
//Obtiene el valor de la variable
Uint8 Menu::getItemSelected()
{
return mItemSelected;
};
//Mueve el grafico del selector al elemento seleccionado
void Menu::moveSelectorSprite(int pos)
{
mSelectorSprite.setPosX(mMenuItem[pos].x - (BLOCK * 1));
mSelectorSprite.setPosY(mMenuItem[pos].y);
}
//Deja el menu apuntando al primer elemento
void Menu::resetMenu()
{
mItemSelected = MENU_NO_OPTION;
mSelectorIndex = 0;
moveSelectorSprite(mSelectorIndex);
};
//Deja el menu apuntando al siguiente elemento
void Menu::increaseSelectorIndex()
{
if (mSelectorIndex < (mTotalItems - 1))
{
++mSelectorIndex;
}
};
//Deja el menu apuntando al elemento anterior
void Menu::decreaseSelectorIndex()
{
if (mSelectorIndex > 0)
{
--mSelectorIndex;
}
};
//Comprueba la entrada (teclado, gamepad) y actua en consecuencia
void Menu::checkInput(Uint8 input)
{
switch (input)
{
case INPUT_UP:
decreaseSelectorIndex();
moveSelectorSprite(mSelectorIndex);
break;
case INPUT_DOWN:
increaseSelectorIndex();
moveSelectorSprite(mSelectorIndex);
break;
case INPUT_FIRE:
mItemSelected = mSelectorIndex;
break;
}
}
//Pinta el menu en pantalla
void Menu::render(Text &text)
{
//Render color filled quad
if (mBackgroundType == MENU_BACKGROUND_SOLID)
{
SDL_SetRenderDrawColor(gRenderer, mRectR, mRectG, mRectB, mRectA);
SDL_RenderFillRect(gRenderer, &mRect);
}
//Render text
int i = 0;
mSelectorSprite.render();
for (i = 0; i < mTotalItems; i++)
{
text.write(mMenuItem[i].x, mMenuItem[i].y, mMenuItem[i].label);
}
}
//Establece el rectangulo de fondo del menu
void Menu::setRectSize()
{
int i = 0;
int maxLength = 0;
//La altura se corresponde al numero de items mas un hueco arriba y otro abajo
mRect.h = (mTotalItems + 2) * BLOCK;
//La anchura es la de la cadena mas larga mas tres bloques, uno por la derecha
//y dos por la izquierda, uno de ellos para el selector
for (i = 0; i < mTotalItems; i++)
{
if ((int)mMenuItem[i].label.length() > maxLength)
{
maxLength = mMenuItem[i].label.length();
}
}
mRect.w = (maxLength + 3) * BLOCK;
mRect.x = mPosX - (BLOCK * 2);
mRect.y = mPosY - BLOCK;
}
//Establece el valor de la variable
void Menu::setTotalItems(int num)
{
mTotalItems = num;
}
//Establece el color del rectangulo de fondo
void Menu::setBackgroundColor(int r, int g, int b, int alpha)
{
mRectR = r;
mRectG = g;
mRectB = b;
mRectA = alpha;
}
//Centra el menu en pantalla
void Menu::centerMenuOnScreen()
{
//Actualiza el rectangulo de fondo para recalcular las dimensiones
setRectSize();
//Establece la nueva posición centrada en funcion del ancho
//de la pantalla y del ancho del rectangulo
mPosX = (SCREEN_WIDTH / 2) - (mRect.w / 2) + (BLOCK * 2);
//Reposiciona los elementos del menu
for (Uint8 i = 0; i < 10; i++)
{
mMenuItem[i].x = mPosX;
}
//Recalcula el rectangulo de fondo
setRectSize();
//Recoloca el selector
moveSelectorSprite(mSelectorIndex);
}
//Añade un item al menu
void Menu::addItem(std::string text)
{
if (mTotalItems < 10)
{
mMenuItem[mTotalItems].label = text;
setTotalItems(mTotalItems + 1);
setRectSize();
}
}