Select Git revision
-
Julian Dreuth authoredJulian Dreuth authored
MainWindow.cpp 6.61 KiB
//
// Created by Leon Lux on 19.01.25.
//
#include "MainWindow.hpp"
namespace editor
{
MainWindow::MainWindow(DialogSelection selected, const std::filesystem::path &path, QWidget *parent, int roomID)
: QMainWindow(parent)
{
resize(800, 600);
setMinimumSize(300, 200);
setWindowTitle("Editor");
std::string text = "";
QWidget *centralWidget = new QWidget(this);
QHBoxLayout *mainLayout = new QHBoxLayout;
if (QFile::exists("./level.h5"))
{
std::filesystem::path path = "./level.h5";
HDF5Handler handler = HDF5Handler(path);
m_ID = handler.getMaxID() + 1;
}
else
{
m_ID = 2;
}
m_loadID = roomID;
// Create the menu bar
QMenuBar *menuBar = new QMenuBar(this);
QMenu *fileMenu = menuBar->addMenu("File");
QAction *saveRoomAction = new QAction("Save Room", this);
connect(saveRoomAction, &QAction::triggered, this, &MainWindow::saveLevel);
fileMenu->addAction(saveRoomAction);
QAction *quitAction = new QAction("Quit", this);
connect(quitAction, &QAction::triggered, this, &MainWindow::close);
fileMenu->addAction(quitAction);
setMenuBar(menuBar);
// Left section: Scrollable list
TileBar *leftWidget = new TileBar(0, this);
mainLayout->addWidget(leftWidget);
m_leftWidget = leftWidget;
connect(leftWidget, &editor::TileBar::itemClicked, this, &MainWindow::handleTileBarClick);
const HDF5Handler handler;
handler.loadTilesheet(m_leftWidget);
// Center grid
CenterGrid *middleWidget = new CenterGrid(this);
mainLayout->addWidget(middleWidget);
m_centerGrid = middleWidget;
// Right section: Scrollable list
TileBar *rightWidget = new TileBar(1, this);
mainLayout->addWidget(rightWidget);
connect(rightWidget, &editor::TileBar::itemClicked, this, &MainWindow::handleTileBarClick);
// temporary example, replace with list of room layouts
QStringList itemsRight;
QList<QPixmap> pixmapsRight;
itemsRight << QString("1x1 room");
pixmapsRight << QPixmap("./editor/assets/1x1.png");
itemsRight << QString("1x2 room");
pixmapsRight << QPixmap("./editor/assets/1x2.png");
itemsRight << QString("2x1 room");
pixmapsRight << QPixmap("./editor/assets/2x1.png");
itemsRight << QString("2x2 room");
pixmapsRight << QPixmap("./editor/assets/2x2.png");
itemsRight << QString("cross");
pixmapsRight << QPixmap("./editor/assets/cross.png");
itemsRight << QString("fully custom");
pixmapsRight << QPixmap("./editor/assets/custom.png");
rightWidget->populateList(itemsRight, pixmapsRight);
// Set layout to the central widget
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
// initialize middleWidget as fully custom board
middleWidget->drawPreset(5);
// Load selected level
if (roomID != -1)
{
lvl::Level level("./level.h5");
std::vector<lvl::Room> &roomsList = level.getRooms();
auto it = std::find_if(roomsList.begin(), roomsList.end(),
[roomID](const lvl::Room &room) { return room.getId() == roomID; });
lvl::Room &foundRoom = *it;
int width = foundRoom.getWidth();
for (auto it = foundRoom.tilesBegin(); it != foundRoom.tilesEnd(); it++)
{
int32_t tileID = *it;
if (tileID != -1)
{
std::size_t index = std::distance(foundRoom.tilesBegin(), it);
int y = index % width;
int x = index / width;
m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID);
m_centerGrid->onTileClicked(x, y);
}
}
for (auto it = foundRoom.objectsBegin(); it != foundRoom.objectsEnd(); it++)
{
lvl::Placement gameObject = *it;
int y = gameObject.position.x;
int x = gameObject.position.y;
int tileID = gameObject.id;
m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(tileID), tileID);
m_centerGrid->onTileClicked(x, y);
}
for (auto it = foundRoom.enemiesBegin(); it != foundRoom.enemiesEnd(); it++)
{
lvl::Placement gameObject = *it;
int y = gameObject.position.x;
int x = gameObject.position.y;
int tileID = gameObject.id;
switch (tileID)
{
case 0:
m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(22), 22);
break;
case 1:
m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(86), 86);
break;
case 2:
m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(86), 146);
break;
}
m_centerGrid->onTileClicked(x,y);
}
}
}
void MainWindow::handleTileBarClick(int index, int id)
{
if (id == 0)
{
m_centerGrid->setCurrentSprite(m_leftWidget->getPixmapByIndex(index), index);
}
else
{
QMessageBox::StandardButton confirmationPopup;
confirmationPopup = QMessageBox::question(this, "Confirmation",
"Changing the preset will delete all placed sprites/tiles! Proceed?",
QMessageBox::Yes | QMessageBox::No);
if (confirmationPopup == QMessageBox::Yes)
{
m_centerGrid->drawPreset(index);
}
}
}
void MainWindow::saveLevel()
{
QVector<QVector<int>> tileIDs = m_centerGrid->getTileIDs();
QVector<QVector<int>> mobIDs = m_centerGrid->getMobIDs();
QVector<QVector<int>> objectIDs = m_centerGrid->getObjectIDs();
if (m_loadID != -1)
{
std::filesystem::path path = "./level.h5";
HDF5Handler deleteHandler = HDF5Handler(path);
QString name = deleteHandler.removeRoom(m_loadID);
HDF5Handler saveHandler = HDF5Handler(path);
saveHandler.saveRoom(m_loadID, name, tileIDs, mobIDs, objectIDs);
m_loadID = -1;
}
else
{
QString name = QInputDialog::getText(nullptr, "Enter Level Name", "Name:");
if (name.isEmpty())
{
name = "defaultName";
}
if (QFile::exists("./level.h5"))
{
std::filesystem::path path = "./level.h5";
HDF5Handler handler = HDF5Handler(path);
handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs);
}
else
{
HDF5Handler handler = HDF5Handler();
handler.saveRoom(m_ID, name, tileIDs, mobIDs, objectIDs);
}
m_ID++;
}
}
} // namespace editor