]>
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
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 def runTest(frame
, nb
, log
):
65 This method is used by the wxPython Demo Framework for integrating
66 this demo with the rest.
68 win
= DrawFrame(None, -1, "FloatCanvas Drawing Window",wx
.DefaultPosition
,(500,500))
74 from floatcanvas
import NavCanvas
, FloatCanvas
75 except ImportError: # if it's not there locally, try the wxPython lib.
76 from wx
.lib
.floatcanvas
import NavCanvas
, FloatCanvas
78 import wxPython
.lib
.colourdb
80 class DrawFrame(wx
.Frame
):
83 A frame used for the FloatCanvas Demo
88 def __init__(self
,parent
, id,title
,position
,size
):
89 wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
)
92 MenuBar
= wx
.MenuBar()
95 item
= file_menu
.Append(-1, "&Close","Close this frame")
96 self
.Bind(wx
.EVT_MENU
, self
.OnQuit
, item
)
97 MenuBar
.Append(file_menu
, "&File")
101 item
= draw_menu
.Append(-1, "&Draw Test","Run a test of drawing random components")
102 self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, item
)
104 item
= draw_menu
.Append(-1, "&Line Test","Run a test of drawing random lines")
105 self
.Bind(wx
.EVT_MENU
, self
.LineTest
, item
)
107 item
= draw_menu
.Append(-1, "Draw &Map","Run a test of drawing a map")
108 self
.Bind(wx
.EVT_MENU
, self
.DrawMap
, item
)
109 item
= draw_menu
.Append(-1, "&Text Test","Run a test of text drawing")
110 self
.Bind(wx
.EVT_MENU
, self
.TestText
, item
)
111 item
= draw_menu
.Append(-1, "&ScaledText Test","Run a test of text drawing")
112 self
.Bind(wx
.EVT_MENU
, self
.TestScaledText
, item
)
113 item
= draw_menu
.Append(-1, "&Clear","Clear the Canvas")
114 self
.Bind(wx
.EVT_MENU
, self
.Clear
, item
)
115 item
= draw_menu
.Append(-1, "&Hit Test","Run a test of the hit test code")
116 self
.Bind(wx
.EVT_MENU
, self
.TestHitTest
, item
)
117 item
= draw_menu
.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
118 self
.Bind(wx
.EVT_MENU
, self
.TestHitTestForeground
, item
)
119 item
= draw_menu
.Append(-1, "&Animation","Run a test of Animation")
120 self
.Bind(wx
.EVT_MENU
, self
.TestAnimation
, item
)
121 item
= draw_menu
.Append(-1, "&Speed","Run a test of Drawing Speed")
122 self
.Bind(wx
.EVT_MENU
, self
.SpeedTest
, item
)
123 item
= draw_menu
.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
124 self
.Bind(wx
.EVT_MENU
, self
.PropertiesChangeTest
, item
)
125 MenuBar
.Append(draw_menu
, "&Tests")
127 view_menu
= wx
.Menu()
128 item
= view_menu
.Append(-1, "Zoom to &Fit","Zoom to fit the window")
129 self
.Bind(wx
.EVT_MENU
, self
.ZoomToFit
, item
)
130 MenuBar
.Append(view_menu
, "&View")
132 help_menu
= wx
.Menu()
133 item
= help_menu
.Append(-1, "&About",
134 "More information About this program")
135 self
.Bind(wx
.EVT_MENU
, self
.OnAbout
, item
)
136 MenuBar
.Append(help_menu
, "&Help")
138 self
.SetMenuBar(MenuBar
)
140 self
.CreateStatusBar()
142 self
.Canvas
= NavCanvas
.NavCanvas(self
,
146 BackgroundColor
= "DARK SLATE BLUE")
148 wx
.EVT_CLOSE(self
, self
.OnCloseWindow
)
150 FloatCanvas
.EVT_MOTION(self
.Canvas
, self
.OnMove
)
151 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
153 self
.EventsAreBound
= False
155 ## getting all the colors and linestyles for random objects
156 wxPython
.lib
.colourdb
.updateColourDB()
157 self
.colors
= wxPython
.lib
.colourdb
.getColourList()
158 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
163 def BindAllMouseEvents(self
):
164 if not self
.EventsAreBound
:
165 ## Here is how you catch FloatCanvas mouse events
166 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, self
.OnLeftDown
)
167 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, self
.OnLeftUp
)
168 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, self
.OnLeftDouble
)
170 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, self
.OnMiddleDown
)
171 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, self
.OnMiddleUp
)
172 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, self
.OnMiddleDouble
)
174 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, self
.OnRightDown
)
175 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, self
.OnRightUp
)
176 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, self
.OnRightDouble
)
178 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, self
.OnWheel
)
179 self
.EventsAreBound
= True
181 def UnBindAllMouseEvents(self
):
182 ## Here is how you catch FloatCanvas mouse events
183 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, None )
184 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, None )
185 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, None)
187 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
188 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
189 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
191 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
192 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
193 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
195 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, None )
197 self
.EventsAreBound
= False
199 def PrintCoords(self
,event
):
200 print "coords are: %s"%(event
.Coords
,)
201 print "pixel coords are: %s\n"%(event
.GetPosition(),)
203 def OnLeftDown(self
, event
):
204 print "Left Button has been clicked in DrawFrame"
205 self
.PrintCoords(event
)
207 def OnLeftUp(self
, event
):
208 print "Left up in DrawFrame"
209 self
.PrintCoords(event
)
211 def OnLeftDouble(self
, event
):
212 print "Left Double Click in DrawFrame"
213 self
.PrintCoords(event
)
215 def OnMiddleDown(self
, event
):
216 print "Middle Button clicked in DrawFrame"
217 self
.PrintCoords(event
)
219 def OnMiddleUp(self
, event
):
220 print "Middle Button Up in DrawFrame"
221 self
.PrintCoords(event
)
223 def OnMiddleDouble(self
, event
):
224 print "Middle Button Double clicked in DrawFrame"
225 self
.PrintCoords(event
)
227 def OnRightDown(self
, event
):
228 print "Right Button has been clicked in DrawFrame"
229 self
.PrintCoords(event
)
231 def OnRightUp(self
, event
):
232 print "Right Button Up in DrawFrame"
233 self
.PrintCoords(event
)
235 def OnRightDouble(self
, event
):
236 print "Right Button Double clicked in DrawFrame"
237 self
.PrintCoords(event
)
239 def OnWheel(self
, event
):
240 print "Mouse Wheel Moved in DrawFrame"
241 self
.PrintCoords(event
)
243 def OnMove(self
, event
):
245 Updates the staus bar with the world coordinates
247 self
.SetStatusText("%.2f, %.2f"%tuple(event
.Coords
))
249 def OnAbout(self
, event
):
250 print "OnAbout called"
252 dlg
= wx
.MessageDialog(self
, "This is a small program to demonstrate\n"
253 "the use of the FloatCanvas\n",
254 "About Me", wx
.OK | wx
.ICON_INFORMATION
)
258 def ZoomToFit(self
,event
):
259 self
.Canvas
.ZoomToBB()
261 def Clear(self
,event
= None):
262 self
.UnBindAllMouseEvents()
263 self
.Canvas
.ClearAll()
264 self
.Canvas
.SetProjectionFun(None)
267 def OnQuit(self
,event
):
270 def OnCloseWindow(self
, event
):
273 def DrawTest(self
,event
=None):
280 self
.BindAllMouseEvents()
284 Canvas
.SetProjectionFun(None)
286 ## Random tests of everything:
290 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
291 lw
= random
.randint(1,5)
292 cf
= random
.randint(0,len(colors
)-1)
293 h
= random
.randint(1,5)
294 w
= random
.randint(1,5)
295 Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
299 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
300 lw
= random
.randint(1,5)
301 cf
= random
.randint(0,len(colors
)-1)
302 h
= random
.randint(1,5)
303 w
= random
.randint(1,5)
304 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
308 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
309 D
= random
.randint(1,50)
310 cf
= random
.randint(0,len(colors
)-1)
311 Canvas
.AddPoint((x
,y
), Color
= colors
[cf
], Diameter
= D
)
315 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
316 D
= random
.randint(1,5)
317 lw
= random
.randint(1,5)
318 cf
= random
.randint(0,len(colors
)-1)
319 cl
= random
.randint(0,len(colors
)-1)
320 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
321 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
326 for j
in range(random
.randint(2,10)):
327 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
329 lw
= random
.randint(1,10)
330 cf
= random
.randint(0,len(colors
)-1)
331 cl
= random
.randint(0,len(colors
)-1)
332 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
337 for j
in range(random
.randint(2,6)):
338 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
340 lw
= random
.randint(1,6)
341 cf
= random
.randint(0,len(colors
)-1)
342 cl
= random
.randint(0,len(colors
)-1)
343 Canvas
.AddPolygon(points
,
345 LineColor
= colors
[cl
],
346 FillColor
= colors
[cf
],
352 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
353 cf
= random
.randint(0,len(colors
)-1)
354 D
= random
.randint(1,4)
355 Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
358 String
= "Unscaled text"
360 ts
= random
.randint(10,40)
361 cf
= random
.randint(0,len(colors
)-1)
362 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
363 Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
366 String
= "Scaled text"
368 ts
= random
.random()*3 + 0.2
369 cf
= random
.randint(0,len(colors
)-1)
370 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
371 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
375 def TestAnimation(self
,event
=None):
378 In this test, a relatively complex background is drawn, and
379 a simple object placed in the foreground is moved over
380 it. This demonstrates how to use the InForeground attribute
381 to make an object in the foregorund draw fast, without
382 having to re-draw the whole background.
389 self
.UnBindAllMouseEvents()
393 Canvas
.SetProjectionFun(None)
395 ## Random tests of everything:
399 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
400 lw
= random
.randint(1,5)
401 cf
= random
.randint(0,len(colors
)-1)
402 h
= random
.randint(1,5)
403 w
= random
.randint(1,5)
404 Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
408 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
409 lw
= random
.randint(1,5)
410 cf
= random
.randint(0,len(colors
)-1)
411 h
= random
.randint(1,5)
412 w
= random
.randint(1,5)
413 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
417 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
418 D
= random
.randint(1,5)
419 lw
= random
.randint(1,5)
420 cf
= random
.randint(0,len(colors
)-1)
421 cl
= random
.randint(0,len(colors
)-1)
422 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
423 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
428 for j
in range(random
.randint(2,10)):
429 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
431 lw
= random
.randint(1,10)
432 cf
= random
.randint(0,len(colors
)-1)
433 cl
= random
.randint(0,len(colors
)-1)
434 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
439 for j
in range(random
.randint(2,6)):
440 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
442 lw
= random
.randint(1,6)
443 cf
= random
.randint(0,len(colors
)-1)
444 cl
= random
.randint(0,len(colors
)-1)
445 Canvas
.AddPolygon(points
,
447 LineColor
= colors
[cl
],
448 FillColor
= colors
[cf
],
452 String
= "Scaled text"
454 ts
= random
.random()*3 + 0.2
455 cf
= random
.randint(0,len(colors
)-1)
456 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
457 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
460 # Now the Foreground Object:
461 C
= Canvas
.AddCircle(0,0,7,LineWidth
= 2,LineColor
= "Black",FillColor
= "Red", InForeground
= True)
462 T
= Canvas
.AddScaledText("Click to Move",0,0, Size
= 0.6, Position
= 'cc', InForeground
= True)
463 C
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.MoveMe
)
466 self
.Timer
= wx
.PyTimer(self
.ShowFrame
)
467 self
.FrameDelay
= 50 # milliseconds
472 Object
= self
.MovingObject
474 if self
.TimeStep
< self
.NumTimeSteps
:
476 if x
> Range
[1] or x
< Range
[0]:
478 if y
> Range
[1] or y
< Range
[0]:
480 Object
.Move( (self
.dx
,self
.dy
) )
481 Object
.Text
.Move( (self
.dx
,self
.dy
))
484 wx
.GetApp().Yield(True)
489 def MoveMe(self
, Object
):
490 self
.MovingObject
= Object
492 self
.dx
= random
.uniform(Range
[0]/4,Range
[1]/4)
493 self
.dy
= random
.uniform(Range
[0]/4,Range
[1]/4)
496 self
.NumTimeSteps
= 200
498 self
.Timer
.Start(self
.FrameDelay
)
499 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
501 def TestHitTest(self
,event
=None):
504 self
.UnBindAllMouseEvents()
508 Canvas
.SetProjectionFun(None)
510 #Add a HitAble rectangle
518 #Add one that is not HitAble
519 Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
520 Canvas
.AddText("Not Hit-able", x
, y
, Size
= FontSize
, Position
= "bl")
524 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
525 R
.Name
= "Line Rectangle"
527 R
.HitLineWidth
= 5 # Makes it a little easier to hit
528 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
529 Canvas
.AddText("Left Click Line", x
, y
, Size
= FontSize
, Position
= "bl")
530 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
534 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
535 R
.Name
= color
+ "Rectangle"
536 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
537 Canvas
.AddText("Left Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
538 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
543 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
544 R
.Name
= color
+ " Rectangle"
545 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
546 Canvas
.AddText("Right Click Fill", x
, y
, Position
= "bl")
547 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
551 R
= Canvas
.AddEllipse(x
, y
, w
, h
,LineWidth
= 2,FillColor
= color
)
552 R
.Name
= color
+" Ellipse"
553 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
554 Canvas
.AddText("Right Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
555 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
559 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2, FillColor
= color
)
560 R
.Name
= color
+ " Circle"
562 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DCLICK
, self
.RectGotHit
)
563 Canvas
.AddText("Left D-Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
564 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
569 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2,FillColor
= color
)
570 R
.Name
= color
+ " Circle"
571 R
.Bind(FloatCanvas
.EVT_FC_LEFT_UP
, self
.RectGotHit
)
572 Canvas
.AddText("Left Up Fill", x
, y
, Size
= FontSize
, Position
= "bl")
573 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
577 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
578 R
.Name
= color
+ " Rectangle"
579 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DOWN
, self
.RectGotHit
)
580 Canvas
.AddText("Middle Down", x
, y
, Size
= FontSize
, Position
= "bl")
581 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
585 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
586 R
.Name
= color
+ " Rectangle"
587 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_UP
, self
.RectGotHit
)
588 Canvas
.AddText("Middle Up", x
, y
, Size
= FontSize
, Position
= "bl")
589 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
594 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
595 R
.Name
= color
+ " Rectangle"
596 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DCLICK
, self
.RectGotHit
)
597 Canvas
.AddText("Middle DoubleClick", x
, y
, Size
= FontSize
, Position
= "bl")
598 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
602 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
603 R
.Name
= color
+ " Rectangle"
604 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_UP
, self
.RectGotHit
)
605 Canvas
.AddText("Right Up", x
, y
, Size
= FontSize
, Position
= "bl")
606 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
610 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
611 R
.Name
= color
+ " Rectangle"
612 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DCLICK
, self
.RectGotHit
)
613 Canvas
.AddText("Right Double Click", x
, y
, Size
= FontSize
, Position
= "bl")
614 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
618 color
= "MEDIUM GOLDENROD"
619 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
621 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
622 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
623 Canvas
.AddText("L and R Click", x
, y
, Size
= FontSize
, Position
= "bl")
624 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
628 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
629 R
.Name
= color
+ " Rectangle"
630 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
631 Canvas
.AddText("Mouse Enter", x
, y
, Size
= FontSize
, Position
= "bl")
632 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
635 color
= "MEDIUM VIOLET RED"
636 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
638 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
639 Canvas
.AddText("Mouse Leave", x
, y
, Size
= FontSize
, Position
= "bl")
640 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
645 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
647 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
648 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
649 Canvas
.AddText("Enter and Leave", x
, y
, Size
= FontSize
, Position
= "bl")
650 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
654 R
= Canvas
.AddRectangle(x
, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
656 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
657 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
658 Canvas
.AddText("Mouse Enter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
659 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
663 R
= Canvas
.AddRectangle(x
-12, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
665 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
666 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
667 Canvas
.AddText("Mouse ENter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
668 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
672 L
= Canvas
.AddLine(( (x
, y
), (x
+10, y
+10), (x
+w
, y
+h
) ), LineWidth
= 2, LineColor
= "Red")
674 L
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
675 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
676 Canvas
.AddText(L
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
680 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
)
681 R
= Canvas
.AddPolygon(Points
, LineWidth
= 2, FillColor
= color
)
682 R
.Name
= color
+ " Polygon"
683 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
684 Canvas
.AddText("RIGHT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
685 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
689 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
)
690 R
= Canvas
.AddPointSet(Points
, Diameter
= 4, Color
= color
)
692 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.PointSetGotHit
)
693 Canvas
.AddText("LEFT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
694 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
698 T
= Canvas
.AddText("Hit-able Text", x
, y
, Size
= 15, Color
= "Red", Position
= 'tl')
699 T
.Name
= "Hit-able Text"
700 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
701 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
704 T
= Canvas
.AddScaledText("Scaled Text", x
, y
, Size
= 1./2*h
, Color
= "Pink", Position
= 'bl')
705 Canvas
.AddPointSet( (x
, y
), Diameter
= 3)
706 T
.Name
= "Scaled Text"
707 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
708 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "tl")
710 self
.Canvas
.ZoomToBB()
712 def TestHitTestForeground(self
,event
=None):
715 self
.UnBindAllMouseEvents()
719 Canvas
.SetProjectionFun(None)
721 #Add a Hitable rectangle
729 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
, InForeground
= False)
730 R
.Name
= color
+ "Rectangle"
732 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
733 Canvas
.AddText("Left Click Fill", x
, y
, Position
= "bl")
734 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
736 ## A set of Rectangles that move together
738 ## NOTE: In a real app, it might be better to create a new
739 ## custom FloatCanvas DrawObject
741 self
.MovingRects
= []
744 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
746 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveLeft
)
747 L
= Canvas
.AddText("Left", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
748 self
.MovingRects
.extend( (R
,L
) )
751 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
753 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveRight
)
754 L
= Canvas
.AddText("Right", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
755 self
.MovingRects
.extend( (R
,L
) )
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
.RectMoveUp
)
762 L
= Canvas
.AddText("Up", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
763 self
.MovingRects
.extend( (R
,L
) )
767 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
769 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveDown
)
770 L
= Canvas
.AddText("Down", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
771 self
.MovingRects
.extend( (R
,L
) )
773 self
.Canvas
.ZoomToBB()
775 def RectMoveLeft(self
,Object
):
776 self
.MoveRects("left")
778 def RectMoveRight(self
,Object
):
779 self
.MoveRects("right")
781 def RectMoveUp(self
,Object
):
784 def RectMoveDown(self
,Object
):
785 self
.MoveRects("down")
787 def MoveRects(self
, Dir
):
788 for Object
in self
.MovingRects
:
790 if Dir
== "left": X
-= 10
791 elif Dir
== "right": X
+= 10
792 elif Dir
== "up": Y
+= 10
793 elif Dir
== "down": Y
-= 10
798 def PointSetGotHit(self
, Object
):
799 print Object
.Name
, "Got Hit\n"
801 def RectGotHit(self
, Object
):
802 print Object
.Name
, "Got Hit\n"
804 def RectGotHitRight(self
, Object
):
805 print Object
.Name
, "Got Hit With Right\n"
807 def RectGotHitLeft(self
, Object
):
808 print Object
.Name
, "Got Hit with Left\n"
810 def RectMouseOver(self
, Object
):
811 print "Mouse entered:", Object
.Name
813 def RectMouseLeave(self
, Object
):
814 print "Mouse left ", Object
.Name
817 def TestText(self
, event
= None):
820 self
.BindAllMouseEvents()
823 Canvas
.SetProjectionFun(None)
827 ## Add a non-visible rectangle, just to get a Bounding Box
828 ## Text objects have a zero-size bounding box, because it changes with zoom
829 Canvas
.AddRectangle(-10,-10,20,20,LineWidth
= 1, LineColor
= None)
833 self
.Canvas
.AddText("Top Left",x
,y
,Size
= 14,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
834 self
.Canvas
.AddText("Bottom Left",x
,y
,Size
= 14,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
835 self
.Canvas
.AddText("Top Right",x
,y
,Size
= 14,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
836 self
.Canvas
.AddText("Bottom Right",x
,y
,Size
= 14,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
837 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
841 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
842 self
.Canvas
.AddText("Top Center",x
,y
,Size
= 14,Color
= "Black",Position
= "tc")
843 self
.Canvas
.AddText("Bottom Center",x
,y
,Size
= 14,Color
= "White",Position
= "bc")
847 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
848 self
.Canvas
.AddText("Center Right",x
,y
,Size
= 14,Color
= "Black",Position
= "cr")
849 self
.Canvas
.AddText("Center Left",x
,y
,Size
= 14,Color
= "Black",Position
= "cl")
853 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
854 self
.Canvas
.AddText("Center Center",x
,y
,Size
= 14,Color
= "Black",Position
= "cc")
856 self
.Canvas
.AddText("40 Pixels",-10,8,Size
= 40)
857 self
.Canvas
.AddText("20 Pixels",-10,5,Size
= 20)
858 self
.Canvas
.AddText("10 Pixels",-10,3,Size
= 10)
860 self
.Canvas
.AddText("MODERN Font", -10, 0, Family
= wx
.MODERN
)
861 self
.Canvas
.AddText("DECORATIVE Font", -10, -1, Family
= wx
.DECORATIVE
)
862 self
.Canvas
.AddText("ROMAN Font", -10, -2, Family
= wx
.ROMAN
)
863 self
.Canvas
.AddText("SCRIPT Font", -10, -3, Family
= wx
.SCRIPT
)
864 self
.Canvas
.AddText("ROMAN BOLD Font", -10, -4, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
865 self
.Canvas
.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
867 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
868 Font
= wx
.Font(20, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
869 self
.Canvas
.AddText("zapf chancery Font", -10, -6, Font
= Font
)
871 self
.Canvas
.ZoomToBB()
873 def TestScaledText(self
, event
= None):
876 self
.BindAllMouseEvents()
879 Canvas
.SetProjectionFun(None)
883 T
= Canvas
.AddScaledText("Top Left",x
,y
,Size
= 5,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
884 T
= Canvas
.AddScaledText("Bottom Left",x
,y
,Size
= 5,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
885 T
= Canvas
.AddScaledText("Top Right",x
,y
,Size
= 5,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
886 T
= Canvas
.AddScaledText("Bottom Right",x
,y
,Size
= 5,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
887 Canvas
.AddPointSet((x
,y
), Color
= "Red", Diameter
= 4)
892 Canvas
.AddScaledText("Top Center",x
,y
,Size
= 7,Color
= "Black",Position
= "tc")
893 Canvas
.AddScaledText("Bottom Center",x
,y
,Size
= 7,Color
= "White",Position
= "bc")
894 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
898 Canvas
.AddScaledText("Center Right",x
,y
,Size
= 9,Color
= "Black",Position
= "cr")
899 Canvas
.AddScaledText("Center Left",x
,y
,Size
= 9,Color
= "Black",Position
= "cl")
900 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
904 self
.Canvas
.AddScaledText("MODERN Font", x
, 0, Size
= 7, Family
= wx
.MODERN
, Color
= (0,0,0))
905 self
.Canvas
.AddScaledText("DECORATIVE Font", x
, -10, Size
= 7, Family
= wx
.DECORATIVE
, Color
= (0,0,1))
906 self
.Canvas
.AddScaledText("ROMAN Font", x
, -20, Size
= 7, Family
= wx
.ROMAN
)
907 self
.Canvas
.AddScaledText("SCRIPT Font", x
, -30, Size
= 7, Family
= wx
.SCRIPT
)
908 self
.Canvas
.AddScaledText("ROMAN BOLD Font", x
, -40, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
909 self
.Canvas
.AddScaledText("ROMAN ITALIC BOLD Font", x
, -50, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
910 Canvas
.AddPointSet((x
,0), Color
= "White", Diameter
= 4)
913 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
915 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
916 T
= self
.Canvas
.AddScaledText("zapf chancery Font", x
, y
, Size
= 20, Font
= Font
, Position
= 'bc')
919 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "bookman")
920 T
= self
.Canvas
.AddScaledText("Bookman Font", x
, y
, Size
= 8, Font
= Font
)
922 self
.Canvas
.ZoomToBB()
924 def DrawMap(self
,event
= None):
927 self
.BindAllMouseEvents()
929 ## Test of Actual Map Data
930 self
.Canvas
.ClearAll()
931 self
.Canvas
.SetProjectionFun("FlatEarth")
932 #start = time.clock()
933 Shorelines
= Read_MapGen(os
.path
.join("data",'world.dat'),stats
= 0)
934 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
935 #start = time.clock()
936 for segment
in Shorelines
:
937 self
.Canvas
.AddLine(segment
)
938 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
939 #start = time.clock()
940 self
.Canvas
.ZoomToBB()
941 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
944 def LineTest(self
,event
= None):
950 ## Test of drawing lots of lines
953 Canvas
.SetProjectionFun(None)
954 #start = time.clock()
958 for i
in range(2000):
959 points
= (random
.randint(Range
[0],Range
[1]),
960 random
.randint(Range
[0],Range
[1]),
961 random
.randint(Range
[0],Range
[1]),
962 random
.randint(Range
[0],Range
[1]))
963 linepoints
.append(points
)
964 linewidths
.append(random
.randint(1,10) )
965 linecolors
.append(random
.randint(0,len(colors
)-1) )
966 for (points
,color
,width
) in zip(linepoints
,linecolors
,linewidths
):
967 Canvas
.AddLine((points
[0:2],points
[2:4]), LineWidth
= width
, LineColor
= colors
[color
])
968 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
969 #start = time.clock()
971 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
973 def SpeedTest(self
,event
=None):
975 BigRange
= (-1000,1000)
978 self
.UnBindAllMouseEvents()
982 Canvas
.SetProjectionFun(None)
986 for i
in range(1000):
987 x
,y
= (random
.uniform(BigRange
[0],BigRange
[1]),random
.uniform(BigRange
[0],BigRange
[1]))
988 coords
.append( (x
,y
) )
989 print "Drawing the Points"
992 Canvas
.AddPoint(Point
, Diameter
= 4)
993 print "It took %s seconds to add the points"%(time
.clock() - start
)
996 def PropertiesChangeTest(self
,event
=None):
1000 colors
= self
.colors
1002 self
.UnBindAllMouseEvents()
1003 Canvas
= self
.Canvas
1006 Canvas
.SetProjectionFun(None)
1008 self
.ColorObjectsAll
= []
1009 self
.ColorObjectsLine
= []
1010 self
.ColorObjectsColor
= []
1011 self
.ColorObjectsText
= []
1012 ##One of each object:
1014 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1015 lw
= random
.randint(1,5)
1016 cf
= random
.randint(0,len(colors
)-1)
1017 h
= random
.randint(1,5)
1018 w
= random
.randint(1,5)
1019 self
.Rectangle
= Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
1020 self
.ColorObjectsAll
.append(self
.Rectangle
)
1023 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1024 lw
= random
.randint(1,5)
1025 cf
= random
.randint(0,len(colors
)-1)
1026 h
= random
.randint(1,5)
1027 w
= random
.randint(1,5)
1028 self
.Ellipse
= Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
1029 self
.ColorObjectsAll
.append(self
.Ellipse
)
1032 xy
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1033 D
= random
.randint(1,50)
1034 lw
= random
.randint(1,5)
1035 cf
= random
.randint(0,len(colors
)-1)
1036 cl
= random
.randint(0,len(colors
)-1)
1037 self
.ColorObjectsColor
.append(Canvas
.AddPoint(xy
, colors
[cf
], D
))
1040 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1041 D
= random
.randint(1,5)
1042 lw
= random
.randint(1,5)
1043 cf
= random
.randint(0,len(colors
)-1)
1044 cl
= random
.randint(0,len(colors
)-1)
1045 self
.Circle
= Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
1046 self
.ColorObjectsAll
.append(self
.Circle
)
1050 for j
in range(random
.randint(2,10)):
1051 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
1052 points
.append(point
)
1053 lw
= random
.randint(1,10)
1054 cf
= random
.randint(0,len(colors
)-1)
1055 cl
= random
.randint(0,len(colors
)-1)
1056 self
.ColorObjectsLine
.append(Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
]))
1060 ## for j in range(random.randint(2,6)):
1061 ## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1062 ## points.append(point)
1063 points
= RandomArray
.uniform(Range
[0],Range
[1],(6,2))
1064 lw
= random
.randint(1,6)
1065 cf
= random
.randint(0,len(colors
)-1)
1066 cl
= random
.randint(0,len(colors
)-1)
1067 self
.ColorObjectsAll
.append(Canvas
.AddPolygon(points
,
1069 LineColor
= colors
[cl
],
1070 FillColor
= colors
[cf
],
1071 FillStyle
= 'Solid'))
1074 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
1075 cf
= random
.randint(0,len(colors
)-1)
1076 D
= random
.randint(1,4)
1077 self
.PointSet
= Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
1078 self
.ColorObjectsColor
.append(self
.PointSet
)
1081 point
= RandomArray
.uniform(Range
[0],Range
[1],(2,))
1082 cf
= random
.randint(0,len(colors
)-1)
1083 D
= random
.randint(1,4)
1084 self
.Point
= Canvas
.AddPoint(point
, Color
= colors
[cf
], Diameter
= D
)
1085 self
.ColorObjectsColor
.append(self
.Point
)
1088 String
= "Unscaled text"
1089 ts
= random
.randint(10,40)
1090 cf
= random
.randint(0,len(colors
)-1)
1091 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1092 self
.ColorObjectsText
.append(Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1095 String
= "Scaled text"
1096 ts
= random
.random()*3 + 0.2
1097 cf
= random
.randint(0,len(colors
)-1)
1098 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1099 self
.ColorObjectsText
.append(Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1102 Button
= Canvas
.AddRectangle(-10, -12, 20, 3, LineStyle
= None, FillColor
= "Red")
1103 Canvas
.AddScaledText("Click Here To Change Properties",
1109 Button
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.ChangeProperties
)
1113 def ChangeProperties(self
, Object
= None):
1114 colors
= self
.colors
1117 for Object
in self
.ColorObjectsAll
:
1119 Object
.SetFillColor(colors
[random
.randint(0,len(colors
)-1)])
1120 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1121 Object
.SetLineWidth(random
.randint(1,7))
1122 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1123 for Object
in self
.ColorObjectsLine
:
1124 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1125 Object
.SetLineWidth(random
.randint(1,7))
1126 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1127 for Object
in self
.ColorObjectsColor
:
1128 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1129 for Object
in self
.ColorObjectsText
:
1130 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1131 Object
.SetBackgroundColor(colors
[random
.randint(0,len(colors
)-1)])
1132 self
.Circle
.SetDiameter(random
.randint(1,10))
1133 self
.PointSet
.SetDiameter(random
.randint(1,8))
1134 self
.Point
.SetDiameter(random
.randint(1,8))
1135 for Object
in (self
.Rectangle
, self
.Ellipse
):
1136 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1137 w
,h
= random
.randint(1,5), random
.randint(1,5)
1138 Object
.SetShape(x
,y
,w
,h
)
1140 self
.Canvas
.Draw(Force
= True)
1142 def TempTest(self
, event
= None):
1145 self
.UnBindAllMouseEvents()
1146 Canvas
= self
.Canvas
1148 Canvas
.SetProjectionFun(None)
1152 # Create a random Polygon
1155 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1156 points
.append(point
)
1157 Poly
= Canvas
.AddPolygon(points
,
1159 LineColor
= "Black",
1160 FillColor
= "LightBlue",
1161 FillStyle
= 'Solid')
1163 Poly
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPoly
)
1165 self
.SelectedPoly
= None
1166 self
.SelectPoints
= []
1167 self
.SelectedPoint
= None
1171 def SelectPoly(self
, Object
):
1172 print "In SelectPoly"
1173 Canvas
= self
.Canvas
1174 if Object
is self
.SelectedPoly
:
1177 #fixme: Do something to unselect the old one
1178 self
.SelectedPoly
= Object
1179 Canvas
.RemoveObjects(self
.SelectPoints
)
1180 self
.SelectPoints
= []
1181 # Draw points on the Vertices of the Selected Poly:
1182 for i
, point
in enumerate(Object
.Points
):
1183 P
= Canvas
.AddPointSet(point
, Diameter
= 6, Color
= "Red")
1185 P
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPointHit
)
1186 self
.SelectPoints
.append(P
)
1190 def SelectPointHit(self
, Point
):
1191 print "Point Num: %i Hit"%Point
.VerticeNum
1192 self
.SelectedPoint
= Point
1196 class DemoApp(wx
.App
):
1200 Under the Draw menu, there are three options:
1202 *Draw Test: will put up a picture of a bunch of randomly generated
1203 objects, of each kind supported.
1205 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1206 with a lot of data, and will take a while to load and draw (about 10 sec
1207 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1208 performance is not very good for large drawings.
1210 *Clear: Clears the Canvas.
1212 Once you have a picture drawn, you can zoom in and out and move about
1213 the picture. There is a tool bar with three tools that can be
1216 The magnifying glass with the plus is the zoom in tool. Once selected,
1217 if you click the image, it will zoom in, centered on where you
1218 clicked. If you click and drag the mouse, you will get a rubber band
1219 box, and the image will zoom to fit that box when you release it.
1221 The magnifying glass with the minus is the zoom out tool. Once selected,
1222 if you click the image, it will zoom out, centered on where you
1223 clicked. (note that this takes a while when you are looking at the map,
1224 as it has a LOT of lines to be drawn. The image is double buffered, so
1225 you don't see the drawing in progress)
1227 The hand is the move tool. Once selected, if you click and drag on the
1228 image, it will move so that the part you clicked on ends up where you
1229 release the mouse. Nothing is changed while you are dragging. The
1230 drawing is too slow for that.
1232 I'd like the cursor to change as you change tools, but the stock
1233 wxCursors didn't include anything I liked, so I stuck with the
1234 pointer. Please let me know if you have any nice cursor images for me to
1238 Any bugs, comments, feedback, questions, and especially code are welcome:
1242 Chris.Barker@noaa.gov
1246 def __init__(self
, *args
, **kwargs
):
1247 wx
.App
.__init
__(self
, *args
, **kwargs
)
1250 wx
.InitAllImageHandlers()
1251 frame
= DrawFrame(None, -1, "FloatCanvas Demo App",wx
.DefaultPosition
,(700,700))
1253 self
.SetTopWindow(frame
)
1256 ## check to see if the demo is set to start in a particular mode.
1257 if StartUpDemo
== "text":
1259 if StartUpDemo
== "stext":
1260 frame
.TestScaledText()
1261 elif StartUpDemo
== "all":
1263 elif StartUpDemo
== "map":
1265 elif StartUpDemo
== "hit":
1267 elif StartUpDemo
== "hitf":
1268 "starting TestHitTestForeground"
1269 frame
.TestHitTestForeground()
1270 elif StartUpDemo
== "animate":
1271 "starting TestAnimation"
1272 frame
.TestAnimation()
1273 elif StartUpDemo
== "speed":
1274 "starting SpeedTest"
1276 elif StartUpDemo
== "temp":
1277 "starting temp Test"
1279 elif StartUpDemo
== "props":
1280 "starting PropertiesChange Test"
1281 frame
.PropertiesChangeTest()
1285 def Read_MapGen(filename
,stats
= 0,AllLines
=0):
1287 This function reads a MapGen Format file, and
1288 returns a list of NumPy arrays with the line segments in them.
1290 Each NumPy array in the list is an NX2 array of Python Floats.
1292 The demo should have come with a file, "world.dat" that is the
1293 shorelines of the whole world, in MapGen format.
1297 file = open(filename
,'rt')
1298 data
= file.readlines()
1299 data
= map(string
.strip
,data
)
1305 if line
== "# -b": #New segment beginning
1306 if segment
: Shorelines
.append(Numeric
.array(segment
))
1309 segment
.append(map(float,string
.split(line
)))
1310 if segment
: Shorelines
.append(Numeric
.array(segment
))
1313 NumSegments
= len(Shorelines
)
1315 for segment
in Shorelines
:
1316 NumPoints
= NumPoints
+ len(segment
)
1317 AvgPoints
= NumPoints
/ NumSegments
1318 print "Number of Segments: ", NumSegments
1319 print "Average Number of Points per segment: ",AvgPoints
1322 for segment
in Shorelines
:
1323 Lines
.append(segment
[0])
1324 for point
in segment
[1:-1]:
1327 Lines
.append(segment
[-1])
1332 ## for the wxPython demo:
1335 except ImportError: # if it's not there locally, try the wxPython lib.
1336 from wx
.lib
import floatcanvas
1338 overview
= floatcanvas
.__doc
__
1340 if __name__
== "__main__":
1344 app
= DemoApp(False)# put in True if you want output to go to it's own window.