implement save and load blueprints

This commit is contained in:
2026-04-28 21:31:29 +02:00
parent 550f46009f
commit ced4ab5fe3
5 changed files with 240 additions and 0 deletions

View File

@@ -3,11 +3,17 @@
#include <algorithm>
#include <climits>
#include <QCoreApplication>
#include <QFile>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include "BlueprintSerializer.h"
#include "Building.h"
#include "BuildingSystem.h"
#include "Simulation.h"
@@ -49,6 +55,18 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
connect(m_createBtn, &QPushButton::clicked, this, &BlueprintPanel::onCreateClicked);
connect(m_deleteBtn, &QPushButton::clicked, this, &BlueprintPanel::onDeleteClicked);
QHBoxLayout* ioLayout = new QHBoxLayout();
m_saveBtn = new QPushButton("Save", this);
m_loadBtn = new QPushButton("Load", this);
m_saveBtn->setFixedHeight(36);
m_loadBtn->setFixedHeight(36);
ioLayout->addWidget(m_saveBtn);
ioLayout->addWidget(m_loadBtn);
layout->addLayout(ioLayout);
connect(m_saveBtn, &QPushButton::clicked, this, &BlueprintPanel::onSaveClicked);
connect(m_loadBtn, &QPushButton::clicked, this, &BlueprintPanel::onLoadClicked);
}
void BlueprintPanel::onSelectionChanged(const std::vector<EntityId>& ids)
@@ -242,6 +260,72 @@ void BlueprintPanel::rebuildButtons()
refreshButtonStates();
}
void BlueprintPanel::onSaveClicked()
{
const QString path = QCoreApplication::applicationDirPath() + "/blueprints.toml";
try
{
const std::string content = BlueprintSerializer::serialize(m_blueprints);
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::critical(this, "Save Failed",
QString("Could not open file for writing:\n%1").arg(path));
return;
}
file.write(QByteArray::fromStdString(content));
}
catch (const std::exception& e)
{
QMessageBox::critical(this, "Save Failed",
QString("Failed to save blueprints:\n%1").arg(e.what()));
}
}
void BlueprintPanel::onLoadClicked()
{
QMessageBox box(this);
box.setWindowTitle("Load Blueprints");
box.setText("Load blueprints? This will replace all current blueprints.");
QPushButton* confirmBtn = box.addButton("Confirm", QMessageBox::AcceptRole);
box.addButton("Cancel", QMessageBox::RejectRole);
box.exec();
if (box.clickedButton() != confirmBtn) { return; }
const QString path = QCoreApplication::applicationDirPath() + "/blueprints.toml";
try
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this, "Load Failed",
QString("Could not open file:\n%1").arg(path));
return;
}
const std::string content = file.readAll().toStdString();
std::vector<Blueprint> loaded = BlueprintSerializer::deserialize(content);
if (m_activeIndex >= 0)
{
emit exitBlueprintModeRequested();
m_activeIndex = -1;
}
if (m_deleteMode)
{
m_deleteMode = false;
m_deleteBtn->setChecked(false);
}
m_blueprints = std::move(loaded);
rebuildButtons();
}
catch (const std::exception& e)
{
QMessageBox::critical(this, "Load Failed",
QString("Failed to load blueprints:\n%1").arg(e.what()));
}
}
void BlueprintPanel::refreshButtonStates()
{
const bool anyPlaceable = [&]() {

View File

@@ -34,6 +34,8 @@ private slots:
void onCreateClicked();
void onDeleteClicked();
void onBlueprintButtonClicked(int index);
void onSaveClicked();
void onLoadClicked();
private:
Blueprint createBlueprintFromSelection() const;
@@ -51,6 +53,8 @@ private:
std::vector<QPushButton*> m_blueprintButtons;
QPushButton* m_createBtn;
QPushButton* m_deleteBtn;
QPushButton* m_saveBtn;
QPushButton* m_loadBtn;
QWidget* m_buttonsContainer;
QVBoxLayout* m_buttonsLayout;
};