]>
Commit | Line | Data |
---|---|---|
51aad6d3 RD |
1 | |
2 | import wx | |
3 | import colorsys | |
4 | from math import cos, sin, radians | |
5 | ||
6 | #---------------------------------------------------------------------- | |
7 | ||
8 | BASE = 80.0 # sizes used in shapes drawn below | |
9 | BASE2 = BASE/2 | |
10 | BASE4 = BASE/4 | |
11 | ||
12 | ||
13 | class TestPanel(wx.Panel): | |
14 | def __init__(self, parent, log): | |
15 | self.log = log | |
16 | wx.Panel.__init__(self, parent, -1) | |
17 | ||
18 | self.Bind(wx.EVT_PAINT, self.OnPaint) | |
19 | ||
20 | def OnPaint(self, evt): | |
21 | dc = wx.PaintDC(self) | |
cbfc9df6 RD |
22 | try: |
23 | gc = wx.GraphicsContext.Create(dc) | |
24 | except NotImplementedError: | |
25 | dc.DrawText("This build of wxPython does not support the wx.GraphicsContext " | |
26 | "family of classes.", | |
27 | 25, 25) | |
28 | return | |
51aad6d3 RD |
29 | |
30 | font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) | |
31 | font.SetWeight(wx.BOLD) | |
32 | gc.SetFont(font) | |
33 | ||
34 | # make a path that contains a circle and some lines, centered at 0,0 | |
35 | path = gc.CreatePath() | |
36 | path.AddCircle(0, 0, BASE2) | |
37 | path.MoveToPoint(0, -BASE2) | |
38 | path.AddLineToPoint(0, BASE2) | |
39 | path.MoveToPoint(-BASE2, 0) | |
40 | path.AddLineToPoint(BASE2, 0) | |
41 | path.CloseSubpath() | |
42 | path.AddRectangle(-BASE4, -BASE4/2, BASE2, BASE4) | |
43 | ||
44 | ||
45 | # Now use that path to demonstrate various capbilites of the grpahics context | |
46 | gc.PushState() # save current translation/scale/other state | |
47 | gc.Translate(60, 75) # reposition the context origin | |
48 | ||
49 | gc.SetPen(wx.Pen("navy", 1)) | |
50 | gc.SetBrush(wx.Brush("pink")) | |
51aad6d3 RD |
51 | |
52 | # show the difference between stroking, filling and drawing | |
53 | for label, PathFunc in [("StrokePath", gc.StrokePath), | |
54 | ("FillPath", gc.FillPath), | |
55 | ("DrawPath", gc.DrawPath)]: | |
56 | if "wxGTK" in wx.PlatformInfo: | |
57 | w, h = dc.GetTextExtent(label) # NYI in Cairo context | |
58 | else: | |
59 | w, h = gc.GetTextExtent(label) | |
60 | ||
61 | gc.DrawText(label, -w/2, -BASE2-h) | |
62 | PathFunc(path) | |
63 | gc.Translate(2*BASE, 0) | |
64 | ||
65 | ||
66 | gc.PopState() # restore saved state | |
67 | gc.PushState() # save it again | |
68 | gc.Translate(60, 200) # offset to the lower part of the window | |
69 | ||
70 | gc.DrawText("Scale", 0, -BASE2) | |
71 | gc.Translate(0, 20) | |
72 | ||
cbfc9df6 RD |
73 | # for testing clipping |
74 | #gc.Clip(0, 0, 100, 100) | |
75 | #rgn = wx.RegionFromPoints([ (0,0), (75,0), (75,25,), (100, 25), | |
76 | # (100,100), (0,100), (0,0) ]) | |
77 | #gc.ClipRegion(rgn) | |
78 | #gc.ResetClip() | |
79 | ||
51aad6d3 RD |
80 | gc.SetBrush(wx.Brush(wx.Colour(178, 34, 34, 128))) # 128 == half transparent |
81 | for cnt in range(8): | |
82 | gc.Scale(1.08, 1.08) # increase scale by 8% | |
83 | gc.Translate(5,5) | |
84 | gc.DrawPath(path) | |
85 | ||
86 | ||
87 | gc.PopState() # restore saved state | |
88 | gc.PushState() # save it again | |
89 | gc.Translate(400, 200) | |
90 | gc.DrawText("Rotate", 0, -BASE2) | |
cbfc9df6 | 91 | |
51aad6d3 RD |
92 | gc.Translate(0, 75) |
93 | for angle in range(0, 360, 30): | |
94 | gc.PushState() # save this new current state so we can pop back to | |
95 | # it at the end of the loop | |
96 | r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(float(angle)/360, 1, 1)] | |
97 | gc.SetBrush(wx.Brush(wx.Colour(r, g, b, 64))) | |
98 | ||
99 | # use translate to artfully reposition each drawn path | |
100 | gc.Translate(1.5 * BASE2 * cos(radians(angle)), | |
101 | 1.5 * BASE2 * sin(radians(angle))) | |
102 | ||
103 | # use Rotate to rotate the path | |
104 | gc.Rotate(radians(angle)) | |
105 | ||
106 | # now draw it | |
107 | gc.DrawPath(path) | |
108 | gc.PopState() | |
109 | ||
110 | gc.PopState() | |
111 | ||
112 | ||
113 | #---------------------------------------------------------------------- | |
114 | ||
115 | def runTest(frame, nb, log): | |
116 | win = TestPanel(nb, log) | |
117 | return win | |
118 | ||
119 | #---------------------------------------------------------------------- | |
120 | ||
121 | ||
122 | ||
123 | overview = """<html><body> | |
124 | <h2><center>wx.GraphicsContext and wx.GraphicsPath</center></h2> | |
125 | ||
126 | The new advanced 2D drawing API is implemented via the | |
127 | wx.GraphicsContext and wx.GraphicsPath classes. They wrap GDI+ on | |
128 | Windows, Cairo on wxGTK and CoreGraphics on OS X. They allow | |
129 | path-based drawing with alpha-blending and anti-aliasing, and use a | |
130 | floating point cooridnate system. | |
131 | ||
132 | <p>When viewing this sample pay attention to how the rounded edges are | |
133 | smoothed with anti-aliased drawing, and how the shapes on the lower | |
134 | half of the window are partially transparent, allowing you to see what | |
135 | was drawn before. | |
136 | ||
137 | </body></html> | |
138 | """ | |
139 | ||
140 | ||
141 | ||
142 | if __name__ == '__main__': | |
143 | import sys,os | |
144 | import run | |
145 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) | |
146 |