Example #1
0
    def convertV1toV2(self, fileName):
        reader = MapReader()
        map = reader.readMap(fileName)
        if (not map):
            qWarning("Error at conversion of " + fileName + ":\n" +
                     reader.errorString())
            return

        for layer in map.layers():
            if (layer.name().lower().startswith("ruleset")):
                layer.setName("Input_set")
            elif (layer.name().lower().startswith("rulenotset")):
                layer.setName("InputNot_set")
            elif (layer.name().lower().startswith("ruleregions")):
                layer.setName("Regions")
            elif (layer.name().lower().startswith("rule")):
                newname = layer.name().right(layer.name().length() - 4)
                layer.setName("Output" + newname)
            else:
                qWarning(
                    QString("Warning at conversion of") + fileName +
                    QString("unused layers found"))

        writer = MapWriter()
        writer.writeMap(map.data(), fileName)
        map.tilesets().clear()
Example #2
0
    def automappingRuleFileVersion(self, fileName):
        reader = MapReader()
        map = reader.readMap(fileName)
        if (not map):
            return self.versionNotAMap()
        # version 1 check
        hasonlyruleprefix = True
        for layer in map.layers():
            if (not layer.name().lower().startswith("rule")):
                hasonlyruleprefix = False

        if (hasonlyruleprefix):
            return self.version1()
        # version 2 check
        hasrule = False
        hasoutput = False
        hasregion = False
        allused = True
        for layer in map.layers():
            isunused = True
            if (layer.name().lower().startswith("input")):
                hasrule = True
                isunused = False

            if (layer.name().lower().startswith("output")):
                hasoutput = True
                isunused = False

            if (layer.name().lower() == "regions"):
                hasregion = True
                isunused = False

            if (isunused):
                allused = False

        if (allused and hasoutput and hasregion and hasrule):
            return self.version2()
        return self.versionUnknown()
Example #3
0
    def viewMap(self, fileName):
        del self.mRenderer
        self.mRenderer = None
        self.mScene.clear()
        self.centerOn(0, 0)
        reader = MapReader()
        self.mMap = reader.readMap(fileName)
        if (not self.mMap):
            qWarning("Error:"+reader.errorString())
            return False

        x = self.mMap.orientation()
        if x==Map.Orientation.Isometric:
            self.mRenderer = IsometricRenderer(self.mMap)
        elif x==Map.Orientation.Staggered:
            self.mRenderer = StaggeredRenderer(self.mMap)
        elif x==Map.Orientation.Hexagonal:
            self.mRenderer = HexagonalRenderer(self.mMap)
        else:
            self.mRenderer = OrthogonalRenderer(self.mMap)

        self.mScene.addItem(MapItem(self.mMap, self.mRenderer))
        return True
Example #4
0
    def render(self, mapFileName, imageFileName):
        map = None
        renderer = None
        reader = MapReader()
        map = reader.readMap(mapFileName)
        if (not map):
            qWarning("Error while reading " + mapFileName + ":\n" +
                     reader.errorString())
            return 1

        x = map.orientation()
        if x == Map.Orientation.Isometric:
            renderer = IsometricRenderer(map)
        elif x == Map.Orientation.Staggered:
            renderer = StaggeredRenderer(map)
        elif x == Map.Orientation.Hexagonal:
            renderer = HexagonalRenderer(map)
        else:
            renderer = OrthogonalRenderer(map)

        if (self.mTileSize > 0):
            xScale = self.mTileSize / map.tileWidth()
            yScale = self.mTileSize / map.tileHeight()
        else:
            xScale = yScale = self.mScale

        mapSize = renderer.mapSize()
        margins = map.computeLayerOffsetMargins()
        mapSize.setWidth(mapSize.width() + margins.left() + margins.right())
        mapSize.setHeight(mapSize.height() + margins.top() + margins.bottom())

        mapSize.setWidth(mapSize.width() * xScale)
        mapSize.setHeight(mapSize.height() * yScale)
        image = QImage(mapSize, QImage.Format_ARGB32)
        image.fill(Qt.transparent)
        painter = QPainter(image)
        if (xScale != 1.0 or yScale != 1.0):
            if (self.mUseAntiAliasing):
                painter.setRenderHints(QPainter.SmoothPixmapTransform
                                       | QPainter.Antialiasing)

            painter.setTransform(QTransform.fromScale(xScale, yScale))

        painter.translate(margins.left(), margins.top())

        # Perform a similar rendering than found in exportasimagedialog.py
        for layer in map.layers():
            if (not self.shouldDrawLayer(layer)):
                continue
            painter.setOpacity(layer.opacity())
            painter.translate(layer.offset())
            tileLayer = layer
            imageLayer = layer
            tp = type(layer)
            if tp == TileLayer:
                renderer.drawTileLayer(painter, tileLayer)
            elif tp == ImageLayer:
                renderer.drawImageLayer(painter, imageLayer)
            painter.translate(-layer.offset())
        painter.end()

        # Save image
        imageWriter = QImageWriter(imageFileName)
        if (not imageWriter.write(image)):
            qWarning("Error while writing " + imageFileName + ": " +
                     imageWriter.errorString())
            return 1

        return 0
Example #5
0
def main(argv):
    a = TiledApplication(argv)
    a.setOrganizationDomain("mapeditor.org")
    a.setApplicationName("Tiled")
    a.setApplicationVersion("0.14.2")

    if sys.platform == 'darwin':
        a.setAttribute(Qt.AA_DontShowIconsInMenus)

    # Enable support for highres images (added in Qt 5.1, but off by default)
    a.setAttribute(Qt.AA_UseHighDpiPixmaps)
    if sys.platform != 'win32':
        baseName = QApplication.style().objectName()
        if (baseName == "windows"):
            # Avoid Windows 95 style at all cost
            if (QStyleFactory.keys().contains("Fusion")):
                baseName = "fusion" # Qt5
            else: # Qt4
                # e.g. if we are running on a KDE4 desktop
                desktopEnvironment = qgetenv("DESKTOP_SESSION")
                if (desktopEnvironment == "kde"):
                    baseName = "plastique"
                else:
                    baseName = "cleanlooks"

            a.setStyle(QStyleFactory.create(baseName))
    languageManager = LanguageManager.instance()
    languageManager.installTranslators()
    commandLine = CommandLineHandler()
    if (not commandLine.parse(QCoreApplication.arguments())):
        return 0
    if (commandLine.quit):
        return 0
    if (commandLine.disableOpenGL):
        preferences.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(QCoreApplication.translate("Command line", "Export syntax is --export-map [format] "))
            return 1

        index = 0
        if commandLine.filesToOpen().length() > 2:
            filter = commandLine.filesToOpen().at(index)
            index += 1
        else:
            filter = None
        sourceFile = commandLine.filesToOpen().at(index)
        index += 1
        targetFile = commandLine.filesToOpen().at(index)
        index += 1
        
        chosenFormat = None
        formats = PluginManager.objects()

        if filter:
            # Find the map format supporting the given filter
            for format in formats:
                if not format.hasCapabilities(MapFormat.Write):
                    continue
                if format.nameFilter().lower()==filter.lower():
                    chosenFormat = format
                    break
            if not chosenFormat:
                qWarning(QCoreApplication.translate("Command line", "Format not recognized (see --export-formats)"))
                return 1
        else:
            # Find the map format based on target file extension
            suffix = QFileInfo(targetFile).completeSuffix()
            for format in formats:
                if not format.hasCapabilities(MapFormat.Write):
                    continue
                if suffix.lower() in format.nameFilter().lower():
                    if chosenFormat:
                        qWarning(QCoreApplication.translate("Command line", "Non-unique file extension. Can't determine correct export format."))
                        return 1
                    chosenFormat = format
                    
            if not chosenFormat:
                qWarning(QCoreApplication.translate("Command line", "No exporter found for target file."))
                return 1

        # Load the source file
        reader = MapReader()
        map = reader.readMap(sourceFile)
        if (not map):
            qWarning(QCoreApplication.translate("Command line", "Failed to load source map."))
            return 1

        # Write out the file
        success = chosenFormat.write(map.data(), targetFile)

        if (not success):
            qWarning(QCoreApplication.translate("Command line", "Failed to export map to target file."))
            return 1

        return 0
    w = MainWindow()
    w.show()
    a.fileOpenRequest.connect(w.openFile)
    if (not commandLine.filesToOpen().isEmpty()):
        for fileName in commandLine.filesToOpen():
            w.openFile(fileName)
    elif preferences.Preferences.instance().openLastFilesOnStartup():
        w.openLastFiles()
    return a.exec()
Example #6
0
def main(argv):
    a = TiledApplication(argv)
    a.setOrganizationDomain("mapeditor.org")
    a.setApplicationName("Tiled")
    a.setApplicationVersion("0.14.2")

    if sys.platform == 'darwin':
        a.setAttribute(Qt.AA_DontShowIconsInMenus)

    # Enable support for highres images (added in Qt 5.1, but off by default)
    a.setAttribute(Qt.AA_UseHighDpiPixmaps)
    if sys.platform != 'win32':
        baseName = QApplication.style().objectName()
        if (baseName == "windows"):
            # Avoid Windows 95 style at all cost
            if (QStyleFactory.keys().contains("Fusion")):
                baseName = "fusion"  # Qt5
            else:  # Qt4
                # e.g. if we are running on a KDE4 desktop
                desktopEnvironment = qgetenv("DESKTOP_SESSION")
                if (desktopEnvironment == "kde"):
                    baseName = "plastique"
                else:
                    baseName = "cleanlooks"

            a.setStyle(QStyleFactory.create(baseName))
    languageManager = LanguageManager.instance()
    languageManager.installTranslators()
    commandLine = CommandLineHandler()
    if (not commandLine.parse(QCoreApplication.arguments())):
        return 0
    if (commandLine.quit):
        return 0
    if (commandLine.disableOpenGL):
        preferences.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(
                QCoreApplication.translate(
                    "Command line", "Export syntax is --export-map [format] "))
            return 1

        index = 0
        if commandLine.filesToOpen().length() > 2:
            filter = commandLine.filesToOpen().at(index)
            index += 1
        else:
            filter = None
        sourceFile = commandLine.filesToOpen().at(index)
        index += 1
        targetFile = commandLine.filesToOpen().at(index)
        index += 1

        chosenFormat = None
        formats = PluginManager.objects()

        if filter:
            # Find the map format supporting the given filter
            for format in formats:
                if not format.hasCapabilities(MapFormat.Write):
                    continue
                if format.nameFilter().lower() == filter.lower():
                    chosenFormat = format
                    break
            if not chosenFormat:
                qWarning(
                    QCoreApplication.translate(
                        "Command line",
                        "Format not recognized (see --export-formats)"))
                return 1
        else:
            # Find the map format based on target file extension
            suffix = QFileInfo(targetFile).completeSuffix()
            for format in formats:
                if not format.hasCapabilities(MapFormat.Write):
                    continue
                if suffix.lower() in format.nameFilter().lower():
                    if chosenFormat:
                        qWarning(
                            QCoreApplication.translate(
                                "Command line",
                                "Non-unique file extension. Can't determine correct export format."
                            ))
                        return 1
                    chosenFormat = format

            if not chosenFormat:
                qWarning(
                    QCoreApplication.translate(
                        "Command line", "No exporter found for target file."))
                return 1

        # Load the source file
        reader = MapReader()
        map = reader.readMap(sourceFile)
        if (not map):
            qWarning(
                QCoreApplication.translate("Command line",
                                           "Failed to load source map."))
            return 1

        # Write out the file
        success = chosenFormat.write(map.data(), targetFile)

        if (not success):
            qWarning(
                QCoreApplication.translate(
                    "Command line", "Failed to export map to target file."))
            return 1

        return 0
    w = MainWindow()
    w.show()
    a.fileOpenRequest.connect(w.openFile)
    if (not commandLine.filesToOpen().isEmpty()):
        for fileName in commandLine.filesToOpen():
            w.openFile(fileName)
    elif preferences.Preferences.instance().openLastFilesOnStartup():
        w.openLastFiles()
    return a.exec()
Example #7
0
    def render(self, mapFileName, imageFileName):
        map = None
        renderer = None
        reader = MapReader()
        map = reader.readMap(mapFileName)
        if (not map):
            qWarning("Error while reading " + mapFileName + ":\n" + reader.errorString())
            return 1

        x = map.orientation()
        if x==Map.Orientation.Isometric:
            renderer = IsometricRenderer(map)
        elif x==Map.Orientation.Staggered:
            renderer = StaggeredRenderer(map)
        elif x==Map.Orientation.Hexagonal:
            renderer = HexagonalRenderer(map)
        else:
            renderer = OrthogonalRenderer(map)

        if (self.mTileSize > 0):
            xScale = self.mTileSize / map.tileWidth()
            yScale = self.mTileSize / map.tileHeight()
        else:
            xScale = yScale = self.mScale

        mapSize = renderer.mapSize()
        margins = map.computeLayerOffsetMargins()
        mapSize.setWidth(mapSize.width() + margins.left() + margins.right())
        mapSize.setHeight(mapSize.height() + margins.top() + margins.bottom())
    
        mapSize.setWidth(mapSize.width()*xScale)
        mapSize.setHeight(mapSize.height()*yScale)
        image = QImage(mapSize, QImage.Format_ARGB32)
        image.fill(Qt.transparent)
        painter = QPainter(image)
        if (xScale != 1.0 or yScale != 1.0):
            if (self.mUseAntiAliasing):
                painter.setRenderHints(QPainter.SmoothPixmapTransform |
                                       QPainter.Antialiasing)

            painter.setTransform(QTransform.fromScale(xScale, yScale))

        painter.translate(margins.left(), margins.top())
        
        # Perform a similar rendering than found in exportasimagedialog.py
        for layer in map.layers():
            if (not self.shouldDrawLayer(layer)):
                continue
            painter.setOpacity(layer.opacity())
            painter.translate(layer.offset())
            tileLayer = layer
            imageLayer = layer
            tp = type(layer)
            if tp == TileLayer:
                renderer.drawTileLayer(painter, tileLayer)
            elif tp == ImageLayer:
                renderer.drawImageLayer(painter, imageLayer)
            painter.translate(-layer.offset())
        painter.end()
        
        # Save image
        imageWriter = QImageWriter(imageFileName)
        if (not imageWriter.write(image)):
            qWarning("Error while writing " + imageFileName + ": " + imageWriter.errorString())
            return 1
    
        return 0