neteja tidy a source/game (fixes d'arrel: BulletKind enum class, signatures, branches)
This commit is contained in:
+62
-57
@@ -14,9 +14,7 @@
|
||||
|
||||
// Constructor
|
||||
Menu::Menu(SDL_Renderer *renderer, const std::string &file)
|
||||
: colorGreyed{128, 128, 128},
|
||||
font_png(""),
|
||||
font_txt("") {
|
||||
: colorGreyed{128, 128, 128} {
|
||||
// Copia punteros
|
||||
this->renderer = renderer;
|
||||
|
||||
@@ -32,7 +30,7 @@ Menu::Menu(SDL_Renderer *renderer, const std::string &file)
|
||||
x = 0;
|
||||
y = 0;
|
||||
w = 0;
|
||||
rectBG.rect = {0, 0, 0, 0};
|
||||
rectBG.rect = {.x = 0, .y = 0, .w = 0, .h = 0};
|
||||
rectBG.color = {0, 0, 0};
|
||||
rectBG.a = 0;
|
||||
backgroundType = MENU_BACKGROUND_SOLID;
|
||||
@@ -51,12 +49,12 @@ Menu::Menu(SDL_Renderer *renderer, const std::string &file)
|
||||
selector.originH = 0;
|
||||
selector.targetH = 0;
|
||||
selector.incH = 0;
|
||||
selector.y = 0.0f;
|
||||
selector.h = 0.0f;
|
||||
selector.y = 0.0F;
|
||||
selector.h = 0.0F;
|
||||
selector.numJumps = 8;
|
||||
selector.moving = false;
|
||||
selector.resizing = false;
|
||||
selector.rect = {0, 0, 0, 0};
|
||||
selector.rect = {.x = 0, .y = 0, .w = 0, .h = 0};
|
||||
selector.color = {0, 0, 0};
|
||||
selector.itemColor = {0, 0, 0};
|
||||
selector.a = 255;
|
||||
@@ -74,25 +72,23 @@ Menu::Menu(SDL_Renderer *renderer, const std::string &file)
|
||||
Menu::~Menu() {
|
||||
renderer = nullptr;
|
||||
|
||||
if (soundMove) {
|
||||
if (soundMove != nullptr) {
|
||||
JA_DeleteSound(soundMove);
|
||||
}
|
||||
|
||||
if (soundAccept) {
|
||||
if (soundAccept != nullptr) {
|
||||
JA_DeleteSound(soundAccept);
|
||||
}
|
||||
|
||||
if (soundCancel) {
|
||||
if (soundCancel != nullptr) {
|
||||
JA_DeleteSound(soundCancel);
|
||||
}
|
||||
|
||||
if (text != nullptr) {
|
||||
delete text;
|
||||
}
|
||||
}
|
||||
|
||||
// Parser compartido (recibe cualquier istream)
|
||||
bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
|
||||
auto Menu::parseFromStream(std::istream &file, const std::string &filename) -> bool {
|
||||
bool success = true;
|
||||
bool textAllocated = false;
|
||||
std::string line;
|
||||
@@ -103,7 +99,9 @@ bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
|
||||
// deixant un \r residual. Sense això, "[item]" no fa match i el vector
|
||||
// d'items queda buit → reset() accedeix a item[0] i crasheja.
|
||||
auto strip_cr = [](std::string &s) {
|
||||
if (!s.empty() && s.back() == '\r') s.pop_back();
|
||||
if (!s.empty() && s.back() == '\r') {
|
||||
s.pop_back();
|
||||
}
|
||||
};
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
@@ -119,9 +117,11 @@ bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
|
||||
newItem.line = false;
|
||||
|
||||
do {
|
||||
if (!std::getline(file, line)) break;
|
||||
if (!std::getline(file, line)) {
|
||||
break;
|
||||
}
|
||||
strip_cr(line);
|
||||
int pos = line.find("=");
|
||||
int pos = line.find('=');
|
||||
if (!setItem(&newItem, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
success = false;
|
||||
}
|
||||
@@ -129,14 +129,14 @@ bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
|
||||
|
||||
addItem(newItem);
|
||||
} else {
|
||||
int pos = line.find("=");
|
||||
int pos = line.find('=');
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items.
|
||||
// Carga via ResourceHelper para que funcione tanto con pack como con filesystem.
|
||||
if (font_png != "" && font_txt != "" && !textAllocated) {
|
||||
if (!font_png.empty() && !font_txt.empty() && !textAllocated) {
|
||||
auto pngBytes = ResourceHelper::loadFile(Asset::get()->get(font_png));
|
||||
auto txtBytes = ResourceHelper::loadFile(Asset::get()->get(font_txt));
|
||||
text = new Text(pngBytes, txtBytes, renderer);
|
||||
@@ -148,7 +148,7 @@ bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
|
||||
}
|
||||
|
||||
// Carga la configuración del menu (vía ResourceHelper: pack si està inicialitzat, filesystem si no)
|
||||
bool Menu::load(const std::string &file_path) {
|
||||
auto Menu::load(const std::string &file_path) -> bool {
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
auto bytes = ResourceHelper::loadFile(file_path);
|
||||
if (bytes.empty()) {
|
||||
@@ -158,8 +158,10 @@ bool Menu::load(const std::string &file_path) {
|
||||
}
|
||||
|
||||
// Carga el menu desde bytes en memoria
|
||||
bool Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &nameForLogs) {
|
||||
if (bytes.empty()) return false;
|
||||
auto Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &nameForLogs) -> bool {
|
||||
if (bytes.empty()) {
|
||||
return false;
|
||||
}
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
bool ok = parseFromStream(ss, nameForLogs);
|
||||
@@ -169,7 +171,7 @@ bool Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &n
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setItem(item_t *item, const std::string &var, const std::string &value) {
|
||||
auto Menu::setItem(item_t *item, const std::string &var, const std::string &value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
@@ -182,26 +184,26 @@ bool Menu::setItem(item_t *item, const std::string &var, const std::string &valu
|
||||
}
|
||||
|
||||
else if (var == "selectable") {
|
||||
item->selectable = value == "true" ? true : false;
|
||||
item->selectable = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "greyed") {
|
||||
item->greyed = value == "true" ? true : false;
|
||||
item->greyed = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "linkedDown") {
|
||||
item->linkedDown = value == "true" ? true : false;
|
||||
item->linkedDown = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "visible") {
|
||||
item->visible = value == "true" ? true : false;
|
||||
item->visible = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "line") {
|
||||
item->line = value == "true" ? true : false;
|
||||
item->line = value == "true";
|
||||
}
|
||||
|
||||
else if ((var == "") || (var == "[/item]")) {
|
||||
else if ((var.empty()) || (var == "[/item]")) {
|
||||
}
|
||||
|
||||
else {
|
||||
@@ -212,7 +214,7 @@ bool Menu::setItem(item_t *item, const std::string &var, const std::string &valu
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setVars(const std::string &var, const std::string &value) {
|
||||
auto Menu::setVars(const std::string &var, const std::string &value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
@@ -226,17 +228,23 @@ bool Menu::setVars(const std::string &var, const std::string &value) {
|
||||
|
||||
else if (var == "sound_cancel") {
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) soundCancel = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
if (!bytes.empty()) {
|
||||
soundCancel = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "sound_accept") {
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) soundAccept = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
if (!bytes.empty()) {
|
||||
soundAccept = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "sound_move") {
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) soundMove = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
if (!bytes.empty()) {
|
||||
soundMove = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "name") {
|
||||
@@ -304,22 +312,22 @@ bool Menu::setVars(const std::string &var, const std::string &value) {
|
||||
}
|
||||
|
||||
else if (var == "areElementsCenteredOnX") {
|
||||
areElementsCenteredOnX = value == "true" ? true : false;
|
||||
areElementsCenteredOnX = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnX") {
|
||||
isCenteredOnX = value == "true" ? true : false;
|
||||
isCenteredOnX = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnY") {
|
||||
isCenteredOnY = value == "true" ? true : false;
|
||||
isCenteredOnY = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "defaultActionWhenCancel") {
|
||||
defaultActionWhenCancel = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "") {
|
||||
else if (var.empty()) {
|
||||
}
|
||||
|
||||
else {
|
||||
@@ -350,12 +358,12 @@ void Menu::loadAudioFile(const std::string &file, int sound) {
|
||||
}
|
||||
|
||||
// Obtiene el nombre del menu
|
||||
const std::string &Menu::getName() const {
|
||||
auto Menu::getName() const -> const std::string & {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Menu::getItemSelected() {
|
||||
auto Menu::getItemSelected() -> int {
|
||||
// Al llamar a esta funcion, se obtiene el valor y se borra
|
||||
const int temp = itemSelected;
|
||||
itemSelected = MENU_NO_OPTION;
|
||||
@@ -440,7 +448,7 @@ void Menu::setSelectorPos(int index) {
|
||||
}
|
||||
|
||||
// Obtiene la anchura del elemento más ancho del menu
|
||||
int Menu::getWidestItem() {
|
||||
auto Menu::getWidestItem() -> int {
|
||||
return std::accumulate(item.begin(), item.end(), 0, [](int acc, const item_t &i) { return std::max(acc, i.rect.w); });
|
||||
}
|
||||
|
||||
@@ -729,20 +737,18 @@ void Menu::centerMenuElementsOnX() {
|
||||
}
|
||||
|
||||
// Añade un item al menu
|
||||
void Menu::addItem(item_t temp) {
|
||||
// item_t temp;
|
||||
|
||||
void Menu::addItem(item_t new_item) {
|
||||
if (item.empty()) { // Si es el primer item coge la posición en el eje Y del propio menu
|
||||
temp.rect.y = y;
|
||||
new_item.rect.y = y;
|
||||
} else { // En caso contrario, coge la posición en el eje Y a partir del último elemento
|
||||
temp.rect.y = item.back().rect.y + item.back().rect.h + item.back().hPaddingDown;
|
||||
new_item.rect.y = item.back().rect.y + item.back().rect.h + item.back().hPaddingDown;
|
||||
}
|
||||
|
||||
temp.rect.x = x;
|
||||
new_item.rect.x = x;
|
||||
|
||||
item.push_back(temp);
|
||||
item.push_back(new_item);
|
||||
|
||||
setItemCaption(item.size() - 1, temp.label);
|
||||
setItemCaption(item.size() - 1, new_item.label);
|
||||
|
||||
if (item.size() > 1) {
|
||||
if (item[item.size() - 2].linkedDown) {
|
||||
@@ -771,40 +777,40 @@ void Menu::setDefaultActionWhenCancel(int item) {
|
||||
void Menu::checkInput() {
|
||||
if (Input::get()->checkInput(input_up, REPEAT_FALSE)) {
|
||||
decreaseSelectorIndex();
|
||||
if (soundMove) {
|
||||
if (soundMove != nullptr) {
|
||||
Audio::get()->playSound(soundMove);
|
||||
}
|
||||
}
|
||||
|
||||
if (Input::get()->checkInput(input_down, REPEAT_FALSE)) {
|
||||
increaseSelectorIndex();
|
||||
if (soundMove) {
|
||||
if (soundMove != nullptr) {
|
||||
Audio::get()->playSound(soundMove);
|
||||
}
|
||||
}
|
||||
|
||||
if (Input::get()->checkInput(input_accept, REPEAT_FALSE)) {
|
||||
itemSelected = selector.index;
|
||||
if (soundAccept) {
|
||||
if (soundAccept != nullptr) {
|
||||
Audio::get()->playSound(soundAccept);
|
||||
}
|
||||
}
|
||||
|
||||
if (Input::get()->checkInput(input_cancel, REPEAT_FALSE)) {
|
||||
itemSelected = defaultActionWhenCancel;
|
||||
if (soundCancel) {
|
||||
if (soundCancel != nullptr) {
|
||||
Audio::get()->playSound(soundCancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula el ancho del menu
|
||||
int Menu::findWidth() {
|
||||
auto Menu::findWidth() -> int {
|
||||
return getWidestItem();
|
||||
}
|
||||
|
||||
// Calcula el alto del menu
|
||||
int Menu::findHeight() {
|
||||
auto Menu::findHeight() -> int {
|
||||
const int height = std::accumulate(item.begin(), item.end(), 0, [](int acc, const item_t &i) { return acc + i.rect.h + i.hPaddingDown; });
|
||||
|
||||
return height - item.back().hPaddingDown;
|
||||
@@ -840,12 +846,11 @@ void Menu::setVisible(int index, bool value) {
|
||||
}
|
||||
|
||||
// Calcula la altura del selector
|
||||
int Menu::getSelectorHeight(int value) {
|
||||
auto Menu::getSelectorHeight(int value) -> int {
|
||||
if (item[value].linkedDown) {
|
||||
return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h;
|
||||
} else {
|
||||
return item[value].rect.h;
|
||||
}
|
||||
return item[value].rect.h;
|
||||
}
|
||||
|
||||
// Establece el nombre del menu
|
||||
@@ -866,7 +871,7 @@ void Menu::setBackgroundType(int value) {
|
||||
|
||||
// Establece la fuente de texto que se utilizará
|
||||
void Menu::setText(const std::string &font_png, const std::string &font_txt) {
|
||||
if (!text) {
|
||||
if (text == nullptr) {
|
||||
text = new Text(Asset::get()->get(font_png), Asset::get()->get(font_txt), renderer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user