-
Notifications
You must be signed in to change notification settings - Fork 1
/
msx2bitmapper.py
executable file
·2870 lines (2638 loc) · 93.2 KB
/
msx2bitmapper.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 Bitmapper
#
# (c) 2019 Ben Ferguson
# (w/contributions from jlbeard83)
# Use Python 3! (Coded in 3.7.1)
#
# v1.1: Added pattern mode export
#
# Assembles z80 byte data for GRAPHIC4-7 (SCREEN5-8)
# bitmap graphics for use with compilers.
# Also supports export to pattern mode (GRAPHIC1-2).
# Easy point-and-click interface.
#
##########################################################
# to-do: add support for any res
import tkinter as tk
import sys
import math
import os
import zipfile
# before anything else:
# define TK
app = tk.Tk()
graphics_mode_width = 256
graphics_mode_192 = 192
graphics_mode_212 = 212
graphics_mode_height = graphics_mode_192
app_scale = 1
zoom_scale = 1
screen_data = []
screen_pixels = []
y_ratio = (3/4)
graphic_mode = 'G4'
draw_mode = 'PX'
selector_rect = None
image_x = 0
image_y = 0
# 'LINE', 'CIRCLE', 'SQUARE', 'SELECT'
###### BIT MAP DATA #######
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
};
"""
scale_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0xff, 0x00, 0xff, 0x01, 0x83, 0x00, 0x81, 0x08, 0x85, 0x00, 0xff, 0x40, 0xf8, 0xff, 0xf8, 0xff, 0xf8, 0xff, 0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0xf8, 0xff
};
"""
zoom1_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xf0, 0x00, 0xf8, 0x01, 0x0c, 0x03, 0x36, 0x06, 0x16, 0x06, 0x06, 0x06, 0x06, 0x06, 0x0c, 0x03, 0xf8, 0x07, 0xf4, 0x0e, 0x06, 0x1c, 0x04, 0x38, 0xa4, 0x70, 0x44, 0xe0, 0xae, 0xc0
};
"""
zoom2_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xf0, 0x00, 0xf8, 0x01, 0x0c, 0x03, 0x36, 0x06, 0x16, 0x06, 0x06, 0x06, 0x06, 0x06, 0x0c, 0x03, 0xf8, 0x07, 0xfe, 0x0e, 0x09, 0x1c, 0x08, 0x38, 0xa4, 0xc0, 0x42, 0xe0, 0xaf, 0xc0
};
"""
zoom4_icon_data= """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xf0, 0x00, 0xf8, 0x01, 0x0c, 0x03, 0x36, 0x06, 0x16, 0x06, 0x06, 0x06, 0x06, 0x06, 0x0c, 0x03, 0xf8, 0x07, 0xf1, 0x0e, 0x09, 0x1c, 0x0f, 0x38, 0xa8, 0x70, 0x48, 0xe0, 0xa8, 0xc0
};
"""
zoom8_icon_data= """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xf0, 0x00, 0xf8, 0x01, 0x0c, 0x03, 0x36, 0x06, 0x16, 0x06, 0x06, 0x06, 0x06, 0x06, 0x0c, 0x03, 0xf8, 0x07, 0xf6, 0x0e, 0x09, 0x1c, 0x06, 0x38, 0xa9, 0x70, 0x49, 0xe0, 0xa6, 0xc0
};
"""
line_icon_data= """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0x14, 0x00, 0x08, 0x00, 0x14, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x50, 0x00, 0x20, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
};
"""
circle_icon_data= """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xe0, 0x07, 0x10, 0x08, 0x08, 0x10, 0x04, 0x28, 0x02, 0x44, 0x02, 0x42, 0x82, 0x41, 0x82, 0x41, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10, 0x10, 0x08, 0xe0, 0x07, 0x00, 0x00, 0x00, 0x00
};
"""
rect_icon_data= """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xfe, 0x3f, 0x02, 0x20, 0x02, 0x20, 0x02, 0x20, 0x02, 0x20, 0x82, 0x7f, 0x82, 0x40, 0x82, 0x40, 0xfe, 0x40, 0x80, 0x40, 0xfc, 0x43, 0x04, 0x42, 0x04, 0x7e, 0xfc, 0x03, 0x00, 0x00
};
"""
select_icon_data = """
#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
};
"""
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
};
"""
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
};
"""
bucket_icon_data = """
#define im_width 16
#define im_height 16
static char im_bits[] = {
0x00, 0x00, 0xe0, 0x1f, 0x50, 0x20, 0xa8, 0x40, 0x54, 0x81, 0xac, 0x81, 0x54, 0x81, 0xac, 0x81, 0xf8, 0x40, 0x70, 0x20, 0xf0, 0x1f, 0x30, 0x00, 0x30, 0x00, 0x78, 0x00,
0xfe, 0x01, 0xff, 0x13
};
"""
dotbmp = tk.BitmapImage(data=dotdata)
scale_icon = tk.BitmapImage(data=scale_icon_data)
zoom1_icon = tk.BitmapImage(data=zoom1_icon_data)
zoom2_icon = tk.BitmapImage(data=zoom2_icon_data)
zoom4_icon = tk.BitmapImage(data=zoom4_icon_data)
zoom8_icon = tk.BitmapImage(data=zoom8_icon_data)
line_icon = tk.BitmapImage(data=line_icon_data)
circle_icon = tk.BitmapImage(data=circle_icon_data)
rect_icon = tk.BitmapImage(data=rect_icon_data)
select_icon = tk.BitmapImage(data=select_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)
save_icon = tk.BitmapImage(data=save_icon_data)
bucket_icon = tk.BitmapImage(data=bucket_icon_data)
# init screen data
def init_screen_data(mode='G4', expanded=False, interlace=False, cx=0, cy=0):
global screen_data
global graphics_mode_width
global graphics_mode_height
global graphics_mode_192
global graphics_mode_212
global y_ratio
global graphic_mode
graphic_mode = mode
if (mode == 'G4') or (mode == 'G7'):
graphics_mode_width = 256
if not interlace:
y_ratio = 1
else:
y_ratio = 0.5
elif (mode == 'G5') or (mode == 'G6'):
graphics_mode_width = 512
if not interlace:
y_ratio = 2
else:
y_ratio = 1
if expanded == False:
graphics_mode_height = graphics_mode_192
if interlace:
graphics_mode_height = 384
else:
graphics_mode_height = graphics_mode_212
if interlace:
graphics_mode_height = 424
custom = False
if cx != 0 and cy != 0:
custom = True
screen_data = []
i = 0
if custom:
while i < (cx*cy):
if mode != 'G7':
screen_data.append(0)
else:
screen_data.append('#000')
i += 1
else:
while i < (graphics_mode_width*graphics_mode_height):
if mode != 'G7':
screen_data.append(0)
else:
screen_data.append('#000')
i += 1
grid_lines=[]
def init_canvas_grid():
global drawCanvas
global app_scale
global zoom_scale
global grid_lines
grid_lines=[]
w = (app_scale*zoom_scale)
h = (app_scale*zoom_scale)*y_ratio
global image_x
global image_y
if image_y != 0:
myh = image_y
else:
myh = graphics_mode_height
if image_x != 0:
myw = image_x
else:
myw = graphics_mode_width
y2 = (app_scale*zoom_scale*myh)*y_ratio
l = myw
i = 0
if (app_scale + zoom_scale) <= 4:
fill = ''
else:
fill = '#333'
if zoom_scale < 4:
fill = ''
while i < l:
p = drawCanvas.create_line(i*w, 0, i*w, y2, fill=fill)
grid_lines.append(p)
i += 1
l = myh
x2 = (app_scale*zoom_scale*myw)
i = 0
while i < l:
p = drawCanvas.create_line(0, i*h, x2, i*h, fill=fill)
grid_lines.append(p)
i += 1
return
def update_canvas_grid():
global drawCanvas
global app_scale
global zoom_scale
global grid_lines
global image_x
global image_y
if image_x != 0:
myw = image_x
else:
myw = graphics_mode_width
if image_y != 0:
myh = image_y
else:
myh = graphics_mode_height
w = (app_scale*zoom_scale)
h = (app_scale*zoom_scale)*y_ratio
y2 = (app_scale*zoom_scale*myh)*y_ratio
l = myw
i = 0
while i < l:
drawCanvas.coords(grid_lines[i], i*w, 0, i*w, y2)
if (app_scale + zoom_scale <= 4) or (zoom_scale < 4):
drawCanvas.itemconfig(grid_lines[i], fill='')
else:
drawCanvas.itemconfig(grid_lines[i], fill='#333')
i += 1
#i = 0
l = myh
x2 = (app_scale*zoom_scale*myw)
i = 0
while i < l:
drawCanvas.coords(grid_lines[i+myw], 0, i*h, x2, i*h)
if (app_scale + zoom_scale <= 4) or (zoom_scale < 4):
drawCanvas.itemconfig(grid_lines[i+myw], fill='')
else:
drawCanvas.itemconfig(grid_lines[i+myw], fill='#333')
i += 1
return
# define base window dimensions
#app.config(background='black')
win = tk.Frame(master=app)
win.grid(row=0, column=0)
class drawFrame(tk.Frame):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
d = drawFrame(win, padx=20, pady=20, width=256*app_scale, height=graphics_mode_height*app_scale*y_ratio)#, background='black')
d.grid(row = 1, column=1, rowspan=20, columnspan=20, sticky='w')
drawCanvas = tk.Canvas(d, width=256*app_scale, height=graphics_mode_height*app_scale*y_ratio, background='black', scrollregion=(0,0,graphics_mode_width*app_scale*zoom_scale, graphics_mode_height*app_scale*zoom_scale*y_ratio))
drawCanvas.grid(row=1, column=1, rowspan=20, columnspan=20, sticky='w')
d.config(width=graphics_mode_width*app_scale, height=graphics_mode_height*app_scale)
draw_scroll_y = tk.Scrollbar(d, orient=tk.VERTICAL, command=drawCanvas.yview)
draw_scroll_y.grid(row=1, rowspan=20, column=21, sticky='ns')
draw_scroll_x = tk.Scrollbar(d, orient=tk.HORIZONTAL, command=drawCanvas.xview)
draw_scroll_x.grid(row=21, column=1, columnspan=20, sticky='ew')
drawCanvas.config(xscrollcommand=draw_scroll_x.set, yscrollcommand=draw_scroll_y.set)
def unclick_all():
b = 0
while b < len(palette_display):
palette_display[b].unclicked()
b += 1
if graphic_mode == 'G4' or graphic_mode == 'G7':
scale = 15*app_scale
else:
scale = 30*app_scale
palette_display = []
palwin = None
if palwin:
palwin.mainloop()
def find_and_replace(num, oldc, newc):
print('Replacing palette number ' + str(num) + '. (Old: ' + oldc + ', new: ' + newc + ')')
global screen_data
global screen_pixels
global drawCanvas
i = 0
while i < len(screen_data):
if screen_data[i] == num:
drawCanvas.itemconfig(screen_pixels[i], fill=newc)
i += 1
class palwin_popup(tk.Tk):
def __init__(self, col):
if col == -1:
return
super().__init__()
global app_scale
global graphic_mode
if graphic_mode == 'G7':
myw = 128
else:
myw = 256
self.myscale = app_scale * 8
self.palnum = col
global palette_display
palette_display[self.palnum].clicked(0)
self.overrideredirect(1)
self.move_to_mouse()
self.frame = tk.Frame(master=self,width=myw*self.myscale, height=128*self.myscale, background='black')
self.frame.grid(row=0,column=0)
self.canvas = tk.Canvas(self.frame, background='black',width=myw*self.myscale, height=128*self.myscale)
self.canvas.grid(row=0,column=0)
self.populate_colors()
self.bind("<Button-1>", self.clicked_color)
def clicked_color(self,o):
x = math.floor(o.x/(self.myscale))
y = math.floor(o.y/self.myscale)
global palette_display
global graphic_mode
global selected_palette_no
global hex_palette
if graphic_mode != 'G7':
find_and_replace(self.palnum, palette_display[self.palnum].myVal, self.colors[(y*32)+x])
palette_display[self.palnum].setVal(self.colors[(y*32)+x])
hex_palette[selected_palette_no] = palette_display[self.palnum].myVal
else:
palette_display[self.palnum].setVal(self.colors[(y*16)+x])
hex_palette[selected_palette_no] = palette_display[self.palnum].myVal
self.withdraw()
def mainloop(self):
super().mainloop()
def populate_colors(self):
self.colors=[]
self.coloricons=[]
global graphic_mode
if graphic_mode == 'G7':
blues = 4
prows = 16
else:
blues = 8
prows = 32
c1 = 0
while c1 < 8:
c2 = 0
while c2 < 8:
c3 = 0
while c3 < blues: #0/2/4/7/9/A/C/F
a = math.floor((c1/7)*15 )
b = math.floor((c2/7)*15)
if blues == 4:
m = 2
else:
m = 1
if c3 != 3:
c = math.floor(((c3*m)/7)*15)
else:
c = 15
self.colors.append('#'+format(a,'1x') + format(b,'1x')+format(c,'1x'))
c3 +=1
c2 += 1
c1 += 1
# now put them on the window
#global app_scale
self.canvas.delete("all")
s = self.myscale
t = 0
j = 0
while j < 16:
i = 0
while i < prows:
f = self.colors[t]
self.coloricons.append(self.canvas.create_rectangle(i*s, j*s, (i*s)+s, (j*s)+s, fill=f))
t += 1
i += 1
j += 1
def resize(self):
if self.state() == 'withdrawn':
self.deiconify()
global app_scale
self.myscale = app_scale * 8
global graphic_mode
if graphic_mode == 'G7':
myw = 16
else:
myw = 32
s = self.myscale
j = 0
while j < 16:
i = 0
while i < myw:
self.canvas.coords(self.coloricons[(j*myw)+i], i*s, j*s, (i*s)+s, (j*s)+s)
i += 1
j += 1
def change_palnum(self, col):
self.palnum = col
global palette_display
palette_display[self.palnum].clicked(0)
self.resize()
self.move_to_mouse()
def move_to_mouse(self):
global app
x,y=app.winfo_pointerxy()
global graphic_mode
if graphic_mode == 'G7':
myw = 16
else:
myw = 32
pos=[self.myscale*myw, self.myscale*16, x+5, y+5]
sx = app.winfo_screenwidth()
sy = app.winfo_screenheight()
if ((pos[0] + pos[2]) > sx):
pos[2] = sx-pos[0]
if ((pos[1]+pos[3])>sy): # width + x
pos[3] = sy-pos[1]
self.geometry('%dx%d+%d+%d' % (pos[0], pos[1], pos[2], pos[3]))
class PaletteButton(tk.Canvas):
def __init__(self, *args, **kwargs):
tk.Canvas.__init__(self, *args, **kwargs)
self.lbl = 0
self.lbl2 = 0
self.swapping = False
self.selector=[]
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
self.bind("<Button-1>", self.clicked)
self.bind("<Button-3>", self.open_palette)
def open_palette(self, event):
i = 0
col = -1
while i < len(palette_display):
if palette_display[i] == self:
col = i
break
i += 1
global selected_palette_no
selected_palette_no = col
global palwin
if palwin == None:
palwin = palwin_popup(col)
else:
palwin.deiconify()
palwin.change_palnum(col)
def on_enter(self, event):
self.delete(self.lbl)
self.delete(self.lbl2)
self.lbl2 = self.create_text(17, 17, text=self.myVal, fill='white')
self.lbl = self.create_text(16, 16, text=self.myVal)
def on_leave(self, enter):
self.delete(self.lbl)
self.delete(self.lbl2)
def setVal(self, num):
self.myVal = num
#global hex_palette
#hex_palette[selected_palette_no] = self.myVal
self.config(background=self.myVal)
def unclicked(self):
b = 0
while b < len(self.selector):
self.delete(self.selector[b])
b += 1
def clicked(self, event):
unclick_all()
self.selector.append(self.create_line(2, 2, scale, 2, width=3, fill='yellow'))
self.selector.append(self.create_line(2, 2, 2, scale, width=3, fill='yellow'))
self.selector.append(self.create_line(5, scale, scale, scale, width=3, fill='yellow'))
self.selector.append(self.create_line(scale, 5, scale, scale, width=3, fill='yellow'))
i = 0
global selected_palette_no
while i < len(palette_display):
if palette_display[i] == self:
selected_palette_no = i
break
i += 1
global palwin
if palwin:
if palwin.state() == "normal":
palwin.withdraw()
#
#
selected_palette_no = 15
hex_palette = [ '#000', '#000', '#2C2', '#6F6',
'#22F', '#46F', '#A22', '#4CF',
'#F22', '#F66', '#CC2', '#CC8',
'#282', '#C4A', '#AAA', '#FFF' ]
#0/2/4/6/8/A/C/F
default_palette = list(hex_palette)
palette_display = []
i = 0
while i < 16:
palette_display.append(PaletteButton(win, width=scale, height=scale, background=hex_palette[i]))
i += 1
def add_palette_display():
global palette_display
global scale
global hex_palette
global graphic_mode
global win
if graphic_mode != 'G5':
palnums = 16
else:
palnums = 4
i = 0
while i < len(palette_display):
if palette_display[i]:
palette_display[i].grid_forget()
i += 1
i = 0
while i < palnums:
palette_display[i].grid(row=i + 1, column=32, sticky='w')
palette_display[i].setVal(hex_palette[i])
i += 1
def rescale_palette():
global palette_display
global scale
global win
global selected_palette_no
global graphic_mode
if graphic_mode == 'G4' or graphic_mode == 'G7':
scale = 12*app_scale
else:
scale = 24*app_scale
scale = int(scale)
i = 0
while i < len(palette_display):
palette_display[i].config(width=scale, height=scale)
i += 1
palette_display[selected_palette_no].clicked(0)
def init_screen_pixels(custom_x=0):
global screen_pixels
global drawCanvas
global app_scale
global zoom_scale
global screen_data
if palwin:
palwin.populate_colors()
palwin.withdraw()
screen_pixels = []
drawCanvas.delete("all")
l = len(screen_data)
i = 0
w = (app_scale*zoom_scale)
h = (app_scale*zoom_scale)*y_ratio
ourwidth = 0
if custom_x != 0:
ourwidth = custom_x
else:
ourwidth = graphics_mode_width
while i < l:
# draw every pixel to the canvas, and append its reference to the array.
xp = i % ourwidth
xp = xp * app_scale * zoom_scale
yp = math.floor(i/ourwidth)
yp = yp * app_scale * zoom_scale * y_ratio
# x pos is (l % graphics_mode_width)*
p = drawCanvas.create_rectangle(xp, yp, xp+w, yp+h, outline='')
screen_pixels.append(p)
i += 1
init_canvas_grid()
add_palette_display()
def reset_screen_pixels():
global screen_data
global drawCanvas
global screen_pixels
global hex_palette
l = len(screen_data)
i = 0
while i < l:
drawCanvas.itemconfig(screen_pixels[i], fill=hex_palette[0])
i += 1
lastpx = (-1,-1)
def clicked_loc(o):
global drawCanvas
global app_scale
global zoom_scale
global lastpx
scr_x = draw_scroll_x.get()
fscr = drawCanvas.cget('scrollregion')
fscr = fscr.split(' ')
xofs = scr_x[0] * float(fscr[2]) # should be 1:1 px left bounding
scale = app_scale*zoom_scale
px = math.floor((xofs+o.x) / scale)
scr_y = draw_scroll_y.get()
yofs = scr_y[0] * float(fscr[3])
py = math.floor((yofs + o.y) / (scale*y_ratio))
if (px,py) != lastpx:
lastpx = (px, py)
color_pixel(px, py)
scroll_orig = (0,0)
def set_scroll_orig(o):
global drawCanvas
fscr = drawCanvas.cget('scrollregion')
fscr = fscr.split(' ')
global scroll_orig
scroll_orig = (o.x, o.y)
return scroll_orig
new_scrollbarpos = (0,0)
class xypos(object):
def __init__(self, x, y):
self.x = x
self.y = y
def set_drawwindow_pos(o):
global drawCanvas
fscr = drawCanvas.cget('scrollregion')
fscr = fscr.split(' ')
scr_rt = o.x / float(fscr[2])
scr_rty = o.y / float(fscr[3])
drawCanvas.xview(tk.MOVETO, scr_rt)
drawCanvas.yview(tk.MOVETO, scr_rty)
def scroll_drawwindow(o):
global scroll_orig
global drawCanvas
global zoom_scale
if zoom_scale == 1:
return
xo = o.x - scroll_orig[0]
yo = o.y - scroll_orig[1]
scr_x = draw_scroll_x.get()
scr_y = draw_scroll_y.get()
if (scr_x[0] == 0) and (xo > 0):
# cant scroll left any more
xo = 0
if (scr_y[0] == 0) and (yo > 0):
yo = 0
fscr = drawCanvas.cget('scrollregion')
fscr = fscr.split(' ')
xofs = scr_x[0] * float(fscr[2])
yofs = scr_y[0] * float(fscr[3])
xo = (-1*xo)+xofs
yo = (-1*yo)+yofs
if xo < 0:
xo = 0
if yo < 0:
yo = 0
if xo > (float(fscr[2]) - drawCanvas.winfo_width()):
xo = float(fscr[2])
if yo > (float(fscr[3]) - drawCanvas.winfo_height()):
yo = float(fscr[3])
# ^^^^ THIS IS ALL GOOD!
# # what we want is to set the left most bounding of the screen
# # to xo + xofs.
scr_rt = xo / float(fscr[2])
scr_rty = yo / float(fscr[3])
drawCanvas.xview(tk.MOVETO, scr_rt)
drawCanvas.yview(tk.MOVETO, scr_rty)
scroll_orig = set_scroll_orig(o)
return
max_scale = True
def toggle_scale(scale=1, interlace=False):
#used in 'd' and 'drawCanvas'
sx = app.winfo_screenwidth()
sy = app.winfo_screenheight()
global max_scale
global app_scale
global graphic_mode
tscale = app_scale + 1
if graphic_mode == 'G4' or graphic_mode == 'G7':
ys = 30
tscale = app_scale+1
else:
if interlace:
ys = 30
tscale = app_scale+1
ys = 50
if max_scale == True:
app_scale = 1
tscale = 1
max_scale = False
if (graphics_mode_height*tscale*y_ratio)+(ys*tscale)+60 > sy:
#print('broke y')
app_scale = sy/(((graphics_mode_height*y_ratio)+60)+ys)
max_scale = True
elif (graphics_mode_width*tscale)+(160*tscale) > sx:
#print('broke x')
app_scale = sx/((graphics_mode_width)+160)
max_scale = True
else:
app_scale = tscale
if app_scale == 1 and (graphic_mode == 'G4' or graphic_mode == 'G7'):
app_scale = 2
w = graphics_mode_width*app_scale
h = graphics_mode_height*app_scale*y_ratio
drawCanvas.config(width=w, height=h, scrollregion=(0,0,graphics_mode_width*app_scale*zoom_scale, graphics_mode_height*app_scale*zoom_scale*y_ratio))
zoom_screen_pixels()
rescale_palette()
x = int(sx/2)-int((w/2)) - 75
y = int(sy/2)-int(h/2) - 120
app.geometry('{}x{}+{}+{}'.format(int((graphics_mode_width*app_scale)+(160)), int((graphics_mode_height*app_scale*y_ratio)+(ys*app_scale)+60), x, y ))
def get_newzoom_offset(plusminus):
global draw_scroll_x
global draw_scroll_y
global drawCanvas
global zoom_scale
scr_x = draw_scroll_x.get()
scr_y = draw_scroll_y.get()
fscr = drawCanvas.cget('scrollregion')
fscr = fscr.split(' ')
xofs = scr_x[0] * float(fscr[2])
yofs = scr_y[0] * float(fscr[3])
# xofs and yofs give us the x/y offset of the canvas after zoom. on a normal zoom,
# this is the top left corner (quarter) of the viewable window.
if plusminus == '+':
xofs += float(fscr[2])/(zoom_scale*2)
yofs += float(fscr[3])/(zoom_scale*2)
else:
xofs -= drawCanvas.winfo_width()/4 #= 2048/2 * 4
yofs -= drawCanvas.winfo_height()/4
return xofs,yofs
def zoom_screen_pixels():
global screen_pixels
global drawCanvas
global app_scale
global zoom_scale
global screen_data
l = len(screen_data)
w = (app_scale*zoom_scale)
h = (app_scale*zoom_scale)*y_ratio
i = 0
while i < l:
xp = i % graphics_mode_width
xp = xp * app_scale * zoom_scale
yp = math.floor(i/graphics_mode_width)
yp = yp * app_scale * zoom_scale * y_ratio
drawCanvas.coords(screen_pixels[i], xp, yp, xp+w, yp+h)
i += 1
global selector_rect
if selector_rect != None:
drawCanvas.delete(selector_rect)
selector_rect = None
update_canvas_grid()
zoomplusminus = '+'
def depress_zooms(z):
global zoom1button
global zoom2button
global zoom4button
global zoom8button
zoom1button.config(relief=tk.RAISED)
zoom2button.config(relief=tk.RAISED)
zoom4button.config(relief=tk.RAISED)
zoom8button.config(relief=tk.RAISED)
if z == 1:
zoom1button.config(relief=tk.SUNKEN)
elif z==2:
zoom2button.config(relief=tk.SUNKEN)
elif z==4:
zoom4button.config(relief=tk.SUNKEN)
elif z==8:
zoom8button.config(relief=tk.SUNKEN)
def zoom_1x():
global zoom_scale
depress_zooms(1)
if zoom_scale != 1:
global zoomplusminus
zoomplusminus = '-'
toggle_zoom(1)
def zoom_2x():
global zoom_scale
depress_zooms(2)
global zoomplusminus
if zoom_scale > 2:
zoomplusminus = '-'
else:
zoomplusminus = '+'
if zoom_scale != 2:
toggle_zoom(2)
def zoom_4x():
global zoom_scale
depress_zooms(4)
global zoomplusminus
if zoom_scale > 4:
zoomplusminus = '-'
else:
zoomplusminus = '+'
if zoom_scale != 4:
toggle_zoom(4)
def zoom_8x():
global zoom_scale
depress_zooms(8)
global zoomplusminus
if zoom_scale > 8:
zoomplusminus = '-'
else:
zoomplusminus = '+'
if zoom_scale != 8:
toggle_zoom(8)
def toggle_zoom(z=0):
global zoom_scale
if z==0:
if zoom_scale == 2:
zoom_scale = 4
elif zoom_scale == 1:
zoom_scale = 2
elif zoom_scale == 4:
zoom_scale = 8
elif zoom_scale == 8:
zoom_scale = 1
else:
zoom_scale = z
drawCanvas.config(scrollregion=(0,0,graphics_mode_width*app_scale*zoom_scale, graphics_mode_height*app_scale*zoom_scale*y_ratio))
zoom_screen_pixels()
global zoomplusminus
newx, newy = get_newzoom_offset(zoomplusminus)
set_drawwindow_pos(xypos(newx,newy))
brush_style = 'square'
button_held = False
def color_pixel(x, y, size=1):
global drawCanvas
global graphics_mode_width
global graphics_mode_height
if (x >= graphics_mode_width) or (y >= graphics_mode_height):
return
if (x < 0) or (y < 0):
return
global button_held
if button_held == False:
button_held = True
set_undo_point()
global screen_data
global screen_pixels
global selected_palette_no
global hex_palette
tp = (y*graphics_mode_width) + x
global graphic_mode
if graphic_mode != 'G7':
screen_data[tp] = selected_palette_no
else:
screen_data[tp] = hex_palette[selected_palette_no]
drawCanvas.itemconfig(screen_pixels[tp], fill=hex_palette[selected_palette_no])
global pxsize
size = int(pxsize.get())
if size < 1:
size = 1
elif size > 9:
size = 9
pxsize.delete(0,tk.END)
pxsize.insert(0,size)
if size > 1:
#TODO: possible toggle for double-high brush size
#TODO: possible toggle for diamond vs square
#atm only pixel-perfect square
#brush_style = 'diamond'
global brush_style
if brush_style == 'square':
size_loop = size - 1
if size_loop % 2 == 0: #is pixel size odd?
tp_origin_y = y - ((size_loop)/2)
tp_origin_x = x - ((size_loop)/2)
else: #is pixel size even?
tp_origin_y = y - ((size_loop-1)/2)
tp_origin_x = x - ((size_loop-1)/2)
paint_square_brush(size, tp_origin_y, tp_origin_x)
elif brush_style == 'diamond':
paint_diamond_brush(size, y, x)
def client_exit():
a = tk.messagebox.askyesnocancel('Save?', message='Save current bitmap before\nquitting?')
if a == True:
save_normal()
global m2bfilename
if m2bfilename == '' or type(m2bfilename)==tuple:
return
sys.exit()
elif a == False:
sys.exit()
elif a == None:
return