Ejemplo n.º 1
0
bool TiledMap::load()
{
    Tiled::Map *tiledMap;
    Tiled::MapReader reader;

    tiledMap = reader.readMap(m_mapSource);

    if (!tiledMap) {
        qCritical() << "Fail to read map " << m_mapSource;
        return false;
    }

    if (tiledMap->orientation() == Tiled::Map::Isometric) {
        setRenderer(new TiledRenderer(new Tiled::IsometricRenderer(tiledMap), this));
    } else if (tiledMap->orientation() == Tiled::Map::Orthogonal) {
        setRenderer(new TiledRenderer(new Tiled::OrthogonalRenderer(tiledMap), this));
    } else {
        qWarning() << "Map type not supported.";
        return false;
    }

    setTiledMap(tiledMap);

    loadLayers(tiledMap);

    emit loaded();

    return true;
}
Ejemplo n.º 2
0
/* -------------------------------------------------------------------------------------------
 *
 * ------------------------------------------------------------------------------------------- */
void DMWindow::addMap() {
  QString fileName = QFileDialog::getOpenFileName(this,tr("Open Game"),rpg::FileUtils::mapsDirectory(), tr("Map Files (*.tmx)"));
  if(fileName  != "") {
    // Get the name of the map
    Tiled::MapReader reader;
    Tiled::Map *map = reader.readMap(fileName);
    QString name = map->properties()["name"] == "" ? QDir(fileName).dirName() : map->properties()["name"];
    delete map;

    QVariantMap data;
    data.insert("name", name);
    data.insert("file",rpg::FileUtils::relativeTo(rpg::FileUtils::mapsDirectory(), fileName));
    this->engine->getRepository()->put("maps",QUuid::createUuid().toString(),data);
    this->updateMapList();
  }
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    /*
     * On X11, Tiled uses the 'raster' graphics system by default, because the
     * X11 native graphics system has performance problems with drawing the
     * tile grid.
     */
#ifdef Q_WS_X11
    QApplication::setGraphicsSystem(QLatin1String("raster"));
#endif

    TiledApplication a(argc, argv);

    a.setOrganizationDomain(QLatin1String("mapeditor.org"));
    a.setApplicationName(QLatin1String("Tiled - Sydoria Map Editor"));
#ifdef BUILD_INFO_VERSION
    a.setApplicationVersion(QLatin1String(AS_STRING(BUILD_INFO_VERSION)));
#else
    a.setApplicationVersion(QLatin1String("0.11.0"));
#endif

#ifdef Q_OS_MAC
    a.setAttribute(Qt::AA_DontShowIconsInMenus);
#endif

#if QT_VERSION >= 0x050100
    // Enable support for highres images (added in Qt 5.1, but off by default)
    a.setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

#ifndef Q_OS_WIN
    QString baseName = QApplication::style()->objectName();
    if (baseName == QLatin1String("windows")) {
        // Avoid Windows 95 style at all cost
        if (QStyleFactory::keys().contains(QLatin1String("Fusion"))) {
            baseName = QLatin1String("fusion"); // Qt5
        } else { // Qt4
            // e.g. if we are running on a KDE4 desktop
            QByteArray desktopEnvironment = qgetenv("DESKTOP_SESSION");
            if (desktopEnvironment == "kde")
                baseName = QLatin1String("plastique");
            else
                baseName = QLatin1String("cleanlooks");
        }
        a.setStyle(QStyleFactory::create(baseName));
    }
#endif

    LanguageManager *languageManager = LanguageManager::instance();
    languageManager->installTranslators();

    CommandLineHandler commandLine;

    if (!commandLine.parse(QCoreApplication::arguments()))
        return 0;
    if (commandLine.quit)
        return 0;
    if (commandLine.disableOpenGL)
        Preferences::instance()->setUseOpenGL(false);

    PluginManager::instance()->loadPlugins();

    if (commandLine.exportMap) {
        // Get the path to the source file and target file
        if (commandLine.filesToOpen().length() < 2) {
            qWarning() << qPrintable(QCoreApplication::translate("Command line",
                                                                 "Export syntax is --export-map [format] <tmx file> <target file>"));
            return 1;
        }
        int index = 0;
        const QString *filter = commandLine.filesToOpen().length() > 2 ? &commandLine.filesToOpen().at(index++) : 0;
        const QString &sourceFile = commandLine.filesToOpen().at(index++);
        const QString &targetFile = commandLine.filesToOpen().at(index++);

        // Find the map writer interface for the target file
        Tiled::MapWriterInterface *chosenWriter = 0;
        QString suffix = QFileInfo(targetFile).completeSuffix();
        QList<Tiled::MapWriterInterface*> writers = PluginManager::instance()->interfaces<Tiled::MapWriterInterface>();
        foreach (Tiled::MapWriterInterface *writer, writers) {
            if (filter) {
                if (writer->nameFilters().contains(*filter, Qt::CaseInsensitive)) {
                    chosenWriter = writer;
                }
            }
            else if (!writer->nameFilters().filter(suffix, Qt::CaseInsensitive).isEmpty()) {
                if (chosenWriter) {
                    qWarning() << qPrintable(QCoreApplication::translate("Command line",
                                                                         "Non-unique file extension. Can't determine correct export format."));
                    return 1;
                }
                chosenWriter = writer;
            }
        }
        if (!chosenWriter) {
            qWarning() << qPrintable(QCoreApplication::translate("Command line",
                                                                 "No exporter found for target file."));
            return 1;
        }

        // Load the source file
        Tiled::MapReader reader;
        Tiled::Map *map = reader.readMap(sourceFile);
        if (!map) {
            qWarning() << qPrintable(QCoreApplication::translate("Command line",
                                                                 "Failed to load source map."));
            return 1;
        }

        // Write out the file
        bool success = chosenWriter->write(map, targetFile);

        qDeleteAll(map->tilesets());
        delete map;

        if (!success) {
            qWarning() << qPrintable(QCoreApplication::translate("Command line",
                                                                 "Failed to export map to target file."));
            return 1;
        }
        return 0;
    }

    MainWindow w;
    w.show();

    QObject::connect(&a, SIGNAL(fileOpenRequest(QString)),
                     &w, SLOT(openFile(QString)));

    if (!commandLine.filesToOpen().isEmpty()) {
        foreach (const QString &fileName, commandLine.filesToOpen())
            w.openFile(fileName);
    } else {
Ejemplo n.º 4
0
void MainWindow::updateSavegameList()
{
    if(!datapackPathExists)
    {
        ui->savegameEmpty->setText(QString("<html><head/><body><p align=\"center\"><span style=\"font-size:12pt;color:#a0a0a0;\">%1</span></p></body></html>").arg(tr("No datapack!")));
        return;
    }
    QString lastSelectedPath;
    if(selectedSavegame!=NULL)
        lastSelectedPath=savegamePathList[selectedSavegame];
    selectedSavegame=NULL;
    int index=0;
    while(savegame.size()>0)
    {
        delete savegame.at(0);
        savegame.removeAt(0);
        index++;
    }
    savegamePathList.clear();
    savegameWithMetaData.clear();
    QFileInfoList entryList=QDir(savegamePath).entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System,QDir::DirsFirst);//possible wait time here
    index=0;
    while(index<entryList.size())
    {
        bool ok=false;
        QFileInfo fileInfo=entryList.at(index);
        if(!fileInfo.isDir())
        {
            index++;
            continue;
        }
        QString savegamesPath=fileInfo.absoluteFilePath()+"/";
        QSettings metaData(savegamesPath+"metadata.conf",QSettings::IniFormat);
        SaveGameLabel *newEntry=new SaveGameLabel();
        connect(newEntry,SIGNAL(clicked()),this,SLOT(savegameLabelClicked()),Qt::QueuedConnection);
        connect(newEntry,SIGNAL(doubleClicked()),this,SLOT(savegameLabelDoubleClicked()),Qt::QueuedConnection);
        newEntry->setStyleSheet("QLabel::hover{border:1px solid #bbb;background-color:rgb(180,180,180,100);border-radius:10px;}");
        QString dateString;
        if(!QFileInfo(savegamesPath+"metadata.conf").exists())
            newEntry->setText(QString("<span style=\"font-size:12pt;font-weight:600;\">Missing metadata</span><br/><span style=\"color:#909090;\">Missing metadata<br/>Bug</span>"));
        else
        {
            dateString=QFileInfo(savegamesPath+"metadata.conf").lastModified().toString("dd/MM/yyyy hh:mm:ssAP");
            if(!metaData.isWritable())
                newEntry->setText(QString("<span style=\"font-size:12pt;font-weight:600;\">%1</span><br/><span style=\"color:#909090;\">%2<br/>Bug</span>").arg("Bug").arg(dateString));
            else
            {
                if(metaData.status()==QSettings::NoError)
                {
                    if(metaData.contains("title") && metaData.contains("location") && metaData.contains("time_played") && metaData.contains("pass"))
                    {
                        int time_played_number=metaData.value("time_played").toUInt(&ok);
                        QString time_played;
                        if(!ok)
                            time_played="Time player: bug";
                        else if(time_played_number>=3600*24*10)
                            time_played=QObject::tr("%n day(s) played","",time_played_number/(3600*24));
                        else if(time_played_number>=3600*24)
                            time_played=QObject::tr("%n day(s) and %1 played","",time_played_number/3600*24).arg(QObject::tr("%n hour(s)","",(time_played_number%(3600*24))/3600));
                        else if(time_played_number>=3600)
                            time_played=QObject::tr("%n hour(s) and %1 played","",time_played_number/3600).arg(QObject::tr("%n minute(s)","",(time_played_number%3600)/60));
                        else
                            time_played=QObject::tr("%n minute(s) and %1 played","",time_played_number/60).arg(QObject::tr("%n second(s)","",time_played_number%60));

                        //load the map name
                        QString mapName=metaData.value("location").toString();
                        Tiled::MapReader reader;
                        Tiled::Map * tiledMap = reader.readMap(datapackPath+DATAPACK_BASE_PATH_MAP+metaData.value("location").toString());
                        if(tiledMap)
                        {
                            if(tiledMap->properties().contains("name"))
                                mapName=tiledMap->properties().value("name");
                            delete tiledMap;
                        }

                        newEntry->setText(QString("<span style=\"font-size:12pt;font-weight:600;\">%1</span><br/><span style=\"color:#909090;\">%2<br/>%3 (%4)</span>")
                                          .arg(metaData.value("title").toString())
                                          .arg(dateString)
                                          .arg(mapName)
                                          .arg(time_played)
                                          );
                    }
                }
                else
                    newEntry->setText(QString("<span style=\"font-size:12pt;font-weight:600;\">%1</span><br/><span style=\"color:#909090;\">%2<br/>Bug</span>").arg(metaData.value("title").toString()).arg(dateString));
            }
        }
        ui->scrollAreaWidgetContents->layout()->addWidget(newEntry);

        if(lastSelectedPath==savegamesPath)
            selectedSavegame=newEntry;
        savegame << newEntry;
        savegamePathList[newEntry]=savegamesPath;
        savegameWithMetaData[newEntry]=ok;
        index++;
    }
    ui->savegameEmpty->setVisible(index==0);
    if(index>0)
    {
        ui->scrollAreaWidgetContents->layout()->removeItem(spacer);
        delete spacer;
        spacer=new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Expanding);
        ui->scrollAreaWidgetContents->layout()->addItem(spacer);
    }
    savegameLabelUpdate();
}