]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
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"])
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":
61 def runTest(frame
, nb
, log
):
63 This method is used by the wxPython Demo Framework for integrating
64 this demo with the rest.
66 win
= DrawFrame(None, -1, "FloatCanvas Drawing Window",wx
.DefaultPosition
,(500,500))
72 from floatcanvas
import NavCanvas
, FloatCanvas
73 except ImportError: # if it's not there locally, try the wxPython lib.
74 from wx
.lib
.floatcanvas
import NavCanvas
, FloatCanvas
76 import wxPython
.lib
.colourdb
78 class DrawFrame(wx
.Frame
):
81 A frame used for the FloatCanvas Demo
86 def __init__(self
,parent
, id,title
,position
,size
):
87 wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
)
90 MenuBar
= wx
.MenuBar()
93 item
= file_menu
.Append(-1, "&Close","Close this frame")
94 self
.Bind(wx
.EVT_MENU
, self
.OnQuit
, item
)
95 MenuBar
.Append(file_menu
, "&File")
99 item
= draw_menu
.Append(-1, "&Draw Test","Run a test of drawing random components")
100 self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, item
)
102 item
= draw_menu
.Append(-1, "&Line Test","Run a test of drawing random lines")
103 self
.Bind(wx
.EVT_MENU
, self
.LineTest
, item
)
105 item
= draw_menu
.Append(-1, "Draw &Map","Run a test of drawing a map")
106 self
.Bind(wx
.EVT_MENU
, self
.DrawMap
, item
)
107 item
= draw_menu
.Append(-1, "&Text Test","Run a test of text drawing")
108 self
.Bind(wx
.EVT_MENU
, self
.TestText
, item
)
109 item
= draw_menu
.Append(-1, "&ScaledText Test","Run a test of text drawing")
110 self
.Bind(wx
.EVT_MENU
, self
.TestScaledText
, item
)
111 item
= draw_menu
.Append(-1, "&Clear","Clear the Canvas")
112 self
.Bind(wx
.EVT_MENU
, self
.Clear
, item
)
113 item
= draw_menu
.Append(-1, "&Hit Test","Run a test of the hit test code")
114 self
.Bind(wx
.EVT_MENU
, self
.TestHitTest
, item
)
115 item
= draw_menu
.Append(-1, "&Hit Test Foreground","Run a test of the hit test code with a foreground Object")
116 self
.Bind(wx
.EVT_MENU
, self
.TestHitTestForeground
, item
)
117 item
= draw_menu
.Append(-1, "&Animation","Run a test of Animation")
118 self
.Bind(wx
.EVT_MENU
, self
.TestAnimation
, item
)
119 item
= draw_menu
.Append(-1, "&Speed","Run a test of Drawing Speed")
120 self
.Bind(wx
.EVT_MENU
, self
.SpeedTest
, item
)
121 MenuBar
.Append(draw_menu
, "&Tests")
123 view_menu
= wx
.Menu()
124 item
= view_menu
.Append(-1, "Zoom to &Fit","Zoom to fit the window")
125 self
.Bind(wx
.EVT_MENU
, self
.ZoomToFit
, item
)
126 MenuBar
.Append(view_menu
, "&View")
128 help_menu
= wx
.Menu()
129 item
= help_menu
.Append(-1, "&About",
130 "More information About this program")
131 self
.Bind(wx
.EVT_MENU
, self
.OnAbout
, item
)
132 MenuBar
.Append(help_menu
, "&Help")
134 self
.SetMenuBar(MenuBar
)
136 self
.CreateStatusBar()
138 self
.Canvas
= NavCanvas
.NavCanvas(self
,
142 BackgroundColor
= "DARK SLATE BLUE")
144 wx
.EVT_CLOSE(self
, self
.OnCloseWindow
)
146 FloatCanvas
.EVT_MOTION(self
.Canvas
, self
.OnMove
)
147 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
149 self
.EventsAreBound
= False
151 ## getting all the colors and linestyles for random objects
152 wxPython
.lib
.colourdb
.updateColourDB()
153 self
.colors
= wxPython
.lib
.colourdb
.getColourList()
154 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
159 def BindAllMouseEvents(self
):
160 if not self
.EventsAreBound
:
161 ## Here is how you catch FloatCanvas mouse events
162 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, self
.OnLeftDown
)
163 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, self
.OnLeftUp
)
164 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, self
.OnLeftDouble
)
166 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, self
.OnMiddleDown
)
167 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, self
.OnMiddleUp
)
168 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, self
.OnMiddleDouble
)
170 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, self
.OnRightDown
)
171 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, self
.OnRightUp
)
172 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, self
.OnRightDouble
)
174 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, self
.OnWheel
)
175 self
.EventsAreBound
= True
177 def UnBindAllMouseEvents(self
):
178 ## Here is how you catch FloatCanvas mouse events
179 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, None )
180 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, None )
181 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, None)
183 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
184 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
185 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
187 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
188 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
189 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
191 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, None )
193 self
.EventsAreBound
= False
195 def PrintCoords(self
,event
):
196 print "coords are: %s"%(event
.Coords
,)
197 print "pixel coords are: %s\n"%(event
.GetPosition(),)
199 def OnLeftDown(self
, event
):
200 print "Left Button has been clicked in DrawFrame"
201 self
.PrintCoords(event
)
203 def OnLeftUp(self
, event
):
204 print "Left up in DrawFrame"
205 self
.PrintCoords(event
)
207 def OnLeftDouble(self
, event
):
208 print "Left Double Click in DrawFrame"
209 self
.PrintCoords(event
)
211 def OnMiddleDown(self
, event
):
212 print "Middle Button clicked in DrawFrame"
213 self
.PrintCoords(event
)
215 def OnMiddleUp(self
, event
):
216 print "Middle Button Up in DrawFrame"
217 self
.PrintCoords(event
)
219 def OnMiddleDouble(self
, event
):
220 print "Middle Button Double clicked in DrawFrame"
221 self
.PrintCoords(event
)
223 def OnRightDown(self
, event
):
224 print "Right Button has been clicked in DrawFrame"
225 self
.PrintCoords(event
)
227 def OnRightUp(self
, event
):
228 print "Right Button Up in DrawFrame"
229 self
.PrintCoords(event
)
231 def OnRightDouble(self
, event
):
232 print "Right Button Double clicked in DrawFrame"
233 self
.PrintCoords(event
)
235 def OnWheel(self
, event
):
236 print "Mouse Wheel Moved in DrawFrame"
237 self
.PrintCoords(event
)
239 def OnMove(self
, event
):
241 Updates the staus bar with the world coordinates
243 self
.SetStatusText("%.2f, %.2f"%tuple(event
.Coords
))
245 def OnAbout(self
, event
):
246 print "OnAbout called"
248 dlg
= wx
.MessageDialog(self
, "This is a small program to demonstrate\n"
249 "the use of the FloatCanvas\n",
250 "About Me", wx
.OK | wx
.ICON_INFORMATION
)
254 def ZoomToFit(self
,event
):
255 self
.Canvas
.ZoomToBB()
257 def Clear(self
,event
= None):
258 self
.UnBindAllMouseEvents()
259 self
.Canvas
.ClearAll()
260 self
.Canvas
.SetProjectionFun(None)
263 def OnQuit(self
,event
):
266 def OnCloseWindow(self
, event
):
269 def DrawTest(self
,event
=None):
275 self
.BindAllMouseEvents()
279 Canvas
.SetProjectionFun(None)
281 ## Random tests of everything:
285 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
286 lw
= random
.randint(1,5)
287 cf
= random
.randint(0,len(colors
)-1)
288 h
= random
.randint(1,5)
289 w
= random
.randint(1,5)
290 Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
294 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
295 lw
= random
.randint(1,5)
296 cf
= random
.randint(0,len(colors
)-1)
297 h
= random
.randint(1,5)
298 w
= random
.randint(1,5)
299 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
301 ## # Dots -- Does anyone need this?
302 ## for i in range(5):
303 ## x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
304 ## D = random.randint(1,50)
305 ## lw = random.randint(1,5)
306 ## cf = random.randint(0,len(colors)-1)
307 ## cl = random.randint(0,len(colors)-1)
308 ## Canvas.AddDot(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
312 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
313 D
= random
.randint(1,5)
314 lw
= random
.randint(1,5)
315 cf
= random
.randint(0,len(colors
)-1)
316 cl
= random
.randint(0,len(colors
)-1)
317 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
318 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
323 for j
in range(random
.randint(2,10)):
324 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
326 lw
= random
.randint(1,10)
327 cf
= random
.randint(0,len(colors
)-1)
328 cl
= random
.randint(0,len(colors
)-1)
329 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
334 for j
in range(random
.randint(2,6)):
335 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
337 lw
= random
.randint(1,6)
338 cf
= random
.randint(0,len(colors
)-1)
339 cl
= random
.randint(0,len(colors
)-1)
340 Canvas
.AddPolygon(points
,
342 LineColor
= colors
[cl
],
343 FillColor
= colors
[cf
],
349 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
350 cf
= random
.randint(0,len(colors
)-1)
351 D
= random
.randint(1,4)
352 Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
355 String
= "Unscaled text"
357 ts
= random
.randint(10,40)
358 cf
= random
.randint(0,len(colors
)-1)
359 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
360 Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
363 String
= "Scaled text"
365 ts
= random
.random()*3 + 0.2
366 cf
= random
.randint(0,len(colors
)-1)
367 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
368 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
372 def TestAnimation(self
,event
=None):
375 In this test, a relatively complex background is drawn, and
376 a simple object placed in the foreground is moved over
377 it. This demonstrates how to use the InForeground attribute
378 to make an object in the foregorund draw fast, without
379 having to re-draw the whole background.
382 print "Running TestAnimation"
386 self
.UnBindAllMouseEvents()
390 Canvas
.SetProjectionFun(None)
392 ## Random tests of everything:
396 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
397 lw
= random
.randint(1,5)
398 cf
= random
.randint(0,len(colors
)-1)
399 h
= random
.randint(1,5)
400 w
= random
.randint(1,5)
401 Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
405 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
406 lw
= random
.randint(1,5)
407 cf
= random
.randint(0,len(colors
)-1)
408 h
= random
.randint(1,5)
409 w
= random
.randint(1,5)
410 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
414 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
415 D
= random
.randint(1,5)
416 lw
= random
.randint(1,5)
417 cf
= random
.randint(0,len(colors
)-1)
418 cl
= random
.randint(0,len(colors
)-1)
419 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
420 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
425 for j
in range(random
.randint(2,10)):
426 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
428 lw
= random
.randint(1,10)
429 cf
= random
.randint(0,len(colors
)-1)
430 cl
= random
.randint(0,len(colors
)-1)
431 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
436 for j
in range(random
.randint(2,6)):
437 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
439 lw
= random
.randint(1,6)
440 cf
= random
.randint(0,len(colors
)-1)
441 cl
= random
.randint(0,len(colors
)-1)
442 Canvas
.AddPolygon(points
,
444 LineColor
= colors
[cl
],
445 FillColor
= colors
[cf
],
449 String
= "Scaled text"
451 ts
= random
.random()*3 + 0.2
452 cf
= random
.randint(0,len(colors
)-1)
453 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
454 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
457 # Now the Foreground Object:
458 C
= Canvas
.AddCircle(0,0,7,LineWidth
= 2,LineColor
= "Black",FillColor
= "Red", InForeground
= True)
459 T
= Canvas
.AddScaledText("Click to Move",0,0, Size
= 0.8, Position
= 'cc', InForeground
= True)
460 C
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.MoveMe
)
463 self
.Timer
= wx
.PyTimer(self
.ShowFrame
)
464 self
.FrameDelay
= 50 # milliseconds
469 Object
= self
.MovingObject
471 if self
.TimeStep
< self
.NumTimeSteps
:
473 if x
> Range
[1] or x
< Range
[0]:
475 if y
> Range
[1] or y
< Range
[0]:
477 Object
.Move( (self
.dx
,self
.dy
) )
478 Object
.Text
.Move( (self
.dx
,self
.dy
))
485 def MoveMe(self
, Object
):
486 self
.MovingObject
= Object
488 self
.dx
= random
.uniform(Range
[0]/4,Range
[1]/4)
489 self
.dy
= random
.uniform(Range
[0]/4,Range
[1]/4)
492 self
.NumTimeSteps
= 500
494 self
.Timer
.Start(self
.FrameDelay
)
495 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
497 def TestHitTest(self
,event
=None):
499 self
.UnBindAllMouseEvents()
503 Canvas
.SetProjectionFun(None)
505 #Add a HitAble rectangle
512 #Add one that is not HitAble
513 Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
514 Canvas
.AddText("Not Hit-able", x
, y
, Position
= "bl")
518 R
= Canvas
.AddRectangle(x
, y
, w
, h
,LineWidth
= 2)
519 R
.Name
= "Line Rectangle"
521 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
522 Canvas
.AddText("Left Click Line", x
, y
, Position
= "bl")
523 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
528 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
529 R
.Name
= color
+ "Rectangle"
530 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
531 Canvas
.AddText("Left Click Fill", x
, y
, Position
= "bl")
532 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
537 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
538 R
.Name
= color
+ " Rectangle"
539 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
540 Canvas
.AddText("Right Click Fill", x
, y
, Position
= "bl")
541 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
545 R
= Canvas
.AddEllipse(x
, y
, w
, h
,LineWidth
= 2,FillColor
= color
)
546 R
.Name
= color
+" Ellipse"
547 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
548 Canvas
.AddText("Right Click Fill", x
, y
, Position
= "bl")
549 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
553 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2, FillColor
= color
)
554 R
.Name
= color
+ " Circle"
556 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DCLICK
, self
.RectGotHit
)
557 Canvas
.AddText("Left D-Click Fill", x
, y
, Position
= "bl")
558 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
563 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2,FillColor
= color
)
564 R
.Name
= color
+ " Circle"
565 R
.Bind(FloatCanvas
.EVT_FC_LEFT_UP
, self
.RectGotHit
)
566 Canvas
.AddText("Left Up Fill", x
, y
, Position
= "bl")
567 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
571 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
572 R
.Name
= color
+ " Rectangle"
573 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DOWN
, self
.RectGotHit
)
574 Canvas
.AddText("Middle Down", x
, y
, Position
= "bl")
575 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
579 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
580 R
.Name
= color
+ " Rectangle"
581 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_UP
, self
.RectGotHit
)
582 Canvas
.AddText("Middle Up", x
, y
, Position
= "bl")
583 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
588 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
589 R
.Name
= color
+ " Rectangle"
590 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DCLICK
, self
.RectGotHit
)
591 Canvas
.AddText("Middle DoubleClick", x
, y
, Position
= "bl")
592 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
596 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
597 R
.Name
= color
+ " Rectangle"
598 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_UP
, self
.RectGotHit
)
599 Canvas
.AddText("Right Up", x
, y
, Position
= "bl")
600 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
604 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
605 R
.Name
= color
+ " Rectangle"
606 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DCLICK
, self
.RectGotHit
)
607 Canvas
.AddText("Right Double Click", x
, y
, Position
= "bl")
608 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
612 color
= "MEDIUM GOLDENROD"
613 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
615 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
616 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
617 Canvas
.AddText("L and R Click", x
, y
, Position
= "bl")
618 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
622 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
623 R
.Name
= color
+ " Rectangle"
624 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
625 Canvas
.AddText("Mouse Enter", x
, y
, Position
= "bl")
626 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
629 color
= "MEDIUM VIOLET RED"
630 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
632 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
633 Canvas
.AddText("Mouse Leave", x
, y
, Position
= "bl")
634 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
639 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
641 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
642 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
643 Canvas
.AddText("Enter and Leave", x
, y
, Position
= "bl")
644 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
648 R
= Canvas
.AddRectangle(x
, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
650 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
651 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
652 Canvas
.AddText("Mouse Enter&Leave", x
, y
, Position
= "bl")
653 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
657 R
= Canvas
.AddRectangle(x
-12, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
659 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
660 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
661 Canvas
.AddText("Mouse ENter&Leave", x
, y
, Position
= "bl")
662 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
666 L
= Canvas
.AddLine(( (x
, y
), (x
+10, y
+10), (x
+w
, y
+h
) ), LineWidth
= 2, LineColor
= "Red")
668 L
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
669 Canvas
.AddText("Left Down", x
, y
, Position
= "bl")
670 Canvas
.AddText(L
.Name
, x
, y
+h
, Position
= "tl")
674 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
)
675 R
= Canvas
.AddPolygon(Points
, LineWidth
= 2, FillColor
= color
)
676 R
.Name
= color
+ " Polygon"
677 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
678 Canvas
.AddText("RIGHT_DOWN", x
, y
, Position
= "bl")
679 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
683 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
)
684 R
= Canvas
.AddPointSet(Points
, Diameter
= 4, Color
= color
)
686 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.PointSetGotHit
)
687 Canvas
.AddText("LEFT_DOWN", x
, y
, Position
= "bl")
688 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
692 T
= Canvas
.AddText("Hit-able Text", x
, y
, Size
= 15, Color
= "Red", Position
= 'tl')
693 T
.Name
= "Hit-able Text"
694 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
695 Canvas
.AddText("Left Down", x
, y
, Position
= "bl")
698 T
= Canvas
.AddScaledText("Scaled Text", x
, y
, Size
= 1./2*h
, Color
= "Pink", Position
= 'bl')
699 Canvas
.AddPointSet( (x
, y
), Diameter
= 3)
700 T
.Name
= "Scaled Text"
701 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
702 Canvas
.AddText("Left Down", x
, y
, Position
= "tl")
704 self
.Canvas
.ZoomToBB()
706 def TestHitTestForeground(self
,event
=None):
707 print "Running: TestHitTestForeground"
709 self
.UnBindAllMouseEvents()
713 Canvas
.SetProjectionFun(None)
715 #Add a Hitable rectangle
723 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
, InForeground
= False)
724 R
.Name
= color
+ "Rectangle"
726 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
727 Canvas
.AddText("Left Click Fill", x
, y
, Position
= "bl")
728 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
730 ## A set of Rectangles that move together
732 ## NOTE: In a real app, it might be better to create a new
733 ## custom FloatCanvas DrawObject
735 self
.MovingRects
= []
738 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
740 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveLeft
)
741 L
= Canvas
.AddText("Left", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
742 self
.MovingRects
.extend( (R
,L
) )
745 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
747 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveRight
)
748 L
= Canvas
.AddText("Right", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
749 self
.MovingRects
.extend( (R
,L
) )
753 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
755 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveUp
)
756 L
= Canvas
.AddText("Up", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
757 self
.MovingRects
.extend( (R
,L
) )
761 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
763 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveDown
)
764 L
= Canvas
.AddText("Down", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
765 self
.MovingRects
.extend( (R
,L
) )
767 self
.Canvas
.ZoomToBB()
769 def RectMoveLeft(self
,Object
):
770 self
.MoveRects("left")
772 def RectMoveRight(self
,Object
):
773 self
.MoveRects("right")
775 def RectMoveUp(self
,Object
):
778 def RectMoveDown(self
,Object
):
779 self
.MoveRects("down")
781 def MoveRects(self
, Dir
):
782 for Object
in self
.MovingRects
:
784 if Dir
== "left": X
-= 10
785 elif Dir
== "right": X
+= 10
786 elif Dir
== "up": Y
+= 10
787 elif Dir
== "down": Y
-= 10
792 def PointSetGotHit(self
, Object
):
793 print Object
.Name
, "Got Hit\n"
795 def RectGotHit(self
, Object
):
796 print Object
.Name
, "Got Hit\n"
798 def RectGotHitRight(self
, Object
):
799 print Object
.Name
, "Got Hit With Right\n"
801 def RectGotHitLeft(self
, Object
):
802 print Object
.Name
, "Got Hit with Left\n"
804 def RectMouseOver(self
, Object
):
805 print "Mouse entered:", Object
.Name
807 def RectMouseLeave(self
, Object
):
808 print "Mouse left ", Object
.Name
811 def TestText(self
, event
= None):
813 self
.BindAllMouseEvents()
816 Canvas
.SetProjectionFun(None)
820 ## Add a non-visible rectangle, just to get a Bounding Box
821 ## Text objects have a zero-size bounding box, because it changes with zoom
822 Canvas
.AddRectangle(-10,-10,20,20,LineWidth
= 1, LineColor
= None)
826 ## for i in range(10):
827 ## ts = random.randint(10,40)
828 ## cf = random.randint(0,len(colors)-1)
829 ## x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
830 self
.Canvas
.AddText("Top Left",x
,y
,Size
= 14,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
831 self
.Canvas
.AddText("Bottom Left",x
,y
,Size
= 14,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
832 self
.Canvas
.AddText("Top Right",x
,y
,Size
= 14,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
833 self
.Canvas
.AddText("Bottom Right",x
,y
,Size
= 14,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
834 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
838 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
839 self
.Canvas
.AddText("Top Center",x
,y
,Size
= 14,Color
= "Black",Position
= "tc")
840 self
.Canvas
.AddText("Bottom Center",x
,y
,Size
= 14,Color
= "White",Position
= "bc")
844 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
845 self
.Canvas
.AddText("Center Right",x
,y
,Size
= 14,Color
= "Black",Position
= "cr")
846 self
.Canvas
.AddText("Center Left",x
,y
,Size
= 14,Color
= "Black",Position
= "cl")
850 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
851 self
.Canvas
.AddText("Center Center",x
,y
,Size
= 14,Color
= "Black",Position
= "cc")
853 self
.Canvas
.AddText("40 Pixels",-10,8,Size
= 40)
854 self
.Canvas
.AddText("20 Pixels",-10,5,Size
= 20)
855 self
.Canvas
.AddText("10 Pixels",-10,3,Size
= 10)
857 self
.Canvas
.AddText("MODERN Font", -10, 0, Family
= wx
.MODERN
)
858 self
.Canvas
.AddText("DECORATIVE Font", -10, -1, Family
= wx
.DECORATIVE
)
859 self
.Canvas
.AddText("ROMAN Font", -10, -2, Family
= wx
.ROMAN
)
860 self
.Canvas
.AddText("SCRIPT Font", -10, -3, Family
= wx
.SCRIPT
)
861 self
.Canvas
.AddText("ROMAN BOLD Font", -10, -4, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
862 self
.Canvas
.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
864 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
865 Font
= wx
.Font(20, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
866 self
.Canvas
.AddText("zapf chancery Font", -10, -6, Font
= Font
)
868 self
.Canvas
.ZoomToBB()
870 def TestScaledText(self
, event
= None):
872 self
.BindAllMouseEvents()
875 Canvas
.SetProjectionFun(None)
879 T
= Canvas
.AddScaledText("Top Left",x
,y
,Size
= 5,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
880 T
= Canvas
.AddScaledText("Bottom Left",x
,y
,Size
= 5,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
881 T
= Canvas
.AddScaledText("Top Right",x
,y
,Size
= 5,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
882 T
= Canvas
.AddScaledText("Bottom Right",x
,y
,Size
= 5,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
883 Canvas
.AddPointSet((x
,y
), Color
= "Red", Diameter
= 4)
888 Canvas
.AddScaledText("Top Center",x
,y
,Size
= 7,Color
= "Black",Position
= "tc")
889 Canvas
.AddScaledText("Bottom Center",x
,y
,Size
= 7,Color
= "White",Position
= "bc")
890 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
894 Canvas
.AddScaledText("Center Right",x
,y
,Size
= 9,Color
= "Black",Position
= "cr")
895 Canvas
.AddScaledText("Center Left",x
,y
,Size
= 9,Color
= "Black",Position
= "cl")
896 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
900 self
.Canvas
.AddScaledText("MODERN Font", x
, 0, Size
= 7, Family
= wx
.MODERN
, Color
= (0,0,0))
901 self
.Canvas
.AddScaledText("DECORATIVE Font", x
, -10, Size
= 7, Family
= wx
.DECORATIVE
, Color
= (0,0,1))
902 self
.Canvas
.AddScaledText("ROMAN Font", x
, -20, Size
= 7, Family
= wx
.ROMAN
)
903 self
.Canvas
.AddScaledText("SCRIPT Font", x
, -30, Size
= 7, Family
= wx
.SCRIPT
)
904 self
.Canvas
.AddScaledText("ROMAN BOLD Font", x
, -40, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
905 self
.Canvas
.AddScaledText("ROMAN ITALIC BOLD Font", x
, -50, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
906 Canvas
.AddPointSet((x
,0), Color
= "White", Diameter
= 4)
909 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
911 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
912 T
= self
.Canvas
.AddScaledText("zapf chancery Font", x
, y
, Size
= 20, Font
= Font
, Position
= 'bc')
915 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "bookman")
916 T
= self
.Canvas
.AddScaledText("Bookman Font", x
, y
, Size
= 8, Font
= Font
)
918 self
.Canvas
.ZoomToBB()
920 def DrawMap(self
,event
= None):
922 self
.BindAllMouseEvents()
924 ## Test of Actual Map Data
925 self
.Canvas
.ClearAll()
926 self
.Canvas
.SetProjectionFun("FlatEarth")
927 #start = time.clock()
928 Shorelines
= Read_MapGen(os
.path
.join("data",'world.dat'),stats
= 0)
929 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
930 #start = time.clock()
931 for segment
in Shorelines
:
932 self
.Canvas
.AddLine(segment
)
933 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
934 #start = time.clock()
935 self
.Canvas
.ZoomToBB()
936 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
939 def LineTest(self
,event
= None):
944 ## Test of drawing lots of lines
947 Canvas
.SetProjectionFun(None)
948 #start = time.clock()
952 for i
in range(2000):
953 points
= (random
.randint(Range
[0],Range
[1]),
954 random
.randint(Range
[0],Range
[1]),
955 random
.randint(Range
[0],Range
[1]),
956 random
.randint(Range
[0],Range
[1]))
957 linepoints
.append(points
)
958 linewidths
.append(random
.randint(1,10) )
959 linecolors
.append(random
.randint(0,len(colors
)-1) )
960 for (points
,color
,width
) in zip(linepoints
,linecolors
,linewidths
):
961 Canvas
.AddLine((points
[0:2],points
[2:4]), LineWidth
= width
, LineColor
= colors
[color
])
962 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
963 #start = time.clock()
965 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
967 def SpeedTest(self
,event
=None):
970 BigRange
= (-1000,1000)
973 self
.UnBindAllMouseEvents()
977 Canvas
.SetProjectionFun(None)
980 String
= "Unscaled text"
983 for i
in range(5000):
984 x
,y
= (random
.uniform(BigRange
[0],BigRange
[1]),random
.uniform(BigRange
[0],BigRange
[1]))
985 coords
.append( (x
,y
) )
986 print "Drawing the Numbers"
988 for i
in xrange( len(coords
) ):
989 Canvas
.AddText("%i"%(i),
994 BackgroundColor
= "White")
995 print "It took %s seconds to add the numbers"%(time
.clock() - start
)
1000 ## print "Building a list of lots of random objects"
1001 ## ## Random tests of everything:
1004 ## Range = ( random.randint(BigRange[0],BigRange[1]), random.randint(BigRange[0],BigRange[1]) )
1005 ## if abs (Range[0] - Range[1]) < 10:
1007 ## if Range[0] > Range[1]:
1008 ## Range = ( Range[1], Range[0] )
1013 ## for i in range(300):
1014 ## Range = MakeRange()
1015 ## x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1016 ## lw = random.randint(1,5)
1017 ## cf = random.randint(0,len(colors)-1)
1018 ## h = random.randint(1, Range[1] - Range[0])
1019 ## w = random.randint(1, Range[1] - Range[0])
1020 ## ObjectList.append(FloatCanvas.Rectangle(x,y,h,w,LineWidth = lw,FillColor = colors[cf]))
1023 ## for i in range(300):
1024 ## Range = MakeRange()
1025 ## x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1026 ## lw = random.randint(1,5)
1027 ## cf = random.randint(0,len(colors)-1)
1028 ## h = random.randint(1, Range[1] - Range[0])
1029 ## w = random.randint(1, Range[1] - Range[0])
1030 ## ObjectList.append(FloatCanvas.Ellipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf]))
1034 ## for i in range(500):
1035 ## Range = MakeRange()
1036 ## x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1037 ## D = random.randint(1, (Range[1] - Range[0]) / 2)
1038 ## lw = random.randint(1,5)
1039 ## cf = random.randint(0,len(colors)-1)
1040 ## cl = random.randint(0,len(colors)-1)
1041 ## ObjectList.append(FloatCanvas.Circle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf]))
1042 ## #ObjectList.append(FloatCanvas.Text("Circle # %i"%(i),x,y,Size = 12,BackgroundColor = None,Position = "cc"))
1045 ## for i in range(500):
1046 ## Range = MakeRange()
1048 ## for j in range(random.randint(2,100)):
1049 ## point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
1050 ## points.append(point)
1051 ## lw = random.randint(1,10)
1052 ## cf = random.randint(0,len(colors)-1)
1053 ## cl = random.randint(0,len(colors)-1)
1054 ## ObjectList.append(FloatCanvas.Line(points, LineWidth = lw, LineColor = colors[cl]) )
1057 ## for i in range(300):
1058 ## Range = MakeRange()
1060 ## for j in range(random.randint(2,60)):
1061 ## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1062 ## points.append(point)
1063 ## lw = random.randint(1,6)
1064 ## cf = random.randint(0,len(colors)-1)
1065 ## cl = random.randint(0,len(colors)-1)
1066 ## ObjectList.append(FloatCanvas.Polygon(points,
1068 ## LineColor = colors[cl],
1069 ## FillColor = colors[cf],
1070 ## FillStyle = 'Solid') )
1071 ## random.shuffle(ObjectList)
1072 ## print "Adding lots of random objects"
1073 ## start = time.clock()
1074 ## for Object in ObjectList:
1075 ## Canvas.AddObject(Object)
1076 ## print "It took %s Seconds to add %i objects "%(time.clock() - start, len(ObjectList) )
1079 ## for i in range(100):
1081 ## points = RandomArray.uniform(Range[0],Range[1],(100,2))
1082 ## cf = random.randint(0,len(colors)-1)
1083 ## D = random.randint(1,4)
1084 ## Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
1088 ## String = "Scaled text"
1089 ## for i in range(30):
1090 ## ts = random.random()*3 + 0.2
1091 ## cf = random.randint(0,len(colors)-1)
1092 ## x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1093 ## Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
1098 def TempTest(self
, event
= None):
1099 "Running the Temporary test"
1101 self
.UnBindAllMouseEvents()
1102 Canvas
= self
.Canvas
1104 Canvas
.SetProjectionFun(None)
1107 # import RandomArray
1110 # Create a random Polygon
1113 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1114 points
.append(point
)
1115 Poly
= Canvas
.AddPolygon(points
,
1117 LineColor
= "Black",
1118 FillColor
= "LightBlue",
1119 FillStyle
= 'Solid')
1121 Poly
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPoly
)
1123 self
.SelectedPoly
= None
1124 self
.SelectPoints
= []
1125 self
.SelectedPoint
= None
1129 def SelectPoly(self
, Object
):
1130 print "In SelectPoly"
1131 Canvas
= self
.Canvas
1132 if Object
is self
.SelectedPoly
:
1135 #fixme: Do something to unselect the old one
1136 self
.SelectedPoly
= Object
1137 Canvas
.RemoveObjects(self
.SelectPoints
)
1138 self
.SelectPoints
= []
1139 # Draw points on the Vertices of the Selected Poly:
1140 for i
, point
in enumerate(Object
.Points
):
1141 P
= Canvas
.AddPointSet(point
, Diameter
= 6, Color
= "Red")
1143 P
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPointHit
)
1144 self
.SelectPoints
.append(P
)
1148 def SelectPointHit(self
, Point
):
1149 print "Point Num: %i Hit"%Point
.VerticeNum
1150 self
.SelectedPoint
= Point
1154 class DemoApp(wx
.App
):
1158 Under the Draw menu, there are three options:
1160 *Draw Test: will put up a picture of a bunch of randomly generated
1161 objects, of each kind supported.
1163 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1164 with a lot of data, and will take a while to load and draw (about 10 sec
1165 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1166 performance is not very good for large drawings.
1168 *Clear: Clears the Canvas.
1170 Once you have a picture drawn, you can zoom in and out and move about
1171 the picture. There is a tool bar with three tools that can be
1174 The magnifying glass with the plus is the zoom in tool. Once selected,
1175 if you click the image, it will zoom in, centered on where you
1176 clicked. If you click and drag the mouse, you will get a rubber band
1177 box, and the image will zoom to fit that box when you release it.
1179 The magnifying glass with the minus is the zoom out tool. Once selected,
1180 if you click the image, it will zoom out, centered on where you
1181 clicked. (note that this takes a while when you are looking at the map,
1182 as it has a LOT of lines to be drawn. The image is double buffered, so
1183 you don't see the drawing in progress)
1185 The hand is the move tool. Once selected, if you click and drag on the
1186 image, it will move so that the part you clicked on ends up where you
1187 release the mouse. Nothing is changed while you are dragging. The
1188 drawing is too slow for that.
1190 I'd like the cursor to change as you change tools, but the stock
1191 wxCursors didn't include anything I liked, so I stuck with the
1192 pointer. Please let me know if you have any nice cursor images for me to
1196 Any bugs, comments, feedback, questions, and especially code are welcome:
1200 Chris.Barker@noaa.gov
1204 def __init__(self
, *args
, **kwargs
):
1205 wx
.App
.__init
__(self
, *args
, **kwargs
)
1208 wx
.InitAllImageHandlers()
1209 frame
= DrawFrame(None, -1, "FloatCanvas Demo App",wx
.DefaultPosition
,(700,700))
1211 self
.SetTopWindow(frame
)
1214 ## check to see if the demo is set to start in a particular mode.
1215 if StartUpDemo
== "text":
1217 if StartUpDemo
== "stext":
1218 frame
.TestScaledText()
1219 elif StartUpDemo
== "all":
1221 elif StartUpDemo
== "map":
1223 elif StartUpDemo
== "hit":
1225 elif StartUpDemo
== "hitf":
1226 "starting TestHitTestForeground"
1227 frame
.TestHitTestForeground()
1228 elif StartUpDemo
== "animate":
1229 "starting TestAnimation"
1230 frame
.TestAnimation()
1231 elif StartUpDemo
== "speed":
1232 "starting SpeedTest"
1234 elif StartUpDemo
== "temp":
1235 "starting temp Test"
1240 def Read_MapGen(filename
,stats
= 0,AllLines
=0):
1242 This function reads a MapGen Format file, and
1243 returns a list of NumPy arrays with the line segments in them.
1245 Each NumPy array in the list is an NX2 array of Python Floats.
1247 The demo should have come with a file, "world.dat" that is the
1248 shorelines of the whole world, in MapGen format.
1252 file = open(filename
,'rt')
1253 data
= file.readlines()
1254 data
= map(string
.strip
,data
)
1260 if line
== "# -b": #New segment beginning
1261 if segment
: Shorelines
.append(Numeric
.array(segment
))
1264 segment
.append(map(float,string
.split(line
)))
1265 if segment
: Shorelines
.append(Numeric
.array(segment
))
1268 NumSegments
= len(Shorelines
)
1270 for segment
in Shorelines
:
1271 NumPoints
= NumPoints
+ len(segment
)
1272 AvgPoints
= NumPoints
/ NumSegments
1273 print "Number of Segments: ", NumSegments
1274 print "Average Number of Points per segment: ",AvgPoints
1277 for segment
in Shorelines
:
1278 Lines
.append(segment
[0])
1279 for point
in segment
[1:-1]:
1282 Lines
.append(segment
[-1])
1287 ## for the wxPython demo:
1290 except ImportError: # if it's not there locally, try the wxPython lib.
1291 from wx
.lib
import floatcanvas
1293 overview
= floatcanvas
.__doc
__
1295 if __name__
== "__main__":
1299 app
= DemoApp(False)# put in True if you want output to go to it's own window.