ship layout blueprints

This commit is contained in:
2026-05-19 21:01:10 +02:00
parent d08bf5d37b
commit d397b9969a
14 changed files with 511 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
#include "MainWindow.h"
#include <QApplication>
#include <QCloseEvent>
#include <QFile>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QPushButton>
@@ -14,6 +16,7 @@
#include "GameWorldView.h"
#include "HeaderBar.h"
#include "SelectedBuildingPanel.h"
#include "ShipLayoutBlueprintSerializer.h"
#include "ShipLayoutDialog.h"
#include "Simulation.h"
#include "Tick.h"
@@ -111,6 +114,24 @@ MainWindow::MainWindow(Simulation* sim, const std::string& configDir, QWidget* p
m_gameWorldView->setFocus();
}
});
// Load layout blueprints from disk. Missing file is silently ignored.
const QString bpPath = QCoreApplication::applicationDirPath() + "/ship_layouts.toml";
QFile bpFile(bpPath);
if (bpFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
try
{
m_layoutBlueprints = ShipLayoutBlueprintSerializer::deserialize(
bpFile.readAll().toStdString());
}
catch (const std::exception& e)
{
QMessageBox::critical(this, "Load Error",
QString("Failed to load ship_layouts.toml:\n%1").arg(e.what()));
m_layoutBlueprints.clear();
}
}
}
void MainWindow::resizeEvent(QResizeEvent* event)
@@ -119,6 +140,23 @@ void MainWindow::resizeEvent(QResizeEvent* event)
layoutPanels();
}
void MainWindow::closeEvent(QCloseEvent* event)
{
const QString path = QCoreApplication::applicationDirPath() + "/ship_layouts.toml";
QFile file(path);
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
try
{
const std::string content =
ShipLayoutBlueprintSerializer::serialize(m_layoutBlueprints);
file.write(QByteArray::fromStdString(content));
}
catch (...) {}
}
QWidget::closeEvent(event);
}
void MainWindow::layoutPanels()
{
const int totalW = width();
@@ -199,7 +237,8 @@ void MainWindow::onLayoutDialogRequested(EntityId shipyardId)
currentLayout = *b->shipLayout;
}
ShipLayoutDialog dialog(&m_sim->config(), b->recipeId, currentLayout, this);
ShipLayoutDialog dialog(&m_sim->config(), b->recipeId, currentLayout,
m_layoutBlueprints, this);
if (dialog.exec() == QDialog::Accepted && dialog.result().has_value())
{
m_sim->buildings().setShipLayout(shipyardId, *dialog.result());