# HG changeset patch # User Laman # Date 2015-08-23 22:09:28 # Node ID eb2effd8ec3524705220599f53a71b9f3ed4c8b3 # Parent 3bc6cb7cfd8be11b58094f6d0075deaa0fd7c579 GUI modifications removed Hello World added correct handling for clicked board corners added board for showing the detected game state diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -1,2 +1,2 @@ ^images/ -^__pycache__/ \ No newline at end of file +^src/__pycache__/ \ No newline at end of file diff --git a/src/gui.py b/src/gui.py --- a/src/gui.py +++ b/src/gui.py @@ -3,6 +3,7 @@ from PIL import ImageTk import PIL import math + class EPoint: def __init__(self,x,y): self.x=x @@ -10,49 +11,50 @@ class EPoint: def dist(self,a): return math.sqrt((self.x-a.x)**2+(self.y-a.y)**2) + + def __str__(self): return "({0},{1})".format(self.x,self.y) + def __repr__(self): return "EPoint({0},{1})".format(self.x,self.y) + class Application(tk.Frame): def __init__(self, master=None): self.corners=[] tk.Frame.__init__(self, master) - self.pack() + self.grid(column=0,row=0) self.createWidgets() def createWidgets(self): - self.hi_there = tk.Button(self) - self.hi_there["text"] = "Hello World\n(click me)" - self.hi_there["command"] = self.say_hi - self.hi_there.pack(side="top") - - self.canvas=tk.Canvas(self) - self.canvas.configure(width=480,height=360,background="#ff4444") + # a captured frame with overlay graphics + self.imgView=tk.Canvas(self) + self.imgView.configure(width=480,height=360) imgOrig=PIL.Image.open("../images/1.jpg") - self.img=ImageTk.PhotoImage(imgOrig.resize((int(self.canvas['width']),int(self.canvas['height'])),resample=PIL.Image.BILINEAR)) + self.img=ImageTk.PhotoImage(imgOrig.resize((int(self.imgView['width']),int(self.imgView['height'])),resample=PIL.Image.BILINEAR)) - self.canvas.bind('<1>',lambda e: self.addCorner(e.x,e.y)) - self.canvas.create_image(2,2,anchor="nw",image=self.img) - self.canvas.create_line(30,30,40,40,fill="#00ff00") - self.canvas.create_line(30,40,40,30,fill="#00ff00") + self.imgView.bind('<1>',lambda e: self.addCorner(e.x,e.y)) + self.redrawImgView() - self.canvas.pack() - - self.QUIT = tk.Button(self, text="QUIT", fg="red", command=root.destroy) - self.QUIT.pack(side="bottom") - - def say_hi(self): - print("hi there, everyone!") + self.imgView.grid(column=0,row=0) + + # board with detected stones + self.boardView=BoardView(self) + self.boardView.grid(column=1,row=0) def addCorner(self,x,y): a=EPoint(x,y) for i,c in enumerate(self.corners): # move an improperly placed point if a.dist(c)<20: self.corners[i]=a + # print(self.corners) + self.redrawImgView() return if len(self.corners)<4: # add a new corner self.corners.append(a) - return + # print(self.corners) + self.redrawImgView() + + if len(self.corners)<4: return index,minDist=0,float('inf') # replace the corner closest to the clicked point for i,c in enumerate(self.corners): @@ -60,10 +62,57 @@ class Application(tk.Frame): index,minDist=i,a.dist(c) self.corners[index]=a - print(self.corners) + # print(self.corners) + + self.redrawImgView() + + def redrawImgView(self): + self.imgView.create_image(2,2,anchor="nw",image=self.img) + for corner in self.corners: + self.markPoint(corner.x,corner.y) + + self.imgView.grid() + + def markPoint(self,x,y): + self.imgView.create_line(x-3,y-3,x+4,y+4,fill="#00ff00") + self.imgView.create_line(x-3,y+3,x+4,y-4,fill="#00ff00") + + self.imgView.grid() + + +class BoardView(tk.Canvas): + def __init__(self, master=None): + tk.Canvas.__init__(self, master) + self.configure(width=360,height=360,background="#ffcc00") + + self.drawGrid() + self.drawStones() - def redrawGrid(self): + self.grid() + + def drawGrid(self): + 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.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') + + def drawStones(self): pass + + ## Draws a stone at provided coordinates. + # + # @param r row coordinate, [0-18], counted from top + # @param c column coordinate, [0-18], counted from left + # @param color color indicator, {'b','w'} + def drawStone(self,r,c,color): + self.create_oval(r*18-9,c*18-9,r*18+9,r*18+9,'#000000' if color=='b' else '#ffffff') + root = tk.Tk() app = Application(master=root)