]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
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
25 def runTest(frame
, nb
, log
):
26 dlg
= wx
.MessageDialog(frame
, errorText
, 'Sorry', wx
.OK |
35 if __name__
== "__main__": # parse options if run stand-alone
38 optlist
, args
= getopt
.getopt(sys
.argv
[1:],'l',["local","all","text","map","stext","hit","hitf","animate","speed","temp","props"])
43 elif opt
[0] == "--text":
45 elif opt
[0] == "--map":
47 elif opt
[0] == "--stext":
49 elif opt
[0] == "--hit":
51 elif opt
[0] == "--hitf":
53 elif opt
[0] == "--animate":
54 StartUpDemo
= "animate"
55 elif opt
[0] == "--speed":
57 elif opt
[0] == "--temp":
59 elif opt
[0] == "--props":
64 #---------------------------------------------------------------------------
66 class TestPanel(wx
.Panel
):
67 def __init__(self
, parent
, log
):
69 wx
.Panel
.__init
__(self
, parent
, -1)
71 b
= wx
.Button(self
, -1, "Show the FloatBar sample", (50,50))
72 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
75 def OnButton(self
, evt
):
76 win
= DrawFrame(None, -1, "FloatCanvas Drawing Window",wx
.DefaultPosition
,(500,500))
81 def runTest(frame
, nb
, log
):
82 win
= TestPanel(nb
, log
)
89 from floatcanvas
import NavCanvas
, FloatCanvas
90 except ImportError: # if it's not there locally, try the wxPython lib.
91 from wx
.lib
.floatcanvas
import NavCanvas
, FloatCanvas
93 import wxPython
.lib
.colourdb
95 class DrawFrame(wx
.Frame
):
98 A frame used for the FloatCanvas Demo
103 def __init__(self
,parent
, id,title
,position
,size
):
104 wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
)
106 ## Set up the MenuBar
107 MenuBar
= wx
.MenuBar()
109 file_menu
= wx
.Menu()
110 item
= file_menu
.Append(-1, "&Close","Close this frame")
111 self
.Bind(wx
.EVT_MENU
, self
.OnQuit
, item
)
112 MenuBar
.Append(file_menu
, "&File")
114 draw_menu
= wx
.Menu()
116 item
= draw_menu
.Append(-1, "&Draw Test","Run a test of drawing random components")
117 self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, item
)
119 item
= draw_menu
.Append(-1, "&Line Test","Run a test of drawing random lines")
120 self
.Bind(wx
.EVT_MENU
, self
.LineTest
, item
)
122 item
= draw_menu
.Append(-1, "Draw &Map","Run a test of drawing a map")
123 self
.Bind(wx
.EVT_MENU
, self
.DrawMap
, item
)
124 item
= draw_menu
.Append(-1, "&Text Test","Run a test of text drawing")
125 self
.Bind(wx
.EVT_MENU
, self
.TestText
, item
)
126 item
= draw_menu
.Append(-1, "&ScaledText Test","Run a test of text drawing")
127 self
.Bind(wx
.EVT_MENU
, self
.TestScaledText
, item
)
128 item
= draw_menu
.Append(-1, "&Clear","Clear the Canvas")
129 self
.Bind(wx
.EVT_MENU
, self
.Clear
, item
)
130 item
= draw_menu
.Append(-1, "&Hit Test","Run a test of the hit test code")
131 self
.Bind(wx
.EVT_MENU
, self
.TestHitTest
, item
)
132 item
= draw_menu
.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
133 self
.Bind(wx
.EVT_MENU
, self
.TestHitTestForeground
, item
)
134 item
= draw_menu
.Append(-1, "&Animation","Run a test of Animation")
135 self
.Bind(wx
.EVT_MENU
, self
.TestAnimation
, item
)
136 item
= draw_menu
.Append(-1, "&Speed","Run a test of Drawing Speed")
137 self
.Bind(wx
.EVT_MENU
, self
.SpeedTest
, item
)
138 item
= draw_menu
.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
139 self
.Bind(wx
.EVT_MENU
, self
.PropertiesChangeTest
, item
)
140 MenuBar
.Append(draw_menu
, "&Tests")
142 view_menu
= wx
.Menu()
143 item
= view_menu
.Append(-1, "Zoom to &Fit","Zoom to fit the window")
144 self
.Bind(wx
.EVT_MENU
, self
.ZoomToFit
, item
)
145 MenuBar
.Append(view_menu
, "&View")
147 help_menu
= wx
.Menu()
148 item
= help_menu
.Append(-1, "&About",
149 "More information About this program")
150 self
.Bind(wx
.EVT_MENU
, self
.OnAbout
, item
)
151 MenuBar
.Append(help_menu
, "&Help")
153 self
.SetMenuBar(MenuBar
)
155 self
.CreateStatusBar()
157 self
.Canvas
= NavCanvas
.NavCanvas(self
,
161 BackgroundColor
= "DARK SLATE BLUE")
163 wx
.EVT_CLOSE(self
, self
.OnCloseWindow
)
165 FloatCanvas
.EVT_MOTION(self
.Canvas
, self
.OnMove
)
166 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
168 self
.EventsAreBound
= False
170 ## getting all the colors and linestyles for random objects
171 wxPython
.lib
.colourdb
.updateColourDB()
172 self
.colors
= wxPython
.lib
.colourdb
.getColourList()
173 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
178 def BindAllMouseEvents(self
):
179 if not self
.EventsAreBound
:
180 ## Here is how you catch FloatCanvas mouse events
181 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, self
.OnLeftDown
)
182 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, self
.OnLeftUp
)
183 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, self
.OnLeftDouble
)
185 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, self
.OnMiddleDown
)
186 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, self
.OnMiddleUp
)
187 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, self
.OnMiddleDouble
)
189 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, self
.OnRightDown
)
190 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, self
.OnRightUp
)
191 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, self
.OnRightDouble
)
193 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, self
.OnWheel
)
194 self
.EventsAreBound
= True
196 def UnBindAllMouseEvents(self
):
197 ## Here is how you catch FloatCanvas mouse events
198 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, None )
199 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, None )
200 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, None)
202 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
203 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
204 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
206 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
207 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
208 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
210 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, None )
212 self
.EventsAreBound
= False
214 def PrintCoords(self
,event
):
215 print "coords are: %s"%(event
.Coords
,)
216 print "pixel coords are: %s\n"%(event
.GetPosition(),)
218 def OnLeftDown(self
, event
):
219 print "Left Button has been clicked in DrawFrame"
220 self
.PrintCoords(event
)
222 def OnLeftUp(self
, event
):
223 print "Left up in DrawFrame"
224 self
.PrintCoords(event
)
226 def OnLeftDouble(self
, event
):
227 print "Left Double Click in DrawFrame"
228 self
.PrintCoords(event
)
230 def OnMiddleDown(self
, event
):
231 print "Middle Button clicked in DrawFrame"
232 self
.PrintCoords(event
)
234 def OnMiddleUp(self
, event
):
235 print "Middle Button Up in DrawFrame"
236 self
.PrintCoords(event
)
238 def OnMiddleDouble(self
, event
):
239 print "Middle Button Double clicked in DrawFrame"
240 self
.PrintCoords(event
)
242 def OnRightDown(self
, event
):
243 print "Right Button has been clicked in DrawFrame"
244 self
.PrintCoords(event
)
246 def OnRightUp(self
, event
):
247 print "Right Button Up in DrawFrame"
248 self
.PrintCoords(event
)
250 def OnRightDouble(self
, event
):
251 print "Right Button Double clicked in DrawFrame"
252 self
.PrintCoords(event
)
254 def OnWheel(self
, event
):
255 print "Mouse Wheel Moved in DrawFrame"
256 self
.PrintCoords(event
)
258 def OnMove(self
, event
):
260 Updates the staus bar with the world coordinates
262 self
.SetStatusText("%.2f, %.2f"%tuple(event
.Coords
))
264 def OnAbout(self
, event
):
265 print "OnAbout called"
267 dlg
= wx
.MessageDialog(self
, "This is a small program to demonstrate\n"
268 "the use of the FloatCanvas\n",
269 "About Me", wx
.OK | wx
.ICON_INFORMATION
)
273 def ZoomToFit(self
,event
):
274 self
.Canvas
.ZoomToBB()
276 def Clear(self
,event
= None):
277 self
.UnBindAllMouseEvents()
278 self
.Canvas
.ClearAll()
279 self
.Canvas
.SetProjectionFun(None)
282 def OnQuit(self
,event
):
285 def OnCloseWindow(self
, event
):
288 def DrawTest(self
,event
=None):
295 self
.BindAllMouseEvents()
299 Canvas
.SetProjectionFun(None)
301 ## Random tests of everything:
305 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
306 lw
= random
.randint(1,5)
307 cf
= random
.randint(0,len(colors
)-1)
308 h
= random
.randint(1,5)
309 w
= random
.randint(1,5)
310 Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
314 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
315 lw
= random
.randint(1,5)
316 cf
= random
.randint(0,len(colors
)-1)
317 h
= random
.randint(1,5)
318 w
= random
.randint(1,5)
319 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
323 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
324 D
= random
.randint(1,50)
325 cf
= random
.randint(0,len(colors
)-1)
326 Canvas
.AddPoint((x
,y
), Color
= colors
[cf
], Diameter
= D
)
330 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
331 D
= random
.randint(1,5)
332 lw
= random
.randint(1,5)
333 cf
= random
.randint(0,len(colors
)-1)
334 cl
= random
.randint(0,len(colors
)-1)
335 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
336 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
341 for j
in range(random
.randint(2,10)):
342 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
344 lw
= random
.randint(1,10)
345 cf
= random
.randint(0,len(colors
)-1)
346 cl
= random
.randint(0,len(colors
)-1)
347 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
352 for j
in range(random
.randint(2,6)):
353 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
355 lw
= random
.randint(1,6)
356 cf
= random
.randint(0,len(colors
)-1)
357 cl
= random
.randint(0,len(colors
)-1)
358 Canvas
.AddPolygon(points
,
360 LineColor
= colors
[cl
],
361 FillColor
= colors
[cf
],
367 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
368 cf
= random
.randint(0,len(colors
)-1)
369 D
= random
.randint(1,4)
370 Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
373 String
= "Unscaled text"
375 ts
= random
.randint(10,40)
376 cf
= random
.randint(0,len(colors
)-1)
377 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
378 Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
381 String
= "Scaled text"
383 ts
= random
.random()*3 + 0.2
384 cf
= random
.randint(0,len(colors
)-1)
385 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
386 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
390 def TestAnimation(self
,event
=None):
393 In this test, a relatively complex background is drawn, and
394 a simple object placed in the foreground is moved over
395 it. This demonstrates how to use the InForeground attribute
396 to make an object in the foregorund draw fast, without
397 having to re-draw the whole background.
404 self
.UnBindAllMouseEvents()
408 Canvas
.SetProjectionFun(None)
410 ## Random tests of everything:
414 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
415 lw
= random
.randint(1,5)
416 cf
= random
.randint(0,len(colors
)-1)
417 h
= random
.randint(1,5)
418 w
= random
.randint(1,5)
419 Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
423 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
424 lw
= random
.randint(1,5)
425 cf
= random
.randint(0,len(colors
)-1)
426 h
= random
.randint(1,5)
427 w
= random
.randint(1,5)
428 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
432 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
433 D
= random
.randint(1,5)
434 lw
= random
.randint(1,5)
435 cf
= random
.randint(0,len(colors
)-1)
436 cl
= random
.randint(0,len(colors
)-1)
437 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
438 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
443 for j
in range(random
.randint(2,10)):
444 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
446 lw
= random
.randint(1,10)
447 cf
= random
.randint(0,len(colors
)-1)
448 cl
= random
.randint(0,len(colors
)-1)
449 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
454 for j
in range(random
.randint(2,6)):
455 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
457 lw
= random
.randint(1,6)
458 cf
= random
.randint(0,len(colors
)-1)
459 cl
= random
.randint(0,len(colors
)-1)
460 Canvas
.AddPolygon(points
,
462 LineColor
= colors
[cl
],
463 FillColor
= colors
[cf
],
467 String
= "Scaled text"
469 ts
= random
.random()*3 + 0.2
470 cf
= random
.randint(0,len(colors
)-1)
471 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
472 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
475 # Now the Foreground Object:
476 C
= Canvas
.AddCircle(0,0,7,LineWidth
= 2,LineColor
= "Black",FillColor
= "Red", InForeground
= True)
477 T
= Canvas
.AddScaledText("Click to Move",0,0, Size
= 0.6, Position
= 'cc', InForeground
= True)
478 C
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.MoveMe
)
481 self
.Timer
= wx
.PyTimer(self
.ShowFrame
)
482 self
.FrameDelay
= 50 # milliseconds
487 Object
= self
.MovingObject
489 if self
.TimeStep
< self
.NumTimeSteps
:
491 if x
> Range
[1] or x
< Range
[0]:
493 if y
> Range
[1] or y
< Range
[0]:
495 Object
.Move( (self
.dx
,self
.dy
) )
496 Object
.Text
.Move( (self
.dx
,self
.dy
))
499 wx
.GetApp().Yield(True)
504 def MoveMe(self
, Object
):
505 self
.MovingObject
= Object
507 self
.dx
= random
.uniform(Range
[0]/4,Range
[1]/4)
508 self
.dy
= random
.uniform(Range
[0]/4,Range
[1]/4)
511 self
.NumTimeSteps
= 200
513 self
.Timer
.Start(self
.FrameDelay
)
514 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
516 def TestHitTest(self
,event
=None):
519 self
.UnBindAllMouseEvents()
523 Canvas
.SetProjectionFun(None)
525 #Add a HitAble rectangle
533 #Add one that is not HitAble
534 Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
535 Canvas
.AddText("Not Hit-able", x
, y
, Size
= FontSize
, Position
= "bl")
539 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
540 R
.Name
= "Line Rectangle"
542 R
.HitLineWidth
= 5 # Makes it a little easier to hit
543 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
544 Canvas
.AddText("Left Click Line", x
, y
, Size
= FontSize
, Position
= "bl")
545 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
549 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
550 R
.Name
= color
+ "Rectangle"
551 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
552 Canvas
.AddText("Left Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
553 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
558 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
559 R
.Name
= color
+ " Rectangle"
560 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
561 Canvas
.AddText("Right Click Fill", x
, y
, Position
= "bl")
562 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
566 R
= Canvas
.AddEllipse(x
, y
, w
, h
,LineWidth
= 2,FillColor
= color
)
567 R
.Name
= color
+" Ellipse"
568 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
569 Canvas
.AddText("Right Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
570 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
574 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2, FillColor
= color
)
575 R
.Name
= color
+ " Circle"
577 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DCLICK
, self
.RectGotHit
)
578 Canvas
.AddText("Left D-Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
579 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
584 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2,FillColor
= color
)
585 R
.Name
= color
+ " Circle"
586 R
.Bind(FloatCanvas
.EVT_FC_LEFT_UP
, self
.RectGotHit
)
587 Canvas
.AddText("Left Up Fill", x
, y
, Size
= FontSize
, Position
= "bl")
588 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
592 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
593 R
.Name
= color
+ " Rectangle"
594 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DOWN
, self
.RectGotHit
)
595 Canvas
.AddText("Middle Down", x
, y
, Size
= FontSize
, Position
= "bl")
596 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
600 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
601 R
.Name
= color
+ " Rectangle"
602 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_UP
, self
.RectGotHit
)
603 Canvas
.AddText("Middle Up", x
, y
, Size
= FontSize
, Position
= "bl")
604 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
609 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
610 R
.Name
= color
+ " Rectangle"
611 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DCLICK
, self
.RectGotHit
)
612 Canvas
.AddText("Middle DoubleClick", x
, y
, Size
= FontSize
, Position
= "bl")
613 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
617 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
618 R
.Name
= color
+ " Rectangle"
619 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_UP
, self
.RectGotHit
)
620 Canvas
.AddText("Right Up", x
, y
, Size
= FontSize
, Position
= "bl")
621 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
625 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
626 R
.Name
= color
+ " Rectangle"
627 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DCLICK
, self
.RectGotHit
)
628 Canvas
.AddText("Right Double Click", x
, y
, Size
= FontSize
, Position
= "bl")
629 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
633 color
= "MEDIUM GOLDENROD"
634 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
636 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
637 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
638 Canvas
.AddText("L and R Click", x
, y
, Size
= FontSize
, Position
= "bl")
639 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
643 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
644 R
.Name
= color
+ " Rectangle"
645 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
646 Canvas
.AddText("Mouse Enter", x
, y
, Size
= FontSize
, Position
= "bl")
647 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
650 color
= "MEDIUM VIOLET RED"
651 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
653 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
654 Canvas
.AddText("Mouse Leave", x
, y
, Size
= FontSize
, Position
= "bl")
655 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
660 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
662 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
663 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
664 Canvas
.AddText("Enter and Leave", x
, y
, Size
= FontSize
, Position
= "bl")
665 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
669 R
= Canvas
.AddRectangle(x
, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
671 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
672 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
673 Canvas
.AddText("Mouse Enter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
674 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
678 R
= Canvas
.AddRectangle(x
-12, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
680 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
681 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
682 Canvas
.AddText("Mouse ENter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
683 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
687 L
= Canvas
.AddLine(( (x
, y
), (x
+10, y
+10), (x
+w
, y
+h
) ), LineWidth
= 2, LineColor
= "Red")
689 L
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
690 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
691 Canvas
.AddText(L
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
695 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
)
696 R
= Canvas
.AddPolygon(Points
, LineWidth
= 2, FillColor
= color
)
697 R
.Name
= color
+ " Polygon"
698 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
699 Canvas
.AddText("RIGHT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
700 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
704 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
)
705 R
= Canvas
.AddPointSet(Points
, Diameter
= 4, Color
= color
)
707 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.PointSetGotHit
)
708 Canvas
.AddText("LEFT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
709 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
713 T
= Canvas
.AddText("Hit-able Text", x
, y
, Size
= 15, Color
= "Red", Position
= 'tl')
714 T
.Name
= "Hit-able Text"
715 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
716 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
719 T
= Canvas
.AddScaledText("Scaled Text", x
, y
, Size
= 1./2*h
, Color
= "Pink", Position
= 'bl')
720 Canvas
.AddPointSet( (x
, y
), Diameter
= 3)
721 T
.Name
= "Scaled Text"
722 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
723 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "tl")
725 self
.Canvas
.ZoomToBB()
727 def TestHitTestForeground(self
,event
=None):
730 self
.UnBindAllMouseEvents()
734 Canvas
.SetProjectionFun(None)
736 #Add a Hitable rectangle
744 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
, InForeground
= False)
745 R
.Name
= color
+ "Rectangle"
747 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
748 Canvas
.AddText("Left Click Fill", x
, y
, Position
= "bl")
749 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
751 ## A set of Rectangles that move together
753 ## NOTE: In a real app, it might be better to create a new
754 ## custom FloatCanvas DrawObject
756 self
.MovingRects
= []
759 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
761 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveLeft
)
762 L
= Canvas
.AddText("Left", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
763 self
.MovingRects
.extend( (R
,L
) )
766 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
768 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveRight
)
769 L
= Canvas
.AddText("Right", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
770 self
.MovingRects
.extend( (R
,L
) )
774 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
776 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveUp
)
777 L
= Canvas
.AddText("Up", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
778 self
.MovingRects
.extend( (R
,L
) )
782 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
784 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveDown
)
785 L
= Canvas
.AddText("Down", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
786 self
.MovingRects
.extend( (R
,L
) )
788 self
.Canvas
.ZoomToBB()
790 def RectMoveLeft(self
,Object
):
791 self
.MoveRects("left")
793 def RectMoveRight(self
,Object
):
794 self
.MoveRects("right")
796 def RectMoveUp(self
,Object
):
799 def RectMoveDown(self
,Object
):
800 self
.MoveRects("down")
802 def MoveRects(self
, Dir
):
803 for Object
in self
.MovingRects
:
805 if Dir
== "left": X
-= 10
806 elif Dir
== "right": X
+= 10
807 elif Dir
== "up": Y
+= 10
808 elif Dir
== "down": Y
-= 10
813 def PointSetGotHit(self
, Object
):
814 print Object
.Name
, "Got Hit\n"
816 def RectGotHit(self
, Object
):
817 print Object
.Name
, "Got Hit\n"
819 def RectGotHitRight(self
, Object
):
820 print Object
.Name
, "Got Hit With Right\n"
822 def RectGotHitLeft(self
, Object
):
823 print Object
.Name
, "Got Hit with Left\n"
825 def RectMouseOver(self
, Object
):
826 print "Mouse entered:", Object
.Name
828 def RectMouseLeave(self
, Object
):
829 print "Mouse left ", Object
.Name
832 def TestText(self
, event
= None):
835 self
.BindAllMouseEvents()
838 Canvas
.SetProjectionFun(None)
842 ## Add a non-visible rectangle, just to get a Bounding Box
843 ## Text objects have a zero-size bounding box, because it changes with zoom
844 Canvas
.AddRectangle(-10,-10,20,20,LineWidth
= 1, LineColor
= None)
848 self
.Canvas
.AddText("Top Left",x
,y
,Size
= 14,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
849 self
.Canvas
.AddText("Bottom Left",x
,y
,Size
= 14,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
850 self
.Canvas
.AddText("Top Right",x
,y
,Size
= 14,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
851 self
.Canvas
.AddText("Bottom Right",x
,y
,Size
= 14,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
852 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
856 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
857 self
.Canvas
.AddText("Top Center",x
,y
,Size
= 14,Color
= "Black",Position
= "tc")
858 self
.Canvas
.AddText("Bottom Center",x
,y
,Size
= 14,Color
= "White",Position
= "bc")
862 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
863 self
.Canvas
.AddText("Center Right",x
,y
,Size
= 14,Color
= "Black",Position
= "cr")
864 self
.Canvas
.AddText("Center Left",x
,y
,Size
= 14,Color
= "Black",Position
= "cl")
868 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
869 self
.Canvas
.AddText("Center Center",x
,y
,Size
= 14,Color
= "Black",Position
= "cc")
871 self
.Canvas
.AddText("40 Pixels",-10,8,Size
= 40)
872 self
.Canvas
.AddText("20 Pixels",-10,5,Size
= 20)
873 self
.Canvas
.AddText("10 Pixels",-10,3,Size
= 10)
875 self
.Canvas
.AddText("MODERN Font", -10, 0, Family
= wx
.MODERN
)
876 self
.Canvas
.AddText("DECORATIVE Font", -10, -1, Family
= wx
.DECORATIVE
)
877 self
.Canvas
.AddText("ROMAN Font", -10, -2, Family
= wx
.ROMAN
)
878 self
.Canvas
.AddText("SCRIPT Font", -10, -3, Family
= wx
.SCRIPT
)
879 self
.Canvas
.AddText("ROMAN BOLD Font", -10, -4, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
880 self
.Canvas
.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
882 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
883 Font
= wx
.Font(20, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
884 self
.Canvas
.AddText("zapf chancery Font", -10, -6, Font
= Font
)
886 self
.Canvas
.ZoomToBB()
888 def TestScaledText(self
, event
= None):
891 self
.BindAllMouseEvents()
894 Canvas
.SetProjectionFun(None)
898 T
= Canvas
.AddScaledText("Top Left",x
,y
,Size
= 5,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
899 T
= Canvas
.AddScaledText("Bottom Left",x
,y
,Size
= 5,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
900 T
= Canvas
.AddScaledText("Top Right",x
,y
,Size
= 5,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
901 T
= Canvas
.AddScaledText("Bottom Right",x
,y
,Size
= 5,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
902 Canvas
.AddPointSet((x
,y
), Color
= "Red", Diameter
= 4)
907 Canvas
.AddScaledText("Top Center",x
,y
,Size
= 7,Color
= "Black",Position
= "tc")
908 Canvas
.AddScaledText("Bottom Center",x
,y
,Size
= 7,Color
= "White",Position
= "bc")
909 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
913 Canvas
.AddScaledText("Center Right",x
,y
,Size
= 9,Color
= "Black",Position
= "cr")
914 Canvas
.AddScaledText("Center Left",x
,y
,Size
= 9,Color
= "Black",Position
= "cl")
915 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
919 self
.Canvas
.AddScaledText("MODERN Font", x
, 0, Size
= 7, Family
= wx
.MODERN
, Color
= (0,0,0))
920 self
.Canvas
.AddScaledText("DECORATIVE Font", x
, -10, Size
= 7, Family
= wx
.DECORATIVE
, Color
= (0,0,1))
921 self
.Canvas
.AddScaledText("ROMAN Font", x
, -20, Size
= 7, Family
= wx
.ROMAN
)
922 self
.Canvas
.AddScaledText("SCRIPT Font", x
, -30, Size
= 7, Family
= wx
.SCRIPT
)
923 self
.Canvas
.AddScaledText("ROMAN BOLD Font", x
, -40, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
924 self
.Canvas
.AddScaledText("ROMAN ITALIC BOLD Font", x
, -50, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
925 Canvas
.AddPointSet((x
,0), Color
= "White", Diameter
= 4)
928 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
930 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
931 T
= self
.Canvas
.AddScaledText("zapf chancery Font", x
, y
, Size
= 20, Font
= Font
, Position
= 'bc')
934 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "bookman")
935 T
= self
.Canvas
.AddScaledText("Bookman Font", x
, y
, Size
= 8, Font
= Font
)
937 self
.Canvas
.ZoomToBB()
939 def DrawMap(self
,event
= None):
942 self
.BindAllMouseEvents()
944 ## Test of Actual Map Data
945 self
.Canvas
.ClearAll()
946 self
.Canvas
.SetProjectionFun("FlatEarth")
947 #start = time.clock()
948 Shorelines
= Read_MapGen(os
.path
.join("data",'world.dat'),stats
= 0)
949 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
950 #start = time.clock()
951 for segment
in Shorelines
:
952 self
.Canvas
.AddLine(segment
)
953 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
954 #start = time.clock()
955 self
.Canvas
.ZoomToBB()
956 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
959 def LineTest(self
,event
= None):
965 ## Test of drawing lots of lines
968 Canvas
.SetProjectionFun(None)
969 #start = time.clock()
973 for i
in range(2000):
974 points
= (random
.randint(Range
[0],Range
[1]),
975 random
.randint(Range
[0],Range
[1]),
976 random
.randint(Range
[0],Range
[1]),
977 random
.randint(Range
[0],Range
[1]))
978 linepoints
.append(points
)
979 linewidths
.append(random
.randint(1,10) )
980 linecolors
.append(random
.randint(0,len(colors
)-1) )
981 for (points
,color
,width
) in zip(linepoints
,linecolors
,linewidths
):
982 Canvas
.AddLine((points
[0:2],points
[2:4]), LineWidth
= width
, LineColor
= colors
[color
])
983 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
984 #start = time.clock()
986 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
988 def SpeedTest(self
,event
=None):
990 BigRange
= (-1000,1000)
993 self
.UnBindAllMouseEvents()
997 Canvas
.SetProjectionFun(None)
1001 for i
in range(1000):
1002 x
,y
= (random
.uniform(BigRange
[0],BigRange
[1]),random
.uniform(BigRange
[0],BigRange
[1]))
1003 coords
.append( (x
,y
) )
1004 print "Drawing the Points"
1005 start
= time
.clock()
1006 for Point
in coords
:
1007 Canvas
.AddPoint(Point
, Diameter
= 4)
1008 print "It took %s seconds to add the points"%(time
.clock() - start
)
1011 def PropertiesChangeTest(self
,event
=None):
1015 colors
= self
.colors
1017 self
.UnBindAllMouseEvents()
1018 Canvas
= self
.Canvas
1021 Canvas
.SetProjectionFun(None)
1023 self
.ColorObjectsAll
= []
1024 self
.ColorObjectsLine
= []
1025 self
.ColorObjectsColor
= []
1026 self
.ColorObjectsText
= []
1027 ##One of each object:
1029 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1030 lw
= random
.randint(1,5)
1031 cf
= random
.randint(0,len(colors
)-1)
1032 h
= random
.randint(1,5)
1033 w
= random
.randint(1,5)
1034 self
.Rectangle
= Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
1035 self
.ColorObjectsAll
.append(self
.Rectangle
)
1038 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1039 lw
= random
.randint(1,5)
1040 cf
= random
.randint(0,len(colors
)-1)
1041 h
= random
.randint(1,5)
1042 w
= random
.randint(1,5)
1043 self
.Ellipse
= Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
1044 self
.ColorObjectsAll
.append(self
.Ellipse
)
1047 xy
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1048 D
= random
.randint(1,50)
1049 lw
= random
.randint(1,5)
1050 cf
= random
.randint(0,len(colors
)-1)
1051 cl
= random
.randint(0,len(colors
)-1)
1052 self
.ColorObjectsColor
.append(Canvas
.AddPoint(xy
, colors
[cf
], D
))
1055 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1056 D
= random
.randint(1,5)
1057 lw
= random
.randint(1,5)
1058 cf
= random
.randint(0,len(colors
)-1)
1059 cl
= random
.randint(0,len(colors
)-1)
1060 self
.Circle
= Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
1061 self
.ColorObjectsAll
.append(self
.Circle
)
1065 for j
in range(random
.randint(2,10)):
1066 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
1067 points
.append(point
)
1068 lw
= random
.randint(1,10)
1069 cf
= random
.randint(0,len(colors
)-1)
1070 cl
= random
.randint(0,len(colors
)-1)
1071 self
.ColorObjectsLine
.append(Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
]))
1075 ## for j in range(random.randint(2,6)):
1076 ## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1077 ## points.append(point)
1078 points
= RandomArray
.uniform(Range
[0],Range
[1],(6,2))
1079 lw
= random
.randint(1,6)
1080 cf
= random
.randint(0,len(colors
)-1)
1081 cl
= random
.randint(0,len(colors
)-1)
1082 self
.ColorObjectsAll
.append(Canvas
.AddPolygon(points
,
1084 LineColor
= colors
[cl
],
1085 FillColor
= colors
[cf
],
1086 FillStyle
= 'Solid'))
1089 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
1090 cf
= random
.randint(0,len(colors
)-1)
1091 D
= random
.randint(1,4)
1092 self
.PointSet
= Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
1093 self
.ColorObjectsColor
.append(self
.PointSet
)
1096 point
= RandomArray
.uniform(Range
[0],Range
[1],(2,))
1097 cf
= random
.randint(0,len(colors
)-1)
1098 D
= random
.randint(1,4)
1099 self
.Point
= Canvas
.AddPoint(point
, Color
= colors
[cf
], Diameter
= D
)
1100 self
.ColorObjectsColor
.append(self
.Point
)
1103 String
= "Unscaled text"
1104 ts
= random
.randint(10,40)
1105 cf
= random
.randint(0,len(colors
)-1)
1106 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1107 self
.ColorObjectsText
.append(Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1110 String
= "Scaled text"
1111 ts
= random
.random()*3 + 0.2
1112 cf
= random
.randint(0,len(colors
)-1)
1113 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1114 self
.ColorObjectsText
.append(Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1117 Button
= Canvas
.AddRectangle(-10, -12, 20, 3, LineStyle
= None, FillColor
= "Red")
1118 Canvas
.AddScaledText("Click Here To Change Properties",
1124 Button
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.ChangeProperties
)
1128 def ChangeProperties(self
, Object
= None):
1129 colors
= self
.colors
1132 for Object
in self
.ColorObjectsAll
:
1134 Object
.SetFillColor(colors
[random
.randint(0,len(colors
)-1)])
1135 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1136 Object
.SetLineWidth(random
.randint(1,7))
1137 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1138 for Object
in self
.ColorObjectsLine
:
1139 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1140 Object
.SetLineWidth(random
.randint(1,7))
1141 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1142 for Object
in self
.ColorObjectsColor
:
1143 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1144 for Object
in self
.ColorObjectsText
:
1145 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1146 Object
.SetBackgroundColor(colors
[random
.randint(0,len(colors
)-1)])
1147 self
.Circle
.SetDiameter(random
.randint(1,10))
1148 self
.PointSet
.SetDiameter(random
.randint(1,8))
1149 self
.Point
.SetDiameter(random
.randint(1,8))
1150 for Object
in (self
.Rectangle
, self
.Ellipse
):
1151 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1152 w
,h
= random
.randint(1,5), random
.randint(1,5)
1153 Object
.SetShape(x
,y
,w
,h
)
1155 self
.Canvas
.Draw(Force
= True)
1157 def TempTest(self
, event
= None):
1160 self
.UnBindAllMouseEvents()
1161 Canvas
= self
.Canvas
1163 Canvas
.SetProjectionFun(None)
1167 # Create a random Polygon
1170 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1171 points
.append(point
)
1172 Poly
= Canvas
.AddPolygon(points
,
1174 LineColor
= "Black",
1175 FillColor
= "LightBlue",
1176 FillStyle
= 'Solid')
1178 Poly
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPoly
)
1180 self
.SelectedPoly
= None
1181 self
.SelectPoints
= []
1182 self
.SelectedPoint
= None
1186 def SelectPoly(self
, Object
):
1187 print "In SelectPoly"
1188 Canvas
= self
.Canvas
1189 if Object
is self
.SelectedPoly
:
1192 #fixme: Do something to unselect the old one
1193 self
.SelectedPoly
= Object
1194 Canvas
.RemoveObjects(self
.SelectPoints
)
1195 self
.SelectPoints
= []
1196 # Draw points on the Vertices of the Selected Poly:
1197 for i
, point
in enumerate(Object
.Points
):
1198 P
= Canvas
.AddPointSet(point
, Diameter
= 6, Color
= "Red")
1200 P
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPointHit
)
1201 self
.SelectPoints
.append(P
)
1205 def SelectPointHit(self
, Point
):
1206 print "Point Num: %i Hit"%Point
.VerticeNum
1207 self
.SelectedPoint
= Point
1211 class DemoApp(wx
.App
):
1215 Under the Draw menu, there are three options:
1217 *Draw Test: will put up a picture of a bunch of randomly generated
1218 objects, of each kind supported.
1220 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1221 with a lot of data, and will take a while to load and draw (about 10 sec
1222 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1223 performance is not very good for large drawings.
1225 *Clear: Clears the Canvas.
1227 Once you have a picture drawn, you can zoom in and out and move about
1228 the picture. There is a tool bar with three tools that can be
1231 The magnifying glass with the plus is the zoom in tool. Once selected,
1232 if you click the image, it will zoom in, centered on where you
1233 clicked. If you click and drag the mouse, you will get a rubber band
1234 box, and the image will zoom to fit that box when you release it.
1236 The magnifying glass with the minus is the zoom out tool. Once selected,
1237 if you click the image, it will zoom out, centered on where you
1238 clicked. (note that this takes a while when you are looking at the map,
1239 as it has a LOT of lines to be drawn. The image is double buffered, so
1240 you don't see the drawing in progress)
1242 The hand is the move tool. Once selected, if you click and drag on the
1243 image, it will move so that the part you clicked on ends up where you
1244 release the mouse. Nothing is changed while you are dragging. The
1245 drawing is too slow for that.
1247 I'd like the cursor to change as you change tools, but the stock
1248 wxCursors didn't include anything I liked, so I stuck with the
1249 pointer. Please let me know if you have any nice cursor images for me to
1253 Any bugs, comments, feedback, questions, and especially code are welcome:
1257 Chris.Barker@noaa.gov
1261 def __init__(self
, *args
, **kwargs
):
1262 wx
.App
.__init
__(self
, *args
, **kwargs
)
1265 wx
.InitAllImageHandlers()
1266 frame
= DrawFrame(None, -1, "FloatCanvas Demo App",wx
.DefaultPosition
,(700,700))
1268 self
.SetTopWindow(frame
)
1271 ## check to see if the demo is set to start in a particular mode.
1272 if StartUpDemo
== "text":
1274 if StartUpDemo
== "stext":
1275 frame
.TestScaledText()
1276 elif StartUpDemo
== "all":
1278 elif StartUpDemo
== "map":
1280 elif StartUpDemo
== "hit":
1282 elif StartUpDemo
== "hitf":
1283 "starting TestHitTestForeground"
1284 frame
.TestHitTestForeground()
1285 elif StartUpDemo
== "animate":
1286 "starting TestAnimation"
1287 frame
.TestAnimation()
1288 elif StartUpDemo
== "speed":
1289 "starting SpeedTest"
1291 elif StartUpDemo
== "temp":
1292 "starting temp Test"
1294 elif StartUpDemo
== "props":
1295 "starting PropertiesChange Test"
1296 frame
.PropertiesChangeTest()
1300 def Read_MapGen(filename
,stats
= 0,AllLines
=0):
1302 This function reads a MapGen Format file, and
1303 returns a list of NumPy arrays with the line segments in them.
1305 Each NumPy array in the list is an NX2 array of Python Floats.
1307 The demo should have come with a file, "world.dat" that is the
1308 shorelines of the whole world, in MapGen format.
1312 file = open(filename
,'rt')
1313 data
= file.readlines()
1314 data
= map(string
.strip
,data
)
1320 if line
== "# -b": #New segment beginning
1321 if segment
: Shorelines
.append(Numeric
.array(segment
))
1324 segment
.append(map(float,string
.split(line
)))
1325 if segment
: Shorelines
.append(Numeric
.array(segment
))
1328 NumSegments
= len(Shorelines
)
1330 for segment
in Shorelines
:
1331 NumPoints
= NumPoints
+ len(segment
)
1332 AvgPoints
= NumPoints
/ NumSegments
1333 print "Number of Segments: ", NumSegments
1334 print "Average Number of Points per segment: ",AvgPoints
1337 for segment
in Shorelines
:
1338 Lines
.append(segment
[0])
1339 for point
in segment
[1:-1]:
1342 Lines
.append(segment
[-1])
1347 ## for the wxPython demo:
1350 except ImportError: # if it's not there locally, try the wxPython lib.
1351 from wx
.lib
import floatcanvas
1353 overview
= floatcanvas
.__doc
__
1355 if __name__
== "__main__":
1359 app
= DemoApp(False)# put in True if you want output to go to it's own window.