Added animation for icon rotation.
[drlaunch] / src / icon.py
index aafe320..df559cc 100755 (executable)
@@ -82,6 +82,8 @@ class Icon(gobject.GObject):
 
        self.clickcount=0
 
+       self.angle=0
+
     def timePressed(self):
        """ return how much time a button is pressed """
        dt=time.time() - self.lastpress
@@ -100,7 +102,20 @@ class Icon(gobject.GObject):
     def getSize(self):
        return(self.config.iconsize+self.config.iconspace)
 
-    def draw(self, cr, x, y, mode):
+    def setAngle(self, angle):
+       if self.angle==angle:
+           print "Same angle"
+           return
+
+       self.angle=angle
+
+       # The caller should be responsible for redrawing.
+       # If we call invalidate() here there is the risk of having
+       # icons rotate individually using different angles
+#      self.invalidate()
+
+    def draw(self, cr, x, y):
+       self.draw_queued=False
        self.x=x
        self.y=y
 
@@ -108,6 +123,7 @@ class Icon(gobject.GObject):
            return
 
        cr.save()
+
        cr.set_source_rgba(0.1, 0.1, 0.1, 1)
        cr.set_line_width(5)
 
@@ -143,15 +159,54 @@ class Icon(gobject.GObject):
 
        icon=self.icon
 
-       if mode=='l':
-           icon2=icon
-       else:
-           icon2=icon.rotate_simple(gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE)
+       icon2=icon
+
+# Old method. Faster rotation but without support for rotation
+# animation
+
+#      if mode=='l':
+#          icon2=icon
+#      else:
+#          icon2=icon.rotate_simple(gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE)
+
+#      cr.save()
+#      x3=x + (self.config.iconspace/2)
+#      y3=y + (self.config.iconspace/2)
+#      cr.set_source_pixbuf(icon2, x3, y3)
+#
+#      cr.paint()
+#      cr.restore()
 
+
+       # Width is the iconsize plus the empty border around the icon
+       w=self.config.iconsize + self.config.iconspace
+
+       # This is used to locate the center of the surface
+       dx=int(w/2)
+
+       # This is the delta from the center where icons are drawn
+       dx2=int(self.config.iconsize/2)
+
+       # A surface to draw on
+       t_s=cairo.ImageSurface(cairo.FORMAT_ARGB32, w, w)
+
+       # And a context to draw
+       t_cr0=cairo.Context(t_s)
+       t_cr=gtk.gdk.CairoContext(t_cr0)
+
+       # A transformation matrix with dx/dy set to point to the center
+       m=cairo.Matrix(1, 0, 0, 1, dx, dx)
+       t_cr.set_matrix(m)
+       # Transform degrees to rads
+       rot=-1 * pi * 2 * self.angle / 360
+       t_cr.rotate(rot)
+       # Draw the icon
+       t_cr.set_source_pixbuf(icon2, -dx2, -dx2)
+       t_cr.paint()
+
+       # Draw the rotated icon on the main cairo context
        cr.save()
-       x3=x + (self.config.iconspace/2)
-       y3=y + (self.config.iconspace/2)
-       cr.set_source_pixbuf(icon2, x3, y3)
+       cr.set_source_surface(t_s, x, y)
        cr.paint()
        cr.restore()
 
@@ -198,6 +253,9 @@ class Icon(gobject.GObject):
     def doCancel(self):
        self.ispressed=False
 
+    def setWindow(self, window):
+       self.window=window
+
     def invalidate(self, window=None):
        if window==None:
            window=self.window
@@ -207,6 +265,10 @@ class Icon(gobject.GObject):
        if window==None:
            return
 
+       if self.draw_queued:
+           print "queued"
+           return
+       self.draw_queued=True
        w=self.config.iconsize + self.config.iconspace
        rect=gdk.Rectangle(self.x, self.y, w, w)
        gdk.Window.invalidate_rect(window, rect, True)