2012-04-17 05:01:34 +00:00
|
|
|
# This is a widget that handles a go game
|
|
|
|
# It needs a goban object, and handles it from beginning to end
|
|
|
|
|
2012-04-17 05:21:39 +00:00
|
|
|
import os
|
2012-04-17 05:01:34 +00:00
|
|
|
import gtk
|
2012-04-17 05:21:39 +00:00
|
|
|
|
|
|
|
import goban
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GoGame(gtk.HBox):
|
|
|
|
"""
|
|
|
|
This class draws a board and some information about the game (turn, captures, etc).
|
|
|
|
It also handles moves and other operations on the goban.
|
|
|
|
"""
|
|
|
|
|
2012-04-17 05:46:55 +00:00
|
|
|
img_res = None
|
2012-04-17 05:01:34 +00:00
|
|
|
|
2012-04-17 18:59:46 +00:00
|
|
|
def __init__(self, goban, network_mode=False, our_color=None):
|
2012-04-17 05:21:39 +00:00
|
|
|
super(GoGame, self).__init__()
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
self.goban = goban
|
|
|
|
|
2012-04-17 05:46:55 +00:00
|
|
|
if GoGame.img_res is None:
|
|
|
|
GoGame.img_res = _build_img_res()
|
|
|
|
self.init_widgets()
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
|
2012-04-17 05:46:55 +00:00
|
|
|
def init_widgets(self):
|
2012-04-17 05:21:39 +00:00
|
|
|
# All of this is to create the info box along the right side of the board
|
2012-04-17 05:01:34 +00:00
|
|
|
info_box = gtk.VBox()
|
2012-04-17 05:21:39 +00:00
|
|
|
info_rows = []
|
|
|
|
for i in range(3):
|
2012-04-17 05:39:16 +00:00
|
|
|
info_rows.append(gtk.HBox())
|
2012-04-17 05:21:39 +00:00
|
|
|
|
2012-04-17 05:01:34 +00:00
|
|
|
to_move_label = gtk.Label('To Move:')
|
|
|
|
black_cap_label = gtk.Label('Black Captures:')
|
|
|
|
white_cap_label = gtk.Label('White Captures:')
|
|
|
|
|
2012-04-17 05:39:16 +00:00
|
|
|
self.to_move_value = gtk.Label('None')
|
|
|
|
self.black_cap_value = gtk.Label('0')
|
|
|
|
self.white_cap_value = gtk.Label('0')
|
2012-04-17 05:01:34 +00:00
|
|
|
|
2012-04-17 05:39:16 +00:00
|
|
|
info_rows[0].pack_start(to_move_label, expand=False, padding=5)
|
|
|
|
info_rows[1].pack_start(black_cap_label, expand=False, padding=5)
|
|
|
|
info_rows[2].pack_start(white_cap_label, expand=False, padding=5)
|
|
|
|
info_rows[0].pack_end(self.to_move_value, expand=False, padding=5)
|
|
|
|
info_rows[1].pack_end(self.black_cap_value, expand=False, padding=5)
|
|
|
|
info_rows[2].pack_end(self.white_cap_value, expand=False, padding=5)
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
for row in info_rows:
|
2012-04-17 05:39:16 +00:00
|
|
|
info_box.pack_start(row, expand=False)
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
self.pass_button = gtk.Button('Pass')
|
|
|
|
self.resign_button = gtk.Button('Resign')
|
2012-04-17 05:46:55 +00:00
|
|
|
self.pass_button.connect('clicked', self.on_pass)
|
|
|
|
self.resign_button.connect('clicked', self.on_resign)
|
2012-04-17 05:01:34 +00:00
|
|
|
|
2012-04-17 05:39:16 +00:00
|
|
|
info_box.pack_start(self.pass_button, expand=False, padding=10)
|
|
|
|
info_box.pack_start(self.resign_button, expand=False, padding=10)
|
2012-04-17 05:01:34 +00:00
|
|
|
|
2012-04-17 22:07:04 +00:00
|
|
|
self.board_area = gtk.DrawingArea()
|
|
|
|
self.board_area.set_size_request(800, 800)
|
|
|
|
self.board_area.connect('expose-event', self.draw_board)
|
|
|
|
|
2012-04-17 05:01:34 +00:00
|
|
|
self.pack_start(self.board_area)
|
2012-04-17 05:39:16 +00:00
|
|
|
self.pack_end(info_box, expand=False)
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
|
2012-04-17 22:07:04 +00:00
|
|
|
# fixme: create a backbuffer pixmap and draw to *that* when we need to update
|
|
|
|
# the board, then just use *this* to print that to the DrawingArea...
|
|
|
|
def draw_board(self, widget, event):
|
|
|
|
gc = widget.get_style().fg_gc[gtk.STATE_NORMAL]
|
|
|
|
drawable = widget.window
|
|
|
|
# pixmap = gtk.gdk.Pixmap(window, width, height, depth=-1)
|
|
|
|
|
|
|
|
width, height = widget.size_request()
|
|
|
|
size = min(width, height)
|
|
|
|
board_size = self.goban.board_size
|
|
|
|
inc = size / board_size
|
|
|
|
|
|
|
|
for i in range(len(self.goban.board)):
|
|
|
|
# Get the right image and scale it
|
|
|
|
base_img = GoGame.img_res[self.goban.draw_code(i)]
|
|
|
|
img = base_img.scale_simple(inc, inc, gtk.gdk.INTERP_BILINEAR)
|
|
|
|
|
|
|
|
row = i / board_size
|
|
|
|
col = i % board_size
|
|
|
|
drawable.draw_pixbuf(gc, img, 0, 0, inc * col, inc * row, inc, inc)
|
2012-04-17 05:01:34 +00:00
|
|
|
|
2012-04-17 05:39:16 +00:00
|
|
|
|
2012-04-17 05:46:55 +00:00
|
|
|
def on_pass(self, widget):
|
|
|
|
print 'GoGame.on_pass(): stub'
|
2012-04-17 05:39:16 +00:00
|
|
|
|
|
|
|
|
2012-04-17 05:46:55 +00:00
|
|
|
def on_resign(self, widget):
|
|
|
|
print 'GoGame.on_resign(): stub'
|
2012-04-17 05:39:16 +00:00
|
|
|
|
2012-04-17 05:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# pixmap.draw_pixbuf(None, pixbuf, 0, 0, x, y, -1, -1, gtk.gdk.RGB_DITHER_NONE, 0, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _magnitude(vector):
|
|
|
|
x,y = vector
|
|
|
|
return math.sqrt(x*x + y*y)
|
|
|
|
|
|
|
|
|
|
|
|
def _build_img_res():
|
|
|
|
ret = {}
|
|
|
|
|
|
|
|
triangle = _load_png('go_t.png')
|
|
|
|
|
|
|
|
ret['w'] = _load_png('go_w.png')
|
|
|
|
ret['wT'] = _load_png('go_w.png')
|
|
|
|
# ret['wT'].blit(triangle, (0,0))
|
|
|
|
ret['wH'] = _load_png('go_w.png', 128)
|
|
|
|
|
|
|
|
ret['b'] = _load_png('go_b.png')
|
|
|
|
ret['bT'] = _load_png('go_b.png')
|
|
|
|
# ret['bT'].blit(triangle, (0,0))
|
|
|
|
ret['bH'] = _load_png('go_b.png', 128)
|
|
|
|
|
|
|
|
circle = _load_png('go_c.png')
|
|
|
|
|
|
|
|
for d in ('u', 'd', 'l', 'r', 'm', 'dl', 'dr', 'ul', 'ur', 'h'):
|
|
|
|
ret[d] = _load_png('go_' + d + '.png')
|
|
|
|
ret[d + 'C'] = _load_png('go_' + d + '.png')
|
|
|
|
# ret[d + 'C'].blit(circle, (0,0))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def _load_png(name, alpha=None):
|
|
|
|
""" Load image and return image object"""
|
|
|
|
fullname = os.path.join('ui/res/', name)
|
|
|
|
image = gtk.gdk.pixbuf_new_from_file(fullname)
|
|
|
|
|
|
|
|
# if alpha is not None:
|
|
|
|
# image.set_alpha(alpha)
|
|
|
|
|
|
|
|
return image
|