-
Notifications
You must be signed in to change notification settings - Fork 25
/
items.py
3064 lines (2466 loc) · 120 KB
/
items.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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 ############
import base64
from math import copysign
import random
from PyQt5 import QtCore, QtGui, QtWidgets
Qt = QtCore.Qt
if not hasattr(QtWidgets.QGraphicsItem, 'ItemSendsGeometryChanges'):
# enables itemChange being called on QGraphicsItem
QtWidgets.QGraphicsItem.ItemSendsGeometryChanges = QtWidgets.QGraphicsItem.GraphicsItemFlag(0x800)
import globals
import spritelib as SLib
#from sprites import SpriteImage_LiquidOrFog
from tileset import RenderObject
from ui import GetIcon
from verifications import SetDirty
#################################
class LevelEditorItem(QtWidgets.QGraphicsItem):
"""
Class for any type of item that can show up in the level editor control
"""
positionChanged = None # Callback: positionChanged(LevelEditorItem obj, int oldx, int oldy, int x, int y)
autoPosChange = False
objx, objy = 0, 0
def __init__(self):
"""
Generic constructor for level editor items
"""
super().__init__()
self.setFlag(self.ItemSendsGeometryChanges, True)
def __lt__(self, other):
return (self.objx, self.objy) < (other.objx, other.objy)
def itemChange(self, change, value):
"""
Makes sure positions don't go out of bounds and updates them as necessary
"""
if change == QtWidgets.QGraphicsItem.ItemPositionChange:
if self.scene() is None:
return value
tileWidthMult = globals.TileWidth / 16
# Snap to 1x1
newpos = value
newpos.setX(int((newpos.x() + tileWidthMult / 2) / tileWidthMult) * tileWidthMult)
newpos.setY(int((newpos.y() + tileWidthMult / 2) / tileWidthMult) * tileWidthMult)
# Snap even further if Alt isn't held
if not (globals.OverrideSnapping or QtWidgets.QApplication.keyboardModifiers() == Qt.AltModifier or self.autoPosChange or hasattr(self, 'dragging') and self.dragging):
objectsSelected = any([isinstance(thing, ObjectItem) for thing in globals.mainWindow.CurrentSelection])
if not objectsSelected and self.isSelected() and len(globals.mainWindow.CurrentSelection) > 1:
# Move in sync by snapping to block halves (8x8)
old_x, old_y = self.objx * tileWidthMult, self.objy * tileWidthMult
new_x, new_y = newpos.x(), newpos.y()
delta_x, delta_y = new_x - old_x, new_y - old_y
newpos.setX(old_x + int(copysign((abs(delta_x) + globals.TileWidth / 4) / (globals.TileWidth // 2), delta_x)) * (globals.TileWidth // 2))
newpos.setY(old_y + int(copysign((abs(delta_y) + globals.TileWidth / 4) / (globals.TileWidth // 2), delta_y)) * (globals.TileWidth // 2))
elif objectsSelected and self.isSelected():
# Objects are selected, too; move in sync by snapping to whole blocks (16x16)
old_x, old_y = self.objx * tileWidthMult, self.objy * tileWidthMult
new_x, new_y = newpos.x(), newpos.y()
delta_x, delta_y = new_x - old_x, new_y - old_y
newpos.setX(old_x + int(copysign((abs(delta_x) + (globals.TileWidth // 2)) / globals.TileWidth, delta_x)) * globals.TileWidth)
newpos.setY(old_y + int(copysign((abs(delta_y) + (globals.TileWidth // 2)) / globals.TileWidth, delta_y)) * globals.TileWidth)
else:
# Snap to 8x8
newpos.setX(int(int((newpos.x() + globals.TileWidth / 4) / (globals.TileWidth // 2)) * (globals.TileWidth // 2)))
newpos.setY(int(int((newpos.y() + globals.TileWidth / 4) / (globals.TileWidth // 2)) * (globals.TileWidth // 2)))
x = newpos.x()
y = newpos.y()
# don't let it get out of the boundaries
if x < 0: newpos.setX(0)
if x > 1023 * globals.TileWidth: newpos.setX(1023 * globals.TileWidth)
if y < 0: newpos.setY(0)
if y > 511 * globals.TileWidth: newpos.setY(511 * globals.TileWidth)
# update the data
x = int(newpos.x() / tileWidthMult)
y = int(newpos.y() / tileWidthMult)
if x != self.objx or y != self.objy:
if self.scene() is not None:
self.scene().update(self.sceneBoundingRect())
if hasattr(self, 'LevelRect'):
self.LevelRect.moveTo(x / 16, y / 16)
oldx = self.objx
oldy = self.objy
self.objx = x
self.objy = y
if self.positionChanged is not None:
self.positionChanged(self, oldx, oldy, x, y)
SetDirty()
return newpos
return QtWidgets.QGraphicsItem.itemChange(self, change, value)
def getFullRect(self):
"""
Basic implementation that returns self.BoundingRect
"""
return self.BoundingRect.translated(self.pos())
def UpdateListItem(self, updateTooltipPreview=False):
"""
Updates the list item
"""
if not hasattr(self, 'listitem'): return
if self.listitem is None: return
if updateTooltipPreview:
# It's just like Qt to make this overly complicated. XP
img = self.renderInLevelIcon()
byteArray = QtCore.QByteArray()
buf = QtCore.QBuffer(byteArray)
img.save(buf, 'PNG')
byteObj = bytes(byteArray)
b64 = base64.b64encode(byteObj).decode('utf-8')
self.listitem.setToolTip('<img src="data:image/png;base64,' + b64 + '" />')
self.listitem.setText(self.ListString())
def renderInLevelIcon(self):
"""
Renders an icon of this item as it appears in the level
"""
# Constants:
# Maximum size of the preview (it will be shrunk if it exceeds this)
maxSize = QtCore.QSize(256, 256)
# Percentage of the size to use for margins
marginPct = 0.75
# Maximum margin (24 = 1 block)
maxMargin = 96
# Get the full bounding rectangle
br = self.getFullRect()
# Expand the rect to add extra margins around the edges
marginX = br.width() * marginPct
marginY = br.height() * marginPct
marginX = min(marginX, maxMargin)
marginY = min(marginY, maxMargin)
br.setX(br.x() - marginX)
br.setY(br.y() - marginY)
br.setWidth(br.width() + marginX)
br.setHeight(br.height() + marginY)
# Take the screenshot
ScreenshotImage = QtGui.QImage(br.width(), br.height(), QtGui.QImage.Format_ARGB32)
ScreenshotImage.fill(Qt.transparent)
RenderPainter = QtGui.QPainter(ScreenshotImage)
globals.mainWindow.scene.render(
RenderPainter,
QtCore.QRectF(0, 0, br.width(), br.height()),
br,
)
RenderPainter.end()
# Shrink it if it's too big
final = ScreenshotImage
if ScreenshotImage.width() > maxSize.width() or ScreenshotImage.height() > maxSize.height():
final = ScreenshotImage.scaled(
maxSize,
Qt.KeepAspectRatio,
Qt.SmoothTransformation,
)
return final
def boundingRect(self):
"""
Required for Qt
"""
return self.BoundingRect
class ObjectItem(LevelEditorItem):
"""
Level editor item that represents an ingame object
"""
def __init__(self, tileset, type, layer, x, y, width, height, z, data=0):
"""
Creates an object with specific data
"""
super().__init__()
# Specify the data value for each Item-containing block
items = {16: 1, 17: 2, 18: 3, 19: 4, 20: 5, 21: 6, 22: 7, 23: 8,
24: 9, 25: 10, 26: 11, 27: 12, 28: data, 29: 14, 30: 15,
31: 16, 32: 17, 33: 18, 34: 19, 35: 20, 36: 21, 37: 22, 38: 23, 39: 24}
self.tileset = tileset
self.type = type
self.original_type = type
self.objx = x
self.objy = y
self.layer = layer
self.width = width
self.height = height
if self.tileset == 0 and self.type in items:
# Set the data value for
# each Item-containing block.
self.data = items[self.type]
# Transform Item-containing blocks into
# ? blocks with specific data values.
# Technically, we don't even need to do this
# but this is how Nintendo did it, so... ¯\_(ツ)_/¯
self.type = 28
# Nintendo didn't use value 0 for ?
# blocks even though it's fully functional.
# Let's use value 13, like them.
if self.data == 0: self.data = 13
else:
# In NSMBU, you can transform *any* object
# from *any* tileset into a brick/?/stone/etc.
# block by changing its data value.
# (from 0 to something else)
# This was discovered by flzmx
# and AboodXD by an accident.
# The tiles' properties can also effect
# what the object will turn into
# when its data value is not 0.
# Let's hardcode the object's data value to 0
# to prevent funny stuff from happening ingame.
if data > 0: SetDirty()
self.data = 0
self.objdata = None
self.setFlag(self.ItemIsMovable, not globals.ObjectsFrozen)
self.setFlag(self.ItemIsSelectable, not globals.ObjectsFrozen)
self.UpdateRects()
self.TLGrabbed = self.TRGrabbed = self.BLGrabbed = self.BRGrabbed = False
self.MTGrabbed = self.MLGrabbed = self.MBGrabbed = self.MRGrabbed = False
self.dragging = False
self.dragstartx = -1
self.dragstarty = -1
self.objsDragging = {}
globals.DirtyOverride += 1
self.setPos(x * globals.TileWidth, y * globals.TileWidth)
globals.DirtyOverride -= 1
self.setZValue(z)
self.UpdateTooltip()
if layer == 0:
self.setVisible(globals.Layer0Shown)
elif layer == 1:
self.setVisible(globals.Layer1Shown)
elif layer == 2:
self.setVisible(globals.Layer2Shown)
self.updateObjCache()
self.UpdateTooltip()
def UpdateSearchDatabase(self):
y = 0
self.RemoveFromSearchDatabase()
import quickpaint
quickpaint.QuickPaintOperations.object_search_database[self] = []
if self.width == 1 and self.height == 1:
if not quickpaint.QuickPaintOperations.object_search_database.get((self.objx, self.objy, self.layer)):
quickpaint.QuickPaintOperations.object_search_database[self.objx, self.objy, self.layer] = []
if self not in quickpaint.QuickPaintOperations.object_search_database[(self.objx, self.objy, self.layer)]:
quickpaint.QuickPaintOperations.object_search_database[(self.objx, self.objy, self.layer)].append(self)
quickpaint.QuickPaintOperations.object_search_database[self].append((self.objx, self.objy, self.layer))
elif self.objdata:
for row in self.objdata:
x = 0
for tile in row:
if tile != -1:
if not quickpaint.QuickPaintOperations.object_search_database.get(
(self.objx + x, self.objy + y, self.layer)):
quickpaint.QuickPaintOperations.object_search_database[
self.objx + x, self.objy + y, self.layer] = []
if self not in quickpaint.QuickPaintOperations.object_search_database[
(self.objx + x, self.objy + y, self.layer)]:
quickpaint.QuickPaintOperations.object_search_database[
(self.objx + x, self.objy + y, self.layer)].append(self)
quickpaint.QuickPaintOperations.object_search_database[self].append(
(self.objx + x, self.objy + y, self.layer))
x += 1
y += 1
del quickpaint
def RemoveFromSearchDatabase(self):
import quickpaint
if self in quickpaint.QuickPaintOperations.object_search_database:
for t in quickpaint.QuickPaintOperations.object_search_database[self]:
if (quickpaint.QuickPaintOperations.object_search_database.get(t)
and self in quickpaint.QuickPaintOperations.object_search_database[t]):
quickpaint.QuickPaintOperations.object_search_database[t].remove(self)
del quickpaint.QuickPaintOperations.object_search_database[self]
del quickpaint
def SetType(self, tileset, type):
"""
Sets the type of the object
"""
self.tileset = tileset
self.type = type
self.updateObjCache()
self.update()
self.UpdateTooltip()
def UpdateTooltip(self):
"""
Updates the tooltip
"""
self.setToolTip(
globals.trans.string('Objects', 0, '[tileset]', self.tileset + 1, '[obj]', self.type, '[width]', self.width,
'[height]', self.height, '[layer]', self.layer))
def updateObjCache(self):
"""
Updates the rendered object data
"""
self.objdata = RenderObject(self.tileset, self.type, self.width, self.height)
self.randomise()
self.UpdateSearchDatabase()
def randomise(self, startx=0, starty=0, width=None, height=None):
"""
Randomises (a part of) the self.objdata according to the loaded tileset
info
"""
# TODO: Make this work even on the edges of the object. This requires a
# function that returns the tile on the block next to the current tile
# on a specified layer. Maybe something for the Area class?
if globals.ObjectDefinitions is None \
or globals.ObjectDefinitions[self.tileset] is None \
or globals.ObjectDefinitions[self.tileset][self.type] is None \
or globals.ObjectDefinitions[self.tileset][self.type].rows is None \
or globals.ObjectDefinitions[self.tileset][self.type].rows[0] is None \
or globals.ObjectDefinitions[self.tileset][self.type].rows[0][0] is None \
or len(globals.ObjectDefinitions[self.tileset][self.type].rows[0][0]) == 1:
# no randomisation info -> exit
return
obj = globals.ObjectDefinitions[self.tileset][self.type]
if (obj.width, obj.height) != (1, 1) or obj.randByte & 0xF < 2:
# cannot randomise -> exit
return
if width is None:
width = self.width
if height is None:
height = self.height
direction = obj.randByte >> 4
randLen = obj.randByte & 0xF
tile = obj.rows[0][0][1] & 0xFF
tiles = []
for z in range(randLen):
tiles.append(tile + z)
# randomise every tile in this thing
for y in range(starty, starty + height):
for x in range(startx, startx + width):
# should we randomise this tile?
tiles_ = tiles[:]
# Take direction into account - chosen tile must be different from
# the tile to the left/right/top/bottom. Using try/except here
# so the value has to be looked up only once.
# direction is 2 bits:
# highest := vertical direction; lowest := horizontal direction
if direction & 0b01:
# only look at the left neighbour, since we will generate the
# right neighbour later
try:
tiles_.remove(self.objdata[y][x-1] & 0xFF)
except:
pass
if direction & 0b10:
# only look at the above neighbour, since we will generate the
# neighbour below later
try:
tiles_.remove(self.objdata[y-1][x] & 0xFF)
except:
pass
# if we removed all options, just use the original tiles
if len(tiles_) == 0:
tiles_ = tiles
choice = (self.tileset << 8) | random.choice(tiles_)
self.objdata[y][x] = choice
def updateObjCacheWH(self, width, height):
"""
Updates the rendered object data with custom width and height
"""
# if we don't have to randomise, simply rerender everything
if globals.ObjectDefinitions is None \
or globals.ObjectDefinitions[self.tileset] is None \
or globals.ObjectDefinitions[self.tileset][self.type] is None \
or globals.ObjectDefinitions[self.tileset][self.type].rows is None \
or globals.ObjectDefinitions[self.tileset][self.type].rows[0] is None \
or globals.ObjectDefinitions[self.tileset][self.type].rows[0][0] is None \
or len(globals.ObjectDefinitions[self.tileset][self.type].rows[0][0]) == 1:
# no randomisation info -> exit
save = (self.width, self.height)
self.width, self.height = width, height
self.updateObjCache()
self.width, self.height = save
return
obj = globals.ObjectDefinitions[self.tileset][self.type]
if (obj.width, obj.height) != (1, 1) or obj.randByte & 0xF < 2:
# cannot randomise -> exit
save = (self.width, self.height)
self.width, self.height = width, height
self.updateObjCache()
self.width, self.height = save
return
self.objdata = self.objdata[:self.height]
for y in range(len(self.objdata)):
self.objdata[y] = self.objdata[y][:self.width]
self.height = len(self.objdata)
self.width = 0 if self.height == 0 else len(self.objdata[0])
if width == self.width and height == self.height:
return
if height < self.height:
self.objdata = self.objdata[:height]
elif height > self.height:
self.objdata += RenderObject(self.tileset, self.type, self.width, height - self.height)
self.randomise(0, self.height, self.width, height - self.height)
if width < self.width:
for y in range(len(self.objdata)):
self.objdata[y] = self.objdata[y][:width]
elif width > self.width:
new = RenderObject(self.tileset, self.type, width - self.width, height)
for y in range(len(self.objdata)):
self.objdata[y] += new[y]
self.randomise(self.width, 0, width - self.width, height)
self.UpdateSearchDatabase()
def UpdateRects(self):
"""
Recreates the bounding and selection rects
"""
self.prepareGeometryChange()
self.BoundingRect = QtCore.QRectF(0, 0, globals.TileWidth * self.width, globals.TileWidth * self.height)
self.SelectionRect = QtCore.QRectF(0, 0, (globals.TileWidth * self.width) - 1, (globals.TileWidth * self.height) - 1)
GrabberSide = 4 * (globals.TileWidth / 15)
self.GrabberRectTL = QtCore.QRectF(0, 0, GrabberSide, GrabberSide)
self.GrabberRectTR = QtCore.QRectF((globals.TileWidth * self.width) - GrabberSide, 0, GrabberSide, GrabberSide)
self.GrabberRectBL = QtCore.QRectF(0, (globals.TileWidth * self.height) - GrabberSide, GrabberSide, GrabberSide)
self.GrabberRectBR = QtCore.QRectF((globals.TileWidth * self.width) - GrabberSide, (globals.TileWidth * self.height) - GrabberSide, GrabberSide, GrabberSide)
self.GrabberRectMT = QtCore.QRectF(GrabberSide, 0, (globals.TileWidth * self.width) - GrabberSide * 2, GrabberSide)
self.GrabberRectML = QtCore.QRectF(0, GrabberSide, GrabberSide, (globals.TileWidth * self.height) - GrabberSide * 2)
self.GrabberRectMB = QtCore.QRectF(GrabberSide, (globals.TileWidth * self.height) - GrabberSide, (globals.TileWidth * self.width) - GrabberSide * 2, GrabberSide)
self.GrabberRectMR = QtCore.QRectF((globals.TileWidth * self.width) - GrabberSide, GrabberSide, GrabberSide, (globals.TileWidth * self.height) - GrabberSide * 2)
DrawGrabberSide = 4 * (globals.TileWidth / 20)
self.DrawGrabberRectTL = QtCore.QRectF(0, 0, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectTR = QtCore.QRectF((globals.TileWidth * self.width) - DrawGrabberSide, 0, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectBL = QtCore.QRectF(0, (globals.TileWidth * self.height) - DrawGrabberSide, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectBR = QtCore.QRectF((globals.TileWidth * self.width) - DrawGrabberSide, (globals.TileWidth * self.height) - DrawGrabberSide, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectMT = QtCore.QRectF(((globals.TileWidth * self.width) - DrawGrabberSide) / 2, 0, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectML = QtCore.QRectF(0, ((globals.TileWidth * self.height) - DrawGrabberSide) / 2, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectMB = QtCore.QRectF(((globals.TileWidth * self.width) - DrawGrabberSide) / 2, (globals.TileWidth * self.height) - DrawGrabberSide, DrawGrabberSide, DrawGrabberSide)
self.DrawGrabberRectMR = QtCore.QRectF((globals.TileWidth * self.width) - DrawGrabberSide, ((globals.TileWidth * self.height) - DrawGrabberSide) / 2, DrawGrabberSide, DrawGrabberSide)
self.LevelRect = QtCore.QRectF(self.objx, self.objy, self.width, self.height)
def itemChange(self, change, value):
"""
Makes sure positions don't go out of bounds and updates them as necessary
"""
if change == QtWidgets.QGraphicsItem.ItemPositionChange:
scene = self.scene()
if scene is None: return value
# Snap to whole blocks
newpos = value
old_x, old_y = self.objx * globals.TileWidth, self.objy * globals.TileWidth
new_x, new_y = newpos.x(), newpos.y()
delta_x, delta_y = new_x - old_x, new_y - old_y
newpos.setX(old_x + int(copysign((abs(delta_x) + (globals.TileWidth // 2)) / globals.TileWidth, delta_x)) * globals.TileWidth)
newpos.setY(old_y + int(copysign((abs(delta_y) + (globals.TileWidth // 2)) / globals.TileWidth, delta_y)) * globals.TileWidth)
x = newpos.x()
y = newpos.y()
# don't let it get out of the boundaries
if x < 0: newpos.setX(0)
if x > globals.TileWidth * 1023: newpos.setX(globals.TileWidth * 1023)
if y < 0: newpos.setY(0)
if y > globals.TileWidth * 511: newpos.setY(globals.TileWidth * 511)
# update the data
x = int(newpos.x() / globals.TileWidth)
y = int(newpos.y() / globals.TileWidth)
if x != self.objx or y != self.objy:
self.LevelRect.moveTo(x, y)
oldx = self.objx
oldy = self.objy
self.objx = x
self.objy = y
self.UpdateSearchDatabase()
if self.positionChanged is not None:
self.positionChanged(self, oldx, oldy, x, y)
SetDirty()
# updRect = QtCore.QRectF(self.x(), self.y(), self.BoundingRect.width(), self.BoundingRect.height())
# scene.invalidate(updRect)
scene.invalidate(self.x(), self.y(), self.width * globals.TileWidth, self.height * globals.TileWidth,
QtWidgets.QGraphicsScene.BackgroundLayer)
# scene.invalidate(newpos.x(), newpos.y(), self.width * globals.TileWidth, self.height * globals.TileWidth, QtWidgets.QGraphicsScene.BackgroundLayer)
return newpos
return QtWidgets.QGraphicsItem.itemChange(self, change, value)
def paint(self, painter, option, widget):
"""
Paints the object
"""
if self.isSelected():
painter.setPen(QtGui.QPen(globals.theme.color('object_lines_s'), 1, Qt.DotLine))
painter.drawRect(self.SelectionRect)
painter.fillRect(self.SelectionRect, globals.theme.color('object_fill_s'))
painter.fillRect(self.DrawGrabberRectTL, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectTR, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectBL, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectBR, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectMT, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectML, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectMB, globals.theme.color('object_lines_s'))
painter.fillRect(self.DrawGrabberRectMR, globals.theme.color('object_lines_s'))
def mousePressEvent(self, event):
"""
Overrides mouse pressing events if needed for resizing
"""
if event.button() == Qt.LeftButton:
if QtWidgets.QApplication.keyboardModifiers() == Qt.ControlModifier:
layer = globals.Area.layers[self.layer]
if len(layer) == 0:
newZ = (2 - self.layer) * 8192
else:
newZ = layer[-1].zValue() + 1
currentZ = self.zValue()
self.setZValue(newZ) # swap the Z values so it doesn't look like the cloned item is the old one
newitem = ObjectItem(self.tileset, self.type, self.layer, self.objx, self.objy, self.width, self.height,
currentZ, self.data)
layer.append(newitem)
globals.mainWindow.scene.addItem(newitem)
globals.mainWindow.scene.clearSelection()
self.setSelected(True)
SetDirty()
elif self.isSelected():
self.TLGrabbed = self.GrabberRectTL.contains(event.pos())
self.TRGrabbed = self.GrabberRectTR.contains(event.pos())
self.BLGrabbed = self.GrabberRectBL.contains(event.pos())
self.BRGrabbed = self.GrabberRectBR.contains(event.pos())
self.MTGrabbed = self.GrabberRectMT.contains(event.pos())
self.MLGrabbed = self.GrabberRectML.contains(event.pos())
self.MBGrabbed = self.GrabberRectMB.contains(event.pos())
self.MRGrabbed = self.GrabberRectMR.contains(event.pos())
if (
self.TLGrabbed
or self.TRGrabbed
or self.BLGrabbed
or self.BRGrabbed
or self.MTGrabbed
or self.MLGrabbed
or self.MBGrabbed
or self.MRGrabbed
):
# start dragging
self.dragging = True
self.dragstartx = int((event.pos().x() - globals.TileWidth // 2) / globals.TileWidth)
self.dragstarty = int((event.pos().y() - globals.TileWidth // 2) / globals.TileWidth)
self.objsDragging = {}
for selitem in globals.mainWindow.scene.selectedItems():
if not isinstance(selitem, ObjectItem):
continue
self.objsDragging[selitem] = [selitem.width, selitem.height]
self.UpdateTooltip()
self.update()
event.accept()
return
self.dragging = False
self.objsDragging = {}
LevelEditorItem.mousePressEvent(self, event)
self.UpdateTooltip()
self.update()
def UpdateObj(self, oldX, oldY):
"""
Updates the object if the width/height/position has been changed
"""
# Save the new dimensions for later
newWidth = self.width
newHeight = self.height
# Replace the dimensions with the old ones
self.width = int(self.BoundingRect.width() / globals.TileWidth)
self.height = int(self.BoundingRect.height() / globals.TileWidth)
# Update the object data
self.updateObjCacheWH(newWidth, newHeight)
# Update the dimensions
self.width = newWidth
self.height = newHeight
# Update the object's Rects and scene
oldrect = self.BoundingRect
oldrect.translate(oldX * globals.TileWidth, oldY * globals.TileWidth)
newrect = QtCore.QRectF(self.x(), self.y(), self.width * globals.TileWidth, self.height * globals.TileWidth)
updaterect = oldrect.united(newrect)
self.UpdateRects()
self.scene().update(updaterect)
globals.mainWindow.levelOverview.update()
def mouseMoveEvent(self, event):
"""
Overrides mouse movement events if needed for resizing
"""
if event.buttons() & QtCore.Qt.LeftButton and self.dragging:
# resize it
dsx = self.dragstartx
dsy = self.dragstarty
clickedx = int((event.pos().x() - globals.TileWidth // 2) / globals.TileWidth)
clickedy = int((event.pos().y() - globals.TileWidth // 2) / globals.TileWidth)
cx = self.objx
cy = self.objy
if self.TLGrabbed:
if clickedx != dsx or clickedy != dsy:
for obj in self.objsDragging:
oldWidth = self.objsDragging[obj][0] + 0
oldHeight = self.objsDragging[obj][1] + 0
self.objsDragging[obj][0] -= clickedx - dsx
self.objsDragging[obj][1] -= clickedy - dsy
newX = obj.objx + clickedx - dsx
newY = obj.objy + clickedy - dsy
newWidth = self.objsDragging[obj][0]
newHeight = self.objsDragging[obj][1]
if newWidth < 1:
newWidth = 1
if newHeight < 1:
newHeight = 1
if newX >= 0 and newX + newWidth == obj.objx + obj.width:
obj.objx = newX
obj.width = newWidth
else:
self.objsDragging[obj][0] = oldWidth
if newY >= 0 and newY + newHeight == obj.objy + obj.height:
obj.objy = newY
obj.height = newHeight
else:
self.objsDragging[obj][1] = oldHeight
obj.setPos(obj.objx * globals.TileWidth, obj.objy * globals.TileWidth)
obj.UpdateObj(cx, cy)
SetDirty()
elif self.TRGrabbed:
if clickedx < 0:
clickedx = 0
if clickedx != dsx or clickedy != dsy:
self.dragstartx = clickedx
for obj in self.objsDragging:
oldHeight = self.objsDragging[obj][1] + 0
self.objsDragging[obj][0] += clickedx - dsx
self.objsDragging[obj][1] -= clickedy - dsy
newY = obj.objy + clickedy - dsy
newWidth = self.objsDragging[obj][0]
newHeight = self.objsDragging[obj][1]
if newWidth < 1:
newWidth = 1
if newHeight < 1:
newHeight = 1
if newY >= 0 and newY + newHeight == obj.objy + obj.height:
obj.objy = newY
obj.height = newHeight
else:
self.objsDragging[obj][1] = oldHeight
obj.width = newWidth
obj.setPos(obj.objx * globals.TileWidth, obj.objy * globals.TileWidth)
obj.UpdateObj(cx, cy)
SetDirty()
elif self.BLGrabbed:
if clickedy < 0:
clickedy = 0
if clickedx != dsx or clickedy != dsy:
self.dragstarty = clickedy
for obj in self.objsDragging:
oldWidth = self.objsDragging[obj][0] + 0
self.objsDragging[obj][0] -= clickedx - dsx
self.objsDragging[obj][1] += clickedy - dsy
newX = obj.objx + clickedx - dsx
newWidth = self.objsDragging[obj][0]
newHeight = self.objsDragging[obj][1]
if newWidth < 1:
newWidth = 1
if newHeight < 1:
newHeight = 1
if newX >= 0 and newX + newWidth == obj.objx + obj.width:
obj.objx = newX
obj.width = newWidth
else:
self.objsDragging[obj][0] = oldWidth
obj.height = newHeight
obj.setPos(obj.objx * globals.TileWidth, obj.objy * globals.TileWidth)
obj.UpdateObj(cx, cy)
SetDirty()
elif self.BRGrabbed:
if clickedx < 0: clickedx = 0
if clickedy < 0: clickedy = 0
if clickedx != dsx or clickedy != dsy:
self.dragstartx = clickedx
self.dragstarty = clickedy
for obj in self.objsDragging:
self.objsDragging[obj][0] += clickedx - dsx
self.objsDragging[obj][1] += clickedy - dsy
newWidth = self.objsDragging[obj][0]
newHeight = self.objsDragging[obj][1]
if newWidth < 1:
newWidth = 1
if newHeight < 1:
newHeight = 1
obj.width = newWidth
obj.height = newHeight
obj.UpdateObj(cx, cy)
SetDirty()
elif self.MTGrabbed:
if clickedy != dsy:
for obj in self.objsDragging:
oldHeight = self.objsDragging[obj][1] + 0
self.objsDragging[obj][1] -= clickedy - dsy
newY = obj.objy + clickedy - dsy
newHeight = self.objsDragging[obj][1]
if newHeight < 1:
newHeight = 1
if newY >= 0 and newY + newHeight == obj.objy + obj.height:
obj.objy = newY
obj.height = newHeight
else:
self.objsDragging[obj][1] = oldHeight
obj.setPos(obj.objx * globals.TileWidth, obj.objy * globals.TileWidth)
obj.UpdateObj(cx, cy)
SetDirty()
elif self.MLGrabbed:
if clickedx != dsx:
for obj in self.objsDragging:
oldWidth = self.objsDragging[obj][0] + 0
self.objsDragging[obj][0] -= clickedx - dsx
newX = obj.objx + clickedx - dsx
newWidth = self.objsDragging[obj][0]
if newWidth < 1:
newWidth = 1
if newX >= 0 and newX + newWidth == obj.objx + obj.width:
obj.objx = newX
obj.width = newWidth
else:
self.objsDragging[obj][0] = oldWidth
obj.setPos(obj.objx * globals.TileWidth, obj.objy * globals.TileWidth)
obj.UpdateObj(cx, cy)
SetDirty()
elif self.MBGrabbed:
if clickedy < 0:
clickedy = 0
if clickedy != dsy:
self.dragstarty = clickedy
for obj in self.objsDragging:
self.objsDragging[obj][1] += clickedy - dsy
newHeight = self.objsDragging[obj][1]
if newHeight < 1:
newHeight = 1
obj.height = newHeight
obj.UpdateObj(cx, cy)
SetDirty()
elif self.MRGrabbed:
if clickedx < 0:
clickedx = 0
if clickedx != dsx:
self.dragstartx = clickedx
for obj in self.objsDragging:
self.objsDragging[obj][0] += clickedx - dsx
newWidth = self.objsDragging[obj][0]
if newWidth < 1:
newWidth = 1
obj.width = newWidth
obj.UpdateObj(cx, cy)
SetDirty()
event.accept()
else:
LevelEditorItem.mouseMoveEvent(self, event)
self.UpdateTooltip()
def mouseReleaseEvent(self, event):
"""
Disables "dragging" when the mouse is released
"""
LevelEditorItem.mouseReleaseEvent(self, event)
if event.button() == QtCore.Qt.LeftButton:
self.TLGrabbed = self.TRGrabbed = self.BLGrabbed = self.BRGrabbed = False
self.MTGrabbed = self.MLGrabbed = self.MBGrabbed = self.MRGrabbed = False
self.dragging = False
self.update()
def delete(self):
"""
Delete the object from the level
"""
self.RemoveFromSearchDatabase()
globals.Area.RemoveFromLayer(self)
self.scene().update(self.x(), self.y(), self.BoundingRect.width(), self.BoundingRect.height())
class ZoneItem(LevelEditorItem):