[cairo-commit] [cairo-www] src/hittestpython.mdwn

Carl Worth cworth at freedesktop.org
Fri Nov 9 09:13:33 PST 2007


 src/hittestpython.mdwn |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

New commits:
commit afb20e2fccebceb4bfc8e5293017247766cc7367
Author: Donn <donn.ingle at gmail.com>
Date:   Fri Nov 9 09:13:26 2007 -0800

    fix

diff --git a/src/hittestpython.mdwn b/src/hittestpython.mdwn
index f1c197c..cf947e9 100644
--- a/src/hittestpython.mdwn
+++ b/src/hittestpython.mdwn
@@ -4,6 +4,9 @@ Once you've drawn something and **before** you cr.fill() or cr.stroke(), you can
 
 I am trying to contact the author of the algorithm to double check that it can appear here. Will update.
 
+##Newsflash
+
+Jeff Muizelaar informed me that Cairo has a built-in function to do this anyway. See code listing 2.
 
     #! /usr/bin/env python
 
@@ -157,3 +160,43 @@ I am trying to contact the author of the algorithm to double check that it can a
             
     
     run(Shapes)
+
+##Code Listing 2 : It's already done.
+
+    ...snip
+
+    class Shapes(Screen):
+        
+        #Override the press event
+        def button_press(self,widget,event):
+
+            ## Gues what? Cairo had it built-in all along :)
+            ## You just need to keep a ref to the context.
+            ## I'm not sure if re-"drawing" the entire path, just so you can
+            ## test it for a hit is faster than the other version of this
+            ## script that uses a manual python-speed algorithm.
+
+            self.cr.append_path(self.hitpath) # re-gen the path
+            result = self.cr.in_fill(event.x, event.y) # Test it. Sweet.
+            print result
+            
+        def draw(self, cr, width, height):
+            x = y = 10
+            sx = sy = 50
+            cr.move_to(x,y)
+            cr.line_to(x+sx,y)
+            cr.line_to(x+sx,y+sy)
+            cr.line_to(x+(sx/2),y+sy)
+            cr.line_to(x+(sx/2),y+(sy/2))
+            cr.line_to(x,y+(sy/2))
+            cr.line_to(x,y+sy)
+            cr.line_to(x-sx,y+sy)
+            cr.close_path()
+            cr.set_source_rgb(1,0,0)
+            
+            self.hitpath = cr.copy_path_flat() #record the path to use as a hit area.
+            
+            cr.fill() #consumes the path, so get it before the fill
+            
+    
+    run(Shapes)


More information about the cairo-commit mailing list