update menu.cpp

This commit is contained in:
2021-02-19 19:34:06 +01:00
parent 2b49e07a85
commit 06bbb22ce5
2 changed files with 38 additions and 38 deletions

View File

@@ -26,10 +26,10 @@ void Menu::init(std::string name, int x, int y, int backgroundType, LTexture *te
mVerticalPadding = 1;
mPosX = x;
mPosY = y;
mRectBG.rect.x = 0;
mRectBG.rect.y = 0;
mRectBG.rect.w = 0;
mRectBG.rect.h = 0;
mRectBG.x = 0;
mRectBG.y = 0;
mRectBG.w = 0;
mRectBG.h = 0;
mRectBG.r = 0;
mRectBG.g = 0;
mRectBG.b = 0;
@@ -37,10 +37,10 @@ void Menu::init(std::string name, int x, int y, int backgroundType, LTexture *te
mSelector.rect.y = 0;
mSelector.rect.w = 0;
mSelector.rect.h = 0;
mSelector.r = 0;
mSelector.g = 0;
mSelector.b = 0;
mSelector.a = 255;
mSelector.rect.r = 0;
mSelector.rect.g = 0;
mSelector.rect.b = 0;
mSelector.rect.a = 255;
mBackgroundType = backgroundType;
mText = text;
mRenderer = renderer;
@@ -157,7 +157,6 @@ void Menu::reset()
{
mItemSelected = MENU_NO_OPTION;
mSelector.index = 0;
//moveSelectorSprite(mSelector.index);
mSelector.origin = mSelector.target = mSelector.y = mItem[0].y;
mSelector.moving = false;
}
@@ -245,15 +244,17 @@ void Menu::update()
// Pinta el menu en pantalla
void Menu::render()
{
SDL_Rect rect = {mRectBG.x, mRectBG.y, mRectBG.w, mRectBG.h};
// Rendereritza el fondo del menu
if (mBackgroundType == MENU_BACKGROUND_SOLID)
{
SDL_SetRenderDrawColor(mRenderer, mRectBG.r, mRectBG.g, mRectBG.b, mRectBG.a);
SDL_RenderFillRect(mRenderer, &mRectBG.rect);
SDL_RenderFillRect(mRenderer, &rect);
}
// Renderiza el rectangulo del selector
SDL_SetRenderDrawColor(mRenderer, mSelector.r, mSelector.g, mSelector.b, mSelector.a);
SDL_SetRenderDrawColor(mRenderer, mSelector.rect.r, mSelector.rect.g, mSelector.rect.b, mSelector.rect.a);
SDL_RenderFillRect(mRenderer, &mSelector.rect);
// Renderitza el text
@@ -274,37 +275,37 @@ void Menu::render()
void Menu::setRectSize()
{
Uint8 i = 0;
mRectBG.rect.w = 0;
mRectBG.rect.h = 0;
mRectBG.w = 0;
mRectBG.h = 0;
mSelector.rect.w = 0;
mSelector.rect.h = 0;
// Obtenemos la anchura del item mas ancho y la altura de la suma de alturas de los items
for (i = 0; i < mTotalItems; i++)
{
if (mItem[i].w > mRectBG.rect.w)
if (mItem[i].w > mRectBG.w)
{
mRectBG.rect.w = mItem[i].w;
mRectBG.w = mItem[i].w;
}
mRectBG.rect.h += mItem[i].h + mItem[i].hPaddingDown;
mRectBG.h += mItem[i].h + mItem[i].hPaddingDown;
}
// La anchura de la cadena más larga, mas un caracter, mas la anchura del sprite del selector
mRectBG.rect.w += (mText->getHeight() * 1); // + mSelectorSprite.getWidth();
mRectBG.w += (mText->getHeight() * 1); // + mSelectorSprite.getWidth();
// La altura de la suma de los items mas un caracter y menos un pixel (porque el texto en realidad es de 7 pixeles)
mRectBG.rect.h += (mText->getHeight() * 1) - 1;
mRectBG.h += (mText->getHeight() * 1) - 1;
// La posición X es la del menú menos la anchura del sprite del selector y menos medio caracter
mRectBG.rect.x = mPosX - (mText->getHeight() / 2); // - mSelectorSprite.getWidth();
mRectBG.x = mPosX - (mText->getHeight() / 2); // - mSelectorSprite.getWidth();
// La posición Y es la del menu menos la altura de medio caracter i el padding
mRectBG.rect.y = mPosY - (mText->getHeight() / 2) - mVerticalPadding;
mRectBG.y = mPosY - (mText->getHeight() / 2) - mVerticalPadding;
// Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo
mSelector.rect.h = (mText->getHeight() * 1) + 1;
mSelector.rect.w = mRectBG.rect.w;
mSelector.rect.x = mRectBG.rect.x;
mSelector.rect.w = mRectBG.w;
mSelector.rect.x = mRectBG.x;
}
// Establece el valor de la variable
@@ -325,10 +326,10 @@ void Menu::setBackgroundColor(int r, int g, int b, int alpha)
// Establece el color del rectangulo del selector
void Menu::setSelectorColor(int r, int g, int b, int alpha)
{
mSelector.r = r;
mSelector.g = g;
mSelector.b = b;
mSelector.a = alpha;
mSelector.rect.r = r;
mSelector.rect.g = g;
mSelector.rect.b = b;
mSelector.rect.a = alpha;
}
// Establece el color del texto del selector

View File

@@ -114,7 +114,10 @@ private:
struct rectangle
{
SDL_Rect rect; // Rectangulo
Uint8 x; // Posición en el eje X
Uint8 y; // Posición en el eje Y
Uint8 w; // Anchura
Uint8 h; // Altura
Uint8 r; // Rojo
Uint8 g; // Verde
Uint8 b; // Azul
@@ -143,14 +146,10 @@ private:
double target; // Coordenada de destino
double y; // Coordenada actual
Uint8 numJumps; // Numero de pasos preestablecido para llegar al destino
double despY; // (target - origin) / numJumps
double despY; // Desplazamiento en el eje Y que realiza el selector en cada iteración. (target - origin) / numJumps
bool moving; // Indica si el selector está avanzando hacia el destino
Uint8 index; // Elemento del menu que tiene el foco
SDL_Rect rect; // Rectangulo para dibujar el selector
Uint8 r; // Color para el selector. Rojo
Uint8 g; // Color para el selector. Verde
Uint8 b; // Color para el selector. Azul
Uint8 a; // Color para el selector. Transparencia
rectangle rect; // Rectangulo del selectur
Uint8 itemR; // Color para el texto seleccionado. Rojo
Uint8 itemG; // Color para el texto seleccionado. Verde
Uint8 itemB; // Color para el texto seleccionado. Azul