menu selector now resizes smooth
This commit is contained in:
@@ -80,44 +80,45 @@ bool Input::checkInput(Uint8 input, bool repeat, int device)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
if (repeat)
|
||||
|
||||
if (gameControllerFound())
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
|
||||
successGameController = true;
|
||||
else
|
||||
successGameController = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!mGameControllerBindings[input].active)
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
|
||||
{
|
||||
mGameControllerBindings[input].active = true;
|
||||
successGameController = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) == 0)
|
||||
if (!mGameControllerBindings[input].active)
|
||||
{
|
||||
mGameControllerBindings[input].active = false;
|
||||
successGameController = false;
|
||||
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
|
||||
{
|
||||
mGameControllerBindings[input].active = true;
|
||||
successGameController = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) == 0)
|
||||
{
|
||||
mGameControllerBindings[input].active = false;
|
||||
successGameController = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
}
|
||||
@@ -159,4 +160,13 @@ bool Input::discoverGameController()
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound()
|
||||
{
|
||||
if (mNumGamepads > 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
@@ -71,6 +71,9 @@ public:
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool checkInput(Uint8 input, bool repeat, int device=INPUT_USE_ANY);
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool gameControllerFound();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
119
source/menu.cpp
119
source/menu.cpp
@@ -38,14 +38,6 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
|
||||
mRectBG.r = 0;
|
||||
mRectBG.g = 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;
|
||||
mIsCenteredOnX = false;
|
||||
mIsCenteredOnY = false;
|
||||
@@ -56,12 +48,24 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
|
||||
mColorGreyed = {128, 128, 128};
|
||||
|
||||
// Selector
|
||||
mSelector.origin = 0;
|
||||
mSelector.target = 0;
|
||||
mSelector.originY = 0;
|
||||
mSelector.targetY = 0;
|
||||
mSelector.despY = 0;
|
||||
mSelector.originH = 0;
|
||||
mSelector.targetH = 0;
|
||||
mSelector.incH = 0;
|
||||
mSelector.y = 0;
|
||||
mSelector.numJumps = 4;
|
||||
mSelector.despY = 0;
|
||||
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
|
||||
for (int i = 0; i < MENU_MAX_ITEMS; i++)
|
||||
@@ -109,20 +113,21 @@ void Menu::updateSelector()
|
||||
{
|
||||
if (mSelector.moving)
|
||||
{
|
||||
// Calcula el desplazamiento en Y
|
||||
mSelector.y += mSelector.despY;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -132,18 +137,45 @@ void Menu::updateSelector()
|
||||
{
|
||||
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
|
||||
void Menu::setSelectorOrigin(int value)
|
||||
{
|
||||
mSelector.origin = value;
|
||||
mSelector.originY = value;
|
||||
}
|
||||
|
||||
// Establece el destino del selector
|
||||
void Menu::setSelectorTarget(int value)
|
||||
{
|
||||
mSelector.target = value;
|
||||
mSelector.targetY = value;
|
||||
}
|
||||
|
||||
// Coloca el selector en una posición específica
|
||||
@@ -152,8 +184,9 @@ void Menu::setSelectorPos(Uint8 index)
|
||||
if (index < mTotalItems)
|
||||
{
|
||||
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.resizing = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,8 +212,11 @@ void Menu::reset()
|
||||
{
|
||||
mItemSelected = MENU_NO_OPTION;
|
||||
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.resizing = false;
|
||||
|
||||
}
|
||||
|
||||
// Deja el menu sin elemento seleccionado
|
||||
@@ -205,7 +241,8 @@ void Menu::reorganize()
|
||||
bool Menu::increaseSelectorIndex()
|
||||
{
|
||||
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))
|
||||
{
|
||||
@@ -215,10 +252,12 @@ bool Menu::increaseSelectorIndex()
|
||||
success = true;
|
||||
}
|
||||
|
||||
mSelector.target = mItem[mSelector.index].y;
|
||||
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps;
|
||||
mSelector.targetY = mItem[mSelector.index].y;
|
||||
mSelector.despY = (mSelector.targetY - mSelector.originY) / mSelector.numJumps;
|
||||
mSelector.targetH = getSelectorHeight(mSelector.index);
|
||||
mSelector.incH = (mSelector.targetH - mSelector.originH) / mSelector.numJumps;
|
||||
mSelector.moving = true;
|
||||
updateSelectorHeight();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -226,7 +265,8 @@ bool Menu::increaseSelectorIndex()
|
||||
bool Menu::decreaseSelectorIndex()
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -236,10 +276,11 @@ bool Menu::decreaseSelectorIndex()
|
||||
success = true;
|
||||
}
|
||||
|
||||
mSelector.target = mItem[mSelector.index].y;
|
||||
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps;
|
||||
mSelector.targetY = mItem[mSelector.index].y;
|
||||
mSelector.despY = (mSelector.targetY - mSelector.originY) / mSelector.numJumps;
|
||||
mSelector.targetH = getSelectorHeight(mSelector.index);
|
||||
mSelector.incH = (mSelector.targetH - mSelector.originH) / mSelector.numJumps;
|
||||
mSelector.moving = true;
|
||||
updateSelectorHeight();
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -295,7 +336,7 @@ void Menu::render()
|
||||
}
|
||||
else // No enlazado con el de arriba
|
||||
{
|
||||
mText->write(mItem[i].x, mItem[i].y, mItem[i].label);
|
||||
mText->write(mItem[i].x, mItem[i].y, mItem[i].label);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,10 +357,10 @@ 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 = (mText->getCharacterWidth() * 1) + 1;
|
||||
mSelector.rect.h = getSelectorHeight(mSelector.index);
|
||||
mSelector.rect.w = mRectBG.rect.w;
|
||||
mSelector.rect.x = mRectBG.rect.x;
|
||||
updateSelectorHeight();
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
@@ -381,8 +422,10 @@ void Menu::centerMenuOnX(int value)
|
||||
setRectSize();
|
||||
|
||||
// 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.originH = mSelector.targetH = mSelector.rect.h = mItem[mSelector.index].h;
|
||||
mSelector.resizing = false;
|
||||
}
|
||||
|
||||
// Centra el menu respecto un punto en el eje Y
|
||||
@@ -407,8 +450,10 @@ void Menu::centerMenuOnY(int value)
|
||||
setRectSize();
|
||||
|
||||
// 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.originH = mSelector.targetH = mSelector.rect.h = mItem[mSelector.index].h;
|
||||
mSelector.resizing = false;
|
||||
}
|
||||
|
||||
// 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
|
||||
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
|
||||
{
|
||||
mSelector.rect.h = mItem[mSelector.index].h - 1;
|
||||
return mItem[value].h - 1;
|
||||
}
|
||||
}
|
||||
@@ -67,12 +67,16 @@ private:
|
||||
|
||||
struct selector
|
||||
{
|
||||
double origin; // Coordenada de origen
|
||||
double target; // Coordenada de destino
|
||||
double y; // Coordenada actual
|
||||
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
|
||||
float originY; // Coordenada de origen
|
||||
float targetY; // Coordenada de destino
|
||||
float 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
|
||||
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
|
||||
SDL_Rect rect; // 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
|
||||
void replaceElementsOnY();
|
||||
|
||||
// Calcula la altura del selector
|
||||
int getSelectorHeight(int value);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList);
|
||||
@@ -196,9 +203,6 @@ public:
|
||||
|
||||
// Establece el estado de enlace de un item
|
||||
void setLinkedDown(Uint8 index, bool value);
|
||||
|
||||
// Calcula la altura del selector
|
||||
void updateSelectorHeight();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -347,10 +347,15 @@ void Title::updateMenuLabels()
|
||||
{
|
||||
case INPUT_USE_KEYBOARD:
|
||||
mMenu.options->setItemCaption(i, mLang->getText(69)); // KEYBOARD
|
||||
mMenu.options->setGreyed(i, false);
|
||||
break;
|
||||
|
||||
case INPUT_USE_GAMECONTROLLER:
|
||||
mMenu.options->setItemCaption(i, mLang->getText(70)); // GAME CONTROLLER
|
||||
if (!mInput->gameControllerFound())
|
||||
mMenu.options->setGreyed(i, true);
|
||||
else
|
||||
mMenu.options->setGreyed(i, false);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -368,10 +373,15 @@ void Title::updateMenuLabels()
|
||||
{
|
||||
case INPUT_USE_KEYBOARD:
|
||||
mMenu.options->setItemCaption(i, mLang->getText(69)); // KEYBOARD
|
||||
mMenu.options->setGreyed(i, false);
|
||||
break;
|
||||
|
||||
case INPUT_USE_GAMECONTROLLER:
|
||||
mMenu.options->setItemCaption(i, mLang->getText(70)); // GAME CONTROLLER
|
||||
if (!mInput->gameControllerFound())
|
||||
mMenu.options->setGreyed(i, true);
|
||||
else
|
||||
mMenu.options->setGreyed(i, false);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -892,6 +902,12 @@ void Title::switchInputs(int value)
|
||||
temp = mOptions->input[0];
|
||||
mOptions->input[0] = mOptions->input[1];
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user