- nou format per a assets.yaml
- ResourceList gestiona addAsset i removeAsset
This commit is contained in:
@@ -1266,74 +1266,6 @@ auto MapEditor::setRoomProperty(const std::string& property, const std::string&
|
||||
return "Unknown property: " + property + " (use: bgcolor, border, itemcolor1, itemcolor2, conveyor, tileset, up, down, left, right)";
|
||||
}
|
||||
|
||||
// Obtiene la ruta de assets.yaml derivada de la ruta de una room
|
||||
auto MapEditor::getAssetsYamlPath() -> std::string {
|
||||
std::string ref_path = Resource::List::get()->get(room_path_);
|
||||
if (ref_path.empty()) { return ""; }
|
||||
// ref_path es algo como .../data/room/03.yaml → queremos .../config/assets.yaml
|
||||
auto pos = ref_path.find("/data/room/");
|
||||
if (pos == std::string::npos) { return ""; }
|
||||
return ref_path.substr(0, pos) + "/config/assets.yaml";
|
||||
}
|
||||
|
||||
// Añade una room a assets.yaml (bajo la sección rooms:)
|
||||
void MapEditor::addRoomToAssetsYaml(const std::string& room_name) {
|
||||
std::string assets_path = getAssetsYamlPath();
|
||||
if (assets_path.empty()) { return; }
|
||||
|
||||
// Leer el fichero
|
||||
std::ifstream in(assets_path);
|
||||
if (!in.is_open()) { return; }
|
||||
std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
in.close();
|
||||
|
||||
// Buscar la última entrada de room y añadir después
|
||||
std::string entry = " - type: ROOM\n path: ${PREFIX}/data/room/" + room_name + "\n";
|
||||
auto last_room = content.rfind("path: ${PREFIX}/data/room/");
|
||||
if (last_room != std::string::npos) {
|
||||
auto end_of_line = content.find('\n', last_room);
|
||||
if (end_of_line != std::string::npos) {
|
||||
content.insert(end_of_line + 1, entry);
|
||||
}
|
||||
}
|
||||
|
||||
std::ofstream out(assets_path);
|
||||
if (out.is_open()) {
|
||||
out << content;
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Quita una room de assets.yaml
|
||||
void MapEditor::removeRoomFromAssetsYaml(const std::string& room_name) {
|
||||
std::string assets_path = getAssetsYamlPath();
|
||||
if (assets_path.empty()) { return; }
|
||||
|
||||
std::ifstream in(assets_path);
|
||||
if (!in.is_open()) { return; }
|
||||
std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
in.close();
|
||||
|
||||
// Buscar "path: ${PREFIX}/data/room/XX.yaml" y eliminar esa entry (2 líneas: type + path)
|
||||
std::string search = "path: ${PREFIX}/data/room/" + room_name;
|
||||
auto pos = content.find(search);
|
||||
if (pos != std::string::npos) {
|
||||
// Retroceder hasta " - type: ROOM\n"
|
||||
auto line_start = content.rfind(" - type: ROOM", pos);
|
||||
// Avanzar hasta el fin de la línea del path
|
||||
auto line_end = content.find('\n', pos);
|
||||
if (line_start != std::string::npos && line_end != std::string::npos) {
|
||||
content.erase(line_start, line_end - line_start + 1);
|
||||
}
|
||||
}
|
||||
|
||||
std::ofstream out(assets_path);
|
||||
if (out.is_open()) {
|
||||
out << content;
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Crea una nueva habitación
|
||||
auto MapEditor::createNewRoom(const std::string& direction) -> std::string {
|
||||
if (!active_) { return "Editor not active"; }
|
||||
@@ -1444,11 +1376,10 @@ auto MapEditor::createNewRoom(const std::string& direction) -> std::string {
|
||||
}
|
||||
file.close();
|
||||
|
||||
// Registrar en Resource::List, cache y assets.yaml
|
||||
Resource::List::get()->add(new_path, Resource::List::Type::ROOM, true, true);
|
||||
// Registrar en Resource::List (mapa + assets.yaml) y cache
|
||||
Resource::List::get()->addAsset(new_path, Resource::List::Type::ROOM);
|
||||
Resource::Cache::get()->getRooms().emplace_back(
|
||||
RoomResource{.name = new_name, .room = std::make_shared<Room::Data>(new_room)});
|
||||
addRoomToAssetsYaml(new_name);
|
||||
|
||||
// Conectar la room actual con la nueva (recíproco: ya hecho arriba para la nueva)
|
||||
if (direction == "UP") {
|
||||
@@ -1546,8 +1477,8 @@ auto MapEditor::deleteRoom() -> std::string {
|
||||
std::remove_if(cache_rooms.begin(), cache_rooms.end(), [&](const RoomResource& r) { return r.name == deleted_name; }),
|
||||
cache_rooms.end());
|
||||
|
||||
// Quitar de assets.yaml
|
||||
removeRoomFromAssetsYaml(deleted_name);
|
||||
// Quitar de Resource::List (mapa + assets.yaml)
|
||||
Resource::List::get()->removeAsset(deleted_name);
|
||||
|
||||
// Re-entrar al editor en la room destino
|
||||
if (GameControl::enter_editor) { GameControl::enter_editor(); }
|
||||
|
||||
@@ -109,9 +109,6 @@ class MapEditor {
|
||||
void handleMouseUp();
|
||||
void updateDrag();
|
||||
void autosave();
|
||||
auto getAssetsYamlPath() -> std::string;
|
||||
void addRoomToAssetsYaml(const std::string& room_name);
|
||||
void removeRoomFromAssetsYaml(const std::string& room_name);
|
||||
void updateStatusBarInfo();
|
||||
static auto snapToGrid(float value) -> float;
|
||||
static auto pointInRect(float px, float py, const SDL_FRect& rect) -> bool;
|
||||
|
||||
Reference in New Issue
Block a user