Changeset - d138366bd4bb
[Not reviewed]
default
0 4 0
Laman - 8 years ago 2017-02-20 22:04:07

resizing BoardView
4 files changed with 63 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/gui/__init__.py
Show inline comments
 
@@ -11,12 +11,14 @@ class GUI:
 
		self.root = tk.Tk()
 
		self.root.title("OneEye {0}.{1}.{2}".format(*config.misc.version))
 
 
		self._coreMessages=None
 
 
		self.mainWindow = MainWindow(self, master=self.root)
 
		self.root.columnconfigure(0,weight=1)
 
		self.root.rowconfigure(0,weight=1)
 
 
		self.root.bind("<<redrawImgView>>", lambda e: self.mainWindow.redrawImgView())
 
		self.root.bind("<<receiveState>>", lambda e: print("fired receiveState"))
 
 
	def __call__(self,ownMessages,coreMessages):
 
		self._coreMessages=coreMessages
src/gui/boardview.py
Show inline comments
 
import tkinter as tk
 

	
 
import go
 
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):
 
		tk.Canvas.__init__(self, master)
 
		self.configure(width=360,height=360,background="#ffcc00")
 
		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()
 

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

	
 
	def redrawState(self,gameState):
 
		# !! will need to remove old stones or redraw the widget completely
 
		self.delete("black","white")
 
		for r,row in enumerate(gameState):
 
			for c,point in enumerate(row):
 
				self.drawStone(r,c,point)
 

	
 
		self.grid()
 

	
 
	def drawGrid(self):
 
		padding=self.padding
 
		for i in range(19):
 
			self.create_line(18,18*(i+1),360-18,18*(i+1),fill="#000000") # rows
 
			self.create_line(18*(i+1),18,18*(i+1),360-18,fill="#000000") # cols
 
			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):
 
		for r in range(4,19,6):
 
			for c in range(4,19,6):
 
				self.create_oval(r*18-2,c*18-2,r*18+2,c*18+2,fill='#000000')
 
		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==go.BLACK: hexCode='#000000'
 
		elif color==go.WHITE: hexCode='#ffffff'
 
		if color==BLACK:
 
			hexCode='#000000'
 
			tag="black"
 
		elif color==WHITE:
 
			hexCode='#ffffff'
 
			tag="white"
 
		else: return False
 
		r+=1
 
		c+=1
 
		self.create_oval(c*18-9,r*18-9,c*18+9,r*18+9,fill=hexCode)
 

	
 
		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)
src/gui/mainwindow.py
Show inline comments
 
import logging as log
 
import tkinter as tk
 
from tkinter import N,S,E,W
 

	
 
import PIL
 
from PIL import ImageTk
 

	
 
import config
 
import imageanalyzer
 
from corners import Corners
 
from epoint import EPoint
 
@@ -22,13 +22,13 @@ class MainWindow(tk.Frame):
 

	
 
		self.img=None
 
		self._imgSizeCoef=1
 
		self._imgShift=EPoint(0,0)
 

	
 
		tk.Frame.__init__(self, master)
 
		self.grid(column=0,row=0)
 
		self.grid(column=0,row=0,sticky=(N,S,E,W))
 
		self._createWidgets()
 

	
 
	def setCurrentFrame(self,frame):
 
		self.currentFrame=frame
 

	
 
		w=int(self.imgView['width'])
 
@@ -43,30 +43,35 @@ class MainWindow(tk.Frame):
 
		frame.thumbnail((w,h)) # resize
 
		self.img=ImageTk.PhotoImage(frame)
 

	
 
	def _createWidgets(self):
 
		# a captured frame with overlay graphics
 
		self.imgView=tk.Canvas(self)
 
		self.imgView.configure(width=480,height=360)
 
		self.imgView.configure(width=480,height=360,background="#ff00ff")
 

	
 
		self.imgView.bind('<1>',lambda e: self.addCorner(e.x,e.y))
 

	
 
		self.imgView.grid(column=0,row=0)
 
		self.imgView.grid(column=0,row=0,sticky=(N,S,E,W))
 

	
 
		# board with detected stones
 
		self.boardView= BoardView(self)
 
		self.boardView.grid(column=1,row=0)
 
		self._boardWrapper=tk.Frame(self,width=360,height=360)
 
		self.boardView=BoardView(self._boardWrapper)
 
		self._boardWrapper.grid(column=1,row=0,sticky=(N,S,E,W))
 

	
 
		# more controls below the board
 
		self.scaleTresB=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=self.refreshTresholds)
 
		self.scaleTresW=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=self.refreshTresholds)
 
		self.scaleTresB.set(30.0) # !! proper defaults
 
		self.scaleTresW.set(60.0)
 
		self.scaleTresB.grid(column=0,row=1,columnspan=2)
 
		self.scaleTresW.grid(column=0,row=2,columnspan=2)
 

	
 
		self.columnconfigure(0,weight=1)
 
		self.columnconfigure(1,weight=1)
 
		self.rowconfigure(0,weight=1)
 

	
 
		# render everything
 
		self.redrawImgView()
 

	
 
	## Stores a grid corner located at x,y coordinates.
 
	def addCorner(self,x,y):
 
		self.corners.add(x,y)
src/imageanalyzer.py
Show inline comments
 
@@ -35,13 +35,13 @@ class ImageAnalyzer:
 
		for y in range(y1,y2+1):
 
			for x in range(x1,x2+1):
 
				red,green,blue=image.getpixel((x,y))
 
 
				I=(red+green+blue)/255/3
 
				m=min(red,green,blue)
 
				S=1-m/I
 
				S=1-m/I if I!=0 else 0
 
				if 100*I<self.tresB: b+=1
 
				elif 100*I>self.tresW: w+=1
 
				else: e+=1
 
 
		log.debug("(%d,%d) ... (b=%d,w=%d,e=%d)", row, col, b, w, e)
 
0 comments (0 inline, 0 general)