Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UncertainProd
GitHub Repository: UncertainProd/FnF-Spritesheet-and-XML-Maker
Path: blob/master/src/animationwindow.py
254 views
1
from animpreviewwindow import Ui_animation_view
2
from PyQt5.QtWidgets import QWidget
3
from PyQt5.QtCore import QTimer
4
import engine.spritesheetutils as spritesheetutils
5
from utils import imghashes
6
7
class AnimationView(QWidget):
8
def __init__(self, *args, **kwargs):
9
super().__init__(*args, **kwargs)
10
self.ui = Ui_animation_view()
11
self.ui.setupUi(self)
12
13
self.ui.play_anim_button.clicked.connect(self.play_animation)
14
self.ui.animation_display_area.setText("Click 'Play Animation' to start the animation preview")
15
self.ui.animation_display_area.setStyleSheet("background-color:#696969;")
16
17
self.animframes = []
18
self.anim_names = {}
19
self.frameindex = 0
20
self.animstarted = False
21
self.timer = QTimer()
22
self.timer.timeout.connect(self.set_next_frame)
23
24
def parse_and_load_frames(self, frames):
25
for f in frames:
26
if f.data.pose_name in self.anim_names:
27
self.anim_names[f.data.pose_name].append(f)
28
else:
29
self.anim_names[f.data.pose_name] = [ f ]
30
self.ui.pose_combobox.addItems(list(self.anim_names.keys()))
31
32
def play_animation(self):
33
if self.animstarted:
34
self.timer.stop()
35
self.animstarted = False
36
self.ui.play_anim_button.setText("Play Animation")
37
else:
38
self.animstarted = True
39
framerate = self.ui.framerate_adjust.value()
40
animname = self.ui.pose_combobox.currentText()
41
self.animframes = self.anim_names[animname]
42
self.frameindex = 0
43
print(f"Playing {animname} at {framerate}fps with nframes:{len(self.animframes)}")
44
self.ui.play_anim_button.setText("Stop Animation")
45
self.timer.start(int(1000/framerate))
46
47
def set_next_frame(self):
48
curframe = self.animframes[self.frameindex]
49
curframeimg = imghashes.get(curframe.data.img_hash)
50
truframe_pixmap = spritesheetutils.get_true_frame(
51
curframeimg,
52
curframe.data.framex if curframe.data.framex is not None else 0,
53
curframe.data.framey if curframe.data.framey is not None else 0,
54
curframe.data.framew if curframe.data.framew is not None else curframeimg.width,
55
curframe.data.frameh if curframe.data.frameh is not None else curframeimg.height,
56
).toqpixmap()
57
self.ui.animation_display_area.setPixmap(truframe_pixmap)
58
self.frameindex = (self.frameindex + 1) % len(self.animframes)
59
60
def closeEvent(self, a0):
61
self.timer.stop()
62
self.animstarted = False
63
self.ui.animation_display_area.clear()
64
self.ui.pose_combobox.clear()
65
self.animframes.clear()
66
self.anim_names.clear()
67
self.frameindex = 0
68
return super().closeEvent(a0)
69
70