Improving menu class. Selector flickers when selecting

This commit is contained in:
2021-08-30 17:16:53 +02:00
parent 005eab5694
commit 74c08884ae
6 changed files with 58 additions and 43 deletions

View File

@@ -215,8 +215,7 @@ void Menu::reset()
mSelector.originY = mSelector.targetY = mSelector.y = mItem[0].y;
mSelector.originH = mSelector.targetH = mItem[0].h;
mSelector.moving = false;
mSelector.resizing = false;
mSelector.resizing = false;
}
// Deja el menu sin elemento seleccionado
@@ -311,7 +310,7 @@ void Menu::render()
SDL_RenderDrawRect(mRenderer, &mRectBG.rect);
}
// Renderitza el text
// Renderitza el texto
for (int i = 0; i < mTotalItems; i++)
{
if (i == mSelector.index)
@@ -345,10 +344,8 @@ void Menu::render()
// Establece el rectangulo de fondo del menu y el selector
void Menu::setRectSize()
{
findHeight();
findWidth();
mRectBG.rect.w = mWidth;
mRectBG.rect.h = mHeight;
mRectBG.rect.w = findWidth();
mRectBG.rect.h = findHeight();
// La posición X es la del menú menos medio caracter
mRectBG.rect.x = mPosX - (mText->getCharacterWidth() / 2);
@@ -357,7 +354,6 @@ void Menu::setRectSize()
mRectBG.rect.y = mPosY - (mText->getCharacterWidth() / 2) - mVerticalPadding;
// Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo
//mSelector.rect.h = (mText->getCharacterWidth() * 1) + 1;
mSelector.rect.h = getSelectorHeight(mSelector.index);
mSelector.rect.w = mRectBG.rect.w;
mSelector.rect.x = mRectBG.rect.x;
@@ -438,7 +434,7 @@ void Menu::centerMenuOnY(int value)
setRectSize();
// Obten el alto del menu
findHeight();
mHeight = findHeight();
// Establece la nueva posición centrada en funcion del elemento más ancho
mPosY = (value) - (mHeight / 2);
@@ -534,30 +530,34 @@ void Menu::checkInput()
}
// Calcula el ancho del menu
void Menu::findWidth()
Uint16 Menu::findWidth()
{
mWidth = 0;
Uint16 width = 0;
// Obtenemos la anchura del item mas ancho
for (int i = 0; i < mTotalItems; i++)
if (mItem[i].w > mWidth)
mWidth = mItem[i].w;
if (mItem[i].w > width)
width = mItem[i].w;
// La anchura de la cadena más larga, mas un caracter
mWidth += (mText->getCharacterWidth() * 1);
width += (mText->getCharacterWidth() * 1);
return width;
}
// Calcula el alto del menu
void Menu::findHeight()
Uint16 Menu::findHeight()
{
mHeight = 0;
Uint16 height = 0;
// Obtenemos la altura de la suma de alturas de los items
for (int i = 0; i < mTotalItems; i++)
mHeight += mItem[i].h + mItem[i].hPaddingDown;
height += mItem[i].h + mItem[i].hPaddingDown;
// La altura de la suma de los items mas un caracter y menos un pixel (porque el texto en realidad es de 7 pixeles)
mHeight += (mText->getCharacterWidth() * 1) - 1;
height += (mText->getCharacterWidth() * 1) - 1;
return height;
}
// Recoloca los elementos del menu en el eje Y
@@ -566,9 +566,7 @@ void Menu::replaceElementsOnY()
mItem[0].y = mPosY;
for (int i = 1; i < mTotalItems; i++)
{
mItem[i].y = mItem[i - 1].y + mItem[i - 1].h + mItem[i - 1].hPaddingDown;
}
}
// Establece el estado seleccionable de un item
@@ -593,11 +591,7 @@ void Menu::setLinkedDown(Uint8 index, bool value)
int Menu::getSelectorHeight(int value)
{
if (mItem[value].linkedDown)
{
return mItem[value + 1].y + mItem[value + 1].h - mItem[value].y - 1;
}
else
{
return mItem[value].h - 1;
}
}