+ xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ Canvas.AddText(String, xy, 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)
+ Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ Canvas.AddScaledText(String, Point, Size = ts, Color = colors[cf], Position = "cc")
+
+ # Arrows
+ N = 5
+ Points = RandomArray.uniform(Range[0], Range[1], (N,2) )
+ for i in range(N):
+ Canvas.AddArrow(Points[i],
+ random.uniform(20,100),
+ Direction = random.uniform(0,360),
+ LineWidth = random.uniform(1,5),
+ LineColor = colors[random.randint(0,len(colors)-1)],
+ ArrowHeadAngle = random.uniform(20,90))
+
+ 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.
+
+ """
+ wx.GetApp().Yield()
+ 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):
+ xy = (random.uniform(Range[0],Range[1]), random.uniform(Range[0],Range[1]))
+ lw = random.randint(1,5)
+ cf = random.randint(0,len(colors)-1)
+ wh = (random.randint(1,5), random.randint(1,5) )
+ Canvas.AddRectangle(xy, wh, LineWidth = lw, FillColor = colors[cf])
+
+ # Ellipses
+ for i in range(3):
+ xy = (random.uniform(Range[0],Range[1]), random.uniform(Range[0],Range[1]))
+ lw = random.randint(1,5)
+ cf = random.randint(0,len(colors)-1)
+ wh = (random.randint(1,5), random.randint(1,5) )
+ Canvas.AddEllipse(xy, wh, LineWidth = lw, FillColor = colors[cf])
+
+ # Circles
+ for i in range(5):
+ xy = (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(xy, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
+ Canvas.AddText("Circle # %i"%(i), xy, 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)
+ xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
+ Canvas.AddScaledText(String, xy, 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.6, 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
+ wx.GetApp().Yield(True)
+ 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 = 200
+ self.TimeStep = 1
+ self.Timer.Start(self.FrameDelay)
+ #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
+
+ def TestHitTest(self,event=None):
+ wx.GetApp().Yield()
+
+ self.UnBindAllMouseEvents()
+ Canvas = self.Canvas
+
+ Canvas.ClearAll()
+ Canvas.SetProjectionFun(None)
+
+ #Add a Hit-able rectangle
+ w, h = 60, 20
+
+ dx = 80
+ dy = 40
+ x, y = 20, 20
+ FontSize = 8
+
+ #Add one that is not HitAble
+ Canvas.AddRectangle((x,y), (w, h), LineWidth = 2)
+ Canvas.AddText("Not Hit-able", (x,y), Size = FontSize, Position = "bl")
+
+
+ x += dx
+ R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2)
+ R.Name = "Line Rectangle"
+ R.HitFill = False
+ R.HitLineWidth = 5 # Makes it a little easier to hit
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
+ Canvas.AddText("Left Click Line", (x,y), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(L.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y+h), Size = FontSize, 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), Size = FontSize, 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), Size = FontSize, Position = "tl")
+
+ x += dx
+ color = "Cyan"
+ Point = (x + w/2, y)
+ #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.AddSquarePoint(Point, Size = 8, Color = color)
+ R.Name = "SquarePoint"
+ R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
+ Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
+ Canvas.AddText(R.Name, (x, y), Size = FontSize, Position = "tl")
+
+