7
|
1 from wxPython.wx import *
|
|
2 from wxPython.ogl import *
|
|
3
|
|
4 #wxOGLInitialize()
|
|
5
|
|
6
|
|
7 class MyEvtHandler(wxShapeEvtHandler):
|
|
8 """
|
|
9 Overwrite the default event handler to implement some custom features.
|
|
10 """
|
|
11 def __init__(self):
|
|
12 wxShapeEvtHandler.__init__(self)
|
|
13
|
|
14 def OnLeftClick(self, x, y, keys = 0, attachment = 0):
|
|
15 """
|
|
16 The dragging is done here.
|
|
17 You should probably comment out the EVT_MOTION below, to see it work.
|
|
18 """
|
|
19 shape = self.GetShape()
|
|
20 print shape.__class__, shape.GetClassName(), shape.a
|
|
21 canvas = shape.GetCanvas()
|
|
22 dc = wxClientDC(canvas)
|
|
23 canvas.PrepareDC(dc)
|
|
24
|
|
25 if shape.Selected():
|
|
26 shape.Select(False, dc)
|
|
27 canvas.Redraw(dc)
|
|
28 else:
|
|
29 redraw = False
|
|
30 shapeList = canvas.GetDiagram().GetShapeList()
|
|
31 toUnselect = []
|
|
32 for s in shapeList:
|
|
33 if s.Selected():
|
|
34 toUnselect.append(s)
|
|
35
|
|
36 shape.Select(True, dc)
|
|
37
|
|
38 if toUnselect:
|
|
39 for s in toUnselect:
|
|
40 s.Select(False, dc)
|
|
41 canvas.Redraw(dc)
|
|
42
|
|
43
|
|
44 class OGLCanvas(wxShapeCanvas):
|
|
45 def __init__(self, parent, frame):
|
|
46 wxShapeCanvas.__init__(self, parent)
|
|
47
|
|
48 self.SetBackgroundColour("LIGHT BLUE")
|
|
49 self.diagram = wxDiagram()
|
|
50 self.SetDiagram(self.diagram)
|
|
51 self.diagram.SetCanvas(self)
|
|
52
|
|
53 self.circle = wxCircleShape(100)
|
|
54 self.circle.SetCanvas(self)
|
|
55 self.circle.a="Circle identified"
|
|
56 self.diagram.AddShape(self.circle)
|
|
57 self.circle.Show(True)
|
|
58
|
|
59 self.evthandler = MyEvtHandler()
|
|
60 self.evthandler.SetShape(self.circle)
|
|
61 self.evthandler.SetPreviousHandler(self.circle.GetEventHandler())
|
|
62 self.circle.SetEventHandler(self.evthandler)
|
|
63
|
|
64 EVT_MOTION(self, self.OnMotion)
|
|
65
|
|
66 def OnMotion(self, event):
|
|
67 shape = self.evthandler.GetShape()
|
|
68
|
|
69 bx = shape.GetX()
|
|
70 by = shape.GetY()
|
|
71 bw, bh = shape.GetBoundingBoxMax()
|
|
72 oldrect = wxRect(int(bx-bw/2)-1, int(by-bh/2)-1, int(bw)+2, int(bh)+2)
|
|
73
|
|
74 canvas = shape.GetCanvas()
|
|
75 dc = wxClientDC(canvas)
|
|
76 canvas.PrepareDC(dc)
|
|
77
|
|
78 shape.Move(dc, event.GetPosition()[0], event.GetPosition()[1])
|
|
79 canvas.Refresh(False, oldrect)
|
|
80 event.Skip()
|
|
81
|
|
82
|
|
83 class OGLFrame(wxFrame):
|
|
84 def __init__(self, *args, **kwds):
|
|
85 wxFrame.__init__(self, *args, **kwds)
|
|
86
|
|
87 self.SetTitle("OGL TEST")
|
|
88 self.SetBackgroundColour(wxColour(8, 197, 248))
|
|
89 self.canvas = OGLCanvas(self, self)
|
|
90
|
|
91 class Main(wxApp):
|
|
92 def OnInit(self):
|
|
93 wxInitAllImageHandlers()
|
|
94 frame = OGLFrame(None, -1, "")
|
|
95 self.SetTopWindow(frame)
|
|
96 frame.Show(True)
|
|
97 return True
|
|
98
|
|
99 if __name__ == "__main__":
|
|
100 app = Main()
|
|
101 app.MainLoop()
|