Esempio n. 1
0
class HangDude(HorizontalPanel):
    def __init__(self):
        HorizontalPanel.__init__(self)
        self.context = GWTCanvas(300,300,300,300)
        self.context.addStyleName("gwt-canvas")
        self.add(self.context)

    def clear(self):
        self.context.clear()

    def draw(self, guesses):
        self.context.fillStyle = '#000'
        self.context.lineWidth = 2 
        if guesses == 1:
            self.context.fillRect(20, 280, 200,10)
        elif guesses == 2:
            self.context.fillRect(40, 20, 10, 260)
        elif guesses == 3:
            self.context.fillRect(20, 20, 160,10)
        elif guesses == 4:
            self.context.saveContext()
            self.context.translate(80,30)
            self.context.rotate(130 * math.pi / 180)
            self.context.fillRect(0,0, 50,10)
            self.context.restoreContext()
        elif guesses == 5:
            self.context.fillRect(140, 20, 10, 50)
        elif guesses == 6:
            self.context.beginPath()
            self.context.arc(145, 100, 30, 0, math.pi * 2, True)
            self.context.closePath()
            self.context.stroke()
        elif guesses == 7:
            self.context.fillRect(145, 130, 2, 80)
        elif guesses == 8:
            self.context.saveContext()
            self.context.translate(147,160)
            self.context.rotate(130 * math.pi / 180)
            self.context.fillRect(0,0, 50,2)
            self.context.restoreContext()
        elif guesses == 9:
            self.context.saveContext()
            self.context.translate(147,160)
            self.context.rotate(45 * math.pi / 180)
            self.context.fillRect(0,0, 50,2)
            self.context.restoreContext()
        elif guesses == 10:
            self.context.saveContext()
            self.context.translate(147,210)
            self.context.rotate(130 * math.pi / 180)
            self.context.fillRect(0,0, 60,2)
            self.context.restoreContext()
        elif guesses == 11:
            self.context.saveContext()
            self.context.translate(147,210)
            self.context.rotate(45 * math.pi / 180)
            self.context.fillRect(0,0, 60,2)
            self.context.restoreContext()
        self.context.restoreContext()
Esempio n. 2
0
class HangDude(HorizontalPanel):
    def __init__(self):
        HorizontalPanel.__init__(self)
        self.context = GWTCanvas(300,300,300,300)
        self.context.addStyleName("gwt-canvas")
        self.add(self.context)

    def clear(self):
        self.context.clear()

    def draw(self, guesses):
        self.context.fillStyle = '#000'
        self.context.lineWidth = 2 
        if guesses == 1:
            self.context.fillRect(20, 280, 200,10)
        elif guesses == 2:
            self.context.fillRect(40, 20, 10, 260)
        elif guesses == 3:
            self.context.fillRect(20, 20, 160,10)
        elif guesses == 4:
            self.context.saveContext()
            self.context.translate(80,30)
            self.context.rotate(130 * math.pi / 180)
            self.context.fillRect(0,0, 50,10)
            self.context.restoreContext()
        elif guesses == 5:
            self.context.fillRect(140, 20, 10, 50)
        elif guesses == 6:
            self.context.beginPath()
            self.context.arc(145, 100, 30, 0, math.pi * 2, True)
            self.context.closePath()
            self.context.stroke()
        elif guesses == 7:
            self.context.fillRect(145, 130, 2, 80)
        elif guesses == 8:
            self.context.saveContext()
            self.context.translate(147,160)
            self.context.rotate(130 * math.pi / 180)
            self.context.fillRect(0,0, 50,2)
            self.context.restoreContext()
        elif guesses == 9:
            self.context.saveContext()
            self.context.translate(147,160)
            self.context.rotate(45 * math.pi / 180)
            self.context.fillRect(0,0, 50,2)
            self.context.restoreContext()
        elif guesses == 10:
            self.context.saveContext()
            self.context.translate(147,210)
            self.context.rotate(130 * math.pi / 180)
            self.context.fillRect(0,0, 60,2)
            self.context.restoreContext()
        elif guesses == 11:
            self.context.saveContext()
            self.context.translate(147,210)
            self.context.rotate(45 * math.pi / 180)
            self.context.fillRect(0,0, 60,2)
            self.context.restoreContext()
        self.context.restoreContext()
Esempio n. 3
0
class MainWidget(HorizontalPanel):
    def __init__(self, SW, SH):
        HorizontalPanel.__init__(self)
        self.SW = SW
        self.SH = SH
        self.context = GWTCanvas(SW, SH, SW, SH)
        self.context.addStyleName("gwt-canvas")
        self.add(self.context)

    def draw(self, game=None):
        self.context.setFillStyle(Color.Color("#00AA00"))
        self.context.fillRect(0, 0, self.SW, self.SH)

        SP = self.SW // 8

        for x in range(0, self.SW, SP):
            self.context.beginPath()
            self.context.setStrokeStyle(Color.BLACK)
            self.context.setLineWidth(1)
            self.context.moveTo(x, 0)
            self.context.lineTo(x, self.SH)
            self.context.closePath()
            self.context.stroke()
        for y in range(0, self.SH, SP):
            self.context.beginPath()
            self.context.setStrokeStyle(Color.BLACK)
            self.context.setLineWidth(1)
            self.context.moveTo(0, y)
            self.context.lineTo(self.SW, y)
            self.context.closePath()
            self.context.stroke()

        # Draw pieces
        for (x, y) in [(x, y) for x in range(8) for y in range(8)]:
            if game.state[x][y] != Game.NONE:
                if game.state[x][y] == Game.BLACK:
                    self.context.setFillStyle(Color.BLACK)
                elif game.state[x][y] == Game.WHITE:
                    self.context.setFillStyle(Color.WHITE)
                self.context.beginPath()
                self.context.arc(x * SP + SP // 2, y * SP + SP // 2, SP // 2, 0, 2 * pi)
                self.context.closePath()
                self.context.fill()
    
        # Draw liberties
        for (x, y) in Game.liberties(game.current_player, game.state):
            self.context.setFillStyle(Color.WHITE)
            self.context.beginPath()
            self.context.arc(x * SP + SP // 2, y * SP + SP // 2, 4, 0, 2 * pi)
            self.context.closePath()
            self.context.fill()
Esempio n. 4
0
class MainPanel(VerticalPanel):
    CANVAS_WIDTH = 900
    CANVAS_HEIGHT = 700
    def __init__(self, owner):
        super(VerticalPanel, self).__init__()
        self.edgequeue = list()
        self.owner = owner
        self.InitialiseScreen()
        StaticObjectsTask(self.Draw).schedule(5000) #Poll every five seconds

    def EdgeListener(self, edge):
        self.edgequeue.append(edge.asDict())
        EdgePoster(self.edgequeue).schedule(1) #Schedule in the future so edges are sent in bulk

    def InitialiseScreen(self):
        hpanel = HorizontalPanel()
        self.add(hpanel)
        vpanelMenu = VerticalPanel()
        hpanel.add(vpanelMenu)
        self.addbutton = Button("Add Triangle")
        vpanelMenu.add(self.addbutton)
        self.addbutton.addClickListener(getattr(self, "addtriangle"))
        self.canvas = GWTCanvas(self.CANVAS_WIDTH, self.CANVAS_HEIGHT)
        vpanelCanvas = VerticalPanel()
        self.canvas.setWidth(self.CANVAS_WIDTH)
        self.canvas.setHeight(self.CANVAS_HEIGHT)
        hpanel.add(vpanelCanvas)
        vpanelCanvas.add(self.canvas)
        self.canvas.addMouseListener(self)
        self.selecteditem = None
        self.selectedhandle = None
        self.mouseisdown = False
        dc = DocumentCollection.documentcollection
        DocumentCollection.documentcollection.edgelistener = self.EdgeListener
        if len(dc.documentsbyclass[model.Drawing.__name__]) == 0:
            drawing = model.Drawing(None)
            dc.AddDocumentObject(drawing)
            EdgePoster([a.asDict() for a in drawing.history.GetAllEdges()])
        else:
            for k,v in dc.documentsbyclass[model.Drawing.__name__].iteritems():
                drawing = v
        self.drawingid = drawing.id
        self.Draw()

    def GetDrawing(self):
        return DocumentCollection.documentcollection.objectsbyid[self.drawingid]

    def sortfn(self, t1, t2):
        return cmp(t1.z_order, t2.z_order)

    def GetTrianglesAsList(self):
        #Return the triangles as an ordinary python list
        #triangles = [self.drawing.triangles.GetDocument().documentobjects[objid] for objid in self.drawing.triangles]
        triangles = list()
        for triangle in self.GetDrawing().triangles:
            #triangles = self.drawing.GetDocument().documentobjects[objid]
            triangles.append(triangle)
        if len(triangles) > 0:
            triangles.sort(self.sortfn)
        return triangles

    def DrawHandle(self,x,y):
        self.canvas.setFillStyle(Color.RED)
        self.canvas.beginPath()
        self.canvas.arc(x, y, 5, 0,  math.pi * 2, False)
        self.canvas.fill()

    def Draw(self):
        self.canvas.setFillStyle(Color.WHITE)
        self.canvas.fillRect(0, 0, self.CANVAS_WIDTH, self.CANVAS_HEIGHT)
        for t in self.GetTrianglesAsList():
            self.canvas.setFillStyle(Color.BLUE)
            self.canvas.setLineWidth(5)
            self.canvas.setStrokeStyle(Color.BLACK)
            self.canvas.setLineWidth(2)
            self.canvas.beginPath()
            self.canvas.moveTo(t.x1, t.y1)
            self.canvas.lineTo(t.x2, t.y2)
            self.canvas.lineTo(t.x3, t.y3)
            self.canvas.lineTo(t.x1, t.y1)
            self.canvas.fill()
            self.canvas.stroke()

            if self.selecteditem == t:
                self.canvas.setLineWidth(1)
                self.canvas.setStrokeStyle(Color.RED)
                self.canvas.beginPath()
                self.canvas.moveTo(t.x1, t.y1)
                self.canvas.lineTo(t.x2, t.y2)
                self.canvas.lineTo(t.x3, t.y3)
                self.canvas.lineTo(t.x1, t.y1)
                self.canvas.stroke()
            
                self.DrawHandle(t.x1, t.y1)
                self.DrawHandle(t.x2, t.y2)
                self.DrawHandle(t.x3, t.y3)

    def addtriangle(self, sender):
        left_margin = 50
        triangle_spacing = 150
        drawing = self.GetDrawing()
        c = len(drawing.triangles)
        posx = left_margin + c % ((self.CANVAS_WIDTH - left_margin) // triangle_spacing) * triangle_spacing
        posy = c // ((self.CANVAS_WIDTH - left_margin) // triangle_spacing) * triangle_spacing
        t = model.Triangle(None)
        drawing.triangles.add(t)
        t.z_order = c
        setattr(t, 'x1', posx)
        setattr(t, 'y1', posy + 50)
        setattr(t, 'x2', posx + 100)
        setattr(t, 'y2', posy + 100)
        setattr(t, 'x3', posx + 50)
        setattr(t, 'y3', posy + 150)
        self.Draw()
    

    def onMouseDown(self, sender, x, y):
        self.mouseisdown = True
        if self.selecteditem is not None:
            self.selectedhandle = None
            if HandleSelected(x,y, self.selecteditem.x1, self.selecteditem.y1):
                self.selectedhandle = 1
            elif HandleSelected(x,y, self.selecteditem.x2, self.selecteditem.y2):
                self.selectedhandle = 2
            elif HandleSelected(x,y, self.selecteditem.x3, self.selecteditem.y3):
                self.selectedhandle = 3
        if self.selectedhandle is None:
            self.selecteditem = self.FindTriangle(x,y)
        if self.selecteditem is None and self.selectedhandle is None:
            self.selectedhandle = None
        if self.selecteditem is not None:
            self.selecteditem.changessuspended = True
        self.Draw()
        self.lastx = x
        self.lasty = y

    def onMouseMove(self, sender, x, y):
        if self.selecteditem is not None and self.mouseisdown:
            diffx = x - self.lastx
            diffy = y - self.lasty
            t = self.selecteditem
            if self.selectedhandle is None:
                setattr(t, 'x1', t.x1 + diffx)
                setattr(t, 'y1', t.y1 + diffy)
                setattr(t, 'x2', t.x2 + diffx)
                setattr(t, 'y2', t.y2 + diffy)
                setattr(t, 'x3', t.x3 + diffx)
                setattr(t, 'y3', t.y3 + diffy)
            elif self.selectedhandle == 1:
                setattr(t, 'x1', t.x1 + diffx)
                setattr(t, 'y1', t.y1 + diffy)
            elif self.selectedhandle == 2:
                setattr(t, 'x2', t.x2 + diffx)
                setattr(t, 'y2', t.y2 + diffy)
            elif self.selectedhandle == 3:
                setattr(t, 'x3', t.x3 + diffx)
                setattr(t, 'y3', t.y3 + diffy)
            self.lastx = x
            self.lasty = y
            self.Draw()

    def onMouseUp(self, sender,x, y):
        if self.mouseisdown and self.selecteditem:
            t = self.selecteditem
            self.selecteditem.changessuspended = False
            setattr(t, 'x1', t.x1)
            setattr(t, 'y1', t.y1)
            setattr(t, 'x2', t.x2)
            setattr(t, 'y2', t.y2)
            setattr(t, 'x3', t.x3)
            setattr(t, 'y3', t.y3)
        self.mouseisdown = False

    def onMouseEnter(self, sender, x, y):
        pass

    def onMouseLeave(self, sender, x, y):
        pass

    def FindTriangle(self, x, y):
        pt = Point(x, y)
        for t in self.GetTrianglesAsList():
            if PointInTriangle(pt, t):
                return t
        return None
Esempio n. 5
0
class MainPanel(VerticalPanel):
    CANVAS_WIDTH = 900
    CANVAS_HEIGHT = 700

    def __init__(self, owner):
        super(VerticalPanel, self).__init__()
        self.edgequeue = list()
        self.owner = owner
        self.InitialiseScreen()
        StaticObjectsTask(self.Draw).schedule(5000)  #Poll every five seconds

    def EdgeListener(self, edge):
        self.edgequeue.append(edge.asDict())
        EdgePoster(self.edgequeue).schedule(
            1)  #Schedule in the future so edges are sent in bulk

    def InitialiseScreen(self):
        hpanel = HorizontalPanel()
        self.add(hpanel)
        vpanelMenu = VerticalPanel()
        hpanel.add(vpanelMenu)
        self.addbutton = Button("Add Triangle")
        vpanelMenu.add(self.addbutton)
        self.addbutton.addClickListener(getattr(self, "addtriangle"))
        self.canvas = GWTCanvas(self.CANVAS_WIDTH, self.CANVAS_HEIGHT)
        vpanelCanvas = VerticalPanel()
        self.canvas.setWidth(self.CANVAS_WIDTH)
        self.canvas.setHeight(self.CANVAS_HEIGHT)
        hpanel.add(vpanelCanvas)
        vpanelCanvas.add(self.canvas)
        self.canvas.addMouseListener(self)
        self.selecteditem = None
        self.selectedhandle = None
        self.mouseisdown = False
        dc = DocumentCollection.documentcollection
        DocumentCollection.documentcollection.edgelistener = self.EdgeListener
        if len(dc.documentsbyclass[model.Drawing.__name__]) == 0:
            drawing = model.Drawing(None)
            dc.AddDocumentObject(drawing)
            EdgePoster([a.asDict() for a in drawing.history.GetAllEdges()])
        else:
            for k, v in dc.documentsbyclass[
                    model.Drawing.__name__].iteritems():
                drawing = v
        self.drawingid = drawing.id
        self.Draw()

    def GetDrawing(self):
        return DocumentCollection.documentcollection.objectsbyid[
            self.drawingid]

    def sortfn(self, t1, t2):
        return cmp(t1.z_order, t2.z_order)

    def GetTrianglesAsList(self):
        #Return the triangles as an ordinary python list
        #triangles = [self.drawing.triangles.GetDocument().documentobjects[objid] for objid in self.drawing.triangles]
        triangles = list()
        for triangle in self.GetDrawing().triangles:
            #triangles = self.drawing.GetDocument().documentobjects[objid]
            triangles.append(triangle)
        if len(triangles) > 0:
            triangles.sort(self.sortfn)
        return triangles

    def DrawHandle(self, x, y):
        self.canvas.setFillStyle(Color.RED)
        self.canvas.beginPath()
        self.canvas.arc(x, y, 5, 0, math.pi * 2, False)
        self.canvas.fill()

    def Draw(self):
        self.canvas.setFillStyle(Color.WHITE)
        self.canvas.fillRect(0, 0, self.CANVAS_WIDTH, self.CANVAS_HEIGHT)
        for t in self.GetTrianglesAsList():
            self.canvas.setFillStyle(Color.BLUE)
            self.canvas.setLineWidth(5)
            self.canvas.setStrokeStyle(Color.BLACK)
            self.canvas.setLineWidth(2)
            self.canvas.beginPath()
            self.canvas.moveTo(t.x1, t.y1)
            self.canvas.lineTo(t.x2, t.y2)
            self.canvas.lineTo(t.x3, t.y3)
            self.canvas.lineTo(t.x1, t.y1)
            self.canvas.fill()
            self.canvas.stroke()

            if self.selecteditem == t:
                self.canvas.setLineWidth(1)
                self.canvas.setStrokeStyle(Color.RED)
                self.canvas.beginPath()
                self.canvas.moveTo(t.x1, t.y1)
                self.canvas.lineTo(t.x2, t.y2)
                self.canvas.lineTo(t.x3, t.y3)
                self.canvas.lineTo(t.x1, t.y1)
                self.canvas.stroke()

                self.DrawHandle(t.x1, t.y1)
                self.DrawHandle(t.x2, t.y2)
                self.DrawHandle(t.x3, t.y3)

    def addtriangle(self, sender):
        left_margin = 50
        triangle_spacing = 150
        drawing = self.GetDrawing()
        c = len(drawing.triangles)
        posx = left_margin + c % ((self.CANVAS_WIDTH - left_margin) //
                                  triangle_spacing) * triangle_spacing
        posy = c // ((self.CANVAS_WIDTH - left_margin) //
                     triangle_spacing) * triangle_spacing
        t = model.Triangle(None)
        drawing.triangles.add(t)
        t.z_order = c
        setattr(t, 'x1', posx)
        setattr(t, 'y1', posy + 50)
        setattr(t, 'x2', posx + 100)
        setattr(t, 'y2', posy + 100)
        setattr(t, 'x3', posx + 50)
        setattr(t, 'y3', posy + 150)
        self.Draw()

    def onMouseDown(self, sender, x, y):
        self.mouseisdown = True
        if self.selecteditem is not None:
            self.selectedhandle = None
            if HandleSelected(x, y, self.selecteditem.x1,
                              self.selecteditem.y1):
                self.selectedhandle = 1
            elif HandleSelected(x, y, self.selecteditem.x2,
                                self.selecteditem.y2):
                self.selectedhandle = 2
            elif HandleSelected(x, y, self.selecteditem.x3,
                                self.selecteditem.y3):
                self.selectedhandle = 3
        if self.selectedhandle is None:
            self.selecteditem = self.FindTriangle(x, y)
        if self.selecteditem is None and self.selectedhandle is None:
            self.selectedhandle = None
        if self.selecteditem is not None:
            self.selecteditem.changessuspended = True
        self.Draw()
        self.lastx = x
        self.lasty = y

    def onMouseMove(self, sender, x, y):
        if self.selecteditem is not None and self.mouseisdown:
            diffx = x - self.lastx
            diffy = y - self.lasty
            t = self.selecteditem
            if self.selectedhandle is None:
                setattr(t, 'x1', t.x1 + diffx)
                setattr(t, 'y1', t.y1 + diffy)
                setattr(t, 'x2', t.x2 + diffx)
                setattr(t, 'y2', t.y2 + diffy)
                setattr(t, 'x3', t.x3 + diffx)
                setattr(t, 'y3', t.y3 + diffy)
            elif self.selectedhandle == 1:
                setattr(t, 'x1', t.x1 + diffx)
                setattr(t, 'y1', t.y1 + diffy)
            elif self.selectedhandle == 2:
                setattr(t, 'x2', t.x2 + diffx)
                setattr(t, 'y2', t.y2 + diffy)
            elif self.selectedhandle == 3:
                setattr(t, 'x3', t.x3 + diffx)
                setattr(t, 'y3', t.y3 + diffy)
            self.lastx = x
            self.lasty = y
            self.Draw()

    def onMouseUp(self, sender, x, y):
        if self.mouseisdown and self.selecteditem:
            t = self.selecteditem
            self.selecteditem.changessuspended = False
            setattr(t, 'x1', t.x1)
            setattr(t, 'y1', t.y1)
            setattr(t, 'x2', t.x2)
            setattr(t, 'y2', t.y2)
            setattr(t, 'x3', t.x3)
            setattr(t, 'y3', t.y3)
        self.mouseisdown = False

    def onMouseEnter(self, sender, x, y):
        pass

    def onMouseLeave(self, sender, x, y):
        pass

    def FindTriangle(self, x, y):
        pt = Point(x, y)
        for t in self.GetTrianglesAsList():
            if PointInTriangle(pt, t):
                return t
        return None