From ffc9daf57c6a3ad8377c82d811cb3bcad2421bea Mon Sep 17 00:00:00 2001 From: Treeki Date: Mon, 14 Nov 2011 22:32:47 +0100 Subject: created a generic KPLayer class --- src/editorui.py | 67 +++++++++++++++++++++++++++++---------------------------- src/mapdata.py | 24 ++++++++++++++++----- 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/editorui.py b/src/editorui.py index 05cb77f..7969100 100644 --- a/src/editorui.py +++ b/src/editorui.py @@ -262,50 +262,51 @@ class KPMapScene(QtGui.QGraphicsScene): for layer in reversed(KP.map.layers): if not layer.visible: continue - left, top = layer.cacheBasePos - width, height = layer.cacheSize - right, bottom = left+width, top+height + if isinstance(layer, KPTileLayer): + left, top = layer.cacheBasePos + width, height = layer.cacheSize + right, bottom = left+width, top+height - if width == 0 and height == 0: continue + if width == 0 and height == 0: continue - if right < areaLeftT: continue - if left > areaRightT: continue + if right < areaLeftT: continue + if left > areaRightT: continue - if bottom < areaTopT: continue - if top > areaBottomT: continue + if bottom < areaTopT: continue + if top > areaBottomT: continue - # decide how much of the layer we'll actually draw - drawLeft = int(max(areaLeftT, left)) - drawRight = int(min(areaRightT, right)) + # decide how much of the layer we'll actually draw + drawLeft = int(max(areaLeftT, left)) + drawRight = int(min(areaRightT, right)) - drawTop = int(max(areaTopT, top)) - drawBottom = int(min(areaBottomT, bottom)) + drawTop = int(max(areaTopT, top)) + drawBottom = int(min(areaBottomT, bottom)) - srcY = drawTop - top - destY = drawTop * 24 + srcY = drawTop - top + destY = drawTop * 24 - baseSrcX = drawLeft - left - baseDestX = drawLeft * 24 + baseSrcX = drawLeft - left + baseDestX = drawLeft * 24 - rows = layer.cache - tileset = KP.map.loadedTilesets[layer.tileset] - tileList = tileset.tiles + rows = layer.cache + tileset = KP.map.loadedTilesets[layer.tileset] + tileList = tileset.tiles - for y in xrange(drawTop, drawBottom): - srcX = baseSrcX - destX = baseDestX - row = rows[srcY] + for y in xrange(drawTop, drawBottom): + srcX = baseSrcX + destX = baseDestX + row = rows[srcY] - for x in xrange(drawLeft, drawRight): - tile = row[srcX] - if tile != -1: - painter.drawPixmap(destX, destY, tileList[tile]) - - srcX += 1 - destX += 24 + for x in xrange(drawLeft, drawRight): + tile = row[srcX] + if tile != -1: + painter.drawPixmap(destX, destY, tileList[tile]) + + srcX += 1 + destX += 24 - srcY += 1 - destY += 24 + srcY += 1 + destY += 24 def setCurrentLayer(self, layer): diff --git a/src/mapdata.py b/src/mapdata.py index 3ce63a6..2f1f5a2 100644 --- a/src/mapdata.py +++ b/src/mapdata.py @@ -23,11 +23,7 @@ class KPLayer(object): def __init__(self): self.name = '' - self.tileset = '' - self.objects = [] self._visible = True - self.cache = ['DUMMY_FLAG'] - self.updateCache() @property def visible(self): @@ -39,6 +35,24 @@ class KPLayer(object): return self._visible = value + self._visibilityChanged(value) + + def _visibilityChanged(self, value): + pass + + +class KPTileLayer(KPLayer): + def __repr__(self): + return "" % (self.name, self.tileset) + + def __init__(self): + KPLayer.__init__(self) + self.tileset = '' + self.objects = [] + self.cache = ['DUMMY_FLAG'] + self.updateCache() + + def _visibilityChanged(self, value): for obj in self.objects: item = obj.qtItem if item: @@ -207,7 +221,7 @@ class KPMap(object): def createNewLayer(self, tilesetName): - layer = KPLayer() + layer = KPTileLayer() layer.name = "Layer %d" % self.nextLayerNumber self.nextLayerNumber += 1 layer.tileset = tilesetName -- cgit v1.2.3