menu: enum class Menu::Background/Sound + constant Menu::NO_OPTION

This commit is contained in:
2026-05-16 21:14:14 +02:00
parent 76d0c72b85
commit 7d8aac6121
2 changed files with 28 additions and 27 deletions
+12 -12
View File
@@ -25,7 +25,7 @@ Menu::Menu(SDL_Renderer *renderer, const std::string &file)
// Inicializa variables
selector_.index = 0;
selector_.previous_index = 0;
item_selected_ = MENU_NO_OPTION;
item_selected_ = NO_OPTION;
x_ = 0;
y_ = 0;
w_ = 0;
@@ -33,7 +33,7 @@ Menu::Menu(SDL_Renderer *renderer, const std::string &file)
rect_bg_.rect = {.x = 0, .y = 0, .w = 0, .h = 0};
rect_bg_.color = {0, 0, 0};
rect_bg_.a = 0;
background_type_ = MENU_BACKGROUND_SOLID;
background_type_ = Background::SOLID;
is_centered_on_x_ = false;
is_centered_on_y_ = false;
are_elements_centered_on_x_ = false;
@@ -286,7 +286,7 @@ auto Menu::setVars(const std::string &var, const std::string &value) -> bool {
} else if (var == "centerY") {
center_y_ = std::stoi(value);
} else if (var == "backgroundType") {
background_type_ = std::stoi(value);
background_type_ = static_cast<Background>(std::stoi(value));
} else if (var == "areElementsCenteredOnX") {
are_elements_centered_on_x_ = (value == "true");
} else if (var == "isCenteredOnX") {
@@ -303,17 +303,17 @@ auto Menu::setVars(const std::string &var, const std::string &value) -> bool {
}
// Carga los ficheros de audio
void Menu::loadAudioFile(const std::string &file, int sound) {
void Menu::loadAudioFile(const std::string &file, Sound sound) {
switch (sound) {
case SOUND_ACCEPT:
case Sound::ACCEPT:
sound_accept_ = Ja::loadSound(file.c_str());
break;
case SOUND_CANCEL:
case Sound::CANCEL:
sound_cancel_ = Ja::loadSound(file.c_str());
break;
case SOUND_MOVE:
case Sound::MOVE:
sound_move_ = Ja::loadSound(file.c_str());
break;
@@ -331,7 +331,7 @@ auto Menu::getName() const -> const std::string & {
auto Menu::getItemSelected() -> int {
// Al llamar a esta funcion, se obtiene el valor y se borra
const int TEMP = item_selected_;
item_selected_ = MENU_NO_OPTION;
item_selected_ = NO_OPTION;
return TEMP;
}
@@ -419,7 +419,7 @@ auto Menu::getWidestItem() -> int {
// Deja el menu apuntando al primer elemento
void Menu::reset() {
item_selected_ = MENU_NO_OPTION;
item_selected_ = NO_OPTION;
selector_.index = 0;
if (items_.empty()) {
return;
@@ -527,7 +527,7 @@ void Menu::update() {
// Pinta el menu en pantalla
void Menu::render() {
// Rendereritza el fondo del menu
if (background_type_ == MENU_BACKGROUND_SOLID) {
if (background_type_ == Background::SOLID) {
SDL_FRect f_bg = {(float)rect_bg_.rect.x, (float)rect_bg_.rect.y, (float)rect_bg_.rect.w, (float)rect_bg_.rect.h};
SDL_SetRenderDrawColor(renderer_, rect_bg_.color.r, rect_bg_.color.g, rect_bg_.color.b, rect_bg_.a);
SDL_RenderFillRect(renderer_, &f_bg);
@@ -539,7 +539,7 @@ void Menu::render() {
SDL_RenderFillRect(renderer_, &F_TEMP);
// Renderiza el borde del fondo
if (background_type_ == MENU_BACKGROUND_SOLID) {
if (background_type_ == Background::SOLID) {
SDL_FRect f_bg_border = {(float)rect_bg_.rect.x, (float)rect_bg_.rect.y, (float)rect_bg_.rect.w, (float)rect_bg_.rect.h};
SDL_SetRenderDrawColor(renderer_, rect_bg_.color.r, rect_bg_.color.g, rect_bg_.color.b, 255);
SDL_RenderRect(renderer_, &f_bg_border);
@@ -830,7 +830,7 @@ void Menu::setPos(int x, int y) {
}
// Establece el tipo de fondo del menu
void Menu::setBackgroundType(int value) {
void Menu::setBackgroundType(Background value) {
background_type_ = value;
}
+16 -15
View File
@@ -12,26 +12,25 @@ namespace Ja {
struct Sound;
} // namespace Ja
// Tipos de fondos para el menú
constexpr int MENU_BACKGROUND_TRANSPARENT = 0;
constexpr int MENU_BACKGROUND_SOLID = 1;
// Tipos de archivos de audio
constexpr int SOUND_ACCEPT = 0;
constexpr int SOUND_MOVE = 1;
constexpr int SOUND_CANCEL = 2;
// Opciones de menú
constexpr int MENU_NO_OPTION = -1;
// Clase Menu
class Menu {
public:
enum class Background : std::uint8_t {
TRANSPARENT,
SOLID
};
enum class Sound : std::uint8_t {
ACCEPT,
MOVE,
CANCEL
};
explicit Menu(SDL_Renderer *renderer, const std::string &file = ""); // Constructor
~Menu(); // Destructor
auto loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &name_for_logs = "") -> bool; // Carga el menu desde bytes en memoria
void loadAudioFile(const std::string &file, int sound); // Carga los ficheros de audio
void loadAudioFile(const std::string &file, Sound sound); // Carga los ficheros de audio
[[nodiscard]] auto getName() const -> const std::string &; // Obtiene el nombre del menu
auto getItemSelected() -> int; // Obtiene el valor de la variable
@@ -72,11 +71,13 @@ class Menu {
void setName(const std::string &name); // Establece el nombre del menu
void setPos(int x, int y); // Establece la posición del menu
void setBackgroundType(int value); // Establece el tipo de fondo del menu
void setBackgroundType(Background value); // Establece el tipo de fondo del menu
void setText(const std::string &font_png, const std::string &font_txt); // Establece la fuente de texto que se utilizará
void setRectSize(int w = 0, int h = 0); // Establece el rectangulo de fondo del menu
private:
static constexpr int NO_OPTION = -1; // Centinela: ningún item seleccionado
struct Rectangle {
SDL_Rect rect; // Rectangulo
Color color; // Color
@@ -137,7 +138,7 @@ class Menu {
int w_; // Anchura del menu
int item_selected_; // Índice del item del menu que ha sido seleccionado
int default_action_when_cancel_; // Indice del item del menu que se selecciona cuando se cancela el menu
int background_type_; // Tipo de fondo para el menu
Background background_type_; // Tipo de fondo para el menu
int center_x_; // Centro del menu en el eje X
int center_y_; // Centro del menu en el eje Y
bool is_centered_on_x_; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje X