menu class improved

This commit is contained in:
2021-08-29 00:54:05 +02:00
parent 0baf73b9ae
commit 697e5d796f
3 changed files with 64 additions and 24 deletions

View File

@@ -64,7 +64,7 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
mSelector.moving = false; mSelector.moving = false;
// Elementos del menu // Elementos del menu
for (int i = 0; i < MAX_ITEMS; i++) for (int i = 0; i < MENU_MAX_ITEMS; i++)
{ {
mItem[i].label = ""; mItem[i].label = "";
mItem[i].w = 0; mItem[i].w = 0;
@@ -218,6 +218,7 @@ bool Menu::increaseSelectorIndex()
mSelector.target = mItem[mSelector.index].y; mSelector.target = mItem[mSelector.index].y;
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps; mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps;
mSelector.moving = true; mSelector.moving = true;
updateSelectorHeight();
return success; return success;
} }
@@ -238,6 +239,7 @@ bool Menu::decreaseSelectorIndex()
mSelector.target = mItem[mSelector.index].y; mSelector.target = mItem[mSelector.index].y;
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps; mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps;
mSelector.moving = true; mSelector.moving = true;
updateSelectorHeight();
return success; return success;
} }
@@ -284,11 +286,18 @@ void Menu::render()
{ {
mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, mColorGreyed); mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, mColorGreyed);
} }
//else // no selectable else // No seleccionable
//{ {
// const color_t color = {mSelector.itemR, mSelector.itemG, mSelector.itemB}; if ((mItem[i].linkedUp) && (i == mSelector.index + 1))
// mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, color); {
//} const color_t color = {mSelector.itemR, mSelector.itemG, mSelector.itemB};
mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, color);
}
else // No enlazado con el de arriba
{
mText->write(mItem[i].x, mItem[i].y, mItem[i].label);
}
}
} }
} }
@@ -310,13 +319,15 @@ void Menu::setRectSize()
mSelector.rect.h = (mText->getCharacterWidth() * 1) + 1; mSelector.rect.h = (mText->getCharacterWidth() * 1) + 1;
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
void Menu::setTotalItems(int num) void Menu::setTotalItems(int num)
{ {
mTotalItems = num; mTotalItems = num;
mTotalItems > MAX_ITEMS ? MAX_ITEMS : mTotalItems; if (mTotalItems > MENU_MAX_ITEMS)
mTotalItems = MENU_MAX_ITEMS;
} }
// Establece el color del rectangulo de fondo // Establece el color del rectangulo de fondo
@@ -361,7 +372,7 @@ void Menu::centerMenuOnX(int value)
mPosX = (value) - (mWidestItem / 2); mPosX = (value) - (mWidestItem / 2);
// Reposiciona los elementos del menu // Reposiciona los elementos del menu
for (int i = 0; i < MAX_ITEMS; i++) for (int i = 0; i < MENU_MAX_ITEMS; i++)
{ {
mItem[i].x = mPosX; mItem[i].x = mPosX;
} }
@@ -410,7 +421,7 @@ void Menu::centerMenuElementsOnX()
} }
// Añade un item al menu // Añade un item al menu
void Menu::addItem(std::string text, const Uint8 hPaddingUp, const Uint8 hPaddingDown, bool selectable, bool greyed) void Menu::addItem(std::string text, const Uint8 hPaddingUp, const Uint8 hPaddingDown, bool selectable, bool greyed, bool linkedDown)
{ {
// Si es el primer item coge la posición en el eje Y del propio menu // Si es el primer item coge la posición en el eje Y del propio menu
if (mTotalItems == 0) if (mTotalItems == 0)
@@ -427,6 +438,8 @@ void Menu::addItem(std::string text, const Uint8 hPaddingUp, const Uint8 hPaddin
mItem[mTotalItems].hPaddingDown = hPaddingDown; mItem[mTotalItems].hPaddingDown = hPaddingDown;
mItem[mTotalItems].selectable = selectable; mItem[mTotalItems].selectable = selectable;
mItem[mTotalItems].greyed = greyed; mItem[mTotalItems].greyed = greyed;
mItem[mTotalItems].linkedDown = linkedDown;
mItem[mTotalItems + 1].linkedUp = linkedDown;
setTotalItems(mTotalItems + 1); setTotalItems(mTotalItems + 1);
reorganize(); reorganize();
@@ -523,4 +536,23 @@ void Menu::setSelectable(Uint8 index, bool value)
void Menu::setGreyed(Uint8 index, bool value) void Menu::setGreyed(Uint8 index, bool value)
{ {
mItem[index].greyed = value; mItem[index].greyed = value;
}
// Establece el estado de enlace de un item
void Menu::setLinkedDown(Uint8 index, bool value)
{
mItem[index].linkedDown = value;
}
// Calcula la altura del selector
void Menu::updateSelectorHeight()
{
if (mItem[mSelector.index].linkedDown)
{
mSelector.rect.h = mItem[mSelector.index + 1].y + mItem[mSelector.index + 1].h - mItem[mSelector.index].y - 1;
}
else
{
mSelector.rect.h = mItem[mSelector.index].h - 1;
}
} }

View File

@@ -37,7 +37,7 @@ private:
JA_Sound mSoundCancel; // Sonido al cancelar el menu JA_Sound mSoundCancel; // Sonido al cancelar el menu
JA_Sound mSoundMove; // Sonido al mover el selector JA_Sound mSoundMove; // Sonido al mover el selector
Input *mInput; // Gestor de eventos de entrada de teclado o gamepad Input *mInput; // Gestor de eventos de entrada de teclado o gamepad
color_t mColorGreyed; // Color para los elementos agrisados color_t mColorGreyed; // Color para los elementos agrisados
struct rectangle struct rectangle
{ {
@@ -60,6 +60,8 @@ private:
Uint8 hPaddingDown; // Espaciado bajo el elemento Uint8 hPaddingDown; // Espaciado bajo el elemento
bool selectable; // Indica si se puede seleccionar bool selectable; // Indica si se puede seleccionar
bool greyed; // Indica si ha de aparecer con otro color mas oscuro bool greyed; // Indica si ha de aparecer con otro color mas oscuro
bool linkedDown; // Indica si el elemento actual y el siguiente se tratan como uno solo. Afecta al selector
bool linkedUp; // Indica si el elemento actual y el anterior se tratan como uno solo. Afecta al selector
}; };
item mItem[MENU_MAX_ITEMS]; // Estructura para cada elemento del menu item mItem[MENU_MAX_ITEMS]; // Estructura para cada elemento del menu
@@ -175,7 +177,7 @@ public:
void centerMenuElementsOnX(); void centerMenuElementsOnX();
// Añade un item al menu // Añade un item al menu
void addItem(std::string text, const Uint8 hPaddingUp = 0, const Uint8 hPaddingDown = 0, bool selectable = true, bool greyed = false); void addItem(std::string text, const Uint8 hPaddingUp = 0, const Uint8 hPaddingDown = 0, bool selectable = true, bool greyed = false, bool linkedDown = false);
// Cambia el texto de un item // Cambia el texto de un item
void setItemCaption(Uint8 index, std::string text); void setItemCaption(Uint8 index, std::string text);
@@ -191,6 +193,12 @@ public:
// Establece el estado agrisado de un item // Establece el estado agrisado de un item
void setGreyed(Uint8 index, bool value); void setGreyed(Uint8 index, bool value);
// Establece el estado de enlace de un item
void setLinkedDown(Uint8 index, bool value);
// Calcula la altura del selector
void updateSelectorHeight();
}; };
#endif #endif

View File

@@ -263,19 +263,19 @@ void Title::init(bool demo, Uint8 subsection)
mMenu.title->centerMenuElementsOnX(); mMenu.title->centerMenuElementsOnX();
mMenu.options->init("OPTIONS", 0, BLOCK, MENU_BACKGROUND_TRANSPARENT); mMenu.options->init("OPTIONS", 0, BLOCK, MENU_BACKGROUND_TRANSPARENT);
mMenu.options->addItem(mLang->getText(59), 0, 5); // (0) DIFFICULTY mMenu.options->addItem(mLang->getText(59), 0, 5); // (0) DIFFICULTY
mMenu.options->addItem(mLang->getText(62)); // (1) PLAYER 1 CONTROLS mMenu.options->addItem(mLang->getText(62), 0, 0, true, false, true); // (1) PLAYER 1 CONTROLS
mMenu.options->addItem(mLang->getText(69), 0, 0, false, false); // (2) KEYBOARD mMenu.options->addItem(mLang->getText(69), 0, 0, false, false); // (2) KEYBOARD
mMenu.options->addItem(mLang->getText(63)); // (3) PLAYER 2 CONTROLS mMenu.options->addItem(mLang->getText(63), 0, 0, true, false, true); // (3) PLAYER 2 CONTROLS
mMenu.options->addItem(mLang->getText(70), 0, 5, false, false); // (4) GAME CONTROLLER mMenu.options->addItem(mLang->getText(70), 0, 5, false, false); // (4) GAME CONTROLLER
mMenu.options->addItem(mLang->getText(8), 0, 5); // (5) LANGUAGE mMenu.options->addItem(mLang->getText(8), 0, 5); // (5) LANGUAGE
mMenu.options->addItem(mLang->getText(58)); // (6) DISPLAY MODE mMenu.options->addItem(mLang->getText(58), 0, 0, true, false, true); // (6) DISPLAY MODE
mMenu.options->addItem(mLang->getText(4), 0, 0, false, false); // (7) WINDOWED mMenu.options->addItem(mLang->getText(4), 0, 0, false, false); // (7) WINDOWED
mMenu.options->addItem(mLang->getText(7)); // (8) WINDOW SIZE mMenu.options->addItem(mLang->getText(7)); // (8) WINDOW SIZE
mMenu.options->addItem(mLang->getText(60)); // (9) FILTER mMenu.options->addItem(mLang->getText(60)); // (9) FILTER
mMenu.options->addItem(mLang->getText(61), 0, 5); // (10) VSYNC mMenu.options->addItem(mLang->getText(61), 0, 5); // (10) VSYNC
mMenu.options->addItem(mLang->getText(9)); // (11) ACCEPT mMenu.options->addItem(mLang->getText(9)); // (11) ACCEPT
mMenu.options->addItem(mLang->getText(10)); // (12) CANCEL mMenu.options->addItem(mLang->getText(10)); // (12) CANCEL
mMenu.options->setDefaultActionWhenCancel(9); mMenu.options->setDefaultActionWhenCancel(9);
mMenu.options->setBackgroundColor(0x30, 0x30, 0x40, 192); mMenu.options->setBackgroundColor(0x30, 0x30, 0x40, 192);
mMenu.options->setSelectorColor(0xe5, 0x1c, 0x23, 255); mMenu.options->setSelectorColor(0xe5, 0x1c, 0x23, 255);