1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#!/usr/bin/env python
"""
A simple demo to show how to drw a bitmap on top of the Canvas at given pixel coords.
"""
import wx
import numpy as np
try:
# See if there is a local copy
import sys
sys.path.append("../")
from floatcanvas import NavCanvas, FloatCanvas
except ImportError:
from wx.lib.floatcanvas import NavCanvas, FloatCanvas
FC = FloatCanvas
class PixelBitmap:
"""
An unscaled bitmap that can be put on top of the canvas using:
Canvas.GridOver = MyPixelBitmap
It will always be drawn on top of everything else, and be positioned
according to pixel coordinates on the screen, regardless of zoom and
pan position.
"""
def __init__(self, Bitmap, XY, Position = 'tl'):
"""
PixelBitmap (Bitmap, XY, Position='tl')
Bitmap is a wx.Bitmap or wx.Image
XY is the (x,y) location to place the bitmap, in pixel coordinates
Position indicates from where in the window the position is relative to:
'tl' indicated the position from the top left the the window (the detault)
'br' the bottom right
'cr the center right, etc.
"""
if type(Bitmap) == wx.Bitmap:
self.Bitmap = Bitmap
elif type(Bitmap) == wx.Image:
self.Bitmap = wx.BitmapFromImage(Bitmap)
else:
raise FC.FloatCanvasError("PixelBitmap takes only a wx.Bitmap or a wx.Image as input")
self.XY = np.asarray(XY, dtype=int).reshape((2,))
self.Position = Position
(self.Width, self.Height) = self.Bitmap.GetWidth(), self.Bitmap.GetHeight()
self.ShiftFun = FC.TextObjectMixin.ShiftFunDict[Position]
def _Draw(self, dc, Canvas):
w, h = Canvas.Size
XY = self.XY
if self.Position[0] == 'b':
XY = (XY[0], h - XY[1] - self.Height)
elif self.Position[0] == 'c':
XY = (XY[0], XY[1] + (h - self.Height)/2)
if self.Position[1] == 'r':
XY = (w - XY[0] - self.Width, XY[1])
elif self.Position[1] == 'c':
XY = (XY[0] + (w - self.Width)/2, XY[1])
dc.DrawBitmap(self.Bitmap, XY, True)
class GridGroup:
def __init__(self, grids=[]):
self.Grids = grids
def _Draw(self, *args):
for grid in self.Grids:
grid._Draw(*args)
class DrawFrame(wx.Frame):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.CreateStatusBar()
# Add the Canvas
Canvas = NavCanvas.NavCanvas(self,-1,
size = (500,500),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE",
).Canvas
Point = (45,40)
Box = Canvas.AddCircle(Point,
Diameter = 10,
FillColor = "Black",
LineColor = "Red",
LineWidth = 6)
bmp = wx.Bitmap('NOAA.png')
grids = GridGroup([PixelBitmap(bmp, (10, 10), Position='tl'),
PixelBitmap(bmp, (10, 10), Position='br'),
PixelBitmap(bmp, (10, 10), Position='tr'),
PixelBitmap(bmp, (10, 10), Position='bl'),
PixelBitmap(bmp, (10, 10), Position='cl'),
PixelBitmap(bmp, (10, 10), Position='cr'),
PixelBitmap(bmp, (10, 10), Position='cc'),
PixelBitmap(bmp, (10, 10), Position='tc'),
PixelBitmap(bmp, (10, 10), Position='bc'),
])
Canvas.GridOver = grids
FloatCanvas.EVT_MOTION(Canvas, self.OnMove )
self.Show()
Canvas.ZoomToBB()
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
"""
self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
app = wx.App(False) # true to get its own output window.
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700,700) )
app.MainLoop()
|