def startElement(self, name, attrs): if name == 'map': # We can get some basic info about the map from this tag if 'width' in attrs.getNames(): # Be careful here, attrs.getValue always return a # string, so always convert afterwards self.map.size[0] = int(attrs.getValue('width')) if 'height' in attrs.getNames(): self.map.size[1] = int(attrs.getValue('height')) # remember that we are in this tag self.inMap = True # Tileset tag # Add a new tileset to the map with all the options if name == 'tileset' and self.inMap == True: # Make a new blank tileset. tileset = Tileset() # Attach the map to the tileset tileset.map = self.map # Fill it with some of the new info in this tag if 'tilewidth' in attrs.getNames(): tileset.tileSize[0] = int(attrs.getValue('tilewidth')) if 'tileheight' in attrs.getNames(): tileset.tileSize[1] = int(attrs.getValue('tileheight')) if 'name' in attrs.getNames(): tileset.name = attrs.getValue('name') # K, add this tileset to the loaded map self.map.tilesets.append(tileset) # We can add things from nested tags into it later. self.inTileset = True # Image Tag # We should add this image to the current tileset if name == 'image' and self.inTileset: # Find the location of the image and load it if 'source' in attrs.getNames(): self.map.tilesets[-1].load(attrs.getValue('source'), self.map.tilesets[-1].tileSize) # K, done... assuming that image loaded. # Layer Tag if name == 'layer' and self.inMap: # Make a new layer layer = Layer() # Attach layer to map layer.map = self.map # Get the name of the layer if 'name' in attrs.getNames(): layer.name = attrs.getValue('name') # Width and Height if 'width' in attrs.getNames(): layer.width = int(attrs.getValue('width')) if 'height' in attrs.getNames(): layer.height = int(attrs.getValue('height')) # Add the new layer to the map self.map.layers.append(layer) # We're inside the layer tag now self.inLayer = True # Properties if name == 'properties' and self.inLayer: self.inProperties = True if name == 'property' and self.inProperties and self.inLayer: # Add this property to the current layer if 'name' in attrs.getNames(): if attrs.getValue('name') == 'Solid': if 'value' in attrs.getNames(): if attrs.getValue('value') == '1': self.map.layers[-1].solid = True # Data Tag if name == 'data' and self.inLayer: # Find out how the data is encoded if 'encoding' in attrs.getNames(): self.dataEncoding = attrs.getValue('encoding') # Flush the buffer self.dataBuffer = '' # Inside data, now we can load the layer data self.inData = True