summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/editorui.py157
-rw-r--r--src/ui.py10
2 files changed, 112 insertions, 55 deletions
diff --git a/src/editorui.py b/src/editorui.py
index 98d57f2..505af08 100644
--- a/src/editorui.py
+++ b/src/editorui.py
@@ -13,20 +13,24 @@ class KPEditorItem(QtGui.QGraphicsItem):
self.ItemIsMovable
)
+ self.ignoreMovement = False
self.overrideSnap = False
def itemChange(self, change, value):
- if change == self.ItemPositionChange and not self.overrideSnap:
+ if change == self.ItemPositionChange and not self.ignoreMovement:
currentX, currentY = self.x(), self.y()
- # snap the item
- snapX, snapY = self.SNAP_TO
newpos = value.toPyObject()
x, y = newpos.x(), newpos.y()
- x = int((x + (snapX/2)) / snapX) * snapX
- y = int((y + (snapY/2)) / snapY) * snapY
+ if self.overrideSnap:
+ snapX, snapY = 1, 1
+ else:
+ # snap the item
+ snapX, snapY = self.SNAP_TO
+ x = int((x + (snapX/2)) / snapX) * snapX
+ y = int((y + (snapY/2)) / snapY) * snapY
if x < 0: x = 0
if x >= (12288+snapX): x = (12288+snapX-1)
@@ -55,25 +59,28 @@ class KPEditorItem(QtGui.QGraphicsItem):
RESIZER_LEFT = 7
RESIZER_RIGHT = 8
- def resizerPortionAt(self, x, y):
+ def resizerPortionAt(self, x, y, originX=0, originY=0):
try:
+ leftBound, topBound = originX+5, originY+5
rightBound, bottomBound = self._resizerEndXY
except AttributeError:
rect = self._boundingRect
+ leftBound, topBound = rect.x() + 5, rect.y() + 5
rightBound, bottomBound = rect.right() - 5, rect.bottom() - 5
- if y < 5:
- if x < 5: return 1 # TOP_LEFT
+ print "Testing %f,%f against (%f,%f) - (%f,%f)" % (x, y, leftBound, topBound, rightBound, bottomBound)
+ if y < topBound:
+ if x < leftBound: return 1 # TOP_LEFT
elif x >= rightBound: return 2 # TOP_RIGHT
else: return 5 # TOP
elif y >= bottomBound:
- if x < 5: return 3 # BOTTOM_LEFT
+ if x < leftBound: return 3 # BOTTOM_LEFT
elif x >= rightBound: return 4 # BOTTOM_RIGHT
else: return 6 # BOTTOM
else:
- if x < 5: return 7 # LEFT
+ if x < leftBound: return 7 # LEFT
elif x >= rightBound: return 8 # RIGHT
else: return None
@@ -102,8 +109,10 @@ class KPEditorObject(KPEditorItem):
# care of the layered drawing
def _updatePosition(self):
+ self.ignoreMovement = True
x,y = self._objRef().position
self.setPos(x*24, y*24)
+ self.ignoreMovement = False
def _updateSize(self):
self.prepareGeometryChange()
@@ -225,14 +234,10 @@ class KPEditorObject(KPEditorItem):
hasChanged |= self._tryAndResize(obj, 1, int(scenePos.y() / 24), ySide)
if hasChanged:
- # TODO: move overrides into updatePosition?
- # TODO: snapping for resizing
- self.overrideSnap = True
obj.updateCache()
self._layerRef().updateCache()
self._updatePosition()
self._updateSize()
- self.overrideSnap = False
else:
KPEditorItem.mouseMoveEvent(self, event)
@@ -286,6 +291,9 @@ class KPEditorDoodad(KPEditorItem):
self._updateSize()
self._updateTransform()
+ global BULLSHIT
+ BULLSHIT = self.bullshit
+
self.setAcceptHoverEvents(True)
@@ -299,31 +307,48 @@ class KPEditorDoodad(KPEditorItem):
self.pixmap = pixmap
def _updatePosition(self):
- x,y = self._doodadRef().position
- self.setPos(x, y)
+ # NOTE: EditorDoodads originate at the centre, not the top left like the others
+ self.ignoreMovement = True
+ doodad = self._doodadRef()
+ x,y = doodad.position
+ w,h = doodad.size
+ self.setPos(x+floor(w/2.0), y+floor(h/2.0))
+ self.ignoreMovement = False
def _updateSize(self):
self.prepareGeometryChange()
w,h = self._doodadRef().size
- self.setTransformOriginPoint(w/2, h/2)
- self._boundingRect = QtCore.QRectF(0, 0, w, h)
- self._selectionRect = QtCore.QRectF(0, 0, w-1, h-1)
-
- self._resizerEndXY = (w-5, h-5)
+ #self.setTransformOriginPoint(w/2, h/2)
+ self._boundingRect = QtCore.QRectF(-w/2, -h/2, w, h)
+ self._selectionRect = self._boundingRect.adjusted(0, 0, -1, -1)
def _updateTransform(self):
doodad = self._doodadRef()
- pixmap = self.pixmap
- displaceX, displaceY = pixmap.width()/2, pixmap.height()/2
transform = QtGui.QTransform()
- transform.translate(displaceX, displaceY)
transform.rotate(doodad.angle)
- transform.translate(-displaceX, -displaceY)
self.setTransform(transform)
+ itransform = QtGui.QTransform()
+ itransform.rotate(-doodad.angle)
+ self.invertedTransform = itransform
+
+ print "----"
+ self._pt(transform)
+ print "~~"
+ self._pt(itransform)
+ #self.invertedTransform, invertible = transform.inverted()
+ #self.setRotation(doodad.angle)
+ #self.invertedTransform, invertible = self.transform().inverted()
+
+ def _pt(self, t):
+ print t.m11(), t.m12(), t.m13()
+ print t.m21(), t.m22(), t.m23()
+ print t.m31(), t.m32(), t.m33()
+
+
def paint(self, painter, option, widget):
if self.isSelected():
@@ -333,7 +358,9 @@ class KPEditorDoodad(KPEditorItem):
def _itemMoved(self, oldX, oldY, newX, newY):
doodad = self._doodadRef()
- doodad.position = (newX, newY)
+ w,h = doodad.size
+ print "Moved to", newX, newY, "size", w, h
+ doodad.position = (newX-floor(w/2.0), newY-floor(h/2.0))
def hoverMoveEvent(self, event):
@@ -393,6 +420,7 @@ class KPEditorDoodad(KPEditorItem):
y, ySide = True, 0
self.resizing = (x, xSide, y, ySide)
+ print self.resizing
return
@@ -403,38 +431,31 @@ class KPEditorDoodad(KPEditorItem):
objPosition = obj.position[axisIndex]
objSize = obj.size[axisIndex]
+ currentCentre = objPosition + (objSize / 2)
- if stationarySide == 0:
- # Resize the right/bottom side
- relativeMousePosition = mousePosition - objPosition
- newSize = relativeMousePosition + 1
- if newSize == objSize or newSize < 1:
- return False
-
- if axisIndex == 1:
- obj.size = (obj.size[0], newSize)
- else:
- obj.size = (newSize, obj.size[1])
+ lowerSide = objPosition
+ upperSide = lowerSide + objSize
+ if stationarySide == 0:
+ upperSide = mousePosition
else:
- # Resize the left/top side
- rightSide = objPosition + objSize - 1
- newLeftSide = mousePosition
+ lowerSide = mousePosition
- newPosition = newLeftSide
- newSize = rightSide - newLeftSide + 1
+ newSize = upperSide - lowerSide
+ newPosition = lowerSide
+ #newPosition = currentCentre - (newSize / 2)
- if newSize < 1:
- return False
- if newPosition == objPosition and newSize == objSize:
- return False
+ if newPosition == objPosition and newSize == objSize:
+ return False
+ elif newSize < 1:
+ return False
- if axisIndex == 1:
- obj.position = (obj.position[0], newPosition)
- obj.size = (obj.size[0], newSize)
- else:
- obj.position = (newPosition, obj.position[1])
- obj.size = (newSize, obj.size[1])
+ if axisIndex == 1:
+ obj.position = (obj.position[0], newPosition)
+ obj.size = (obj.size[0], newSize)
+ else:
+ obj.position = (newPosition, obj.position[1])
+ obj.size = (newSize, obj.size[1])
return True
@@ -475,18 +496,35 @@ class KPEditorDoodad(KPEditorItem):
if self.resizing:
obj = self._doodadRef()
scenePos = event.scenePos()
+ #mappedPos = self.transform().map(scenePos)
+ #mappedPos = self.invertedTransform.map(scenePos)
+
+ x = event.pos().x() + self.x()
+ y = event.pos().y() + self.y()
+ mappedPos = QtCore.QPointF(x, y)
hasChanged = False
resizeX, xSide, resizeY, ySide = self.resizing
+ print "Scene Position: %f,%f" % (scenePos.x(), scenePos.y())
+ print "Mapped Position: %f,%f" % (mappedPos.x(), mappedPos.y())
+ print "Current X/Y: %r; Size: %r" % (obj.position, obj.size)
+
if resizeX:
- hasChanged |= self._tryAndResize(obj, 0, scenePos.x(), xSide)
+ hasChanged |= self._tryAndResize(obj, 0, mappedPos.x(), xSide)
if resizeY:
- hasChanged |= self._tryAndResize(obj, 1, scenePos.y(), ySide)
+ hasChanged |= self._tryAndResize(obj, 1, mappedPos.y(), ySide)
+ print "New X/Y: %r; Size: %r" % (obj.position, obj.size)
if hasChanged:
+ # TODO: move overrides into updatePosition?
+ # TODO: snapping for resizing
+ self.overrideSnap = True
self._updatePosition()
self._updateSize()
+ self._updateTransform()
+ self.overrideSnap = False
+ print "New Rect: (%f,%f) %f x %f" % (self._boundingRect.x(), self._boundingRect.y(), self._boundingRect.width(), self._boundingRect.height())
elif self.rotating:
@@ -506,6 +544,11 @@ class KPEditorDoodad(KPEditorItem):
KPEditorItem.mouseMoveEvent(self, event)
+ def bullshit(self):
+ obj = self._doodadRef()
+ print "Current X/Y: %r; Size: %r" % (obj.position, obj.size)
+
+
def mouseReleaseEvent(self, event):
if self.resizing and event.button() == Qt.LeftButton:
self.resizing = None
@@ -661,12 +704,16 @@ class KPMapScene(QtGui.QGraphicsScene):
except KeyError:
continue
+ painter.setPen(Qt.red)
for item in reversed(toDraw):
painter.save()
painter.setWorldTransform(item.sceneTransform(), True)
w, h = item._doodadRef().size
- painter.drawPixmap(0, 0, w, h, item.pixmap)
+ painter.drawPixmap(-w/2, -h/2, w, h, item.pixmap)
painter.restore()
+ x, y = item._doodadRef().position
+ #painter.drawPoint(x, y)
+ painter.drawRect(x, y, w, h)
elif isinstance(layer, KPTileLayer):
left, top = layer.cacheBasePos
diff --git a/src/ui.py b/src/ui.py
index a6d1516..af5dbda 100644
--- a/src/ui.py
+++ b/src/ui.py
@@ -34,8 +34,13 @@ class KPLayerList(QtGui.QWidget):
self.actRemove = tb.addAction(QtGui.QIcon(), 'Remove', self.removeLayer)
self.actMoveUp = tb.addAction(QtGui.QIcon(), 'Move Up', self.moveUp)
self.actMoveDown = tb.addAction(QtGui.QIcon(), 'Move Down', self.moveDown)
+ self.actBullshit = tb.addAction(QtGui.QIcon(), 'BULLSHIT', self.bullshit)
+ def bullshit(self):
+ import editorui
+ editorui.BULLSHIT()
+
def setButtonStates(self):
index = self.selectedLayerIndex()
@@ -140,6 +145,11 @@ class KPDoodadSelector(QtGui.QWidget):
self.doodadList.currentItemChanged.connect(self.handleRowChanged)
+ # TODO: REMOVE THIS!!!
+ pm = QtGui.QPixmap()
+ pm.load('/home/me/Dropbox/NEWERsmbw/BildOlPipeThingy2x.png')
+ self.addDoodad(pm, 'Test Doodad')
+
def keyPressEvent(self, event):