forked from aboood40091/PointlessMaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
426 lines (315 loc) · 13.5 KB
/
widgets.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# PointlessMaker
# Version 0.2
# Copyright © 2017 MasterVermilli0n/AboodXD
# This file is part of PointlessMaker.
# PointlessMaker 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.
# PointlessMaker 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 this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt5 import QtCore, QtGui, QtWidgets
Qt = QtCore.Qt
import globals
from items import *
from objects import *
class LevelViewWidget(QtWidgets.QGraphicsView):
def __init__(self, scene, parent):
super().__init__(scene, parent)
self.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
self.setMouseTracking(True)
self.YScrollBar = QtWidgets.QScrollBar(Qt.Vertical, parent)
self.XScrollBar = QtWidgets.QScrollBar(Qt.Horizontal, parent)
self.setVerticalScrollBar(self.YScrollBar)
self.setHorizontalScrollBar(self.XScrollBar)
self.currentobj = None
self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
type_ = globals.CurrentObject
if type_ == -1 or len(globals.Area.objects) == 2600:
return
data = 0
parentFlags = 0
w, h = [globals.TileWidth] * 2
if type_ == 0:
type_ = EditRengaBlock
pix = globals.Tiles[1]
elif type_ == 1:
type_ = EditHatenaBlock
pix = globals.Tiles[2]
elif type_ == 2:
type_ = EditHardBlock
pix = globals.Tiles[6]
elif type_ == 3:
type_ = EditKumoBlock
pix = globals.Tiles[102]
elif type_ == 4:
pix = globals.Tiles[120]
type_ = EditIceBlock
elif 4 < type_ < 76:
data = type_ - 5
pix = globals.Tiles[184 + data]
type_ = EditGround
elif type_ == 76:
pix = globals.Tiles[7]
type_ = EditCoin
elif type_ == 77:
parentFlags = 0x6000844
pix = globals.Tiles[256]
type_ = EditCoin
elif type_ == 78:
pix, w, h = globals.mainWindow.paintPipe(2, 2, 0x40)
type_ = EditDokan
elif type_ == 79:
parentFlags = 0x6000800
pix, w, h = globals.mainWindow.paintPipe(2, 2, 0)
type_ = EditDokan
elif type_ == 80:
parentFlags = 0x6000860
pix, w, h = globals.mainWindow.paintPipe(2, 2, 0x60)
type_ = EditDokan
elif type_ == 81:
parentFlags = 0x6000820
pix, w, h = globals.mainWindow.paintPipe(2, 2, 0x20)
type_ = EditDokan
elif type_ == 82:
pix, w, h = globals.mainWindow.paintGroundBox(5, 5, 0)
type_ = EditGroundBox
elif type_ == 83:
pix, w, h = globals.mainWindow.paintGroundBox(5, 5, 1)
parentFlags = 0x6040840
type_ = EditGroundBox
elif type_ == 84:
pix, w, h = globals.mainWindow.paintGroundBox(5, 5, 2)
parentFlags = 0x6080840
type_ = EditGroundBox
elif type_ == 85:
pix, w, h = globals.mainWindow.paintGroundGoal(13, 2)
type_ = EditGroundGoal
elif type_ == 86:
pix, w, h = globals.mainWindow.paintGroundStart(8, 2)
type_ = EditGroundStart
clicked = globals.mainWindow.graphicsView.mapToScene(event.x(), event.y())
clickedx = clicked.x()
clickedy = clicked.y()
clickedx -= globals.TileWidth // 2
clickedy -= globals.TileWidth // 2
clickedx = (clickedx // globals.TileWidth) * globals.TileWidth
clickedy = (clickedy // globals.TileWidth) * globals.TileWidth
clickedx += globals.TileWidth // 2
clickedy += globals.TileWidth // 2
maxX = 240 * globals.TileWidth
minY = -27 * globals.TileWidth
if clickedx < 0:
clickedx = 0
elif clickedx > maxX:
clickedx = maxX
if clickedy < minY:
clickedy = minY
elif clickedy > 0:
clickedy = 0
for z in objectsZValues:
if type_ in objectsZValues[z]:
break
if type_ == EditGroundBox:
z -= 0x100000000
item = PixmapItem(type_, clickedx, clickedy, z, w, h, pix, data)
if parentFlags:
item.parentFlags = parentFlags
else:
item.parentFlags = 0x6000840
item.childFlags = 0x6000840
item.childType = -1
item.linkID = -1
item.eIndex = -1
item._1E = -1
item.cTID = -1
globals.Area.objects.append(item)
globals.mainWindow.scene.addItem(item)
elif event.button() == Qt.LeftButton and QtWidgets.QApplication.keyboardModifiers() == Qt.ShiftModifier:
pos = self.mapToScene(event.x(), event.y())
addsel = globals.mainWindow.scene.items(pos)
for i in addsel:
if (int(i.flags()) & i.ItemIsSelectable) != 0:
i.setSelected(not i.isSelected())
break
else:
super().mousePressEvent(event)
def drawForeground(self, painter, rect):
if globals.GridShown:
drawLine = painter.drawLine
GridColor = QtGui.QColor(255, 255, 255, 100)
startx = rect.x()
startx -= (startx % globals.TileWidth) + globals.TileWidth // 2
endx = startx + rect.width() + globals.TileWidth
starty = rect.y()
starty -= (starty % globals.TileWidth) + globals.TileWidth // 2
endy = starty + rect.height() + globals.TileWidth
x = startx - globals.TileWidth
while x <= endx:
x += globals.TileWidth
painter.setPen(QtGui.QPen(GridColor, 1, Qt.DotLine))
drawLine(x, starty, x, endy)
y = starty - globals.TileWidth
while y <= endy:
y += globals.TileWidth
painter.setPen(QtGui.QPen(GridColor, 1, Qt.DotLine))
drawLine(startx, y, endx, y)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, *args):
self.bgbrush = QtGui.QBrush(QtGui.QColor(119, 136, 153))
super().__init__(*args)
def drawBackground(self, painter, rect):
painter.fillRect(rect, self.bgbrush)
class ObjectPickerWidget(QtWidgets.QListView):
"""
Widget that shows a list of available objects
"""
def __init__(self):
"""
Initializes the widget
"""
super().__init__()
self.setFlow(QtWidgets.QListView.LeftToRight)
self.setLayoutMode(QtWidgets.QListView.SinglePass)
self.setMovement(QtWidgets.QListView.Static)
self.setResizeMode(QtWidgets.QListView.Adjust)
self.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
self.setWrapping(True)
self.model = self.ObjectListModel()
self.setModel(self.model)
self.setItemDelegate(self.ObjectItemDelegate())
self.clicked.connect(self.HandleObjReplace)
@QtCore.pyqtSlot(QtCore.QModelIndex, QtCore.QModelIndex)
def currentChanged(self, current, previous):
"""
Throws a signal when the selected object changed
"""
self.ObjChanged.emit(current.row())
@QtCore.pyqtSlot(QtCore.QModelIndex)
def HandleObjReplace(self, index):
"""
Throws a signal when the selected object is used as a replacement
"""
if QtWidgets.QApplication.keyboardModifiers() == Qt.AltModifier:
self.ObjReplace.emit(index.row())
ObjChanged = QtCore.pyqtSignal(int)
ObjReplace = QtCore.pyqtSignal(int)
class ObjectItemDelegate(QtWidgets.QAbstractItemDelegate):
"""
Handles tileset objects and their rendering
"""
def paint(self, painter, option, index):
"""
Paints an object
"""
if option.state & QtWidgets.QStyle.State_Selected:
painter.fillRect(option.rect, option.palette.highlight())
p = index.model().data(index, Qt.DecorationRole)
painter.drawPixmap(option.rect.x() + 2, option.rect.y() + 2, p)
# painter.drawText(option.rect, str(index.row()))
def sizeHint(self, option, index):
"""
Returns the size for the object
"""
p = index.model().data(index, Qt.UserRole)
return p
class ObjectListModel(QtCore.QAbstractListModel):
"""
Model containing all the objects in a tileset
"""
def __init__(self):
"""
Initializes the model
"""
super().__init__()
self.ritems = []
self.itemsize = []
def rowCount(self, parent=None):
"""
Required by Qt
"""
return len(self.ritems)
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.ritems): return None
if role == Qt.DecorationRole and n < len(self.ritems):
return self.ritems[n]
if role == Qt.BackgroundRole:
return QtWidgets.qApp.palette().base()
if role == Qt.UserRole and n < len(self.itemsize):
return self.itemsize[n]
if role == Qt.ToolTipRole and n < len(self.tooltips):
return self.tooltips[n]
return None
def loadObjects(self):
self.ritems = []
self.itemsize = []
self.tooltips = []
self.beginResetModel()
for type_ in TilesetObjects:
rw, rh = [globals.TileWidth] * 2
if type_ == EditGround:
for i in range(71):
pix = globals.Tiles[184 + i]
self.ritems.append(pix)
self.itemsize.append(QtCore.QSize(rw + 4, rh + 4))
self.tooltips.append('%s, type %d' % (objectNames[type_], i + 1))
continue
elif type_ == EditCoin:
pix = globals.Tiles[7]
self.ritems.append(pix)
self.itemsize.append(QtCore.QSize(rw + 4, rh + 4))
self.tooltips.append('%s, type 1' % objectNames[type_])
pix = globals.Tiles[256]
self.ritems.append(pix)
self.itemsize.append(QtCore.QSize(rw + 4, rh + 4))
self.tooltips.append('%s, type 2' % objectNames[type_])
continue
elif type_ == EditDokan:
directions = [0x40, 0, 0x60, 0x20]
for i, direction in enumerate(directions):
pix, rw, rh = globals.mainWindow.paintPipe(2, 2, direction)
self.ritems.append(pix)
self.itemsize.append(QtCore.QSize(rw + 4, rh + 4))
self.tooltips.append('%s, type %d' % (objectNames[type_], i + 1))
continue
elif type_ == EditGroundBox:
for boxType in [0, 1, 2]:
pix, rw, rh = globals.mainWindow.paintGroundBox(5, 5, boxType)
self.ritems.append(pix)
self.itemsize.append(QtCore.QSize(rw + 4, rh + 4))
self.tooltips.append('%s, type %d' % (objectNames[type_], boxType + 1))
continue
elif type_ == EditRengaBlock:
pix = globals.Tiles[1]
elif type_ == EditHatenaBlock:
pix = globals.Tiles[2]
elif type_ == EditHardBlock:
pix = globals.Tiles[6]
elif type_ == EditKumoBlock:
pix = globals.Tiles[102]
elif type_ == EditIceBlock:
pix = globals.Tiles[120]
elif type_ == EditGroundGoal:
pix, rw, rh = globals.mainWindow.paintGroundGoal(13, 2)
elif type_ == EditGroundStart:
pix, rw, rh = globals.mainWindow.paintGroundStart(8, 2)
self.ritems.append(pix)
self.itemsize.append(QtCore.QSize(rw + 4, rh + 4))
self.tooltips.append('%s' % objectNames[type_])
self.endResetModel()