menu selector now resizes smooth

This commit is contained in:
2021-08-30 12:39:11 +02:00
parent 384cfa7156
commit eb7f534b3b
5 changed files with 145 additions and 67 deletions

View File

@@ -81,6 +81,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device)
} }
} }
if (gameControllerFound())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
{ {
if (repeat) if (repeat)
@@ -160,3 +161,12 @@ bool Input::discoverGameController()
return found; return found;
} }
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound()
{
if (mNumGamepads > 0)
return true;
else
return false;
}

View File

@@ -71,6 +71,9 @@ public:
// Comprueba si un input esta activo // Comprueba si un input esta activo
bool checkInput(Uint8 input, bool repeat, int device=INPUT_USE_ANY); bool checkInput(Uint8 input, bool repeat, int device=INPUT_USE_ANY);
// Comprueba si hay algun mando conectado
bool gameControllerFound();
}; };
#endif #endif

View File

@@ -38,14 +38,6 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
mRectBG.r = 0; mRectBG.r = 0;
mRectBG.g = 0; mRectBG.g = 0;
mRectBG.b = 0; mRectBG.b = 0;
mSelector.rect.x = 0;
mSelector.rect.y = 0;
mSelector.rect.w = 0;
mSelector.rect.h = 0;
mSelector.r = 0;
mSelector.g = 0;
mSelector.b = 0;
mSelector.a = 255;
mBackgroundType = backgroundType; mBackgroundType = backgroundType;
mIsCenteredOnX = false; mIsCenteredOnX = false;
mIsCenteredOnY = false; mIsCenteredOnY = false;
@@ -56,12 +48,24 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
mColorGreyed = {128, 128, 128}; mColorGreyed = {128, 128, 128};
// Selector // Selector
mSelector.origin = 0; mSelector.originY = 0;
mSelector.target = 0; mSelector.targetY = 0;
mSelector.despY = 0;
mSelector.originH = 0;
mSelector.targetH = 0;
mSelector.incH = 0;
mSelector.y = 0; mSelector.y = 0;
mSelector.numJumps = 4; mSelector.numJumps = 4;
mSelector.despY = 0;
mSelector.moving = false; mSelector.moving = false;
mSelector.resizing = false;
mSelector.rect.x = 0;
mSelector.rect.y = 0;
mSelector.rect.w = 0;
mSelector.rect.h = 0;
mSelector.r = 0;
mSelector.g = 0;
mSelector.b = 0;
mSelector.a = 255;
// Elementos del menu // Elementos del menu
for (int i = 0; i < MENU_MAX_ITEMS; i++) for (int i = 0; i < MENU_MAX_ITEMS; i++)
@@ -109,20 +113,21 @@ void Menu::updateSelector()
{ {
if (mSelector.moving) if (mSelector.moving)
{ {
// Calcula el desplazamiento en Y
mSelector.y += mSelector.despY; mSelector.y += mSelector.despY;
if (mSelector.despY > 0) // Va hacia abajo if (mSelector.despY > 0) // Va hacia abajo
{ {
if (mSelector.y > mSelector.target) // Ha llegado al destino if (mSelector.y > mSelector.targetY) // Ha llegado al destino
{ {
mSelector.origin = mSelector.y = mSelector.target; mSelector.originY = mSelector.y = mSelector.targetY;
mSelector.moving = false; mSelector.moving = false;
} }
} }
if (mSelector.despY < 0) // Va hacia abajo if (mSelector.despY < 0) // Va hacia arriba
{ {
if (mSelector.y < mSelector.target) // Ha llegado al destino if (mSelector.y < mSelector.targetY) // Ha llegado al destino
{ {
mSelector.origin = mSelector.y = mSelector.target; mSelector.originY = mSelector.y = mSelector.targetY;
mSelector.moving = false; mSelector.moving = false;
} }
} }
@@ -132,18 +137,45 @@ void Menu::updateSelector()
{ {
mSelector.rect.y = int(mSelector.y) - 1; mSelector.rect.y = int(mSelector.y) - 1;
} }
if (mSelector.resizing)
{
// Calcula el incremento en H
mSelector.rect.h += mSelector.incH;
if (mSelector.incH > 0) // Crece
{
if (mSelector.rect.h > mSelector.targetH) // Ha llegado al destino
{
mSelector.originH = mSelector.rect.h = mSelector.targetH;
mSelector.resizing = false;
}
}
if (mSelector.incH < 0) // Decrece
{
if (mSelector.rect.h < mSelector.targetH) // Ha llegado al destino
{
mSelector.originH = mSelector.rect.h = mSelector.targetH;
mSelector.resizing = false;
}
}
//mSelector.rect.h = getSelectorHeight(mSelector.index);
}
else
{
mSelector.rect.h = getSelectorHeight(mSelector.index);
}
} }
// Establece el origen del selector // Establece el origen del selector
void Menu::setSelectorOrigin(int value) void Menu::setSelectorOrigin(int value)
{ {
mSelector.origin = value; mSelector.originY = value;
} }
// Establece el destino del selector // Establece el destino del selector
void Menu::setSelectorTarget(int value) void Menu::setSelectorTarget(int value)
{ {
mSelector.target = value; mSelector.targetY = value;
} }
// Coloca el selector en una posición específica // Coloca el selector en una posición específica
@@ -152,8 +184,9 @@ void Menu::setSelectorPos(Uint8 index)
if (index < mTotalItems) if (index < mTotalItems)
{ {
mSelector.index = index; mSelector.index = index;
mSelector.y = mSelector.origin = mSelector.target = mItem[mSelector.index].y; mSelector.y = mSelector.originY = mSelector.targetY = mItem[mSelector.index].y;
mSelector.moving = false; mSelector.moving = false;
mSelector.resizing = false;
} }
} }
@@ -179,8 +212,11 @@ void Menu::reset()
{ {
mItemSelected = MENU_NO_OPTION; mItemSelected = MENU_NO_OPTION;
mSelector.index = 0; mSelector.index = 0;
mSelector.origin = mSelector.target = mSelector.y = mItem[0].y; mSelector.originY = mSelector.targetY = mSelector.y = mItem[0].y;
mSelector.originH = mSelector.targetH = mItem[0].h;
mSelector.moving = false; mSelector.moving = false;
mSelector.resizing = false;
} }
// Deja el menu sin elemento seleccionado // Deja el menu sin elemento seleccionado
@@ -205,7 +241,8 @@ void Menu::reorganize()
bool Menu::increaseSelectorIndex() bool Menu::increaseSelectorIndex()
{ {
bool success = false; bool success = false;
mSelector.y = mSelector.origin = mItem[mSelector.index].y; mSelector.y = mSelector.originY = mItem[mSelector.index].y;
mSelector.rect.h = mSelector.originH = getSelectorHeight(mSelector.index);
if (mSelector.index < (mTotalItems - 1)) if (mSelector.index < (mTotalItems - 1))
{ {
@@ -215,10 +252,12 @@ bool Menu::increaseSelectorIndex()
success = true; success = true;
} }
mSelector.target = mItem[mSelector.index].y; mSelector.targetY = mItem[mSelector.index].y;
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps; mSelector.despY = (mSelector.targetY - mSelector.originY) / mSelector.numJumps;
mSelector.targetH = getSelectorHeight(mSelector.index);
mSelector.incH = (mSelector.targetH - mSelector.originH) / mSelector.numJumps;
mSelector.moving = true; mSelector.moving = true;
updateSelectorHeight();
return success; return success;
} }
@@ -226,7 +265,8 @@ bool Menu::increaseSelectorIndex()
bool Menu::decreaseSelectorIndex() bool Menu::decreaseSelectorIndex()
{ {
bool success = false; bool success = false;
mSelector.y = mSelector.origin = mItem[mSelector.index].y; mSelector.y = mSelector.originY = mItem[mSelector.index].y;
mSelector.rect.h = mSelector.originH = getSelectorHeight(mSelector.index);
if (mSelector.index > 0) if (mSelector.index > 0)
{ {
@@ -236,10 +276,11 @@ bool Menu::decreaseSelectorIndex()
success = true; success = true;
} }
mSelector.target = mItem[mSelector.index].y; mSelector.targetY = mItem[mSelector.index].y;
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps; mSelector.despY = (mSelector.targetY - mSelector.originY) / mSelector.numJumps;
mSelector.targetH = getSelectorHeight(mSelector.index);
mSelector.incH = (mSelector.targetH - mSelector.originH) / mSelector.numJumps;
mSelector.moving = true; mSelector.moving = true;
updateSelectorHeight();
return success; return success;
} }
@@ -316,10 +357,10 @@ void Menu::setRectSize()
mRectBG.rect.y = mPosY - (mText->getCharacterWidth() / 2) - mVerticalPadding; mRectBG.rect.y = mPosY - (mText->getCharacterWidth() / 2) - mVerticalPadding;
// Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo // 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 = (mText->getCharacterWidth() * 1) + 1;
mSelector.rect.h = getSelectorHeight(mSelector.index);
mSelector.rect.w = mRectBG.rect.w; mSelector.rect.w = mRectBG.rect.w;
mSelector.rect.x = mRectBG.rect.x; mSelector.rect.x = mRectBG.rect.x;
updateSelectorHeight();
} }
// Establece el valor de la variable // Establece el valor de la variable
@@ -381,8 +422,10 @@ void Menu::centerMenuOnX(int value)
setRectSize(); setRectSize();
// Recoloca el selector // Recoloca el selector
mSelector.origin = mSelector.target = mSelector.y = mItem[mSelector.index].y; mSelector.originY = mSelector.targetY = mSelector.y = mItem[mSelector.index].y;
mSelector.moving = false; mSelector.moving = false;
mSelector.originH = mSelector.targetH = mSelector.rect.h = mItem[mSelector.index].h;
mSelector.resizing = false;
} }
// Centra el menu respecto un punto en el eje Y // Centra el menu respecto un punto en el eje Y
@@ -407,8 +450,10 @@ void Menu::centerMenuOnY(int value)
setRectSize(); setRectSize();
// Recoloca el selector // Recoloca el selector
mSelector.origin = mSelector.target = mSelector.y = mItem[mSelector.index].y; mSelector.originY = mSelector.targetY = mSelector.y = mItem[mSelector.index].y;
mSelector.moving = false; mSelector.moving = false;
mSelector.originH = mSelector.targetH = mSelector.rect.h = mItem[mSelector.index].h;
mSelector.resizing = false;
} }
// Centra los elementos del menu en el eje X // Centra los elementos del menu en el eje X
@@ -545,14 +590,14 @@ void Menu::setLinkedDown(Uint8 index, bool value)
} }
// Calcula la altura del selector // Calcula la altura del selector
void Menu::updateSelectorHeight() int Menu::getSelectorHeight(int value)
{ {
if (mItem[mSelector.index].linkedDown) if (mItem[value].linkedDown)
{ {
mSelector.rect.h = mItem[mSelector.index + 1].y + mItem[mSelector.index + 1].h - mItem[mSelector.index].y - 1; return mItem[value + 1].y + mItem[value + 1].h - mItem[value].y - 1;
} }
else else
{ {
mSelector.rect.h = mItem[mSelector.index].h - 1; return mItem[value].h - 1;
} }
} }

View File

@@ -67,12 +67,16 @@ private:
struct selector struct selector
{ {
double origin; // Coordenada de origen float originY; // Coordenada de origen
double target; // Coordenada de destino float targetY; // Coordenada de destino
double y; // Coordenada actual float despY; // Cantidad de pixeles que se desplaza el selector en cada salto: (target - origin) / numJumps
Uint8 numJumps; // Numero de pasos preestablecido para llegar al destino
double despY; // Cantidad de pixeles que se desplaza el selector en cada salto: (target - origin) / numJumps
bool moving; // Indica si el selector está avanzando hacia el destino bool moving; // Indica si el selector está avanzando hacia el destino
float originH; // Altura de origen
float targetH; // Altura de destino
float incH; // Cantidad de pixels que debe incrementar o decrementar el selector en cada salto
bool resizing; // Indica si el selector está cambiando de tamaño
float y; // Coordenada actual
Uint8 numJumps; // Numero de pasos preestablecido para llegar al destino
Uint8 index; // Elemento del menu que tiene el foco Uint8 index; // Elemento del menu que tiene el foco
SDL_Rect rect; // Rectangulo del selector SDL_Rect rect; // Rectangulo del selector
Uint8 r; // Cantidad de color rojo para el rectangulo del selector Uint8 r; // Cantidad de color rojo para el rectangulo del selector
@@ -127,6 +131,9 @@ private:
// Recoloca los elementos del menu en el eje Y // Recoloca los elementos del menu en el eje Y
void replaceElementsOnY(); void replaceElementsOnY();
// Calcula la altura del selector
int getSelectorHeight(int value);
public: public:
// Constructor // Constructor
Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList); Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList);
@@ -196,9 +203,6 @@ public:
// Establece el estado de enlace de un item // Establece el estado de enlace de un item
void setLinkedDown(Uint8 index, bool value); void setLinkedDown(Uint8 index, bool value);
// Calcula la altura del selector
void updateSelectorHeight();
}; };
#endif #endif

View File

@@ -347,10 +347,15 @@ void Title::updateMenuLabels()
{ {
case INPUT_USE_KEYBOARD: case INPUT_USE_KEYBOARD:
mMenu.options->setItemCaption(i, mLang->getText(69)); // KEYBOARD mMenu.options->setItemCaption(i, mLang->getText(69)); // KEYBOARD
mMenu.options->setGreyed(i, false);
break; break;
case INPUT_USE_GAMECONTROLLER: case INPUT_USE_GAMECONTROLLER:
mMenu.options->setItemCaption(i, mLang->getText(70)); // GAME CONTROLLER mMenu.options->setItemCaption(i, mLang->getText(70)); // GAME CONTROLLER
if (!mInput->gameControllerFound())
mMenu.options->setGreyed(i, true);
else
mMenu.options->setGreyed(i, false);
break; break;
default: default:
@@ -368,10 +373,15 @@ void Title::updateMenuLabels()
{ {
case INPUT_USE_KEYBOARD: case INPUT_USE_KEYBOARD:
mMenu.options->setItemCaption(i, mLang->getText(69)); // KEYBOARD mMenu.options->setItemCaption(i, mLang->getText(69)); // KEYBOARD
mMenu.options->setGreyed(i, false);
break; break;
case INPUT_USE_GAMECONTROLLER: case INPUT_USE_GAMECONTROLLER:
mMenu.options->setItemCaption(i, mLang->getText(70)); // GAME CONTROLLER mMenu.options->setItemCaption(i, mLang->getText(70)); // GAME CONTROLLER
if (!mInput->gameControllerFound())
mMenu.options->setGreyed(i, true);
else
mMenu.options->setGreyed(i, false);
break; break;
default: default:
@@ -892,6 +902,12 @@ void Title::switchInputs(int value)
temp = mOptions->input[0]; temp = mOptions->input[0];
mOptions->input[0] = mOptions->input[1]; mOptions->input[0] = mOptions->input[1];
mOptions->input[1] = temp; mOptions->input[1] = temp;
if (!mInput->gameControllerFound())
{
mOptions->input[0] = INPUT_USE_KEYBOARD;
mOptions->input[1] = INPUT_USE_GAMECONTROLLER;
}
} }
// Crea el mosaico de fondo del titulo // Crea el mosaico de fondo del titulo