1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 00:59:47 +02:00
openrw/rwviewer/ViewerWindow.cpp

217 lines
5.2 KiB
C++
Raw Normal View History

2014-02-10 13:41:05 +01:00
#include "ViewerWindow.hpp"
#include "ObjectViewer.hpp"
2014-02-11 06:46:29 +01:00
#include <engine/GameWorld.hpp>
2014-02-10 16:34:09 +01:00
#include <QMenuBar>
#include <QFileDialog>
2014-02-10 17:15:22 +01:00
#include <QApplication>
#include <QSettings>
#include <QDebug>
2014-03-01 12:19:33 +01:00
#include <fstream>
#include <QOffscreenSurface>
#include <ViewerWidget.hpp>
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
ViewerWindow::ViewerWindow(QWidget* parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags), gameWorld(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
viewerWidget = new ViewerWidget;
viewerWidget->context()->makeCurrent();
glewExperimental = 1;
glewInit();
objectViewer = new ObjectViewer(viewerWidget);
connect(this, SIGNAL(loadedData(GameWorld*)), objectViewer, SLOT(showData(GameWorld*)));
connect(this, SIGNAL(loadedData(GameWorld*)), viewerWidget, SLOT(dataLoaded(GameWorld*)));
viewSwitcher = new QStackedWidget;
viewSwitcher->addWidget(objectViewer);
QMenu* view = mb->addMenu("&View");
QAction* objectAction = view->addAction("&Object");
QAction* modelAction = view->addAction("&Model");
objectAction->setData(0);
modelAction->setData(1);
connect(objectAction, SIGNAL(triggered()), this, SLOT(switchWidget()));
connect(modelAction, SIGNAL(triggered()), this, SLOT(switchWidget()));
QMenu* data = mb->addMenu("&Data");
//data->addAction("Export &Model", objectViewer, SLOT(exportModel()));
QMenu* anim = mb->addMenu("&Animation");
anim->addAction("Load &Animations", this, SLOT(openAnimations()));
2014-09-21 17:13:01 +02:00
modelLayout = new QVBoxLayout;
QWidget* span = new QWidget;
span->setLayout(modelLayout);
viewSwitcher->addWidget(span);
this->setCentralWidget(viewSwitcher);
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()
{
#if 0
2014-03-01 12:19:33 +01:00
QFileDialog dialog(this, "Open Animations", QDir::homePath(), "IFP Animations (*.ifp)");
if(dialog.exec()) {
std::ifstream dfile(dialog.selectedFiles().at(0).toStdString().c_str());
AnimationList anims;
if(dfile.is_open())
{
dfile.seekg(0, std::ios_base::end);
size_t length = dfile.tellg();
dfile.seekg(0);
char *file = new char[length];
dfile.read(file, length);
LoaderIFP loader;
if( loader.loadFromMemory(file) ) {
for(auto& f : loader.animations) {
anims.push_back(f);
}
}
delete[] file;
}
animationswidget->setAnimations(anims);
}
#endif
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 ) {
gameWorld = new GameWorld( gameDir.absolutePath().toStdString() );
gameWorld->load();
// Initalize all the archives.
gameWorld->gameData.loadIMG("/models/gta3");
gameWorld->gameData.loadIMG("/models/txd");
gameWorld->gameData.loadIMG("/anim/cuts");
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
}
}
void ViewerWindow::switchWidget()
{
QAction* r = qobject_cast< QAction* >(sender());
if(r) {
2014-09-21 17:13:01 +02:00
int index = r->data().toInt();
if( index == 0 )
{
modelLayout->removeWidget( viewerWidget );
objectViewer->setViewerWidget( viewerWidget );
}
else if( index == 1 )
{
modelLayout->addWidget( viewerWidget );
}
viewSwitcher->setCurrentIndex( index );
}
}
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);
}