- afegides opcions persistents al editor

- afegida rejilla
This commit is contained in:
2026-04-02 17:38:03 +02:00
parent 44b6f6830d
commit 3c3e012386
7 changed files with 144 additions and 5 deletions

View File

@@ -5,9 +5,11 @@
#include <SDL3/SDL.h>
#include <cmath> // Para std::round
#include <fstream> // Para ifstream, ofstream
#include <iostream> // Para cout
#include "core/input/mouse.hpp" // Para Mouse
#include "core/rendering/render_info.hpp" // Para RenderInfo
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface.hpp" // Para Surface
#include "core/resources/resource_cache.hpp" // Para Resource::Cache
@@ -40,11 +42,64 @@ auto MapEditor::get() -> MapEditor* {
}
// Constructor
MapEditor::MapEditor() = default;
MapEditor::MapEditor() {
loadSettings();
}
// Destructor
MapEditor::~MapEditor() = default;
// Carga las opciones del editor desde editor.yaml
void MapEditor::loadSettings() {
std::string path = Resource::List::get()->get("editor.yaml");
if (path.empty()) { return; }
std::ifstream file(path);
if (!file.is_open()) { return; }
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
try {
auto yaml = fkyaml::node::deserialize(content);
if (yaml.contains("grid")) { settings_.grid = yaml["grid"].get_value<bool>(); }
if (yaml.contains("show_render_info")) { settings_.show_render_info = yaml["show_render_info"].get_value<bool>(); }
} catch (...) {
// Fichero corrupto o vacío, usar defaults
}
}
// Guarda las opciones del editor a editor.yaml
void MapEditor::saveSettings() {
std::string path = Resource::List::get()->get("editor.yaml");
if (path.empty()) { return; }
std::ofstream file(path);
if (!file.is_open()) { return; }
file << "# Map Editor Settings\n";
file << "grid: " << (settings_.grid ? "true" : "false") << "\n";
file << "show_render_info: " << (settings_.show_render_info ? "true" : "false") << "\n";
file.close();
}
// Muestra/oculta render info (persistente)
auto MapEditor::showInfo(bool show) -> std::string {
settings_.show_render_info = show;
if (RenderInfo::get()->isActive() != show) {
RenderInfo::get()->toggle();
}
saveSettings();
return show ? "Info ON" : "Info OFF";
}
// Muestra/oculta grid (persistente)
auto MapEditor::showGrid(bool show) -> std::string {
settings_.grid = show;
saveSettings();
return show ? "Grid ON" : "Grid OFF";
}
// Entra en modo editor
void MapEditor::enter(std::shared_ptr<Room> room, std::shared_ptr<Player> player, const std::string& room_path, std::shared_ptr<Scoreboard::Data> scoreboard_data) {
if (active_) { return; }
@@ -72,6 +127,12 @@ void MapEditor::enter(std::shared_ptr<Room> room, std::shared_ptr<Player> player
Options::cheats.invincible = Options::Cheat::State::ENABLED;
player_->setColor();
// Guardar estado de render_info y aplicar el setting del editor
render_info_before_editor_ = RenderInfo::get()->isActive();
if (settings_.show_render_info != render_info_before_editor_) {
RenderInfo::get()->toggle();
}
// Resetear enemigos a su posición inicial (pueden haberse movido durante el gameplay)
room_->resetEnemyPositions(room_data_.enemies);
@@ -99,6 +160,11 @@ void MapEditor::exit() {
Options::cheats.invincible = invincible_before_editor_;
player_->setColor();
// Restaurar render_info
if (RenderInfo::get()->isActive() != render_info_before_editor_) {
RenderInfo::get()->toggle();
}
// Restaurar prompt de la consola y limpiar estado
selected_enemy_ = -1;
Console::get()->setPrompt("> ");
@@ -203,6 +269,11 @@ void MapEditor::update(float delta_time) {
void MapEditor::render() {
// El tilemap ya ha sido renderizado por Game::renderPlaying() antes de llamar aquí
// Grid (debajo de todo)
if (settings_.grid) {
renderGrid();
}
// Renderizar los marcadores de boundaries y líneas de ruta (debajo de los sprites)
renderEnemyBoundaries();
@@ -1136,4 +1207,40 @@ auto MapEditor::duplicateItem() -> std::string {
return "Duplicated as item " + std::to_string(selected_item_);
}
// Elige un color de grid que contraste con el fondo
// Empieza con bright_black (1), si coincide con el bg en la paleta activa, sube índices
static auto pickGridColor(Uint8 bg, const std::shared_ptr<Surface>& surface) -> Uint8 {
Uint8 grid = static_cast<Uint8>(PaletteColor::BRIGHT_BLACK);
Uint32 bg_argb = surface->getPaletteColor(bg);
// Si bright_black es igual al bg, buscar el siguiente color distinto
while (grid < 15 && surface->getPaletteColor(grid) == bg_argb) {
grid += 2; // Saltar de 2 en 2 para mantenerse en tonos discretos
}
return grid;
}
// Dibuja la cuadrícula de tiles (líneas de puntos, 1 pixel sí / 1 no)
void MapEditor::renderGrid() {
auto game_surface = Screen::get()->getRendererSurface();
if (!game_surface) { return; }
const Uint8 COLOR = pickGridColor(stringToColor(room_data_.bg_color), game_surface);
// Líneas verticales (cada 8 pixels)
for (int x = Tile::SIZE; x < PlayArea::WIDTH; x += Tile::SIZE) {
for (int y = 0; y < PlayArea::HEIGHT; y += 2) {
game_surface->putPixel(x, y, COLOR);
}
}
// Líneas horizontales (cada 8 pixels)
for (int y = Tile::SIZE; y < PlayArea::HEIGHT; y += Tile::SIZE) {
for (int x = 0; x < PlayArea::WIDTH; x += 2) {
game_surface->putPixel(x, y, COLOR);
}
}
}
#endif // _DEBUG