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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#!/usr/bin/env python
"""
This is a small demo, showing how to make an object that can be moved around.
"""
import wx
#ver = 'local'
ver = 'installed'
if ver == 'installed': ## import the installed version
from wx.lib.floatcanvas import NavCanvas, Resources
from wx.lib.floatcanvas import FloatCanvas as FC
print("using installed version: %s" % wx.lib.floatcanvas.__version__)
elif ver == 'local':
## import a local version
import sys
sys.path.append("..")
from floatcanvas import NavCanvas, Resources
from floatcanvas import FloatCanvas as FC
import numpy as np
## here we create a new DrawObject:
## code borrowed and adapted from Werner Bruhin
class ShapeMixin:
"""
just here for added features later
"""
def __init__(self):
pass
class TriangleShape1(FC.Polygon, ShapeMixin):
def __init__(self, XY, L):
"""
An equilateral triangle object
XY is the middle of the triangle
L is the length of one side of the Triangle
"""
XY = np.asarray(XY)
XY.shape = (2,)
Points = self.CompPoints(XY, L)
FC.Polygon.__init__(self, Points,
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 2,
FillColor = "Red",
FillStyle = "Solid")
ShapeMixin.__init__(self)
def CompPoints(self, XY, L):
c = L/ np.sqrt(3)
Points = np.array(((0, c),
( L/2.0, -c/2.0),
(-L/2.0, -c/2.0)),
np.float64)
Points += XY
return Points
class DrawFrame(wx.Frame):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)
self.CreateStatusBar()
# Add the Canvas
Canvas = NavCanvas.NavCanvas(self,-1,(500,500),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE",
).Canvas
self.Canvas = Canvas
Canvas.Bind(FC.EVT_MOTION, self.OnMove )
Canvas.Bind(FC.EVT_LEFT_UP, self.OnLeftUp )
Canvas.AddRectangle((-5,-5),
(10,10),
LineColor = "Red",
LineStyle = "Solid",
LineWidth = 2,
FillColor = "CYAN",
FillStyle = "Solid")
Points = np.array(((0,0),
(1,0),
(0.5, 1)),
np.float64)
data = (( (0,0), 1),
( (3,3), 2),
( (-2,3), 2.5 ),
)
for p, L in data:
Tri = TriangleShape1(p, 1)
Canvas.AddObject(Tri)
Tri.Bind(FC.EVT_FC_LEFT_DOWN, self.TriHit)
self.MoveTri = None
self.Show(True)
self.Canvas.ZoomToBB()
self.Moving = False
def TriHit(self, object):
print("In TriHit")
if not self.Moving:
self.Moving = True
self.StartPoint = object.HitCoordsPixel
self.StartTri = self.Canvas.WorldToPixel(object.Points)
self.MoveTri = None
self.MovingTri = object
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
And moves the triangle it it is clicked on
"""
self.SetStatusText("%.4f, %.4f"%tuple(event.Coords))
if self.Moving:
dxy = event.GetPosition() - self.StartPoint
# Draw the Moving Triangle:
dc = wx.ClientDC(self.Canvas)
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetLogicalFunction(wx.XOR)
if self.MoveTri is not None:
dc.DrawPolygon(self.MoveTri)
self.MoveTri = self.StartTri + dxy
dc.DrawPolygon(self.MoveTri)
def OnLeftUp(self, event):
if self.Moving:
self.Moving = False
if self.MoveTri is not None:
dxy = event.GetPosition() - self.StartPoint
dxy = self.Canvas.ScalePixelToWorld(dxy)
self.MovingTri.Move(dxy) ## The Move function has just been added
## to the FloatCanvas PointsObject
## It does the next three lines for you.
#self.Tri.Points += dxy
#self.Tri.BoundingBox += dxy
#self.Canvas.BoundingBoxDirty = True
self.MoveTri = None
self.Canvas.Draw(True)
if __name__ == "__main__":
app = wx.App(0)
x = DrawFrame(None, -1, "FloatCanvas TextBox Test App", wx.DefaultPosition, (700,700) )
x.Show()
app.MainLoop()
|