summaryrefslogtreecommitdiff
path: root/src/editorui/doodads.py
blob: 5a02248c4faf9adaea5f162e6693633880c40f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from common import *
from editorcommon import *
import weakref
from math import floor, ceil
import math


class KPEditorDoodad(KPEditorItem):
	SNAP_TO = (12,12)

	def __init__(self, doodad, layer):
		KPEditorItem.__init__(self)

		doodad.qtItem = self
		self._doodadRef = weakref.ref(doodad)
		self._layerRef = weakref.ref(layer)

		# TODO: refactor this to store source doodad data under KP.map
		sourceItem = KP.mainWindow.doodadSelector.getDoodad(doodad.index)
		self._sourceRef = weakref.ref(sourceItem)

		self.resizing = None
		self.rotating = None

		self._updatePixmap()
		self._updatePosition()
		self._updateSize()

		self.setAcceptHoverEvents(True)

		if not hasattr(KPEditorDoodad, 'SELECTION_PEN'):
			KPEditorDoodad.SELECTION_PEN = QtGui.QPen(Qt.red, 1, Qt.DotLine)



	def _updatePixmap(self):
		source = self._sourceRef()
		pixmap = source.icon().pixmap(source.icon().availableSizes()[0])

		self.prepareGeometryChange()
		w, h = pixmap.width(), pixmap.height()

		self.pixmap = pixmap


	def _updatePosition(self):
		# NOTE: EditorDoodads originate at the centre, not the top left like the others
		doodad = self._doodadRef()
		x,y = doodad.position
		w,h = doodad.size
		self.setPos(x+floor(w/2.0), y+floor(h/2.0))


	def _updateSize(self):
		self.prepareGeometryChange()

		w,h = self._doodadRef().size

		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()

		self.setRotation(doodad.angle)


	def paint(self, painter, option, widget):
		if self.isSelected():
			painter.setPen(self.SELECTION_PEN)
			painter.drawRect(self._selectionRect)
	

	def _itemMoved(self, oldX, oldY, newX, newY):
		doodad = self._doodadRef()
		w,h = doodad.size
		doodad.position = [newX-floor(w/2.0), newY-floor(h/2.0)]


	def hoverMoveEvent(self, event):
		if self._layerRef() != KP.mapScene.currentLayer:
			self.setCursor(Qt.ArrowCursor)
			return

		pos = event.pos()
		bit = self.resizerPortionAt(pos.x(), pos.y())


		if (event.modifiers() == Qt.ShiftModifier):

			if bit:
				self.setCursor(Qt.OpenHandCursor)
			else:
				self.setCursor(Qt.ArrowCursor)

		else:

			if bit == 1 or bit == 4:
				self.setCursor(Qt.SizeFDiagCursor)
			elif bit == 2 or bit == 3:
				self.setCursor(Qt.SizeBDiagCursor)
			elif bit == 7 or bit == 8:
				self.setCursor(Qt.SizeHorCursor)
			elif bit == 5 or bit == 6:
				self.setCursor(Qt.SizeVerCursor)
			else:
				self.setCursor(Qt.ArrowCursor)
	

	def mousePressEvent(self, event):
		if event.button() == Qt.LeftButton:
			pos = event.pos()
			bit = self.resizerPortionAt(pos.x(), pos.y())

			if self._layerRef() == KP.mapScene.currentLayer and bit:
				event.accept()

				if (event.modifiers() & Qt.ShiftModifier):
					self.rotating = self.mapToScene(pos), self._doodadRef().angle
					self.setCursor(Qt.ClosedHandCursor)
					return
				
				else:
					x, xSide, y, ySide = False, None, False, None
			
					if bit == 1 or bit == 7 or bit == 3: # left
						x, xSide = True, 1

					elif bit == 2 or bit == 4 or bit == 8: # right
						x, xSide = True, 0

					if bit == 1 or bit == 2 or bit == 5: # top
						y, ySide = True, 1

					elif bit == 3 or bit == 4 or bit == 6: # bottom
						y, ySide = True, 0

					self._updateSize()
					self.resizing = (x, xSide, y, ySide)
					return


		KPEditorItem.mousePressEvent(self, event)
	

	def _tryAndResize(self, obj, axisIndex, mousePosition, stationarySide):

		newSize = abs(mousePosition) * 2

		if newSize < 10:
			return False
	
		obj.size[axisIndex] = newSize
		
		return True


	def _tryAndRotate(self, obj, mouseX, mouseY, originalPos, oldAngle, modifiers):
		center = self.mapToScene(self.boundingRect().center())

		objX = center.x()
		objY = center.y()


		origX = originalPos.x()
		origY = originalPos.y()

		dy = origY - objY
		dx = origX - objX
		rads = math.atan2(dy, dx)

		origAngle = math.degrees(rads)

		dy = mouseY - objY
		dx = mouseX - objX
		rads = math.atan2(dy, dx)

		angle = math.degrees(rads)

		
		# Move this to ItemChange() or something at some point.
		finalAngle = angle - origAngle + oldAngle

		if (modifiers & Qt.ControlModifier):
			finalAngle = int(finalAngle / 45.0) * 45.0

		return True, finalAngle


	def mouseMoveEvent(self, event):
		if self.resizing:
			obj = self._doodadRef()

			hasChanged = False
			resizeX, xSide, resizeY, ySide = self.resizing

			if resizeX:
				hasChanged |= self._tryAndResize(obj, 0, event.pos().x(), xSide)
			if resizeY:
				hasChanged |= self._tryAndResize(obj, 1, event.pos().y(), ySide)

			if hasChanged:
				# Doodads aren't supposed to snap, they're all free flowing like the wind.
				self._updateSize()


		elif self.rotating:
			obj = self._doodadRef()
			scenePos = event.scenePos()
			self.setTransformOriginPoint(self.boundingRect().center())

			hasChanged = False

			hasChanged, angle = self._tryAndRotate(obj, scenePos.x(), scenePos.y(), self.rotating[0], self.rotating[1], event.modifiers())

			if hasChanged:
				obj.angle = angle
				self._updateTransform()


		else:
			KPEditorItem.mouseMoveEvent(self, event)
	

	def mouseReleaseEvent(self, event):
		if self.resizing and event.button() == Qt.LeftButton:
			self.resizing = None
			# self._doodadRef().position = [self.x(), self.y()]

		elif self.rotating and event.button() == Qt.LeftButton:
			self.rotating = None

		else:
			KPEditorItem.mouseReleaseEvent(self, event)


	def remove(self, withItem=False):
		doodad = self._doodadRef()
		layer = self._layerRef()

		layer.objects.remove(doodad)

		if withItem:
			self.scene().removeItem(self)