]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxOGL.py
Moved wxPy_ConvertList function from oglhelpers to helpers
[wxWidgets.git] / utils / wxPython / demo / wxOGL.py
1
2 from wxPython.wx import *
3 from wxPython.ogl import *
4
5 #----------------------------------------------------------------------
6 # This creates some pens and brushes that the OGL library uses.
7
8 wxOGLInitialize()
9
10 #----------------------------------------------------------------------
11
12 class DiamondShape(wxPolygonShape):
13 def __init__(self, w=0.0, h=0.0):
14 wxPolygonShape.__init__(self)
15 if w == 0.0:
16 w = 60.0
17 if h == 0.0:
18 h = 60.0
19
20 ## Either wxRealPoints or 2-tuples of floats works.
21
22 #points = [ wxRealPoint(0.0, -h/2.0),
23 # wxRealPoint(w/2.0, 0.0),
24 # wxRealPoint(0.0, h/2.0),
25 # wxRealPoint(-w/2.0, 0.0),
26 # ]
27 points = [ (0.0, -h/2.0),
28 (w/2.0, 0.0),
29 (0.0, h/2.0),
30 (-w/2.0, 0.0),
31 ]
32
33 self.Create(points)
34
35
36 #----------------------------------------------------------------------
37
38 class RoundedRectangleShape(wxRectangleShape):
39 def __init__(self, w=0.0, h=0.0):
40 wxRectangleShape.__init__(self, w, h)
41 self.SetCornerRadius(-0.3)
42
43
44 #----------------------------------------------------------------------
45
46 class MyEvtHandler(wxShapeEvtHandler):
47 def __init__(self, log, frame):
48 wxShapeEvtHandler.__init__(self)
49 self.log = log
50 self.statbarFrame = frame
51
52
53 def UpdateStatusBar(self, shape):
54 x,y = shape.GetX(), shape.GetY()
55 width, height = shape.GetBoundingBoxMax()
56 self.statbarFrame.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
57 (x, y, width, height))
58
59
60 def OnLeftClick(self, x, y, keys = 0, attachment = 0):
61 shape = self.GetShape()
62 canvas = shape.GetCanvas()
63 dc = wxClientDC(canvas)
64 canvas.PrepareDC(dc)
65
66 if shape.Selected():
67 shape.Select(false, dc)
68 canvas.Redraw(dc)
69 else:
70 redraw = false
71 shapeList = canvas.GetDiagram().GetShapeList()
72 toUnselect = []
73 for s in shapeList:
74 if s.Selected():
75 # If we unselect it now then some of the objects in
76 # shapeList will become invalid (the control points are
77 # shapes too!) and bad things will happen...
78 toUnselect.append(s)
79
80 shape.Select(true, dc)
81
82 if toUnselect:
83 for s in toUnselect:
84 s.Select(false, dc)
85 canvas.Redraw(dc)
86
87 self.UpdateStatusBar(shape)
88
89
90 def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
91 shape = self.GetShape()
92 self.base_OnEndDragLeft(x, y, keys, attachment)
93 if not shape.Selected():
94 self.OnLeftClick(x, y, keys, attachment)
95 self.UpdateStatusBar(shape)
96
97
98 def OnSize(self, x, y):
99 self.base_OnSize(x, y)
100 self.UpdateStatusBar(self.GetShape())
101
102
103 # def OnMovePost(self, dc, x, y, oldX, oldY, display):
104 # self.base_OnMovePost(dc, x, y, oldX, oldY, display)
105 # self.UpdateStatusBar(self.GetShape())
106
107
108 def OnRightClick(self, *dontcare):
109 self.log.WriteText("%s\n" % self.GetShape())
110
111
112 #----------------------------------------------------------------------
113
114 class TestWindow(wxShapeCanvas):
115 def __init__(self, parent, log, frame):
116 wxShapeCanvas.__init__(self, parent)
117
118 self.log = log
119 self.frame = frame
120 self.SetBackgroundColour(wxWHITE)
121 self.diagram = wxDiagram()
122 self.SetDiagram(self.diagram)
123 self.diagram.SetCanvas(self)
124 self.shapes = []
125
126 self.MyAddShape(wxCircleShape(80), 100, 100, wxPen(wxBLUE, 3), wxGREEN_BRUSH)
127 self.MyAddShape(wxRectangleShape(85, 50), 305, 60, wxBLACK_PEN, wxLIGHT_GREY_BRUSH)
128 self.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE, 3, wxDOT), wxRED_BRUSH)
129 self.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED, 1), wxBLUE_BRUSH)
130
131 dc = wxClientDC(self)
132 self.PrepareDC(dc)
133 for x in range(len(self.shapes)):
134 fromShape = self.shapes[x]
135 if x+1 == len(self.shapes):
136 toShape = self.shapes[0]
137 else:
138 toShape = self.shapes[x+1]
139 line = wxLineShape()
140 line.SetCanvas(self)
141 line.SetPen(wxBLACK_PEN)
142 line.SetBrush(wxBLACK_BRUSH)
143 line.AddArrow(ARROW_ARROW)
144 line.MakeLineControlPoints(2)
145 fromShape.AddLine(line, toShape)
146 self.diagram.AddShape(line)
147 line.Show(true)
148
149 # for some reason, the shapes have to be moved for the line to show up...
150 fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
151
152
153
154 def MyAddShape(self, shape, x, y, pen, brush):
155 shape.SetDraggable(true)
156 shape.SetCanvas(self)
157 shape.SetX(x)
158 shape.SetY(y)
159 shape.SetPen(pen)
160 shape.SetBrush(brush)
161 #shape.SetShadowMode(SHADOW_RIGHT)
162 self.diagram.AddShape(shape)
163 shape.Show(true)
164
165 evthandler = MyEvtHandler(self.log, self.frame)
166 evthandler.SetShape(shape)
167 evthandler.SetPreviousHandler(shape.GetEventHandler())
168 shape.SetEventHandler(evthandler)
169
170 self.shapes.append(shape)
171
172
173 def __del__(self):
174 for shape in self.diagram.GetShapeList():
175 if shape.GetParent() == None:
176 shape.SetCanvas(None)
177 shape.Destroy()
178
179
180 #----------------------------------------------------------------------
181
182 def runTest(frame, nb, log):
183 win = TestWindow(nb, log, frame)
184 return win
185
186 #----------------------------------------------------------------------
187
188 class __Cleanup:
189 cleanup = wxOGLCleanUp
190 def __del__(self):
191 self.cleanup()
192
193 # when this module gets cleaned up then wxOGLCleanUp() will get called
194 __cu = __Cleanup()
195
196
197
198
199
200
201
202
203 overview = """\
204 The Object Graphics Library is a library supporting the creation and
205 manipulation of simple and complex graphic images on a canvas.
206
207 """
208
209