-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1726 lines (1435 loc) · 61.2 KB
/
main.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
import os
import sys
import time
import typing
from collections import defaultdict, deque
from functools import partial, wraps
from PyQt5 import uic, QtGui, QtPrintSupport
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QPixmap, QCloseEvent
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QScrollArea,
QMdiArea,
QMdiSubWindow,
QAction,
QStatusBar,
QFileDialog,
QMessageBox,
QWidget,
QSplashScreen,
QMenu,
)
import palettes
import symbol
from commands import (
UpdateLinePaletteCode,
CommandsGroup,
UpdateLineBackgroundPaletteCode,
ClearPixels,
UpdatePixels,
)
from dialogs.about import AboutDialog
from dialogs.new import NewDialog
from dialogs.text import InsertText
from models import (
ScanlineModel,
ToolboxTool,
Command,
PixelModel,
PlayfieldMode,
)
from persistency import save_playfield, load_playfield
from symbol import Symbol
from tools import (
ObservableProperty,
combine,
is_iterable,
resource_path,
run_x,
error_box,
)
from widgets import WPlayfield, WPixel, WPalette, WScanline
version = "202104.A"
font_ext = ".font"
fonts = {}
class MouseEventHandler:
supported_keyboard_modifiers = [
Qt.NoModifier,
Qt.ControlModifier,
Qt.ShiftModifier,
Qt.AltModifier,
]
def __init__(self):
self._mapping = defaultdict(list)
def register(
self,
*,
tools: typing.Optional[
typing.Union[typing.Iterable[ToolboxTool], ToolboxTool]
] = None,
buttons: Qt.MouseButton = Qt.MouseButton.NoButton,
keyboard_modifiers: typing.Optional[
typing.Union[typing.Iterable[int], int]
] = None,
):
def decorator(f):
for tool in (
(tools if is_iterable(tools) else (tools,))
if tools is not None
else ToolboxTool
):
for modifier in (
(
keyboard_modifiers
if is_iterable(keyboard_modifiers)
else (keyboard_modifiers,)
)
if keyboard_modifiers is not None
else self.supported_keyboard_modifiers
):
if modifier not in self.supported_keyboard_modifiers:
raise ValueError(str(modifier))
self._mapping[(tool, buttons, modifier)].append(f)
return f
return decorator
def __call__(
self,
*,
tool: ToolboxTool,
event: typing.Union[QtGui.QMouseEvent, QtGui.QWheelEvent],
**kwargs,
):
buttons = (
event.buttons()
if isinstance(event, QtGui.QMouseEvent)
else Qt.MouseButton.NoButton
)
funcs = self._mapping.get((tool, buttons, event.modifiers().__int__()))
if funcs:
for f in funcs:
f(tool=tool, event=event, **kwargs)
class Main(QMainWindow):
_window_title_prefix = f"Playfield Pixel Perfect Pro {version}"
_pf_cursor_size = QSize(4, 4)
_load_save_filter = "PPPP project (*.pppp);; All Files (*.*)"
_export_png_filter = "PNG (*.png);; All Files (*.*)"
_default_zoom = 2
_asm_rows = {
"PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}\t; {y}",
"PF0_PF1_PF2_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}\t; {y}",
"PF0_PF1_PF2_COLUPF_COLUBK": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.palette_code.value:02X}, ${line.model.bg_palette_code.value:02X}\t; {y}",
"PF0_PF1_PF2_COLUPF": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.palette_code.value:02X}\t; {y}",
"PF0_PF1_PF2_COLUBK": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.bg_palette_code.value:02X}\t; {y}",
"PF0_PF1_PF2_PF0_PF1_PF2_COLUPF_COLUBK": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}, ${line.model.palette_code.value:02X}, ${line.model.bg_palette_code.value:02X}\t; {y}",
"PF0_PF1_PF2_PF0_PF1_PF2_COLUPF": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}, ${line.model.palette_code.value:02X}\t; {y}",
"PF0_PF1_PF2_PF0_PF1_PF2_COLUBK": lambda y, line: f"\t.byte ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}, ${line.model.bg_palette_code.value:02X}\t; {y}",
"PF0_COLUPF_PF1_PF2_PF0_COLUPF_PF1_PF2": lambda y, line: f"\t.byte ${(line.model.pf0 | ((line.model.palette_code.value & 0xF0) >> 4)):02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor | (line.model.palette_code.value & 0x0F):02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}\t; {y}",
"PF0_COLUBK_PF1_PF2_PF0_COLUBK_PF1_PF2": lambda y, line: f"\t.byte ${(line.model.pf0 | ((line.model.bg_palette_code.value & 0xF0) >> 4)):02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor | (line.model.bg_palette_code.value & 0x0F):02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}\t; {y}",
"PF0_COLUPF_PF1_PF2_PF0_COLUPF_PF1_PF2_COLUBK": lambda y, line: f"\t.byte ${(line.model.pf0 | ((line.model.palette_code.value & 0xF0)) >> 4):02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor | (line.model.palette_code.value & 0x0F):02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}, ${line.model.bg_palette_code.value:02X}\t; {y}",
"PF0_COLUBK_PF1_PF2_PF0_COLUBK_PF1_PF2_COLUPF": lambda y, line: f"\t.byte ${(line.model.pf0 | ((line.model.bg_palette_code.value & 0xF0) >> 4)):02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor | (line.model.bg_palette_code.value & 0x0F):02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}, ${line.model.palette_code.value:02X}\t; {y}",
"COLUPF_COLUBK_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.palette_code.value:02X}, ${line.model.bg_palette_code.value:02X}, ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}\t; {y}",
"COLUPF_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.palette_code.value:02X}, ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}\t; {y}",
"COLUBK_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.bg_palette_code.value:02X}, ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}\t; {y}",
"COLUPF_COLUBK_PF0_PF1_PF2_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.palette_code.value:02X}, ${line.model.bg_palette_code.value:02X}, ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}\t; {y}",
"COLUPF_PF0_PF1_PF2_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.palette_code.value:02X}, ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}\t; {y}",
"COLUBK_PF0_PF1_PF2_PF0_PF1_PF2": lambda y, line: f"\t.byte ${line.model.bg_palette_code.value:02X}, ${line.model.pf0:02X}, ${line.model.pf1:02X}, ${line.model.pf2:02X}, ${line.model.pf0_neighbor:02X}, ${line.model.pf1_neighbor:02X}, ${line.model.pf2_neighbor:02X}\t; {y}",
}
def copy_asm_to_clipboard(self, data: typing.List[str]):
data.insert(0, f"\t; auto-generated by Playfield Pixel Perfect Pro {version}")
cb = QApplication.clipboard()
cb.clear(mode=cb.Clipboard)
cb.setText("\n".join(data), mode=cb.Clipboard)
QMessageBox.information(
self,
"ASM",
"Assembly data copied to clipboard",
QMessageBox.Ok,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
uic.loadUi(resource_path("ui/main.ui"), self)
self._cursors = {
ToolboxTool.Pen: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/pen.png")), 3, 15
),
ToolboxTool.Brush: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/brush.png")), 8, 2
),
ToolboxTool.Bucket: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/bucket.png")), 13, 11
),
ToolboxTool.Line: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/line.png")), 8, 17
),
ToolboxTool.ColorPicker: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/picker.png")), 4, 13
),
ToolboxTool.Selection: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/selection.png")), 4, 4
),
ToolboxTool.Eraser: QtGui.QCursor(
QPixmap(resource_path("assets/cursors/18/eraser.png")), 3, 14
),
}
self._pf_icon = QtGui.QIcon(
resource_path("assets/icons/24/common-file-empty.png")
)
self._mouse_press_handler = MouseEventHandler()
self._mouse_move_handler = MouseEventHandler()
self._wheel_handler = MouseEventHandler()
self.register_mouse_actions()
self.setWindowTitle(self._window_title_prefix)
self._playfields = {}
self._active_pf = None
self._mdi_area = typing.cast(QMdiArea, self.findChild(QMdiArea, "mdiArea"))
self._action_file_new = typing.cast(
QAction, self.findChild(QAction, "actionFileNew")
)
self._action_file_save = typing.cast(
QAction, self.findChild(QAction, "actionFileSave")
)
self._action_file_save_as = typing.cast(
QAction, self.findChild(QAction, "actionFileSaveAs")
)
self._action_file_export_to_png = typing.cast(
QAction, self.findChild(QAction, "actionFileExportToPng")
)
self._action_file_load = typing.cast(
QAction, self.findChild(QAction, "actionFileLoad")
)
self._action_file_print = typing.cast(
QAction, self.findChild(QAction, "actionFilePrint")
)
self._action_file_exit = typing.cast(
QAction, self.findChild(QAction, "actionFileExit")
)
self._action_file_asm_registers = typing.cast(
QAction, self.findChild(QAction, "actionFileAsmRegisters")
)
self._menu_file_asm = typing.cast(QMenu, self.findChild(QMenu, "menuFileAsm"))
self._action_edit_clear = typing.cast(
QAction, self.findChild(QAction, "actionEditClear")
)
self._action_edit_undo = typing.cast(
QAction, self.findChild(QAction, "actionEditUndo")
)
self._action_edit_redo = typing.cast(
QAction, self.findChild(QAction, "actionEditRedo")
)
self._action_edit_toolbox_pen = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxPen")
)
self._action_edit_toolbox_brush = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxBrush")
)
self._action_edit_toolbox_selection = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxSelection")
)
self._action_edit_toolbox_bucket = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxBucket")
)
self._action_edit_toolbox_eraser = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxEraser")
)
self._action_edit_toolbox_eraser = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxEraser")
)
self._action_edit_toolbox_color_picker = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxColorPicker")
)
self._action_edit_toolbox_line = typing.cast(
QAction, self.findChild(QAction, "actionEditToolboxLine")
)
self._toolbox_actions = [
(self._action_edit_toolbox_pen, ToolboxTool.Pen),
(self._action_edit_toolbox_brush, ToolboxTool.Brush),
(self._action_edit_toolbox_selection, ToolboxTool.Selection),
(self._action_edit_toolbox_bucket, ToolboxTool.Bucket),
(self._action_edit_toolbox_eraser, ToolboxTool.Eraser),
(self._action_edit_toolbox_color_picker, ToolboxTool.ColorPicker),
(self._action_edit_toolbox_line, ToolboxTool.Line),
]
self._action_edit_selection_move_up = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionMoveUp")
)
self._action_edit_selection_move_down = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionMoveDown")
)
self._action_edit_selection_move_left = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionMoveLeft")
)
self._action_edit_selection_move_right = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionMoveRight")
)
self._action_edit_selection_text = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionText")
)
self._action_edit_selection_copy = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionCopy")
)
self._action_edit_selection_cut = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionCut")
)
self._action_edit_selection_delete = typing.cast(
QAction, self.findChild(QAction, "actionEditSelectionDelete")
)
self._selection_actions = [
self._action_edit_selection_move_up,
self._action_edit_selection_move_down,
self._action_edit_selection_move_left,
self._action_edit_selection_move_right,
self._action_edit_selection_text,
self._action_edit_selection_copy,
self._action_edit_selection_cut,
self._action_edit_selection_delete,
]
self._action_view_zoom_in = typing.cast(
QAction, self.findChild(QAction, "actionViewZoomIn")
)
self._action_view_zoom_out = typing.cast(
QAction, self.findChild(QAction, "actionViewZoomOut")
)
self._action_view_foreground_palette = typing.cast(
QAction, self.findChild(QAction, "actionViewForegroundPalette")
)
self._action_view_background_palette = typing.cast(
QAction, self.findChild(QAction, "actionViewBackgroundPalette")
)
self._action_help_about = typing.cast(
QAction, self.findChild(QAction, "actionHelpAbout")
)
self._status_bar = typing.cast(
QStatusBar, self.findChild(QStatusBar, "statusbar")
)
self.setCentralWidget(self._mdi_area)
self.showMaximized()
self._palette = self.add_palette(
title="foreground", color_mapping=palettes.ntsc
)
self._bg_palette = self.add_palette(
title="background", color_mapping=palettes.ntsc
)
def close_event(event: QCloseEvent):
for pf in self._playfields.values():
if not self.ask_save_dialog(pf=pf):
event.ignore()
return
event.accept()
self.closeEvent = close_event
@self._palette.model.code.observe
def on_color_change(code: int, _):
for pf in self._playfields.values():
pf.model.palette_code.value = code
@self._bg_palette.model.code.observe
def on_bg_color_change(code: int, _):
for pf in self._playfields.values():
pf.model.bg_palette_code.value = code
def on_file_new_click():
# def init(parent, *args, **kwargs) -> WPlayfield:
# return WPlayfield(
# name=f"asd",
# mode=PlayfieldMode.Asymmetric,
# color_system=ColorSystem.NTSC,
# scanline_count=192,
# zoom=ObservableProperty(2),
# parent=parent,
# *args,
# **kwargs,
# )
#
# self.add_playfield(init=init)
# return
dlg = NewDialog(self)
if dlg.exec():
def init(parent: QWidget, *args, **kwargs) -> WPlayfield:
return WPlayfield(
name=dlg.name,
mode=dlg.mode,
color_system=dlg.color_system,
scanline_count=dlg.scanlines,
zoom=ObservableProperty(self._default_zoom),
parent=parent,
*args,
**kwargs,
)
self.add_playfield(init=init)
self._action_file_new.triggered.connect(on_file_new_click)
def on_file_save_click(as_: bool):
if self.active_pf:
self.file_save_dialog(pf=self.active_pf, as_=as_)
self._action_file_save.triggered.connect(partial(on_file_save_click, False))
self._action_file_save_as.triggered.connect(partial(on_file_save_click, True))
self._asm_actions = []
def on_file_asm_action_click(
action: QAction, f: typing.Callable[[int, WScanline], str]
):
if self.active_pf:
data = [f"\t; {action.text()}", "Data:"]
for y, line in enumerate(self.active_pf.scanlines):
data.append(f(y, line))
self.copy_asm_to_clipboard(data=data)
for k, v in self._asm_rows.items():
action_ = typing.cast(
QAction, self.findChild(QAction, f"actionFileAsmRows_{k}")
)
self._asm_actions.append(action_)
action_.triggered.connect(partial(on_file_asm_action_click, action_, v))
def on_file_asm_registers_click():
if self.active_pf:
bytes_in_row = 8
pf0_raw = []
pf1_raw = []
pf2_raw = []
colupf_raw = []
colubk_raw = []
for line in self.active_pf.scanlines:
pf0_raw.append(f"${line.model.pf0:02X}")
pf1_raw.append(f"${line.model.pf1:02X}")
pf2_raw.append(f"${line.model.pf2:02X}")
if self.active_pf.model.mode == PlayfieldMode.Asymmetric:
pf0_raw.append(f"${line.model.pf0_neighbor:02X}")
pf1_raw.append(f"${line.model.pf1_neighbor:02X}")
pf2_raw.append(f"${line.model.pf2_neighbor:02X}")
colupf_raw.append(f"${line.model.palette_code.value:02X}")
colubk_raw.append(f"${line.model.bg_palette_code.value:02X}")
pf0_data = [
f"\t.byte {', '.join(pf0_raw[i : i + bytes_in_row])}"
for i in range(0, len(pf0_raw), bytes_in_row)
]
pf1_data = [
f"\t.byte {', '.join(pf1_raw[i: i + bytes_in_row])}"
for i in range(0, len(pf1_raw), bytes_in_row)
]
pf2_data = [
f"\t.byte {', '.join(pf2_raw[i: i + bytes_in_row])}"
for i in range(0, len(pf2_raw), bytes_in_row)
]
colupf_data = [
f"\t.byte {', '.join(colupf_raw[i: i + bytes_in_row])}"
for i in range(0, len(colupf_raw), bytes_in_row)
]
colubk_data = [
f"\t.byte {', '.join(colubk_raw[i: i + bytes_in_row])}"
for i in range(0, len(colubk_raw), bytes_in_row)
]
data = ["DataPF0:"]
data.extend(pf0_data)
data.append("\nDataPF1:")
data.extend(pf1_data)
data.append("\nDataPF2:")
data.extend(pf2_data)
data.append("\nDataCOLUPF:")
data.extend(colupf_data)
data.append("\nDataCOLUBK:")
data.extend(colubk_data)
self.copy_asm_to_clipboard(data=data)
self._action_file_asm_registers.triggered.connect(on_file_asm_registers_click)
def on_file_export_to_png_click():
if self.active_pf:
self.png_export_dialog(self.active_pf)
self._action_file_export_to_png.triggered.connect(on_file_export_to_png_click)
def on_file_load_click():
def init(from_: str, *args, **kwargs):
return load_playfield(
from_, *args, zoom=ObservableProperty(self._default_zoom), **kwargs
)
files_to_load, _ = QFileDialog.getOpenFileNames(
self,
caption="Load project",
directory="",
filter=self._load_save_filter,
)
if files_to_load:
for filename in files_to_load:
pf = self.add_playfield(partial(init, filename))
pf.model.filename = filename
self.update_playfield_window_title(pf=pf)
self._action_file_load.triggered.connect(on_file_load_click)
def on_file_print_click():
if self.active_pf:
self.clear_selection(pf=self.active_pf)
self.send_to_printer(pf=self.active_pf)
self._action_file_print.triggered.connect(on_file_print_click)
def on_file_exit_click():
self.close()
self._action_file_exit.triggered.connect(on_file_exit_click)
def on_view_zoom_click(in_: bool):
if self.active_pf:
self.zoom_in_out(pf=self.active_pf, in_=in_)
self._action_view_zoom_in.triggered.connect(partial(on_view_zoom_click, True))
self._action_view_zoom_out.triggered.connect(partial(on_view_zoom_click, False))
def on_edit_clear_click():
if self.active_pf:
self.clear_selection(pf=self.active_pf)
self.execute(
pf=self.active_pf,
command=ClearPixels(
pf=self.active_pf,
code=self.active_pf.model.bg_palette_code.value,
),
)
self._action_edit_clear.triggered.connect(on_edit_clear_click)
def on_edit_undo_click():
if self.active_pf:
self.clear_selection(pf=self.active_pf)
self.undo(pf=self.active_pf)
self._action_edit_undo.triggered.connect(on_edit_undo_click)
def on_edit_redo_click():
if self.active_pf:
self.clear_selection(pf=self.active_pf)
self.redo(pf=self.active_pf)
self._action_edit_redo.triggered.connect(on_edit_redo_click)
def on_view_foreground_palette_click():
if self._action_view_foreground_palette.isChecked():
self._palette.parent().show()
else:
self._palette.parent().hide()
self._action_view_foreground_palette.triggered.connect(
on_view_foreground_palette_click
)
def on_view_background_palette_click():
if self._action_view_background_palette.isChecked():
self._bg_palette.parent().show()
else:
self._bg_palette.parent().hide()
self._action_view_background_palette.triggered.connect(
on_view_background_palette_click
)
def on_toolbox_item_selection(action: QAction, tool: ToolboxTool):
for toolbox_action, _ in self._toolbox_actions:
toolbox_action.setChecked(toolbox_action is action)
self._toolbox_tool = tool
for action in self._selection_actions:
action.setEnabled(tool == ToolboxTool.Selection)
if tool != ToolboxTool.Selection and self.active_pf is not None:
self.clear_selection(self.active_pf)
for pf in self._playfields.values():
pf.setCursor(self._cursors[tool])
for toolbox_action_, toolbox_item in self._toolbox_actions:
toolbox_action_.triggered.connect(
partial(
on_toolbox_item_selection,
toolbox_action_,
toolbox_item,
)
)
on_toolbox_item_selection(self._action_edit_toolbox_pen, ToolboxTool.Pen)
self._action_edit_selection_move_up.triggered.connect(
lambda: self.rotate_up(self.active_pf)
)
self._action_edit_selection_move_down.triggered.connect(
lambda: self.rotate_down(self.active_pf)
)
self._action_edit_selection_move_left.triggered.connect(
lambda: self.rotate_left(self.active_pf)
)
self._action_edit_selection_move_right.triggered.connect(
lambda: self.rotate_right(self.active_pf)
)
self._action_edit_selection_copy.triggered.connect(
lambda: self.copy_selection(self.active_pf)
)
self._action_edit_selection_cut.triggered.connect(
lambda: self.cut_selection(self.active_pf)
)
self._action_edit_selection_delete.triggered.connect(
lambda: self.delete_selection(self.active_pf)
)
@error_box(Exception, text=lambda err: str(err), parent=self)
@error_box(KeyError, text=lambda err: f"Font {str(err)} not found", parent=self)
def on_edit_selection_text_click(_):
if self.active_pf:
if len(fonts) == 0:
raise Exception("No fonts found")
dlg = InsertText(self, fonts=fonts.keys())
if dlg.exec():
font = fonts[dlg.font]
self.clear_selection(pf=self.active_pf)
self.draw_text(
y=1,
x=1,
font=font,
pf=self.active_pf,
text=dlg.text,
)
self._action_edit_selection_text.triggered.connect(on_edit_selection_text_click)
def on_help_about_click():
AboutDialog(version=version).exec()
self._action_help_about.triggered.connect(on_help_about_click)
self.active_pf = typing.cast(WPlayfield, None)
def undo(self, pf: WPlayfield):
pf.model.undo()
self._action_edit_undo.setEnabled(len(pf.model.undo_commands) > 0)
self._action_edit_redo.setEnabled(len(pf.model.redo_commands) > 0)
def redo(self, pf: WPlayfield):
pf.model.redo()
self._action_edit_undo.setEnabled(len(pf.model.undo_commands) > 0)
self._action_edit_redo.setEnabled(len(pf.model.redo_commands) > 0)
def set_window_title(self, text: str):
self.setWindowTitle(
f"{self._window_title_prefix} - {text}"
if text
else self._window_title_prefix
)
@property
def active_pf(self) -> WPlayfield:
return self._active_pf
@active_pf.setter
def active_pf(self, value: WPlayfield):
if not value:
self.set_window_title(text=typing.cast(str, None))
elif self._active_pf is not value:
self.set_window_title(text=value.model.name)
self._active_pf = value
self._palette.model.color_mapping.value = value.model.color_mapping
self._bg_palette.model.color_mapping.value = value.model.color_mapping
self._action_file_export_to_png.setEnabled(value is not None)
self._action_file_save.setEnabled(value is not None)
self._action_file_save_as.setEnabled(value is not None)
self._action_file_print.setEnabled(value is not None)
self._action_view_zoom_in.setEnabled(value is not None)
self._action_view_zoom_out.setEnabled(value is not None)
self._action_edit_clear.setEnabled(value is not None)
self._action_edit_undo.setEnabled(
value is not None and not value.model.undo_commands.empty()
)
self._action_edit_redo.setEnabled(
value is not None and not value.model.redo_commands.empty()
)
self._menu_file_asm.setEnabled(value is not None)
for action in self._asm_actions:
action.setEnabled(value is not None)
for action, _ in self._toolbox_actions:
action.setEnabled(value is not None)
for action in self._selection_actions:
action.setEnabled(
value is not None and self._toolbox_tool == ToolboxTool.Selection
)
def ask_save_dialog(self, pf: WPlayfield) -> bool:
if pf.model.need_save:
answer = QMessageBox.question(
self,
"Save",
f"Save changes to {pf.model.name}?",
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
)
if answer == QMessageBox.Cancel:
return False
if answer == QMessageBox.Yes:
self.file_save_dialog(pf=pf, as_=False)
return True
def file_save_dialog(self, pf: WPlayfield, as_: bool):
if pf:
if not as_ and pf.model.filename:
file_to_save = pf.model.filename
else:
file_to_save, _ = QFileDialog.getSaveFileName(
self,
caption=f"Save {pf.model.name}",
directory=pf.model.filename if pf.model.filename else pf.model.name,
filter=self._load_save_filter,
)
if file_to_save:
save_playfield(pf=pf, to=file_to_save, version=version)
pf.model.need_save = False
pf.model.filename = file_to_save
self.update_playfield_window_title(pf=pf)
self._status_bar.showMessage(
f"Saved {pf.model.name} to {file_to_save}", 3000
)
def png_export_dialog(self, pf: WPlayfield):
if pf:
filename, _ = QFileDialog.getSaveFileName(
self,
caption=f"Export {pf.model.name} to PNG",
directory=pf.model.name,
filter=self._export_png_filter,
)
pf.grab().save(filename, "png")
def zoom_in_out(self, pf: WPlayfield, in_: bool):
self._status_bar.showMessage(
f"{(pf.model.zoom_in() if in_ else pf.model.zoom_out()) * 100}%",
1000,
)
def add_palette(
self, title: str, color_mapping: typing.Mapping[int, str]
) -> WPalette:
sub = QMdiSubWindow()
sub.setWindowFlags(
Qt.WindowType.Window
| Qt.WindowType.WindowTitleHint
| Qt.WindowType.CustomizeWindowHint
| Qt.WindowType.WindowStaysOnTopHint
)
palette = WPalette(color_mapping=ObservableProperty(color_mapping), parent=sub)
palette.show()
sub.setFixedSize(palette.size())
sub.setWidget(palette)
sub.setWindowTitle(title)
self._mdi_area.addSubWindow(sub)
sub.show()
return palette
@staticmethod
def update_playfield_window_title(pf: WPlayfield):
sub = pf.parent().parent()
title = f"{pf.model.name} - {pf.model.mode.name} - {pf.model.color_system.name} - {pf.model.scanline_count}"
if pf.model.filename:
title += f" ({pf.model.filename})"
sub.setWindowTitle(title)
def add_playfield(
self,
init: typing.Callable[..., WPlayfield],
) -> WPlayfield:
sub = QMdiSubWindow()
scroll_area = QScrollArea(sub)
scroll_area.setAlignment(Qt.AlignCenter)
pf = init(parent=scroll_area)
pf.setCursor(self._cursors[self._toolbox_tool])
sub.resize(pf.size() * 1.1)
sub.setWindowIcon(self._pf_icon)
pf.show()
pf.model.palette_code.value = self._palette.model.code.value
pf.model.bg_palette_code.value = self._bg_palette.model.code.value
scroll_area.setWidget(pf)
sub.setWidget(scroll_area)
self.update_playfield_window_title(pf)
self._mdi_area.addSubWindow(sub)
sub.show()
def focus_in_event(*args, **kwargs):
self.active_pf = pf
scroll_area.mousePressEvent = combine(
scroll_area.mousePressEvent, focus_in_event
)
sub.focusInEvent = combine(sub.focusInEvent, focus_in_event)
pf_id = pf.__hash__()
self._playfields[pf_id] = pf
def close_event(event: QCloseEvent):
if self.ask_save_dialog(pf=pf):
del self._playfields[pf_id]
self.active_pf = typing.cast(WPlayfield, None)
if len(self._playfields) != 0:
self.active_pf = next(iter(self._playfields.values()))
event.accept()
else:
event.ignore()
sub.closeEvent = close_event
# def key_press_event(event: QKeyEvent):
# #print(event.key())
# if event.key() == Qt.Key.Key_P:
#
#
# sub.keyPressEvent = combine(sub.keyPressEvent, key_press_event)
def scroll_area_wheel_event(
f: typing.Callable[[QtGui.QWheelEvent], None], event: QtGui.QWheelEvent
):
if QApplication.keyboardModifiers() == Qt.ControlModifier:
event.ignore()
else:
f(event)
scroll_area.wheelEvent = partial(
scroll_area_wheel_event, scroll_area.wheelEvent
)
pf.on_scanline_mouse_press_event = combine(
pf.on_scanline_mouse_press_event,
partial(self.on_playfield_mouse_press_event, pf),
)
pf.on_scanline_mouse_move_event = combine(
pf.on_scanline_mouse_move_event,
partial(self.on_playfield_mouse_move_event, pf),
)
pf.on_scanline_wheel_event = combine(
pf.on_scanline_wheel_event,
partial(self.on_playfield_wheel_event, pf),
)
def brush(event: QtGui.QMouseEvent):
if (
self._toolbox_tool != ToolboxTool.Brush
or event.buttons() not in (Qt.LeftButton, Qt.RightButton)
or event.modifiers() != Qt.NoModifier
):
return
zoom = pf.model.zoom.value
x = int(event.x() / (zoom * PixelModel.default_width))
y = int(event.y() / (zoom * PixelModel.default_height))
if (
x < 0
or y < 0
or x > ScanlineModel.pixel_count - 1
or y > pf.model.scanline_count - 1
):
return
tool = (
ToolboxTool.Pen
if event.buttons() == Qt.LeftButton
else ToolboxTool.Eraser
)
event.buttons = lambda: Qt.NoButton
event.modifiers = lambda: Qt.ShiftModifier
line = pf[y]
pixel = line[x]
self._mouse_move_handler(
tool=tool,
pf=pf,
line=line,
y=y,
x=x,
pixel=pixel,
event=event,
neighbor=pf.model.neighbor(x),
)
def select(event: QtGui.QMouseEvent):
if (
self._toolbox_tool != ToolboxTool.Selection
or event.buttons() != Qt.LeftButton
or event.modifiers() != Qt.NoModifier
):
return
zoom = pf.model.zoom.value
x = int(event.x() / (zoom * PixelModel.default_width))
y = int(event.y() / (zoom * PixelModel.default_height))
if (
x < 0
or y < 0
or x > ScanlineModel.pixel_count - 1
or y > pf.model.scanline_count - 1
):
return
line = pf[y]
pixel = line[x]
self._mouse_move_handler(
tool=ToolboxTool.Selection,
pf=pf,
line=line,
y=y,
x=x,
pixel=pixel,
event=event,
neighbor=pf.model.neighbor(x),
)
def drag(event: QtGui.QMouseEvent):
if (