From 51aad6d34f4f434686868d926437c8cad3984824 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Thu, 26 Oct 2006 03:34:54 +0000 Subject: [PATCH] Add a GraphicsContext sample to the demo git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42424 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/demo/AlphaDrawing.py | 14 ++-- wxPython/demo/GraphicsContext.py | 134 +++++++++++++++++++++++++++++++ wxPython/demo/Main.py | 2 + wxPython/docs/CHANGES.txt | 2 + 4 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 wxPython/demo/GraphicsContext.py diff --git a/wxPython/demo/AlphaDrawing.py b/wxPython/demo/AlphaDrawing.py index 9834afe4f2..7023b7beda 100644 --- a/wxPython/demo/AlphaDrawing.py +++ b/wxPython/demo/AlphaDrawing.py @@ -50,13 +50,13 @@ def runTest(frame, nb, log): overview = """

Alpha Drawing

-The wx.DC on Mac now supports alpha transparent drawing using pens and -brushes. This is accomplished by enabling the wx.Colour class to have -a fourth component for the alpha value, where 0 is fully transparent, -and 255 is fully opaque. - -

You can consider this a \"preview of coming attractions\" for the -other platforms. +The wx.GCDC class is a class that implemented the wx.DC API using the +new wx.GraphicsContext class, and so it supports anti-aliased drawing +using pens and brushes, that can optionally also be drawn using an +alpha transparency. (On the Mac all the DC classes are using this new +implementation.) This is accomplished by enabling the wx.Colour class +to have a fourth component for the alpha value, where 0 is fully +transparent, and 255 is fully opaque. """ diff --git a/wxPython/demo/GraphicsContext.py b/wxPython/demo/GraphicsContext.py new file mode 100644 index 0000000000..a383ff7425 --- /dev/null +++ b/wxPython/demo/GraphicsContext.py @@ -0,0 +1,134 @@ + +import wx +import colorsys +from math import cos, sin, radians + +#---------------------------------------------------------------------- + +BASE = 80.0 # sizes used in shapes drawn below +BASE2 = BASE/2 +BASE4 = BASE/4 + + +class TestPanel(wx.Panel): + def __init__(self, parent, log): + self.log = log + wx.Panel.__init__(self, parent, -1) + + self.Bind(wx.EVT_PAINT, self.OnPaint) + + def OnPaint(self, evt): + dc = wx.PaintDC(self) + gc = wx.GraphicsContext.Create(dc) + + font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) + font.SetWeight(wx.BOLD) + gc.SetFont(font) + + # make a path that contains a circle and some lines, centered at 0,0 + path = gc.CreatePath() + path.AddCircle(0, 0, BASE2) + path.MoveToPoint(0, -BASE2) + path.AddLineToPoint(0, BASE2) + path.MoveToPoint(-BASE2, 0) + path.AddLineToPoint(BASE2, 0) + path.CloseSubpath() + path.AddRectangle(-BASE4, -BASE4/2, BASE2, BASE4) + + + # Now use that path to demonstrate various capbilites of the grpahics context + gc.PushState() # save current translation/scale/other state + gc.Translate(60, 75) # reposition the context origin + + gc.SetPen(wx.Pen("navy", 1)) + gc.SetBrush(wx.Brush("pink")) + gc.SetTextColour("black") + + # show the difference between stroking, filling and drawing + for label, PathFunc in [("StrokePath", gc.StrokePath), + ("FillPath", gc.FillPath), + ("DrawPath", gc.DrawPath)]: + if "wxGTK" in wx.PlatformInfo: + w, h = dc.GetTextExtent(label) # NYI in Cairo context + else: + w, h = gc.GetTextExtent(label) + + gc.DrawText(label, -w/2, -BASE2-h) + PathFunc(path) + gc.Translate(2*BASE, 0) + + + gc.PopState() # restore saved state + gc.PushState() # save it again + gc.Translate(60, 200) # offset to the lower part of the window + + gc.DrawText("Scale", 0, -BASE2) + gc.Translate(0, 20) + + gc.SetBrush(wx.Brush(wx.Colour(178, 34, 34, 128))) # 128 == half transparent + for cnt in range(8): + gc.Scale(1.08, 1.08) # increase scale by 8% + gc.Translate(5,5) + gc.DrawPath(path) + + + gc.PopState() # restore saved state + gc.PushState() # save it again + gc.Translate(400, 200) + gc.DrawText("Rotate", 0, -BASE2) + + gc.Translate(0, 75) + for angle in range(0, 360, 30): + gc.PushState() # save this new current state so we can pop back to + # it at the end of the loop + r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(float(angle)/360, 1, 1)] + gc.SetBrush(wx.Brush(wx.Colour(r, g, b, 64))) + + # use translate to artfully reposition each drawn path + gc.Translate(1.5 * BASE2 * cos(radians(angle)), + 1.5 * BASE2 * sin(radians(angle))) + + # use Rotate to rotate the path + gc.Rotate(radians(angle)) + + # now draw it + gc.DrawPath(path) + gc.PopState() + + gc.PopState() + + +#---------------------------------------------------------------------- + +def runTest(frame, nb, log): + win = TestPanel(nb, log) + return win + +#---------------------------------------------------------------------- + + + +overview = """ +

wx.GraphicsContext and wx.GraphicsPath

+ +The new advanced 2D drawing API is implemented via the +wx.GraphicsContext and wx.GraphicsPath classes. They wrap GDI+ on +Windows, Cairo on wxGTK and CoreGraphics on OS X. They allow +path-based drawing with alpha-blending and anti-aliasing, and use a +floating point cooridnate system. + +

When viewing this sample pay attention to how the rounded edges are +smoothed with anti-aliased drawing, and how the shapes on the lower +half of the window are partially transparent, allowing you to see what +was drawn before. + + +""" + + + +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) + diff --git a/wxPython/demo/Main.py b/wxPython/demo/Main.py index e25583b64d..4fe4988015 100644 --- a/wxPython/demo/Main.py +++ b/wxPython/demo/Main.py @@ -68,6 +68,7 @@ _treeList = [ 'AboutBox', 'AnimateCtrl', 'AlphaDrawing', + 'GraphicsContext', ]), # managed windows == things with a (optional) caption you can close @@ -268,6 +269,7 @@ _treeList = [ 'DrawXXXList', 'FileHistory', 'FontEnumerator', + 'GraphicsContext', 'GLCanvas', 'Joystick', 'MimeTypesManager', diff --git a/wxPython/docs/CHANGES.txt b/wxPython/docs/CHANGES.txt index c372cf4edf..ec35ae275c 100644 --- a/wxPython/docs/CHANGES.txt +++ b/wxPython/docs/CHANGES.txt @@ -23,6 +23,8 @@ of wx.ListCtrl available, which will be used for wx.LC_REPORT style list controls if you set the "mac.listctrl.always_use_generic" SystemOption to zero. In a future release this will be the default. +Added a sample to the demo that shows some of what can be done with +the new wx.GraphicsContext and wx.GraphicsPath classes.