]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
77fbcb8c829ab395895a0ac105643bdc871b8ac6
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 if __name__
== "__main__": # parse options if run stand-alone
28 optlist
, args
= getopt
.getopt(sys
.argv
[1:],'l',["local","all","text","map","stext","hit","hitf","animate","speed","temp","props"])
33 elif opt
[0] == "--text":
35 elif opt
[0] == "--map":
37 elif opt
[0] == "--stext":
39 elif opt
[0] == "--hit":
41 elif opt
[0] == "--hitf":
43 elif opt
[0] == "--animate":
44 StartUpDemo
= "animate"
45 elif opt
[0] == "--speed":
47 elif opt
[0] == "--temp":
49 elif opt
[0] == "--props":
54 #---------------------------------------------------------------------------
56 class TestPanel(wx
.Panel
):
57 def __init__(self
, parent
, log
):
59 wx
.Panel
.__init
__(self
, parent
, -1)
61 b
= wx
.Button(self
, -1, "Show the FloatCanvas sample", (50,50))
62 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
65 def OnButton(self
, evt
):
67 dlg
= wx
.MessageDialog(self
, errorText
, 'Sorry', wx
.OK |
73 win
= DrawFrame(None, -1, "FloatCanvas Drawing Window",wx
.DefaultPosition
,(500,500))
79 #---------------------------------------------------------------------------
85 from floatcanvas
import NavCanvas
, FloatCanvas
86 except ImportError: # if it's not there locally, try the wxPython lib.
87 from wx
.lib
.floatcanvas
import NavCanvas
, FloatCanvas
89 import wxPython
.lib
.colourdb
91 class DrawFrame(wx
.Frame
):
94 A frame used for the FloatCanvas Demo
99 def __init__(self
,parent
, id,title
,position
,size
):
100 wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
)
102 ## Set up the MenuBar
103 MenuBar
= wx
.MenuBar()
105 file_menu
= wx
.Menu()
106 item
= file_menu
.Append(-1, "&Close","Close this frame")
107 self
.Bind(wx
.EVT_MENU
, self
.OnQuit
, item
)
108 MenuBar
.Append(file_menu
, "&File")
110 draw_menu
= wx
.Menu()
112 item
= draw_menu
.Append(-1, "&Draw Test","Run a test of drawing random components")
113 self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, item
)
115 item
= draw_menu
.Append(-1, "&Line Test","Run a test of drawing random lines")
116 self
.Bind(wx
.EVT_MENU
, self
.LineTest
, item
)
118 item
= draw_menu
.Append(-1, "Draw &Map","Run a test of drawing a map")
119 self
.Bind(wx
.EVT_MENU
, self
.DrawMap
, item
)
120 item
= draw_menu
.Append(-1, "&Text Test","Run a test of text drawing")
121 self
.Bind(wx
.EVT_MENU
, self
.TestText
, item
)
122 item
= draw_menu
.Append(-1, "&ScaledText Test","Run a test of text drawing")
123 self
.Bind(wx
.EVT_MENU
, self
.TestScaledText
, item
)
124 item
= draw_menu
.Append(-1, "&Clear","Clear the Canvas")
125 self
.Bind(wx
.EVT_MENU
, self
.Clear
, item
)
126 item
= draw_menu
.Append(-1, "&Hit Test","Run a test of the hit test code")
127 self
.Bind(wx
.EVT_MENU
, self
.TestHitTest
, item
)
128 item
= draw_menu
.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
129 self
.Bind(wx
.EVT_MENU
, self
.TestHitTestForeground
, item
)
130 item
= draw_menu
.Append(-1, "&Animation","Run a test of Animation")
131 self
.Bind(wx
.EVT_MENU
, self
.TestAnimation
, item
)
132 item
= draw_menu
.Append(-1, "&Speed","Run a test of Drawing Speed")
133 self
.Bind(wx
.EVT_MENU
, self
.SpeedTest
, item
)
134 item
= draw_menu
.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
135 self
.Bind(wx
.EVT_MENU
, self
.PropertiesChangeTest
, item
)
136 MenuBar
.Append(draw_menu
, "&Tests")
138 view_menu
= wx
.Menu()
139 item
= view_menu
.Append(-1, "Zoom to &Fit","Zoom to fit the window")
140 self
.Bind(wx
.EVT_MENU
, self
.ZoomToFit
, item
)
141 MenuBar
.Append(view_menu
, "&View")
143 help_menu
= wx
.Menu()
144 item
= help_menu
.Append(-1, "&About",
145 "More information About this program")
146 self
.Bind(wx
.EVT_MENU
, self
.OnAbout
, item
)
147 MenuBar
.Append(help_menu
, "&Help")
149 self
.SetMenuBar(MenuBar
)
151 self
.CreateStatusBar()
153 self
.Canvas
= NavCanvas
.NavCanvas(self
,
157 BackgroundColor
= "DARK SLATE BLUE")
159 wx
.EVT_CLOSE(self
, self
.OnCloseWindow
)
161 FloatCanvas
.EVT_MOTION(self
.Canvas
, self
.OnMove
)
162 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
164 self
.EventsAreBound
= False
166 ## getting all the colors and linestyles for random objects
167 wxPython
.lib
.colourdb
.updateColourDB()
168 self
.colors
= wxPython
.lib
.colourdb
.getColourList()
169 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
174 def BindAllMouseEvents(self
):
175 if not self
.EventsAreBound
:
176 ## Here is how you catch FloatCanvas mouse events
177 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, self
.OnLeftDown
)
178 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, self
.OnLeftUp
)
179 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, self
.OnLeftDouble
)
181 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, self
.OnMiddleDown
)
182 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, self
.OnMiddleUp
)
183 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, self
.OnMiddleDouble
)
185 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, self
.OnRightDown
)
186 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, self
.OnRightUp
)
187 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, self
.OnRightDouble
)
189 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, self
.OnWheel
)
190 self
.EventsAreBound
= True
192 def UnBindAllMouseEvents(self
):
193 ## Here is how you catch FloatCanvas mouse events
194 FloatCanvas
.EVT_LEFT_DOWN(self
.Canvas
, None )
195 FloatCanvas
.EVT_LEFT_UP(self
.Canvas
, None )
196 FloatCanvas
.EVT_LEFT_DCLICK(self
.Canvas
, None)
198 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
199 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
200 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
202 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
203 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
204 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
206 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, None )
208 self
.EventsAreBound
= False
210 def PrintCoords(self
,event
):
211 print "coords are: %s"%(event
.Coords
,)
212 print "pixel coords are: %s\n"%(event
.GetPosition(),)
214 def OnLeftDown(self
, event
):
215 print "Left Button has been clicked in DrawFrame"
216 self
.PrintCoords(event
)
218 def OnLeftUp(self
, event
):
219 print "Left up in DrawFrame"
220 self
.PrintCoords(event
)
222 def OnLeftDouble(self
, event
):
223 print "Left Double Click in DrawFrame"
224 self
.PrintCoords(event
)
226 def OnMiddleDown(self
, event
):
227 print "Middle Button clicked in DrawFrame"
228 self
.PrintCoords(event
)
230 def OnMiddleUp(self
, event
):
231 print "Middle Button Up in DrawFrame"
232 self
.PrintCoords(event
)
234 def OnMiddleDouble(self
, event
):
235 print "Middle Button Double clicked in DrawFrame"
236 self
.PrintCoords(event
)
238 def OnRightDown(self
, event
):
239 print "Right Button has been clicked in DrawFrame"
240 self
.PrintCoords(event
)
242 def OnRightUp(self
, event
):
243 print "Right Button Up in DrawFrame"
244 self
.PrintCoords(event
)
246 def OnRightDouble(self
, event
):
247 print "Right Button Double clicked in DrawFrame"
248 self
.PrintCoords(event
)
250 def OnWheel(self
, event
):
251 print "Mouse Wheel Moved in DrawFrame"
252 self
.PrintCoords(event
)
254 def OnMove(self
, event
):
256 Updates the staus bar with the world coordinates
258 self
.SetStatusText("%.2f, %.2f"%tuple(event
.Coords
))
260 def OnAbout(self
, event
):
261 print "OnAbout called"
263 dlg
= wx
.MessageDialog(self
, "This is a small program to demonstrate\n"
264 "the use of the FloatCanvas\n",
265 "About Me", wx
.OK | wx
.ICON_INFORMATION
)
269 def ZoomToFit(self
,event
):
270 self
.Canvas
.ZoomToBB()
272 def Clear(self
,event
= None):
273 self
.UnBindAllMouseEvents()
274 self
.Canvas
.ClearAll()
275 self
.Canvas
.SetProjectionFun(None)
278 def OnQuit(self
,event
):
281 def OnCloseWindow(self
, event
):
284 def DrawTest(self
,event
=None):
291 self
.BindAllMouseEvents()
295 Canvas
.SetProjectionFun(None)
297 ## Random tests of everything:
301 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
302 lw
= random
.randint(1,5)
303 cf
= random
.randint(0,len(colors
)-1)
304 h
= random
.randint(1,5)
305 w
= random
.randint(1,5)
306 Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
310 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
311 lw
= random
.randint(1,5)
312 cf
= random
.randint(0,len(colors
)-1)
313 h
= random
.randint(1,5)
314 w
= random
.randint(1,5)
315 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
319 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
320 D
= random
.randint(1,50)
321 cf
= random
.randint(0,len(colors
)-1)
322 Canvas
.AddPoint((x
,y
), Color
= colors
[cf
], Diameter
= D
)
326 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
327 D
= random
.randint(1,5)
328 lw
= random
.randint(1,5)
329 cf
= random
.randint(0,len(colors
)-1)
330 cl
= random
.randint(0,len(colors
)-1)
331 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
332 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
337 for j
in range(random
.randint(2,10)):
338 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
340 lw
= random
.randint(1,10)
341 cf
= random
.randint(0,len(colors
)-1)
342 cl
= random
.randint(0,len(colors
)-1)
343 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
348 for j
in range(random
.randint(2,6)):
349 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
351 lw
= random
.randint(1,6)
352 cf
= random
.randint(0,len(colors
)-1)
353 cl
= random
.randint(0,len(colors
)-1)
354 Canvas
.AddPolygon(points
,
356 LineColor
= colors
[cl
],
357 FillColor
= colors
[cf
],
363 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
364 cf
= random
.randint(0,len(colors
)-1)
365 D
= random
.randint(1,4)
366 Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
369 String
= "Unscaled text"
371 ts
= random
.randint(10,40)
372 cf
= random
.randint(0,len(colors
)-1)
373 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
374 Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
377 String
= "Scaled text"
379 ts
= random
.random()*3 + 0.2
380 cf
= random
.randint(0,len(colors
)-1)
381 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
382 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
386 def TestAnimation(self
,event
=None):
389 In this test, a relatively complex background is drawn, and
390 a simple object placed in the foreground is moved over
391 it. This demonstrates how to use the InForeground attribute
392 to make an object in the foregorund draw fast, without
393 having to re-draw the whole background.
400 self
.UnBindAllMouseEvents()
404 Canvas
.SetProjectionFun(None)
406 ## Random tests of everything:
410 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
411 lw
= random
.randint(1,5)
412 cf
= random
.randint(0,len(colors
)-1)
413 h
= random
.randint(1,5)
414 w
= random
.randint(1,5)
415 Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
419 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
420 lw
= random
.randint(1,5)
421 cf
= random
.randint(0,len(colors
)-1)
422 h
= random
.randint(1,5)
423 w
= random
.randint(1,5)
424 Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
428 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
429 D
= random
.randint(1,5)
430 lw
= random
.randint(1,5)
431 cf
= random
.randint(0,len(colors
)-1)
432 cl
= random
.randint(0,len(colors
)-1)
433 Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
434 Canvas
.AddText("Circle # %i"%(i),x
,y
,Size
= 12,BackgroundColor
= None,Position
= "cc")
439 for j
in range(random
.randint(2,10)):
440 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
442 lw
= random
.randint(1,10)
443 cf
= random
.randint(0,len(colors
)-1)
444 cl
= random
.randint(0,len(colors
)-1)
445 Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
])
450 for j
in range(random
.randint(2,6)):
451 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
453 lw
= random
.randint(1,6)
454 cf
= random
.randint(0,len(colors
)-1)
455 cl
= random
.randint(0,len(colors
)-1)
456 Canvas
.AddPolygon(points
,
458 LineColor
= colors
[cl
],
459 FillColor
= colors
[cf
],
463 String
= "Scaled text"
465 ts
= random
.random()*3 + 0.2
466 cf
= random
.randint(0,len(colors
)-1)
467 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
468 Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc")
471 # Now the Foreground Object:
472 C
= Canvas
.AddCircle(0,0,7,LineWidth
= 2,LineColor
= "Black",FillColor
= "Red", InForeground
= True)
473 T
= Canvas
.AddScaledText("Click to Move",0,0, Size
= 0.6, Position
= 'cc', InForeground
= True)
474 C
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.MoveMe
)
477 self
.Timer
= wx
.PyTimer(self
.ShowFrame
)
478 self
.FrameDelay
= 50 # milliseconds
483 Object
= self
.MovingObject
485 if self
.TimeStep
< self
.NumTimeSteps
:
487 if x
> Range
[1] or x
< Range
[0]:
489 if y
> Range
[1] or y
< Range
[0]:
491 Object
.Move( (self
.dx
,self
.dy
) )
492 Object
.Text
.Move( (self
.dx
,self
.dy
))
495 wx
.GetApp().Yield(True)
500 def MoveMe(self
, Object
):
501 self
.MovingObject
= Object
503 self
.dx
= random
.uniform(Range
[0]/4,Range
[1]/4)
504 self
.dy
= random
.uniform(Range
[0]/4,Range
[1]/4)
507 self
.NumTimeSteps
= 200
509 self
.Timer
.Start(self
.FrameDelay
)
510 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
512 def TestHitTest(self
,event
=None):
515 self
.UnBindAllMouseEvents()
519 Canvas
.SetProjectionFun(None)
521 #Add a HitAble rectangle
529 #Add one that is not HitAble
530 Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
531 Canvas
.AddText("Not Hit-able", x
, y
, Size
= FontSize
, Position
= "bl")
535 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
536 R
.Name
= "Line Rectangle"
538 R
.HitLineWidth
= 5 # Makes it a little easier to hit
539 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
540 Canvas
.AddText("Left Click Line", x
, y
, Size
= FontSize
, Position
= "bl")
541 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
545 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
546 R
.Name
= color
+ "Rectangle"
547 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
548 Canvas
.AddText("Left Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
549 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
554 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
555 R
.Name
= color
+ " Rectangle"
556 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
557 Canvas
.AddText("Right Click Fill", x
, y
, Position
= "bl")
558 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
562 R
= Canvas
.AddEllipse(x
, y
, w
, h
,LineWidth
= 2,FillColor
= color
)
563 R
.Name
= color
+" Ellipse"
564 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHit
)
565 Canvas
.AddText("Right Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
566 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
570 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2, FillColor
= color
)
571 R
.Name
= color
+ " Circle"
573 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DCLICK
, self
.RectGotHit
)
574 Canvas
.AddText("Left D-Click Fill", x
, y
, Size
= FontSize
, Position
= "bl")
575 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
580 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2,FillColor
= color
)
581 R
.Name
= color
+ " Circle"
582 R
.Bind(FloatCanvas
.EVT_FC_LEFT_UP
, self
.RectGotHit
)
583 Canvas
.AddText("Left Up Fill", x
, y
, Size
= FontSize
, Position
= "bl")
584 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, 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_DOWN
, self
.RectGotHit
)
591 Canvas
.AddText("Middle Down", x
, y
, Size
= FontSize
, Position
= "bl")
592 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, 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_MIDDLE_UP
, self
.RectGotHit
)
599 Canvas
.AddText("Middle Up", x
, y
, Size
= FontSize
, Position
= "bl")
600 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
605 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
606 R
.Name
= color
+ " Rectangle"
607 R
.Bind(FloatCanvas
.EVT_FC_MIDDLE_DCLICK
, self
.RectGotHit
)
608 Canvas
.AddText("Middle DoubleClick", x
, y
, Size
= FontSize
, Position
= "bl")
609 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
613 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
614 R
.Name
= color
+ " Rectangle"
615 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_UP
, self
.RectGotHit
)
616 Canvas
.AddText("Right Up", x
, y
, Size
= FontSize
, Position
= "bl")
617 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
621 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
622 R
.Name
= color
+ " Rectangle"
623 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DCLICK
, self
.RectGotHit
)
624 Canvas
.AddText("Right Double Click", x
, y
, Size
= FontSize
, Position
= "bl")
625 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
629 color
= "MEDIUM GOLDENROD"
630 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
632 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
633 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
634 Canvas
.AddText("L and R Click", x
, y
, Size
= FontSize
, Position
= "bl")
635 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
639 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
640 R
.Name
= color
+ " Rectangle"
641 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
642 Canvas
.AddText("Mouse Enter", x
, y
, Size
= FontSize
, Position
= "bl")
643 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
646 color
= "MEDIUM VIOLET RED"
647 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
649 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
650 Canvas
.AddText("Mouse Leave", x
, y
, Size
= FontSize
, Position
= "bl")
651 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
656 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
)
658 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
659 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
660 Canvas
.AddText("Enter and Leave", x
, y
, Size
= FontSize
, Position
= "bl")
661 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
665 R
= Canvas
.AddRectangle(x
, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
667 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
668 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
669 Canvas
.AddText("Mouse Enter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
670 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
674 R
= Canvas
.AddRectangle(x
-12, y
, w
+12, h
, LineColor
= None, FillColor
= color
)
676 R
.Bind(FloatCanvas
.EVT_FC_ENTER_OBJECT
, self
.RectMouseOver
)
677 R
.Bind(FloatCanvas
.EVT_FC_LEAVE_OBJECT
, self
.RectMouseLeave
)
678 Canvas
.AddText("Mouse ENter&Leave", x
, y
, Size
= FontSize
, Position
= "bl")
679 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
683 L
= Canvas
.AddLine(( (x
, y
), (x
+10, y
+10), (x
+w
, y
+h
) ), LineWidth
= 2, LineColor
= "Red")
685 L
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
686 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
687 Canvas
.AddText(L
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
691 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
)
692 R
= Canvas
.AddPolygon(Points
, LineWidth
= 2, FillColor
= color
)
693 R
.Name
= color
+ " Polygon"
694 R
.Bind(FloatCanvas
.EVT_FC_RIGHT_DOWN
, self
.RectGotHitRight
)
695 Canvas
.AddText("RIGHT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
696 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
700 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
)
701 R
= Canvas
.AddPointSet(Points
, Diameter
= 4, Color
= color
)
703 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.PointSetGotHit
)
704 Canvas
.AddText("LEFT_DOWN", x
, y
, Size
= FontSize
, Position
= "bl")
705 Canvas
.AddText(R
.Name
, x
, y
+h
, Size
= FontSize
, Position
= "tl")
709 T
= Canvas
.AddText("Hit-able Text", x
, y
, Size
= 15, Color
= "Red", Position
= 'tl')
710 T
.Name
= "Hit-able Text"
711 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
712 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "bl")
715 T
= Canvas
.AddScaledText("Scaled Text", x
, y
, Size
= 1./2*h
, Color
= "Pink", Position
= 'bl')
716 Canvas
.AddPointSet( (x
, y
), Diameter
= 3)
717 T
.Name
= "Scaled Text"
718 T
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHitLeft
)
719 Canvas
.AddText("Left Down", x
, y
, Size
= FontSize
, Position
= "tl")
721 self
.Canvas
.ZoomToBB()
723 def TestHitTestForeground(self
,event
=None):
726 self
.UnBindAllMouseEvents()
730 Canvas
.SetProjectionFun(None)
732 #Add a Hitable rectangle
740 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
, InForeground
= False)
741 R
.Name
= color
+ "Rectangle"
743 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectGotHit
)
744 Canvas
.AddText("Left Click Fill", x
, y
, Position
= "bl")
745 Canvas
.AddText(R
.Name
, x
, y
+h
, Position
= "tl")
747 ## A set of Rectangles that move together
749 ## NOTE: In a real app, it might be better to create a new
750 ## custom FloatCanvas DrawObject
752 self
.MovingRects
= []
755 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
757 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveLeft
)
758 L
= Canvas
.AddText("Left", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
759 self
.MovingRects
.extend( (R
,L
) )
762 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
764 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveRight
)
765 L
= Canvas
.AddText("Right", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
766 self
.MovingRects
.extend( (R
,L
) )
770 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
772 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveUp
)
773 L
= Canvas
.AddText("Up", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
774 self
.MovingRects
.extend( (R
,L
) )
778 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= True)
780 R
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.RectMoveDown
)
781 L
= Canvas
.AddText("Down", x
+ w
/4, y
+ h
/4, Position
= "cc", InForeground
= True)
782 self
.MovingRects
.extend( (R
,L
) )
784 self
.Canvas
.ZoomToBB()
786 def RectMoveLeft(self
,Object
):
787 self
.MoveRects("left")
789 def RectMoveRight(self
,Object
):
790 self
.MoveRects("right")
792 def RectMoveUp(self
,Object
):
795 def RectMoveDown(self
,Object
):
796 self
.MoveRects("down")
798 def MoveRects(self
, Dir
):
799 for Object
in self
.MovingRects
:
801 if Dir
== "left": X
-= 10
802 elif Dir
== "right": X
+= 10
803 elif Dir
== "up": Y
+= 10
804 elif Dir
== "down": Y
-= 10
809 def PointSetGotHit(self
, Object
):
810 print Object
.Name
, "Got Hit\n"
812 def RectGotHit(self
, Object
):
813 print Object
.Name
, "Got Hit\n"
815 def RectGotHitRight(self
, Object
):
816 print Object
.Name
, "Got Hit With Right\n"
818 def RectGotHitLeft(self
, Object
):
819 print Object
.Name
, "Got Hit with Left\n"
821 def RectMouseOver(self
, Object
):
822 print "Mouse entered:", Object
.Name
824 def RectMouseLeave(self
, Object
):
825 print "Mouse left ", Object
.Name
828 def TestText(self
, event
= None):
831 self
.BindAllMouseEvents()
834 Canvas
.SetProjectionFun(None)
838 ## Add a non-visible rectangle, just to get a Bounding Box
839 ## Text objects have a zero-size bounding box, because it changes with zoom
840 Canvas
.AddRectangle(-10,-10,20,20,LineWidth
= 1, LineColor
= None)
844 self
.Canvas
.AddText("Top Left",x
,y
,Size
= 14,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
845 self
.Canvas
.AddText("Bottom Left",x
,y
,Size
= 14,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
846 self
.Canvas
.AddText("Top Right",x
,y
,Size
= 14,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
847 self
.Canvas
.AddText("Bottom Right",x
,y
,Size
= 14,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
848 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
852 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
853 self
.Canvas
.AddText("Top Center",x
,y
,Size
= 14,Color
= "Black",Position
= "tc")
854 self
.Canvas
.AddText("Bottom Center",x
,y
,Size
= 14,Color
= "White",Position
= "bc")
858 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
859 self
.Canvas
.AddText("Center Right",x
,y
,Size
= 14,Color
= "Black",Position
= "cr")
860 self
.Canvas
.AddText("Center Left",x
,y
,Size
= 14,Color
= "Black",Position
= "cl")
864 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
865 self
.Canvas
.AddText("Center Center",x
,y
,Size
= 14,Color
= "Black",Position
= "cc")
867 self
.Canvas
.AddText("40 Pixels",-10,8,Size
= 40)
868 self
.Canvas
.AddText("20 Pixels",-10,5,Size
= 20)
869 self
.Canvas
.AddText("10 Pixels",-10,3,Size
= 10)
871 self
.Canvas
.AddText("MODERN Font", -10, 0, Family
= wx
.MODERN
)
872 self
.Canvas
.AddText("DECORATIVE Font", -10, -1, Family
= wx
.DECORATIVE
)
873 self
.Canvas
.AddText("ROMAN Font", -10, -2, Family
= wx
.ROMAN
)
874 self
.Canvas
.AddText("SCRIPT Font", -10, -3, Family
= wx
.SCRIPT
)
875 self
.Canvas
.AddText("ROMAN BOLD Font", -10, -4, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
876 self
.Canvas
.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
878 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
879 Font
= wx
.Font(20, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
880 self
.Canvas
.AddText("zapf chancery Font", -10, -6, Font
= Font
)
882 self
.Canvas
.ZoomToBB()
884 def TestScaledText(self
, event
= None):
887 self
.BindAllMouseEvents()
890 Canvas
.SetProjectionFun(None)
894 T
= Canvas
.AddScaledText("Top Left",x
,y
,Size
= 5,Color
= "Yellow",BackgroundColor
= "Blue", Position
= "tl")
895 T
= Canvas
.AddScaledText("Bottom Left",x
,y
,Size
= 5,Color
= "Cyan",BackgroundColor
= "Black",Position
= "bl")
896 T
= Canvas
.AddScaledText("Top Right",x
,y
,Size
= 5,Color
= "Black",BackgroundColor
= "Cyan",Position
= "tr")
897 T
= Canvas
.AddScaledText("Bottom Right",x
,y
,Size
= 5,Color
= "Blue",BackgroundColor
= "Yellow",Position
= "br")
898 Canvas
.AddPointSet((x
,y
), Color
= "Red", Diameter
= 4)
903 Canvas
.AddScaledText("Top Center",x
,y
,Size
= 7,Color
= "Black",Position
= "tc")
904 Canvas
.AddScaledText("Bottom Center",x
,y
,Size
= 7,Color
= "White",Position
= "bc")
905 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
909 Canvas
.AddScaledText("Center Right",x
,y
,Size
= 9,Color
= "Black",Position
= "cr")
910 Canvas
.AddScaledText("Center Left",x
,y
,Size
= 9,Color
= "Black",Position
= "cl")
911 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 4)
915 self
.Canvas
.AddScaledText("MODERN Font", x
, 0, Size
= 7, Family
= wx
.MODERN
, Color
= (0,0,0))
916 self
.Canvas
.AddScaledText("DECORATIVE Font", x
, -10, Size
= 7, Family
= wx
.DECORATIVE
, Color
= (0,0,1))
917 self
.Canvas
.AddScaledText("ROMAN Font", x
, -20, Size
= 7, Family
= wx
.ROMAN
)
918 self
.Canvas
.AddScaledText("SCRIPT Font", x
, -30, Size
= 7, Family
= wx
.SCRIPT
)
919 self
.Canvas
.AddScaledText("ROMAN BOLD Font", x
, -40, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
)
920 self
.Canvas
.AddScaledText("ROMAN ITALIC BOLD Font", x
, -50, Size
= 7, Family
= wx
.ROMAN
, Weight
=wx
.BOLD
, Style
=wx
.ITALIC
)
921 Canvas
.AddPointSet((x
,0), Color
= "White", Diameter
= 4)
924 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
926 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "zapf chancery")
927 T
= self
.Canvas
.AddScaledText("zapf chancery Font", x
, y
, Size
= 20, Font
= Font
, Position
= 'bc')
930 Font
= wx
.Font(12, wx
.DEFAULT
, wx
.ITALIC
, wx
.NORMAL
, False, "bookman")
931 T
= self
.Canvas
.AddScaledText("Bookman Font", x
, y
, Size
= 8, Font
= Font
)
933 self
.Canvas
.ZoomToBB()
935 def DrawMap(self
,event
= None):
938 self
.BindAllMouseEvents()
940 ## Test of Actual Map Data
941 self
.Canvas
.ClearAll()
942 self
.Canvas
.SetProjectionFun("FlatEarth")
943 #start = time.clock()
944 Shorelines
= Read_MapGen(os
.path
.join("data",'world.dat'),stats
= 0)
945 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
946 #start = time.clock()
947 for segment
in Shorelines
:
948 self
.Canvas
.AddLine(segment
)
949 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
950 #start = time.clock()
951 self
.Canvas
.ZoomToBB()
952 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
955 def LineTest(self
,event
= None):
961 ## Test of drawing lots of lines
964 Canvas
.SetProjectionFun(None)
965 #start = time.clock()
969 for i
in range(2000):
970 points
= (random
.randint(Range
[0],Range
[1]),
971 random
.randint(Range
[0],Range
[1]),
972 random
.randint(Range
[0],Range
[1]),
973 random
.randint(Range
[0],Range
[1]))
974 linepoints
.append(points
)
975 linewidths
.append(random
.randint(1,10) )
976 linecolors
.append(random
.randint(0,len(colors
)-1) )
977 for (points
,color
,width
) in zip(linepoints
,linecolors
,linewidths
):
978 Canvas
.AddLine((points
[0:2],points
[2:4]), LineWidth
= width
, LineColor
= colors
[color
])
979 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
980 #start = time.clock()
982 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
984 def SpeedTest(self
,event
=None):
986 BigRange
= (-1000,1000)
989 self
.UnBindAllMouseEvents()
993 Canvas
.SetProjectionFun(None)
997 for i
in range(1000):
998 x
,y
= (random
.uniform(BigRange
[0],BigRange
[1]),random
.uniform(BigRange
[0],BigRange
[1]))
999 coords
.append( (x
,y
) )
1000 print "Drawing the Points"
1001 start
= time
.clock()
1002 for Point
in coords
:
1003 Canvas
.AddPoint(Point
, Diameter
= 4)
1004 print "It took %s seconds to add the points"%(time
.clock() - start
)
1007 def PropertiesChangeTest(self
,event
=None):
1011 colors
= self
.colors
1013 self
.UnBindAllMouseEvents()
1014 Canvas
= self
.Canvas
1017 Canvas
.SetProjectionFun(None)
1019 self
.ColorObjectsAll
= []
1020 self
.ColorObjectsLine
= []
1021 self
.ColorObjectsColor
= []
1022 self
.ColorObjectsText
= []
1023 ##One of each object:
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,5)
1029 w
= random
.randint(1,5)
1030 self
.Rectangle
= Canvas
.AddRectangle(x
,y
,w
,h
,LineWidth
= lw
,FillColor
= colors
[cf
])
1031 self
.ColorObjectsAll
.append(self
.Rectangle
)
1034 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1035 lw
= random
.randint(1,5)
1036 cf
= random
.randint(0,len(colors
)-1)
1037 h
= random
.randint(1,5)
1038 w
= random
.randint(1,5)
1039 self
.Ellipse
= Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth
= lw
,FillColor
= colors
[cf
])
1040 self
.ColorObjectsAll
.append(self
.Ellipse
)
1043 xy
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1044 D
= random
.randint(1,50)
1045 lw
= random
.randint(1,5)
1046 cf
= random
.randint(0,len(colors
)-1)
1047 cl
= random
.randint(0,len(colors
)-1)
1048 self
.ColorObjectsColor
.append(Canvas
.AddPoint(xy
, colors
[cf
], D
))
1051 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1052 D
= random
.randint(1,5)
1053 lw
= random
.randint(1,5)
1054 cf
= random
.randint(0,len(colors
)-1)
1055 cl
= random
.randint(0,len(colors
)-1)
1056 self
.Circle
= Canvas
.AddCircle(x
,y
,D
,LineWidth
= lw
,LineColor
= colors
[cl
],FillColor
= colors
[cf
])
1057 self
.ColorObjectsAll
.append(self
.Circle
)
1061 for j
in range(random
.randint(2,10)):
1062 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
1063 points
.append(point
)
1064 lw
= random
.randint(1,10)
1065 cf
= random
.randint(0,len(colors
)-1)
1066 cl
= random
.randint(0,len(colors
)-1)
1067 self
.ColorObjectsLine
.append(Canvas
.AddLine(points
, LineWidth
= lw
, LineColor
= colors
[cl
]))
1071 ## for j in range(random.randint(2,6)):
1072 ## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1073 ## points.append(point)
1074 points
= RandomArray
.uniform(Range
[0],Range
[1],(6,2))
1075 lw
= random
.randint(1,6)
1076 cf
= random
.randint(0,len(colors
)-1)
1077 cl
= random
.randint(0,len(colors
)-1)
1078 self
.ColorObjectsAll
.append(Canvas
.AddPolygon(points
,
1080 LineColor
= colors
[cl
],
1081 FillColor
= colors
[cf
],
1082 FillStyle
= 'Solid'))
1085 points
= RandomArray
.uniform(Range
[0],Range
[1],(100,2))
1086 cf
= random
.randint(0,len(colors
)-1)
1087 D
= random
.randint(1,4)
1088 self
.PointSet
= Canvas
.AddPointSet(points
, Color
= colors
[cf
], Diameter
= D
)
1089 self
.ColorObjectsColor
.append(self
.PointSet
)
1092 point
= RandomArray
.uniform(Range
[0],Range
[1],(2,))
1093 cf
= random
.randint(0,len(colors
)-1)
1094 D
= random
.randint(1,4)
1095 self
.Point
= Canvas
.AddPoint(point
, Color
= colors
[cf
], Diameter
= D
)
1096 self
.ColorObjectsColor
.append(self
.Point
)
1099 String
= "Unscaled text"
1100 ts
= random
.randint(10,40)
1101 cf
= random
.randint(0,len(colors
)-1)
1102 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1103 self
.ColorObjectsText
.append(Canvas
.AddText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1106 String
= "Scaled text"
1107 ts
= random
.random()*3 + 0.2
1108 cf
= random
.randint(0,len(colors
)-1)
1109 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1110 self
.ColorObjectsText
.append(Canvas
.AddScaledText(String
, x
, y
, Size
= ts
, Color
= colors
[cf
], Position
= "cc"))
1113 Button
= Canvas
.AddRectangle(-10, -12, 20, 3, LineStyle
= None, FillColor
= "Red")
1114 Canvas
.AddScaledText("Click Here To Change Properties",
1120 Button
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.ChangeProperties
)
1124 def ChangeProperties(self
, Object
= None):
1125 colors
= self
.colors
1128 for Object
in self
.ColorObjectsAll
:
1130 Object
.SetFillColor(colors
[random
.randint(0,len(colors
)-1)])
1131 Object
.SetLineColor(colors
[random
.randint(0,len(colors
)-1)])
1132 Object
.SetLineWidth(random
.randint(1,7))
1133 Object
.SetLineStyle(FloatCanvas
.DrawObject
.LineStyleList
.keys()[random
.randint(0,5)])
1134 for Object
in self
.ColorObjectsLine
:
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
.ColorObjectsColor
:
1139 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1140 for Object
in self
.ColorObjectsText
:
1141 Object
.SetColor(colors
[random
.randint(0,len(colors
)-1)])
1142 Object
.SetBackgroundColor(colors
[random
.randint(0,len(colors
)-1)])
1143 self
.Circle
.SetDiameter(random
.randint(1,10))
1144 self
.PointSet
.SetDiameter(random
.randint(1,8))
1145 self
.Point
.SetDiameter(random
.randint(1,8))
1146 for Object
in (self
.Rectangle
, self
.Ellipse
):
1147 x
,y
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1148 w
,h
= random
.randint(1,5), random
.randint(1,5)
1149 Object
.SetShape(x
,y
,w
,h
)
1151 self
.Canvas
.Draw(Force
= True)
1153 def TempTest(self
, event
= None):
1156 self
.UnBindAllMouseEvents()
1157 Canvas
= self
.Canvas
1159 Canvas
.SetProjectionFun(None)
1163 # Create a random Polygon
1166 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1167 points
.append(point
)
1168 Poly
= Canvas
.AddPolygon(points
,
1170 LineColor
= "Black",
1171 FillColor
= "LightBlue",
1172 FillStyle
= 'Solid')
1174 Poly
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPoly
)
1176 self
.SelectedPoly
= None
1177 self
.SelectPoints
= []
1178 self
.SelectedPoint
= None
1182 def SelectPoly(self
, Object
):
1183 print "In SelectPoly"
1184 Canvas
= self
.Canvas
1185 if Object
is self
.SelectedPoly
:
1188 #fixme: Do something to unselect the old one
1189 self
.SelectedPoly
= Object
1190 Canvas
.RemoveObjects(self
.SelectPoints
)
1191 self
.SelectPoints
= []
1192 # Draw points on the Vertices of the Selected Poly:
1193 for i
, point
in enumerate(Object
.Points
):
1194 P
= Canvas
.AddPointSet(point
, Diameter
= 6, Color
= "Red")
1196 P
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPointHit
)
1197 self
.SelectPoints
.append(P
)
1201 def SelectPointHit(self
, Point
):
1202 print "Point Num: %i Hit"%Point
.VerticeNum
1203 self
.SelectedPoint
= Point
1207 class DemoApp(wx
.App
):
1211 Under the Draw menu, there are three options:
1213 *Draw Test: will put up a picture of a bunch of randomly generated
1214 objects, of each kind supported.
1216 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1217 with a lot of data, and will take a while to load and draw (about 10 sec
1218 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1219 performance is not very good for large drawings.
1221 *Clear: Clears the Canvas.
1223 Once you have a picture drawn, you can zoom in and out and move about
1224 the picture. There is a tool bar with three tools that can be
1227 The magnifying glass with the plus is the zoom in tool. Once selected,
1228 if you click the image, it will zoom in, centered on where you
1229 clicked. If you click and drag the mouse, you will get a rubber band
1230 box, and the image will zoom to fit that box when you release it.
1232 The magnifying glass with the minus is the zoom out tool. Once selected,
1233 if you click the image, it will zoom out, centered on where you
1234 clicked. (note that this takes a while when you are looking at the map,
1235 as it has a LOT of lines to be drawn. The image is double buffered, so
1236 you don't see the drawing in progress)
1238 The hand is the move tool. Once selected, if you click and drag on the
1239 image, it will move so that the part you clicked on ends up where you
1240 release the mouse. Nothing is changed while you are dragging. The
1241 drawing is too slow for that.
1243 I'd like the cursor to change as you change tools, but the stock
1244 wxCursors didn't include anything I liked, so I stuck with the
1245 pointer. Please let me know if you have any nice cursor images for me to
1249 Any bugs, comments, feedback, questions, and especially code are welcome:
1253 Chris.Barker@noaa.gov
1257 def __init__(self
, *args
, **kwargs
):
1258 wx
.App
.__init
__(self
, *args
, **kwargs
)
1261 wx
.InitAllImageHandlers()
1262 frame
= DrawFrame(None, -1, "FloatCanvas Demo App",wx
.DefaultPosition
,(700,700))
1264 self
.SetTopWindow(frame
)
1267 ## check to see if the demo is set to start in a particular mode.
1268 if StartUpDemo
== "text":
1270 if StartUpDemo
== "stext":
1271 frame
.TestScaledText()
1272 elif StartUpDemo
== "all":
1274 elif StartUpDemo
== "map":
1276 elif StartUpDemo
== "hit":
1278 elif StartUpDemo
== "hitf":
1279 "starting TestHitTestForeground"
1280 frame
.TestHitTestForeground()
1281 elif StartUpDemo
== "animate":
1282 "starting TestAnimation"
1283 frame
.TestAnimation()
1284 elif StartUpDemo
== "speed":
1285 "starting SpeedTest"
1287 elif StartUpDemo
== "temp":
1288 "starting temp Test"
1290 elif StartUpDemo
== "props":
1291 "starting PropertiesChange Test"
1292 frame
.PropertiesChangeTest()
1296 def Read_MapGen(filename
,stats
= 0,AllLines
=0):
1298 This function reads a MapGen Format file, and
1299 returns a list of NumPy arrays with the line segments in them.
1301 Each NumPy array in the list is an NX2 array of Python Floats.
1303 The demo should have come with a file, "world.dat" that is the
1304 shorelines of the whole world, in MapGen format.
1308 file = open(filename
,'rt')
1309 data
= file.readlines()
1310 data
= map(string
.strip
,data
)
1316 if line
== "# -b": #New segment beginning
1317 if segment
: Shorelines
.append(Numeric
.array(segment
))
1320 segment
.append(map(float,string
.split(line
)))
1321 if segment
: Shorelines
.append(Numeric
.array(segment
))
1324 NumSegments
= len(Shorelines
)
1326 for segment
in Shorelines
:
1327 NumPoints
= NumPoints
+ len(segment
)
1328 AvgPoints
= NumPoints
/ NumSegments
1329 print "Number of Segments: ", NumSegments
1330 print "Average Number of Points per segment: ",AvgPoints
1333 for segment
in Shorelines
:
1334 Lines
.append(segment
[0])
1335 for point
in segment
[1:-1]:
1338 Lines
.append(segment
[-1])
1343 #---------------------------------------------------------------------------
1344 ## for the wxPython demo:
1346 def runTest(frame
, nb
, log
):
1347 win
= TestPanel(nb
, log
)
1354 except ImportError: # if it's not there locally, try the wxPython lib.
1355 from wx
.lib
import floatcanvas
1357 overview
= floatcanvas
.__doc
__
1365 if __name__
== "__main__":
1369 app
= DemoApp(False)# put in True if you want output to go to it's own window.