From 258ea63e0629df5a398fe540059c4d1f6105bf08 Mon Sep 17 00:00:00 2001 From: Treeki Date: Tue, 8 Nov 2011 23:33:31 +0100 Subject: made the thing run and added some quick and dirty testing code --- src/tileset.py | 51 +++++++++++++------------ src/ui.py | 106 +++++++++++++++++++++++++++------------------------- src/wii/__init__.py | 0 3 files changed, 83 insertions(+), 74 deletions(-) create mode 100644 src/wii/__init__.py (limited to 'src') diff --git a/src/tileset.py b/src/tileset.py index 609fa1a..857a805 100644 --- a/src/tileset.py +++ b/src/tileset.py @@ -1,4 +1,6 @@ from common import * +import struct +import cPickle @@ -195,22 +197,22 @@ class KPGroupModel(QtCore.QAbstractListModel): item = self.container.getItem(n) if role == QtCore.Qt.DecorationRole: - if isInstance(item, KPTileObject): + if isinstance(item, KPTileObject): return item.icon if role == QtCore.Qt.BackgroundRole: return QtGui.qApp.palette().base() if role == QtCore.Qt.UserRole: - if isInstance(item, KPTileObject): + if isinstance(item, KPTileObject): return item.itemsize if role == QtCore.Qt.DisplayRole: - if isInstance(item, KPGroupItem): + if isinstance(item, KPGroupItem): return item.name if role == QtCore.Qt.TextAlignmentRole: - if isInstance(item, KPGroupItem): + if isinstance(item, KPGroupItem): return item.alignment @@ -239,13 +241,13 @@ class KPGroupItem(object): objCount += len(self.objects) objCount += len(self.groups) - for group in groups: + for group in self.groups: objCount += group.objectCount() return objCount - def getItem(index): + def getItem(self, index): ''' Retrieves an item of a specific index. The index is already checked for validity ''' if index == self.startIndex: @@ -313,7 +315,7 @@ class KPTileset(object): '''Takes an imageBuffer from a Koopatlas Tileset and converts it into 24x24 tiles, which get put into KPTileset.tiles.''' - dest = self.RGB4A3Decode(Image) + dest = self.RGB4A3Decode(imageBuffer) self.tileImage = QtGui.QPixmap.fromImage(dest) @@ -325,11 +327,12 @@ class KPTileset(object): Xoffset += 32 if Xoffset >= 1024: Xoffset = 4 - Yoffset += 32 + Yoffset += 32 - def RGB4A3Decode (tex): + @staticmethod + def RGB4A3Decode(tex): dest = QtGui.QImage(1024,512,QtGui.QImage.Format_ARGB32) dest.fill(QtCore.Qt.transparent) @@ -399,7 +402,7 @@ class KPTileset(object): tilelist.append(rowlist) rowlist = [] - painter.end() + painter.end() self.objects.append(KPTileObject(tilelist, entry[2], entry[1], entry[3], tex)) @@ -411,7 +414,7 @@ class KPTileset(object): def processGroup(self, groupBuffer): - grovyle = cPickle.loads(group) + grovyle = cPickle.loads(groupBuffer) self.makeTree(grovyle, self.groupItem) @@ -426,7 +429,7 @@ class KPTileset(object): if (type(razorLeaf) is str) and (razorLeaf[:6] == "Object"): objnum = int(razorLeaf[7:]) - obj = self.objects(objnum) + obj = self.objects[objnum] treecko.objects.append(obj) @@ -437,11 +440,11 @@ class KPTileset(object): makeTree(razorLeaf[1], a) - treecko.endIndex = treecko.startIndex + treecko.objCount() + treecko.endIndex = treecko.startIndex + treecko.objectCount() - def getTile(index): + def getTile(self, index): '''Takes a tile index and returns a tile image as a QPixmap, or -1 if failed.''' if index > 511: @@ -453,7 +456,7 @@ class KPTileset(object): return self.tiles[index] - def getObject(index): + def getObject(self, index): '''Takes an object index and returns a KPTileObject, or false if failed.''' if index < 0: @@ -462,14 +465,14 @@ class KPTileset(object): return self.tiles[index] - def getObjectIcon(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): + if isinstance(index, KPTileObject): return index.icon - if isInstance(index, int): + if isinstance(index, int): try: return self.objects[index].icon except: @@ -478,14 +481,14 @@ class KPTileset(object): return false - def getObjectRender(index, size): + 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): + if isinstance(index, KPTileObject): return index.render(size) - if isInstance(index, int): + if isinstance(index, int): try: return self.objects[index].render(size) except: @@ -494,14 +497,14 @@ class KPTileset(object): return false - def getMode(): + def getModel(self): '''Returns the Group Model''' return self.groupModel - def overrideTile(index, pixmap): + def overrideTile(self, index, pixmap): '''Takes a 24x24 QPixmap and a tile index, and returns true if it succeeds.''' if index > 511: @@ -510,7 +513,7 @@ class KPTileset(object): if index < 0: return false - if not isInstance(pixmap, QtGui.QPixmap): + if not isinstance(pixmap, QtGui.QPixmap): return false if (QtGui.QPixmap.height() != 24) or (QtGui.QPixmap.width() != 24): diff --git a/src/ui.py b/src/ui.py index 96e2ceb..9536206 100644 --- a/src/ui.py +++ b/src/ui.py @@ -3,8 +3,6 @@ from editorui import * class KPLayerList(QtGui.QWidget): - - def __init__(self): QtGui.QWidget.__init__(self) @@ -23,20 +21,20 @@ class KPLayerList(QtGui.QWidget): self.setupToolbar(self.toolbar) self.setLayout(self.layout) - + def setupToolbar(self, tb): tb.addAction(QtGui.QIcon(), 'Add', self.addLayer) tb.addAction(QtGui.QIcon(), 'Remove', self.removeLayer) tb.addAction(QtGui.QIcon(), 'Move Up', self.moveUp) tb.addAction(QtGui.QIcon(), 'Move Down', self.moveDown) - + def selectedLayerIndex(self): return self.listView.selectionModel().currentIndex().row() def selectedLayer(self): return KP.map.layers[self.listView.selectionModel().currentIndex().row()] - + def addLayer(self): KP.map.appendLayer(KP.map.createNewLayer()) @@ -58,27 +56,26 @@ class KPLayerList(QtGui.QWidget): class KPObjectSelector(QtGui.QListView): - def __init__(self): """Initialises the widget. Remember to call setModel() on it with a KPGroupModel whenever the layer changes.""" - + QtGui.QListView.__init__(self) self.setFlow(QtGui.QListView.LeftToRight) self.setLayoutMode(QtGui.QListView.SinglePass) self.setMovement(QtGui.QListView.Static) self.setResizeMode(QtGui.QListView.Adjust) self.setWrapping(True) - + # self.setItemDelegate(KPObjectSelector.ObjectItemDelegate()) # Borrowed the signals and junk from Reggie, figure we'll need em' - self.clicked.connect(self.HandleObjReplace) - - + self.clicked.connect(self.handleObjReplace) + + def toggleTopLevel(self, id): """Changes the top level group in the list view.""" - + # Not quite sure how to implement this yet. Basically, the model is hierarchal, # and it'll return items from whatever the top level KPGroupItem is. But removing # and adding stuff isn't possible, since I need to retain my recursive object. @@ -90,47 +87,47 @@ class KPObjectSelector(QtGui.QListView): # even though all rows are retained. # # It's kind of like a custom QSortFilterProxyModel. Maybe I should subclass that. - + self.setCurrentIndex(self.model().index(sel, 0, QtCore.QModelIndex())) - + @QtCore.pyqtSlot(QtCore.QModelIndex, QtCore.QModelIndex) def currentChanged(self, current, previous): """Throws a signal when the selected object changed""" - self.ObjChanged.emit(current.row()) - - def HandleObjReplace(self, index): + self.objChanged.emit(current.row()) + + def handleObjReplace(self, index): """Throws a signal when the selected object is used as a replacement""" if QtGui.QApplication.keyboardModifiers() == QtCore.Qt.AltModifier: - self.ObjReplace.emit(index.row()) - - ObjChanged = QtCore.pyqtSignal(int) - ObjReplace = QtCore.pyqtSignal(int) - - + self.objReplaced.emit(index.row()) + + objChanged = QtCore.pyqtSignal(int) + objReplaced = QtCore.pyqtSignal(int) + + class KPGroupItemDelegate(QtGui.QAbstractItemDelegate): - """Handles tileset groups, objects and their rendering""" - - def __init__(self): - """Initialises the delegate""" - QtGui.QAbstractItemDelegate.__init__(self) - - def paint(self, painter, option, index): - """Paints an object""" - if option.state & QtGui.QStyle.State_Selected: - painter.fillRect(option.rect, option.palette.highlight()) - - p = index.model().data(index, QtCore.Qt.DecorationRole) - painter.drawPixmap(option.rect.x()+2, option.rect.y()+2, p) - #painter.drawText(option.rect, str(index.row())) - - def sizeHint(self, option, index): - """Returns the size for the object""" - p = index.model().data(index, QtCore.Qt.UserRole) - return p + """Handles tileset groups, objects and their rendering""" + + def __init__(self): + """Initialises the delegate""" + QtGui.QAbstractItemDelegate.__init__(self) + + def paint(self, painter, option, index): + """Paints an object""" + if option.state & QtGui.QStyle.State_Selected: + painter.fillRect(option.rect, option.palette.highlight()) + + p = index.model().data(index, QtCore.Qt.DecorationRole) + painter.drawPixmap(option.rect.x()+2, option.rect.y()+2, p) + #painter.drawText(option.rect, str(index.row())) + + def sizeHint(self, option, index): + """Returns the size for the object""" + p = index.model().data(index, QtCore.Qt.UserRole) + return p + + - - @@ -147,27 +144,36 @@ class KPMainWindow(QtGui.QMainWindow): self.setupMenuBar() self.setupDocks() - + def setupMenuBar(self): mb = self.menuBar() m = mb.addMenu('&File') # ... - + def setupDocks(self): self.layerList = KPLayerList() self.layerListDock = QtGui.QDockWidget('Layers') self.layerListDock.setWidget(self.layerList) - # self.objectSelector = KPObjectSelector() - # # self.objectSelector.setModel(SomeTileset.getModel()) - # self.objectSelectorDock = QtGui.QDockWidget('Layers') - # self.objectSelectorDock.setWidget(self.layerList) + self.objectSelector = KPObjectSelector() + # TODO: refactor this, this is just a bit of a hack for now + from wii.u8archive import WiiArchiveU8 + arc = WiiArchiveU8(open('/home/me/Dropbox/NEWERsmbw/Test2.arc', 'rb')) + img = arc.resolvePath('/BG_tex/Test2_tex.bin').data + obj = arc.resolvePath('/BG_unt/Test2.bin').data + objm = arc.resolvePath('/BG_unt/Test2_hd.bin').data + grp = arc.resolvePath('/BG_grp/Test2_grp.bin').data + TestTileset = KPTileset(img, obj, objm, grp) + self.objectSelector.setModel(TestTileset.getModel()) + self.objectSelectorDock = QtGui.QDockWidget('Objects') + self.objectSelectorDock.setWidget(self.objectSelector) self.addDockWidget(Qt.RightDockWidgetArea, self.layerListDock) + self.addDockWidget(Qt.RightDockWidgetArea, self.objectSelectorDock) diff --git a/src/wii/__init__.py b/src/wii/__init__.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3