Exemple #1
0
def _loadExtras(filename, elemList, extraAddFunc):
    """Calls extraAddFunc on data in elemList.

    Arguments: filename - path to State file being loaded
               elemList - list of elements (should be sub elements of an
                          'extras' type like <music>, <portals>, or <fonts>)
                          to load data from.
               extraAddFunc - function to call on data from each element
    """
    for elem in elemList:
        path = elem.get("path")
        absPath = subInPath(filename, path)
        name = elem.get("name")
        extraAddFunc(absPath, name)
Exemple #2
0
    def loadTiles(self, pathname):
        """Loads tiles from NT tile animation file.

        When tiles are loaded, they are put onto the tile bar for usage with
        the map editor. If the file isn't a tile animation file then nothing
        will happen. Any tiles previously loaded in are removed.

        Arguments: pathname -- name of path to tile animation file.

        """
        self.clear()

        self._tilesPath = pathname

        tree = ElementTree()
        tree.parse(pathname)
        root = tree.getroot()

        sheets = root.findall('sheet')

        posX = 0
        posY = 0
        column = 0
        MAX_COLUMNS = 4

        for sheet in sheets:
            sheetPath = sheet.get('path')
            sheetImg = QtGui.QImage(subInPath(pathname, sheetPath))

            strips = sheet.findall('strip')

            for strip in strips:
                tile = Tile()
                tile.setAnimPath(self._tilesPath)
                tileId = int(strip.get('id'))
                tile.setId(tileId)
                self._tileIds[tileId] = tile

                bar.clipFromSheet(sheetImg, strip, tile)

                tile.setSize(tile.pixmap().width())
                lnX, lnY = bar.setForBar(posX, posY, self._defOpacity, tile)

                self.addItem(tile)
                self.addLine(lnX)
                self.addLine(lnY)

                posX, posY, column = bar.updateGridPos(posX, posY, column,
                    MAX_COLUMNS, tile.pixmap().width(), tile.pixmap().height())
Exemple #3
0
def load(workingPack, tilemap, dimsButton, objbar, tilebar, extras):
    """Loads NT state file into editor.

    Arguments: workingPack -- Pack currently being worked on, which is where
                              state should be loaded from
               tilemap -- editor's tilemap
               dimsButton -- button to set dimensions for tile map
               objbar -- editor's object bar
               tilebar -- editor's tile bar
               extras -- editor's extras section

    Returns: Path of file loaded. None if user cancels.

    """
    filename = QtGui.QFileDialog.getOpenFileName(None, "Select state file", workingPack, "*.xml")
    if not filename:
        return None
    filename = str(filename)

    tree = ElementTree()
    tree.parse(filename)
    root = tree.getroot()

    tilemap.clearPlacements()
    tilemap.clearActions()
    extras.clear()
    objbar.clear()
    tilebar.clear()

    # Load Tiles
    tiles = root.find("tiles")

    animPath = tiles.find("animation").get("path")
    if animPath != "":
        animPath = subInPath(filename, animPath)
        tilebar.loadTiles(animPath)

    layout = tiles.find("layout")

    size = int(tiles.find("size").get("px"))
    mapWidth = int(layout.get("width"))
    mapHeight = int(layout.get("height"))
    tilemap.tileSize = size
    tilemap.mapWidth = mapWidth
    tilemap.mapHeight = mapHeight

    if layout.text:
        # strip because there is whitespace on the ends
        tileIds = str(layout.text).strip().split(" ")

        tX = 0
        tY = 0
        for tid in tileIds:
            tid = int(tid)
            if tid != -1:
                tile = tilebar.getTile(tid)
                tilemap.placeTileOnTile(tX, tY, tile)

            tX = tX + 1
            if tX >= mapWidth:
                tX = 0
                tY = tY + 1

    # Load Objects
    objects = root.find("objects")
    allObjs = objects.findall("object")

    for obj in allObjs:
        path = obj.get("path")
        absPath = subInPath(filename, path)
        objbar.loadObject(absPath)

        instances = obj.findall("inst")
        for inst in instances:
            strip = int(inst.get("strip"))
            x = int(inst.get("x"))
            y = int(inst.get("y"))

            obj = objbar.getObject(absPath, strip)
            if obj:
                tilemap.placeObjectOnTile(x, y, obj)

    # Load Extras
    music = root.find("music")
    songs = music.findall("song")
    _loadExtras(filename, songs, extras.addSong)

    portals = root.find("portals")
    ports = portals.findall("port")
    _loadExtras(filename, ports, extras.addPortal)

    fonts = root.find("fonts")
    fontList = fonts.findall("font")
    _loadExtras(filename, fonts, extras.addFont)

    # Undo any selections for tilemap
    tilemap.setSelectionObject(None)

    return filename
Exemple #4
0
    def loadObject(self, filepath):
        """Loads object located at filepath.

        If an object doesn't have an animation file, a default "script" image
        is loaded in.

        Arguments: filepath -- path to object's xml file
        """
        objTree = ElementTree()
        objTree.parse(filepath)
        objRoot = objTree.getroot()

        animElem = objRoot.find('animation')
        relAnimPath = animElem.get('path')

        # Empty path means no animation file for object. So represent it
        # via a visual representation for mod objects. 
        objIsAnimated = (relAnimPath != '')

        absAnimPath = ''
        if objIsAnimated:
            absAnimPath = subInPath(filepath, relAnimPath)
        else:
            absAnimPath = "state_editor/media/mod_rep.xml"

        animTree = ElementTree()
        animTree.parse(absAnimPath)
        animRoot = animTree.getroot()

        sheets = animRoot.findall('sheet')

        objFile = basename(filepath)
        objName = splitext(objFile)[0]

        MAX_COLUMNS = 4
        animNum = 0

        for sheet in sheets:
            sheetPath = sheet.get('path')

            sheetImg = None
            if objIsAnimated:
                sheetImg = QtGui.QImage(subInPath(filepath, sheetPath))
            else:
                sheetImg = QtGui.QImage(sheetPath)

            strips = sheet.findall('strip')

            for strip in strips:
                obj = Object()
                self._objDict[filepath].append(obj)
                obj.path = filepath
                obj.animNum = animNum

                tipSuffix = ''
                if objIsAnimated:
                    tipSuffix = 'Strip ' + str(animNum)
                else:
                    tipSuffix = 'No Anim'
                obj.setToolTip(objName + ': ' + tipSuffix)

                bar.clipFromSheet(sheetImg, strip, obj)

                lnX, lnY = bar.setForBar(self._posX, self._posY,
                    self._defOpacity, obj)
                self.addItem(obj)
                self.addLine(lnX)
                self.addLine(lnY)

                if obj.pixmap().height() > self._greatestHeight:
                    self._greatestHeight = obj.pixmap().height()

                self._posX, self._posY, self._column = bar.updateGridPos(
                    self._posX, self._posY, self._column, MAX_COLUMNS,
                    obj.pixmap().width(), self._greatestHeight)

                # On a new row
                if self._column == 0:
                    self._greatestHeight = 0

                animNum += 1