-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtk_draw.py
148 lines (114 loc) · 5.02 KB
/
tk_draw.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
# module to draw things with tkinter
from tkinter import *
import consts as Consts
DEBUG = False
def getInstance():
''' Returns a Tkinter instance for future Tkinter calls '''
return Tk()
def clickHandler(event, gols):
if DEBUG:
print('click at x : (%d), y : (%d)' % (event.x, event.y))
scaledPoint = scaleGraphicToBoard((event.x, event.y))
if DEBUG:
print('stupid scale, x : (%d), y : (%d)' % (scaledPoint[0],
scaledPoint[1]))
"""
gols.setFrame(scaledPoint[0], scaledPoint[1])
"""
def keyboardHandler(event):
if DEBUG:
print('keystroke pressed : ', repr(event.char))
def updateHandler(instance, window, ids, gols):
if DEBUG:
print('update')
gols.update()
drawField(window, ids, gols)
attachUpdater(instance, window, ids, gols)
def getWindow(instance, gols):
''' Returns a canvas object to draw on '''
canvas = Canvas(instance, width=Consts.TK_WIDTH, height=Consts.TK_HEIGHT)
canvas.bind('<Key>', keyboardHandler)
canvas.bind('<ButtonPress-1>',
lambda event, localGols=gols: clickHandler(event, localGols))
canvas.pack()
return canvas
def attachUpdater(instance, window, ids, gols):
instance.after(Consts.TK_UPDATE_TIMER, updateHandler,
instance, window, ids, gols)
def attachMainloop(instance):
''' Calls Tkinter mainloop() function '''
instance.mainloop()
def scaleBoardToGraphic(point):
if len(point) == 2:
for i in range(2):
if not isinstance(point[i], int):
raise Exception('bad point format')
return ((point[0] * Consts.TK_WIDTH) / Consts.BOA_WIDTH,
(point[1] * Consts.TK_HEIGHT) / Consts.BOA_HEIGHT)
def scaleGraphicToBoard(point):
if len(point) == 2:
for i in range(2):
if (not isinstance(point[i], int)
and not isinstance(point[i], float)):
raise Exception('bad point format')
return (int((point[0] / Consts.TK_WIDTH) * Consts.BOA_WIDTH),
int((point[1] / Consts.TK_HEIGHT) * Consts.BOA_HEIGHT))
def drawRect(window, x, y):
sPoint = scaleBoardToGraphic((x, y))
ePoint = scaleBoardToGraphic((x + 1, y + 1))
if DEBUG:
print('RECTANGLE:: start point : {}, end point : {}'.format(sPoint,
ePoint))
window.create_rectangle(sPoint[0],
sPoint[1],
ePoint[0],
ePoint[1],
outline=Consts.TK_COLOR_GREEN,
fill=Consts.TK_CELL_COLOR,
width=2)
def drawField(window, ids, gols):
'''
Update the field in the canvas on the window
There is dead cells (white) and alive cells (black)
'''
# Reset background
window.itemconfig(ids[0], fill=Consts.TK_BG_COLOR)
# Update all cells
for y in range(gols.field.height):
for x in range(gols.field.width):
frame = gols.field.getFrame(x, y)
if frame in Consts.BOA_VALID_REPR:
frameId = ids[1][(y * gols.field.width) + x]
if frame == Consts.BOA_ALIVE_REPR:
window.itemconfig(frameId, outline=Consts.TK_COLOR_GREEN, fill=Consts.TK_CELL_COLOR, width=2)
else:
window.itemconfig(frameId, outline=Consts.TK_BG_COLOR, fill=Consts.TK_BG_COLOR, width=0)
# Update generation count on window
window.itemconfig(ids[2], text=str(gols.generation), font=('Helvetica', '20'))
return
def initWindow(window, gols):
'''
Draws the field in the canvas on the window
There is dead cells (white) and alive cells (black)
Init function returns all object ids needed for further graphic modifications
'''
backgroundId = window.create_rectangle(0, 0,
Consts.TK_WIDTH, Consts.TK_HEIGHT,
fill=Consts.TK_BG_COLOR)
cellsIds = []
for y in range(gols.field.height):
for x in range(gols.field.width):
frame = gols.field.getFrame(x, y)
if frame in Consts.BOA_VALID_REPR:
sPoint = scaleBoardToGraphic((x, y))
ePoint = scaleBoardToGraphic((x + 1, y + 1))
cellsIds.append(window.create_rectangle(sPoint[0],
sPoint[1],
ePoint[0],
ePoint[1],
outline=Consts.TK_COLOR_BLUE,
fill=Consts.TK_CELL_COLOR,
width=1))
textId = window.create_text(10, 10, anchor="nw", fill="white")
window.itemconfig(textId, text=str(gols.generation), font=('Helvetica', '20'))
return (backgroundId, cellsIds, textId)