]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxOGL.py
2 from wxPython
.wx
import *
3 from wxPython
.ogl
import *
5 #----------------------------------------------------------------------
6 # This creates some pens and brushes that the OGL library uses.
10 #----------------------------------------------------------------------
12 class DiamondShape(wxPolygonShape
):
13 def __init__(self
, w
=0.0, h
=0.0):
14 wxPolygonShape
.__init
__(self
)
20 ## Either wxRealPoints or 2-tuples of floats works.
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),
27 points
= [ (0.0, -h
/2.0),
36 #----------------------------------------------------------------------
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)
44 #----------------------------------------------------------------------
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())
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)
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)
69 ## self.AddChild(self.bs)
70 ## self.AddChild(self.ts, self.bs)
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)
78 #----------------------------------------------------------------------
80 class MyEvtHandler(wxShapeEvtHandler
):
81 def __init__(self
, log
, frame
):
82 wxShapeEvtHandler
.__init
__(self
)
84 self
.statbarFrame
= frame
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
))
93 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
94 shape
= self
.GetShape()
95 canvas
= shape
.GetCanvas()
96 dc
= wxClientDC(canvas
)
100 shape
.Select(false
, dc
)
104 shapeList
= canvas
.GetDiagram().GetShapeList()
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...
113 shape
.Select(true
, dc
)
120 self
.UpdateStatusBar(shape
)
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
)
131 def OnSize(self
, x
, y
):
132 self
.base_OnSize(x
, y
)
133 self
.UpdateStatusBar(self
.GetShape())
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())
141 def OnRightClick(self
, *dontcare
):
142 self
.log
.WriteText("%s\n" % self
.GetShape())
145 #----------------------------------------------------------------------
147 class TestWindow(wxShapeCanvas
):
148 def __init__(self
, parent
, log
, frame
):
149 wxShapeCanvas
.__init
__(self
, parent
)
153 self
.SetScrollbars(20, 20, maxWidth
/20, maxHeight
/20)
157 self
.SetBackgroundColour(wxWHITE
)
158 self
.diagram
= wxDiagram()
159 self
.SetDiagram(self
.diagram
)
160 self
.diagram
.SetCanvas(self
)
164 rRectBrush
= wxBrush(wxNamedColour("MEDIUM TURQUOISE"), wxSOLID
)
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")
171 bmp
= wxBitmap('bitmaps/test2.bmp', wxBITMAP_TYPE_BMP
)
172 mask
= wxMaskColour(bmp
, wxBLUE
)
174 ## s = LabeledBitmapShape(self, bmp, "Hello")
175 ## self.MyAddShape(s, 225, 150, None, None, None)
176 ## print s.Recompute()
178 ## print s.GetX(), s.GetY()
182 ## print s.GetX(), s.GetY()
183 ## #print s.GetSize()
187 self
.MyAddShape(s
, 225, 150, None, None, "Bitmap")
189 dc
= wxClientDC(self
)
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]
196 toShape
= self
.shapes
[x
+1]
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
)
207 # for some reason, the shapes have to be moved for the line to show up...
208 fromShape
.Move(dc
, fromShape
.GetX(), fromShape
.GetY())
212 def MyAddShape(self
, shape
, x
, y
, pen
, brush
, text
):
213 shape
.SetDraggable(true
, true
)
214 shape
.SetCanvas(self
)
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
)
224 evthandler
= MyEvtHandler(self
.log
, self
.frame
)
225 evthandler
.SetShape(shape
)
226 evthandler
.SetPreviousHandler(shape
.GetEventHandler())
227 shape
.SetEventHandler(evthandler
)
229 self
.shapes
.append(shape
)
234 for shape
in self
.diagram
.GetShapeList():
235 if shape
.GetParent() == None:
236 shape
.SetCanvas(None)
240 #----------------------------------------------------------------------
242 def runTest(frame
, nb
, log
):
243 win
= TestWindow(nb
, log
, frame
)
246 #----------------------------------------------------------------------
249 cleanup
= wxOGLCleanUp
253 # when this module gets cleaned up then wxOGLCleanUp() will get called
264 The Object Graphics Library is a library supporting the creation and
265 manipulation of simple and complex graphic images on a canvas.