diff options
Diffstat (limited to '')
-rw-r--r-- | src/tileset.py | 136 |
1 files changed, 58 insertions, 78 deletions
diff --git a/src/tileset.py b/src/tileset.py index c060f07..8d4bbb6 100644 --- a/src/tileset.py +++ b/src/tileset.py @@ -175,17 +175,17 @@ class KPTileObject(object): class KPGroupModel(QtCore.QAbstractListModel): """Model containing all the grouped objects in a tileset""" - + def __init__(self, groupItem): self.container = groupItem self.view = None QtCore.QAbstractListModel.__init__(self) - - + + def rowCount(self, parent=None): return self.container.objectCount() - + def groupItem(self): """Returns the group item""" @@ -224,16 +224,16 @@ class KPGroupModel(QtCore.QAbstractListModel): if not index.isValid(): return None n = index.row() - + if n < 0: return None if n >= self.container.objectCount(): return None - + item, depth = self.container.getItem(n) if role == Qt.DecorationRole: if isinstance(item, KPTileObject): return item.icon - + elif role == Qt.DisplayRole: if isinstance(item, KPGroupItem): return item.name @@ -251,7 +251,7 @@ class KPGroupModel(QtCore.QAbstractListModel): colour = 255 brush = QtGui.QBrush(QtGui.QColor(colour, colour, colour), Qt.Dense4Pattern) - + return brush elif (role == Qt.FontRole): @@ -260,7 +260,7 @@ class KPGroupModel(QtCore.QAbstractListModel): font.setBold(True) return font - + return None @@ -268,7 +268,7 @@ class KPGroupModel(QtCore.QAbstractListModel): class KPGroupItem(object): """Hierarchal object for making a 2D hierarchy recursively using classes""" - + def __init__(self, name): self.objects = [] @@ -278,7 +278,7 @@ class KPGroupItem(object): self.name = name self.alignment = Qt.AlignCenter - + def getGroupList(self, returnList=[], depth=0): """Gets a list of group names and indices for the sorter menu""" @@ -304,7 +304,7 @@ class KPGroupItem(object): objCount += group.objectCount() return objCount - + def getItem(self, index, depth=0): ''' Retrieves an item of a specific index. The index is already checked for validity ''' @@ -342,7 +342,7 @@ class KPTileset(object): @classmethod def loadFromArc(cls, handleOrPath): arc = WiiArchiveU8(handleOrPath) - + img = arc.resolvePath('/BG_tex').children[0].data grp = arc.resolvePath('/BG_grp').children[0].data @@ -357,7 +357,7 @@ class KPTileset(object): return cls(img, obj, meta, grp) - + def __init__(self, imageBuffer, objectBuffer, objectMetadata, groupBuffer): '''A Koopatlas Tileset class. To initialize, pass it image data, object data, and group data as read from a Koopatlas tileset file. @@ -372,7 +372,7 @@ class KPTileset(object): getTile(TileIndex) # Returns a tile image as a QPixmap - + getObject(ObjectIndex) # Returns a KPTileObject @@ -410,9 +410,9 @@ class KPTileset(object): tiles, which get put into KPTileset.tiles.''' dest = self.RGB4A3Decode(imageBuffer) - + self.tileImage = QtGui.QPixmap.fromImage(dest) - + # Makes us some nice Tiles! Xoffset = 4 Yoffset = 4 @@ -435,13 +435,13 @@ class KPTileset(object): for xtile in xrange(0, 1024, 4): for ypixel in xrange(ytile, ytile + 4): for xpixel in xrange(xtile, xtile + 4): - + if(xpixel >= 1024 or ypixel >= 512): continue newpixel = struct.unpack_from('>H', tex, i)[0] # newpixel = (int(tex[i]) << 8) | int(tex[i+1]) - + if(newpixel >= 0x8000): # Check if it's RGB555 red = ((newpixel >> 10) & 0x1F) * 255 / 0x1F @@ -465,24 +465,24 @@ class KPTileset(object): '''Takes the object files from a Koopatlas Tileset and converts it into KPTileObject classes, which get put into KPTileset.objects.''' - # Load Objects - + # Load Objects + meta = [] for i in xrange(len(metadata)/5): meta.append(struct.unpack_from('>H3B', metadata, i * 5)) - + tilelist = [] rowlist = [] for entry in meta: offset = entry[0] row = 0 - + tex = QtGui.QPixmap(entry[1] * 24, entry[2] * 24) tex.fill(Qt.transparent) painter = QtGui.QPainter(tex) - - + + for tilesA in xrange(entry[2]): for tilesB in xrange(entry[1]): untile = struct.unpack_from('>h', objstrings, offset)[0] @@ -497,7 +497,7 @@ class KPTileset(object): rowlist = [] painter.end() - + self.objects.append(KPTileObject(tilelist, entry[2], entry[1], entry[3], tex)) @@ -515,7 +515,7 @@ class KPTileset(object): def makeTree(self, grovyle, treecko): - + for razorLeaf in grovyle: @@ -540,98 +540,78 @@ class KPTileset(object): '''Takes a tile index and returns a tile image as a QPixmap, or -1 if failed.''' if index > 511: - return false - + return False + if index < 0: - return false + return False return self.tiles[index] def getObject(self, index): - '''Takes an object index and returns a KPTileObject, or false if failed.''' - + '''Takes an object index and returns a KPTileObject, or False if failed.''' + if index < 0: - return false + return False return self.tiles[index] def getObjectIcon(self, index): '''Takes an object index or a KPTileObject and returns a QPixmap for the - object, or false if failed.''' - - if isinstance(index, KPTileObject): + object, or False if failed.''' + + if hasattr(index, 'icon'): return index.icon - if isinstance(index, int): - try: - return self.objects[index].icon - except: - return false + try: + return self.objects[index].icon + except: + pass - return false + return False def getObjectRender(self, index, size): '''Takes an object index or a KPTileObject and returns a render map for the - object, or false if failed.''' - - if isinstance(index, KPTileObject): + object, or False if failed.''' + + if hasattr(index, 'render'): return index.render(size) - if isinstance(index, int): - try: - return self.objects[index].render(size) - except: - return false + try: + return self.objects[index].render(size) + except: + pass - return false + return False def getModel(self): '''Returns the Group Model''' - + return self.groupModel def overrideTile(self, index, pixmap): - '''Takes a 24x24 QPixmap and a tile index, and returns true if it succeeds.''' + '''Takes a 24x24 QPixmap and a tile index, and returns True if it succeeds.''' if index > 511: - return false - + return False + if index < 0: - return false + return False if not isinstance(pixmap, QtGui.QPixmap): - return false + return False - if (QtGui.QPixmap.height() != 24) or (QtGui.QPixmap.width() != 24): - return false + if (pixmap.height() != 24) or (pixmap.width() != 24): + return False self.tiles[index] = pixmap - return true - - - - - - - - - - - - - - - - - - + return True |