summaryrefslogtreecommitdiff
path: root/src/mapdata.py
blob: 5e65599f64635625f4c4ff5807e01f197ce44f53 (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
from common import *

class KPLayer(object):
	def __repr__(self):
		return "<KPLayer %r>" % self.name

	def __init__(self):
		self.name = ''
		self.tileset = ''
		self.objects = []
		self.visible = True


class KPNodeAction(object):
	def __init__(self):
		pass


class KPNode(object):
	def __init__(self):
		self.position = (0,0)
		self.actions = []


class KPPathSegment(object):
	def __init__(self):
		self.start = None
		self.end = None
		self.animation = None # default


class KPPath(object):
	def __init__(self):
		self.startNode = None
		self.endNode = None
		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() for x in range(4)]
		self.layerModel = KPMap.LayerModel(self.layers)
		self.nodes = []
		self.paths = []
		self.doodads = []


	# LAYERS
	class LayerModel(QtCore.QAbstractListModel):
		def __init__(self, layerList):
			QtCore.QAbstractListModel.__init__(self)
			self.list = layerList


		def headerData(self, section, orientation, role = Qt.DisplayRole):
			return 'Layer'

		def rowCount(self, parent):
			return len(self.list)

		def data(self, index, role = Qt.DisplayRole):
			try:
				if index.isValid():
					layer = self.list[index.row()]

					if (role == Qt.DisplayRole or role == Qt.EditRole):
						return layer.name
					elif role == Qt.CheckStateRole:
						return (Qt.Checked if layer.visible else Qt.Unchecked)

			except IndexError:
				pass

			return QtCore.QVariant()

		def flags(self, index):
			if not index.isValid():
				return Qt.ItemIsEnabled
			
			return Qt.ItemIsEditable | Qt.ItemIsUserCheckable \
					| QtCore.QAbstractListModel.flags(self, index)

		def setData(self, index, value, role = Qt.EditRole):
			if index.isValid():
				layer = self.list[index.row()]

				if role == Qt.EditRole:
					value = str(value.toString())
					if len(value) > 0:
						layer.name = value
						self.dataChanged.emit(index, index)
						return True

				elif role == Qt.CheckStateRole:
					layer.visible = value.toBool()
					self.dataChanged.emit(index, index)
					return True

			return False


	def createNewLayer(self):
		layer = KPLayer()
		layer.name = "Layer %d" % self.nextLayerNumber
		self.nextLayerNumber += 1
		return layer

	def appendLayer(self, layer):
		return self.insertLayer(len(self.layers), layer)
	
	def insertLayer(self, index, layer):
		self.layerModel.beginInsertRows(QtCore.QModelIndex(), index, index)
		self.layers.insert(index, layer)
		self.layerModel.endInsertRows()
		return index

	def moveLayer(self, fromIndex, toIndex):
		if fromIndex == toIndex:
			return
		if fromIndex < 0 or toIndex < 0:
			raise ValueError
		if fromIndex >= len(self.layers) or toIndex > len(self.layers):
			raise ValueError

		self.layerModel.beginMoveRows(
				QtCore.QModelIndex(), fromIndex, fromIndex,
				QtCore.QModelIndex(), toIndex)

		toMove = self.layers[fromIndex]
		newIndex = ((toIndex > fromIndex) and (toIndex - 1)) or toIndex

		del self.layers[fromIndex]
		self.layers.insert(newIndex, toMove)

		self.layerModel.endMoveRows()

		return newIndex

	def removeLayer(self, layer):
		if layer not in self.layers:
			raise ValueError

		index = self.layers.index(layer)
		self.layerModel.beginRemoveRows(QtCore.QModelIndex(), index, index)
		del self.layers[index]
		self.layerModel.endRemoveRows()