clang-format
This commit is contained in:
141
source/asset.cpp
141
source/asset.cpp
@@ -1,10 +1,11 @@
|
|||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@@ -27,13 +28,14 @@ auto Asset::get() -> Asset * {
|
|||||||
void Asset::addToMap(const std::string &file_path, Type type, bool required, bool absolute) {
|
void Asset::addToMap(const std::string &file_path, Type type, bool required, bool absolute) {
|
||||||
std::string full_path = absolute ? file_path : executable_path_ + file_path;
|
std::string full_path = absolute ? file_path : executable_path_ + file_path;
|
||||||
std::string filename = getFileName(full_path);
|
std::string filename = getFileName(full_path);
|
||||||
|
|
||||||
// Verificar si ya existe el archivo
|
// Verificar si ya existe el archivo
|
||||||
if (file_list_.find(filename) != file_list_.end()) {
|
if (file_list_.find(filename) != file_list_.end()) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Warning: Asset '%s' already exists, overwriting", filename.c_str());
|
"Warning: Asset '%s' already exists, overwriting",
|
||||||
|
filename.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
file_list_.emplace(filename, Item{std::move(full_path), type, required});
|
file_list_.emplace(filename, Item{std::move(full_path), type, required});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,76 +49,81 @@ void Asset::add(const std::string &file_path, Type type, bool required, bool abs
|
|||||||
void Asset::loadFromFile(const std::string &config_file_path, const std::string &prefix, const std::string &system_folder) {
|
void Asset::loadFromFile(const std::string &config_file_path, const std::string &prefix, const std::string &system_folder) {
|
||||||
std::ifstream file(config_file_path);
|
std::ifstream file(config_file_path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Error: Cannot open config file: %s", config_file_path.c_str());
|
"Error: Cannot open config file: %s",
|
||||||
|
config_file_path.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
int line_number = 0;
|
int line_number = 0;
|
||||||
|
|
||||||
while (std::getline(file, line)) {
|
while (std::getline(file, line)) {
|
||||||
++line_number;
|
++line_number;
|
||||||
|
|
||||||
// Limpiar espacios en blanco al principio y final
|
// Limpiar espacios en blanco al principio y final
|
||||||
line.erase(0, line.find_first_not_of(" \t\r"));
|
line.erase(0, line.find_first_not_of(" \t\r"));
|
||||||
line.erase(line.find_last_not_of(" \t\r") + 1);
|
line.erase(line.find_last_not_of(" \t\r") + 1);
|
||||||
|
|
||||||
// DEBUG: mostrar línea leída (opcional, puedes comentar esta línea)
|
// DEBUG: mostrar línea leída (opcional, puedes comentar esta línea)
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Line %d: '%s'", line_number, line.c_str());
|
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Line %d: '%s'", line_number, line.c_str());
|
||||||
|
|
||||||
// Ignorar líneas vacías y comentarios
|
// Ignorar líneas vacías y comentarios
|
||||||
if (line.empty() || line[0] == '#' || line[0] == ';') {
|
if (line.empty() || line[0] == '#' || line[0] == ';') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dividir la línea por el separador '|'
|
// Dividir la línea por el separador '|'
|
||||||
std::vector<std::string> parts;
|
std::vector<std::string> parts;
|
||||||
std::istringstream iss(line);
|
std::istringstream iss(line);
|
||||||
std::string part;
|
std::string part;
|
||||||
|
|
||||||
while (std::getline(iss, part, '|')) {
|
while (std::getline(iss, part, '|')) {
|
||||||
parts.push_back(part);
|
parts.push_back(part);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verificar que tenemos al menos tipo y ruta
|
// Verificar que tenemos al menos tipo y ruta
|
||||||
if (parts.size() < 2) {
|
if (parts.size() < 2) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Warning: Malformed line %d in config file (insufficient fields)", line_number);
|
"Warning: Malformed line %d in config file (insufficient fields)",
|
||||||
|
line_number);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::string type_str = parts[0];
|
std::string type_str = parts[0];
|
||||||
std::string path = parts[1];
|
std::string path = parts[1];
|
||||||
|
|
||||||
// Valores por defecto
|
// Valores por defecto
|
||||||
bool required = true;
|
bool required = true;
|
||||||
bool absolute = false;
|
bool absolute = false;
|
||||||
|
|
||||||
// Si hay opciones en el tercer campo, parsearlas
|
// Si hay opciones en el tercer campo, parsearlas
|
||||||
if (parts.size() >= 3) {
|
if (parts.size() >= 3) {
|
||||||
parseOptions(parts[2], required, absolute);
|
parseOptions(parts[2], required, absolute);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reemplazar variables en la ruta
|
// Reemplazar variables en la ruta
|
||||||
path = replaceVariables(path, prefix, system_folder);
|
path = replaceVariables(path, prefix, system_folder);
|
||||||
|
|
||||||
// Parsear el tipo de asset
|
// Parsear el tipo de asset
|
||||||
Type type = parseAssetType(type_str);
|
Type type = parseAssetType(type_str);
|
||||||
|
|
||||||
// Añadir al mapa
|
// Añadir al mapa
|
||||||
addToMap(path, type, required, absolute);
|
addToMap(path, type, required, absolute);
|
||||||
|
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Error parsing line %d in config file: %s", line_number, e.what());
|
"Error parsing line %d in config file: %s",
|
||||||
|
line_number,
|
||||||
|
e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Loaded %d assets from config file", static_cast<int>(file_list_.size()));
|
"Loaded %d assets from config file",
|
||||||
|
static_cast<int>(file_list_.size()));
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +133,7 @@ auto Asset::get(const std::string &filename) const -> std::string {
|
|||||||
if (it != file_list_.end()) {
|
if (it != file_list_.end()) {
|
||||||
return it->second.file;
|
return it->second.file;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: file %s not found", filename.c_str());
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: file %s not found", filename.c_str());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -139,26 +146,27 @@ auto Asset::exists(const std::string &filename) const -> bool {
|
|||||||
// Comprueba que existen todos los elementos
|
// Comprueba que existen todos los elementos
|
||||||
auto Asset::check() const -> bool {
|
auto Asset::check() const -> bool {
|
||||||
bool success = true;
|
bool success = true;
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES");
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES");
|
||||||
|
|
||||||
// Agrupar por tipo para mostrar organizado
|
// Agrupar por tipo para mostrar organizado
|
||||||
std::unordered_map<Type, std::vector<const Item*>> by_type;
|
std::unordered_map<Type, std::vector<const Item *>> by_type;
|
||||||
|
|
||||||
for (const auto &[filename, item] : file_list_) {
|
for (const auto &[filename, item] : file_list_) {
|
||||||
if (item.required) {
|
if (item.required) {
|
||||||
by_type[item.type].push_back(&item);
|
by_type[item.type].push_back(&item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verificar por tipo
|
// Verificar por tipo
|
||||||
for (int type = 0; type < static_cast<int>(Type::SIZE); ++type) {
|
for (int type = 0; type < static_cast<int>(Type::SIZE); ++type) {
|
||||||
Type asset_type = static_cast<Type>(type);
|
Type asset_type = static_cast<Type>(type);
|
||||||
|
|
||||||
if (by_type.find(asset_type) != by_type.end()) {
|
if (by_type.find(asset_type) != by_type.end()) {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"\n>> %s FILES", getTypeName(asset_type).c_str());
|
"\n>> %s FILES",
|
||||||
|
getTypeName(asset_type).c_str());
|
||||||
|
|
||||||
bool type_success = true;
|
bool type_success = true;
|
||||||
for (const auto *item : by_type[asset_type]) {
|
for (const auto *item : by_type[asset_type]) {
|
||||||
if (!checkFile(item->file)) {
|
if (!checkFile(item->file)) {
|
||||||
@@ -166,20 +174,20 @@ auto Asset::check() const -> bool {
|
|||||||
type_success = false;
|
type_success = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type_success) {
|
if (type_success) {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, " All files are OK.");
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, " All files are OK.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resultado
|
// Resultado
|
||||||
if (success) {
|
if (success) {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES COMPLETED.\n");
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES COMPLETED.\n");
|
||||||
} else {
|
} else {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES FAILED.\n");
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES FAILED.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,12 +196,13 @@ auto Asset::checkFile(const std::string &path) -> bool {
|
|||||||
std::ifstream file(path);
|
std::ifstream file(path);
|
||||||
bool success = file.good();
|
bool success = file.good();
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Checking file: %s [ ERROR ]", getFileName(path).c_str());
|
"Checking file: %s [ ERROR ]",
|
||||||
|
getFileName(path).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,50 +242,60 @@ auto Asset::parseAssetType(const std::string &type_str) -> Type {
|
|||||||
// Devuelve el nombre del tipo de recurso
|
// Devuelve el nombre del tipo de recurso
|
||||||
auto Asset::getTypeName(Type type) -> std::string {
|
auto Asset::getTypeName(Type type) -> std::string {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Type::BITMAP: return "BITMAP";
|
case Type::BITMAP:
|
||||||
case Type::MUSIC: return "MUSIC";
|
return "BITMAP";
|
||||||
case Type::SOUND: return "SOUND";
|
case Type::MUSIC:
|
||||||
case Type::FONT: return "FONT";
|
return "MUSIC";
|
||||||
case Type::LANG: return "LANG";
|
case Type::SOUND:
|
||||||
case Type::DATA: return "DATA";
|
return "SOUND";
|
||||||
case Type::DEMODATA: return "DEMODATA";
|
case Type::FONT:
|
||||||
case Type::ANIMATION: return "ANIMATION";
|
return "FONT";
|
||||||
case Type::PALETTE: return "PALETTE";
|
case Type::LANG:
|
||||||
default: return "ERROR";
|
return "LANG";
|
||||||
|
case Type::DATA:
|
||||||
|
return "DATA";
|
||||||
|
case Type::DEMODATA:
|
||||||
|
return "DEMODATA";
|
||||||
|
case Type::ANIMATION:
|
||||||
|
return "ANIMATION";
|
||||||
|
case Type::PALETTE:
|
||||||
|
return "PALETTE";
|
||||||
|
default:
|
||||||
|
return "ERROR";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Devuelve la lista de recursos de un tipo
|
// Devuelve la lista de recursos de un tipo
|
||||||
auto Asset::getListByType(Type type) const -> std::vector<std::string> {
|
auto Asset::getListByType(Type type) const -> std::vector<std::string> {
|
||||||
std::vector<std::string> list;
|
std::vector<std::string> list;
|
||||||
|
|
||||||
for (const auto &[filename, item] : file_list_) {
|
for (const auto &[filename, item] : file_list_) {
|
||||||
if (item.type == type) {
|
if (item.type == type) {
|
||||||
list.push_back(item.file);
|
list.push_back(item.file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reemplaza variables en las rutas
|
// Reemplaza variables en las rutas
|
||||||
auto Asset::replaceVariables(const std::string &path, const std::string &prefix, const std::string &system_folder) -> std::string {
|
auto Asset::replaceVariables(const std::string &path, const std::string &prefix, const std::string &system_folder) -> std::string {
|
||||||
std::string result = path;
|
std::string result = path;
|
||||||
|
|
||||||
// Reemplazar ${PREFIX}
|
// Reemplazar ${PREFIX}
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
while ((pos = result.find("${PREFIX}", pos)) != std::string::npos) {
|
while ((pos = result.find("${PREFIX}", pos)) != std::string::npos) {
|
||||||
result.replace(pos, 9, prefix); // 9 = longitud de "${PREFIX}"
|
result.replace(pos, 9, prefix); // 9 = longitud de "${PREFIX}"
|
||||||
pos += prefix.length();
|
pos += prefix.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reemplazar ${SYSTEM_FOLDER}
|
// Reemplazar ${SYSTEM_FOLDER}
|
||||||
pos = 0;
|
pos = 0;
|
||||||
while ((pos = result.find("${SYSTEM_FOLDER}", pos)) != std::string::npos) {
|
while ((pos = result.find("${SYSTEM_FOLDER}", pos)) != std::string::npos) {
|
||||||
result.replace(pos, 16, system_folder); // 16 = longitud de "${SYSTEM_FOLDER}"
|
result.replace(pos, 16, system_folder); // 16 = longitud de "${SYSTEM_FOLDER}"
|
||||||
pos += system_folder.length();
|
pos += system_folder.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,12 +307,12 @@ auto Asset::parseOptions(const std::string &options, bool &required, bool &absol
|
|||||||
|
|
||||||
std::istringstream iss(options);
|
std::istringstream iss(options);
|
||||||
std::string option;
|
std::string option;
|
||||||
|
|
||||||
while (std::getline(iss, option, ',')) {
|
while (std::getline(iss, option, ',')) {
|
||||||
// Eliminar espacios
|
// Eliminar espacios
|
||||||
option.erase(0, option.find_first_not_of(" \t"));
|
option.erase(0, option.find_first_not_of(" \t"));
|
||||||
option.erase(option.find_last_not_of(" \t") + 1);
|
option.erase(option.find_last_not_of(" \t") + 1);
|
||||||
|
|
||||||
if (option == "optional") {
|
if (option == "optional") {
|
||||||
required = false;
|
required = false;
|
||||||
} else if (option == "absolute") {
|
} else if (option == "absolute") {
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// Clase Asset: gestor optimizado de recursos (singleton)
|
// Clase Asset: gestor optimizado de recursos (singleton)
|
||||||
class Asset {
|
class Asset {
|
||||||
@@ -32,7 +32,7 @@ class Asset {
|
|||||||
// --- Métodos para la gestión de recursos ---
|
// --- Métodos para la gestión de recursos ---
|
||||||
void add(const std::string &file_path, Type type, bool required = true, bool absolute = false);
|
void add(const std::string &file_path, Type type, bool required = true, bool absolute = false);
|
||||||
void loadFromFile(const std::string &config_file_path, const std::string &prefix = "", const std::string &system_folder = ""); // Con soporte para variables
|
void loadFromFile(const std::string &config_file_path, const std::string &prefix = "", const std::string &system_folder = ""); // Con soporte para variables
|
||||||
[[nodiscard]] auto get(const std::string &filename) const -> std::string; // Mantener nombre original
|
[[nodiscard]] auto get(const std::string &filename) const -> std::string; // Mantener nombre original
|
||||||
[[nodiscard]] auto check() const -> bool;
|
[[nodiscard]] auto check() const -> bool;
|
||||||
[[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
|
[[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
|
||||||
[[nodiscard]] auto exists(const std::string &filename) const -> bool; // Nueva función para verificar existencia
|
[[nodiscard]] auto exists(const std::string &filename) const -> bool; // Nueva función para verificar existencia
|
||||||
@@ -40,17 +40,17 @@ class Asset {
|
|||||||
private:
|
private:
|
||||||
// --- Estructura interna para almacenar información de cada recurso ---
|
// --- Estructura interna para almacenar información de cada recurso ---
|
||||||
struct Item {
|
struct Item {
|
||||||
std::string file; // Ruta completa del archivo (mantener nombre original)
|
std::string file; // Ruta completa del archivo (mantener nombre original)
|
||||||
Type type; // Tipo de recurso
|
Type type; // Tipo de recurso
|
||||||
bool required; // Indica si el archivo es obligatorio
|
bool required; // Indica si el archivo es obligatorio
|
||||||
|
|
||||||
Item(std::string path, Type asset_type, bool is_required)
|
Item(std::string path, Type asset_type, bool is_required)
|
||||||
: file(std::move(path)), type(asset_type), required(is_required) {}
|
: file(std::move(path)), type(asset_type), required(is_required) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Variables internas ---
|
// --- Variables internas ---
|
||||||
std::unordered_map<std::string, Item> file_list_; // Mapa para búsqueda O(1) (mantener nombre original)
|
std::unordered_map<std::string, Item> file_list_; // Mapa para búsqueda O(1) (mantener nombre original)
|
||||||
std::string executable_path_; // Ruta del ejecutable
|
std::string executable_path_; // Ruta del ejecutable
|
||||||
|
|
||||||
// --- Métodos internos ---
|
// --- Métodos internos ---
|
||||||
[[nodiscard]] static auto checkFile(const std::string &path) -> bool;
|
[[nodiscard]] static auto checkFile(const std::string &path) -> bool;
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ void Director::loadAssets() {
|
|||||||
// Cargar la configuración de assets (también aplicar el prefijo al archivo de configuración)
|
// Cargar la configuración de assets (también aplicar el prefijo al archivo de configuración)
|
||||||
std::string config_path = executable_path_ + prefix + "/data/config/assets.txt";
|
std::string config_path = executable_path_ + prefix + "/data/config/assets.txt";
|
||||||
Asset::get()->loadFromFile(config_path, prefix, system_folder_);
|
Asset::get()->loadFromFile(config_path, prefix, system_folder_);
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Assets configuration loaded successfully");
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Assets configuration loaded successfully");
|
||||||
|
|
||||||
// Si falta algun fichero, sale del programa
|
// Si falta algun fichero, sale del programa
|
||||||
|
|||||||
@@ -247,7 +247,7 @@ class GamepadManager {
|
|||||||
private:
|
private:
|
||||||
static constexpr std::string_view UNASSIGNED_TEXT = "---";
|
static constexpr std::string_view UNASSIGNED_TEXT = "---";
|
||||||
static constexpr size_t MAX_PLAYERS = 2;
|
static constexpr size_t MAX_PLAYERS = 2;
|
||||||
std::array<Gamepad, MAX_PLAYERS> gamepads_; // Punteros a las estructuras de mandos de Options
|
std::array<Gamepad, MAX_PLAYERS> gamepads_; // Punteros a las estructuras de mandos de Options
|
||||||
std::vector<std::shared_ptr<Player>> players_; // Punteros a los jugadores
|
std::vector<std::shared_ptr<Player>> players_; // Punteros a los jugadores
|
||||||
|
|
||||||
// Convierte Player::Id a índice del array
|
// Convierte Player::Id a índice del array
|
||||||
|
|||||||
@@ -460,26 +460,21 @@ void Player::setAnimation() {
|
|||||||
player_sprite_->setFlip(flipMode);
|
player_sprite_->setFlip(flipMode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case State::WAITING:
|
case State::WAITING:
|
||||||
player_sprite_->setCurrentAnimation("hello");
|
player_sprite_->setCurrentAnimation("hello");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case State::ROLLING:
|
case State::ROLLING:
|
||||||
case State::CONTINUE_TIME_OUT:
|
case State::CONTINUE_TIME_OUT:
|
||||||
player_sprite_->setCurrentAnimation("rolling");
|
player_sprite_->setCurrentAnimation("rolling");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case State::LYING_ON_THE_FLOOR_FOREVER:
|
case State::LYING_ON_THE_FLOOR_FOREVER:
|
||||||
case State::ENTERING_NAME:
|
case State::ENTERING_NAME:
|
||||||
case State::CONTINUE:
|
case State::CONTINUE:
|
||||||
player_sprite_->setCurrentAnimation("dead");
|
player_sprite_->setCurrentAnimation("dead");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case State::CELEBRATING:
|
case State::CELEBRATING:
|
||||||
player_sprite_->setCurrentAnimation("celebration");
|
player_sprite_->setCurrentAnimation("celebration");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,25 +13,33 @@
|
|||||||
// --- Implementación de las estructuras de animación ---
|
// --- Implementación de las estructuras de animación ---
|
||||||
|
|
||||||
void MenuRenderer::ResizeAnimation::start(float from_w, float from_h, float to_w, float to_h) {
|
void MenuRenderer::ResizeAnimation::start(float from_w, float from_h, float to_w, float to_h) {
|
||||||
start_width = from_w; start_height = from_h;
|
start_width = from_w;
|
||||||
target_width = to_w; target_height = to_h;
|
start_height = from_h;
|
||||||
|
target_width = to_w;
|
||||||
|
target_height = to_h;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
active = true;
|
active = true;
|
||||||
}
|
}
|
||||||
void MenuRenderer::ResizeAnimation::stop() { active = false;
|
void MenuRenderer::ResizeAnimation::stop() {
|
||||||
|
active = false;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MenuRenderer::ShowHideAnimation::startShow(float to_w, float to_h) {
|
void MenuRenderer::ShowHideAnimation::startShow(float to_w, float to_h) {
|
||||||
type = Type::SHOWING; target_width = to_w; target_height = to_h;
|
type = Type::SHOWING;
|
||||||
|
target_width = to_w;
|
||||||
|
target_height = to_h;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
active = true;
|
active = true;
|
||||||
}
|
}
|
||||||
void MenuRenderer::ShowHideAnimation::startHide() { type = Type::HIDING;
|
void MenuRenderer::ShowHideAnimation::startHide() {
|
||||||
|
type = Type::HIDING;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
active = true;
|
active = true;
|
||||||
}
|
}
|
||||||
void MenuRenderer::ShowHideAnimation::stop() { type = Type::NONE; active = false;
|
void MenuRenderer::ShowHideAnimation::stop() {
|
||||||
|
type = Type::NONE;
|
||||||
|
active = false;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +70,7 @@ void MenuRenderer::render(const ServiceMenu *menu_state) {
|
|||||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), BORDER_COLOR.r, BORDER_COLOR.g, BORDER_COLOR.b, 255);
|
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), BORDER_COLOR.r, BORDER_COLOR.g, BORDER_COLOR.b, 255);
|
||||||
SDL_RenderRect(Screen::get()->getRenderer(), &rect_);
|
SDL_RenderRect(Screen::get()->getRenderer(), &rect_);
|
||||||
SDL_RenderRect(Screen::get()->getRenderer(), &border_rect_);
|
SDL_RenderRect(Screen::get()->getRenderer(), &border_rect_);
|
||||||
|
|
||||||
// Solo renderizar contenido si la animación lo permite
|
// Solo renderizar contenido si la animación lo permite
|
||||||
if (shouldShowContent()) {
|
if (shouldShowContent()) {
|
||||||
// Dibuja el título
|
// Dibuja el título
|
||||||
@@ -101,7 +109,7 @@ void MenuRenderer::render(const ServiceMenu *menu_state) {
|
|||||||
void MenuRenderer::update(const ServiceMenu *menu_state) {
|
void MenuRenderer::update(const ServiceMenu *menu_state) {
|
||||||
float delta_time = 1.0F / 60.0F; // Asumiendo 60 FPS
|
float delta_time = 1.0F / 60.0F; // Asumiendo 60 FPS
|
||||||
updateAnimations(delta_time);
|
updateAnimations(delta_time);
|
||||||
|
|
||||||
if (visible_) {
|
if (visible_) {
|
||||||
updateColorCounter();
|
updateColorCounter();
|
||||||
param.service_menu.selected_color = getAnimatedSelectedColor();
|
param.service_menu.selected_color = getAnimatedSelectedColor();
|
||||||
@@ -110,7 +118,7 @@ void MenuRenderer::update(const ServiceMenu *menu_state) {
|
|||||||
|
|
||||||
// --- Nuevos métodos de control ---
|
// --- Nuevos métodos de control ---
|
||||||
|
|
||||||
void MenuRenderer::show(const ServiceMenu* menu_state) {
|
void MenuRenderer::show(const ServiceMenu *menu_state) {
|
||||||
if (visible_) {
|
if (visible_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -118,13 +126,13 @@ void MenuRenderer::show(const ServiceMenu* menu_state) {
|
|||||||
|
|
||||||
// Calcula el tamaño final y lo usa para la animación
|
// Calcula el tamaño final y lo usa para la animación
|
||||||
SDL_FRect target_rect = calculateNewRect(menu_state);
|
SDL_FRect target_rect = calculateNewRect(menu_state);
|
||||||
|
|
||||||
// Detener cualquier animación anterior
|
// Detener cualquier animación anterior
|
||||||
resize_animation_.stop();
|
resize_animation_.stop();
|
||||||
|
|
||||||
// Iniciar animación de mostrar
|
// Iniciar animación de mostrar
|
||||||
show_hide_animation_.startShow(target_rect.w, target_rect.h);
|
show_hide_animation_.startShow(target_rect.w, target_rect.h);
|
||||||
|
|
||||||
// El tamaño inicial es cero para la animación
|
// El tamaño inicial es cero para la animación
|
||||||
rect_.w = 0.0F;
|
rect_.w = 0.0F;
|
||||||
rect_.h = 0.0F;
|
rect_.h = 0.0F;
|
||||||
@@ -194,14 +202,13 @@ auto MenuRenderer::calculateNewRect(const ServiceMenu *menu_state) -> SDL_FRect
|
|||||||
const auto &display_options = menu_state->getDisplayOptions();
|
const auto &display_options = menu_state->getDisplayOptions();
|
||||||
lower_height_ = ((!display_options.empty() ? display_options.size() - 1 : 0) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2);
|
lower_height_ = ((!display_options.empty() ? display_options.size() - 1 : 0) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2);
|
||||||
height_ = std::min(upper_height_ + lower_height_, max_menu_height_);
|
height_ = std::min(upper_height_ + lower_height_, max_menu_height_);
|
||||||
|
|
||||||
SDL_FRect new_rect = {0, 0, (float)width_, (float)height_};
|
SDL_FRect new_rect = {0, 0, (float)width_, (float)height_};
|
||||||
|
|
||||||
// La posición x, y se establecerá en `updatePosition`
|
// La posición x, y se establecerá en `updatePosition`
|
||||||
return new_rect;
|
return new_rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MenuRenderer::resize(const ServiceMenu *menu_state) {
|
void MenuRenderer::resize(const ServiceMenu *menu_state) {
|
||||||
SDL_FRect new_rect = calculateNewRect(menu_state);
|
SDL_FRect new_rect = calculateNewRect(menu_state);
|
||||||
|
|
||||||
@@ -219,7 +226,7 @@ void MenuRenderer::setSize(const ServiceMenu *menu_state) {
|
|||||||
SDL_FRect new_rect = calculateNewRect(menu_state);
|
SDL_FRect new_rect = calculateNewRect(menu_state);
|
||||||
rect_.w = new_rect.w;
|
rect_.w = new_rect.w;
|
||||||
rect_.h = new_rect.h;
|
rect_.h = new_rect.h;
|
||||||
|
|
||||||
show_hide_animation_.stop();
|
show_hide_animation_.stop();
|
||||||
resize_animation_.stop();
|
resize_animation_.stop();
|
||||||
|
|
||||||
@@ -242,7 +249,7 @@ void MenuRenderer::updateAnimations(float delta_time) {
|
|||||||
void MenuRenderer::updateShowHideAnimation(float delta_time) {
|
void MenuRenderer::updateShowHideAnimation(float delta_time) {
|
||||||
show_hide_animation_.elapsed += delta_time;
|
show_hide_animation_.elapsed += delta_time;
|
||||||
float duration = show_hide_animation_.duration;
|
float duration = show_hide_animation_.duration;
|
||||||
|
|
||||||
if (show_hide_animation_.elapsed >= duration) {
|
if (show_hide_animation_.elapsed >= duration) {
|
||||||
if (show_hide_animation_.type == ShowHideAnimation::Type::SHOWING) {
|
if (show_hide_animation_.type == ShowHideAnimation::Type::SHOWING) {
|
||||||
rect_.w = show_hide_animation_.target_width;
|
rect_.w = show_hide_animation_.target_width;
|
||||||
@@ -286,7 +293,6 @@ void MenuRenderer::updateResizeAnimation(float delta_time) {
|
|||||||
options_y_ = rect_.y + upper_height_ + lower_padding_;
|
options_y_ = rect_.y + upper_height_ + lower_padding_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MenuRenderer::updatePosition() {
|
void MenuRenderer::updatePosition() {
|
||||||
switch (position_mode_) {
|
switch (position_mode_) {
|
||||||
case PositionMode::CENTERED:
|
case PositionMode::CENTERED:
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class MenuRenderer {
|
|||||||
|
|
||||||
// --- Nuevos: Métodos de configuración de posición ---
|
// --- Nuevos: Métodos de configuración de posición ---
|
||||||
void setPosition(float x, float y, PositionMode mode);
|
void setPosition(float x, float y, PositionMode mode);
|
||||||
|
|
||||||
// Método para notificar al renderer que el layout puede haber cambiado
|
// Método para notificar al renderer que el layout puede haber cambiado
|
||||||
void onLayoutChanged(const ServiceMenu *menu_state);
|
void onLayoutChanged(const ServiceMenu *menu_state);
|
||||||
void setLayout(const ServiceMenu *menu_state);
|
void setLayout(const ServiceMenu *menu_state);
|
||||||
@@ -77,27 +77,29 @@ class MenuRenderer {
|
|||||||
|
|
||||||
// --- Estructuras de Animación (inspirado en WindowMessage) ---
|
// --- Estructuras de Animación (inspirado en WindowMessage) ---
|
||||||
struct ResizeAnimation {
|
struct ResizeAnimation {
|
||||||
bool active = false;
|
bool active = false;
|
||||||
float start_width, start_height;
|
float start_width, start_height;
|
||||||
float target_width, target_height;
|
float target_width, target_height;
|
||||||
float elapsed = 0.0F;
|
float elapsed = 0.0F;
|
||||||
float duration = 0.2F;
|
float duration = 0.2F;
|
||||||
|
|
||||||
void start(float from_w, float from_h, float to_w, float to_h);
|
void start(float from_w, float from_h, float to_w, float to_h);
|
||||||
void stop();
|
void stop();
|
||||||
} resize_animation_;
|
} resize_animation_;
|
||||||
|
|
||||||
struct ShowHideAnimation {
|
struct ShowHideAnimation {
|
||||||
enum class Type { NONE, SHOWING, HIDING };
|
enum class Type { NONE,
|
||||||
Type type = Type::NONE;
|
SHOWING,
|
||||||
bool active = false;
|
HIDING };
|
||||||
float target_width, target_height;
|
Type type = Type::NONE;
|
||||||
float elapsed = 0.0F;
|
bool active = false;
|
||||||
float duration = 0.25F;
|
float target_width, target_height;
|
||||||
|
float elapsed = 0.0F;
|
||||||
|
float duration = 0.25F;
|
||||||
|
|
||||||
void startShow(float to_w, float to_h);
|
void startShow(float to_w, float to_h);
|
||||||
void startHide();
|
void startHide();
|
||||||
void stop();
|
void stop();
|
||||||
} show_hide_animation_;
|
} show_hide_animation_;
|
||||||
|
|
||||||
// --- Anchos precalculados ---
|
// --- Anchos precalculados ---
|
||||||
@@ -109,7 +111,7 @@ class MenuRenderer {
|
|||||||
auto calculateNewRect(const ServiceMenu *menu_state) -> SDL_FRect;
|
auto calculateNewRect(const ServiceMenu *menu_state) -> SDL_FRect;
|
||||||
void resize(const ServiceMenu *menu_state);
|
void resize(const ServiceMenu *menu_state);
|
||||||
void setSize(const ServiceMenu *menu_state);
|
void setSize(const ServiceMenu *menu_state);
|
||||||
|
|
||||||
void updateAnimations(float delta_time);
|
void updateAnimations(float delta_time);
|
||||||
void updateResizeAnimation(float delta_time);
|
void updateResizeAnimation(float delta_time);
|
||||||
void updateShowHideAnimation(float delta_time);
|
void updateShowHideAnimation(float delta_time);
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ void ServiceMenu::toggle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
playBackSound();
|
playBackSound();
|
||||||
|
|
||||||
if (!enabled_) { // Si está cerrado, abrir
|
if (!enabled_) { // Si está cerrado, abrir
|
||||||
reset();
|
reset();
|
||||||
Options::gamepad_manager.assignAndLinkGamepads();
|
Options::gamepad_manager.assignAndLinkGamepads();
|
||||||
|
|||||||
@@ -26,13 +26,11 @@ void WindowMessage::render() {
|
|||||||
SDL_Renderer* renderer = Screen::get()->getRenderer();
|
SDL_Renderer* renderer = Screen::get()->getRenderer();
|
||||||
|
|
||||||
// Dibujar fondo con transparencia
|
// Dibujar fondo con transparencia
|
||||||
SDL_SetRenderDrawColor(renderer, config_.bg_color.r, config_.bg_color.g,
|
SDL_SetRenderDrawColor(renderer, config_.bg_color.r, config_.bg_color.g, config_.bg_color.b, config_.bg_color.a);
|
||||||
config_.bg_color.b, config_.bg_color.a);
|
|
||||||
SDL_RenderFillRect(renderer, &rect_);
|
SDL_RenderFillRect(renderer, &rect_);
|
||||||
|
|
||||||
// Dibujar borde
|
// Dibujar borde
|
||||||
SDL_SetRenderDrawColor(renderer, config_.border_color.r, config_.border_color.g,
|
SDL_SetRenderDrawColor(renderer, config_.border_color.r, config_.border_color.g, config_.border_color.b, config_.border_color.a);
|
||||||
config_.border_color.b, config_.border_color.a);
|
|
||||||
SDL_RenderRect(renderer, &rect_);
|
SDL_RenderRect(renderer, &rect_);
|
||||||
|
|
||||||
// Solo mostrar contenido si no estamos en animación de show/hide
|
// Solo mostrar contenido si no estamos en animación de show/hide
|
||||||
@@ -54,8 +52,7 @@ void WindowMessage::render() {
|
|||||||
|
|
||||||
// Línea separadora debajo del título (solo si hay título visible)
|
// Línea separadora debajo del título (solo si hay título visible)
|
||||||
if (!visible_title.empty()) {
|
if (!visible_title.empty()) {
|
||||||
SDL_SetRenderDrawColor(renderer, config_.border_color.r, config_.border_color.g,
|
SDL_SetRenderDrawColor(renderer, config_.border_color.r, config_.border_color.g, config_.border_color.b, config_.border_color.a);
|
||||||
config_.border_color.b, config_.border_color.a);
|
|
||||||
SDL_RenderLine(renderer,
|
SDL_RenderLine(renderer,
|
||||||
rect_.x + config_.padding,
|
rect_.x + config_.padding,
|
||||||
current_y - config_.title_separator_spacing / 2.0F,
|
current_y - config_.title_separator_spacing / 2.0F,
|
||||||
@@ -91,34 +88,34 @@ void WindowMessage::update() {
|
|||||||
|
|
||||||
void WindowMessage::show() {
|
void WindowMessage::show() {
|
||||||
if (visible_) {
|
if (visible_) {
|
||||||
return; // Ya visible
|
return; // Ya visible
|
||||||
}
|
}
|
||||||
|
|
||||||
visible_ = true;
|
visible_ = true;
|
||||||
ensureTextFits();
|
ensureTextFits();
|
||||||
|
|
||||||
// Detener cualquier animación anterior
|
// Detener cualquier animación anterior
|
||||||
resize_animation_.stop();
|
resize_animation_.stop();
|
||||||
|
|
||||||
// Iniciar animación de mostrar desde tamaño 0
|
// Iniciar animación de mostrar desde tamaño 0
|
||||||
show_hide_animation_.startShow(rect_.w, rect_.h);
|
show_hide_animation_.startShow(rect_.w, rect_.h);
|
||||||
rect_.w = 0.0F;
|
rect_.w = 0.0F;
|
||||||
rect_.h = 0.0F;
|
rect_.h = 0.0F;
|
||||||
updatePosition(); // Reposicionar con tamaño 0
|
updatePosition(); // Reposicionar con tamaño 0
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowMessage::hide() {
|
void WindowMessage::hide() {
|
||||||
if (!visible_) {
|
if (!visible_) {
|
||||||
return; // Ya oculto
|
return; // Ya oculto
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detener cualquier animación anterior
|
// Detener cualquier animación anterior
|
||||||
resize_animation_.stop();
|
resize_animation_.stop();
|
||||||
|
|
||||||
// Guardar el tamaño actual para la animación
|
// Guardar el tamaño actual para la animación
|
||||||
show_hide_animation_.target_width = rect_.w;
|
show_hide_animation_.target_width = rect_.w;
|
||||||
show_hide_animation_.target_height = rect_.h;
|
show_hide_animation_.target_height = rect_.h;
|
||||||
|
|
||||||
// Iniciar animación de ocultar hacia tamaño 0
|
// Iniciar animación de ocultar hacia tamaño 0
|
||||||
show_hide_animation_.startHide();
|
show_hide_animation_.startHide();
|
||||||
}
|
}
|
||||||
@@ -158,7 +155,7 @@ void WindowMessage::setPosition(float x, float y, PositionMode mode) {
|
|||||||
void WindowMessage::setSize(float width, float height) {
|
void WindowMessage::setSize(float width, float height) {
|
||||||
rect_.w = width;
|
rect_.w = width;
|
||||||
rect_.h = height;
|
rect_.h = height;
|
||||||
updatePosition(); // Reposicionar después de cambiar el tamaño
|
updatePosition(); // Reposicionar después de cambiar el tamaño
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowMessage::centerOnScreen() {
|
void WindowMessage::centerOnScreen() {
|
||||||
@@ -167,18 +164,18 @@ void WindowMessage::centerOnScreen() {
|
|||||||
|
|
||||||
void WindowMessage::autoSize() {
|
void WindowMessage::autoSize() {
|
||||||
if (show_hide_animation_.active) {
|
if (show_hide_animation_.active) {
|
||||||
return; // No redimensionar durante show/hide
|
return; // No redimensionar durante show/hide
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resize_animation_.active) {
|
if (resize_animation_.active) {
|
||||||
resize_animation_.stop(); // Detener animación anterior
|
resize_animation_.stop(); // Detener animación anterior
|
||||||
}
|
}
|
||||||
|
|
||||||
float old_width = rect_.w;
|
float old_width = rect_.w;
|
||||||
float old_height = rect_.h;
|
float old_height = rect_.h;
|
||||||
|
|
||||||
calculateAutoSize();
|
calculateAutoSize();
|
||||||
|
|
||||||
// Solo animar si hay cambio en el tamaño y la ventana está visible
|
// Solo animar si hay cambio en el tamaño y la ventana está visible
|
||||||
if (visible_ && (old_width != rect_.w || old_height != rect_.h)) {
|
if (visible_ && (old_width != rect_.w || old_height != rect_.h)) {
|
||||||
resize_animation_.start(old_width, old_height, rect_.w, rect_.h);
|
resize_animation_.start(old_width, old_height, rect_.w, rect_.h);
|
||||||
@@ -186,21 +183,19 @@ void WindowMessage::autoSize() {
|
|||||||
rect_.w = old_width;
|
rect_.w = old_width;
|
||||||
rect_.h = old_height;
|
rect_.h = old_height;
|
||||||
} else {
|
} else {
|
||||||
updatePosition(); // Reposicionar después de ajustar el tamaño
|
updatePosition(); // Reposicionar después de ajustar el tamaño
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowMessage::updateStyles() {
|
void WindowMessage::updateStyles() {
|
||||||
title_style_ = Text::Style(Text::CENTER | Text::COLOR, config_.title_color,
|
title_style_ = Text::Style(Text::CENTER | Text::COLOR, config_.title_color, config_.title_color, 0, -2);
|
||||||
config_.title_color, 0, -2);
|
text_style_ = Text::Style(Text::CENTER | Text::COLOR, config_.text_color, config_.text_color, 0, -2);
|
||||||
text_style_ = Text::Style(Text::CENTER | Text::COLOR, config_.text_color,
|
|
||||||
config_.text_color, 0, -2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowMessage::updatePosition() {
|
void WindowMessage::updatePosition() {
|
||||||
switch (position_mode_) {
|
switch (position_mode_) {
|
||||||
case PositionMode::CENTERED:
|
case PositionMode::CENTERED:
|
||||||
rect_.x = anchor_.x- rect_.w / 2.0F;
|
rect_.x = anchor_.x - rect_.w / 2.0F;
|
||||||
rect_.y = anchor_.y - rect_.h / 2.0F;
|
rect_.y = anchor_.y - rect_.h / 2.0F;
|
||||||
break;
|
break;
|
||||||
case PositionMode::FIXED:
|
case PositionMode::FIXED:
|
||||||
@@ -217,10 +212,10 @@ void WindowMessage::updatePosition() {
|
|||||||
void WindowMessage::ensureTextFits() {
|
void WindowMessage::ensureTextFits() {
|
||||||
float required_width = calculateContentWidth() + (config_.padding * 2) + config_.text_safety_margin;
|
float required_width = calculateContentWidth() + (config_.padding * 2) + config_.text_safety_margin;
|
||||||
float required_height = calculateContentHeight() + (config_.padding * 2) + config_.text_safety_margin;
|
float required_height = calculateContentHeight() + (config_.padding * 2) + config_.text_safety_margin;
|
||||||
|
|
||||||
// Verificar si el tamaño actual es suficiente
|
// Verificar si el tamaño actual es suficiente
|
||||||
if (rect_.w < required_width || rect_.h < required_height) {
|
if (rect_.w < required_width || rect_.h < required_height) {
|
||||||
autoSize(); // Recalcular tamaño automáticamente
|
autoSize(); // Recalcular tamaño automáticamente
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,7 +234,7 @@ void WindowMessage::calculateAutoSize() {
|
|||||||
// Aplicar límites máximos basados en el tamaño de pantalla
|
// Aplicar límites máximos basados en el tamaño de pantalla
|
||||||
float max_width = getScreenWidth() * config_.max_width_ratio;
|
float max_width = getScreenWidth() * config_.max_width_ratio;
|
||||||
float max_height = getScreenHeight() * config_.max_height_ratio;
|
float max_height = getScreenHeight() * config_.max_height_ratio;
|
||||||
|
|
||||||
rect_.w = std::min(rect_.w, max_width);
|
rect_.w = std::min(rect_.w, max_width);
|
||||||
rect_.h = std::min(rect_.h, max_height);
|
rect_.h = std::min(rect_.h, max_height);
|
||||||
}
|
}
|
||||||
@@ -264,7 +259,7 @@ auto WindowMessage::calculateContentHeight() const -> float {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto WindowMessage::calculateContentWidth() const -> float {
|
auto WindowMessage::calculateContentWidth() const -> float {
|
||||||
float max_width = config_.min_width - (config_.padding * 2); // Ancho mínimo sin padding
|
float max_width = config_.min_width - (config_.padding * 2); // Ancho mínimo sin padding
|
||||||
|
|
||||||
// Ancho del título
|
// Ancho del título
|
||||||
if (!title_.empty()) {
|
if (!title_.empty()) {
|
||||||
@@ -299,7 +294,7 @@ void WindowMessage::updateAnimation(float delta_time) {
|
|||||||
if (show_hide_animation_.active) {
|
if (show_hide_animation_.active) {
|
||||||
updateShowHideAnimation(delta_time);
|
updateShowHideAnimation(delta_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resize_animation_.active) {
|
if (resize_animation_.active) {
|
||||||
updateResizeAnimation(delta_time);
|
updateResizeAnimation(delta_time);
|
||||||
}
|
}
|
||||||
@@ -309,9 +304,9 @@ void WindowMessage::updateShowHideAnimation(float delta_time) {
|
|||||||
if (!show_hide_animation_.active) {
|
if (!show_hide_animation_.active) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
show_hide_animation_.elapsed += delta_time;
|
show_hide_animation_.elapsed += delta_time;
|
||||||
|
|
||||||
if (show_hide_animation_.isFinished(config_.animation_duration)) {
|
if (show_hide_animation_.isFinished(config_.animation_duration)) {
|
||||||
// Animación terminada
|
// Animación terminada
|
||||||
if (show_hide_animation_.type == ShowHideAnimation::Type::SHOWING) {
|
if (show_hide_animation_.type == ShowHideAnimation::Type::SHOWING) {
|
||||||
@@ -324,13 +319,13 @@ void WindowMessage::updateShowHideAnimation(float delta_time) {
|
|||||||
rect_.h = 0.0F;
|
rect_.h = 0.0F;
|
||||||
visible_ = false;
|
visible_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
show_hide_animation_.stop();
|
show_hide_animation_.stop();
|
||||||
updatePosition();
|
updatePosition();
|
||||||
} else {
|
} else {
|
||||||
// Interpolar el tamaño
|
// Interpolar el tamaño
|
||||||
float progress = easeOut(show_hide_animation_.getProgress(config_.animation_duration));
|
float progress = easeOut(show_hide_animation_.getProgress(config_.animation_duration));
|
||||||
|
|
||||||
if (show_hide_animation_.type == ShowHideAnimation::Type::SHOWING) {
|
if (show_hide_animation_.type == ShowHideAnimation::Type::SHOWING) {
|
||||||
// Crecer desde 0 hasta el tamaño objetivo
|
// Crecer desde 0 hasta el tamaño objetivo
|
||||||
rect_.w = show_hide_animation_.target_width * progress;
|
rect_.w = show_hide_animation_.target_width * progress;
|
||||||
@@ -340,8 +335,8 @@ void WindowMessage::updateShowHideAnimation(float delta_time) {
|
|||||||
rect_.w = show_hide_animation_.target_width * (1.0F - progress);
|
rect_.w = show_hide_animation_.target_width * (1.0F - progress);
|
||||||
rect_.h = show_hide_animation_.target_height * (1.0F - progress);
|
rect_.h = show_hide_animation_.target_height * (1.0F - progress);
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePosition(); // Mantener la posición centrada durante la animación
|
updatePosition(); // Mantener la posición centrada durante la animación
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,9 +344,9 @@ void WindowMessage::updateResizeAnimation(float delta_time) {
|
|||||||
if (!resize_animation_.active) {
|
if (!resize_animation_.active) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
resize_animation_.elapsed += delta_time;
|
resize_animation_.elapsed += delta_time;
|
||||||
|
|
||||||
if (resize_animation_.isFinished(config_.animation_duration)) {
|
if (resize_animation_.isFinished(config_.animation_duration)) {
|
||||||
// Animación terminada
|
// Animación terminada
|
||||||
rect_.w = resize_animation_.target_width;
|
rect_.w = resize_animation_.target_width;
|
||||||
@@ -361,13 +356,13 @@ void WindowMessage::updateResizeAnimation(float delta_time) {
|
|||||||
} else {
|
} else {
|
||||||
// Interpolar el tamaño
|
// Interpolar el tamaño
|
||||||
float progress = easeOut(resize_animation_.getProgress(config_.animation_duration));
|
float progress = easeOut(resize_animation_.getProgress(config_.animation_duration));
|
||||||
|
|
||||||
rect_.w = resize_animation_.start_width +
|
rect_.w = resize_animation_.start_width +
|
||||||
(resize_animation_.target_width - resize_animation_.start_width) * progress;
|
(resize_animation_.target_width - resize_animation_.start_width) * progress;
|
||||||
rect_.h = resize_animation_.start_height +
|
rect_.h = resize_animation_.start_height +
|
||||||
(resize_animation_.target_height - resize_animation_.start_height) * progress;
|
(resize_animation_.target_height - resize_animation_.start_height) * progress;
|
||||||
|
|
||||||
updatePosition(); // Mantener la posición centrada durante la animación
|
updatePosition(); // Mantener la posición centrada durante la animación
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,13 +385,13 @@ auto WindowMessage::getTruncatedText(const std::string& text, float available_wi
|
|||||||
if (text.empty()) {
|
if (text.empty()) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si el texto completo cabe, devolverlo tal como está
|
// Si el texto completo cabe, devolverlo tal como está
|
||||||
int text_width = text_renderer_->length(text, -2);
|
int text_width = text_renderer_->length(text, -2);
|
||||||
if (text_width <= available_width) {
|
if (text_width <= available_width) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si no hay espacio suficiente, devolver string vacío
|
// Si no hay espacio suficiente, devolver string vacío
|
||||||
if (available_width < 10.0F) { // Mínimo espacio para al menos un carácter
|
if (available_width < 10.0F) { // Mínimo espacio para al menos un carácter
|
||||||
return "";
|
return "";
|
||||||
@@ -406,12 +401,12 @@ auto WindowMessage::getTruncatedText(const std::string& text, float available_wi
|
|||||||
int left = 0;
|
int left = 0;
|
||||||
int right = text.length();
|
int right = text.length();
|
||||||
int best_length = 0;
|
int best_length = 0;
|
||||||
|
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
int mid = (left + right) / 2;
|
int mid = (left + right) / 2;
|
||||||
std::string partial = text.substr(0, mid);
|
std::string partial = text.substr(0, mid);
|
||||||
int partial_width = text_renderer_->length(partial, -2);
|
int partial_width = text_renderer_->length(partial, -2);
|
||||||
|
|
||||||
if (partial_width <= available_width) {
|
if (partial_width <= available_width) {
|
||||||
best_length = mid;
|
best_length = mid;
|
||||||
left = mid + 1;
|
left = mid + 1;
|
||||||
@@ -419,6 +414,6 @@ auto WindowMessage::getTruncatedText(const std::string& text, float available_wi
|
|||||||
right = mid - 1;
|
right = mid - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return text.substr(0, best_length);
|
return text.substr(0, best_length);
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
|
|
||||||
class WindowMessage {
|
class WindowMessage {
|
||||||
public:
|
public:
|
||||||
@@ -17,34 +17,34 @@ class WindowMessage {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
// Colores
|
// Colores
|
||||||
Color bg_color;
|
Color bg_color;
|
||||||
Color border_color;
|
Color border_color;
|
||||||
Color title_color;
|
Color title_color;
|
||||||
Color text_color;
|
Color text_color;
|
||||||
|
|
||||||
// Espaciado y dimensiones
|
|
||||||
float padding{15.0f};
|
|
||||||
float line_spacing{5.0f};
|
|
||||||
float title_separator_spacing{10.0f}; // Espacio extra para separador del título
|
|
||||||
|
|
||||||
// Límites de tamaño
|
// Espaciado y dimensiones
|
||||||
float min_width{200.0f};
|
float padding{15.0f};
|
||||||
float min_height{100.0f};
|
float line_spacing{5.0f};
|
||||||
float max_width_ratio{0.8f}; // % máximo de ancho de pantalla
|
float title_separator_spacing{10.0f}; // Espacio extra para separador del título
|
||||||
float max_height_ratio{0.8f}; // % máximo de alto de pantalla
|
|
||||||
|
|
||||||
// Margen de seguridad para texto
|
// Límites de tamaño
|
||||||
float text_safety_margin{20.0f}; // Margen extra para evitar texto cortado
|
float min_width{200.0f};
|
||||||
|
float min_height{100.0f};
|
||||||
|
float max_width_ratio{0.8f}; // % máximo de ancho de pantalla
|
||||||
|
float max_height_ratio{0.8f}; // % máximo de alto de pantalla
|
||||||
|
|
||||||
// Animaciones
|
// Margen de seguridad para texto
|
||||||
float animation_duration{0.3f}; // Duración en segundos para todas las animaciones
|
float text_safety_margin{20.0f}; // Margen extra para evitar texto cortado
|
||||||
|
|
||||||
// Constructor con valores por defecto
|
// Animaciones
|
||||||
Config()
|
float animation_duration{0.3f}; // Duración en segundos para todas las animaciones
|
||||||
: bg_color{40, 40, 60, 220}, border_color{100, 100, 120, 255}, title_color{255, 255, 255, 255}, text_color{200, 200, 200, 255}
|
|
||||||
|
|
||||||
{}
|
// Constructor con valores por defecto
|
||||||
|
Config()
|
||||||
|
: bg_color{40, 40, 60, 220}, border_color{100, 100, 120, 255}, title_color{255, 255, 255, 255}, text_color{200, 200, 200, 255}
|
||||||
|
|
||||||
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
WindowMessage(
|
WindowMessage(
|
||||||
@@ -69,7 +69,7 @@ class WindowMessage {
|
|||||||
void setTexts(const std::vector<std::string>& texts);
|
void setTexts(const std::vector<std::string>& texts);
|
||||||
void addText(const std::string& text);
|
void addText(const std::string& text);
|
||||||
void clearTexts();
|
void clearTexts();
|
||||||
|
|
||||||
// Control de redimensionado automático
|
// Control de redimensionado automático
|
||||||
void enableAutoResize(bool enabled) { auto_resize_enabled_ = enabled; }
|
void enableAutoResize(bool enabled) { auto_resize_enabled_ = enabled; }
|
||||||
[[nodiscard]] auto isAutoResizeEnabled() const -> bool { return auto_resize_enabled_; }
|
[[nodiscard]] auto isAutoResizeEnabled() const -> bool { return auto_resize_enabled_; }
|
||||||
@@ -78,20 +78,29 @@ class WindowMessage {
|
|||||||
void setPosition(float x, float y, PositionMode mode = PositionMode::CENTERED);
|
void setPosition(float x, float y, PositionMode mode = PositionMode::CENTERED);
|
||||||
void setSize(float width, float height);
|
void setSize(float width, float height);
|
||||||
void centerOnScreen();
|
void centerOnScreen();
|
||||||
void autoSize(); // Ajusta automáticamente al contenido y reposiciona si es necesario
|
void autoSize(); // Ajusta automáticamente al contenido y reposiciona si es necesario
|
||||||
|
|
||||||
// Configuración de colores
|
// Configuración de colores
|
||||||
void setBackgroundColor(const Color& color) { config_.bg_color = color; }
|
void setBackgroundColor(const Color& color) { config_.bg_color = color; }
|
||||||
void setBorderColor(const Color& color) { config_.border_color = color; }
|
void setBorderColor(const Color& color) { config_.border_color = color; }
|
||||||
void setTitleColor(const Color& color) { config_.title_color = color; updateStyles(); }
|
void setTitleColor(const Color& color) {
|
||||||
void setTextColor(const Color& color) { config_.text_color = color; updateStyles(); }
|
config_.title_color = color;
|
||||||
|
updateStyles();
|
||||||
|
}
|
||||||
|
void setTextColor(const Color& color) {
|
||||||
|
config_.text_color = color;
|
||||||
|
updateStyles();
|
||||||
|
}
|
||||||
|
|
||||||
// Configuración de espaciado
|
// Configuración de espaciado
|
||||||
void setPadding(float padding) { config_.padding = padding; }
|
void setPadding(float padding) { config_.padding = padding; }
|
||||||
void setLineSpacing(float spacing) { config_.line_spacing = spacing; }
|
void setLineSpacing(float spacing) { config_.line_spacing = spacing; }
|
||||||
|
|
||||||
// Configuración avanzada
|
// Configuración avanzada
|
||||||
void setConfig(const Config& config) { config_ = config; updateStyles(); }
|
void setConfig(const Config& config) {
|
||||||
|
config_ = config;
|
||||||
|
updateStyles();
|
||||||
|
}
|
||||||
[[nodiscard]] auto getConfig() const -> const Config& { return config_; }
|
[[nodiscard]] auto getConfig() const -> const Config& { return config_; }
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
@@ -102,15 +111,15 @@ class WindowMessage {
|
|||||||
private:
|
private:
|
||||||
std::shared_ptr<Text> text_renderer_;
|
std::shared_ptr<Text> text_renderer_;
|
||||||
Config config_;
|
Config config_;
|
||||||
|
|
||||||
// Estado de visibilidad y redimensionado
|
// Estado de visibilidad y redimensionado
|
||||||
bool visible_ = false;
|
bool visible_ = false;
|
||||||
bool auto_resize_enabled_ = true; // Por defecto habilitado
|
bool auto_resize_enabled_ = true; // Por defecto habilitado
|
||||||
|
|
||||||
// Contenido
|
// Contenido
|
||||||
std::string title_;
|
std::string title_;
|
||||||
std::vector<std::string> texts_;
|
std::vector<std::string> texts_;
|
||||||
|
|
||||||
// Posición y tamaño
|
// Posición y tamaño
|
||||||
SDL_FRect rect_{0, 0, 300, 200};
|
SDL_FRect rect_{0, 0, 300, 200};
|
||||||
PositionMode position_mode_ = PositionMode::CENTERED;
|
PositionMode position_mode_ = PositionMode::CENTERED;
|
||||||
@@ -118,94 +127,96 @@ class WindowMessage {
|
|||||||
|
|
||||||
// Animación de redimensionado
|
// Animación de redimensionado
|
||||||
struct ResizeAnimation {
|
struct ResizeAnimation {
|
||||||
bool active = false;
|
bool active = false;
|
||||||
float start_width, start_height;
|
float start_width, start_height;
|
||||||
float target_width, target_height;
|
float target_width, target_height;
|
||||||
float elapsed = 0.0F;
|
float elapsed = 0.0F;
|
||||||
|
|
||||||
void start(float from_w, float from_h, float to_w, float to_h) {
|
void start(float from_w, float from_h, float to_w, float to_h) {
|
||||||
start_width = from_w;
|
start_width = from_w;
|
||||||
start_height = from_h;
|
start_height = from_h;
|
||||||
target_width = to_w;
|
target_width = to_w;
|
||||||
target_height = to_h;
|
target_height = to_h;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
active = true;
|
active = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() {
|
void stop() {
|
||||||
active = false;
|
active = false;
|
||||||
elapsed = 0.0F;
|
elapsed = 0.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] auto isFinished(float duration) const -> bool {
|
[[nodiscard]] auto isFinished(float duration) const -> bool {
|
||||||
return elapsed >= duration;
|
return elapsed >= duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] auto getProgress(float duration) const -> float {
|
[[nodiscard]] auto getProgress(float duration) const -> float {
|
||||||
return std::min(elapsed / duration, 1.0F);
|
return std::min(elapsed / duration, 1.0F);
|
||||||
}
|
}
|
||||||
} resize_animation_;
|
} resize_animation_;
|
||||||
|
|
||||||
// Animación de mostrar/ocultar
|
// Animación de mostrar/ocultar
|
||||||
struct ShowHideAnimation {
|
struct ShowHideAnimation {
|
||||||
enum class Type { NONE, SHOWING, HIDING };
|
enum class Type { NONE,
|
||||||
|
SHOWING,
|
||||||
Type type = Type::NONE;
|
HIDING };
|
||||||
bool active = false;
|
|
||||||
float target_width, target_height; // Tamaño final al mostrar
|
|
||||||
float elapsed = 0.0F;
|
|
||||||
|
|
||||||
void startShow(float to_w, float to_h) {
|
Type type = Type::NONE;
|
||||||
type = Type::SHOWING;
|
bool active = false;
|
||||||
target_width = to_w;
|
float target_width, target_height; // Tamaño final al mostrar
|
||||||
target_height = to_h;
|
float elapsed = 0.0F;
|
||||||
elapsed = 0.0F;
|
|
||||||
active = true;
|
void startShow(float to_w, float to_h) {
|
||||||
}
|
type = Type::SHOWING;
|
||||||
|
target_width = to_w;
|
||||||
void startHide() {
|
target_height = to_h;
|
||||||
type = Type::HIDING;
|
elapsed = 0.0F;
|
||||||
elapsed = 0.0F;
|
active = true;
|
||||||
active = true;
|
}
|
||||||
}
|
|
||||||
|
void startHide() {
|
||||||
void stop() {
|
type = Type::HIDING;
|
||||||
type = Type::NONE;
|
elapsed = 0.0F;
|
||||||
active = false;
|
active = true;
|
||||||
elapsed = 0.0F;
|
}
|
||||||
}
|
|
||||||
|
void stop() {
|
||||||
[[nodiscard]] auto isFinished(float duration) const -> bool {
|
type = Type::NONE;
|
||||||
return elapsed >= duration;
|
active = false;
|
||||||
}
|
elapsed = 0.0F;
|
||||||
|
}
|
||||||
[[nodiscard]] auto getProgress(float duration) const -> float {
|
|
||||||
return std::min(elapsed / duration, 1.0F);
|
[[nodiscard]] auto isFinished(float duration) const -> bool {
|
||||||
}
|
return elapsed >= duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] auto getProgress(float duration) const -> float {
|
||||||
|
return std::min(elapsed / duration, 1.0F);
|
||||||
|
}
|
||||||
} show_hide_animation_;
|
} show_hide_animation_;
|
||||||
|
|
||||||
// Estilos
|
// Estilos
|
||||||
Text::Style title_style_;
|
Text::Style title_style_;
|
||||||
Text::Style text_style_;
|
Text::Style text_style_;
|
||||||
|
|
||||||
// Métodos privados
|
// Métodos privados
|
||||||
void calculateAutoSize();
|
void calculateAutoSize();
|
||||||
void updatePosition(); // Actualiza la posición según el modo y punto de anclaje
|
void updatePosition(); // Actualiza la posición según el modo y punto de anclaje
|
||||||
void updateStyles(); // Actualiza los estilos de texto cuando cambian los colores
|
void updateStyles(); // Actualiza los estilos de texto cuando cambian los colores
|
||||||
void ensureTextFits(); // Verifica y ajusta para que todo el texto sea visible
|
void ensureTextFits(); // Verifica y ajusta para que todo el texto sea visible
|
||||||
void triggerAutoResize(); // Inicia redimensionado automático si está habilitado
|
void triggerAutoResize(); // Inicia redimensionado automático si está habilitado
|
||||||
void updateAnimation(float delta_time); // Actualiza la animación de redimensionado
|
void updateAnimation(float delta_time); // Actualiza la animación de redimensionado
|
||||||
void updateShowHideAnimation(float delta_time); // Actualiza la animación de mostrar/ocultar
|
void updateShowHideAnimation(float delta_time); // Actualiza la animación de mostrar/ocultar
|
||||||
void updateResizeAnimation(float delta_time); // Actualiza la animación de redimensionado
|
void updateResizeAnimation(float delta_time); // Actualiza la animación de redimensionado
|
||||||
|
|
||||||
// Función de suavizado (ease-out)
|
// Función de suavizado (ease-out)
|
||||||
[[nodiscard]] static auto easeOut(float t) -> float;
|
[[nodiscard]] static auto easeOut(float t) -> float;
|
||||||
|
|
||||||
// Métodos para manejo de texto durante animación
|
// Métodos para manejo de texto durante animación
|
||||||
[[nodiscard]] auto getTruncatedText(const std::string& text, float available_width) const -> std::string;
|
[[nodiscard]] auto getTruncatedText(const std::string& text, float available_width) const -> std::string;
|
||||||
[[nodiscard]] auto getAvailableTextWidth() const -> float;
|
[[nodiscard]] auto getAvailableTextWidth() const -> float;
|
||||||
[[nodiscard]] auto shouldShowContent() const -> bool; // Si mostrar el contenido (texto, líneas, etc.)
|
[[nodiscard]] auto shouldShowContent() const -> bool; // Si mostrar el contenido (texto, líneas, etc.)
|
||||||
|
|
||||||
[[nodiscard]] auto calculateContentHeight() const -> float;
|
[[nodiscard]] auto calculateContentHeight() const -> float;
|
||||||
[[nodiscard]] auto calculateContentWidth() const -> float;
|
[[nodiscard]] auto calculateContentWidth() const -> float;
|
||||||
[[nodiscard]] static auto getScreenWidth() -> float;
|
[[nodiscard]] static auto getScreenWidth() -> float;
|
||||||
|
|||||||
Reference in New Issue
Block a user