2014-02-10 13:41:05 +01:00
|
|
|
#include "ViewerWindow.hpp"
|
2016-09-09 22:13:21 +02:00
|
|
|
#include <ViewerWidget.hpp>
|
2015-04-13 02:48:29 +02:00
|
|
|
#include "views/ModelViewer.hpp"
|
2016-09-09 22:13:21 +02:00
|
|
|
#include "views/ObjectViewer.hpp"
|
2016-05-15 21:25:20 +02:00
|
|
|
#include "views/WorldViewer.hpp"
|
2017-09-07 19:03:18 +02:00
|
|
|
#include "views/TextViewer.hpp"
|
2014-09-19 01:10:05 +02:00
|
|
|
|
2016-05-16 01:06:51 +02:00
|
|
|
#include <engine/GameState.hpp>
|
2014-02-11 06:46:29 +01:00
|
|
|
#include <engine/GameWorld.hpp>
|
2015-04-13 02:48:29 +02:00
|
|
|
#include <render/GameRenderer.hpp>
|
2016-05-15 21:25:20 +02:00
|
|
|
|
2014-02-10 17:15:22 +01:00
|
|
|
#include <QApplication>
|
2016-09-09 22:13:21 +02:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMenuBar>
|
2016-05-15 21:25:20 +02:00
|
|
|
#include <QPushButton>
|
2016-09-09 22:13:21 +02:00
|
|
|
#include <QSettings>
|
2018-01-16 02:05:23 +01:00
|
|
|
#include <QWindow>
|
|
|
|
#include <QMessageBox>
|
2014-02-10 13:41:05 +01:00
|
|
|
|
2014-06-08 02:58:49 +02:00
|
|
|
static int MaxRecentGames = 5;
|
2014-02-11 15:41:46 +01:00
|
|
|
|
2014-09-19 01:10:05 +02:00
|
|
|
ViewerWindow::ViewerWindow(QWidget* parent, Qt::WindowFlags flags)
|
2016-09-09 22:13:21 +02:00
|
|
|
: QMainWindow(parent, flags)
|
2018-08-10 02:40:23 +02:00
|
|
|
, m_context(this)
|
2016-09-09 22:13:21 +02:00
|
|
|
, gameData(nullptr)
|
|
|
|
, gameWorld(nullptr)
|
|
|
|
, renderer(nullptr) {
|
2018-01-16 02:05:23 +01:00
|
|
|
show();
|
2016-09-09 22:13:21 +02:00
|
|
|
setMinimumSize(640, 480);
|
2018-01-16 02:05:23 +01:00
|
|
|
createMenus();
|
|
|
|
if (!setupEngine()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
createDefaultViews();
|
|
|
|
}
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
void ViewerWindow::createMenus() {
|
2016-09-09 22:13:21 +02:00
|
|
|
QMenuBar* mb = this->menuBar();
|
|
|
|
QMenu* file = mb->addMenu("&File");
|
|
|
|
|
|
|
|
file->addAction("Open &Game", this, SLOT(loadGame()));
|
|
|
|
|
|
|
|
file->addSeparator();
|
|
|
|
for (int i = 0; i < MaxRecentGames; ++i) {
|
|
|
|
QAction* r = file->addAction("");
|
|
|
|
recentGames.append(r);
|
2017-09-07 19:03:18 +02:00
|
|
|
connect(r, &QAction::triggered, this, [r, this]() {
|
|
|
|
QString recentGame = r->data().toString();
|
|
|
|
loadGame(recentGame);
|
|
|
|
});
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
recentSep = file->addSeparator();
|
|
|
|
auto ex = file->addAction("E&xit");
|
|
|
|
ex->setShortcut(QKeySequence::Quit);
|
2017-09-07 19:03:18 +02:00
|
|
|
connect(ex, &QAction::triggered,
|
|
|
|
QApplication::instance(), &QApplication::closeAllWindows);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
mb->addMenu("&Data");
|
|
|
|
|
|
|
|
updateRecentGames();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewerWindow::setupEngine() {
|
|
|
|
QSurfaceFormat format = windowHandle()->format();
|
|
|
|
format.setProfile(QSurfaceFormat::CoreProfile);
|
|
|
|
format.setVersion(3,3);
|
2018-08-10 02:40:23 +02:00
|
|
|
m_context.setup(format);
|
2018-01-16 02:05:23 +01:00
|
|
|
|
|
|
|
hiddenSurface = new QOffscreenSurface(windowHandle()->screen());
|
|
|
|
hiddenSurface->setFormat(format);
|
|
|
|
hiddenSurface->create();
|
|
|
|
|
2018-08-10 02:40:23 +02:00
|
|
|
if (!m_context.create()) {
|
2018-01-16 02:05:23 +01:00
|
|
|
QMessageBox::critical(this, "OpenGL Failure",
|
|
|
|
"Failed to create OpenGL context");
|
|
|
|
QApplication::exit(1);
|
|
|
|
return false;
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
void ViewerWindow::createDefaultViews() {
|
|
|
|
views = new QTabWidget(this);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
auto objectView = new ObjectViewer(this);
|
|
|
|
views->addTab(objectView, "Objects");
|
|
|
|
connect(this, &ViewerWindow::gameLoaded, objectView, &ObjectViewer::showData);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
auto modelView = new ModelViewer(this);
|
|
|
|
views->addTab(modelView, "Model");
|
|
|
|
connect(this, &ViewerWindow::gameLoaded, modelView, &ModelViewer::showData);
|
|
|
|
connect(objectView, &ObjectViewer::showObjectModel, modelView, &ModelViewer::showObject);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
auto worldView = new WorldViewer(this);
|
|
|
|
views->addTab(worldView, "World");
|
|
|
|
connect(this, &ViewerWindow::gameLoaded, worldView, &WorldViewer::showData);
|
|
|
|
|
2017-09-07 19:03:18 +02:00
|
|
|
auto textView = new TextViewer(this);
|
|
|
|
views->addTab(textView, "Texts");
|
|
|
|
connect(this, &ViewerWindow::gameLoaded, textView, &TextViewer::showData);
|
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
setCentralWidget(views);
|
2014-02-10 16:34:09 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ViewerWindow::showEvent(QShowEvent*) {
|
|
|
|
static bool first = true;
|
|
|
|
if (first) {
|
|
|
|
QSettings settings("OpenRW", "rwviewer");
|
|
|
|
restoreGeometry(settings.value("window/geometry").toByteArray());
|
|
|
|
restoreState(settings.value("window/windowState").toByteArray());
|
|
|
|
first = false;
|
|
|
|
}
|
2014-02-12 08:12:40 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ViewerWindow::closeEvent(QCloseEvent* event) {
|
|
|
|
QSettings settings("OpenRW", "rwviewer");
|
|
|
|
settings.setValue("window/geometry", saveGeometry());
|
|
|
|
settings.setValue("window/windowState", saveState());
|
|
|
|
QMainWindow::closeEvent(event);
|
2014-02-10 17:15:22 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ViewerWindow::loadGame() {
|
|
|
|
QString dir = QFileDialog::getExistingDirectory(
|
|
|
|
this, tr("Open Directory"), QDir::homePath(),
|
|
|
|
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
|
2014-06-08 02:58:49 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
if (dir.size() > 0) loadGame(dir);
|
2014-06-08 02:58:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ViewerWindow::loadGame(const QString& path) {
|
|
|
|
QDir gameDir(path);
|
2018-01-16 02:05:23 +01:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!gameDir.exists()) {
|
|
|
|
QMessageBox::critical(this, "Error", "The requested path doesn't exist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!makeCurrent()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-08 02:58:49 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
gameData = std::make_unique<GameData>(&engineLog, gameDir.absolutePath().toStdString());
|
|
|
|
gameWorld = std::make_unique<GameWorld>(&engineLog, gameData.get());
|
|
|
|
renderer = std::make_unique<GameRenderer>(&engineLog, gameData.get());
|
|
|
|
gameWorld->state = new GameState;
|
2015-04-13 02:48:29 +02:00
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
gameWorld->data->load();
|
2014-09-19 01:10:05 +02:00
|
|
|
|
2017-09-07 19:03:18 +02:00
|
|
|
renderer->text.setFontTexture(FONT_PAGER, "pager");
|
|
|
|
renderer->text.setFontTexture(FONT_PRICEDOWN, "font1");
|
|
|
|
renderer->text.setFontTexture(FONT_ARIAL, "font2");
|
|
|
|
|
2018-01-16 02:05:23 +01:00
|
|
|
gameLoaded(gameWorld.get(), renderer.get());
|
2014-06-08 02:58:49 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
QSettings settings("OpenRW", "rwviewer");
|
|
|
|
QStringList recent = settings.value("recentGames").toStringList();
|
|
|
|
recent.removeAll(path);
|
|
|
|
recent.prepend(path);
|
|
|
|
while (recent.size() > MaxRecentGames) recent.removeLast();
|
|
|
|
settings.setValue("recentGames", recent);
|
2014-06-08 02:58:49 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
updateRecentGames();
|
2014-02-11 06:46:29 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ViewerWindow::showObjectModel(uint16_t) {
|
2018-06-07 01:51:37 +02:00
|
|
|
#pragma message("implement me")
|
2015-04-14 02:06:50 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ViewerWindow::updateRecentGames() {
|
|
|
|
QSettings settings("OpenRW", "rwviewer");
|
|
|
|
QStringList recent = settings.value("recentGames").toStringList();
|
|
|
|
|
|
|
|
for (int i = 0; i < MaxRecentGames; ++i) {
|
|
|
|
if (i < recent.size()) {
|
|
|
|
QString fnm(QFileInfo(recent[i]).fileName());
|
|
|
|
recentGames[i]->setText(tr("&%1 - %2").arg(i).arg(fnm));
|
|
|
|
recentGames[i]->setData(recent[i]);
|
|
|
|
recentGames[i]->setVisible(true);
|
|
|
|
} else {
|
|
|
|
recentGames[i]->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recentSep->setVisible(recent.size() > 0);
|
2014-02-11 15:41:46 +01:00
|
|
|
}
|
2018-01-16 02:05:23 +01:00
|
|
|
|
|
|
|
ViewerWindow::~ViewerWindow() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewerWindow::makeCurrent() {
|
2018-08-10 02:40:23 +02:00
|
|
|
if (!m_context.makeCurrent(hiddenSurface)) {
|
2018-01-16 02:05:23 +01:00
|
|
|
QMessageBox::critical(this, "OpenGL", "makeCurrent failed");
|
|
|
|
QApplication::exit(1);
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 02:40:23 +02:00
|
|
|
return true;
|
2018-01-16 02:05:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewerWidget *ViewerWindow::createViewer() {
|
2018-08-10 02:40:23 +02:00
|
|
|
auto view = new ViewerWidget(&m_context, windowHandle());
|
2018-01-16 02:05:23 +01:00
|
|
|
connect(this, &ViewerWindow::gameLoaded, view, &ViewerWidget::gameLoaded);
|
|
|
|
return view;
|
|
|
|
}
|