From 06bbb22ce5a1a6198f6791484dc612d032676e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Fri, 19 Feb 2021 19:34:06 +0100 Subject: [PATCH] update menu.cpp --- source/menu.cpp | 55 +++++++++++++++++++++++++------------------------ source/menu.h | 21 +++++++++---------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/source/menu.cpp b/source/menu.cpp index 909b79e..6c09cb8 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -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 @@ -426,4 +427,4 @@ void Menu::setItemCaption(Uint8 index, std::string text) void Menu::setDefaultActionWhenCancel(Uint8 item) { mDefaultActionWhenCancel = item; -} +} \ No newline at end of file diff --git a/source/menu.h b/source/menu.h index 2d4184f..ea203dd 100644 --- a/source/menu.h +++ b/source/menu.h @@ -114,11 +114,14 @@ private: struct rectangle { - SDL_Rect rect; // Rectangulo - Uint8 r; // Rojo - Uint8 g; // Verde - Uint8 b; // Azul - Uint8 a; // Transparencia + 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 + Uint8 a; // Transparencia }; rectangle mRectBG; // Rectangulo de fondo del menu @@ -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