summaryrefslogtreecommitdiff
path: root/src/editorui/editormain.py
blob: 09bc4a3e8bebaf09ab690a4d202553a607fd642d (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
from common import *
from math import floor, ceil

from objects import *
from doodads import *
from paths import *

class KPMapScene(QtGui.QGraphicsScene):
	def __init__(self):
		QtGui.QGraphicsScene.__init__(self, 0, 0, 512*24, 512*24)
		
		# todo: handle selectionChanged
		# todo: look up why I used setItemIndexMethod(self.NoIndex) in Reggie

		self.currentLayer = None
		KP.mapScene = self
		self.playing = False
		self.timeLines = []
		self.ticker = QtCore.QTimeLine(100000)
		self.ticker.setLoopCount(10)
		self.ticker.setCurveShape(4)
		self.ticker.setFrameRange(0,100000)
		self.ticker.valueChanged.connect(self.thing)
		self.ticker.setUpdateInterval


	def playPause(self):
		if self.playing == False:
			self.playing = True
			self.views()[0].setViewportUpdateMode(0)
			self.ticker.start()

			for timeline in self.timeLines:
				timeline.start()
			self.views()[0].viewport().update()
		
		else:
			self.playing = False
			self.views()[0].setViewportUpdateMode(1)
			self.ticker.stop()

			for timeline in self.timeLines:
				timeline.stop()
			self.views()[0].viewport().update()


	@QtCore.pyqtSlot(int)
	def thing(self, value):
		self.views()[0].viewport().update()


	def drawBackground(self, painter, rect):
		painter.fillRect(rect, QtGui.QColor(209, 218, 236))

		areaLeft, areaTop = rect.x(), rect.y()
		areaWidth, areaHeight = rect.width(), rect.height()
		areaRight, areaBottom = areaLeft+areaWidth, areaTop+areaHeight

		areaLeftT = floor(areaLeft / 24)
		areaTopT = floor(areaTop / 24)
		areaRightT = ceil(areaRight / 24)
		areaBottomT = ceil(areaBottom / 24)

		# compile a list of doodads
		visibleDoodadsByLayer = {}

		for obj in self.items(rect):
			if not isinstance(obj, KPEditorDoodad): continue

			layer = obj._layerRef()

			try:
				doodadList = visibleDoodadsByLayer[layer]
			except KeyError:
				doodadList = []
				visibleDoodadsByLayer[layer] = doodadList

			doodadList.append(obj)

		# now draw everything!
		for layer in reversed(KP.map.layers):
			if not layer.visible: continue

			if isinstance(layer, KPDoodadLayer):
				try:
					toDraw = visibleDoodadsByLayer[layer]
				except KeyError:
					continue

				for item in reversed(toDraw):
					
					painter.save()

					if self.playing == False:
						painter.setWorldTransform(item.sceneTransform(), True)
						p = item._boundingRect
						painter.drawPixmap(p.x(), p.y(), p.width(), p.height(), item.pixmap)

					else:
						self.animateDoodad(painter, item)
					painter.restore()


			elif 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 right < areaLeftT: continue
				if left > areaRightT: 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))

				drawTop = int(max(areaTopT, top))
				drawBottom = int(min(areaBottomT, bottom))

				srcY = drawTop - top
				destY = drawTop * 24

				baseSrcX = drawLeft - left
				baseDestX = drawLeft * 24

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

	def animateDoodad(self, painter, item):

		doodad = item._doodadRef()
		animations = doodad.animations

		transform = item.sceneTransform()
		posRect = item._boundingRect.adjusted(0,0,0,0)

		# Anm indexes are Looping, Interpolation, Frame Len, Type, Start Value, End Value
		#
		# Anm Loops are Contiguous, Loop, Reversible Loop
		# Anm Interpolations are Linear, Sinusoidial, Cosinoidial
		# Anm Types are X Position, Y Position, Angle, X Scale, Y Scale, Opacity

		for anm in animations:

			Type = anm[3]
			Timeline = anm[6]
		
			modifier = Timeline.currentFrame()

			if Type == "X Position":
				posRect.adjust(modifier, 0, modifier, 0)

			elif Type == "Y Position":
				posRect.adjust(0, modifier, 0, modifier)

			elif Type == "Angle":
				transform.rotate(modifier)

			elif Type == "X Scale":
				posRect.setWidth(posRect.width()*modifier/100.0)
		
			elif Type == "Y Scale":
				posRect.setHeight(posRect.height()*modifier/100.0)

			elif Type == "Opacity":
				painter.setOpacity(modifier/100.0)

		painter.setWorldTransform(transform, True)
		painter.drawPixmap(posRect.x(), posRect.y(), posRect.width(), posRect.height(), item.pixmap)


	def setCurrentLayer(self, layer):
		if self.currentLayer is not None:
			self.currentLayer.setActivated(False)

		self.currentLayer = layer
		self.currentLayer.setActivated(True)


class KPEditorWidget(QtGui.QGraphicsView):
	def __init__(self, scene, parent=None):
		QtGui.QGraphicsView.__init__(self, scene, parent)

		self.setRenderHints(QtGui.QPainter.Antialiasing)

		self.setAlignment(Qt.AlignLeft | Qt.AlignTop)
		self.setDragMode(self.RubberBandDrag)
		
		self.xScrollBar = QtGui.QScrollBar(Qt.Horizontal, parent)
		self.setHorizontalScrollBar(self.xScrollBar)
		
		self.yScrollBar = QtGui.QScrollBar(Qt.Vertical, parent)
		self.setVerticalScrollBar(self.yScrollBar)

		self.centerOn(0,0)

		# set up stuff for painting
		self.paintNext = None
		self.paintNextID = None
		self._resetPaintVars()
	
	def _resetPaintVars(self):
		self.painting = None
		self.paintingItem = None
		self.paintBeginPosition = None
	
	def _tryToPaint(self, event):
		'''Called when a paint attempt is initiated'''

		paint = self.paintNext
		layer = self.scene().currentLayer
		if not layer.visible: return

		if isinstance(layer, KPTileLayer):
			if paint is None: return

			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)

			obj = KPObject()
			obj.position = (x,y)
			obj.size = (1,1)
			obj.tileset = layer.tileset
			obj.kind = paint
			obj.updateCache()
			layer.objects.append(obj)
			layer.updateCache()

			item = KPEditorObject(obj, layer)
			self.scene().addItem(item)

			self.painting = obj
			self.paintingItem = item
			self.paintBeginPosition = (x, y)

		elif isinstance(layer, KPDoodadLayer):
			if paint is None: return

			clicked = self.mapToScene(event.x(), event.y())
			x, y = clicked.x(), clicked.y()
			if x < 0: x = 0
			if y < 0: y = 0

			obj = KPDoodad()
			obj.position = [x,y]
			obj.index = self.paintNextID
			obj.setDefaultSize()
			layer.objects.append(obj)

			item = KPEditorDoodad(obj, layer)
			self.scene().addItem(item)

			self.painting = obj
			self.paintingItem = item
			self.paintBeginPosition = (x, y)

		elif isinstance(layer, KPPathLayer):
			# decide what's under the mouse
			clicked = self.mapToScene(event.x(), event.y())
			x, y = clicked.x(), clicked.y()
			itemsUnder = self.scene().items(clicked)

			for item in itemsUnder:
				if isinstance(item, KPEditorNode):
					# Paint a path to this node (if one is selected)
					sourceItem, sourceNode = None, None
					selected = self.scene().selectedItems()

					for selItem in selected:
						if isinstance(item, KPEditorNode) and selItem != item:
							sourceItem = selItem
							sourceNode = selItem._nodeRef()
							break

					if sourceItem is None: return

					# Make sure that no path already exists between these nodes
					destNode = item._nodeRef()

					for pathToCheck in sourceNode.exits:
						if pathToCheck._startNodeRef() == destNode:
							return
						if pathToCheck._endNodeRef() == destNode:
							return

					# No node can have more than four paths, because there are only
					# four directions registered by a Wiimote DPad.

					if len(sourceNode.exits) > 3:
						return

					if len(destNode.exits) > 3:
						return

					path = KPPath(sourceNode, destNode)

					KP.map.pathLayer.paths.append(path)

					item = KPEditorPath(path)
					self.scene().addItem(item)

					return

				elif isinstance(item, KPEditorPath):
					# Split this path into two... at this point

					origPath = item._pathRef()

					node = KPNode()
					node.position = (x - 12, y - 12)
					KP.map.pathLayer.nodes.append(node)

					# Start node => Original path => New node => New path => End node

					endNode = origPath._endNodeRef()

					origPath.setEnd(node)
					
					nodeItem = KPEditorNode(node)
					self.scene().addItem(nodeItem)

					# TODO: fix this ugly bit of code
					item._endNodeRef = weakref.ref(nodeItem)
					item.updatePosition()

					self.painting = node
					self.paintingItem = item
					self.paintBeginPosition = (x - 12, y - 12)

					newPath = KPPath(node, endNode, origPath)
					KP.map.pathLayer.paths.append(newPath)

					pathItem = KPEditorPath(newPath)
					self.scene().addItem(pathItem)

					return

			# Paint a new node
			node = KPNode()
			node.position = (x - 12, y - 12)
			KP.map.pathLayer.nodes.append(node)

			item = KPEditorNode(node)
			self.scene().addItem(item)

			# Paint a path to this node (if one is selected)
			sourceItem, sourceNode = None, None
			selected = self.scene().selectedItems()

			for selItem in selected:
				if isinstance(item, KPEditorNode) and selItem != item:
					sourceItem = selItem
					sourceNode = selItem._nodeRef()
					break

			# No node can have more than four paths, because there are only
			# four directions registered by a Wiimote DPad.

			if not sourceItem is None:
				if len(sourceNode.exits) > 3:
					return

				# There, now you can draw paths easily in a row.
				path = KPPath(sourceNode, node)

				KP.map.pathLayer.paths.append(path)

				pathItem = KPEditorPath(path)
				self.scene().addItem(pathItem)


			# Switch the selection to the recently drawn node, so you can keep on rolling.
			self.scene().clearSelection()
			item.setSelected(True)

			self.painting = node
			self.paintingItem = item
			self.paintBeginPosition = (x - 12, y - 12)


	
	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)
			event.accept()

		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)


	def keyPressEvent(self, event):
		if event.key() == QtCore.Qt.Key_Delete or event.key() == QtCore.Qt.Key_Backspace:
			scene = self.scene()

			selection = scene.selectedItems()
			if len(selection) > 0:
				for obj in selection:
					obj.setSelected(False)
					obj.remove(True)
				scene.update()
				self.update()
				return

		else:
			QtGui.QGraphicsView.keyPressEvent(self, event)