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