from common import * TILE_SIZE = (24,24) MAP_SIZE_IN_TILES = (512,512) MAP_SIZE = (MAP_SIZE_IN_TILES[0] * TILE_SIZE[0], MAP_SIZE_IN_TILES[1] * TILE_SIZE[1]) class KPObject(object): def __init__(self): self.position = (0,0) self.size = (1,1) self.kind = 0 self.cache = [] self.tileset = None self.qtItem = None def updateCache(self): self.cache = KP.map.tilesets[self.tileset].objects[self.kind].render(self.size) class KPLayer(object): def __repr__(self): return "" % self.name def __init__(self): self.name = '' self.tileset = '' self.objects = [] self.visible = True self.cache = ['DUMMY_FLAG'] self.updateCache() def updateCache(self): if len(self.objects) == 0: if len(self.cache) != 0: self.cache = [] self.cacheBasePos = (0,0) self.cacheSize = (0,0) return x1, x2 = MAP_SIZE_IN_TILES[0] - 1, 0 y1, y2 = MAP_SIZE_IN_TILES[1] - 1, 0 for obj in self.objects: pos, size = obj.position, obj.size if pos[0] < x1: x1 = pos[0] if pos[1] < y1: y1 = pos[1] if (pos[0] + size[0]) > x2: x2 = pos[0] + size[0] if (pos[1] + size[1]) > y2: y2 = pos[1] + size[1] # create the cache # I was going to just resize it, but setting every tile to -1 # in Python would probably be slower than creating a new one ... size = (x2 - x1, y2 - y1) width, height = size cache = [[-1 for i in xrange(width)] for j in xrange(height)] self.cache = cache self.cacheBasePos = (x1, y1) self.cacheSize = size # now generate the thing for obj in self.objects: oX, oY = obj.position baseX = oX - x1 y = oY - y1 for row in obj.cache: destRow = cache[y] x = baseX for tile in row: destRow[x] = tile x += 1 y += 1 class KPNodeAction(object): def __init__(self): pass class KPNode(object): def __init__(self): self.position = (0,0) self.actions = [] class KPPathSegment(object): def __init__(self): self.start = None self.end = None self.animation = None # default class KPPath(object): def __init__(self): self.startNode = None self.endNode = None self.segments = [] class KPDoodad(object): def __init__(self): self.position = (0,0) self.angle = 0 self.scale = (0,0) class KPMap(object): def __init__(self): self.nextLayerNumber = 1 self.layers = [self.createNewLayer() for x in range(4)] self.layerModel = KPMap.LayerModel(self.layers) self.nodes = [] self.paths = [] self.doodads = [] self.tilesets = {} self.loadedTilesets = {} # TESTING CRAP import os.path if os.path.exists('/Users/Tempus'): self.tilesets['Test'] = {'path': '/Users/Tempus/Dropbox/NEWERsmbw/Test3.arc'} else: self.tilesets['Test'] = {'path': '/home/me/Dropbox/NEWERsmbw/Test3.arc'} self.reloadTileset('Test') # LAYERS class LayerModel(QtCore.QAbstractListModel): def __init__(self, layerList): QtCore.QAbstractListModel.__init__(self) self.list = layerList def headerData(self, section, orientation, role = Qt.DisplayRole): return 'Layer' def rowCount(self, parent): return len(self.list) def data(self, index, role = Qt.DisplayRole): try: if index.isValid(): layer = self.list[index.row()] if (role == Qt.DisplayRole or role == Qt.EditRole): return layer.name elif role == Qt.CheckStateRole: return (Qt.Checked if layer.visible else Qt.Unchecked) except IndexError: pass return QtCore.QVariant() def flags(self, index): if not index.isValid(): return Qt.ItemIsEnabled return Qt.ItemIsEditable | Qt.ItemIsUserCheckable \ | QtCore.QAbstractListModel.flags(self, index) def setData(self, index, value, role = Qt.EditRole): if index.isValid(): layer = self.list[index.row()] if role == Qt.EditRole: value = str(value.toString()) if len(value) > 0: layer.name = value self.dataChanged.emit(index, index) return True elif role == Qt.CheckStateRole: layer.visible = value.toBool() self.dataChanged.emit(index, index) return True return False def createNewLayer(self): layer = KPLayer() layer.name = "Layer %d" % self.nextLayerNumber self.nextLayerNumber += 1 return layer def appendLayer(self, layer): return self.insertLayer(len(self.layers), layer) def insertLayer(self, index, layer): self.layerModel.beginInsertRows(QtCore.QModelIndex(), index, index) self.layers.insert(index, layer) self.layerModel.endInsertRows() return index def moveLayer(self, fromIndex, toIndex): if fromIndex == toIndex: return if fromIndex < 0 or toIndex < 0: raise ValueError if fromIndex >= len(self.layers) or toIndex > len(self.layers): raise ValueError self.layerModel.beginMoveRows( QtCore.QModelIndex(), fromIndex, fromIndex, QtCore.QModelIndex(), toIndex) toMove = self.layers[fromIndex] newIndex = ((toIndex > fromIndex) and (toIndex - 1)) or toIndex del self.layers[fromIndex] self.layers.insert(newIndex, toMove) self.layerModel.endMoveRows() return newIndex def removeLayer(self, layer): if layer not in self.layers: raise ValueError index = self.layers.index(layer) self.layerModel.beginRemoveRows(QtCore.QModelIndex(), index, index) del self.layers[index] self.layerModel.endRemoveRows() def reloadTileset(self, name): info = self.tilesets[name] self.loadedTilesets[name] = KPTileset.loadFromArc(info['path']) # TODO: handle relative paths properly in relation to the map file # TODO: add some sort of callback/signal to regenerate objects/layers # and refresh the view