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
|
from common import *
import weakref
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 KPLayer(object):
def __repr__(self):
return "<KPLayer %r>" % self.name
def __init__(self):
self.name = ''
self._visible = True
@property
def visible(self):
return self._visible
@visible.setter
def visible(self, value):
if self._visible == value:
return
self._visible = value
self._visibilityChanged(value)
def _visibilityChanged(self, value):
pass
def setActivated(self, value, listToUse=None):
flag1 = QtGui.QGraphicsItem.ItemIsSelectable
flag2 = QtGui.QGraphicsItem.ItemIsMovable
if listToUse is None:
listToUse = self.objects
for obj in listToUse:
item = obj.qtItem
if item:
item.setFlag(flag1, value)
item.setFlag(flag2, value)
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)
def __init__(self):
KPLayer.__init__(self)
self.tileset = ''
self.objects = []
self.cache = ['DUMMY_FLAG']
self.updateCache()
self.icon = KP.icon('LayerTile')
def _visibilityChanged(self, value):
for obj in self.objects:
item = obj.qtItem
if item:
item.setVisible(value)
def updateCache(self):
if len(self.objects) == 0:
if len(self.cache) != 0:
self.cache = []
self.cacheBasePos = (0,0)
self.cacheSize = (0,0)
return
x1, x2 = MAP_SIZE_IN_TILES[0] - 1, 0
y1, y2 = MAP_SIZE_IN_TILES[1] - 1, 0
for obj in self.objects:
x, y = obj.position
w, h = obj.size
right, bottom = (x+w-1), (y+h-1)
if x < x1:
x1 = x
if y < y1:
y1 = y
if right > x2:
x2 = right
if bottom > y2:
y2 = bottom
# create the cache
# I was going to just resize it, but setting every tile to -1
# in Python would probably be slower than creating a new one ...
size = (x2 - x1 + 1, y2 - y1 + 1)
width, height = size
cache = [[-1 for i in xrange(width)] for j in xrange(height)]
self.cache = cache
self.cacheBasePos = (x1, y1)
self.cacheSize = size
# now generate the thing
for obj in self.objects:
oX, oY = obj.position
baseX = oX - x1
y = oY - y1
for row in obj.cache:
destRow = cache[y]
x = baseX
for tile in row:
destRow[x] = tile
x += 1
y += 1
class KPDoodad(object):
def __init__(self):
self.position = [0,0]
self.size = [0,0]
self.angle = 0
self.index = 0
def setDefaultSize(self):
source = KP.mainWindow.doodadSelector.getDoodad(self.index)
pixmap = source.icon().pixmap(source.icon().availableSizes()[0])
self.size = [pixmap.width(), pixmap.height()]
class KPDoodadLayer(KPLayer):
def __repr__(self):
return "<KPDoodadLayer %r>" % self.name
def __init__(self):
KPLayer.__init__(self)
self.objects = []
self.icon = KP.icon('LayerObjects')
def _visibilityChanged(self, value):
for obj in self.objects:
item = obj.qtItem
if item:
item.setVisible(value)
class KPNodeAction(object):
def __init__(self):
pass
class KPNode(object):
def __init__(self):
self.position = (0,0)
self.actions = []
self.exits = []
self.level = [0,0]
self.isStop = False
class KPPath(object):
def __init__(self, startNode, endNode, cloneFrom=None):
self._startNodeRef = weakref.ref(startNode)
self._endNodeRef = weakref.ref(endNode)
startNode.exits.append(self)
endNode.exits.append(self)
self.unlocks = 0 # 0 = always unlocked, 1 = unlocked from startNode, 2 = unlocked from endNode
self.secret = 0 # 0 = unlocks from normal exit, 1 = unlocks from secret exit
if cloneFrom is None:
self.animation = None
else:
self.animation = cloneFrom.animation
self.movementSpeed = 1.0
self.linkedLayer = None
def setStart(self, newStart):
currentStart = self._startNodeRef()
if currentStart is not None:
currentStart.exits.remove(self)
newStart.exits.append(self)
self._startNodeRef = weakref.ref(newStart)
def setEnd(self, newEnd):
currentEnd = self._endNodeRef()
if currentEnd is not None:
currentEnd.exits.remove(self)
newEnd.exits.append(self)
self._endNodeRef = weakref.ref(newEnd)
class KPPathLayer(KPLayer):
def __repr__(self):
return "<KPPathLayer %r>" % self.name
def __init__(self):
KPLayer.__init__(self)
self.nodes = []
self.paths = []
self.icon = KP.icon('LayerPath')
def _visibilityChanged(self, value):
for objList in (self.nodes, self.paths):
for obj in objList:
item = obj.qtItem
if item:
item.setVisible(value)
def setActivated(self, value):
KPLayer.setActivated(self, value, self.nodes)
flag = QtGui.QGraphicsItem.ItemIsSelectable
for path in self.paths:
item = path.qtItem
if item:
item.setFlag(flag, value)
class KPMap(object):
def __init__(self):
self.nextLayerNumber = 1
self.pathLayer = self._createPathLayer()
self.layers = [self.pathLayer]
self.layerModel = KPMap.LayerModel(self.layers)
self.nodes = []
self.paths = []
self.tilesets = {}
self.loadedTilesets = {}
# TESTING CRAP
import os.path
if os.path.exists('/Users/Tempus'):
self.tilesets['Test'] = {'path': '/Users/Tempus/Dropbox/NEWERsmbw/Test3.arc'}
else:
self.tilesets['Test'] = {'path': '/home/me/Dropbox/NEWERsmbw/Test3.arc'}
self.reloadTileset('Test')
# 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.DecorationRole:
return layer.icon
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 _createPathLayer(self):
layer = KPPathLayer()
layer.name = 'Paths'
return layer
def createNewTileLayer(self, tilesetName):
layer = KPTileLayer()
layer.name = "Tilemap - Layer %d" % self.nextLayerNumber
self.nextLayerNumber += 1
layer.tileset = tilesetName
return layer
def createNewDoodadLayer(self):
layer = KPDoodadLayer()
layer.name = "Doodads - 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()
def reloadTileset(self, name):
info = self.tilesets[name]
self.loadedTilesets[name] = KPTileset.loadFromArc(info['path'])
# TODO: handle relative paths properly in relation to the map file
# TODO: add some sort of callback/signal to regenerate objects/layers
# and refresh the view
|