forked from bcorfman/raven-checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardview.py
485 lines (439 loc) · 21.8 KB
/
boardview.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
import os
from Tkinter import *
from Tkconstants import END, N, S, E, W
from command import *
from observer import *
from globalconst import *
from autoscrollbar import AutoScrollbar
from textserialize import Serializer
from hyperlinkmgr import HyperlinkManager
from tkFileDialog import askopenfilename
from tkFont import Font
from tooltip import ToolTip
class BoardView(Observer):
def __init__(self, root, **props):
self._statusbar = props['statusbar']
self.root = root
self._model = props['model']
self._model.curr_state.attach(self)
self._gameMgr = props['parent']
self._board_side = props.get('side') or DEFAULT_SIZE
self.light_squares = props.get('lightsquares') or LIGHT_SQUARES
self.dark_squares = props.get('darksquares') or DARK_SQUARES
self.light_color = props.get('lightcheckers') or LIGHT_CHECKERS
self.dark_color = props.get('darkcheckers') or DARK_CHECKERS
self._square_size = self._board_side / 8
self._piece_offset = self._square_size / 5
self._crownpic = PhotoImage(file=CROWN_IMAGE)
self._boardpos = POS_MAP
self._gridpos = GRID_MAP
self.canvas = Canvas(root, width=self._board_side,
height=self._board_side, borderwidth=0,
highlightthickness=0)
right_panel = Frame(root, borderwidth=1, relief='sunken')
self.toolbar = Frame(root)
font, size = get_preferences_from_file()
self.scrollbar = AutoScrollbar(root, container=right_panel,
row=1, column=1, sticky='ns')
self.txt = Text(root, width=40, height=1, borderwidth=0,
font=(font,size), wrap='word',
yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.txt.yview)
self.canvas.pack(side='left', fill='both', expand=False)
self.toolbar.grid(in_=right_panel, row=0, column=0, sticky='ew')
right_panel.pack(side='right', fill='both', expand=True)
self.txt.grid(in_=right_panel, row=1, column=0, sticky='nsew')
right_panel.grid_rowconfigure(1, weight=1)
right_panel.grid_columnconfigure(0, weight=1)
self.init_images()
self.init_toolbar_buttons()
self.init_font_sizes(font, size)
self.init_tags()
self._register_event_handlers()
self.btnset = set([self.bold, self.italic, self.addLink, self.remLink])
self.btnmap = {'bold': self.bold, 'italic': self.italic,
'bullet': self.bullets, 'number': self.numbers,
'hyper': self.addLink}
self.hypermgr = HyperlinkManager(self.txt, self._gameMgr.load_game)
self.serializer = Serializer(self.txt, self.hypermgr)
self.curr_annotation = ''
self._setup_board(root)
starting_squares = [i for i in self._model.curr_state.valid_squares
if self._model.curr_state.squares[i] &
(BLACK | WHITE)]
self._draw_checkers(Command(add=starting_squares))
self.flip_view = False # black on bottom
self._label_board()
self.update_statusbar()
def _toggle_state(self, tags, btn):
# toggle the text state based on the first character in the
# selected range.
if self.txt.tag_ranges('sel'):
current_tags = self.txt.tag_names('sel.first')
elif self.txt.tag_ranges('insert'):
current_tags = self.txt.tag_names('insert')
else:
return
for tag in tags:
already_tagged = any((x for x in current_tags if
x.startswith(tag)))
for t in current_tags:
if t != 'sel':
self.txt.tag_remove(t, 'sel.first', 'sel.last')
if not already_tagged:
self.txt.tag_add(tag, 'sel.first', 'sel.last')
btn.configure(relief='sunken')
other_btns = self.btnset.difference([btn])
for b in other_btns:
b.configure(relief='raised')
else:
btn.configure(relief='raised')
def _on_bold(self):
self.bold_tooltip.hide()
self._toggle_state(['bold'], self.bold)
def _on_italic(self):
self.italic_tooltip.hide()
self._toggle_state(['italic'], self.italic)
def _on_bullets(self):
self._process_button_click('bullet', self.bullets_tooltip,
self._add_bullets_if_needed,
self._remove_bullets_if_needed)
def _on_numbers(self):
self._process_button_click('number', self.numbers_tooltip,
self._add_numbers_if_needed,
self._remove_numbers_if_needed)
def _process_button_click(self, tag, tooltip, add_func, remove_func):
tooltip.hide()
if self.txt.tag_ranges('sel'):
startline, _ = parse_index(self.txt.index('sel.first'))
endline, _ = parse_index(self.txt.index('sel.last'))
else:
startline, _ = parse_index(self.txt.index(INSERT))
endline = startline
current_tags = self.txt.tag_names('%d.0' % startline)
if tag not in current_tags:
add_func(startline, endline)
else:
remove_func(startline, endline)
def _add_bullets_if_needed(self, startline, endline):
self._remove_numbers_if_needed(startline, endline)
for line in range(startline, endline+1):
current_tags = self.txt.tag_names('%d.0' % line)
if 'bullet' not in current_tags:
start = '%d.0' % line
end = '%d.end' % line
self.txt.insert(start, '\t')
self.txt.image_create(start, image=self.bullet_image)
self.txt.insert(start, '\t')
self.txt.tag_add('bullet', start, end)
self.bullets.configure(relief='sunken')
self.numbers.configure(relief='raised')
def _remove_bullets_if_needed(self, startline, endline):
for line in range(startline, endline+1):
current_tags = self.txt.tag_names('%d.0' % line)
if 'bullet' in current_tags:
start = '%d.0' % line
end = '%d.end' % line
self.txt.tag_remove('bullet', start, end)
start = '%d.0' % line
end = '%d.3' % line
self.txt.delete(start, end)
self.bullets.configure(relief='raised')
def _add_numbers_if_needed(self, startline, endline):
self._remove_bullets_if_needed(startline, endline)
num = 1
for line in range(startline, endline+1):
current_tags = self.txt.tag_names('%d.0' % line)
if 'number' not in current_tags:
start = '%d.0' % line
end = '%d.end' % line
self.txt.insert(start, '\t')
numstr = '%d.' % num
self.txt.insert(start, numstr)
self.txt.insert(start, '\t')
self.txt.tag_add('number', start, end)
num += 1
self.numbers.configure(relief='sunken')
self.bullets.configure(relief='raised')
def _remove_numbers_if_needed(self, startline, endline):
cnt = IntVar()
for line in range(startline, endline+1):
current_tags = self.txt.tag_names('%d.0' % line)
if 'number' in current_tags:
start = '%d.0' % line
end = '%d.end' % line
self.txt.tag_remove('number', start, end)
# Regex to match a tab, followed by any number of digits,
# followed by a period, all at the start of a line.
# The cnt variable stores the number of characters matched.
pos = self.txt.search('^\t\d+\.\t', start, end, None, None,
None, True, None, cnt)
if pos:
end = '%d.%d' % (line, cnt.get())
self.txt.delete(start, end)
self.numbers.configure(relief='raised')
def _on_undo(self):
self.undo_tooltip.hide()
self._gameMgr.parent.undo_single_move()
def _on_undo_all(self):
self.undoall_tooltip.hide()
self._gameMgr.parent.undo_all_moves()
def _on_redo(self):
self.redo_tooltip.hide()
self._gameMgr.parent.redo_single_move()
def _on_redo_all(self):
self.redoall_tooltip.hide()
self._gameMgr.parent.redo_all_moves()
def _on_add_link(self):
filename = askopenfilename(initialdir='training')
if filename:
filename = os.path.relpath(filename, CUR_DIR)
self._toggle_state(self.hypermgr.add(filename), self.addLink)
def _on_remove_link(self):
if self.txt.tag_ranges('sel'):
current_tags = self.txt.tag_names('sel.first')
if 'hyper' in current_tags:
self._toggle_state(['hyper'], self.addLink)
def _register_event_handlers(self):
self.txt.event_add('<<KeyRel>>', '<KeyRelease-Home>', '<KeyRelease-End>',
'<KeyRelease-Left>', '<KeyRelease-Right>',
'<KeyRelease-Up>', '<KeyRelease-Down>',
'<KeyRelease-Delete>', '<KeyRelease-BackSpace>')
Widget.bind(self.txt, '<<Selection>>', self._sel_changed)
Widget.bind(self.txt, '<ButtonRelease-1>', self._sel_changed)
Widget.bind(self.txt, '<<KeyRel>>', self._key_release)
def _key_release(self, event):
line, char = parse_index(self.txt.index(INSERT))
self.update_button_state(to_string(line, char))
def _sel_changed(self, event):
self.update_button_state(self.txt.index(INSERT))
def is_dirty(self):
return self.curr_annotation != self.get_annotation()
def reset_toolbar_buttons(self):
for btn in self.btnset:
btn.configure(relief='raised')
def update_button_state(self, index):
if self.txt.tag_ranges('sel'):
current_tags = self.txt.tag_names('sel.first')
else:
current_tags = self.txt.tag_names(index)
for btn in self.btnmap.itervalues():
btn.configure(relief='raised')
for tag in current_tags:
if tag in self.btnmap.keys():
self.btnmap[tag].configure(relief='sunken')
def init_font_sizes(self, font, size):
self.txt.config(font=[font, size])
self._b_font = Font(self.root, (font, size, 'bold'))
self._i_font = Font(self.root, (font, size, 'italic'))
def init_tags(self):
self.txt.tag_config('bold', font=self._b_font, wrap='word')
self.txt.tag_config('italic', font=self._i_font, wrap='word')
self.txt.tag_config('number', tabs='.5c center 1c left',
lmargin1='0', lmargin2='1c')
self.txt.tag_config('bullet', tabs='.5c center 1c left',
lmargin1='0', lmargin2='1c')
def init_images(self):
self.bold_image = PhotoImage(file=BOLD_IMAGE)
self.italic_image = PhotoImage(file=ITALIC_IMAGE)
self.addlink_image = PhotoImage(file=ADDLINK_IMAGE)
self.remlink_image = PhotoImage(file=REMLINK_IMAGE)
self.bullets_image = PhotoImage(file=BULLETS_IMAGE)
self.bullet_image = PhotoImage(file=BULLET_IMAGE)
self.numbers_image = PhotoImage(file=NUMBERS_IMAGE)
self.undo_image = PhotoImage(file=UNDO_IMAGE)
self.undoall_image= PhotoImage(file=UNDOALL_IMAGE)
self.redo_image = PhotoImage(file=REDO_IMAGE)
self.redoall_image = PhotoImage(file=REDOALL_IMAGE)
def init_toolbar_buttons(self):
self.bold = Button(name='bold', image=self.bold_image,
borderwidth=1, command=self._on_bold)
self.bold.grid(in_=self.toolbar, row=0, column=0, sticky=W)
self.italic = Button(name='italic', image=self.italic_image,
borderwidth=1, command=self._on_italic)
self.italic.grid(in_=self.toolbar, row=0, column=1, sticky=W)
self.bullets = Button(name='bullets', image=self.bullets_image,
borderwidth=1, command=self._on_bullets)
self.bullets.grid(in_=self.toolbar, row=0, column=2, sticky=W)
self.numbers = Button(name='numbers', image=self.numbers_image,
borderwidth=1, command=self._on_numbers)
self.numbers.grid(in_=self.toolbar, row=0, column=3, sticky=W)
self.addLink = Button(name='addlink', image=self.addlink_image,
borderwidth=1, command=self._on_add_link)
self.addLink.grid(in_=self.toolbar, row=0, column=4, sticky=W)
self.remLink = Button(name='remlink', image=self.remlink_image,
borderwidth=1, command=self._on_remove_link)
self.remLink.grid(in_=self.toolbar, row=0, column=5, sticky=W)
self.frame = Frame(width=0)
self.frame.grid(in_=self.toolbar, padx=5, row=0, column=6, sticky=W)
self.undoall = Button(name='undoall', image=self.undoall_image,
borderwidth=1, command=self._on_undo_all)
self.undoall.grid(in_=self.toolbar, row=0, column=7, sticky=W)
self.undo = Button(name='undo', image=self.undo_image,
borderwidth=1, command=self._on_undo)
self.undo.grid(in_=self.toolbar, row=0, column=8, sticky=W)
self.redo = Button(name='redo', image=self.redo_image,
borderwidth=1, command=self._on_redo)
self.redo.grid(in_=self.toolbar, row=0, column=9, sticky=W)
self.redoall = Button(name='redoall', image=self.redoall_image,
borderwidth=1, command=self._on_redo_all)
self.redoall.grid(in_=self.toolbar, row=0, column=10, sticky=W)
self.bold_tooltip = ToolTip(self.bold, 'Bold')
self.italic_tooltip = ToolTip(self.italic, 'Italic')
self.bullets_tooltip = ToolTip(self.bullets, 'Bullet list')
self.numbers_tooltip = ToolTip(self.numbers, 'Numbered list')
self.addlink_tooltip = ToolTip(self.addLink, 'Add hyperlink')
self.remlink_tooltip = ToolTip(self.remLink, 'Remove hyperlink')
self.undoall_tooltip = ToolTip(self.undoall, 'First move')
self.undo_tooltip = ToolTip(self.undo, 'Back one move')
self.redo_tooltip = ToolTip(self.redo, 'Forward one move')
self.redoall_tooltip = ToolTip(self.redoall, 'Last move')
def reset_view(self, model):
self._model = model
self.txt.delete('1.0', END)
sq = self._model.curr_state.valid_squares
self.canvas.delete(self.dark_color)
self.canvas.delete(self.light_color)
starting_squares = [i for i in sq if (self._model.curr_state.squares[i]
& (BLACK | WHITE))]
self._draw_checkers(Command(add=starting_squares))
for i in sq:
self.highlight_square(i, DARK_SQUARES)
def calc_board_loc(self, x, y):
vx, vy = self.calc_valid_xy(x, y)
xi = int(vx / self._square_size)
yi = int(vy / self._square_size)
return xi, yi
def calc_board_pos(self, xi, yi):
return self._boardpos.get(xi + yi * 8, 0)
def calc_grid_pos(self, pos):
return self._gridpos[pos]
def highlight_square(self, idx, color):
row, col = self._gridpos[idx]
hpos = col + row * 8
self.canvas.itemconfigure('o'+str(hpos), outline=color)
def calc_valid_xy(self, x, y):
return (min(max(0, self.canvas.canvasx(x)), self._board_side-1),
min(max(0, self.canvas.canvasy(y)), self._board_side-1))
def notify(self, move):
add_lst = []
rem_lst = []
for idx, _, newval in move.affected_squares:
if newval & FREE:
rem_lst.append(idx)
else:
add_lst.append(idx)
cmd = Command(add=add_lst, remove=rem_lst)
self._draw_checkers(cmd)
self.txt.delete('1.0', END)
self.serializer.restore(move.annotation)
self.curr_annotation = move.annotation
if self.txt.get('1.0','end').strip() == '':
start = KEY_MAP[move.affected_squares[FIRST][0]]
dest = KEY_MAP[move.affected_squares[LAST][0]]
movestr = '%d-%d' % (start, dest)
self.txt.insert('1.0', movestr)
def get_annotation(self):
return self.serializer.dump()
def erase_checker(self, index):
self.canvas.delete('c'+str(index))
def flip_board(self, flip):
self._delete_labels()
self.canvas.delete(self.dark_color)
self.canvas.delete(self.light_color)
if self.flip_view != flip:
self.flip_view = flip
self._gridpos = reverse_dict(self._gridpos)
self._boardpos = reverse_dict(self._boardpos)
self._label_board()
starting_squares = [i for i in self._model.curr_state.valid_squares
if self._model.curr_state.squares[i] &
(BLACK | WHITE)]
all_checkers = Command(add=starting_squares)
self._draw_checkers(all_checkers)
def update_statusbar(self, output=None):
if output:
self._statusbar['text'] = output
self.root.update()
return
if self._model.terminal_test():
text = "Game over. "
if self._model.curr_state.to_move == WHITE:
text += "Black won."
else:
text += "White won."
self._statusbar['text'] = text
return
if self._model.curr_state.to_move == WHITE:
self._statusbar['text'] = "White to move"
else:
self._statusbar['text'] = "Black to move"
def get_positions(self, type):
return map(str, sorted((KEY_MAP[i]
for i in self._model.curr_state.valid_squares
if self._model.curr_state.squares[i] == type)))
# private functions
def _setup_board(self, root):
for r in range(0, 8, 2):
row = r * self._square_size
for c in range(0, 8, 2):
col = c * self._square_size
self.canvas.create_rectangle(col, row, col+self._square_size-1,
row+self._square_size-1,
fill=LIGHT_SQUARES,
outline=LIGHT_SQUARES)
for c in range(1, 8, 2):
col = c * self._square_size
self.canvas.create_rectangle(col, row+self._square_size,
col+self._square_size-1,
row+self._square_size*2-1,
fill=LIGHT_SQUARES,
outline=LIGHT_SQUARES)
for r in range(0, 8, 2):
row = r * self._square_size
for c in range(1, 8, 2):
col = c * self._square_size
self.canvas.create_rectangle(col, row, col+self._square_size-1,
row+self._square_size-1,
fill=DARK_SQUARES,
outline=DARK_SQUARES,
tags='o'+str(r*8+c))
for c in range(0, 8, 2):
col = c * self._square_size
self.canvas.create_rectangle(col, row+self._square_size,
col+self._square_size-1,
row+self._square_size*2-1,
fill=DARK_SQUARES,
outline=DARK_SQUARES,
tags='o'+str(((r+1)*8)+c))
def _label_board(self):
for key, pair in self._gridpos.iteritems():
row, col = pair
xpos, ypos = col * self._square_size, row * self._square_size
self.canvas.create_text(xpos+self._square_size-7,
ypos+self._square_size-7,
text=str(KEY_MAP[key]),
fill=LIGHT_SQUARES, tag='label')
def _delete_labels(self):
self.canvas.delete('label')
def _draw_checkers(self, change):
if change == None:
return
for i in change.remove:
self.canvas.delete('c'+str(i))
for i in change.add:
checker = self._model.curr_state.squares[i]
color = self.dark_color if checker & COLORS == BLACK else self.light_color
row, col = self._gridpos[i]
x = col * self._square_size + self._piece_offset
y = row * self._square_size + self._piece_offset
tag = 'c'+str(i)
self.canvas.create_oval(x+2, y+2, x+2+CHECKER_SIZE,
y+2+CHECKER_SIZE, outline='black',
fill='black', tags=(color, tag))
self.canvas.create_oval(x, y, x+CHECKER_SIZE, y+CHECKER_SIZE,
outline='black', fill=color,
tags=(color, tag))
if checker & KING:
self.canvas.create_image(x+15, y+15, image=self._crownpic,
anchor=CENTER, tags=(color, tag))