Trabajando en la carga de menus desde fichero
This commit is contained in:
285
source/menu.cpp
285
source/menu.cpp
@@ -2,21 +2,28 @@
|
||||
#include "menu.h"
|
||||
|
||||
// Constructor
|
||||
Menu::Menu(SDL_Renderer *renderer, Text *text, Input *input)
|
||||
Menu::Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file)
|
||||
{
|
||||
this->renderer = renderer;
|
||||
this->text = text;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
|
||||
soundMove = nullptr;
|
||||
soundAccept = nullptr;
|
||||
soundCancel = nullptr;
|
||||
|
||||
init();
|
||||
if (file != "")
|
||||
{
|
||||
load(file);
|
||||
}
|
||||
reorganize();
|
||||
}
|
||||
|
||||
Menu::~Menu()
|
||||
{
|
||||
renderer = nullptr;
|
||||
text = nullptr;
|
||||
asset = nullptr;
|
||||
input = nullptr;
|
||||
|
||||
if (soundMove)
|
||||
@@ -33,21 +40,278 @@ Menu::~Menu()
|
||||
{
|
||||
JA_DeleteSound(soundCancel);
|
||||
}
|
||||
|
||||
if (text != nullptr)
|
||||
{
|
||||
delete text;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializador
|
||||
void Menu::init(std::string name, int x, int y, int backgroundType)
|
||||
// Carga la configuración del menu desde un archivo de texto
|
||||
bool Menu::load(std::string file_path)
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Indica si se ha creado ya el objeto de texto
|
||||
bool textAllocated = false;
|
||||
|
||||
std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::string line;
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
printf("Reading file %s\n", filename.c_str());
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line == "[item]")
|
||||
{
|
||||
item_t item;
|
||||
item.label = "";
|
||||
item.hPaddingDown = 1;
|
||||
item.selectable = true;
|
||||
item.greyed = false;
|
||||
item.linkedDown = false;
|
||||
|
||||
do
|
||||
{
|
||||
|
||||
std::getline(file, line);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
} while (line != "[/item]");
|
||||
|
||||
addItem(item.label, item.hPaddingDown, item.selectable, item.greyed, item.linkedDown);
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else
|
||||
{
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items
|
||||
if (font_png != "" && font_txt != "" && !textAllocated)
|
||||
{
|
||||
text = new Text(asset->get(font_png), asset->get(font_txt), renderer);
|
||||
textAllocated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
printf("Closing file %s\n\n", filename.c_str());
|
||||
file.close();
|
||||
}
|
||||
// El fichero no se puede abrir
|
||||
else
|
||||
{
|
||||
printf("Warning: Unable to open %s file\n", filename.c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setItem(item_t *item, std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "text")
|
||||
{
|
||||
item->label = value;
|
||||
}
|
||||
|
||||
else if (var == "hPaddingDown")
|
||||
{
|
||||
item->hPaddingDown = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "selectable")
|
||||
{
|
||||
item->selectable = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "greyed")
|
||||
{
|
||||
item->greyed = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "linkedDown")
|
||||
{
|
||||
item->linkedDown = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if ((var == "") || (var == "[/item]"))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setVars(std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "font_png")
|
||||
{
|
||||
font_png = value;
|
||||
}
|
||||
|
||||
else if (var == "font_txt")
|
||||
{
|
||||
font_txt = value;
|
||||
}
|
||||
|
||||
else if (var == "sound_cancel")
|
||||
{
|
||||
soundCancel = JA_LoadSound(asset->get(value).c_str());
|
||||
}
|
||||
|
||||
else if (var == "sound_accept")
|
||||
{
|
||||
soundAccept = JA_LoadSound(asset->get(value).c_str());
|
||||
}
|
||||
|
||||
else if (var == "sound_move")
|
||||
{
|
||||
soundMove = JA_LoadSound(asset->get(value).c_str());
|
||||
}
|
||||
|
||||
else if (var == "name")
|
||||
{
|
||||
name = value;
|
||||
}
|
||||
|
||||
else if (var == "x")
|
||||
{
|
||||
x = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "y")
|
||||
{
|
||||
y = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "backgroundType")
|
||||
{
|
||||
backgroundType = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "backgroundColor")
|
||||
{
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.color.b = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.a = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "selector_color")
|
||||
{
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
selector.color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.color.b = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.a = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "selector_text_color")
|
||||
{
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
selector.itemColor.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.itemColor.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.itemColor.b = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "areElementsCenteredOnX")
|
||||
{
|
||||
areElementsCenteredOnX = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnX")
|
||||
{
|
||||
isCenteredOnX = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnY")
|
||||
{
|
||||
isCenteredOnY = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "defaultActionWhenCancel")
|
||||
{
|
||||
defaultActionWhenCancel = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "")
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Inicializa las variables
|
||||
void Menu::init()
|
||||
{
|
||||
// Inicia variables
|
||||
this->name = name;
|
||||
name = "";
|
||||
selector.index = 0;
|
||||
itemSelected = MENU_NO_OPTION;
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
x = 0;
|
||||
y = 0;
|
||||
rectBG.rect = {0, 0, 0, 0};
|
||||
rectBG.color = {0, 0, 0};
|
||||
rectBG.a = 0;
|
||||
this->backgroundType = backgroundType;
|
||||
backgroundType = MENU_BACKGROUND_SOLID;
|
||||
isCenteredOnX = false;
|
||||
isCenteredOnY = false;
|
||||
areElementsCenteredOnX = false;
|
||||
@@ -55,6 +319,9 @@ void Menu::init(std::string name, int x, int y, int backgroundType)
|
||||
centerY = 0;
|
||||
widestItem = 0;
|
||||
colorGreyed = {128, 128, 128};
|
||||
defaultActionWhenCancel = 0;
|
||||
font_png = "";
|
||||
font_txt = "";
|
||||
|
||||
// Selector
|
||||
selector.originY = 0;
|
||||
|
||||
Reference in New Issue
Block a user