From 7bb952ac294991753b86a7075007070676baafb9 Mon Sep 17 00:00:00 2001
From: Treeki <treeki@gmail.com>
Date: Thu, 17 Nov 2011 03:38:06 +0100
Subject: unfinished code for doodads

---
 src/editorui.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/mapdata.py  | 65 +++++++++++++++++++++++++++++++++++++--------------------
 src/ui.py       | 10 +++++++--
 3 files changed, 114 insertions(+), 26 deletions(-)

(limited to 'src')

diff --git a/src/editorui.py b/src/editorui.py
index 7969100..c7da106 100644
--- a/src/editorui.py
+++ b/src/editorui.py
@@ -236,6 +236,40 @@ class KPEditorObject(KPEditorItem):
 		self._layerRef().updateCache()
 
 
+class KPEditorDoodad(KPEditorItem):
+	def __init__(self, doodad, layer):
+		KPEditorItem.__init__(self)
+		obj.qtItem = self
+		self._doodadRef = weakref.ref(doodad)
+		self._layerRef = weakref.ref(layer)
+		self._updatePosition()
+		self._updateSize()
+
+		self.setAcceptHoverEvents(True)
+	
+	def _updatePosition(self):
+		self.setPos(*self._doodadRef().position)
+	
+	def _updateSize(self):
+		self.prepareGeometryChange()
+
+		w,h = self._doodadRef().size
+
+		self._boundingRect = QtCore.QRectF(0, 0, w, h)
+		self._selectionRect = QtCore.QRectF(0, 0, w, h)
+	
+
+	def paint(self, painter, option, widget):
+		if self.isSelected():
+			painter.setPen(QtGui.QPen(Qt.white, 1, Qt.DotLine))
+			painter.drawRect(self._selectionRect)
+	
+
+	def _itemMoved(self, oldX, oldY, newX, newY):
+		doodad = self._doodadRef()
+		doodad.position = (newX, newY)
+
+
 class KPMapScene(QtGui.QGraphicsScene):
 	def __init__(self):
 		QtGui.QGraphicsScene.__init__(self, 0, 0, 512*24, 512*24)
@@ -259,10 +293,39 @@ class KPMapScene(QtGui.QGraphicsScene):
 		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, KPTileLayer):
+			if isinstance(layer, KPDoodadLayer):
+				try:
+					toDraw = visibleDoodadsByLayer[layer]
+				except KeyError:
+					continue
+
+				for item in toDraw:
+					painter.save()
+					painter.setWorldTransform(item.transform(), True)
+					painter.drawPixmap(0, 0, item.pixmap)
+					painter.restore()
+
+			elif isinstance(layer, KPTileLayer):
 				left, top = layer.cacheBasePos
 				width, height = layer.cacheSize
 				right, bottom = left+width, top+height
diff --git a/src/mapdata.py b/src/mapdata.py
index 2f1f5a2..53331c5 100644
--- a/src/mapdata.py
+++ b/src/mapdata.py
@@ -4,19 +4,6 @@ TILE_SIZE = (24,24)
 MAP_SIZE_IN_TILES = (512,512)
 MAP_SIZE = (MAP_SIZE_IN_TILES[0] * TILE_SIZE[0], MAP_SIZE_IN_TILES[1] * TILE_SIZE[1])
 
-class KPObject(object):
-	def __init__(self):
-		self.position = (0,0)
-		self.size = (1,1)
-		self.kind = 0
-		self.cache = []
-		self.tileset = None
-		self.qtItem = None
-	
-	def updateCache(self):
-		self.cache = self.kind.render(self.size)
-
-
 class KPLayer(object):
 	def __repr__(self):
 		return "<KPLayer %r>" % self.name
@@ -41,6 +28,19 @@ class KPLayer(object):
 		pass
 
 
+class KPObject(object):
+	def __init__(self):
+		self.position = (0,0)
+		self.size = (1,1)
+		self.kind = 0
+		self.cache = []
+		self.tileset = None
+		self.qtItem = None
+	
+	def updateCache(self):
+		self.cache = self.kind.render(self.size)
+
+
 class KPTileLayer(KPLayer):
 	def __repr__(self):
 		return "<KPTileLayer %r with %r>" % (self.name, self.tileset)
@@ -111,6 +111,27 @@ class KPTileLayer(KPLayer):
 				y += 1
 
 
+class KPDoodad(object):
+	def __init__(self):
+		self.position = (0,0)
+		self.angle = 0
+		self.scale = (0,0)
+
+
+class KPDoodadLayer(KPLayer):
+	def __repr__(self):
+		return "<KPDoodadLayer %r>" % self.name
+
+	def __init__(self):
+		KPLayer.__init__(self)
+		self.objects = []
+	
+	def _visibilityChanged(self, value):
+		for obj in self.objects:
+			item = obj.qtItem
+			if item:
+				item.setVisible(value)
+
 
 
 class KPNodeAction(object):
@@ -138,22 +159,14 @@ class KPPath(object):
 		self.segments = []
 
 
-class KPDoodad(object):
-	def __init__(self):
-		self.position = (0,0)
-		self.angle = 0
-		self.scale = (0,0)
-
-
 class KPMap(object):
 	def __init__(self):
 		self.nextLayerNumber = 1
 
-		self.layers = [self.createNewLayer('Test') for x in range(4)]
+		self.layers = [self.createNewTileLayer('Test') for x in range(4)]
 		self.layerModel = KPMap.LayerModel(self.layers)
 		self.nodes = []
 		self.paths = []
-		self.doodads = []
 		self.tilesets = {}
 		self.loadedTilesets = {}
 
@@ -220,13 +233,19 @@ class KPMap(object):
 			return False
 
 
-	def createNewLayer(self, tilesetName):
+	def createNewTileLayer(self, tilesetName):
 		layer = KPTileLayer()
 		layer.name = "Layer %d" % self.nextLayerNumber
 		self.nextLayerNumber += 1
 		layer.tileset = tilesetName
 		return layer
 
+	def createNewDoodadLayer(self):
+		layer = KPDoodadLayer()
+		layer.name = "Layer %d" % self.nextLayerNumber
+		self.nextLayerNumber += 1
+		return layer
+
 	def appendLayer(self, layer):
 		return self.insertLayer(len(self.layers), layer)
 	
diff --git a/src/ui.py b/src/ui.py
index 648cfa9..247938e 100644
--- a/src/ui.py
+++ b/src/ui.py
@@ -58,11 +58,17 @@ class KPLayerList(QtGui.QWidget):
 	def addLayer(self):
 		from dialogs import KPTilesetChooserDialog
 
-		tilesetName = KPTilesetChooserDialog.run('Choose a tileset for the new layer')
+		special = ['Doodads (Special)']
+		tilesetName = KPTilesetChooserDialog.run('Choose a tileset for the new layer', special)
 		if tilesetName is None:
 			return
 
-		KP.map.appendLayer(KP.map.createNewLayer(tilesetName))
+		if tilesetName == 0:
+			layer = KP.map.createNewDoodadLayer()
+		else:
+			layer = KP.map.createNewTileLayer(tilesetName)
+
+		KP.map.appendLayer(layer)
 		self.setButtonStates()
 
 	def removeLayer(self):
-- 
cgit v1.2.3