Files @ d138366bd4bb
Branch filter:

Location: OneEye/src/gui/boardview.py - annotation

Laman
resizing BoardView
import tkinter as tk

from go import BLACK,WHITE


## Handles and presents the game state as detected by the program.
class BoardView(tk.Canvas):
	def __init__(self, master=None):
		self._master=master
		tk.Canvas.__init__(self, master, highlightthickness=0)

		self.width=360
		self.height=360
		self.padding=18
		self.cellWidth=(self.width-2*self.padding)/18
		self.cellHeight=(self.height-2*self.padding)/18
		self.configure(width=self.width,height=self.height,background="#ffcc00")

		self.drawGrid()
		master.bind("<Configure>", self._onResize)

	def redrawState(self,gameState):
		self.delete("black","white")
		for r,row in enumerate(gameState):
			for c,point in enumerate(row):
				self.drawStone(r,c,point)

	def drawGrid(self):
		padding=self.padding
		for i in range(19):
			self.create_line(padding,18*i+padding,self.width-padding,18*i+padding,tags="row",fill="#000000") # rows
			self.create_line(18*i+padding,padding,18*i+padding,self.height-padding,tags="col",fill="#000000") # cols

		self.drawStars()

	def drawStars(self):
		radius=2

		for r in range(3,19,6):
			for c in range(3,19,6):
				x=c*self.cellHeight+self.padding
				y=r*self.cellWidth+self.padding
				self.create_oval(x-radius,y-radius,x+radius,y+radius,tags="star",fill='#000000')

	## Draws a stone at provided coordinates.
	#
	#  For an unknown color draws nothing and returns False.
	#
	#  @param r row coordinate, [0-18], counted from top
	#  @param c column coordinate, [0-18], counted from left
	#  @param color color indicator, go.BLACK or go.WHITE
	def drawStone(self,r,c,color):
		if color==BLACK:
			hexCode='#000000'
			tag="black"
		elif color==WHITE:
			hexCode='#ffffff'
			tag="white"
		else: return False

		x=c*self.cellWidth+self.padding
		y=r*self.cellHeight+self.padding
		radius=self.cellWidth/2
		self.create_oval(x-radius,y-radius,x+radius,y+radius,tags=tag,fill=hexCode)

	def _onResize(self, event):
		wScale=float(event.width)/self.width
		hScale=float(event.height)/self.height
		scale=min(wScale,hScale)

		self.width*=scale
		self.height*=scale
		self.scale("all",0,0,scale,scale) # rescale all the objects tagged with the "all" tag

		x=(event.width-self.width)/2
		y=(event.height-self.height)/2

		# place the window, giving it an explicit size
		self.place(in_=self._master, x=x, y=y,
				width=self.width, height=self.height)