-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathstamp.py
312 lines (243 loc) · 9.12 KB
/
stamp.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Miyamoto! Level Editor - New Super Mario Bros. U Level Editor
# Copyright (C) 2009-2021 Treeki, Tempus, angelsl, JasonP27, Kinnay,
# MalStar1000, RoadrunnerWMC, MrRean, Grop, AboodXD, Gota7, John10v10,
# mrbengtsson
# This file is part of Miyamoto!.
# Miyamoto! is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Miyamoto! is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Miyamoto!. If not, see <http://www.gnu.org/licenses/>.
################################################################
################################################################
############ Imports ############
from PyQt5 import QtCore, QtGui, QtWidgets
Qt = QtCore.Qt
import globals
#################################
class Stamp:
"""
Class that represents a stamp in the list
"""
def __init__(self, MiyamotoClip=None, Name=''):
"""
Initializes the stamp
"""
self.MiyamotoClip = MiyamotoClip
self.Name = Name
self.Icon = self.render()
def renderPreview(self):
"""
Renders the stamp preview
"""
minX, minY, maxX, maxY = 24576, 12288, 0, 0
layers, sprites = globals.mainWindow.getEncodedObjects(self.MiyamotoClip, False)
# Go through the sprites and find the maxs and mins
for spr in sprites:
br = spr.getFullRect()
x1 = br.topLeft().x()
y1 = br.topLeft().y()
x2 = x1 + br.width()
y2 = y1 + br.height()
if x1 < minX: minX = x1
if x2 > maxX: maxX = x2
if y1 < minY: minY = y1
if y2 > maxY: maxY = y2
# Go through the objects and find the maxs and mins
for layer in layers:
for obj in layer:
x1 = (obj.objx * globals.TileWidth)
x2 = x1 + (obj.width * globals.TileWidth)
y1 = (obj.objy * globals.TileWidth)
y2 = y1 + (obj.height * globals.TileWidth)
if x1 < minX: minX = x1
if x2 > maxX: maxX = x2
if y1 < minY: minY = y1
if y2 > maxY: maxY = y2
# Calculate offset amounts (snap to TileWidthxTileWidth increments)
offsetX = int(minX // globals.TileWidth) * globals.TileWidth
offsetY = int(minY // globals.TileWidth) * globals.TileWidth
drawOffsetX = offsetX - minX
drawOffsetY = offsetY - minY
# Go through the things again and shift them by the offset amount
for spr in sprites:
spr.objx -= offsetX / (globals.TileWidth / 16)
spr.objy -= offsetY / (globals.TileWidth / 16)
for layer in layers:
for obj in layer:
obj.objx -= offsetX // globals.TileWidth
obj.objy -= offsetY // globals.TileWidth
# Calculate the required pixmap size
pixmapSize = (maxX - minX, maxY - minY)
# Create the pixmap, and a painter
pix = QtGui.QPixmap(pixmapSize[0], pixmapSize[1])
pix.fill(Qt.transparent)
painter = QtGui.QPainter(pix)
painter.setRenderHint(painter.Antialiasing)
# Paint all objects
objw, objh = int(pixmapSize[0] // globals.TileWidth) + 1, int(pixmapSize[1] // globals.TileWidth) + 1
for layer in reversed(layers):
tmap = []
for i in range(objh):
tmap.append([-1] * objw)
for obj in layer:
startx = int(obj.objx)
starty = int(obj.objy)
desty = starty
for row in obj.objdata:
destrow = tmap[desty]
destx = startx
for tile in row:
if tile > 0:
destrow[destx] = tile
destx += 1
desty += 1
painter.save()
desty = 0
for row in tmap:
destx = 0
for tile in row:
if tile > 0:
if globals.Tiles[tile] is None: continue
r = globals.Tiles[tile].main
if r is None: continue
painter.drawPixmap(destx + drawOffsetX, desty + drawOffsetY, r)
destx += globals.TileWidth
desty += globals.TileWidth
painter.restore()
# Paint all sprites
for spr in sprites:
offx = ((spr.objx + spr.ImageObj.xOffset) * globals.TileWidth / 16) + drawOffsetX
offy = ((spr.objy + spr.ImageObj.yOffset) * globals.TileWidth / 16) + drawOffsetY
painter.save()
painter.translate(offx, offy)
spr.paint(painter, None, None, True)
painter.restore()
# Paint any auxiliary things
for aux in spr.ImageObj.aux:
painter.save()
painter.translate(
offx + aux.x(),
offy + aux.y(),
)
aux.paint(painter, None, None)
painter.restore()
# End painting
painter.end()
del painter
# Scale it
maxW, maxH = 96, 96
w, h = pix.width(), pix.height()
if w > h and w > maxW:
pix = pix.scaledToWidth(maxW)
elif h > w and h > maxH:
pix = pix.scaledToHeight(maxH)
# Return it
return pix
def render(self):
"""
Renders the stamp icon, preview AND text
"""
# Get the preview icon
prevIcon = self.renderPreview()
# Calculate the total size of the icon
textSize = self.calculateTextSize(self.Name)
totalWidth = max(prevIcon.width(), textSize.width())
totalHeight = prevIcon.height() + 2 + textSize.height()
# Make a pixmap and painter
pix = QtGui.QPixmap(totalWidth, totalHeight)
pix.fill(Qt.transparent)
painter = QtGui.QPainter(pix)
# Draw the preview
iconXOffset = (totalWidth - prevIcon.width()) / 2
painter.drawPixmap(iconXOffset, 0, prevIcon)
# Draw the text
textRect = QtCore.QRectF(0, prevIcon.height() + 2, totalWidth, textSize.height())
painter.setFont(QtGui.QFont())
painter.drawText(textRect, Qt.AlignTop | Qt.TextWordWrap, self.Name)
# Return the pixmap
return pix
@staticmethod
def calculateTextSize(text):
"""
Calculates the size of text. Crops to 96 pixels wide.
"""
fontMetrics = QtGui.QFontMetrics(QtGui.QFont())
fontRect = fontMetrics.boundingRect(QtCore.QRect(0, 0, 96, 48), Qt.TextWordWrap, text)
w, h = fontRect.width(), fontRect.height()
return QtCore.QSizeF(min(w, 96), h)
def update(self):
"""
Updates the stamp icon
"""
self.Icon = self.render()
class StampListModel(QtCore.QAbstractListModel):
"""
Model containing all the stamps
"""
def __init__(self):
"""
Initializes the model
"""
super().__init__()
self.items = [] # list of Stamp objects
def rowCount(self, parent=None):
"""
Required by Qt
"""
return len(self.items)
def data(self, index, role=Qt.DisplayRole):
"""
Get what we have for a specific row
"""
if not index.isValid(): return None
n = index.row()
if n < 0: return None
if n >= len(self.items): return None
if role == Qt.DecorationRole:
return self.items[n].Icon
elif role == Qt.BackgroundRole:
return QtWidgets.qApp.palette().base()
elif role == Qt.UserRole:
return self.items[n].Name
elif role == Qt.StatusTipRole:
return self.items[n].Name
else:
return None
def setData(self, index, value, role=Qt.DisplayRole):
"""
Set data for a specific row
"""
if not index.isValid(): return None
n = index.row()
if n < 0: return None
if n >= len(self.items): return None
if role == Qt.UserRole:
self.items[n].Name = value
def addStamp(self, stamp):
"""
Adds a stamp
"""
# Start resetting
self.beginResetModel()
# Add the stamp to self.items
self.items.append(stamp)
# Finish resetting
self.endResetModel()
def removeStamp(self, stamp):
"""
Removes a stamp
"""
# Start resetting
self.beginResetModel()
# Remove the stamp from self.items
self.items.remove(stamp)
# Finish resetting
self.endResetModel()