]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
aeec720a8bcf94ad821cec92ad938d32dd3c09ea
1 #!/usr/bin/env python2.3
8 import numarray
as Numeric
9 import numarray
.random_array
as RandomArray
16 The FloatCanvas requires either the Numeric or Numarray module:
18 http://sourceforge.net/projects/numpy
20 NOTE: The Numeric module is substantially faster than numarray for this
21 purpose, if you have lot's of objects
24 def runTest(frame
, nb
, log
):
25 dlg
= wx
.MessageDialog(frame
, errorText
, 'Sorry', wx
.OK |
34 if __name__
== "__main__": # parse options if run stand-alone
37 optlist
, args
= getopt
.getopt(sys
.argv
[1:],'l',["local","all","text","map","stext","hit","hitf","animate","speed","temp","props"])
42 elif opt
[0] == "--text":
44 elif opt
[0] == "--map":
46 elif opt
[0] == "--stext":
48 elif opt
[0] == "--hit":
50 elif opt
[0] == "--hitf":
52 elif opt
[0] == "--animate":
53 StartUpDemo
= "animate"
54 elif opt
[0] == "--speed":
56 elif opt
[0] == "--temp":
58 elif opt
[0] == "--props":
63 #---------------------------------------------------------------------------
65 class TestPanel(wx
.Panel
):
66 def __init__(self
, parent
, log
):
68 wx
.Panel
.__init
__(self
, parent
, -1)
70 b
= wx
.Button(self
, -1, "Show the FloatBar sample", (50,50))
71 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
74 def OnButton(self
, evt
):
75 win
= DrawFrame(None, -1, "FloatCanvas Drawing Window",wx
.DefaultPosition
,(500,500))
80 def runTest(frame
, nb
, log
):
81 win
= TestPanel(nb
, log
)
88 from floatcanvas
import NavCanvas
, FloatCanvas
89 except ImportError: # if it's not there locally, try the wxPython lib.
90 from wx
.lib
.floatcanvas
import NavCanvas
, FloatCanvas
92 import wxPython
.lib
.colourdb
94 class DrawFrame(wx
.Frame
):
97 A frame used for the FloatCanvas Demo
102 def __init__(self
,parent
, id,title
,position
,size
):
103 wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
)
105 ## Set up the MenuBar
106 MenuBar
= wx
.MenuBar()
108 file_menu
= wx
.Menu()
109 item
= file_menu
.Append(-1, "&Close","Close this frame")
110 self
.Bind(wx
.EVT_MENU
, self
.OnQuit
, item
)
111 MenuBar
.Append(file_menu
, "&File")
113 draw_menu
= wx
.Menu()
115 item
= draw_menu
.Append(-1, "&Draw Test","Run a test of drawing random components")
116 self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, item
)
118 item
= draw_menu
.Append(-1, "&Line Test","Run a test of drawing random lines")
119 self
.Bind(wx
.EVT_MENU
, self
.LineTest
, item
)
121 item
= draw_menu
.Append(-1, "Draw &Map","Run a test of drawing a map")
122 self
.Bind(wx
.EVT_MENU
, self
.DrawMap
, item
)
123 item
= draw_menu
.Append(-1, "&Text Test","Run a test of text drawing")
124 self
.Bind(wx
.EVT_MENU
, self
.TestText
, item
)
125 item
= draw_menu
.Append(-1, "&ScaledText Test","Run a test of text drawing")
126 self
.Bind(wx
.EVT_MENU
, self
.TestScaledText
, item
)
127 item
= draw_menu
.Append(-1, "&Clear","Clear the Canvas")
128 self
.Bind(wx
.EVT_MENU
, self
.Clear
, item
)
129 item
= draw_menu
.Append(-1, "&Hit Test","Run a test of the hit test code")
130 self
.Bind(wx
.EVT_MENU
, self
.TestHitTest
, item
)
131 item
= draw_menu
.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
132 self
.Bind(wx
.EVT_MENU
, self
.TestHitTestForeground
, item
)
133 item
= draw_menu
.Append(-1, "&Animation","Run a test of Animation")
134 self
.Bind(wx
.EVT_MENU
, self
.TestAnimation
, item
)
135 item
= draw_menu
.Append(-1, "&Speed","Run a test of Drawing Speed")
136 self
.Bind(wx
.EVT_MENU
, self
.SpeedTest
, item
)
137 item
= draw_menu
.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
138 self
.Bind(wx
.EVT_MENU
, self
.PropertiesChangeTest
, item
)
139 MenuBar
.Append(draw_menu
, "&Tests")
141 view_menu
= wx
.Menu()
142 item
= view_menu
.Append(-1, "Zoom to &Fit","Zoom to fit the window")
143 self
.Bind(wx
.EVT_MENU
, self
.ZoomToFit
, item
)
144 MenuBar
.Append(view_menu
, "&View")
146 help_menu
= wx
.Menu()
147 item
= help_menu
.Append(-1, "&About",
148 "More information About this program")
149 self
.Bind(wx
.EVT_MENU
, self
.OnAbout
, item
)
150 MenuBar
.Append(help_menu
, "&Help")
152 self
.SetMenuBar(MenuBar
)
154 self
.CreateStatusBar()
156 self
.Canvas
= NavCanvas
.NavCanvas(self
,
160 BackgroundColor
= "DARK SLATE BLUE")
162 wx
.EVT_CLOSE(self
, self
.OnCloseWindow
)
164 FloatCanvas
.EVT_MOTION(self
.Canvas
, self
.OnMove
)
165 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
167 self
.EventsAreBound
= False
169 ## getting all the colors and linestyles for random objects
170 wxPython
.lib
.colourdb
.updateColourDB()
171 self
.colors
= wxPython
.lib
.colourdb
.getColourList()
172 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
177 def BindAllMouseEvents(self
):
178 if not self
.EventsAreBound
:
179 ## Here is how you catch FloatCanvas mouse events
180 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, self
.OnLeftDown
)
181 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, self
.OnLeftUp
)
182 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, self
.OnLeftDouble
)
184 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, self
.OnMiddleDown
)
185 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, self
.OnMiddleUp
)
186 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, self
.OnMiddleDouble
)
188 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, self
.OnRightDown
)
189 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, self
.OnRightUp
)
190 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, self
.OnRightDouble
)
192 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, self
.OnWheel
)
193 self
.EventsAreBound
= True
195 def UnBindAllMouseEvents(self
):
196 ## Here is how you catch FloatCanvas mouse events
197 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, None )
198 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, None )
199 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, None)
201 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
202 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
203 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
205 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
206 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
207 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
209 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, None )
211 self
.EventsAreBound
= False
213 def PrintCoords(self
,event
):
214 print "coords are: %s"%(event
.Coords
,)
215 print "pixel coords are: %s\n"%(event
.GetPosition(),)
217 def OnLeftDown(self
, event
):
218 print "Left Button has been clicked in DrawFrame"
219 self
.PrintCoords(event
)
221 def OnLeftUp(self
, event
):
222 print "Left up in DrawFrame"
223 self
.PrintCoords(event
)
225 def OnLeftDouble(self
, event
):
226 print "Left Double Click in DrawFrame"
227 self
.PrintCoords(event
)
229 def OnMiddleDown(self
, event
):
230 print "Middle Button clicked in DrawFrame"
231 self
.PrintCoords(event
)
233 def OnMiddleUp(self
, event
):
234 print "Middle Button Up in DrawFrame"
235 self
.PrintCoords(event
)
237 def OnMiddleDouble(self
, event
):
238 print "Middle Button Double clicked in DrawFrame"
239 self
.PrintCoords(event
)
241 def OnRightDown(self
, event
):
242 print "Right Button has been clicked in DrawFrame"
243 self
.PrintCoords(event
)
245 def OnRightUp(self
, event
):
246 print "Right Button Up in DrawFrame"
247 self
.PrintCoords(event
)
249 def OnRightDouble(self
, event
):
250 print "Right Button Double clicked in DrawFrame"
251 self
.PrintCoords(event
)
253 def OnWheel(self
, event
):
254 print "Mouse Wheel Moved in DrawFrame"
255 self
.PrintCoords(event
)
257 def OnMove(self
, event
):
259 Updates the staus bar with the world coordinates
261 self
.SetStatusText("%.2f, %.2f"%tuple(event
.Coords
))
263 def OnAbout(self
, event
):
264 print "OnAbout called"
266 dlg
= wx
.MessageDialog(self
, "This is a small program to demonstrate\n"
267 "the use of the FloatCanvas\n",
268 "About Me", wx
.OK | wx
.ICON_INFORMATION
)
272 def ZoomToFit(self
,event
):
273 self
.Canvas
.ZoomToBB()
275 def Clear(self
,event
= None):
276 self
.UnBindAllMouseEvents()
277 self
.Canvas
.ClearAll()
278 self
.Canvas
.SetProjectionFun(None)
281 def OnQuit(self
,event
):
284 def OnCloseWindow(self
, event
):
287 def DrawTest(self
,event
=None):
294 self
.BindAllMouseEvents()
298 Canvas
.SetProjectionFun(None)
300 ## Random tests of everything:
304 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
305 lw
= random
.randint(1,5)
306 cf
= random
.randint(0,len(colors
)-1)
307 h
= random
.randint(1,5)
308 w
= random
.randint(1,5)
309 Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
313 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
314 lw
= random
.randint(1,5)
315 cf
= random
.randint(0,len(colors
)-1)
316 h
= random
.randint(1,5)
317 w
= random
.randint(1,5)
318 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
322 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
323 D
= random
.randint(1,50)
324 cf
= random
.randint(0,len(colors
)-1)
325 Canvas
.AddPoint((x
,y
), Color
= colors
[cf
], Diameter
= D
)
329 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
330 D
= random
.randint(1,5)
331 lw
= random
.randint(1,5)
332 cf
= random
.randint(0,len(colors
)-1)
333 cl
= random
.randint(0,len(colors
)-1)
334 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
335 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
340 for j
in range(random
.randint(2,10)):
341 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
343 lw
= random
.randint(1,10)
344 cf
= random
.randint(0,len(colors
)-1)
345 cl
= random
.randint(0,len(colors
)-1)
346 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
351 for j
in range(random
.randint(2,6)):
352 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
354 lw
= random
.randint(1,6)
355 cf
= random
.randint(0,len(colors
)-1)
356 cl
= random
.randint(0,len(colors
)-1)
357 Canvas
.AddPolygon(points
,
359 LineColor
= colors
[cl
],
360 FillColor
= colors
[cf
],
366 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
367 cf
= random
.randint(0,len(colors
)-1)
368 D
= random
.randint(1,4)
369 Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
372 String
= "Unscaled text"
374 ts
= random
.randint(10,40)
375 cf
= random
.randint(0,len(colors
)-1)
376 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
377 Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
380 String
= "Scaled text"
382 ts
= random
.random()*3 + 0.2
383 cf
= random
.randint(0,len(colors
)-1)
384 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
385 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
389 def TestAnimation(self
,event
=None):
392 In this test, a relatively complex background is drawn, and
393 a simple object placed in the foreground is moved over
394 it. This demonstrates how to use the InForeground attribute
395 to make an object in the foregorund draw fast, without
396 having to re-draw the whole background.
403 self
.UnBindAllMouseEvents()
407 Canvas
.SetProjectionFun(None)
409 ## Random tests of everything:
413 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
414 lw
= random
.randint(1,5)
415 cf
= random
.randint(0,len(colors
)-1)
416 h
= random
.randint(1,5)
417 w
= random
.randint(1,5)
418 Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
422 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
423 lw
= random
.randint(1,5)
424 cf
= random
.randint(0,len(colors
)-1)
425 h
= random
.randint(1,5)
426 w
= random
.randint(1,5)
427 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
431 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
432 D
= random
.randint(1,5)
433 lw
= random
.randint(1,5)
434 cf
= random
.randint(0,len(colors
)-1)
435 cl
= random
.randint(0,len(colors
)-1)
436 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
437 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
442 for j
in range(random
.randint(2,10)):
443 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
445 lw
= random
.randint(1,10)
446 cf
= random
.randint(0,len(colors
)-1)
447 cl
= random
.randint(0,len(colors
)-1)
448 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
453 for j
in range(random
.randint(2,6)):
454 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
456 lw
= random
.randint(1,6)
457 cf
= random
.randint(0,len(colors
)-1)
458 cl
= random
.randint(0,len(colors
)-1)
459 Canvas
.AddPolygon(points
,
461 LineColor
= colors
[cl
],
462 FillColor
= colors
[cf
],
466 String
= "Scaled text"
468 ts
= random
.random()*3 + 0.2
469 cf
= random
.randint(0,len(colors
)-1)
470 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
471 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
474 # Now the Foreground Object:
475 C
= Canvas
.AddCircle(0,0,7,LineWidth
= 2,LineColor
= "Black",FillColor
= "Red", InForeground
= True)
476 T
= Canvas
.AddScaledText("Click to Move",0,0, Size
= 0.6, Position
= 'cc', InForeground
= True)
477 C
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.MoveMe
)
480 self
.Timer
= wx
.PyTimer(self
.ShowFrame
)
481 self
.FrameDelay
= 50 # milliseconds
486 Object
= self
.MovingObject
488 if self
.TimeStep
< self
.NumTimeSteps
:
490 if x
> Range
[1] or x
< Range
[0]:
492 if y
> Range
[1] or y
< Range
[0]:
494 Object
.Move( (self
.dx
,self
.dy
) )
495 Object
.Text
.Move( (self
.dx
,self
.dy
))
498 wx
.GetApp().Yield(True)
503 def MoveMe(self
, Object
):
504 self
.MovingObject
= Object
506 self
.dx
= random
.uniform(Range
[0]/4,Range
[1]/4)
507 self
.dy
= random
.uniform(Range
[0]/4,Range
[1]/4)
510 self
.NumTimeSteps
= 200
512 self
.Timer
.Start(self
.FrameDelay
)
513 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
515 def TestHitTest(self
,event
=None):
518 self
.UnBindAllMouseEvents()
522 Canvas
.SetProjectionFun(None)
524 #Add a HitAble rectangle
532 #Add one that is not HitAble
533 Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
534 Canvas
.AddText("Not Hit-able", x
, y
, Size
= FontSize
, Position
= "bl")
538 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
539 R
.Name
= "Line Rectangle"
541 R
.HitLineWidth
= 5 # Makes it a little easier to hit
542 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
543 Canvas
.AddText("Left Click Line", x
, y
, Size
= FontSize
, Position
= "bl")
544 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
548 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
549 R
.Name
= color
+ "Rectangle"
550 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
551 Canvas
.AddText("Left Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
552 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
557 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
558 R
.Name
= color
+ " Rectangle"
559 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
560 Canvas
.AddText("Right Click Fill", x
, y
, Position
= "bl")
561 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
565 R
= Canvas
.AddEllipse(x
, y
, w
, h
,LineWidth
= 2,FillColor
= color
)
566 R
.Name
= color
+" Ellipse"
567 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
568 Canvas
.AddText("Right Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
569 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
573 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2, FillColor
= color
)
574 R
.Name
= color
+ " Circle"
576 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DCLICK
, self
.RectGotHit
)
577 Canvas
.AddText("Left D-Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
578 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
583 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2,FillColor
= color
)
584 R
.Name
= color
+ " Circle"
585 R
.Bind(FloatCanvas
.EVT_FC_LEFT_UP
, self
.RectGotHit
)
586 Canvas
.AddText("Left Up Fill", x
, y
, Size
= FontSize
, Position
= "bl")
587 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
591 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
592 R
.Name
= color
+ " Rectangle"
593 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DOWN
, self
.RectGotHit
)
594 Canvas
.AddText("Middle Down", x
, y
, Size
= FontSize
, Position
= "bl")
595 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
599 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
600 R
.Name
= color
+ " Rectangle"
601 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_UP
, self
.RectGotHit
)
602 Canvas
.AddText("Middle Up", x
, y
, Size
= FontSize
, Position
= "bl")
603 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
608 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
609 R
.Name
= color
+ " Rectangle"
610 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DCLICK
, self
.RectGotHit
)
611 Canvas
.AddText("Middle DoubleClick", x
, y
, Size
= FontSize
, Position
= "bl")
612 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
616 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
617 R
.Name
= color
+ " Rectangle"
618 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_UP
, self
.RectGotHit
)
619 Canvas
.AddText("Right Up", x
, y
, Size
= FontSize
, Position
= "bl")
620 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
624 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
625 R
.Name
= color
+ " Rectangle"
626 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DCLICK
, self
.RectGotHit
)
627 Canvas
.AddText("Right Double Click", x
, y
, Size
= FontSize
, Position
= "bl")
628 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
632 color
= "MEDIUM GOLDENROD"
633 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
635 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
636 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
637 Canvas
.AddText("L and R Click", x
, y
, Size
= FontSize
, Position
= "bl")
638 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
642 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
643 R
.Name
= color
+ " Rectangle"
644 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
645 Canvas
.AddText("Mouse Enter", x
, y
, Size
= FontSize
, Position
= "bl")
646 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
649 color
= "MEDIUM VIOLET RED"
650 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
652 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
653 Canvas
.AddText("Mouse Leave", x
, y
, Size
= FontSize
, Position
= "bl")
654 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
659 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
661 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
662 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
663 Canvas
.AddText("Enter and Leave", x
, y
, Size
= FontSize
, Position
= "bl")
664 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
668 R
= Canvas
.AddRectangle(x
, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
670 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
671 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
672 Canvas
.AddText("Mouse Enter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
673 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
677 R
= Canvas
.AddRectangle(x
-12, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
679 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
680 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
681 Canvas
.AddText("Mouse ENter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
682 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
686 L
= Canvas
.AddLine(( (x
, y
), (x
+10, y
+10), (x
+w
, y
+h
) ), LineWidth
= 2, LineColor
= "Red")
688 L
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
689 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
690 Canvas
.AddText(L
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
694 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
)
695 R
= Canvas
.AddPolygon(Points
, LineWidth
= 2, FillColor
= color
)
696 R
.Name
= color
+ " Polygon"
697 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
698 Canvas
.AddText("RIGHT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
699 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
703 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
)
704 R
= Canvas
.AddPointSet(Points
, Diameter
= 4, Color
= color
)
706 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.PointSetGotHit
)
707 Canvas
.AddText("LEFT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
708 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
712 T
= Canvas
.AddText("Hit-able Text", x
, y
, Size
= 15, Color
= "Red", Position
= 'tl')
713 T
.Name
= "Hit-able Text"
714 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
715 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
718 T
= Canvas
.AddScaledText("Scaled Text", x
, y
, Size
= 1./2*h
, Color
= "Pink", Position
= 'bl')
719 Canvas
.AddPointSet( (x
, y
), Diameter
= 3)
720 T
.Name
= "Scaled Text"
721 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
722 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "tl")
724 self
.Canvas
.ZoomToBB()
726 def TestHitTestForeground(self
,event
=None):
729 self
.UnBindAllMouseEvents()
733 Canvas
.SetProjectionFun(None)
735 #Add a Hitable rectangle
743 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
, InForeground
= False)
744 R
.Name
= color
+ "Rectangle"
746 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
747 Canvas
.AddText("Left Click Fill", x
, y
, Position
= "bl")
748 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
750 ## A set of Rectangles that move together
752 ## NOTE: In a real app, it might be better to create a new
753 ## custom FloatCanvas DrawObject
755 self
.MovingRects
= []
758 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
760 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveLeft
)
761 L
= Canvas
.AddText("Left", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
762 self
.MovingRects
.extend( (R
,L
) )
765 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
767 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveRight
)
768 L
= Canvas
.AddText("Right", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
769 self
.MovingRects
.extend( (R
,L
) )
773 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
775 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveUp
)
776 L
= Canvas
.AddText("Up", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
777 self
.MovingRects
.extend( (R
,L
) )
781 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
783 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveDown
)
784 L
= Canvas
.AddText("Down", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
785 self
.MovingRects
.extend( (R
,L
) )
787 self
.Canvas
.ZoomToBB()
789 def RectMoveLeft(self
,Object
):
790 self
.MoveRects("left")
792 def RectMoveRight(self
,Object
):
793 self
.MoveRects("right")
795 def RectMoveUp(self
,Object
):
798 def RectMoveDown(self
,Object
):
799 self
.MoveRects("down")
801 def MoveRects(self
, Dir
):
802 for Object
in self
.MovingRects
:
804 if Dir
== "left": X
-= 10
805 elif Dir
== "right": X
+= 10
806 elif Dir
== "up": Y
+= 10
807 elif Dir
== "down": Y
-= 10
812 def PointSetGotHit(self
, Object
):
813 print Object
.Name
, "Got Hit\n"
815 def RectGotHit(self
, Object
):
816 print Object
.Name
, "Got Hit\n"
818 def RectGotHitRight(self
, Object
):
819 print Object
.Name
, "Got Hit With Right\n"
821 def RectGotHitLeft(self
, Object
):
822 print Object
.Name
, "Got Hit with Left\n"
824 def RectMouseOver(self
, Object
):
825 print "Mouse entered:", Object
.Name
827 def RectMouseLeave(self
, Object
):
828 print "Mouse left ", Object
.Name
831 def TestText(self
, event
= None):
834 self
.BindAllMouseEvents()
837 Canvas
.SetProjectionFun(None)
841 ## Add a non-visible rectangle, just to get a Bounding Box
842 ## Text objects have a zero-size bounding box, because it changes with zoom
843 Canvas
.AddRectangle(-10,-10,20,20,LineWidth
= 1, LineColor
= None)
847 self
.Canvas
.AddText("Top Left",x
,y
,Size
= 14,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
848 self
.Canvas
.AddText("Bottom Left",x
,y
,Size
= 14,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
849 self
.Canvas
.AddText("Top Right",x
,y
,Size
= 14,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
850 self
.Canvas
.AddText("Bottom Right",x
,y
,Size
= 14,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
851 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
855 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
856 self
.Canvas
.AddText("Top Center",x
,y
,Size
= 14,Color
= "Black",Position
= "tc")
857 self
.Canvas
.AddText("Bottom Center",x
,y
,Size
= 14,Color
= "White",Position
= "bc")
861 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
862 self
.Canvas
.AddText("Center Right",x
,y
,Size
= 14,Color
= "Black",Position
= "cr")
863 self
.Canvas
.AddText("Center Left",x
,y
,Size
= 14,Color
= "Black",Position
= "cl")
867 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
868 self
.Canvas
.AddText("Center Center",x
,y
,Size
= 14,Color
= "Black",Position
= "cc")
870 self
.Canvas
.AddText("40 Pixels",-10,8,Size
= 40)
871 self
.Canvas
.AddText("20 Pixels",-10,5,Size
= 20)
872 self
.Canvas
.AddText("10 Pixels",-10,3,Size
= 10)
874 self
.Canvas
.AddText("MODERN Font", -10, 0, Family
= wx
.MODERN
)
875 self
.Canvas
.AddText("DECORATIVE Font", -10, -1, Family
= wx
.DECORATIVE
)
876 self
.Canvas
.AddText("ROMAN Font", -10, -2, Family
= wx
.ROMAN
)
877 self
.Canvas
.AddText("SCRIPT Font", -10, -3, Family
= wx
.SCRIPT
)
878 self
.Canvas
.AddText("ROMAN BOLD Font", -10, -4, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
879 self
.Canvas
.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
881 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
882 Font
= wx
.Font(20, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
883 self
.Canvas
.AddText("zapf chancery Font", -10, -6, Font
= Font
)
885 self
.Canvas
.ZoomToBB()
887 def TestScaledText(self
, event
= None):
890 self
.BindAllMouseEvents()
893 Canvas
.SetProjectionFun(None)
897 T
= Canvas
.AddScaledText("Top Left",x
,y
,Size
= 5,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
898 T
= Canvas
.AddScaledText("Bottom Left",x
,y
,Size
= 5,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
899 T
= Canvas
.AddScaledText("Top Right",x
,y
,Size
= 5,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
900 T
= Canvas
.AddScaledText("Bottom Right",x
,y
,Size
= 5,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
901 Canvas
.AddPointSet((x
,y
), Color
= "Red", Diameter
= 4)
906 Canvas
.AddScaledText("Top Center",x
,y
,Size
= 7,Color
= "Black",Position
= "tc")
907 Canvas
.AddScaledText("Bottom Center",x
,y
,Size
= 7,Color
= "White",Position
= "bc")
908 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
912 Canvas
.AddScaledText("Center Right",x
,y
,Size
= 9,Color
= "Black",Position
= "cr")
913 Canvas
.AddScaledText("Center Left",x
,y
,Size
= 9,Color
= "Black",Position
= "cl")
914 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
918 self
.Canvas
.AddScaledText("MODERN Font", x
, 0, Size
= 7, Family
= wx
.MODERN
, Color
= (0,0,0))
919 self
.Canvas
.AddScaledText("DECORATIVE Font", x
, -10, Size
= 7, Family
= wx
.DECORATIVE
, Color
= (0,0,1))
920 self
.Canvas
.AddScaledText("ROMAN Font", x
, -20, Size
= 7, Family
= wx
.ROMAN
)
921 self
.Canvas
.AddScaledText("SCRIPT Font", x
, -30, Size
= 7, Family
= wx
.SCRIPT
)
922 self
.Canvas
.AddScaledText("ROMAN BOLD Font", x
, -40, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
923 self
.Canvas
.AddScaledText("ROMAN ITALIC BOLD Font", x
, -50, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
924 Canvas
.AddPointSet((x
,0), Color
= "White", Diameter
= 4)
927 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
929 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
930 T
= self
.Canvas
.AddScaledText("zapf chancery Font", x
, y
, Size
= 20, Font
= Font
, Position
= 'bc')
933 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "bookman")
934 T
= self
.Canvas
.AddScaledText("Bookman Font", x
, y
, Size
= 8, Font
= Font
)
936 self
.Canvas
.ZoomToBB()
938 def DrawMap(self
,event
= None):
941 self
.BindAllMouseEvents()
943 ## Test of Actual Map Data
944 self
.Canvas
.ClearAll()
945 self
.Canvas
.SetProjectionFun("FlatEarth")
946 #start = time.clock()
947 Shorelines
= Read_MapGen(os
.path
.join("data",'world.dat'),stats
= 0)
948 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
949 #start = time.clock()
950 for segment
in Shorelines
:
951 self
.Canvas
.AddLine(segment
)
952 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
953 #start = time.clock()
954 self
.Canvas
.ZoomToBB()
955 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
958 def LineTest(self
,event
= None):
964 ## Test of drawing lots of lines
967 Canvas
.SetProjectionFun(None)
968 #start = time.clock()
972 for i
in range(2000):
973 points
= (random
.randint(Range
[0],Range
[1]),
974 random
.randint(Range
[0],Range
[1]),
975 random
.randint(Range
[0],Range
[1]),
976 random
.randint(Range
[0],Range
[1]))
977 linepoints
.append(points
)
978 linewidths
.append(random
.randint(1,10) )
979 linecolors
.append(random
.randint(0,len(colors
)-1) )
980 for (points
,color
,width
) in zip(linepoints
,linecolors
,linewidths
):
981 Canvas
.AddLine((points
[0:2],points
[2:4]), LineWidth
= width
, LineColor
= colors
[color
])
982 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
983 #start = time.clock()
985 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
987 def SpeedTest(self
,event
=None):
989 BigRange
= (-1000,1000)
992 self
.UnBindAllMouseEvents()
996 Canvas
.SetProjectionFun(None)
1000 for i
in range(1000):
1001 x
,y
= (random
.uniform(BigRange
[0],BigRange
[1]),random
.uniform(BigRange
[0],BigRange
[1]))
1002 coords
.append( (x
,y
) )
1003 print "Drawing the Points"
1004 start
= time
.clock()
1005 for Point
in coords
:
1006 Canvas
.AddPoint(Point
, Diameter
= 4)
1007 print "It took %s seconds to add the points"%(time
.clock() - start
)
1010 def PropertiesChangeTest(self
,event
=None):
1014 colors
= self
.colors
1016 self
.UnBindAllMouseEvents()
1017 Canvas
= self
.Canvas
1020 Canvas
.SetProjectionFun(None)
1022 self
.ColorObjectsAll
= []
1023 self
.ColorObjectsLine
= []
1024 self
.ColorObjectsColor
= []
1025 self
.ColorObjectsText
= []
1026 ##One of each object:
1028 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1029 lw
= random
.randint(1,5)
1030 cf
= random
.randint(0,len(colors
)-1)
1031 h
= random
.randint(1,5)
1032 w
= random
.randint(1,5)
1033 self
.Rectangle
= Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
1034 self
.ColorObjectsAll
.append(self
.Rectangle
)
1037 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1038 lw
= random
.randint(1,5)
1039 cf
= random
.randint(0,len(colors
)-1)
1040 h
= random
.randint(1,5)
1041 w
= random
.randint(1,5)
1042 self
.Ellipse
= Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
1043 self
.ColorObjectsAll
.append(self
.Ellipse
)
1046 xy
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1047 D
= random
.randint(1,50)
1048 lw
= random
.randint(1,5)
1049 cf
= random
.randint(0,len(colors
)-1)
1050 cl
= random
.randint(0,len(colors
)-1)
1051 self
.ColorObjectsColor
.append(Canvas
.AddPoint(xy
, colors
[cf
], D
))
1054 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1055 D
= random
.randint(1,5)
1056 lw
= random
.randint(1,5)
1057 cf
= random
.randint(0,len(colors
)-1)
1058 cl
= random
.randint(0,len(colors
)-1)
1059 self
.Circle
= Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
1060 self
.ColorObjectsAll
.append(self
.Circle
)
1064 for j
in range(random
.randint(2,10)):
1065 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
1066 points
.append(point
)
1067 lw
= random
.randint(1,10)
1068 cf
= random
.randint(0,len(colors
)-1)
1069 cl
= random
.randint(0,len(colors
)-1)
1070 self
.ColorObjectsLine
.append(Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
]))
1074 ## for j in range(random.randint(2,6)):
1075 ## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1076 ## points.append(point)
1077 points
= RandomArray
.uniform(Range
[0],Range
[1],(6,2))
1078 lw
= random
.randint(1,6)
1079 cf
= random
.randint(0,len(colors
)-1)
1080 cl
= random
.randint(0,len(colors
)-1)
1081 self
.ColorObjectsAll
.append(Canvas
.AddPolygon(points
,
1083 LineColor
= colors
[cl
],
1084 FillColor
= colors
[cf
],
1085 FillStyle
= 'Solid'))
1088 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
1089 cf
= random
.randint(0,len(colors
)-1)
1090 D
= random
.randint(1,4)
1091 self
.PointSet
= Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
1092 self
.ColorObjectsColor
.append(self
.PointSet
)
1095 point
= RandomArray
.uniform(Range
[0],Range
[1],(2,))
1096 cf
= random
.randint(0,len(colors
)-1)
1097 D
= random
.randint(1,4)
1098 self
.Point
= Canvas
.AddPoint(point
, Color
= colors
[cf
], Diameter
= D
)
1099 self
.ColorObjectsColor
.append(self
.Point
)
1102 String
= "Unscaled text"
1103 ts
= random
.randint(10,40)
1104 cf
= random
.randint(0,len(colors
)-1)
1105 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1106 self
.ColorObjectsText
.append(Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1109 String
= "Scaled text"
1110 ts
= random
.random()*3 + 0.2
1111 cf
= random
.randint(0,len(colors
)-1)
1112 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1113 self
.ColorObjectsText
.append(Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1116 Button
= Canvas
.AddRectangle(-10, -12, 20, 3, LineStyle
= None, FillColor
= "Red")
1117 Canvas
.AddScaledText("Click Here To Change Properties",
1123 Button
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.ChangeProperties
)
1127 def ChangeProperties(self
, Object
= None):
1128 colors
= self
.colors
1131 for Object
in self
.ColorObjectsAll
:
1133 Object
.SetFillColor(colors
[random
.randint(0,len(colors
)-1)])
1134 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1135 Object
.SetLineWidth(random
.randint(1,7))
1136 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1137 for Object
in self
.ColorObjectsLine
:
1138 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1139 Object
.SetLineWidth(random
.randint(1,7))
1140 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1141 for Object
in self
.ColorObjectsColor
:
1142 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1143 for Object
in self
.ColorObjectsText
:
1144 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1145 Object
.SetBackgroundColor(colors
[random
.randint(0,len(colors
)-1)])
1146 self
.Circle
.SetDiameter(random
.randint(1,10))
1147 self
.PointSet
.SetDiameter(random
.randint(1,8))
1148 self
.Point
.SetDiameter(random
.randint(1,8))
1149 for Object
in (self
.Rectangle
, self
.Ellipse
):
1150 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1151 w
,h
= random
.randint(1,5), random
.randint(1,5)
1152 Object
.SetShape(x
,y
,w
,h
)
1154 self
.Canvas
.Draw(Force
= True)
1156 def TempTest(self
, event
= None):
1159 self
.UnBindAllMouseEvents()
1160 Canvas
= self
.Canvas
1162 Canvas
.SetProjectionFun(None)
1166 # Create a random Polygon
1169 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1170 points
.append(point
)
1171 Poly
= Canvas
.AddPolygon(points
,
1173 LineColor
= "Black",
1174 FillColor
= "LightBlue",
1175 FillStyle
= 'Solid')
1177 Poly
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPoly
)
1179 self
.SelectedPoly
= None
1180 self
.SelectPoints
= []
1181 self
.SelectedPoint
= None
1185 def SelectPoly(self
, Object
):
1186 print "In SelectPoly"
1187 Canvas
= self
.Canvas
1188 if Object
is self
.SelectedPoly
:
1191 #fixme: Do something to unselect the old one
1192 self
.SelectedPoly
= Object
1193 Canvas
.RemoveObjects(self
.SelectPoints
)
1194 self
.SelectPoints
= []
1195 # Draw points on the Vertices of the Selected Poly:
1196 for i
, point
in enumerate(Object
.Points
):
1197 P
= Canvas
.AddPointSet(point
, Diameter
= 6, Color
= "Red")
1199 P
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPointHit
)
1200 self
.SelectPoints
.append(P
)
1204 def SelectPointHit(self
, Point
):
1205 print "Point Num: %i Hit"%Point
.VerticeNum
1206 self
.SelectedPoint
= Point
1210 class DemoApp(wx
.App
):
1214 Under the Draw menu, there are three options:
1216 *Draw Test: will put up a picture of a bunch of randomly generated
1217 objects, of each kind supported.
1219 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1220 with a lot of data, and will take a while to load and draw (about 10 sec
1221 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1222 performance is not very good for large drawings.
1224 *Clear: Clears the Canvas.
1226 Once you have a picture drawn, you can zoom in and out and move about
1227 the picture. There is a tool bar with three tools that can be
1230 The magnifying glass with the plus is the zoom in tool. Once selected,
1231 if you click the image, it will zoom in, centered on where you
1232 clicked. If you click and drag the mouse, you will get a rubber band
1233 box, and the image will zoom to fit that box when you release it.
1235 The magnifying glass with the minus is the zoom out tool. Once selected,
1236 if you click the image, it will zoom out, centered on where you
1237 clicked. (note that this takes a while when you are looking at the map,
1238 as it has a LOT of lines to be drawn. The image is double buffered, so
1239 you don't see the drawing in progress)
1241 The hand is the move tool. Once selected, if you click and drag on the
1242 image, it will move so that the part you clicked on ends up where you
1243 release the mouse. Nothing is changed while you are dragging. The
1244 drawing is too slow for that.
1246 I'd like the cursor to change as you change tools, but the stock
1247 wxCursors didn't include anything I liked, so I stuck with the
1248 pointer. Please let me know if you have any nice cursor images for me to
1252 Any bugs, comments, feedback, questions, and especially code are welcome:
1256 Chris.Barker@noaa.gov
1260 def __init__(self
, *args
, **kwargs
):
1261 wx
.App
.__init
__(self
, *args
, **kwargs
)
1264 wx
.InitAllImageHandlers()
1265 frame
= DrawFrame(None, -1, "FloatCanvas Demo App",wx
.DefaultPosition
,(700,700))
1267 self
.SetTopWindow(frame
)
1270 ## check to see if the demo is set to start in a particular mode.
1271 if StartUpDemo
== "text":
1273 if StartUpDemo
== "stext":
1274 frame
.TestScaledText()
1275 elif StartUpDemo
== "all":
1277 elif StartUpDemo
== "map":
1279 elif StartUpDemo
== "hit":
1281 elif StartUpDemo
== "hitf":
1282 "starting TestHitTestForeground"
1283 frame
.TestHitTestForeground()
1284 elif StartUpDemo
== "animate":
1285 "starting TestAnimation"
1286 frame
.TestAnimation()
1287 elif StartUpDemo
== "speed":
1288 "starting SpeedTest"
1290 elif StartUpDemo
== "temp":
1291 "starting temp Test"
1293 elif StartUpDemo
== "props":
1294 "starting PropertiesChange Test"
1295 frame
.PropertiesChangeTest()
1299 def Read_MapGen(filename
,stats
= 0,AllLines
=0):
1301 This function reads a MapGen Format file, and
1302 returns a list of NumPy arrays with the line segments in them.
1304 Each NumPy array in the list is an NX2 array of Python Floats.
1306 The demo should have come with a file, "world.dat" that is the
1307 shorelines of the whole world, in MapGen format.
1311 file = open(filename
,'rt')
1312 data
= file.readlines()
1313 data
= map(string
.strip
,data
)
1319 if line
== "# -b": #New segment beginning
1320 if segment
: Shorelines
.append(Numeric
.array(segment
))
1323 segment
.append(map(float,string
.split(line
)))
1324 if segment
: Shorelines
.append(Numeric
.array(segment
))
1327 NumSegments
= len(Shorelines
)
1329 for segment
in Shorelines
:
1330 NumPoints
= NumPoints
+ len(segment
)
1331 AvgPoints
= NumPoints
/ NumSegments
1332 print "Number of Segments: ", NumSegments
1333 print "Average Number of Points per segment: ",AvgPoints
1336 for segment
in Shorelines
:
1337 Lines
.append(segment
[0])
1338 for point
in segment
[1:-1]:
1341 Lines
.append(segment
[-1])
1346 ## for the wxPython demo:
1349 except ImportError: # if it's not there locally, try the wxPython lib.
1350 from wx
.lib
import floatcanvas
1352 overview
= floatcanvas
.__doc
__
1354 if __name__
== "__main__":
1358 app
= DemoApp(False)# put in True if you want output to go to it's own window.