From 2fda919ba6e30b04de8334404ad34d1a42254c7e Mon Sep 17 00:00:00 2001 From: Treeki Date: Sat, 12 Nov 2011 03:52:54 +0100 Subject: added resizing while painting objects and fixed a few bugs --- src/editorui.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/mapdata.py | 2 +- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/editorui.py b/src/editorui.py index 557fe54..1d468c6 100644 --- a/src/editorui.py +++ b/src/editorui.py @@ -30,7 +30,6 @@ class KPEditorItem(QtGui.QGraphicsItem): if y > 12264: y = 12264 if x != currentX or y != currentY: - print "Position change" self._itemMoved(currentX, currentY, x, y) newpos.setX(x) @@ -54,6 +53,10 @@ class KPEditorObject(KPEditorItem): self._layerRef = weakref.ref(layer) self._updatePosition() self._updateSize() + + # I don't bother setting the ZValue because it doesn't quite matter: + # only one layer's objects are ever clickable, and drawBackground takes + # care of the layered drawing def _updatePosition(self): obj = self._objRef() @@ -70,6 +73,7 @@ class KPEditorObject(KPEditorItem): self._boundingRect = QtCore.QRectF(0, 0, w*24, h*24) self._selectionRect = QtCore.QRectF(0, 0, w*24-1, h*24-1) + def paint(self, painter, option, widget): if self.isSelected(): painter.setPen(QtGui.QPen(Qt.white, 1, Qt.DotLine)) @@ -103,7 +107,7 @@ class KPMapScene(QtGui.QGraphicsScene): areaRightT = ceil(areaRight / 24) areaBottomT = ceil(areaBottom / 24) - for layer in KP.map.layers: + for layer in reversed(KP.map.layers): left, top = layer.cacheBasePos width, height = layer.cacheSize right, bottom = left+width, top+height @@ -153,9 +157,11 @@ class KPMapScene(QtGui.QGraphicsScene): def setCurrentLayer(self, layer): if self.currentLayer is not None: self.setLayerObjectsFlag(self.currentLayer, QtGui.QGraphicsItem.ItemIsSelectable, False) + self.setLayerObjectsFlag(self.currentLayer, QtGui.QGraphicsItem.ItemIsMovable, False) self.currentLayer = layer self.setLayerObjectsFlag(layer, QtGui.QGraphicsItem.ItemIsSelectable, True) + self.setLayerObjectsFlag(layer, QtGui.QGraphicsItem.ItemIsMovable, True) def setLayerObjectsFlag(self, layer, flag, value): @@ -189,6 +195,7 @@ class KPEditorWidget(QtGui.QGraphicsView): def _resetPaintVars(self): self.painting = None self.paintingItem = None + self.paintBeginPosition = None def _tryToPaint(self, event): '''Called when a paint attempt is initiated''' @@ -211,7 +218,7 @@ class KPEditorWidget(QtGui.QGraphicsView): obj.position = (x,y) obj.size = (1,1) obj.tileset = layer.tileset - obj.kind = self.paintNextID + obj.kind = paint obj.updateCache() layer.objects.append(obj) layer.updateCache() @@ -221,9 +228,60 @@ class KPEditorWidget(QtGui.QGraphicsView): self.painting = obj self.paintingItem = item + self.paintBeginPosition = (x, y) + def _movedWhilePainting(self, event): + '''Called when the mouse is moved while painting something''' + obj = self.painting + item = self.paintingItem + + if isinstance(obj, KPObject): + clicked = self.mapToScene(event.x(), event.y()) + x, y = clicked.x(), clicked.y() + if x < 0: x = 0 + if y < 0: y = 0 + x = int(x / 24) + y = int(y / 24) + + beginX, beginY = self.paintBeginPosition + + if x >= beginX: + objX = beginX + width = x - beginX + 1 + else: + objX = x + width = beginX - x + 1 + + if y >= beginY: + objY = beginY + height = y - beginY + 1 + else: + objY = y + height = beginY - y + 1 + + currentX, currentY = obj.position + currentWidth, currentHeight = obj.size + + # update everything if changed + changed = False + + if currentX != objX or currentY != objY: + obj.position = (objX, objY) + item._updatePosition() + changed = True + + if currentWidth != width or currentHeight != height: + obj.size = (width, height) + obj.updateCache() + item._updateSize() + changed = True + + if not changed: return + + item._layerRef().updateCache() + def mousePressEvent(self, event): if event.button() == Qt.RightButton: self._tryToPaint(event) @@ -231,6 +289,14 @@ class KPEditorWidget(QtGui.QGraphicsView): else: QtGui.QGraphicsView.mousePressEvent(self, event) + + def mouseMoveEvent(self, event): + if event.buttons() == Qt.RightButton and self.painting: + self._movedWhilePainting(event) + event.accept() + + else: + QtGui.QGraphicsView.mouseMoveEvent(self, event) diff --git a/src/mapdata.py b/src/mapdata.py index f8ededd..67103d9 100644 --- a/src/mapdata.py +++ b/src/mapdata.py @@ -14,7 +14,7 @@ class KPObject(object): self.qtItem = None def updateCache(self): - self.cache = KP.map.loadedTilesets[self.tileset].objects[self.kind].render(self.size) + self.cache = self.kind.render(self.size) class KPLayer(object): -- cgit v1.2.3