-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns_anim.py
159 lines (121 loc) · 5.33 KB
/
ns_anim.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
#!python3
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import (QAbstractTransition, QEasingCurve, QEvent,
QParallelAnimationGroup, QPropertyAnimation, qrand, QRect,
QSequentialAnimationGroup, qsrand, QState, QStateMachine, Qt, QTime,
QTimer)
from PyQt5.QtWidgets import (QApplication, QGraphicsScene, QGraphicsView,
QGraphicsWidget)
#
class StateSwitchEvent(QEvent):
StateSwitchType = QEvent.User + 256
def __init__(self, rand=0):
super(StateSwitchEvent, self).__init__(StateSwitchEvent.StateSwitchType)
self.m_rand = rand
def rand(self):
return self.m_rand
#
class QGraphicsRectWidget(QGraphicsWidget):
def __init__(self, color):
super(QGraphicsRectWidget, self).__init__()
self.color = color
def paint(self, painter, option, widget):
painter.fillRect(self.rect(), self.color)
#
class StateSwitchTransition(QAbstractTransition):
def __init__(self, rand):
super(StateSwitchTransition, self).__init__()
self.m_rand = rand
def eventTest(self, event):
return (event.type() == StateSwitchEvent.StateSwitchType and
event.rand() == self.m_rand)
def onTransition(self, event):
pass
#
class StateSwitcher(QState):
def __init__(self, machine):
super(StateSwitcher, self).__init__(machine)
self.m_stateCount = 0
self.m_lastIndex = 0
def onEntry(self, event):
n = qrand() % self.m_stateCount + 1
while n == self.m_lastIndex:
n = qrand() % self.m_stateCount + 1
self.m_lastIndex = n
self.machine().postEvent(StateSwitchEvent(n))
def onExit(self, event):
pass
def addState(self, state, animation):
self.m_stateCount += 1
trans = StateSwitchTransition(self.m_stateCount)
trans.setTargetState(state)
self.addTransition(trans)
trans.addAnimation(animation)
#
class NS_Animate(object):
def __init__(self, scene, x_max, y_max, back_color):
scene = QGraphicsScene(0, 0, x_max, y_max)
scene.setBackgroundBrush(back_color)
color = [Qt.green, Qt.lightGray, Qt.darkYellow, QtGui.QColor.fromRgb(255, 85, 0)]
self.anim_butt = [ QGraphicsRectWidget(color[j]) for j in range(4) ]
for j in range(4):
scene.addItem(self.anim_butt[j])
self.window = QGraphicsView(scene)
self.window.setFrameStyle(0)
self.window.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.window.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.window.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.machine = QStateMachine()
self.group = QState()
self.timer = QTimer()
self.timer.setInterval(1250)
self.timer.setSingleShot(True)
self.group.entered.connect(self.timer.start)
# set states positions
anim_state_rects = [ [QRect(x_max*xp/6, y_max*yp/4, 8, 8) for xp in range(4)] for yp in range(4) ]
self.states = [ self.createGeometryState(
self.anim_butt[0], anim_state_rects[0][j], self.anim_butt[1], anim_state_rects[1][j],
self.anim_butt[2], anim_state_rects[2][j], self.anim_butt[3], anim_state_rects[3][j],
self.group
) for j in range(4) ]
self.group.setInitialState(self.states[0])
self.animationGroup = QParallelAnimationGroup()
self.anim = QPropertyAnimation(self.anim_butt[3], 'geometry')
self.anim.setDuration(1250)
self.anim.setEasingCurve(QEasingCurve.InBack)
self.animationGroup.addAnimation(self.anim)
self.subGroup = QSequentialAnimationGroup(self.animationGroup)
self.subGroup.addPause(100)
self.anim = QPropertyAnimation(self.anim_butt[2], 'geometry')
self.anim.setDuration(1000)
self.anim.setEasingCurve(QEasingCurve.OutElastic)
self.subGroup.addAnimation(self.anim)
self.subGroup = QSequentialAnimationGroup(self.animationGroup)
self.subGroup.addPause(500)
self.anim = QPropertyAnimation(self.anim_butt[1], 'geometry')
self.anim.setDuration(500)
self.anim.setEasingCurve(QEasingCurve.OutElastic)
self.subGroup.addAnimation(self.anim)
self.subGroup = QSequentialAnimationGroup(self.animationGroup)
self.subGroup.addPause(750)
self.anim = QPropertyAnimation(self.anim_butt[0], 'geometry')
self.anim.setDuration(250)
self.anim.setEasingCurve(QEasingCurve.OutElastic)
self.subGroup.addAnimation(self.anim)
self.stateSwitcher = StateSwitcher(self.machine)
self.group.addTransition(self.timer.timeout, self.stateSwitcher)
for j in range(4):
self.stateSwitcher.addState(self.states[j], self.animationGroup)
self.machine.addState(self.group)
self.machine.setInitialState(self.group)
self.machine.start()
#
def createGeometryState(self, w1, rect1, w2, rect2, w3, rect3, w4, rect4, parent):
result = QState(parent)
result.assignProperty(w1, 'geometry', rect1)
result.assignProperty(w1, 'geometry', rect1)
result.assignProperty(w2, 'geometry', rect2)
result.assignProperty(w3, 'geometry', rect3)
result.assignProperty(w4, 'geometry', rect4)
return result