forked from scummvm-director/continuity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.py
226 lines (211 loc) · 7.21 KB
/
timeline.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
from PySide.QtCore import *
from PySide.QtGui import *
from dutils import *
from movie import *
gridSizeX = 25
gridSizeY = 25
oldX = -1
oldY = -1
class Timeline(QWidget):
def __init__(self, parent, movie):
QWidget.__init__(self, parent)
self.setAutoFillBackground(True)
self.setMouseTracking(True)
self.movie = movie
self.movie.currFrame = -1
self.movie.currChannel = None
self.resize((len(self.movie.frames) + 1) * gridSizeX + 2, (2 + reservedChannelCount + channelCount) * gridSizeY + 2) # +2 to make edges visible
self.parent().parent().parent().selectionChanged.connect(self.selectionChanged)
def selectionChanged(self):
if self.movie.currFrame != -1 and self.movie.currChannel == None:
self.parent().parent().ensureVisible((self.movie.currFrame + 1) * gridSizeX, gridSizeY, 150, 50)
self.update()
def event(self, ev):
if ev.type() != QEvent.ToolTip:
return QWidget.event(self, ev)
ev.ignore()
return True
def mousePressEvent(self, ev):
valid = True
chanValid = True
x = ev.x() - gridSizeX
y = ev.y() - gridSizeY*2
if x < 0:
valid = False
y = None
if y < 0:
chanValid = False
y = None
x = x / gridSizeX
if y != None:
y = (y / gridSizeY) - reservedChannelCount + 1
if (not valid) or x >= len(self.movie.frames):
valid = False
elif chanValid and (y not in self.movie.frames[x].sprites):
chanValid = False
y = None
info = None
if chanValid:
info = self.movie.frames[x].sprites[y]
if info and not info.enabled:
y = None
if not valid:
x = -1
self.movie.currFrame = x
self.movie.currChannel = y
self.parent().parent().parent().parent().selectionChanged.emit()
self.update()
return True
def mouseMoveEvent(self, ev):
x = ev.x() - gridSizeX
y = ev.y() - gridSizeY*(2+reservedChannelCount-1)
if x < 0 or y < 0:
oldX = -1
QToolTip.hideText()
return True
x = x / gridSizeX
y = y / gridSizeY
global oldX, oldY
if oldX == x and oldY == y:
return True
oldX = x
oldY = y
if x >= len(self.movie.frames) or y < 1 or y > channelCount:
oldX = -1
QToolTip.hideText()
return True
info = self.movie.frames[x].sprites[y]
#QToolTip.hideText()
if not info.enabled:
QToolTip.hideText()
return True
myId = 1024 + info.castId
mystr = str(info.castId)
if myId in self.movie.castInfo:
mystr = "%s (%s)" % (mystr, self.movie.castInfo[myId].name)
if "BITD" in self.movie.resources:
for p in self.movie.resources["BITD"]:
if p.rid != myId:
continue
p.seek(0)
# TODO
if myId in self.movie.dibs:
dib = self.movie.dibs[myId]
dib.seek(0)
data = dib.read()
palette = getPaletteFor(x, self.movie)
imgdata = imageFromDIB(data, palette)
img = QImage.fromData(imgdata, "bmp")
ba = QByteArray()
buf = QBuffer(ba)
buf.open(QIODevice.WriteOnly)
img.save(buf, "PNG")
mystr = mystr + "<img src=\"data:image/png;base64,%s\">" % buf.data().toBase64()
mystr = mystr + str(img.size())
if info.height:
mystr = "%s<br>at (%d, %d), size (%d, %d)" % (mystr, info.x, info.y, info.width, info.height)
QToolTip.showText(ev.globalPos(), mystr, self)
return True
def paintEvent(self, ev):
painter = QPainter(self)
myfont = QFont()
myfont.setPointSize(6)
painter.setFont(myfont)
# labels
for i in self.movie.labels:
xpos = i.frame * gridSizeX + gridSizeX/2
# triangle
painter.drawLine(xpos, gridSizeY - 5, xpos - 3, 13)
painter.drawLine(xpos, gridSizeY - 5, xpos + 3, 13)
painter.drawLine(xpos - 3, 13, xpos + 3, 13)
# text
painter.drawText(xpos+10, gridSizeY - 5, i.text)
# ruler
painter.setPen(QPen("gray"))
for n in range(4, len(self.movie.frames), 5):
xpos = (n+1) * gridSizeX + gridSizeX/2
painter.drawText(xpos-gridSizeX, gridSizeY+2, gridSizeX*2, gridSizeY, Qt.AlignHCenter, str(n + 1))
for n in range(len(self.movie.frames)):
xpos = (n+1) * gridSizeX + gridSizeX/2
if self.movie.currFrame == n and self.movie.currChannel == None:
painter.setPen(QPen("red"))
if n % 5 == 4:
painter.drawLine(xpos, gridSizeY + 17, xpos, gridSizeY*2)
else:
painter.drawLine(xpos, gridSizeY + 20, xpos, gridSizeY*2)
if self.movie.currFrame == n and self.movie.currChannel == None:
painter.setPen(QPen("gray"))
painter.drawText(0, gridSizeY * 2, gridSizeX, gridSizeY, 0x82, "tmpo")
painter.drawText(0, gridSizeY * 3, gridSizeX, gridSizeY, 0x82, "pal")
painter.drawText(0, gridSizeY * 4, gridSizeX, gridSizeY, 0x82, "trns")
painter.drawText(0, gridSizeY * 5, gridSizeX, gridSizeY, 0x82, "snd1")
painter.drawText(0, gridSizeY * 6, gridSizeX, gridSizeY, 0x82, "snd2")
painter.drawText(0, gridSizeY * 7, gridSizeX, gridSizeY, 0x82, "scrp")
for n in range(channelCount):
ypos = gridSizeY * (2 + reservedChannelCount + n)
painter.drawText(0, ypos, gridSizeX, gridSizeY, Qt.AlignVCenter | Qt.AlignRight, str(n + 1))
# grid
painter.setPen(QPen("darkGray"))
rect = self.rect()
if rect.left() < gridSizeX:
rect.setLeft(gridSizeX)
if rect.top() < gridSizeY * 2: # notes, ruler
rect.setTop(gridSizeY * 2)
left = int(rect.left()) - (int(rect.left()) % gridSizeX)
top = int(rect.top()) - (int(rect.top()) % gridSizeY)
lines = []
for x in range(left, int(rect.right()), gridSizeX):
lines.append(QLineF(x, rect.top(), x, rect.bottom()))
for y in range(top, int(rect.bottom()), gridSizeY):
lines.append(QLineF(rect.left(), y, rect.right(), y))
painter.drawLines(lines)
# stupid hack
for x in range(len(self.movie.frames)):
xpos = (x+1) * gridSizeX
frame = self.movie.frames[x]
for y in frame.sprites.keys():
entry = frame.sprites[y]
if not entry.enabled:
continue
ypos = (y + 2 + reservedChannelCount - 1) * gridSizeY
if self.movie.currFrame == x and self.movie.currChannel == y:
painter.setBrush(QBrush("red", Qt.SolidPattern))
elif self.movie.currFrame == x:
painter.setBrush(QBrush("pink", Qt.SolidPattern))
else:
painter.setBrush(QBrush("darkGray", Qt.SolidPattern))
painter.setPen(QPen("darkGray"))
painter.drawRect(xpos, ypos, gridSizeX, gridSizeY)
painter.setPen(QPen("gray"))
painter.drawLine(xpos, ypos, xpos+gridSizeX, ypos)
painter.drawLine(xpos, ypos+gridSizeY, xpos+gridSizeX, ypos+gridSizeY)
seenEntries = {}
for x in range(len(self.movie.frames)):
xpos = (x+1) * gridSizeX
frame = self.movie.frames[x]
for y in frame.sprites.keys():
if y <= 0:
continue # TODO
entry = frame.sprites[y]
oldEntry = None
if y in seenEntries:
oldEntry = seenEntries[y]
if y > 0:
seenEntries[y] = entry.castId
ypos = (y + 2 + reservedChannelCount - 1) * gridSizeY
if oldEntry and (not entry.enabled or entry.castId != oldEntry):
# draw a line at the end of the OLD one
painter.setPen(QPen("gray"))
painter.drawLine(xpos, ypos, xpos, ypos+gridSizeY)
if not entry.enabled:
del seenEntries[y]
continue
if entry.castId != oldEntry:
text = "(" + str(entry.castId) + ")"
if ((1024 + entry.castId) in self.movie.castInfo) and len(text):
text = self.movie.castInfo[1024 + entry.castId].name
path = QPainterPath()
painter.setPen(QPen("black"))
painter.setBrush(QBrush("red", Qt.SolidPattern))
painter.drawText(xpos, ypos + 18, text)
painter.drawPath(path)