diff --git a/src/gui/__init__.py b/src/gui/__init__.py --- a/src/gui/__init__.py +++ b/src/gui/__init__.py @@ -1,12 +1,15 @@ import threading +import logging as log import tkinter as tk from PIL import ImageTk import PIL import config +from epoint import EPoint from corners import Corners import image_analyzer from go import Go +from grid import Grid class MainWindow(tk.Frame): @@ -15,23 +18,31 @@ class MainWindow(tk.Frame): self.corners=Corners() self.currentFrame=None - self.boardGrid=None + self._boardGrid=None self.gameState=None self.img=None + self._imgSizeCoef=1 + self._imgShift=EPoint(0,0) tk.Frame.__init__(self, master) self.grid(column=0,row=0) self._createWidgets() - def setCurrentFrame(self,frame,grid): + def setCurrentFrame(self,frame): self.currentFrame=frame - self.boardGrid=grid w=int(self.imgView['width']) h=int(self.imgView['height']) self.img=ImageTk.PhotoImage(frame.resize((w,h),resample=PIL.Image.BILINEAR)) + wo,ho=self.currentFrame.size # o for original + widthRatio=wo/w + heightRatio=ho/h + self._imgSizeCoef=max(widthRatio,heightRatio) + # shift compensates possible horizontal or vertical empty margins from unmatching aspect ratios + self._imgShift=EPoint(wo-w*self._imgSizeCoef,ho-h*self._imgSizeCoef)/2 + def setGameState(self,gameState): pass @@ -67,7 +78,11 @@ class MainWindow(tk.Frame): def addCorner(self,x,y): self.corners.add(x,y) if self.corners.canonizeOrder(): - self.parent.sendMsg("setCorners",(self.corners,)) + # transform corners from show coordinates to real coordinates + log.debug(self.corners.corners) + self._boardGrid=Grid(self.corners.corners) + corners=[(c*self._imgSizeCoef+self._imgShift) for c in self.corners.corners] + self.parent.sendMsg("setCorners",(corners,)) # self.boardGrid=Grid(self.corners) # self.boardView.setBoardGrid(self.boardGrid) # @@ -78,30 +93,23 @@ class MainWindow(tk.Frame): if self.currentFrame and self.img: self.imgView.create_image(2,2,anchor="nw",image=self.img) - origWidth,origHeight=self.currentFrame.size - widthRatio=origWidth/self.img.width() - heightRatio=origHeight/self.img.height() - sizeCoef=max(widthRatio,heightRatio) - - # shift=EPoint(origWidth-self.img.width()*sizeCoef,origHeight-self.img.height()*sizeCoef)/2 - for corner in self.corners.corners: self.markPoint(corner.x,corner.y) - if self.boardGrid!=None and config.gui.showGrid: + if self._boardGrid!=None and config.gui.showGrid: for r in range(19): - a=self.boardGrid.intersections[r][0] - b=self.boardGrid.intersections[r][-1] + a=self._boardGrid.intersections[r][0] + b=self._boardGrid.intersections[r][-1] self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00') for c in range(19): - a=self.boardGrid.intersections[0][c] - b=self.boardGrid.intersections[-1][c] + a=self._boardGrid.intersections[0][c] + b=self._boardGrid.intersections[-1][c] self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00') - if self.boardGrid!=None and config.gui.showBigPoints: + if self._boardGrid!=None and config.gui.showBigPoints: for r in range(19): for c in range(19): - ((r1,c1),(r2,c2))=image_analyzer.relevantRect(self.boardGrid.intersections[r][c],*(self.boardGrid.stoneSizeAt(r,c,sizeCoef))) + ((r1,c1),(r2,c2))=image_analyzer.relevantRect(self._boardGrid.intersections[r][c], *(self._boardGrid.stoneSizeAt(r, c))) self.imgView.create_rectangle(r1,c1,r2,c2,outline="#00ffff") self.imgView.grid() @@ -121,7 +129,6 @@ class MainWindow(tk.Frame): class BoardView(tk.Canvas): def __init__(self, master=None): # self.detector=ImageAnalyzer() - self.boardGrid=None tk.Canvas.__init__(self, master) self.configure(width=360,height=360,background="#ffcc00") @@ -210,17 +217,17 @@ class GUI: msg=incomingQueue.get() if incomingQueue.empty(): incomingEvent.clear() - print(msg) + log.info(msg) self._handleEvent(msg) def _handleEvent(self,e): - actions={"setCurrentFrame": self._frameHandler} + actions={"setCurrentFrame":self._frameHandler} (actionName,args,kwargs)=e return actions[actionName](*args,**kwargs) - def _frameHandler(self,newFrame,grid): - self.mainWindow.setCurrentFrame(newFrame,grid) + def _frameHandler(self,newFrame): + self.mainWindow.setCurrentFrame(newFrame) self.root.event_generate("<>") def _stateHandler(self,e):