-
Notifications
You must be signed in to change notification settings - Fork 1
/
Primitive Paint Alpha 1.2.pyw
1638 lines (1451 loc) · 47.1 KB
/
Primitive Paint Alpha 1.2.pyw
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
##########################################################################
##Program Author: George Zhang ##
##Revision Date: Nov 12, 2018 ##
##Program Name: Primitive Paint Alpha 1.2 ##
##Description: This program draws on the canvas of a tkinter window. ##
##########################################################################
#importation of tkinter, its colour and file chooser, and its alerts
import tkinter as tk
import tkinter.colorchooser as tkcolour
import tkinter.filedialog as tkfile
import tkinter.messagebox as tkmessage
#importation of maths
import math as m
#importation of system
import sys
#current version
VERSION = 1.2
#declaration of variables
actions = list() #used for undo
adjustX = int()
adjustY = int()
canvasHeight = int()
canvasWidth = int()
current = str() #colour
debug = bool() #enable debug
file = str() #file object
fileName = str() #file path
firstX = int() #click and drag drawing
firstY = int()
mouse = bool()
outlinePen = list() #pen outline id
pastPoints = list() #polygon drawing
previousX = int() #pen drawing
previousY = int()
redo = list() #used for redo
saved = bool() #used for saving
statusPreX = int() #used for checking status
statusPreY = int()
stroke = int() #amount of strokes
strokeType = str() #stroke type
window = str() #main window identifier
#default values
actions = [[]]
canvasHeight = 300
canvasWidth = 100
current = '#000000'
fileName = 'untitled.txt'
redo = [[]]
saved = True
strokeType = 'pen'
#instantiate the main window
window = tk.Tk()
window.title('Primitive Paint - untitled.txt')
#declaration of window variables
sizePen = tk.IntVar()
statusX = tk.StringVar()
statusY = tk.StringVar()
#default window values
sizePen.set(1.0)
statusX.set('0')
statusY.set('0')
#makes a shape at specified location and returns the value
def canvasShape(*args, **kwargs):
#declaration of internal variables
x1 = int()
y1 = int()
x2 = int()
y2 = int()
kargs = dict()
#plot arguments to internal variables
if (len(args) < 2):
raise TypeError('canvasShape expected at least 2 arguments')
elif (len(args)%2 != 0):
raise TypeError('canvasShape expected pairs of arguments')
else:
try:
x1 = args[0]
y1 = args[1]
x2 = args[2]
y2 = args[3]
except:
pass
kargs = dict(kwargs)
#check for unfilled keyworded arguments
if 'shape' not in kargs:
raise TypeError('canvasShape expected a keyword shape')
if 'fill' not in kargs and 'outline' not in kargs:
raise TypeError('canvasShape expected a keyword fill or outline')
if 'size' not in kargs:
kargs['size'] = 1
if (('outline' in kargs) and ('fill' not in kargs)):
kargs['fill'] = ''
if 'outline' not in kargs:
kargs['outline'] = kargs['fill']
#create and return item id
if (kargs['shape'] == 'dot'):
return canvas.create_line(
x1, y1, x1 - 1, y1 - 1,
fill = kargs['fill']
)
if (kargs['shape'] == 'line'):
return canvas.create_line(
x1, y1, x2, y2,
fill = kargs['fill'],
width = kargs['size']
)
elif (kargs['shape'] in ('circle', 'square')):
#get corners for specified size
top = m.floor(kargs['size']/2)
bottom = m.ceil(kargs['size']/2) - 1
if ((kargs['shape'] == 'circle') and (kargs['size'] > 2)):
return canvas.create_oval(
x1 - top, y1 - top, x1 + bottom, y1 + bottom,
fill = kargs['fill'],
outline = kargs['outline']
)
elif ((kargs['shape'] == 'square') and (kargs['size'] > 2)):
return canvas.create_rectangle(
x1 - top, y1 - top, x1 + bottom, y1 + bottom,
fill = kargs['fill'],
outline = kargs['outline']
)
elif (kargs['size'] == 2):
return canvas.create_rectangle(
x1, y1, x1 - 1, y1 - 1,
outline = kargs['outline']
)
else:
return canvas.create_line(
x1, y1, x1 - 1, y1 - 1,
fill = kargs['outline']
)
elif (kargs['shape'] == 'oval'):
return canvas.create_oval(
x1, y1, x2, y2,
fill = kargs['fill'],
outline = kargs['outline'],
width = kargs['size']
)
elif (kargs['shape'] == 'rectangle'):
if ((x1 != x2) and (y1 != y2)):
return canvas.create_rectangle(
x1, y1, x2, y2,
fill = kargs['fill'],
outline = kargs['outline'],
width = kargs['size']
)
elif (kargs['size'] == 1):
return canvas.create_line(
x1, y1, x2, y2,
fill = kargs['outline']
)
else:
#get corners for specified size
top = m.floor(kargs['size']/2)
bottom = m.ceil(kargs['size']/2) - 1
#swap ends if needed
if ((x1 - x2 > 0) or (y1 - y2 > 0)):
x1, y1, x2, y2 = x2, y2, x1, y1
return canvas.create_rectangle(
x1 - top, y1 - top, x2 + bottom, y2 + bottom,
fill = kargs['outline'],
outline = kargs['outline'],
width = 1
)
elif (kargs['shape'] == 'polygon'):
if ((args[0::2].count(args[0]) < len(args)/2)
or (args[1::2].count(args[1]) < len(args)/2)
):
return canvas.create_polygon(
*args,
fill = kargs['fill'],
outline = kargs['outline'],
width = kargs['size']
)
elif (kargs['size'] > 1):
#get corners for specified size
top = m.floor(kargs['size']/2)
bottom = m.ceil(kargs['size']/2) - 1
return canvas.create_oval(
x1 - top, y1 - top, x1 + bottom, y1 + bottom,
fill = kargs['outline'],
outline = kargs['outline']
)
else:
return canvas.create_line(
x1, y1, x1 - 1, y1 - 1,
fill = kargs['outline'],
)
else:
pass
#end canvasShape(*args, **kwargs)
#creates a line at the mouse move event
def paint(event=None):
#reference variables outside function
global actions
global current
global previousX
global previousY
global sizePen
global stroke
global strokeType
#declaration of internal variables
currentX = int()
currentY = int()
#if event isnt there, use previous x and y positions
if event is None:
currentX = previousX
currentY = previousY
else:
currentX = event.x
currentY = event.y
#draws line from previous event location if strokeType is pen
if (strokeType == 'pen'):
actions[stroke].append(canvasShape(
previousX, previousY,
currentX, currentY,
shape = 'line',
fill = current,
size = sizePen.get()
))
#finish end dot if larger than one
if (sizePen.get() > 1.0):
actions[stroke].append(canvasShape(
currentX, currentY,
shape = 'circle',
fill = current,
size = sizePen.get()
))
#size five fixes
if (sizePen.get() == 5):
actions[stroke].append(canvasShape(
currentX + 1, currentY,
shape = 'circle',
fill = current,
size = 4
))
#call the outline function
outline(event)
#update line start for next line
previousX = currentX
previousY = currentY
#end paint(event=None)
#resets the line start
def reset(event=None):
#reference variables outside function
global actions
global current
global fileName
global firstX
global firstY
global mouse
global pastPoints
global previousX
global previousY
global redo
global saved
global sizePen
global stroke
global strokeType
#set the mouse down location
if event is None:
firstX = previousX
firstY = previousY
else:
firstX = event.x
firstY = event.y
#set the mouse down detector
mouse = True
#adds a point to the polygon
if (strokeType in ('polygon', 'filled polygon')):
pastPoints.extend((firstX, firstY))
else:
#update current stroke if not polygon
stroke += 1; actions.append([])
#update saved
saved = False
#update title
window.title(('*Primitive Paint - ' + fileName))
#empty redo list
if (len(redo) > 1):
del redo[1:]
#draws starting dot of selected size
if (strokeType in ('pen')):
#circle shape
actions[stroke].append(canvasShape(
firstX, firstY,
shape = 'circle'
if (sizePen.get() != 2)
else 'square',
fill = current,
size = sizePen.get()
))
#size five fixes
if (sizePen.get() == 5):
actions[stroke].append(canvasShape(
firstX + 1, firstY,
shape = 'circle',
fill = current,
size = 4
))
#update line start for next line
previousX = firstX
previousY = firstY
#call the outline function
outline()
#end reset(event=None)
#button release detector
def end(event=None):
#reference variables outside function
global actions
global current
global firstX
global firstY
global mouse
global previousX
global previousY
global sizePen
global stroke
global strokeType
#declaration of internal variables
currentX = int()
currentY = int()
#if event isnt there, use previous x and y positions
if event is None:
currentX = previousX
currentY = previousY
else:
currentX = event.x
currentY = event.y
#reset the mouse down detector
mouse = False
#finish stroke
if (strokeType == 'line'):
actions[stroke].append(canvasShape(
firstX, firstY,
shape = 'circle'
if (sizePen.get() != 2)
else 'square',
fill = current,
size = sizePen.get()
))
#finish line
actions[stroke].append(canvasShape(
firstX, firstY,
currentX, currentY,
shape = 'line',
fill = current,
size = sizePen.get()
))
#finish end dot
actions[stroke].append(canvasShape(
currentX, currentY,
shape = 'circle'
if (sizePen.get() != 2)
else 'square',
fill = current,
size = sizePen.get()
))
#size five fixes
if (sizePen.get() == 5):
actions[stroke].append(canvasShape(
currentX + 1, currentY,
shape = 'circle',
fill = current,
size = 4
))
elif (strokeType in ('oval', 'filled oval')):
actions[stroke].append(canvasShape(
firstX, firstY,
currentX, currentY,
shape = 'oval',
fill = current
if (strokeType == 'filled oval')
else '',
outline = current,
size = sizePen.get()
))
elif (strokeType in ('rectangle', 'filled rectangle')):
actions[stroke].append(canvasShape(
firstX, firstY,
currentX, currentY,
shape = 'rectangle'
if ((currentX - firstX != 0)
and (currentY - firstY != 0))
else 'square',
fill = current
if ((strokeType == 'filled rectangle')
or ((firstX == currentX)
and (firstY == currentY)))
else '',
outline = current,
size = sizePen.get()
))
#call the outline function
outline()
#update line start for next line
previousX = currentX
previousY = currentY
#end end(event=None)
#right click detector function
def finish(event=None):
#reference variables outside function
global actions
global current
global pastPoints
global sizePen
global stroke
global strokeType
#check if stroke type is polygon or filled polygon
if (strokeType not in ('polygon', 'filled polygon')): return
#check if there's actually points in the polygon
if ((len(pastPoints) > 1) and (len(pastPoints)%2 == 0)):
#increase stroke amount
stroke += 1; actions.append([])
#add the polygon
if (len(pastPoints) == 2):
#make a point
actions[stroke].append(canvasShape(
*pastPoints,
shape = 'circle',
fill = current,
size = sizePen.get()
))
elif (len(pastPoints) == 4):
#make a line
actions[stroke].append(canvasShape(
*pastPoints[0:2],
shape = 'circle',
fill = current,
size = sizePen.get()
))
actions[stroke].append(canvasShape(
*pastPoints, *pastPoints[2:4],
shape = 'polygon',
fill = current,
size = sizePen.get()
))
actions[stroke].append(canvasShape(
*pastPoints[2:4],
shape = 'circle',
fill = current,
size = sizePen.get()
))
else:
#make a polygon
actions[stroke].append(canvasShape(
*pastPoints,
shape = 'polygon',
fill = current
if (strokeType == 'filled polygon')
else '',
outline = current,
size = sizePen.get()
))
#empty the points
del pastPoints[:]
#call the outline function
outline()
#end finish(event=None)
#mouse motion detector function
def outline(event=None):
#reference variables outside function
global current
global firstX
global firstY
global mouse
global outlinePen
global pastPoints
global previousX
global previousY
#declaration of internal variables
currentX = int()
currentY = int()
colourRed = int()
colourGreen = int()
colourBlue = int()
colour = str()
#check if mouse isnt down
if not mouse:
#set the colour to be used to the current colour
colour = current
else:
#convert colour to integer
colourRed = int(current[1:3], 16)
colourGreen = int(current[3:5], 16)
colourBlue = int(current[5:7], 16)
#get opposite
colourRed = hex(abs(255 - colourRed))[2:].rjust(2, '0')
colourGreen = hex(abs(255 - colourGreen))[2:].rjust(2, '0')
colourBlue = hex(abs(255 - colourBlue))[2:].rjust(2, '0')
#convert back to normal format
colour = (f'#{colourRed}{colourGreen}{colourBlue}')
#if event isnt there, use previous x and y positions
if event is None:
currentX = previousX
currentY = previousY
else:
currentX = event.x
currentY = event.y
#delete all items in outline
if outlinePen:
#delete the outline
for item in outlinePen: canvas.delete(item)
#reset outline ids
del outlinePen[:]
#draw result
if ((strokeType == 'line') and mouse):
#if type is line
outlinePen.append(canvasShape(
firstX, firstY,
shape = 'circle',
fill = current,
size = sizePen.get()
))
outlinePen.append(canvasShape(
firstX, firstY,
currentX, currentY,
shape = 'line',
fill = current,
size = sizePen.get()
))
outlinePen.append(canvasShape(
currentX, currentY,
shape = 'circle',
fill = current,
size = sizePen.get()
))
elif ((strokeType in ('oval', 'filled oval')) and mouse):
#if type is oval or filled oval
outlinePen.append(canvasShape(
firstX, firstY,
currentX, currentY,
shape = 'oval',
fill = current
if (strokeType == 'filled oval')
else '',
outline = current,
size = sizePen.get()
))
elif ((strokeType in ('rectangle', 'filled rectangle')) and mouse):
#if type is rectangle or filled rectangle
outlinePen.append(canvasShape(
firstX, firstY,
currentX, currentY,
shape = 'rectangle',
fill = current
if (strokeType == 'filled rectangle')
else '',
outline = current,
size = sizePen.get()
))
elif ((strokeType in ('polygon', 'filled polygon')) and pastPoints):
#if type is polygon or filled polygon
outlinePen.append(canvasShape(
*pastPoints,
currentX, currentY,
shape = 'polygon',
fill = current
if (strokeType == 'filled polygon')
else '',
outline = current,
size = sizePen.get()
))
#draws outline of selected size
if (strokeType in ('rectangle', 'filled rectangle')):
outlinePen.append(canvasShape(
currentX, currentY,
shape = 'square',
outline = colour,
size = sizePen.get()
))
elif (sizePen.get() == 5):
def lineMake(x1, y1, x2, y2):
nonlocal currentX
nonlocal currentY
outlinePen.append(canvasShape(
currentX + x1, currentY + y1,
currentX + x2, currentY + y2,
shape = 'line', fill = colour
))
lineMake(-2, 1, -2, -2)
lineMake(-1, 2, 2, 2)
lineMake(2, 1, 2, -2)
lineMake(1, -2, -2, -2)
else:
outlinePen.append(canvasShape(
currentX, currentY,
shape = 'circle',
outline = colour,
size = sizePen.get()
))
#update line start for next line
previousX = currentX
previousY = currentY
#update screen
canvas.update()
#update current mouse location
statusX.set(str(currentX))
statusY.set(str(currentY))
statusMouseX.config(width = len(statusX.get()))
statusMouseY.config(width = len(statusY.get()))
#update last mouse down location
if (strokeType not in ('polygon', 'filled polygon')):
statusLast.config(text = f'{firstX}, {firstY}' if mouse else '')
elif (strokeType in ('polygon', 'filled polygon')):
statusLast.config(
text = f'{pastPoints[-2]}, {pastPoints[-1]}'
if pastPoints else ''
)
#update mouse down status
if mouse: statusMouseDown.config(bg = '#CCCCCC')
else: statusMouseDown.config(bg = 'SystemButtonFace')
#end outline(event=None)
#changes colour with a popup
def colourChange(*args):
#reference variables outside function
global current
#declaration of internal variables
colourAsk = tuple()
#ask user for new colour and replace previous colour
colourAsk = tkcolour.askcolor(initialcolor = current)
if (colourAsk[1] is not None): current = colourAsk[1]
#update display
display.config(bg = current)
#call outline maker
outline()
#end colourChange(*args)
#resize with the scrollwheel
def resize(event=None):
#reference variables outside function
global mouse
global sizePen
#scroll up to increase, down to decrease
if ((event.delta == -120) and (sizeChoose.get() < 100)):
sizeChoose.set(sizePen.get() + 1)
if event and mouse: paint()
elif ((event.delta == 120) and (sizeChoose.get() > 1)):
sizeChoose.set(sizePen.get() - 1)
#call the outline function
if event: outline()
#set size to the scale's setting just in case
sizePen.set(sizeChoose.get())
#end resize(event)
#mouse entered canvas
def entry(event):
#reference variables outside function
global mouse
#reset mouse down
mouse = False
#call outline maker
outline(event)
#end entry(event)
#check status positions
def statusCheck(*args):
#reference variables outside function
global previousX
global previousY
global statusPreX
global statusPreY
#change value into integer or empty
if (statusX.get() == ''):
statusX.set('')
previousX = 0
statusPreX = ''
else:
try:
statusX.set(abs(int(statusX.get())))
statusPreX = statusX.get()
previousX = int(statusX.get())
except: statusX.set(statusPreX)
if (statusY.get() == ''):
statusY.set('')
previousY = 0
statusPreY = ''
else:
try:
statusY.set(abs(int(statusY.get())))
statusPreY = statusY.get()
previousY = int(statusY.get())
except: statusY.set(statusPreY)
#update size of the entry boxes
if (statusMouseX['state'] == 'disabled'):
statusMouseX.config(width = max((len(statusX.get())), 1))
else: statusMouseX.config(width = max((len(statusX.get())), 5))
if (statusMouseY['state'] == 'disabled'):
statusMouseY.config(width = max((len(statusY.get())), 1))
else: statusMouseY.config(width = max((len(statusY.get())), 5))
#end statusCheck(*args)
#update status position displays when mouse moved in or out
def statusEntry(position, move):
#declaration of internal variables
newState = str()
newWidth = int()
#check what state the widget should be
if (move == 'in'): newState = 'normal'; newWidth = 5
elif (move == 'out'): newState = 'disabled'; newWidth = 1
#check which widget this is happening in
if (position == 'x'):
statusMouseX.config(state = newState)
statusMouseX.config(width = max((len(statusX.get())), newWidth))
if (statusX.get() == ''): statusX.set(0)
elif (position == 'y'):
statusMouseY.config(state = newState)
statusMouseY.config(width = max((len(statusY.get())), newWidth))
if (statusY.get() == ''): statusY.set(0)
#end statusEntry(position, move)
#simulate mouse down, release, and right click
def toggleDown(button):
#reference variables outside function
global mouse
#declaration of internal variables
bgOn = '#CCCCCC'
bgOff = 'SystemButtonFace'
#toggle mouse if not right click
if (button == 1): mouse = not mouse
#toggle backgrounds
if mouse: statusMouseDown.config(bg = bgOn)
else: statusMouseDown.config(bg = bgOff)
#update previous mouse positions to entry widgets
previousX = statusMouseX.get()
previousY = statusMouseY.get()
#if stroke type is line, oval, or rectangle, update when down and up
#otherwise, update when it is down
if (strokeType in (
'line',
'oval', 'filled oval',
'rectangle', 'filled rectangle'
)):
if ((button == 1) and mouse): reset()
elif (button == 1): end()
elif (strokeType in (
'pen',
'polygon', 'filled polygon'
)):
if ((button == 1) and mouse): reset()
elif ((button == 3) and (strokeType not in ('pen'))): finish()
#call outline maker
outline()
#end toggleDown(*args)
#change drawing type
def strokeChange(position):
#reference variables outside function
global pastPoints
global strokeType
#declaration of internal variables
lastTypePolygon = bool()
#check if stroke type is a polygon
if (lastTypePolygon in ('polygon', 'filled polygon')):
lastTypePolygon = True
#function to reset background
def bgReset():
#declaration of internal variables
bgOff = 'SystemButtonFace'
bgOn = '#CCCCCC'
shapeCounter = int()
#calculate result
def valueMake():
#reference non-global variables outside function
nonlocal shapeCounter
#increase counter
shapeCounter += 1
#return if it matches the counter's previous value
if (position == shapeCounter - 1): return bgOn
else: return bgOff
#end valueMake()
#reset backgrounds except for the selected one
shapePencil.config(bg = valueMake())
shapeLine.config(bg = valueMake())
shapeOval.config(bg = valueMake())
shapeFilledOval.config(bg = valueMake())
shapeRectangle.config(bg = valueMake())
shapeFilledRectangle.config(bg = valueMake())
shapePolygon.config(bg = valueMake())
shapeFilledPolygon.config(bg = valueMake())
#specific type for each button
for positionCheck, strokeCheck in enumerate((
'pen', 'line',
'oval', 'filled oval',
'rectangle', 'filled rectangle',
'polygon', 'filled polygon'
)):
#check if a button was pressed and is different than the current
if ((position == positionCheck) and (strokeType != strokeCheck)):
strokeType = strokeCheck; bgReset()
#end for
#delete the last points if the last type was a polygon and now it isnt
if ((lastTypePolygon)
and (strokeType not in ('polygon', 'filled polygon'))
): del pastPoints[:]
#call outline
outline()
#end strokeChange(position)
def undoLast(*args):
#reference variables outside function
global actions
global fileName
global redo
global saved
global stroke
#save and delete last stroke one by one for updating
if (len(actions) > 1):
#update saved
saved = False
#update title
window.title(('*Primitive Paint - ' + fileName))
#indicate start of stroke
redo.append([True])
#loop through all items
for item in actions[-1]:
#add last substroke
redo[-1].append(canvas.type(item))
redo[-1].append(canvas.coords(item))
redo[-1].append(
canvas.itemcget(item, 'outline') + '#'
if ((canvas.type(item)
in ('oval', 'rectangle', 'polygon'))
and ((canvas.itemcget(item, 'fill')
!= canvas.itemcget(item, 'outline'))))
else (canvas.itemcget(item, 'fill'))
)
redo[-1].append(canvas.itemcget(item, 'width'))
canvas.delete(item)
#end for
#canvas.update()
del actions[-1]
stroke -= 1
#end undoLast(*args)
def undoSingle(*args):
#reference variables outside function
global actions
global fileName
global redo
global saved
global stroke
#save and delete last substroke, deleting last stroke if needed
if (len(actions) > 1):
#update saved
saved = False
#update title
window.title(('*Primitive Paint - ' + fileName))
#add indicator for new stroke
if (len(actions[-1]) == 1):
redo.append([True])
else:
redo.append([False])
#add last substroke
redo[-1].append(canvas.type(actions[-1][-1]))
redo[-1].append(canvas.coords(actions[-1][-1]))
redo[-1].append(
canvas.itemcget(actions[-1][-1], 'outline') + '#'
if ((canvas.type(actions[-1][-1])
in ('oval', 'rectangle', 'polygon'))
and ((canvas.itemcget(actions[-1][-1], 'fill')
!= canvas.itemcget(actions[-1][-1], 'outline'))))
else (canvas.itemcget(actions[-1][-1], 'fill'))
)
redo[-1].append(canvas.itemcget(actions[-1][-1], 'width'))
canvas.delete(actions[-1][-1])
#delete last substroke, deleting full stroke if needed
if (len(actions[-1]) == 1): del actions[-1]; stroke -= 1
else: del actions[-1][-1]
#end undoSingle(*args)
def redoLast(*args):
#reference variables outside function
global actions
global fileName
global redo
global saved
global stroke
#check first item for new stroke
if (len(redo) > 1):
#update saved
saved = False
#update title
window.title(('*Primitive Paint - ' + fileName))
if redo[-1][0]:
#update stroke
stroke += 1
actions.append([])
#reverse last redo list for easier removing
list(redo[-1]).reverse()
#loop through every item, adding from second, every fourth
for item, option in enumerate(redo[-1]):
if (item % 4 == 1):
actions[stroke].append(canvasShape(
*redo[-1][item + 1],
shape = option,
fill = redo[-1][item + 2]