]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxOGL.py
Other odds and ends...
[wxWidgets.git] / 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 LabeledBitmapShape(wxBitmapShape):
47 ## def __init__(self, bmp, text):
48 ## wxBitmapShape.__init__(self)
49 ## self.SetBitmap(bmp)
50 ## self.region = r = wxShapeRegion()
51 ## r.SetPosition(0, 0) #bmp.GetHeight())
52 ## r.SetSize(bmp.GetWidth(), bmp.GetHeight())
53 ## r.SetText(text)
54 ## self.AddRegion(r)
55
56 ## def OnMovePost(self, dc, x, y, old_x, old_y, display):
57 ## self.region.SetPosition(x, y)
58 ## self.base_OnMovePost(dc, x, y, old_x, old_y, display)
59
60
61 ## class LabeledBitmapShape(wxCompositeShape):
62 ## def __init__(self, canvas, bmp, text):
63 ## wxCompositeShape.__init__(self)
64 ## self.bs = wxBitmapShape()
65 ## self.bs.SetBitmap(bmp)
66 ## self.ts = wxTextShape()
67 ## self.ts.AddText(text)
68
69 ## self.AddChild(self.bs)
70 ## self.AddChild(self.ts, self.bs)
71
72 ## self.AddConstrainedShapes(gyCONSTRAINT_CENTRED_VERTICALLY, self, [self.bs, self.ts])
73 ## self.AddSimpleConstraint(gyCONSTRAINT_BELOW, self.bs, self.ts)
74 ## self.AddSimpleConstraint(gyCONSTRAINT_ALIGNED_TOP, self, self.bs)
75 ## self.AddSimpleConstraint(gyCONSTRAINT_ALIGNED_BOTTOM, self, self.ts)
76
77
78 #----------------------------------------------------------------------
79
80 class MyEvtHandler(wxShapeEvtHandler):
81 def __init__(self, log, frame):
82 wxShapeEvtHandler.__init__(self)
83 self.log = log
84 self.statbarFrame = frame
85
86 def UpdateStatusBar(self, shape):
87 x,y = shape.GetX(), shape.GetY()
88 width, height = shape.GetBoundingBoxMax()
89 self.statbarFrame.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
90 (x, y, width, height))
91
92
93 def OnLeftClick(self, x, y, keys = 0, attachment = 0):
94 shape = self.GetShape()
95 canvas = shape.GetCanvas()
96 dc = wxClientDC(canvas)
97 canvas.PrepareDC(dc)
98
99 if shape.Selected():
100 shape.Select(false, dc)
101 canvas.Redraw(dc)
102 else:
103 redraw = false
104 shapeList = canvas.GetDiagram().GetShapeList()
105 toUnselect = []
106 for s in shapeList:
107 if s.Selected():
108 # If we unselect it now then some of the objects in
109 # shapeList will become invalid (the control points are
110 # shapes too!) and bad things will happen...
111 toUnselect.append(s)
112
113 shape.Select(true, dc)
114
115 if toUnselect:
116 for s in toUnselect:
117 s.Select(false, dc)
118 canvas.Redraw(dc)
119
120 self.UpdateStatusBar(shape)
121
122
123 def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
124 shape = self.GetShape()
125 self.base_OnEndDragLeft(x, y, keys, attachment)
126 if not shape.Selected():
127 self.OnLeftClick(x, y, keys, attachment)
128 self.UpdateStatusBar(shape)
129
130
131 def OnSize(self, x, y):
132 self.base_OnSize(x, y)
133 self.UpdateStatusBar(self.GetShape())
134
135
136 # def OnMovePost(self, dc, x, y, oldX, oldY, display):
137 # self.base_OnMovePost(dc, x, y, oldX, oldY, display)
138 # self.UpdateStatusBar(self.GetShape())
139
140
141 def OnRightClick(self, *dontcare):
142 self.log.WriteText("%s\n" % self.GetShape())
143
144
145 #----------------------------------------------------------------------
146
147 class TestWindow(wxShapeCanvas):
148 def __init__(self, parent, log, frame):
149 wxShapeCanvas.__init__(self, parent)
150
151 maxWidth = 1000
152 maxHeight = 1000
153 self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
154
155 self.log = log
156 self.frame = frame
157 self.SetBackgroundColour(wxWHITE)
158 self.diagram = wxDiagram()
159 self.SetDiagram(self.diagram)
160 self.diagram.SetCanvas(self)
161 self.shapes = []
162 self.save_gdi = []
163
164 rRectBrush = wxBrush(wxNamedColour("MEDIUM TURQUOISE"), wxSOLID)
165
166 self.MyAddShape(wxCircleShape(80), 100, 100, wxPen(wxBLUE, 3), wxGREEN_BRUSH, "Circle")
167 self.MyAddShape(wxRectangleShape(85, 50), 305, 60, wxBLACK_PEN, wxLIGHT_GREY_BRUSH, "Rectangle")
168 self.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE, 3, wxDOT), wxRED_BRUSH, "Polygon")
169 self.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED, 1), rRectBrush, "Rounded Rect")
170
171 bmp = wxBitmap('bitmaps/test2.bmp', wxBITMAP_TYPE_BMP)
172 mask = wxMaskColour(bmp, wxBLUE)
173 bmp.SetMask(mask)
174 ## s = LabeledBitmapShape(self, bmp, "Hello")
175 ## self.MyAddShape(s, 225, 150, None, None, None)
176 ## print s.Recompute()
177 ## s.CalculateSize()
178 ## print s.GetX(), s.GetY()
179 ## s.SetSize(225)
180 ## s.SetY(150)
181 ## s.SetSize(80, 80)
182 ## print s.GetX(), s.GetY()
183 ## #print s.GetSize()
184
185 s = wxBitmapShape()
186 s.SetBitmap(bmp)
187 self.MyAddShape(s, 225, 150, None, None, "Bitmap")
188
189 dc = wxClientDC(self)
190 self.PrepareDC(dc)
191 for x in range(len(self.shapes)):
192 fromShape = self.shapes[x]
193 if x+1 == len(self.shapes):
194 toShape = self.shapes[0]
195 else:
196 toShape = self.shapes[x+1]
197 line = wxLineShape()
198 line.SetCanvas(self)
199 line.SetPen(wxBLACK_PEN)
200 line.SetBrush(wxBLACK_BRUSH)
201 line.AddArrow(ARROW_ARROW)
202 line.MakeLineControlPoints(2)
203 fromShape.AddLine(line, toShape)
204 self.diagram.AddShape(line)
205 line.Show(true)
206
207 # for some reason, the shapes have to be moved for the line to show up...
208 fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
209
210
211
212 def MyAddShape(self, shape, x, y, pen, brush, text):
213 shape.SetDraggable(true, true)
214 shape.SetCanvas(self)
215 shape.SetX(x)
216 shape.SetY(y)
217 if pen: shape.SetPen(pen)
218 if brush: shape.SetBrush(brush)
219 if text: shape.AddText(text)
220 #shape.SetShadowMode(SHADOW_RIGHT)
221 self.diagram.AddShape(shape)
222 shape.Show(true)
223
224 evthandler = MyEvtHandler(self.log, self.frame)
225 evthandler.SetShape(shape)
226 evthandler.SetPreviousHandler(shape.GetEventHandler())
227 shape.SetEventHandler(evthandler)
228
229 self.shapes.append(shape)
230
231
232
233 def __del__(self):
234 for shape in self.diagram.GetShapeList():
235 if shape.GetParent() == None:
236 shape.SetCanvas(None)
237 shape.Destroy()
238
239
240 #----------------------------------------------------------------------
241
242 def runTest(frame, nb, log):
243 win = TestWindow(nb, log, frame)
244 return win
245
246 #----------------------------------------------------------------------
247
248 class __Cleanup:
249 cleanup = wxOGLCleanUp
250 def __del__(self):
251 self.cleanup()
252
253 # when this module gets cleaned up then wxOGLCleanUp() will get called
254 __cu = __Cleanup()
255
256
257
258
259
260
261
262
263 overview = """\
264 The Object Graphics Library is a library supporting the creation and
265 manipulation of simple and complex graphic images on a canvas.
266
267 """
268
269