+ Canvas.AddText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
+
+ # Scaled Text
+ String = "Scaled text"
+ for i in range(3):
+ ts = random.random()*3 + 0.2
+ cf = random.randint(0,len(colors)-1)
+ x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
+
+ Canvas.ZoomToBB()
+
+ def TestAnimation(self,event=None):
+ """
+
+ In this test, a relatively complex background is drawn, and
+ a simple object placed in the foreground is moved over
+ it. This demonstrates how to use the InForeground attribute
+ to make an object in the foregorund draw fast, without
+ having to re-draw the whole background.
+
+ """
+ print "Running TestAnimation"
+ Range = (-10,10)
+ self.Range = Range
+
+ self.UnBindAllMouseEvents()
+ Canvas = self.Canvas
+
+ Canvas.ClearAll()
+ Canvas.SetProjectionFun(None)
+
+ ## Random tests of everything:
+ colors = self.colors
+ # Rectangles
+ for i in range(3):
+ x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ lw = random.randint(1,5)
+ cf = random.randint(0,len(colors)-1)
+ h = random.randint(1,5)
+ w = random.randint(1,5)
+ Canvas.AddRectangle(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
+
+ # Ellipses
+ for i in range(3):
+ x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ lw = random.randint(1,5)
+ cf = random.randint(0,len(colors)-1)
+ h = random.randint(1,5)
+ w = random.randint(1,5)
+ Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
+
+ # Circles
+ for i in range(5):
+ x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ D = random.randint(1,5)
+ lw = random.randint(1,5)
+ cf = random.randint(0,len(colors)-1)
+ cl = random.randint(0,len(colors)-1)
+ Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
+ Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackgroundColor = None,Position = "cc")
+
+ # Lines
+ for i in range(5):
+ points = []
+ for j in range(random.randint(2,10)):
+ point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
+ points.append(point)
+ lw = random.randint(1,10)
+ cf = random.randint(0,len(colors)-1)
+ cl = random.randint(0,len(colors)-1)
+ Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
+
+ # Polygons
+ for i in range(3):
+ points = []
+ for j in range(random.randint(2,6)):
+ point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ points.append(point)
+ lw = random.randint(1,6)
+ cf = random.randint(0,len(colors)-1)
+ cl = random.randint(0,len(colors)-1)
+ Canvas.AddPolygon(points,
+ LineWidth = lw,
+ LineColor = colors[cl],
+ FillColor = colors[cf],
+ FillStyle = 'Solid')
+
+ # Scaled Text
+ String = "Scaled text"
+ for i in range(3):
+ ts = random.random()*3 + 0.2
+ cf = random.randint(0,len(colors)-1)
+ x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
+
+
+ # Now the Foreground Object:
+ C = Canvas.AddCircle(0,0,7,LineWidth = 2,LineColor = "Black",FillColor = "Red", InForeground = True)
+ T = Canvas.AddScaledText("Click to Move",0,0, Size = 0.8, Position = 'cc', InForeground = True)
+ C.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.MoveMe)
+ C.Text = T
+
+ self.Timer = wx.PyTimer(self.ShowFrame)
+ self.FrameDelay = 50 # milliseconds
+
+ Canvas.ZoomToBB()
+
+ def ShowFrame(self):
+ Object = self.MovingObject
+ Range = self.Range
+ if self.TimeStep < self.NumTimeSteps:
+ x,y = Object.XY
+ if x > Range[1] or x < Range[0]:
+ self.dx = -self.dx
+ if y > Range[1] or y < Range[0]:
+ self.dy = -self.dy
+ Object.Move( (self.dx,self.dy) )
+ Object.Text.Move( (self.dx,self.dy))
+ self.Canvas.Draw()
+ self.TimeStep += 1
+ else:
+ self.Timer.Stop()
+
+
+ def MoveMe(self, Object):
+ self.MovingObject = Object
+ Range = self.Range
+ self.dx = random.uniform(Range[0]/4,Range[1]/4)
+ self.dy = random.uniform(Range[0]/4,Range[1]/4)
+ #import time
+ #start = time.time()
+ self.NumTimeSteps = 500
+ self.TimeStep = 1
+ self.Timer.Start(self.FrameDelay)
+ #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
+
+ def TestHitTest(self,event=None):
+
+ self.UnBindAllMouseEvents()
+ Canvas = self.Canvas
+
+ Canvas.ClearAll()
+ Canvas.SetProjectionFun(None)
+
+ #Add a HitAble rectangle
+ w, h = 60, 20
+
+ dx = 80
+ dy = 40
+ x,y = 20, 20
+
+ #Add one that is not HitAble
+ Canvas.AddRectangle(x, y, w, h, LineWidth = 2)
+ Canvas.AddText("Not Hit-able", x, y, Position = "bl")
+
+
+ x += dx
+ R = Canvas.AddRectangle(x, y, w, h,LineWidth = 2)
+ R.Name = "Line Rectangle"
+ R.HitFill = False
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
+ Canvas.AddText("Left Click Line", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+
+ x += dx
+ color = "Red"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + "Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
+ Canvas.AddText("Left Click Fill", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ color = "LightBlue"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
+ Canvas.AddText("Right Click Fill", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "Grey"
+ R = Canvas.AddEllipse(x, y, w, h,LineWidth = 2,FillColor = color)
+ R.Name = color +" Ellipse"
+ R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
+ Canvas.AddText("Right Click Fill", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "Brown"
+ R = Canvas.AddCircle(x+dx/2, y+dy/2, dx/4, LineWidth = 2, FillColor = color)
+ R.Name = color + " Circle"
+ R.HitFill = True
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DCLICK, self.RectGotHit)
+ Canvas.AddText("Left D-Click Fill", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ color = "Pink"
+ R = Canvas.AddCircle(x+dx/2, y+dy/2, dx/4, LineWidth = 2,FillColor = color)
+ R.Name = color + " Circle"
+ R.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.RectGotHit)
+ Canvas.AddText("Left Up Fill", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "White"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_MIDDLE_DOWN, self.RectGotHit)
+ Canvas.AddText("Middle Down", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "AQUAMARINE"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_MIDDLE_UP, self.RectGotHit)
+ Canvas.AddText("Middle Up", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ color = "CORAL"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_MIDDLE_DCLICK, self.RectGotHit)
+ Canvas.AddText("Middle DoubleClick", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "CYAN"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_RIGHT_UP, self.RectGotHit)
+ Canvas.AddText("Right Up", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "LIME GREEN"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_RIGHT_DCLICK, self.RectGotHit)
+ Canvas.AddText("Right Double Click", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ color = "MEDIUM GOLDENROD"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color
+ R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
+ Canvas.AddText("L and R Click", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "SALMON"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color + " Rectangle"
+ R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
+ Canvas.AddText("Mouse Enter", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "MEDIUM VIOLET RED"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color
+ R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
+ Canvas.AddText("Mouse Leave", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ color = "SKY BLUE"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
+ R.Name = color
+ R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
+ R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
+ Canvas.AddText("Enter and Leave", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "WHEAT"
+ R = Canvas.AddRectangle(x, y, w+12, h, LineColor = None, FillColor = color)
+ R.Name = color
+ R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
+ R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
+ Canvas.AddText("Mouse Enter&Leave", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "KHAKI"
+ R = Canvas.AddRectangle(x-12, y, w+12, h, LineColor = None, FillColor = color)
+ R.Name = color
+ R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
+ R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
+ Canvas.AddText("Mouse ENter&Leave", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ L = Canvas.AddLine(( (x, y), (x+10, y+10), (x+w, y+h) ), LineWidth = 2, LineColor = "Red")
+ L.Name = "A Line"
+ L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
+ Canvas.AddText("Left Down", x, y, Position = "bl")
+ Canvas.AddText(L.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "SEA GREEN"
+ Points = Numeric.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), Numeric.Float)
+ R = Canvas.AddPolygon(Points, LineWidth = 2, FillColor = color)
+ R.Name = color + " Polygon"
+ R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
+ Canvas.AddText("RIGHT_DOWN", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x += dx
+ color = "Red"
+ Points = Numeric.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), Numeric.Float)
+ R = Canvas.AddPointSet(Points, Diameter = 4, Color = color)
+ R.Name = "PointSet"
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.PointSetGotHit)
+ Canvas.AddText("LEFT_DOWN", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ x = 20
+ y += dy
+ T = Canvas.AddText("Hit-able Text", x, y, Size = 15, Color = "Red", Position = 'tl')
+ T.Name = "Hit-able Text"
+ T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
+ Canvas.AddText("Left Down", x, y, Position = "bl")
+
+ x += dx
+ T = Canvas.AddScaledText("Scaled Text", x, y, Size = 1./2*h, Color = "Pink", Position = 'bl')
+ Canvas.AddPointSet( (x, y), Diameter = 3)
+ T.Name = "Scaled Text"
+ T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
+ Canvas.AddText("Left Down", x, y, Position = "tl")
+
+ self.Canvas.ZoomToBB()
+
+ def TestHitTestForeground(self,event=None):
+ print "Running: TestHitTestForeground"
+
+ self.UnBindAllMouseEvents()
+ Canvas = self.Canvas
+
+ Canvas.ClearAll()
+ Canvas.SetProjectionFun(None)
+
+ #Add a Hitable rectangle
+ w, h = 60, 20
+
+ dx = 80
+ dy = 40
+ x,y = 20, 20
+
+ color = "Red"
+ R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color, InForeground = False)
+ R.Name = color + "Rectangle"
+ R.HitFill = True
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
+ Canvas.AddText("Left Click Fill", x, y, Position = "bl")
+ Canvas.AddText(R.Name, x, y+h, Position = "tl")
+
+ ## A set of Rectangles that move together
+
+ ## NOTE: In a real app, it might be better to create a new
+ ## custom FloatCanvas DrawObject
+
+ self.MovingRects = []
+ x += dx
+ color = "LightBlue"
+ R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
+ R.HitFill = True
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveLeft)
+ L = Canvas.AddText("Left", x + w/4, y + h/4, Position = "cc", InForeground = True)
+ self.MovingRects.extend( (R,L) )
+
+ x += w/2
+ R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
+ R.HitFill = True
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveRight)
+ L = Canvas.AddText("Right", x + w/4, y + h/4, Position = "cc", InForeground = True)
+ self.MovingRects.extend( (R,L) )
+
+ x -= w/2
+ y += h/2
+ R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
+ R.HitFill = True
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveUp)
+ L = Canvas.AddText("Up", x + w/4, y + h/4, Position = "cc", InForeground = True)
+ self.MovingRects.extend( (R,L) )
+
+
+ x += w/2
+ R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
+ R.HitFill = True
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveDown)
+ L = Canvas.AddText("Down", x + w/4, y + h/4, Position = "cc", InForeground = True)
+ self.MovingRects.extend( (R,L) )
+
+ self.Canvas.ZoomToBB()
+
+ def RectMoveLeft(self,Object):
+ self.MoveRects("left")
+
+ def RectMoveRight(self,Object):
+ self.MoveRects("right")
+
+ def RectMoveUp(self,Object):
+ self.MoveRects("up")
+
+ def RectMoveDown(self,Object):
+ self.MoveRects("down")
+
+ def MoveRects(self, Dir):
+ for Object in self.MovingRects:
+ X,Y = Object.XY
+ if Dir == "left": X -= 10
+ elif Dir == "right": X += 10
+ elif Dir == "up": Y += 10
+ elif Dir == "down": Y -= 10
+ Object.SetXY(X,Y)
+ self.Canvas.Draw()