-
Notifications
You must be signed in to change notification settings - Fork 0
/
animationTypes.py
371 lines (300 loc) · 9.71 KB
/
animationTypes.py
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
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import QPoint, QSize, QRect, pyqtSignal, QObject
import logging
from enum import Enum
class AnimationEvent(Enum):
NEW_SEQUENCE = 1
SEQUENCE_DELETED = 2
NEW_FRAME = 3
FRAME_DELETED = 4
FRAME_ALTERED = 5
ACTIVE_SEQUENCE_CHANGED = 6
ACTIVE_FRAME_CHANGED = 7
FRAME_SELECTION_CHANGED = 8
SEQUENCE_SELECTION_CHANGED = 9
class AnimationData(QObject):
active_sequence_changed = pyqtSignal()
active_frame_changed = pyqtSignal()
data_changed = pyqtSignal(object)
def __init__(self, spritesheet_fname):
QObject.__init__(self)
self.spritesheet_path = spritesheet_fname
self.spritesheet_fname = self.spritesheet_path.split("/")[-1][:-4]
self.sequences = []
self._active_sequence = None
self.selected = []
def new_sequence(self, name=None, active=False):
if name is None:
name = "Sequence_" + str(len(self.sequences))
sequence = AnimationSequence(name=name)
sequence.active_frame_changed.connect(self.active_frame_changed.emit)
if active:
self.active_sequence = sequence
self.selected = [sequence]
self.sequences.append(sequence)
self.data_changed.emit(AnimationEvent.NEW_SEQUENCE)
def get_sequence(self, name):
for sequence in self.sequences:
if sequence.name == name:
return sequence
return None
def del_sequence(self, sequence):
if sequence == self.active_sequence:
self.active_sequence = None
self.sequences.remove(sequence)
self.data_changed.emit(AnimationEvent.SEQUENCE_DELETED)
@property
def active_sequence(self):
return self._active_sequence
@active_sequence.setter
def active_sequence(self, sequence):
self._active_sequence = sequence
self.active_sequence_changed.emit()
self.data_changed.emit(AnimationEvent.ACTIVE_SEQUENCE_CHANGED)
print("active sequence changed")
@active_sequence.deleter
def active_sequence(self):
for frame in self._active_sequence.frames:
del frame
self.sequences.remove(self._active_sequence)
self.selected.remove(self._active_sequence)
self._active_sequence = None
self.data_changed.emit(AnimationEvent.SEQUENCE_DELETED)
self.data_changed.emit(AnimationEvent.SEQUENCE_SELECTION_CHANGED)
self.data_changed.emit(AnimationEvent.ACTIVE_SEQUENCE_CHANGED)
@property
def active_frame(self):
if self.active_sequence is not None:
return self.active_sequence.active_frame
else:
return None
@active_frame.setter
def active_frame(self, frame):
self.active_sequence.active_frame = frame
@active_frame.deleter
def active_frame(self):
if self.active_sequence.active_frame is not None:
self.active_sequence.remove_frame(self.active_frame)
def select(self, name):
seq = self.get_sequence(name)
if seq not in self.selected:
self.selected.append(seq)
self.data_changed.emit(AnimationEvent.SEQUENCE_SELECTION_CHANGED)
def selected_frames(self):
frames = []
for sequence in self.selected:
for frame in sequence.selected:
frames.append(frame)
return frames
def deselect(self, name):
seq = self.get_sequence(name)
if seq in self.selected:
self.selected.remove(seq)
self.data_changed.emit(AnimationEvent.SEQUENCE_SELECTION_CHANGED)
def select_all(self):
self.selected = self.sequences
self.data_changed.emit(AnimationEvent.SEQUENCE_SELECTION_CHANGED)
def deselect_all(self):
self.selected = []
self.active_sequence = None
self.data_changed.emit(AnimationEvent.SEQUENCE_SELECTION_CHANGED)
def export_data(self):
sequence_data = []
for sequence in self.sequences:
frame_data = []
for frame in sequence.frames:
frame_data.append({
"pos": (frame.topLeft().x(), frame.topLeft().y()),
"size": (frame.size().width(), frame.size().height()),
"shift": (frame.shift.x(), frame.shift.y())
})
sequence_data.append({
"name": sequence.name,
"speed": sequence.speed,
"frames": frame_data
})
output = {
"fpath": self.spritesheet_path,
"sequences": sequence_data
}
return output
@staticmethod
def import_data(data):
animation_data = AnimationData(data['fpath'])
for sequence in data['sequences']:
animation_data.new_sequence(sequence['name'], active=True)
animation_data.active_sequence.speed = sequence['speed']
for frame in sequence['frames']:
animation_data.active_sequence.add_frame(
AnimationFrame(QPoint(frame['pos'][0], frame['pos'][1]),
QSize(frame['size'][0], frame['size'][1]),
QPoint(frame['shift'][0], frame['shift'][1])))
animation_data.active_sequence.active_frame_changed.connect(animation_data.active_frame_changed.emit)
return animation_data
class AnimationSequence(QObject):
active_frame_changed = pyqtSignal()
def __init__(self, name):
QObject.__init__(self)
self.name = name
self.frames = []
self._active_frame = None
self.selected = []
self.speed = 20 #Frames per second
@property
def active_frame(self):
return self._active_frame
@active_frame.setter
def active_frame(self, frame):
if frame is None:
self._active_frame = None
self.active_frame_changed.emit()
return
for s_frame in self.frames:
if s_frame == frame:
self._active_frame = frame
self.active_frame_changed.emit()
return
raise Exception('active frame assignment failed. Frame was not found in sequence .' % self.active_sequence.name)
def select(self, arg):
# TODO finish exception handling
if type(arg) == AnimationFrame:
if arg not in self.frames:
raise Exception("Tried to select frame that was not in the sequence")
if arg not in self.selected:
self.selected.append(arg)
self.active_frame = arg
elif type(arg) == int:
try:
if self.frames[arg] not in self.selected:
self.selected.append(self.frames[arg])
self.active_frame = self.frames[arg]
except:
pass
def deselect(self, arg):
if type(arg) == AnimationFrame:
if arg in self.selected:
self.selected.remove(arg)
if arg == self.active_frame:
self.active_frame = None
elif type(arg) == int:
try:
if self.frames[arg] in self.selected:
self.selected.remove(self.frames[arg])
except:
pass
def select_all(self):
self.selected = self.frames
def deselect_all(self):
self.selected = []
def add_frame(self, frame):
self.frames.append(frame)
self.active_frame = frame
self.selected = [frame]
def remove_frame(self, frame):
self.frames.remove(frame)
if frame in self.selected:
self.selected.remove(frame)
if frame == self.active_frame:
self.active_frame = None
def frame_at_pos(self, pos):
for frame in self.frames:
if frame.contains(pos):
return frame
return None
def set_sole(self, frame):
if frame in self.frames:
self.active_frame = frame
self.selected = [frame]
def get_frame_after(self, index):
if len(self.frames) > index + 1:
return None
else:
return self.frames[index + 1]
def get_frame_before(self, index):
if index == 0:
return None
else:
return self.frames[index + 1]
def next_frame(self):
# Sets the active frame to frame after the current active frame
if self.active_frame is None:
if len(self.frames) > 0:
self.active_frame = self.frames[0]
else:
self.active_frame = None
else:
active_frame_index = self.frames.index(self.active_frame)
if active_frame_index == len(self.frames) - 1:
self.active_frame = self.frames[0]
else:
self.active_frame = self.frames[active_frame_index + 1]
def prev_frame(self):
if self.active_frame is None:
if len(self.frames) > 0:
self.active_frame = self.frames[0]
else:
self.active_frame = None
else:
# Sets the active frame to frame before the current active frame
active_frame_index = self.frames.index(self.active_frame)
if active_frame_index == 0:
self.active_frame = self.frames[len(self.frames) - 1]
else:
self.active_frame = self.frames[active_frame_index - 1]
# def get_next_frame(self):
# if self.active_frame is None:
# if len(self.frames) > 0:
# return self.frames[0]
# else:
# return None
#
# else:
# active_frame_index = self.frames.index(self.active_frame)
# if active_frame_index == len(self.frames) - 1:
# return self.frames[0]
# else:
# return self.frames[active_frame_index + 1]
#
# def get_previous_frame(self):
# if self.active_frame is None:
# if len(self.frames) > 0:
# return self.frames[0]
# else:
# return None
# else:
# active_frame_index = self.frames.index(self.active_frame)
# if active_frame_index == 0:
# return self.frames[len(self.frames) - 1]
# else:
# return self.frames[active_frame_index - 1]
def frame_index(self, frame):
try:
return self.frames.index(frame)
except:
return None
def active_frame_index(self):
if self._active_frame is not None:
return self.frames.index(self._active_frame)
else:
return None
class AnimationFrame(QRect):
def __init__(self, pos, size=QSize(0, 0), shift=None):
QRect.__init__(self, pos, size)
if shift is None:
self.shift = QPoint(0,0)
else:
self.shift = shift
def normalize(self):
if self.width() < 0:
left = self.left() - 1
self.setLeft(self.right() + 1)
self.setRight(left)
if self.height() < 0:
top = self.top() - 1
self.setTop(self.bottom() + 1)
self.setBottom(top)
# def with_shift(self):
# return QRect( self.left() - self.padding.left,
# self.top() - self.padding.top,
# self.width() + self.padding.left + self.padding.right,
# self.height() + self.padding.top + self.padding.bottom )