-
Notifications
You must be signed in to change notification settings - Fork 1
/
msx2screener.py
executable file
·1129 lines (1055 loc) · 35.7 KB
/
msx2screener.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
#########################################################
# MSX2 Screener
#
# (c) 2019 Ben Ferguson
#
# Use Python 3! (Coded in 3.7.1)
#
# v1.3: -Added RLE encryption to bytes export.
# Scheme: FF FF FF FF FF AA FF
# becomes
# FF FF 05 AA FF
#
#
# Assembles z80 byte data for GRAPHIC3 (screen 4)
# screen arrangements for use with compilers.
# Easy point-and-click interface.
#
##########################################################
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import math
import sys
import os
import zipfile
# MSX2 default 16-color palette, in integer strings
defaultIntegerPalette = [
'000', '000', '161', '373',
'117', '237', '511', '267',
'711', '733', '661', '664',
'141', '625', '555', '777'
]
dotdata = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0x00, 0x10, 0x00, 0x28, 0x00, 0x5c, 0x00, 0x2e, 0x00, 0x17, 0x80, 0x0b, 0xc0, 0x05, 0xe0, 0x02, 0x70, 0x01, 0xb8, 0x00, 0x54, 0x00, 0x24, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00
};
"""
boxdata = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00,0x00, 0x00,
0x54, 0x15,0x00, 0x20,
0x04, 0x00,0x00, 0x20,
0x04, 0x00,0x00, 0x20,
0x04, 0x00,0x00, 0x20,
0x04, 0x00,0x00, 0x20,
0x04, 0x00,0xa8, 0x2a,
0x00, 0x00,0x00, 0x00
};
"""
save_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xfc, 0x3f, 0x1e, 0x78, 0x5e, 0x78, 0x5e, 0x78, 0x1e, 0x78, 0xfe, 0x7f, 0xfe, 0x7f, 0x7e, 0x7e, 0xbe, 0x7d, 0xbe, 0x7c, 0x7e, 0x7e, 0xfe, 0x7f, 0xfe, 0x6f, 0xfc, 0x3f, 0x00, 0x00
};
"""
cut_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x80, 0x00, 0x80, 0x01, 0x80, 0x02, 0x80, 0x02, 0x80, 0x02, 0x80, 0x02, 0x80, 0x32, 0x80, 0x7a, 0xff, 0xcf, 0x82, 0xce, 0xfc, 0xcb, 0x80, 0x31, 0xc0, 0x03, 0x60, 0x06, 0x60, 0x06, 0xc0, 0x03
};
"""
copy_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0xf8, 0x00, 0xac, 0x01, 0x56, 0x03, 0xab, 0x06, 0x55, 0x05, 0xab, 0x1f, 0xd5, 0x20, 0x6b, 0x40, 0x36, 0x80, 0x2c, 0x80, 0x38, 0x80, 0x20, 0x80, 0x20, 0x80, 0x40, 0x40, 0x80, 0x20, 0x00, 0x1f
};
"""
paste_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0xc0, 0x02, 0xe0, 0x05, 0x20, 0x05, 0xe0, 0x0c, 0x10, 0x09, 0x08, 0x16, 0xe8, 0x17, 0x28, 0x14, 0x28, 0x14, 0xe8, 0x17, 0x08, 0x10, 0x08, 0x10, 0xf0, 0x0f
};
"""
undo_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0x40, 0x00, 0x60, 0x00, 0xf0, 0x0f, 0xf8, 0x1f, 0xf0, 0x3f, 0x60, 0x38, 0x40, 0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x18, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
"""
redo_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0x00, 0x02, 0x00, 0x06, 0xf0, 0x0b, 0x08, 0x10, 0xf4, 0x0b, 0x14, 0x06, 0x0c, 0x02, 0x0c, 0x00, 0x0c, 0x00, 0x18, 0x00, 0x30, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
"""
integerPalette = defaultIntegerPalette.copy()
# TK Setup
app = tk.Tk()
app.title('MSX2 Screener')
boxbmp = tk.BitmapImage(data=boxdata)
dotbmp = tk.BitmapImage(data=dotdata)
save_icon = tk.BitmapImage(data=save_icon_data)
cut_icon = tk.BitmapImage(data=cut_icon_data)
copy_icon = tk.BitmapImage(data=copy_icon_data)
paste_icon = tk.BitmapImage(data=paste_icon_data)
undo_icon = tk.BitmapImage(data=undo_icon_data)
redo_icon = tk.BitmapImage(data=redo_icon_data)
# Global def
win = None
screenCanvas = None
loaded_tiles = False
screenScale = 2.5
iconScale = 2
tileSize = 8
tilePalettes = []
i = 0
while i < 3:
tilePalettes.append(None)
i += 1
m2pfilename = ''
z80filename = ''
m2cfilename = ''
tile_data = []
i = 0
while i < 3:
j = 0
blank = []
while j < 256:
s = 0
tile_data.append(blank)
j += 1
i += 1
screenpixels = []
tilepixels = []
i = 0
while i < 3:
j = 0
blank = []
tilepixels.append(blank)
while j < 32:
s = 0
while s < 8:
p = 0
while p < 8:
r = 0
while r < 8:
screenpixels.append(blank)
tilepixels[i].append(blank)
r += 1
p += 1
s += 1
j += 1
i += 1
#######
def convertIntColorToHex(intstr):
tempPalVals = []
tempPalVals.append('#')
a = math.floor((int(intstr[:-2]) / 7) * 255)
tempPalVals.append(hex(a)[2:])
if a == 0:
tempPalVals.append('0')
a = math.floor((int(intstr[1:-1]) / 7) * 255)
tempPalVals.append(hex(a)[2:])
if a == 0:
tempPalVals.append('0')
a = math.floor((int(intstr[2:]) / 7) * 255)
tempPalVals.append(hex(a)[2:])
if a == 0:
tempPalVals.append('0')
tempPalVals = ''.join(tempPalVals)
return tempPalVals
#nopattern_error = None
displayed_nopat_warn = False
screentiles = []
i = 0
while i < (256*3):
screentiles.append(0)
i += 1
last_tile_printed = -1
last_tile_used = -1
button_not_released = False
def set_undo_release(o):
global button_not_released
button_not_released = False
undo_history = []
redo_history = []
def add_undo_point():
global undo_history
global screentiles
undo_history.append(screentiles.copy())
if len(undo_history) > 100:
undo_history.pop(0)
def undo_last():
global undo_history
global screentiles
global redo_history
if len(undo_history) < 1:
return
redo_history.append(screentiles.copy())
if len(redo_history) > 100:
redo_history.pop(0)
screentiles = undo_history.pop()
refresh_whole_screen()
def redo_last():
global undo_history
global redo_history
global screentiles
if len(redo_history) > 0:
undo_history.append(screentiles.copy())
screentiles = redo_history.pop()
refresh_whole_screen()
def draw_tile(obj):
if loaded_tiles == False:
global displayed_nopat_warn
if displayed_nopat_warn == False:
displayed_nopat_warn = True
nopattern_error = messagebox.showinfo("Error","No patterns loaded.\nLoad an M2P file first!", type='ok')
if nopattern_error == 'ok':
displayed_nopat_warn = False
return
xt = math.floor(obj.x / (screenScale*8))
yt = math.floor(obj.y / (screenScale*8))
# whatever tile number we are within our tab (tab doesn't matter), set that value 0-255
# into screentiles[].
oyt = yt
global selected_tile_num
global last_tile_used
global last_tile_printed
global button_not_released
tab = 0
if yt > 7:
yt -= 8
tab += 1
if yt > 7:
yt -= 8
tab += 1
if (oyt*32)+xt == last_tile_printed and last_tile_used == selected_tile_data[tab]:
return
if selected_tile_data[tab] == []:
return
last_tile_used = selected_tile_num
if button_not_released == False:
add_undo_point()
button_not_released = True
if obj.x > 0 and obj.x <= screenCanvas.winfo_width() and obj.y > 0 and obj.y <= screenCanvas.winfo_height()\
and selected_tile_data[tab] != None:
if screentiles[(oyt*32)+xt] == selected_tile_num[tab] and last_tile_printed > -1:
return
screentiles[(oyt*32)+xt] = selected_tile_num[tab]
xp = 0
while xp < 8:
yp = 0
while yp < 8:
paint = convertIntColorToHex(integerPalette[selected_tile_data[tab][(yp*8)+xp]])
screenCanvas.itemconfig(screenpixels[(tab*32*64*8)+(yt*32*64)+(xt*64)+(yp*8)+xp], fill=paint)
yp += 1
xp += 1
global no_changes_made
no_changes_made = False
last_tile_printed = (oyt*32)+xt
def refresh_whole_screen():
loadingt = screenCanvas.create_text(300,200, text='Refreshing...', fill='black', font=('Times New Roman',24))
loadings = screenCanvas.create_text(302,202, text="Refreshing...", fill='white', font=('Times New Roman',24))
screenCanvas.update_idletasks()
i = 0
while i < 3:
xt = 0
while xt < 32:
yt = 0
while yt < 8:
tilepaint = screentiles[(i*256)+(yt*32)+xt]
tiletopaint = tile_data[(i*256)+tilepaint]
if tiletopaint == []:
RedrawScreenGrid(0)
screenCanvas.delete(loadingt)
screenCanvas.delete(loadings)
return
xp = 0
while xp < 8:
yp = 0
while yp < 8:
paint = convertIntColorToHex(integerPalette[tiletopaint[(yp*8)+xp]])
screenCanvas.itemconfig(screenpixels[(i*32*64*8)+(yt*32*64)+(xt*64)+(yp*8)+xp], fill=paint)
yp += 1 #y pixel
xp += 1 #x pixel
yt += 1
xt += 1 #tile
i += 1 #tab
RedrawScreenGrid(0)
screenCanvas.delete(loadingt)
screenCanvas.delete(loadings)
last_tile_erased = -1
def erase_tile(obj):
global last_tile_used
global button_not_released
last_tile_used = -1
if loaded_tiles == False:
return
xt = math.floor(obj.x / (screenScale*8))
yt = math.floor(obj.y / (screenScale*8))
oyt = yt
tab = 0
global last_tile_erased
if (oyt*32)+xt == last_tile_erased:
return
if yt > 7:
yt -= 8
tab += 1
if yt > 7:
yt -= 8
tab += 1
if button_not_released == False:
add_undo_point()
button_not_released = True
screentiles[(oyt*32)+xt] = 0
tab_ofs = tab*256
if obj.x > 0 and obj.x <= screenCanvas.winfo_width() and obj.y > 0 and obj.y <= screenCanvas.winfo_height():
#and selected_tile_data[tab] != None:
if screentiles[(oyt*32)+xt] == tile_data[(tab*256)+0] and last_tile_erased > -1:
return
xp = 0
while xp < 8:
yp = 0
while yp < 8:
paint = convertIntColorToHex(integerPalette[tile_data[tab_ofs][(yp*8)+xp]])
screenCanvas.itemconfig(screenpixels[(tab*32*64*8)+(yt*32*64)+(xt*64)+(yp*8)+xp], fill=paint)
yp += 1
xp += 1
global no_changes_made
no_changes_made = False
def select_tile0(obj):
select_tile(0, obj.x, obj.y)
def select_tile1(obj):
select_tile(1, obj.x, obj.y)
def select_tile2(obj):
select_tile(2, obj.x, obj.y)
selected_tile_data = []
selected_tile_num = []
i = 0
while i < 3:
selected_tile_data.append(None)
selected_tile_num.append(None)
i += 1
def select_tile(tilepalnum, xpos, ypos):
xt = math.floor(xpos/(iconScale*8))
yt = math.floor(ypos/(iconScale*8))
global selected_tile_data
global selected_tile_num
selected_tile_data[tilepalnum] = tile_data[(tilepalnum*256)+(yt*32)+xt]
selected_tile_num[tilepalnum] = (yt*32)+xt
DrawTileSelector(tilepalnum, xt, yt)
tile_selector = []
i = 0
while i < 3:
tile_selector.append(None)
i += 1
def EraseTileSelectors():
global tile_selector
i = 0
while i < 3:
tilePalettes[i].delete(tile_selector[i])
i += 1
def DrawTileSelector(tilepal, x, y):
global tile_selector
if tile_selector[tilepal] != None:
tilePalettes[tilepal].delete(tile_selector[tilepal])
tox = x * (tileSize*iconScale)
toy = y * (tileSize*iconScale)
tile_selector[tilepal] = tilePalettes[tilepal].create_rectangle(tox, toy, tox+(tileSize*iconScale), toy+(tileSize*iconScale), width=2, outline='white')
area_selector = None
selection_size = (0,0)
def DrawAreaSelector(x, y, w=1, h=1):
#default size 1, 1
global area_selector
if area_selector != None:
screenCanvas.delete(area_selector)
sx = x * (tileSize*screenScale)
sy = y * (tileSize*screenScale)
tx = sx + (tileSize*screenScale*w)
ty = sy + (tileSize*screenScale*h)
area_selector = screenCanvas.create_rectangle(sx, sy, tx, ty, width=2, outline='white')
global selection_size
selection_size = (w, h)
return
tile_pal_grid = []
i = 0
while i < 3:
blank = []
tile_pal_grid.append(blank)
i += 1
def InitTilePalettes():
global tilePalettes
# if tile palettes don't exist, create them, fill with c0
i = 0
while i < 3:
if tilePalettes[i] == None:
tilePalettes[i] = tk.Canvas(win, background=convertIntColorToHex(integerPalette[0]), width=(tileSize*32*iconScale), height=(tileSize*8*iconScale))
# clear them
tilePalettes[i].delete("all")
# align
tilePalettes[i].grid(row=4+(i*3), column=16)
# then draw grid
scale = tileSize*iconScale
x = 0
while x < 32:
tile_pal_grid[i].append(tilePalettes[i].create_line(x*scale, 0, x*scale, scale*8, fill='grey'))
x += 1
y = 0
while y < 24:
tile_pal_grid[i].append(tilePalettes[i].create_line(0, y*scale, scale*32, y*scale, fill='grey'))
y += 1
i += 1
# and bind clicks
scale8 = iconScale*8
i = 0
while i < 3:
# and populate it with 32x8x64 pixels
xt = 0
while xt < 32:
yt = 0
while yt < 8:
xp = 0
while xp < 8:
yp = 0
while yp < 8: # assign to tilepixels
tilepixels[i][(yt*32*64)+(xt*64)+(yp*8)+xp] = tilePalettes[i].create_rectangle( (xp*iconScale)+(xt*scale8),\
((yp*iconScale)+(scale8*yt),\
((xp*iconScale)+(xt*scale8))+iconScale,\
((yp*iconScale)+(scale8*yt))+iconScale), outline='')
yp += 1
xp += 1
yt += 1
xt += 1
i += 1
tilePalettes[0].bind("<Button-1>", select_tile0)
tilePalettes[1].bind("<Button-1>", select_tile1)
tilePalettes[2].bind("<Button-1>", select_tile2)
screen_grid = []
def InitScreenWindow():
global screenCanvas
# if the canvas doesn't exist, create it, fill with color 0 in hex
if screenCanvas == None:
screenCanvas = tk.Canvas(win, background=convertIntColorToHex(integerPalette[0]), width=(screenScale*32*tileSize), height=(screenScale*24*tileSize))
# and bind click events
screenCanvas.bind("<Button-1>", draw_tile)
screenCanvas.bind("<B1-Motion>", draw_tile)
screenCanvas.bind("<Button-3>", erase_tile)
screenCanvas.bind("<B3-Motion>", erase_tile)
screenCanvas.bind("<ButtonRelease-1>", RedrawScreenGrid)
screenCanvas.bind("<ButtonRelease-3>", RedrawScreenGrid)
screenCanvas.bind("<ButtonRelease-1>", set_undo_release, "+")
screenCanvas.bind("<ButtonRelease-3>", set_undo_release, "+")
# then clear it
screenCanvas.delete("all")
# set pos
screenCanvas.grid(row=3, column=3, rowspan=10, columnspan=10)
# and draw 32x24 grid
scale = screenScale * tileSize
x = 0
while x < 32:
screen_grid.append(screenCanvas.create_line(x*scale, 0, x*scale, (scale*24), fill='grey'))
x += 1
y = 0
while y < 24:
if y % 8 == 0:
screen_grid.append(screenCanvas.create_line(0, y*(scale), (scale*32), y*(scale), fill='grey', width=2))
else:
screen_grid.append(screenCanvas.create_line(0, y*(scale), (scale*32), y*(scale), fill='grey'))
y += 1
# then populate it with 3x32x8x(8x8) pixels
i = 0
while i < 3:
xt = 0
while xt < 32:
yt = 0
while yt < 8:
xp = 0
while xp < 8:
yp = 0
while yp < 8: #assign to screenpixels
screenpixels[(i*256*64)+(yt*32*64)+(xt*64)+(yp*8)+xp] = screenCanvas.create_rectangle( (xp*screenScale)+(xt*8*screenScale),\
((yp*screenScale)+(screenScale*8*yt)+(i*screenScale*64),\
((xp*screenScale)+(xt*8*screenScale))+screenScale,\
((yp*screenScale)+(screenScale*8*yt))+(i*screenScale*64)+screenScale), outline='')
yp += 1
xp += 1
yt += 1
xt += 1
i += 1
def RedrawScreenGrid(ob):
d = 0
while d < (32+24):
screenCanvas.tag_raise(screen_grid[d])
d += 1
def RedrawTileGrid():
i = 0
while i < 3:
d = 0
while d < (32+24):
tilePalettes[i].tag_raise(tile_pal_grid[i][d])
d += 1
i += 1
def LoadTileIcons():
i = 0
while i < 3:
xt = 0
while xt < 32:
yt = 0
while yt < 8:
xp = 0
while xp < 8:
yp = 0
while yp < 8: #assign to screenpixels
paint = convertIntColorToHex(integerPalette[tile_data[(i*256)+(yt*32)+xt][(yp*8)+xp]])
tilePalettes[i].itemconfig(tilepixels[i][(yt*32*64)+(xt*64)+(yp*8)+xp], fill=paint)
yp += 1
xp += 1
yt += 1
xt += 1
i += 1
RedrawTileGrid()
def import_m2p():
global m2pfilename
m2pfilename = ''
f = None
z = None
m2pfilename = tk.filedialog.askopenfilename(title='Load MSX2 Spriter file', filetypes=( ('MSX2 Spriter pattern file', '*.m2p'),('All files', '*.*') ))
if m2pfilename == '' or type(m2pfilename) == tuple:
return
else:
inbuffer = 'm2p'
zipped = False
try:
if zipfile.is_zipfile(m2pfilename):
zipped = True
z = zipfile.ZipFile(m2pfilename)
f = z.open(inbuffer, 'r')
data = f.readline().decode("utf-8")
else:
f=open(m2pfilename,'r')
# read in palette
data = f.readline()
global integerPalette
palette_vals = data.split(',')
i = 0
while i < 16:
if palette_vals[i] == 'trans':
palette_vals[i] = '000'
integerPalette[i] = palette_vals[i]
i += 1
# now read in pattern data to tile_data
n = 0
while n < 3:
s = 0
while s < 256:
if zipped:
data = f.readline().decode("utf-8").split(',')
else:
data = f.readline().split(',')
#tdat = data.split(',')
p = 0
while p < 64:
if data[p] != '':
data[p] = int(data[p])
#else:
# p -= 1
p += 1
tile_data[(n*256)+s] = data
s += 1
selected_tile_data[n] = []
n += 1
EraseTileSelectors()
LoadTileIcons()
global loaded_tiles
loaded_tiles = True
except IOError:
messagebox.showerror("I/O error", message="Failed to load file. Check drives and permissions and try again.")
except:
messagebox.showerror("Unexpected error", message="Unknown error loading file. Ensure the file is a proper M2P file.")
finally:
if(f):
f.close()
if(z):
z.close()
refresh_whole_screen()
def launch_app():
global win
global undo_history
global redo_history
if win == None:
win = tk.Frame(master=app, width=800, height=600)
win.grid(row=32, columnspan=32)
undo_history = []
redo_history = []
# open screen draw window
InitScreenWindow()
# open 3x tile palettes
InitTilePalettes()
no_changes_made = True
saved = False
def client_exit():
global no_changes_made
if no_changes_made == True:
sys.exit()
else:
result = messagebox.askquestion("Quit", "Save changes before quit?", icon='warning', type='yesnocancel')
if result == 'yes':
global saved
saved = False
save_normal()
if saved == True:
sys.exit()
else:
global filename
filename = ''
return
elif result == 'no':
sys.exit()
elif result == 'cancel':
return
def new_screen():
global loaded_tiles
global undo_history
global redo_history
if loaded_tiles == False:
messagebox.showwarning("No patterns loaded", message='No pattern file imported!\nOpening import pattern dialog.', type='ok')
import_m2p()
else:
resp = messagebox.askyesno('Load new pattern?', message='Select Yes to load a different M2P file,\nor No to use the same patterns.', type='yesno')
if resp == True:
import_m2p()
else:
pass
if loaded_tiles == False:
messagebox.showwarning('No pattern loaded', message="You can't draw without\nloading a pattern file!")
return # because they cancelled.
screentiles = []
i = 0
while i < (256*3):
screentiles.append(0)
i += 1
undo_history = []
redo_history = []
refresh_whole_screen()
return
def load_m2c():
# ask to change imported m2p
# if none is imported, show warning
global loaded_tiles
global undo_history
global redo_history
if loaded_tiles == False:
messagebox.showwarning("No patterns loaded", message='No pattern file imported!\nOpening import pattern dialog.', type='ok')
import_m2p()
else:
resp = messagebox.askyesno('Load new pattern?', message='Select Yes to load a different M2P file,\nor No to use the same patterns.', type='yesno')
if resp == True:
import_m2p()
else:
pass
if loaded_tiles == False:
return # because they cancelled.
global m2cfilename
m2cfilename = tk.filedialog.askopenfilename(title='Load MSX2 Screener file', filetypes=( ('MSX2 Screener screen file', '*.m2c'),('All files', '*.*') ))
if m2cfilename == '' or type(m2cfilename) == tuple:
return
f = None
z = None
#zipped = False
inbuffer = 'm2c'
try:
if zipfile.is_zipfile(m2cfilename):
#zipped = True
z = zipfile.ZipFile(m2cfilename)
f = z.open(inbuffer, 'r')
indata = f.readline().decode("utf-8")
else:
f = open(m2cfilename, 'r')
indata = f.readline()
#indata = f.readline()
indata = indata.split(',')
indata.pop()
global screentiles
## remove this?
i = 0
while i < (256*3):
screentiles[i] = int(indata[i])
i += 1
undo_history = []
redo_history = []
refresh_whole_screen()
except IOError:
messagebox.showerror("Load failed", message="I/O error loading file. Check drive and permissions and try again.")
except:
messagebox.showerror("Load failed", message="Unknown error loading file. This might be a bug!")
finally:
if f != None:
f.close()
if(z):
z.close()
# now that tiles are confirmed, open m2c dialog
#rle = False
def RLEEncrypt(data):
out = []
char = data[0]
i = 1
count = 1
totalcount = 0
while i <= len(data):
if i == len(data):
out.append(bytes([char]))
totalcount += count
if count > 1:
out.append(bytes([char]))
out.append(bytes([count]))
break
if char == data[i]:
if count < 255:
count += 1
i += 1
continue
else:
totalcount += count
out.append(bytes([char]))
out.append(bytes([char]))
out.append(bytes([count]))
count = 1
i += 1
continue
else:
totalcount += count
if count > 1:
out.append(bytes([char]))
out.append(bytes([char]))
out.append(bytes([count]))
else:
out.append(bytes([char]))
char = data[i]
count = 1
i += 1
continue
mbs = str(totalcount) + ' bytes compressed to ' + str(len(out)) + '\n ' + str(math.floor(len(out)/totalcount)) + '%!'
messagebox.showinfo('Results', mbs)
return out
def export_bytes():
#global rle
rle = messagebox.askquestion('Encrypt?', 'Export screen data using RLE encryption?\n\nRLE encryption will change e.g.:\nff ff ff ff a0\n to \nff ff 04 a0', icon='warning', type='yesno')
bin_file = ''
bin_file = tk.filedialog.asksaveasfilename(title='Export as raw binary')
if bin_file == '' or type(bin_file)==tuple:
return
outdata = []
i = 0
while i < 768:
outdata.append(screentiles[i])
i += 1
if rle == 'yes':
#print(rle)
outdata = RLEEncrypt(outdata)
f = None
try:
f = open(bin_file, 'wb')
for s in outdata:
#b = bytes([s])
f.write(s)
messagebox.showinfo('Export OK', message='Screen binary export successful!')
except:
messagebox.showerror('Export failed...', message='Something went wrong. Maybe a bug!')
finally:
if(f):
f.close()
def export_z80():
global z80filename
z80filename = tk.filedialog.asksaveasfilename(title='Save z80 screen data file', filetypes=( ('z80 screen data file', '*.z80'),('All files', '*.*') ))
if z80filename == '' or type(z80filename)==tuple:
return
if z80filename[-4:].upper() != '.Z80':
z80filename = z80filename + '.z80'
outdata = []
outdata.append("; Created with MSX2 Screener")
outdata.append("; ")
i = 0
while i < 96:
outstr = ' DB '
j = 0
while j < 8:
outstr = outstr + '${:02x}, '.format(screentiles[(i*8)+j])
j += 1
outstr = outstr[:-2]
outdata.append(outstr)
i += 1
f = None
try:
f = open(z80filename, 'w')
for s in outdata:
f.write(s)
f.write('\n')
messagebox.showinfo('Export OK', message='Z80 data file exported successfully.')
except IOError:
messagebox.showerror("Export failed", message="I/O error exporting file. Check drive and permissions and try again.")
except:
messagebox.showerror("Export failed", message="Unknown error exporting file. This might be a bug!")
finally:
if f != None:
f.close()
def save_m2c():
global m2cfilename
# save with global filename already set.
# it's just a csv of 768 values.
f = None
z = None
outbuffer = 'm2c'
try:
f = open(outbuffer, 'w')
#f = open(m2cfilename, 'w')
for s in screentiles:
f.write(str(s) +',')
f.close()
with zipfile.ZipFile(m2cfilename, 'w', zipfile.ZIP_DEFLATED) as z:
z.write(outbuffer)
global saved
saved = True
messagebox.showinfo("Save OK", message='Save successful!')
except IOError:
messagebox.showerror("Save failed", message="I/O error saving file. Check drive and permissions and try again.")
except:
messagebox.showerror("Save failed", message="Unknown error saving file. This might be a bug!")
finally:
os.remove('m2c')
#if f != None:
# f.close()
def save_normal():
global m2cfilename
if m2cfilename == '':
save_as()
else:
save_m2c()
def save_as():
global m2cfilename
m2cfilename = tk.filedialog.asksaveasfilename(title='Save MSX2 Screener file', filetypes=( ('MSX2 Screener screen file', '*.m2c'),('All files', '*.*') ))
if m2cfilename == '' or type(m2cfilename)==tuple:
return
if m2cfilename[-4:].upper() != '.M2C':
m2cfilename = m2cfilename + '.m2c'
save_m2c()
def open_about():
messagebox.showinfo(title='About', message='MSX2 Screener tool v1.22\n(c)2019 Ben Ferguson\nAll rights reserved n such.(Created in Python!)\n\nInfo link: https://github.com/bferguson3/msx2spriter')
topleft_sel = -1
def start_selection(obj):
x = math.floor(obj.x/(screenScale*tileSize))
y = math.floor(obj.y/(screenScale*tileSize))
ti = (y*32)+x
global topleft_sel
topleft_sel = ti
DrawAreaSelector(x, y)
return
def drag_selection(obj):
x = math.floor(obj.x/(screenScale*tileSize))
y = math.floor(obj.y/(screenScale*tileSize))
ti = (y*32)+x
global topleft_sel
if ti != topleft_sel:
ax = topleft_sel % 32
ay = math.floor(topleft_sel/32)
tx = x-ax+1
ty = y-ay+1
if tx < 1:
tx = 1
if ty < 1:
ty = 1
DrawAreaSelector(ax, ay, tx, ty)
return
copy_size = (0,0)
copy_buffer = []
def copy_selection():
global topleft_sel
global selection_size
#top left is tile number
#selection size is tiles wide and high (x,y)
global copy_size
global copy_buffer
global screentiles
copy_buffer = []
copy_size = selection_size
i = 0
while i < copy_size[1]: #y size
j = 0
while j < copy_size[0]: #x size
copy_buffer.append(screentiles[topleft_sel+(i*32)+j])
j += 1
i += 1
#print(copy_buffer)
return
def cut_selection():
global topleft_sel
global selection_size
global copy_size
global copy_buffer
global screentiles
global no_changes_made
no_changes_made = False
add_undo_point()
copy_buffer = []
copy_size = selection_size
i = 0
while i < copy_size[1]: #y size
j = 0
while j < copy_size[0]: #x size
copy_buffer.append(screentiles[topleft_sel+(i*32)+j])
screentiles[topleft_sel+(i*32)+j] = 0
j += 1
i += 1
refresh_whole_screen()
def paste_selection():
global topleft_sel
global copy_size
global copy_buffer
global screentiles
col = topleft_sel % 32
add_undo_point()
i = 0
while i < copy_size[1]:
j = 0
while j < copy_size[0]:
if topleft_sel + (i*32) + j < len(screentiles):
if col + j < 32:
screentiles[topleft_sel + (i*32) + j] = copy_buffer[(i*copy_size[0])+j]
j += 1
i += 1
refresh_whole_screen()
return
def draw_mode():
global selbutton
global pxbutton