2014-02-10 13:41:05 +01:00
|
|
|
#include "ViewerWindow.hpp"
|
2015-04-13 02:48:29 +02:00
|
|
|
#include "views/ObjectViewer.hpp"
|
|
|
|
#include "views/ModelViewer.hpp"
|
2016-05-15 21:25:20 +02:00
|
|
|
#include "views/WorldViewer.hpp"
|
|
|
|
#include <ViewerWidget.hpp>
|
2014-09-19 01:10:05 +02:00
|
|
|
|
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 16:34:09 +01:00
|
|
|
#include <QMenuBar>
|
|
|
|
#include <QFileDialog>
|
2014-02-10 17:15:22 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QSettings>
|
2016-05-15 21:25:20 +02:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSignalMapper>
|
2014-09-19 01:10:05 +02:00
|
|
|
#include <QDebug>
|
2014-03-01 12:19:33 +01:00
|
|
|
#include <fstream>
|
2014-09-19 01:10:05 +02:00
|
|
|
#include <QOffscreenSurface>
|
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-05-15 21:25:20 +02:00
|
|
|
: QMainWindow(parent, flags)
|
|
|
|
, gameData(nullptr)
|
|
|
|
, gameWorld(nullptr)
|
|
|
|
, renderer(nullptr)
|
2014-02-10 13:41:05 +01:00
|
|
|
{
|
2014-02-10 16:34:09 +01:00
|
|
|
setMinimumSize(640, 480);
|
2014-03-01 12:19:33 +01:00
|
|
|
|
2014-02-10 16:34:09 +01:00
|
|
|
QMenuBar* mb = this->menuBar();
|
|
|
|
QMenu* file = mb->addMenu("&File");
|
2014-06-08 02:58:49 +02:00
|
|
|
|
|
|
|
file->addAction("Open &Game", this, SLOT(loadGame()));
|
|
|
|
|
2014-02-10 17:15:22 +01:00
|
|
|
file->addSeparator();
|
2014-06-08 02:58:49 +02:00
|
|
|
for(int i = 0; i < MaxRecentGames; ++i) {
|
2014-02-11 15:41:46 +01:00
|
|
|
QAction* r = file->addAction("");
|
2014-06-08 02:58:49 +02:00
|
|
|
recentGames.append(r);
|
2014-02-11 15:41:46 +01:00
|
|
|
connect(r, SIGNAL(triggered()), SLOT(openRecent()));
|
|
|
|
}
|
2014-06-08 02:58:49 +02:00
|
|
|
|
2014-02-11 15:41:46 +01:00
|
|
|
recentSep = file->addSeparator();
|
2014-02-12 08:12:40 +01:00
|
|
|
auto ex = file->addAction("E&xit");
|
|
|
|
ex->setShortcut(QKeySequence::Quit);
|
|
|
|
connect(ex, SIGNAL(triggered()), QApplication::instance(), SLOT(closeAllWindows()));
|
2014-03-01 12:19:33 +01:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
//----------------------- View Mode setup
|
2014-09-19 01:10:05 +02:00
|
|
|
viewerWidget = new ViewerWidget;
|
|
|
|
viewerWidget->context()->makeCurrent();
|
|
|
|
connect(this, SIGNAL(loadedData(GameWorld*)), viewerWidget, SLOT(dataLoaded(GameWorld*)));
|
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
//------------- Object Viewer
|
|
|
|
m_views[ViewMode::Object] = new ObjectViewer(viewerWidget);
|
|
|
|
m_viewNames[ViewMode::Object] = "Objects";
|
2015-04-13 02:48:29 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
//------------- Model Viewer
|
|
|
|
m_views[ViewMode::Model] = new ModelViewer(viewerWidget);
|
|
|
|
m_viewNames[ViewMode::Model] = "Model";
|
2015-04-13 02:48:29 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
#if 0
|
|
|
|
//------------- World Viewer
|
|
|
|
m_views[ViewMode::World] = new WorldViewer(viewerWidget);
|
|
|
|
m_viewNames[ViewMode::World] = "World";
|
|
|
|
#endif
|
2015-04-13 02:48:29 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
//------------- display mode switching
|
|
|
|
viewSwitcher = new QStackedWidget;
|
|
|
|
auto signalMapper = new QSignalMapper(this);
|
|
|
|
auto switchPanel = new QVBoxLayout();
|
|
|
|
int i = 0;
|
|
|
|
for(auto viewer : m_views) {
|
|
|
|
viewSwitcher->addWidget(viewer);
|
|
|
|
connect(this, SIGNAL(loadedData(GameWorld*)), viewer, SLOT(showData(GameWorld*)));
|
|
|
|
|
|
|
|
auto viewerButton = new QPushButton(m_viewNames[i].c_str());
|
|
|
|
signalMapper->setMapping(viewerButton, i++);
|
|
|
|
connect(viewerButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
|
|
|
|
switchPanel->addWidget(viewerButton);
|
|
|
|
}
|
2015-04-13 02:48:29 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
switchView(ViewMode::Object);
|
2014-09-20 15:20:12 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
connect(m_views[ViewMode::Object], SIGNAL(showObjectModel(uint16_t)), this, SLOT(showObjectModel(uint16_t)));
|
|
|
|
connect(m_views[ViewMode::Object], SIGNAL(showObjectModel(uint16_t)), m_views[ViewMode::Model], SLOT(showObject(uint16_t)));
|
|
|
|
connect(this, SIGNAL(loadAnimations(QString)), m_views[ViewMode::Model], SLOT(loadAnimations(QString)));
|
2014-09-20 15:20:12 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(switchView(int)));
|
|
|
|
connect(signalMapper, SIGNAL(mapped(int)), viewSwitcher, SLOT(setCurrentIndex(int)));
|
2014-09-20 15:20:12 +02:00
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
switchPanel->addStretch();
|
|
|
|
auto mainlayout = new QHBoxLayout();
|
|
|
|
mainlayout->addLayout(switchPanel);
|
|
|
|
mainlayout->addWidget(viewSwitcher);
|
|
|
|
auto mainwidget = new QWidget();
|
|
|
|
mainwidget->setLayout(mainlayout);
|
2014-09-20 15:20:12 +02:00
|
|
|
|
|
|
|
QMenu* data = mb->addMenu("&Data");
|
|
|
|
//data->addAction("Export &Model", objectViewer, SLOT(exportModel()));
|
|
|
|
|
|
|
|
QMenu* anim = mb->addMenu("&Animation");
|
|
|
|
anim->addAction("Load &Animations", this, SLOT(openAnimations()));
|
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
this->setCentralWidget(mainwidget);
|
2014-09-20 15:20:12 +02:00
|
|
|
|
2014-06-08 02:58:49 +02:00
|
|
|
updateRecentGames();
|
2014-02-10 16:34:09 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 08:12:40 +01: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-10 17:15:22 +01:00
|
|
|
void ViewerWindow::closeEvent(QCloseEvent* event)
|
|
|
|
{
|
|
|
|
QSettings settings("OpenRW", "rwviewer");
|
2014-02-12 08:12:40 +01:00
|
|
|
settings.setValue("window/geometry", saveGeometry());
|
|
|
|
settings.setValue("window/windowState", saveState());
|
2014-02-10 17:15:22 +01:00
|
|
|
QMainWindow::closeEvent(event);
|
|
|
|
}
|
|
|
|
|
2014-03-01 12:19:33 +01:00
|
|
|
void ViewerWindow::openAnimations()
|
|
|
|
{
|
|
|
|
QFileDialog dialog(this, "Open Animations", QDir::homePath(), "IFP Animations (*.ifp)");
|
|
|
|
if(dialog.exec()) {
|
2016-05-15 21:25:20 +02:00
|
|
|
loadAnimations(dialog.selectedFiles()[0]);
|
2014-03-01 12:19:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 02:58:49 +02:00
|
|
|
void ViewerWindow::loadGame()
|
2014-02-11 06:46:29 +01:00
|
|
|
{
|
2014-06-08 02:58:49 +02:00
|
|
|
QString dir = QFileDialog::getExistingDirectory(
|
|
|
|
this, tr("Open Directory"),
|
|
|
|
QDir::homePath(),
|
|
|
|
QFileDialog::ShowDirsOnly
|
|
|
|
| QFileDialog::DontResolveSymlinks);
|
|
|
|
|
|
|
|
if( dir.size() > 0 ) loadGame( dir );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewerWindow::loadGame(const QString &path)
|
|
|
|
{
|
|
|
|
QDir gameDir( path );
|
|
|
|
|
|
|
|
if( gameDir.exists() && path.size() > 0 ) {
|
2015-04-27 04:55:18 +02:00
|
|
|
gameData = new GameData( &engineLog, &work, gameDir.absolutePath().toStdString() );
|
|
|
|
gameWorld = new GameWorld( &engineLog, &work, gameData );
|
|
|
|
renderer = new GameRenderer(&engineLog, gameData );
|
2015-04-13 02:48:29 +02:00
|
|
|
viewerWidget->setRenderer(renderer);
|
|
|
|
|
2015-04-18 02:11:17 +02:00
|
|
|
gameWorld->data->load();
|
2014-09-19 01:10:05 +02:00
|
|
|
|
|
|
|
// Initalize all the archives.
|
2015-04-18 02:11:17 +02:00
|
|
|
gameWorld->data->loadIMG("/models/gta3");
|
|
|
|
gameWorld->data->loadIMG("/models/txd");
|
|
|
|
gameWorld->data->loadIMG("/anim/cuts");
|
2014-09-19 01:10:05 +02:00
|
|
|
|
|
|
|
loadedData(gameWorld);
|
2014-02-12 07:42:07 +01:00
|
|
|
}
|
2014-06-08 02:58:49 +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);
|
|
|
|
|
|
|
|
updateRecentGames();
|
2014-02-11 06:46:29 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 15:41:46 +01:00
|
|
|
void ViewerWindow::openRecent()
|
|
|
|
{
|
|
|
|
QAction* r = qobject_cast< QAction* >(sender());
|
|
|
|
if(r) {
|
2014-06-08 02:58:49 +02:00
|
|
|
loadGame( r->data().toString() );
|
2014-02-11 15:41:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
void ViewerWindow::switchView(int mode)
|
2014-09-20 15:20:12 +02:00
|
|
|
{
|
2016-05-15 21:25:20 +02:00
|
|
|
if( mode < int(m_views.size()) )
|
|
|
|
{
|
|
|
|
m_views[mode]->setViewerWidget( viewerWidget );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RW_ERROR("Unhandled view mode" << mode);
|
2014-09-20 15:20:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 21:25:20 +02:00
|
|
|
void ViewerWindow::showObjectModel(uint16_t)
|
2015-04-14 02:06:50 +02:00
|
|
|
{
|
|
|
|
// Switch to the model viewer
|
2016-05-15 21:25:20 +02:00
|
|
|
switchView(ViewMode::Model);
|
|
|
|
viewSwitcher->setCurrentIndex( viewSwitcher->indexOf(m_views[ViewMode::Model]) );
|
2015-04-14 02:06:50 +02:00
|
|
|
}
|
|
|
|
|
2014-06-08 02:58:49 +02:00
|
|
|
void ViewerWindow::updateRecentGames()
|
2014-02-11 15:41:46 +01:00
|
|
|
{
|
|
|
|
QSettings settings("OpenRW", "rwviewer");
|
2014-06-08 02:58:49 +02:00
|
|
|
QStringList recent = settings.value("recentGames").toStringList();
|
2014-02-11 15:41:46 +01:00
|
|
|
|
2014-06-08 02:58:49 +02:00
|
|
|
for(int i = 0; i < MaxRecentGames; ++i) {
|
2014-02-11 15:41:46 +01:00
|
|
|
if(i < recent.size()) {
|
|
|
|
QString fnm(QFileInfo(recent[i]).fileName());
|
2014-06-08 02:58:49 +02:00
|
|
|
recentGames[i]->setText(tr("&%1 - %2").arg(i).arg(fnm));
|
|
|
|
recentGames[i]->setData(recent[i]);
|
|
|
|
recentGames[i]->setVisible(true);
|
2014-02-11 15:41:46 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-06-08 02:58:49 +02:00
|
|
|
recentGames[i]->setVisible(false);
|
2014-02-11 15:41:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recentSep->setVisible(recent.size() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|