]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/FloatCanvas.py
Changed debug print into a log message
[wxWidgets.git] / wxPython / demo / FloatCanvas.py
CommitLineData
5e1796ef 1#!/usr/bin/env python2.3
8b9a4190
RD
2try:
3 import Numeric
42463de2 4 import RandomArray
8b9a4190
RD
5 haveNumeric = True
6except ImportError:
42463de2
RD
7 try:
8 import numarray as Numeric
9 import numarray.random_array as RandomArray
10 haveNumeric = True
11 except ImportError:
12 haveNumeric = False
8b9a4190
RD
13
14if not haveNumeric:
42463de2
RD
15 errorText = """
16The FloatCanvas requires either the Numeric or Numarray module:
17You can get them at:
8b9a4190 18 http://sourceforge.net/projects/numpy
8fa876ca 19
42463de2
RD
20NOTE: The Numeric module is substantially faster than numarray for this
21purpose, if you have lot's of objects
22"""
23
24 def runTest(frame, nb, log):
25 dlg = wx.MessageDialog(frame, errorText, 'Sorry', wx.OK |
26 wx.ICON_INFORMATION)
8b9a4190
RD
27 dlg.ShowModal()
28 dlg.Destroy()
29
30 overview = ""
42463de2 31
8b9a4190 32else:
42463de2
RD
33 StartUpDemo = "all"
34 if __name__ == "__main__": # parse options if run stand-alone
35 # check options:
36 import sys, getopt
5e1796ef 37 optlist, args = getopt.getopt(sys.argv[1:],'l',["local","all","text","map","stext","hit","hitf","animate","speed","temp","props"])
42463de2
RD
38
39 for opt in optlist:
40 if opt[0] == "--all":
41 StartUpDemo = "all"
42 elif opt[0] == "--text":
43 StartUpDemo = "text"
44 elif opt[0] == "--map":
45 StartUpDemo = "map"
46 elif opt[0] == "--stext":
47 StartUpDemo = "stext"
48 elif opt[0] == "--hit":
49 StartUpDemo = "hit"
50 elif opt[0] == "--hitf":
51 StartUpDemo = "hitf"
52 elif opt[0] == "--animate":
53 StartUpDemo = "animate"
54 elif opt[0] == "--speed":
55 StartUpDemo = "speed"
56 elif opt[0] == "--temp":
57 StartUpDemo = "temp"
5e1796ef
RD
58 elif opt[0] == "--props":
59 StartUpDemo = "props"
42463de2
RD
60 import wx
61 import time, random
34a544a6
RD
62
63 #---------------------------------------------------------------------------
64
65 class TestPanel(wx.Panel):
66 def __init__(self, parent, log):
67 self.log = log
68 wx.Panel.__init__(self, parent, -1)
69
70 b = wx.Button(self, -1, "Show the FloatBar sample", (50,50))
71 self.Bind(wx.EVT_BUTTON, self.OnButton, b)
72
73
74 def OnButton(self, evt):
75 win = DrawFrame(None, -1, "FloatCanvas Drawing Window",wx.DefaultPosition,(500,500))
76 win.Show(True)
77 win.DrawTest()
78
79
42463de2 80 def runTest(frame, nb, log):
34a544a6
RD
81 win = TestPanel(nb, log)
82 return win
83
84
85
fbd5dd1d 86
42463de2
RD
87 try:
88 from floatcanvas import NavCanvas, FloatCanvas
89 except ImportError: # if it's not there locally, try the wxPython lib.
90 from wx.lib.floatcanvas import NavCanvas, FloatCanvas
fbd5dd1d 91
42463de2 92 import wxPython.lib.colourdb
8b9a4190 93
8fa876ca 94 class DrawFrame(wx.Frame):
8b9a4190
RD
95
96 """
8b9a4190
RD
97 A frame used for the FloatCanvas Demo
98
99 """
100
42463de2
RD
101
102 def __init__(self,parent, id,title,position,size):
8fa876ca 103 wx.Frame.__init__(self,parent, id,title,position, size)
8b9a4190 104
42463de2 105 ## Set up the MenuBar
8fa876ca 106 MenuBar = wx.MenuBar()
8b9a4190 107
8fa876ca 108 file_menu = wx.Menu()
42463de2
RD
109 item = file_menu.Append(-1, "&Close","Close this frame")
110 self.Bind(wx.EVT_MENU, self.OnQuit, item)
8b9a4190
RD
111 MenuBar.Append(file_menu, "&File")
112
8fa876ca 113 draw_menu = wx.Menu()
8fa876ca 114
42463de2
RD
115 item = draw_menu.Append(-1, "&Draw Test","Run a test of drawing random components")
116 self.Bind(wx.EVT_MENU, self.DrawTest, item)
117
118 item = draw_menu.Append(-1, "&Line Test","Run a test of drawing random lines")
119 self.Bind(wx.EVT_MENU, self.LineTest, item)
120
121 item = draw_menu.Append(-1, "Draw &Map","Run a test of drawing a map")
122 self.Bind(wx.EVT_MENU, self.DrawMap, item)
123 item = draw_menu.Append(-1, "&Text Test","Run a test of text drawing")
124 self.Bind(wx.EVT_MENU, self.TestText, item)
125 item = draw_menu.Append(-1, "&ScaledText Test","Run a test of text drawing")
126 self.Bind(wx.EVT_MENU, self.TestScaledText, item)
127 item = draw_menu.Append(-1, "&Clear","Clear the Canvas")
128 self.Bind(wx.EVT_MENU, self.Clear, item)
129 item = draw_menu.Append(-1, "&Hit Test","Run a test of the hit test code")
130 self.Bind(wx.EVT_MENU, self.TestHitTest, item)
5e1796ef 131 item = draw_menu.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
42463de2
RD
132 self.Bind(wx.EVT_MENU, self.TestHitTestForeground, item)
133 item = draw_menu.Append(-1, "&Animation","Run a test of Animation")
134 self.Bind(wx.EVT_MENU, self.TestAnimation, item)
135 item = draw_menu.Append(-1, "&Speed","Run a test of Drawing Speed")
136 self.Bind(wx.EVT_MENU, self.SpeedTest, item)
5e1796ef
RD
137 item = draw_menu.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
138 self.Bind(wx.EVT_MENU, self.PropertiesChangeTest, item)
42463de2
RD
139 MenuBar.Append(draw_menu, "&Tests")
140
8fa876ca 141 view_menu = wx.Menu()
42463de2
RD
142 item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
143 self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
8b9a4190
RD
144 MenuBar.Append(view_menu, "&View")
145
8fa876ca 146 help_menu = wx.Menu()
42463de2 147 item = help_menu.Append(-1, "&About",
8b9a4190 148 "More information About this program")
42463de2 149 self.Bind(wx.EVT_MENU, self.OnAbout, item)
8b9a4190
RD
150 MenuBar.Append(help_menu, "&Help")
151
152 self.SetMenuBar(MenuBar)
153
42463de2 154 self.CreateStatusBar()
8b9a4190 155 # Add the Canvas
42463de2
RD
156 self.Canvas = NavCanvas.NavCanvas(self,
157 -1,
158 (500,500),
5e1796ef 159 Debug = 0,
42463de2
RD
160 BackgroundColor = "DARK SLATE BLUE")
161
162 wx.EVT_CLOSE(self, self.OnCloseWindow)
163
164 FloatCanvas.EVT_MOTION(self.Canvas, self.OnMove )
165 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
166
167 self.EventsAreBound = False
168
169 ## getting all the colors and linestyles for random objects
170 wxPython.lib.colourdb.updateColourDB()
171 self.colors = wxPython.lib.colourdb.getColourList()
172 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
173
174
8b9a4190 175 return None
42463de2
RD
176
177 def BindAllMouseEvents(self):
178 if not self.EventsAreBound:
179 ## Here is how you catch FloatCanvas mouse events
180 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, self.OnLeftDown )
181 FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
182 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, self.OnLeftDouble )
183
184 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, self.OnMiddleDown )
185 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, self.OnMiddleUp )
186 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, self.OnMiddleDouble )
187
188 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, self.OnRightDown )
189 FloatCanvas.EVT_RIGHT_UP(self.Canvas, self.OnRightUp )
190 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, self.OnRightDouble )
191
192 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, self.OnWheel )
193 self.EventsAreBound = True
194
195 def UnBindAllMouseEvents(self):
196 ## Here is how you catch FloatCanvas mouse events
197 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, None )
198 FloatCanvas.EVT_LEFT_UP(self.Canvas, None )
199 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, None)
200
201 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, None )
202 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, None )
203 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, None )
204
205 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, None )
206 FloatCanvas.EVT_RIGHT_UP(self.Canvas, None )
207 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, None )
208
209 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, None )
210
211 self.EventsAreBound = False
212
213 def PrintCoords(self,event):
214 print "coords are: %s"%(event.Coords,)
215 print "pixel coords are: %s\n"%(event.GetPosition(),)
216
217 def OnLeftDown(self, event):
218 print "Left Button has been clicked in DrawFrame"
219 self.PrintCoords(event)
220
221 def OnLeftUp(self, event):
222 print "Left up in DrawFrame"
223 self.PrintCoords(event)
224
225 def OnLeftDouble(self, event):
226 print "Left Double Click in DrawFrame"
227 self.PrintCoords(event)
228
229 def OnMiddleDown(self, event):
230 print "Middle Button clicked in DrawFrame"
231 self.PrintCoords(event)
232
233 def OnMiddleUp(self, event):
234 print "Middle Button Up in DrawFrame"
235 self.PrintCoords(event)
236
237 def OnMiddleDouble(self, event):
238 print "Middle Button Double clicked in DrawFrame"
239 self.PrintCoords(event)
240
241 def OnRightDown(self, event):
8b9a4190 242 print "Right Button has been clicked in DrawFrame"
42463de2
RD
243 self.PrintCoords(event)
244
245 def OnRightUp(self, event):
246 print "Right Button Up in DrawFrame"
247 self.PrintCoords(event)
248
249 def OnRightDouble(self, event):
250 print "Right Button Double clicked in DrawFrame"
251 self.PrintCoords(event)
252
253 def OnWheel(self, event):
254 print "Mouse Wheel Moved in DrawFrame"
255 self.PrintCoords(event)
256
257 def OnMove(self, event):
258 """
259 Updates the staus bar with the world coordinates
260 """
261 self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
262
8b9a4190 263 def OnAbout(self, event):
42463de2
RD
264 print "OnAbout called"
265
8fa876ca 266 dlg = wx.MessageDialog(self, "This is a small program to demonstrate\n"
8b9a4190 267 "the use of the FloatCanvas\n",
8fa876ca 268 "About Me", wx.OK | wx.ICON_INFORMATION)
8b9a4190
RD
269 dlg.ShowModal()
270 dlg.Destroy()
271
8b9a4190
RD
272 def ZoomToFit(self,event):
273 self.Canvas.ZoomToBB()
274
275 def Clear(self,event = None):
42463de2
RD
276 self.UnBindAllMouseEvents()
277 self.Canvas.ClearAll()
278 self.Canvas.SetProjectionFun(None)
8b9a4190
RD
279 self.Canvas.Draw()
280
281 def OnQuit(self,event):
282 self.Close(True)
283
284 def OnCloseWindow(self, event):
285 self.Destroy()
286
42463de2 287 def DrawTest(self,event=None):
5e1796ef 288 wx.GetApp().Yield()
42463de2
RD
289# import random
290# import RandomArray
8b9a4190 291 Range = (-10,10)
42463de2
RD
292 colors = self.colors
293
294 self.BindAllMouseEvents()
8b9a4190 295 Canvas = self.Canvas
42463de2
RD
296
297 Canvas.ClearAll()
298 Canvas.SetProjectionFun(None)
299
300 ## Random tests of everything:
8b9a4190
RD
301
302 # Rectangles
42463de2 303 for i in range(3):
8b9a4190
RD
304 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
305 lw = random.randint(1,5)
306 cf = random.randint(0,len(colors)-1)
307 h = random.randint(1,5)
308 w = random.randint(1,5)
5e1796ef 309 Canvas.AddRectangle(x,y,w,h,LineWidth = lw,FillColor = colors[cf])
8b9a4190 310
42463de2
RD
311 # Ellipses
312 for i in range(3):
8b9a4190
RD
313 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
314 lw = random.randint(1,5)
315 cf = random.randint(0,len(colors)-1)
316 h = random.randint(1,5)
317 w = random.randint(1,5)
42463de2 318 Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
8b9a4190 319
5e1796ef
RD
320 # Points
321 for i in range(5):
322 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
323 D = random.randint(1,50)
324 cf = random.randint(0,len(colors)-1)
325 Canvas.AddPoint((x,y), Color = colors[cf], Diameter = D)
8b9a4190
RD
326
327 # Circles
328 for i in range(5):
329 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
330 D = random.randint(1,5)
331 lw = random.randint(1,5)
332 cf = random.randint(0,len(colors)-1)
333 cl = random.randint(0,len(colors)-1)
42463de2
RD
334 Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
335 Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackgroundColor = None,Position = "cc")
8b9a4190
RD
336
337 # Lines
338 for i in range(5):
339 points = []
340 for j in range(random.randint(2,10)):
341 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
342 points.append(point)
343 lw = random.randint(1,10)
344 cf = random.randint(0,len(colors)-1)
345 cl = random.randint(0,len(colors)-1)
42463de2 346 Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
8b9a4190
RD
347
348 # Polygons
349 for i in range(3):
350 points = []
351 for j in range(random.randint(2,6)):
352 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
353 points.append(point)
354 lw = random.randint(1,6)
355 cf = random.randint(0,len(colors)-1)
356 cl = random.randint(0,len(colors)-1)
42463de2
RD
357 Canvas.AddPolygon(points,
358 LineWidth = lw,
359 LineColor = colors[cl],
360 FillColor = colors[cf],
361 FillStyle = 'Solid')
362
8b9a4190
RD
363 ## Pointset
364 for i in range(4):
365 points = []
366 points = RandomArray.uniform(Range[0],Range[1],(100,2))
367 cf = random.randint(0,len(colors)-1)
368 D = random.randint(1,4)
42463de2 369 Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
8b9a4190
RD
370
371 # Text
42463de2
RD
372 String = "Unscaled text"
373 for i in range(3):
8b9a4190
RD
374 ts = random.randint(10,40)
375 cf = random.randint(0,len(colors)-1)
376 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
42463de2
RD
377 Canvas.AddText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
378
379 # Scaled Text
380 String = "Scaled text"
381 for i in range(3):
382 ts = random.random()*3 + 0.2
383 cf = random.randint(0,len(colors)-1)
384 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
385 Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
386
387 Canvas.ZoomToBB()
388
389 def TestAnimation(self,event=None):
390 """
391
392 In this test, a relatively complex background is drawn, and
393 a simple object placed in the foreground is moved over
394 it. This demonstrates how to use the InForeground attribute
395 to make an object in the foregorund draw fast, without
396 having to re-draw the whole background.
397
398 """
5e1796ef 399 wx.GetApp().Yield()
42463de2
RD
400 Range = (-10,10)
401 self.Range = Range
402
403 self.UnBindAllMouseEvents()
404 Canvas = self.Canvas
405
406 Canvas.ClearAll()
407 Canvas.SetProjectionFun(None)
408
409 ## Random tests of everything:
410 colors = self.colors
411 # Rectangles
412 for i in range(3):
413 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
414 lw = random.randint(1,5)
415 cf = random.randint(0,len(colors)-1)
416 h = random.randint(1,5)
417 w = random.randint(1,5)
418 Canvas.AddRectangle(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
419
420 # Ellipses
421 for i in range(3):
422 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
423 lw = random.randint(1,5)
424 cf = random.randint(0,len(colors)-1)
425 h = random.randint(1,5)
426 w = random.randint(1,5)
427 Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
428
429 # Circles
430 for i in range(5):
431 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
432 D = random.randint(1,5)
433 lw = random.randint(1,5)
434 cf = random.randint(0,len(colors)-1)
435 cl = random.randint(0,len(colors)-1)
436 Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
437 Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackgroundColor = None,Position = "cc")
438
439 # Lines
440 for i in range(5):
441 points = []
442 for j in range(random.randint(2,10)):
443 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
444 points.append(point)
445 lw = random.randint(1,10)
446 cf = random.randint(0,len(colors)-1)
447 cl = random.randint(0,len(colors)-1)
448 Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
449
450 # Polygons
451 for i in range(3):
452 points = []
453 for j in range(random.randint(2,6)):
454 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
455 points.append(point)
456 lw = random.randint(1,6)
457 cf = random.randint(0,len(colors)-1)
458 cl = random.randint(0,len(colors)-1)
459 Canvas.AddPolygon(points,
460 LineWidth = lw,
461 LineColor = colors[cl],
462 FillColor = colors[cf],
463 FillStyle = 'Solid')
464
465 # Scaled Text
466 String = "Scaled text"
467 for i in range(3):
468 ts = random.random()*3 + 0.2
469 cf = random.randint(0,len(colors)-1)
470 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
471 Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
472
473
474 # Now the Foreground Object:
475 C = Canvas.AddCircle(0,0,7,LineWidth = 2,LineColor = "Black",FillColor = "Red", InForeground = True)
5e1796ef 476 T = Canvas.AddScaledText("Click to Move",0,0, Size = 0.6, Position = 'cc', InForeground = True)
42463de2
RD
477 C.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.MoveMe)
478 C.Text = T
479
480 self.Timer = wx.PyTimer(self.ShowFrame)
481 self.FrameDelay = 50 # milliseconds
482
483 Canvas.ZoomToBB()
484
485 def ShowFrame(self):
486 Object = self.MovingObject
487 Range = self.Range
488 if self.TimeStep < self.NumTimeSteps:
489 x,y = Object.XY
490 if x > Range[1] or x < Range[0]:
491 self.dx = -self.dx
492 if y > Range[1] or y < Range[0]:
493 self.dy = -self.dy
494 Object.Move( (self.dx,self.dy) )
495 Object.Text.Move( (self.dx,self.dy))
496 self.Canvas.Draw()
497 self.TimeStep += 1
5e1796ef 498 wx.GetApp().Yield(True)
42463de2
RD
499 else:
500 self.Timer.Stop()
501
502
503 def MoveMe(self, Object):
504 self.MovingObject = Object
505 Range = self.Range
506 self.dx = random.uniform(Range[0]/4,Range[1]/4)
507 self.dy = random.uniform(Range[0]/4,Range[1]/4)
508 #import time
509 #start = time.time()
5e1796ef 510 self.NumTimeSteps = 200
42463de2
RD
511 self.TimeStep = 1
512 self.Timer.Start(self.FrameDelay)
513 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
8b9a4190 514
42463de2 515 def TestHitTest(self,event=None):
5e1796ef 516 wx.GetApp().Yield()
42463de2
RD
517
518 self.UnBindAllMouseEvents()
519 Canvas = self.Canvas
520
521 Canvas.ClearAll()
522 Canvas.SetProjectionFun(None)
523
524 #Add a HitAble rectangle
525 w, h = 60, 20
526
527 dx = 80
528 dy = 40
529 x,y = 20, 20
5e1796ef 530 FontSize = 8
42463de2
RD
531
532 #Add one that is not HitAble
533 Canvas.AddRectangle(x, y, w, h, LineWidth = 2)
5e1796ef 534 Canvas.AddText("Not Hit-able", x, y, Size = FontSize, Position = "bl")
42463de2
RD
535
536
537 x += dx
5e1796ef 538 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2)
42463de2
RD
539 R.Name = "Line Rectangle"
540 R.HitFill = False
5e1796ef 541 R.HitLineWidth = 5 # Makes it a little easier to hit
42463de2 542 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
5e1796ef
RD
543 Canvas.AddText("Left Click Line", x, y, Size = FontSize, Position = "bl")
544 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
545
546 x += dx
547 color = "Red"
548 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
549 R.Name = color + "Rectangle"
550 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
5e1796ef
RD
551 Canvas.AddText("Left Click Fill", x, y, Size = FontSize, Position = "bl")
552 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
553
554 x = 20
555 y += dy
556 color = "LightBlue"
557 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
558 R.Name = color + " Rectangle"
559 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
560 Canvas.AddText("Right Click Fill", x, y, Position = "bl")
5e1796ef 561 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
562
563 x += dx
564 color = "Grey"
565 R = Canvas.AddEllipse(x, y, w, h,LineWidth = 2,FillColor = color)
566 R.Name = color +" Ellipse"
567 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
5e1796ef
RD
568 Canvas.AddText("Right Click Fill", x, y, Size = FontSize, Position = "bl")
569 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
570
571 x += dx
572 color = "Brown"
573 R = Canvas.AddCircle(x+dx/2, y+dy/2, dx/4, LineWidth = 2, FillColor = color)
574 R.Name = color + " Circle"
575 R.HitFill = True
576 R.Bind(FloatCanvas.EVT_FC_LEFT_DCLICK, self.RectGotHit)
5e1796ef
RD
577 Canvas.AddText("Left D-Click Fill", x, y, Size = FontSize, Position = "bl")
578 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
579
580 x = 20
581 y += dy
582 color = "Pink"
583 R = Canvas.AddCircle(x+dx/2, y+dy/2, dx/4, LineWidth = 2,FillColor = color)
584 R.Name = color + " Circle"
585 R.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.RectGotHit)
5e1796ef
RD
586 Canvas.AddText("Left Up Fill", x, y, Size = FontSize, Position = "bl")
587 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
588
589 x += dx
590 color = "White"
591 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
592 R.Name = color + " Rectangle"
593 R.Bind(FloatCanvas.EVT_FC_MIDDLE_DOWN, self.RectGotHit)
5e1796ef
RD
594 Canvas.AddText("Middle Down", x, y, Size = FontSize, Position = "bl")
595 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
596
597 x += dx
598 color = "AQUAMARINE"
599 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
600 R.Name = color + " Rectangle"
601 R.Bind(FloatCanvas.EVT_FC_MIDDLE_UP, self.RectGotHit)
5e1796ef
RD
602 Canvas.AddText("Middle Up", x, y, Size = FontSize, Position = "bl")
603 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
604
605 x = 20
606 y += dy
607 color = "CORAL"
608 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
609 R.Name = color + " Rectangle"
610 R.Bind(FloatCanvas.EVT_FC_MIDDLE_DCLICK, self.RectGotHit)
5e1796ef
RD
611 Canvas.AddText("Middle DoubleClick", x, y, Size = FontSize, Position = "bl")
612 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
613
614 x += dx
615 color = "CYAN"
616 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
617 R.Name = color + " Rectangle"
618 R.Bind(FloatCanvas.EVT_FC_RIGHT_UP, self.RectGotHit)
5e1796ef
RD
619 Canvas.AddText("Right Up", x, y, Size = FontSize, Position = "bl")
620 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
621
622 x += dx
623 color = "LIME GREEN"
624 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
625 R.Name = color + " Rectangle"
626 R.Bind(FloatCanvas.EVT_FC_RIGHT_DCLICK, self.RectGotHit)
5e1796ef
RD
627 Canvas.AddText("Right Double Click", x, y, Size = FontSize, Position = "bl")
628 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
629
630 x = 20
631 y += dy
632 color = "MEDIUM GOLDENROD"
633 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
634 R.Name = color
635 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
636 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
5e1796ef
RD
637 Canvas.AddText("L and R Click", x, y, Size = FontSize, Position = "bl")
638 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
639
640 x += dx
641 color = "SALMON"
642 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
643 R.Name = color + " Rectangle"
644 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
5e1796ef
RD
645 Canvas.AddText("Mouse Enter", x, y, Size = FontSize, Position = "bl")
646 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
647
648 x += dx
649 color = "MEDIUM VIOLET RED"
650 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
651 R.Name = color
652 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
5e1796ef
RD
653 Canvas.AddText("Mouse Leave", x, y, Size = FontSize, Position = "bl")
654 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
655
656 x = 20
657 y += dy
658 color = "SKY BLUE"
659 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
660 R.Name = color
661 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
662 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
5e1796ef
RD
663 Canvas.AddText("Enter and Leave", x, y, Size = FontSize, Position = "bl")
664 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
665
666 x += dx
667 color = "WHEAT"
668 R = Canvas.AddRectangle(x, y, w+12, h, LineColor = None, FillColor = color)
669 R.Name = color
670 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
671 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
5e1796ef
RD
672 Canvas.AddText("Mouse Enter&Leave", x, y, Size = FontSize, Position = "bl")
673 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
674
675 x += dx
676 color = "KHAKI"
677 R = Canvas.AddRectangle(x-12, y, w+12, h, LineColor = None, FillColor = color)
678 R.Name = color
679 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
680 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
5e1796ef
RD
681 Canvas.AddText("Mouse ENter&Leave", x, y, Size = FontSize, Position = "bl")
682 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
683
684 x = 20
685 y += dy
686 L = Canvas.AddLine(( (x, y), (x+10, y+10), (x+w, y+h) ), LineWidth = 2, LineColor = "Red")
687 L.Name = "A Line"
688 L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
5e1796ef
RD
689 Canvas.AddText("Left Down", x, y, Size = FontSize, Position = "bl")
690 Canvas.AddText(L.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
691
692 x += dx
693 color = "SEA GREEN"
694 Points = Numeric.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), Numeric.Float)
695 R = Canvas.AddPolygon(Points, LineWidth = 2, FillColor = color)
696 R.Name = color + " Polygon"
697 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
5e1796ef
RD
698 Canvas.AddText("RIGHT_DOWN", x, y, Size = FontSize, Position = "bl")
699 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
700
701 x += dx
702 color = "Red"
703 Points = Numeric.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), Numeric.Float)
704 R = Canvas.AddPointSet(Points, Diameter = 4, Color = color)
705 R.Name = "PointSet"
706 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.PointSetGotHit)
5e1796ef
RD
707 Canvas.AddText("LEFT_DOWN", x, y, Size = FontSize, Position = "bl")
708 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
42463de2
RD
709
710 x = 20
711 y += dy
712 T = Canvas.AddText("Hit-able Text", x, y, Size = 15, Color = "Red", Position = 'tl')
713 T.Name = "Hit-able Text"
714 T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
5e1796ef 715 Canvas.AddText("Left Down", x, y, Size = FontSize, Position = "bl")
42463de2
RD
716
717 x += dx
718 T = Canvas.AddScaledText("Scaled Text", x, y, Size = 1./2*h, Color = "Pink", Position = 'bl')
719 Canvas.AddPointSet( (x, y), Diameter = 3)
720 T.Name = "Scaled Text"
721 T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
5e1796ef 722 Canvas.AddText("Left Down", x, y, Size = FontSize, Position = "tl")
42463de2
RD
723
724 self.Canvas.ZoomToBB()
725
726 def TestHitTestForeground(self,event=None):
5e1796ef 727 wx.GetApp().Yield()
42463de2
RD
728
729 self.UnBindAllMouseEvents()
730 Canvas = self.Canvas
731
732 Canvas.ClearAll()
733 Canvas.SetProjectionFun(None)
734
735 #Add a Hitable rectangle
736 w, h = 60, 20
737
738 dx = 80
739 dy = 40
740 x,y = 20, 20
741
742 color = "Red"
743 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color, InForeground = False)
744 R.Name = color + "Rectangle"
745 R.HitFill = True
746 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
747 Canvas.AddText("Left Click Fill", x, y, Position = "bl")
748 Canvas.AddText(R.Name, x, y+h, Position = "tl")
749
750 ## A set of Rectangles that move together
751
752 ## NOTE: In a real app, it might be better to create a new
753 ## custom FloatCanvas DrawObject
754
755 self.MovingRects = []
756 x += dx
757 color = "LightBlue"
758 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
759 R.HitFill = True
760 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveLeft)
761 L = Canvas.AddText("Left", x + w/4, y + h/4, Position = "cc", InForeground = True)
762 self.MovingRects.extend( (R,L) )
763
764 x += w/2
765 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
766 R.HitFill = True
767 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveRight)
768 L = Canvas.AddText("Right", x + w/4, y + h/4, Position = "cc", InForeground = True)
769 self.MovingRects.extend( (R,L) )
770
771 x -= w/2
772 y += h/2
773 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
774 R.HitFill = True
775 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveUp)
776 L = Canvas.AddText("Up", x + w/4, y + h/4, Position = "cc", InForeground = True)
777 self.MovingRects.extend( (R,L) )
778
779
780 x += w/2
781 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
782 R.HitFill = True
783 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveDown)
784 L = Canvas.AddText("Down", x + w/4, y + h/4, Position = "cc", InForeground = True)
785 self.MovingRects.extend( (R,L) )
786
787 self.Canvas.ZoomToBB()
788
789 def RectMoveLeft(self,Object):
790 self.MoveRects("left")
791
792 def RectMoveRight(self,Object):
793 self.MoveRects("right")
794
795 def RectMoveUp(self,Object):
796 self.MoveRects("up")
797
798 def RectMoveDown(self,Object):
799 self.MoveRects("down")
800
801 def MoveRects(self, Dir):
802 for Object in self.MovingRects:
803 X,Y = Object.XY
804 if Dir == "left": X -= 10
805 elif Dir == "right": X += 10
806 elif Dir == "up": Y += 10
807 elif Dir == "down": Y -= 10
808 Object.SetXY(X,Y)
809 self.Canvas.Draw()
810
811
812 def PointSetGotHit(self, Object):
813 print Object.Name, "Got Hit\n"
814
815 def RectGotHit(self, Object):
816 print Object.Name, "Got Hit\n"
817
818 def RectGotHitRight(self, Object):
819 print Object.Name, "Got Hit With Right\n"
820
821 def RectGotHitLeft(self, Object):
822 print Object.Name, "Got Hit with Left\n"
823
824 def RectMouseOver(self, Object):
825 print "Mouse entered:", Object.Name
826
827 def RectMouseLeave(self, Object):
828 print "Mouse left ", Object.Name
829
830
831 def TestText(self, event= None):
5e1796ef 832 wx.GetApp().Yield()
42463de2
RD
833
834 self.BindAllMouseEvents()
835 Canvas = self.Canvas
836 Canvas.ClearAll()
837 Canvas.SetProjectionFun(None)
838
839 x,y = (0, 0)
840
841 ## Add a non-visible rectangle, just to get a Bounding Box
842 ## Text objects have a zero-size bounding box, because it changes with zoom
843 Canvas.AddRectangle(-10,-10,20,20,LineWidth = 1, LineColor = None)
844
845 # Text
846 String = "Some text"
42463de2
RD
847 self.Canvas.AddText("Top Left",x,y,Size = 14,Color = "Yellow",BackgroundColor = "Blue", Position = "tl")
848 self.Canvas.AddText("Bottom Left",x,y,Size = 14,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
849 self.Canvas.AddText("Top Right",x,y,Size = 14,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
850 self.Canvas.AddText("Bottom Right",x,y,Size = 14,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
851 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
852
853 x,y = (0, 2)
854
855 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
856 self.Canvas.AddText("Top Center",x,y,Size = 14,Color = "Black",Position = "tc")
857 self.Canvas.AddText("Bottom Center",x,y,Size = 14,Color = "White",Position = "bc")
858
859 x,y = (0, 4)
860
861 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
862 self.Canvas.AddText("Center Right",x,y,Size = 14,Color = "Black",Position = "cr")
863 self.Canvas.AddText("Center Left",x,y,Size = 14,Color = "Black",Position = "cl")
864
865 x,y = (0, -2)
866
867 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
868 self.Canvas.AddText("Center Center",x,y,Size = 14,Color = "Black",Position = "cc")
869
870 self.Canvas.AddText("40 Pixels",-10,8,Size = 40)
871 self.Canvas.AddText("20 Pixels",-10,5,Size = 20)
872 self.Canvas.AddText("10 Pixels",-10,3,Size = 10)
873
874 self.Canvas.AddText("MODERN Font", -10, 0, Family = wx.MODERN)
875 self.Canvas.AddText("DECORATIVE Font", -10, -1, Family = wx.DECORATIVE)
876 self.Canvas.AddText("ROMAN Font", -10, -2, Family = wx.ROMAN)
877 self.Canvas.AddText("SCRIPT Font", -10, -3, Family = wx.SCRIPT)
878 self.Canvas.AddText("ROMAN BOLD Font", -10, -4, Family = wx.ROMAN, Weight=wx.BOLD)
879 self.Canvas.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
880
881 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
882 Font = wx.Font(20, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "zapf chancery")
883 self.Canvas.AddText("zapf chancery Font", -10, -6, Font = Font)
884
885 self.Canvas.ZoomToBB()
886
887 def TestScaledText(self, event= None):
5e1796ef 888 wx.GetApp().Yield()
42463de2
RD
889
890 self.BindAllMouseEvents()
891 Canvas = self.Canvas
892 Canvas.ClearAll()
893 Canvas.SetProjectionFun(None)
894
895 x,y = (0, 0)
896
897 T = Canvas.AddScaledText("Top Left",x,y,Size = 5,Color = "Yellow",BackgroundColor = "Blue", Position = "tl")
898 T = Canvas.AddScaledText("Bottom Left",x,y,Size = 5,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
899 T = Canvas.AddScaledText("Top Right",x,y,Size = 5,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
900 T = Canvas.AddScaledText("Bottom Right",x,y,Size = 5,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
901 Canvas.AddPointSet((x,y), Color = "Red", Diameter = 4)
902
903
904 x,y = (0, 20)
905
906 Canvas.AddScaledText("Top Center",x,y,Size = 7,Color = "Black",Position = "tc")
907 Canvas.AddScaledText("Bottom Center",x,y,Size = 7,Color = "White",Position = "bc")
908 Canvas.AddPointSet((x,y), Color = "White", Diameter = 4)
909
910 x,y = (0, -20)
911
912 Canvas.AddScaledText("Center Right",x,y,Size = 9,Color = "Black",Position = "cr")
913 Canvas.AddScaledText("Center Left",x,y,Size = 9,Color = "Black",Position = "cl")
914 Canvas.AddPointSet((x,y), Color = "White", Diameter = 4)
915
916 x = -200
917
918 self.Canvas.AddScaledText("MODERN Font", x, 0, Size = 7, Family = wx.MODERN, Color = (0,0,0))
919 self.Canvas.AddScaledText("DECORATIVE Font", x, -10, Size = 7, Family = wx.DECORATIVE, Color = (0,0,1))
920 self.Canvas.AddScaledText("ROMAN Font", x, -20, Size = 7, Family = wx.ROMAN)
921 self.Canvas.AddScaledText("SCRIPT Font", x, -30, Size = 7, Family = wx.SCRIPT)
922 self.Canvas.AddScaledText("ROMAN BOLD Font", x, -40, Size = 7, Family = wx.ROMAN, Weight=wx.BOLD)
923 self.Canvas.AddScaledText("ROMAN ITALIC BOLD Font", x, -50, Size = 7, Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
924 Canvas.AddPointSet((x,0), Color = "White", Diameter = 4)
925
926
927 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
928 x,y = (-100, 50)
929 Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "zapf chancery")
930 T = self.Canvas.AddScaledText("zapf chancery Font", x, y, Size = 20, Font = Font, Position = 'bc')
931
932 x,y = (-50, -50)
933 Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "bookman")
934 T = self.Canvas.AddScaledText("Bookman Font", x, y, Size = 8, Font = Font)
935
8b9a4190
RD
936 self.Canvas.ZoomToBB()
937
938 def DrawMap(self,event = None):
5e1796ef 939 wx.GetApp().Yield()
8b9a4190 940 import os, time
42463de2
RD
941 self.BindAllMouseEvents()
942
8b9a4190 943 ## Test of Actual Map Data
42463de2
RD
944 self.Canvas.ClearAll()
945 self.Canvas.SetProjectionFun("FlatEarth")
946 #start = time.clock()
8b9a4190 947 Shorelines = Read_MapGen(os.path.join("data",'world.dat'),stats = 0)
42463de2
RD
948 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
949 #start = time.clock()
8b9a4190 950 for segment in Shorelines:
42463de2
RD
951 self.Canvas.AddLine(segment)
952 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
953 #start = time.clock()
8b9a4190 954 self.Canvas.ZoomToBB()
42463de2 955 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
8b9a4190 956
42463de2 957
8b9a4190 958 def LineTest(self,event = None):
5e1796ef 959 wx.GetApp().Yield()
8b9a4190 960 import os, time
42463de2
RD
961# import random
962 colors = self.colors
8b9a4190
RD
963 Range = (-10,10)
964 ## Test of drawing lots of lines
42463de2
RD
965 Canvas = self.Canvas
966 Canvas.ClearAll()
967 Canvas.SetProjectionFun(None)
968 #start = time.clock()
8b9a4190
RD
969 linepoints = []
970 linecolors = []
971 linewidths = []
972 for i in range(2000):
973 points = (random.randint(Range[0],Range[1]),
974 random.randint(Range[0],Range[1]),
975 random.randint(Range[0],Range[1]),
976 random.randint(Range[0],Range[1]))
977 linepoints.append(points)
978 linewidths.append(random.randint(1,10) )
979 linecolors.append(random.randint(0,len(colors)-1) )
980 for (points,color,width) in zip(linepoints,linecolors,linewidths):
42463de2
RD
981 Canvas.AddLine((points[0:2],points[2:4]), LineWidth = width, LineColor = colors[color])
982 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
983 #start = time.clock()
984 Canvas.ZoomToBB()
985 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
986
987 def SpeedTest(self,event=None):
5e1796ef 988 wx.GetApp().Yield()
42463de2
RD
989 BigRange = (-1000,1000)
990 colors = self.colors
991
992 self.UnBindAllMouseEvents()
993 Canvas = self.Canvas
994
995 Canvas.ClearAll()
996 Canvas.SetProjectionFun(None)
997
5e1796ef 998 # Pointset
42463de2 999 coords = []
5e1796ef 1000 for i in range(1000):
42463de2
RD
1001 x,y = (random.uniform(BigRange[0],BigRange[1]),random.uniform(BigRange[0],BigRange[1]))
1002 coords.append( (x,y) )
5e1796ef 1003 print "Drawing the Points"
8b9a4190 1004 start = time.clock()
5e1796ef
RD
1005 for Point in coords:
1006 Canvas.AddPoint(Point, Diameter = 4)
1007 print "It took %s seconds to add the points"%(time.clock() - start)
1008 Canvas.ZoomToBB()
1009
1010 def PropertiesChangeTest(self,event=None):
1011 wx.GetApp().Yield()
1012
1013 Range = (-10,10)
1014 colors = self.colors
1015
1016 self.UnBindAllMouseEvents()
1017 Canvas = self.Canvas
1018
1019 Canvas.ClearAll()
1020 Canvas.SetProjectionFun(None)
1021
1022 self.ColorObjectsAll = []
1023 self.ColorObjectsLine = []
1024 self.ColorObjectsColor = []
1025 self.ColorObjectsText = []
1026 ##One of each object:
1027 # Rectangle
1028 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1029 lw = random.randint(1,5)
1030 cf = random.randint(0,len(colors)-1)
1031 h = random.randint(1,5)
1032 w = random.randint(1,5)
1033 self.Rectangle = Canvas.AddRectangle(x,y,w,h,LineWidth = lw,FillColor = colors[cf])
1034 self.ColorObjectsAll.append(self.Rectangle)
42463de2 1035
5e1796ef
RD
1036 # Ellipse
1037 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1038 lw = random.randint(1,5)
1039 cf = random.randint(0,len(colors)-1)
1040 h = random.randint(1,5)
1041 w = random.randint(1,5)
1042 self.Ellipse = Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
1043 self.ColorObjectsAll.append(self.Ellipse)
42463de2 1044
5e1796ef
RD
1045 # Point
1046 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1047 D = random.randint(1,50)
1048 lw = random.randint(1,5)
1049 cf = random.randint(0,len(colors)-1)
1050 cl = random.randint(0,len(colors)-1)
1051 self.ColorObjectsColor.append(Canvas.AddPoint(xy, colors[cf], D))
42463de2 1052
5e1796ef
RD
1053 # Circle
1054 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1055 D = random.randint(1,5)
1056 lw = random.randint(1,5)
1057 cf = random.randint(0,len(colors)-1)
1058 cl = random.randint(0,len(colors)-1)
1059 self.Circle = Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
1060 self.ColorObjectsAll.append(self.Circle)
1061
1062 # Line
1063 points = []
1064 for j in range(random.randint(2,10)):
1065 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
1066 points.append(point)
1067 lw = random.randint(1,10)
1068 cf = random.randint(0,len(colors)-1)
1069 cl = random.randint(0,len(colors)-1)
1070 self.ColorObjectsLine.append(Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl]))
42463de2 1071
5e1796ef
RD
1072 # Polygon
1073## points = []
1074## for j in range(random.randint(2,6)):
1075## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1076## points.append(point)
1077 points = RandomArray.uniform(Range[0],Range[1],(6,2))
1078 lw = random.randint(1,6)
1079 cf = random.randint(0,len(colors)-1)
1080 cl = random.randint(0,len(colors)-1)
1081 self.ColorObjectsAll.append(Canvas.AddPolygon(points,
1082 LineWidth = lw,
1083 LineColor = colors[cl],
1084 FillColor = colors[cf],
1085 FillStyle = 'Solid'))
1086
1087 ## Pointset
1088 points = RandomArray.uniform(Range[0],Range[1],(100,2))
1089 cf = random.randint(0,len(colors)-1)
1090 D = random.randint(1,4)
1091 self.PointSet = Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
1092 self.ColorObjectsColor.append(self.PointSet)
42463de2 1093
5e1796ef
RD
1094 ## Point
1095 point = RandomArray.uniform(Range[0],Range[1],(2,))
1096 cf = random.randint(0,len(colors)-1)
1097 D = random.randint(1,4)
1098 self.Point = Canvas.AddPoint(point, Color = colors[cf], Diameter = D)
1099 self.ColorObjectsColor.append(self.Point)
1100
1101 # Text
1102 String = "Unscaled text"
1103 ts = random.randint(10,40)
1104 cf = random.randint(0,len(colors)-1)
1105 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1106 self.ColorObjectsText.append(Canvas.AddText(String, x, y, Size = ts, Color = colors[cf], Position = "cc"))
1107
1108 # Scaled Text
1109 String = "Scaled text"
1110 ts = random.random()*3 + 0.2
1111 cf = random.randint(0,len(colors)-1)
1112 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1113 self.ColorObjectsText.append(Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc"))
42463de2 1114
5e1796ef
RD
1115 # A "Button"
1116 Button = Canvas.AddRectangle(-10, -12, 20, 3, LineStyle = None, FillColor = "Red")
1117 Canvas.AddScaledText("Click Here To Change Properties",
1118 0, -10.5,
1119 Size = 0.7,
1120 Color = "Black",
1121 Position = "cc")
1122
1123 Button.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ChangeProperties)
42463de2
RD
1124
1125 Canvas.ZoomToBB()
1126
5e1796ef
RD
1127 def ChangeProperties(self, Object = None):
1128 colors = self.colors
1129 Range = (-10,10)
1130
1131 for Object in self.ColorObjectsAll:
1132 pass
1133 Object.SetFillColor(colors[random.randint(0,len(colors)-1)])
1134 Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
1135 Object.SetLineWidth(random.randint(1,7))
1136 Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
1137 for Object in self.ColorObjectsLine:
1138 Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
1139 Object.SetLineWidth(random.randint(1,7))
1140 Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
1141 for Object in self.ColorObjectsColor:
1142 Object.SetColor(colors[random.randint(0,len(colors)-1)])
1143 for Object in self.ColorObjectsText:
1144 Object.SetColor(colors[random.randint(0,len(colors)-1)])
1145 Object.SetBackgroundColor(colors[random.randint(0,len(colors)-1)])
1146 self.Circle.SetDiameter(random.randint(1,10))
1147 self.PointSet.SetDiameter(random.randint(1,8))
1148 self.Point.SetDiameter(random.randint(1,8))
1149 for Object in (self.Rectangle, self.Ellipse):
1150 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1151 w,h = random.randint(1,5), random.randint(1,5)
1152 Object.SetShape(x,y,w,h)
1153
1154 self.Canvas.Draw(Force = True)
42463de2
RD
1155
1156 def TempTest(self, event= None):
5e1796ef 1157 wx.GetApp().Yield()
42463de2
RD
1158
1159 self.UnBindAllMouseEvents()
1160 Canvas = self.Canvas
1161 Canvas.ClearAll()
1162 Canvas.SetProjectionFun(None)
1163
42463de2
RD
1164 Range = (-10,10)
1165
1166 # Create a random Polygon
1167 points = []
1168 for j in range(6):
1169 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1170 points.append(point)
1171 Poly = Canvas.AddPolygon(points,
1172 LineWidth = 2,
1173 LineColor = "Black",
1174 FillColor = "LightBlue",
1175 FillStyle = 'Solid')
1176
1177 Poly.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPoly)
1178
1179 self.SelectedPoly = None
1180 self.SelectPoints = []
1181 self.SelectedPoint = None
1182
1183 Canvas.ZoomToBB()
1184
1185 def SelectPoly(self, Object):
1186 print "In SelectPoly"
1187 Canvas = self.Canvas
1188 if Object is self.SelectedPoly:
1189 pass
1190 else:
1191 #fixme: Do something to unselect the old one
1192 self.SelectedPoly = Object
1193 Canvas.RemoveObjects(self.SelectPoints)
1194 self.SelectPoints = []
1195 # Draw points on the Vertices of the Selected Poly:
1196 for i, point in enumerate(Object.Points):
1197 P = Canvas.AddPointSet(point, Diameter = 6, Color = "Red")
1198 P.VerticeNum = i
1199 P.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPointHit)
1200 self.SelectPoints.append(P)
1201 #Canvas.ZoomToBB()
1202 Canvas.Draw()
1203
1204 def SelectPointHit(self, Point):
1205 print "Point Num: %i Hit"%Point.VerticeNum
1206 self.SelectedPoint = Point
1207
1208
1209
8fa876ca 1210 class DemoApp(wx.App):
8b9a4190
RD
1211 """
1212 How the demo works:
1213
1214 Under the Draw menu, there are three options:
1215
1216 *Draw Test: will put up a picture of a bunch of randomly generated
1217 objects, of each kind supported.
1218
1219 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1220 with a lot of data, and will take a while to load and draw (about 10 sec
1221 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1222 performance is not very good for large drawings.
1223
1224 *Clear: Clears the Canvas.
1225
1226 Once you have a picture drawn, you can zoom in and out and move about
1227 the picture. There is a tool bar with three tools that can be
1228 selected.
1229
1230 The magnifying glass with the plus is the zoom in tool. Once selected,
1231 if you click the image, it will zoom in, centered on where you
1232 clicked. If you click and drag the mouse, you will get a rubber band
1233 box, and the image will zoom to fit that box when you release it.
1234
1235 The magnifying glass with the minus is the zoom out tool. Once selected,
1236 if you click the image, it will zoom out, centered on where you
1237 clicked. (note that this takes a while when you are looking at the map,
1238 as it has a LOT of lines to be drawn. The image is double buffered, so
1239 you don't see the drawing in progress)
1240
1241 The hand is the move tool. Once selected, if you click and drag on the
1242 image, it will move so that the part you clicked on ends up where you
1243 release the mouse. Nothing is changed while you are dragging. The
1244 drawing is too slow for that.
1245
1246 I'd like the cursor to change as you change tools, but the stock
1247 wxCursors didn't include anything I liked, so I stuck with the
1248 pointer. Please let me know if you have any nice cursor images for me to
1249 use.
1250
1251
1252 Any bugs, comments, feedback, questions, and especially code are welcome:
1253
1254 -Chris Barker
1255
1256 Chris.Barker@noaa.gov
1257
1258 """
42463de2
RD
1259
1260 def __init__(self, *args, **kwargs):
1261 wx.App.__init__(self, *args, **kwargs)
8b9a4190
RD
1262
1263 def OnInit(self):
42463de2
RD
1264 wx.InitAllImageHandlers()
1265 frame = DrawFrame(None, -1, "FloatCanvas Demo App",wx.DefaultPosition,(700,700))
8b9a4190
RD
1266
1267 self.SetTopWindow(frame)
42463de2
RD
1268 frame.Show()
1269
1270 ## check to see if the demo is set to start in a particular mode.
1271 if StartUpDemo == "text":
1272 frame.TestText()
1273 if StartUpDemo == "stext":
1274 frame.TestScaledText()
1275 elif StartUpDemo == "all":
1276 frame.DrawTest()
1277 elif StartUpDemo == "map":
1278 frame.DrawMap()
1279 elif StartUpDemo == "hit":
1280 frame.TestHitTest()
1281 elif StartUpDemo == "hitf":
1282 "starting TestHitTestForeground"
1283 frame.TestHitTestForeground()
1284 elif StartUpDemo == "animate":
1285 "starting TestAnimation"
1286 frame.TestAnimation()
1287 elif StartUpDemo == "speed":
1288 "starting SpeedTest"
1289 frame.SpeedTest()
1290 elif StartUpDemo == "temp":
1291 "starting temp Test"
1292 frame.TempTest()
5e1796ef
RD
1293 elif StartUpDemo == "props":
1294 "starting PropertiesChange Test"
1295 frame.PropertiesChangeTest()
42463de2 1296
8b9a4190
RD
1297 return True
1298
1299 def Read_MapGen(filename,stats = 0,AllLines=0):
1300 """
1301 This function reads a MapGen Format file, and
1302 returns a list of NumPy arrays with the line segments in them.
1303
1304 Each NumPy array in the list is an NX2 array of Python Floats.
1305
1306 The demo should have come with a file, "world.dat" that is the
1307 shorelines of the whole world, in MapGen format.
1308
1309 """
1310 import string
8b9a4190
RD
1311 file = open(filename,'rt')
1312 data = file.readlines()
1313 data = map(string.strip,data)
1314
1315 Shorelines = []
1316 segment = []
1317 for line in data:
1318 if line:
1319 if line == "# -b": #New segment beginning
42463de2 1320 if segment: Shorelines.append(Numeric.array(segment))
8b9a4190
RD
1321 segment = []
1322 else:
1323 segment.append(map(float,string.split(line)))
42463de2 1324 if segment: Shorelines.append(Numeric.array(segment))
8b9a4190
RD
1325
1326 if stats:
1327 NumSegments = len(Shorelines)
1328 NumPoints = 0
1329 for segment in Shorelines:
1330 NumPoints = NumPoints + len(segment)
1331 AvgPoints = NumPoints / NumSegments
1332 print "Number of Segments: ", NumSegments
1333 print "Average Number of Points per segment: ",AvgPoints
1334 if AllLines:
1335 Lines = []
1336 for segment in Shorelines:
1337 Lines.append(segment[0])
1338 for point in segment[1:-1]:
1339 Lines.append(point)
1340 Lines.append(point)
1341 Lines.append(segment[-1])
8b9a4190
RD
1342 return Lines
1343 else:
1344 return Shorelines
1345
1346 ## for the wxPython demo:
42463de2
RD
1347 try:
1348 import floatcanvas
1349 except ImportError: # if it's not there locally, try the wxPython lib.
1350 from wx.lib import floatcanvas
fbd5dd1d 1351
42463de2 1352 overview = floatcanvas.__doc__
8b9a4190
RD
1353
1354if __name__ == "__main__":
1355 if not haveNumeric:
1356 print errorText
1357 else:
42463de2 1358 app = DemoApp(False)# put in True if you want output to go to it's own window.
8b9a4190
RD
1359 app.MainLoop()
1360
1361
1362
1363
42463de2
RD
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381