[hamster-applet] moved animation bits into graphics module to simplify animation!



commit 1ca5fb56e5645338bd0d9f8d014f1896ec178db6
Author: Toms Bauģis <toms baugis gmail com>
Date:   Fri Nov 27 11:53:14 2009 +0000

    moved animation bits into graphics module to simplify animation!

 hamster/charting.py |   53 +++++++++++-------------------------
 hamster/graphics.py |   75 ++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 66 insertions(+), 62 deletions(-)
---
diff --git a/hamster/charting.py b/hamster/charting.py
index 8b95636..4385600 100644
--- a/hamster/charting.py
+++ b/hamster/charting.py
@@ -34,7 +34,6 @@ http://projecthamster.wordpress.com/
 """
 
 import gtk
-import gobject
 import cairo, pango
 import copy
 import math
@@ -44,7 +43,7 @@ import time
 import colorsys
 import logging
 
-import graphics, pytweener
+import graphics
 
 
 def size_list(set, target_set):
@@ -122,7 +121,7 @@ class Chart(graphics.Area):
         # options
         self.max_bar_width     = args.get("max_bar_width", 500)
         self.legend_width      = args.get("legend_width", 0)
-        self.animate           = args.get("animate", True)
+        self.animation           = args.get("animate", True)
 
         self.background        = args.get("background", None)
         self.chart_background  = args.get("chart_background", None)
@@ -138,10 +137,6 @@ class Chart(graphics.Area):
         self.framerate         = args.get("framerate", 60)
 
         # other stuff
-        self.tweener = pytweener.Tweener(0.4, pytweener.Easing.Cubic.easeInOut)
-        self.last_frame_time = None
-        self.moving = False
-        
         self.bars = []
         self.keys = []
         self.stack_keys = []
@@ -149,6 +144,11 @@ class Chart(graphics.Area):
         self.key_colors = {} # key:color dictionary. if key's missing will grab basecolor
         self.stack_key_colors = {} # key:color dictionary. if key's missing will grab basecolor
         
+
+        # use these to mark area where the "real" drawing is going on
+        self.graph_x, self.graph_y = 0, 0
+        self.graph_width, self.graph_height = None, None
+        
         
     def get_bar_color(self, index):
         # returns color darkened by it's index
@@ -185,34 +185,13 @@ class Chart(graphics.Area):
 
         self._update_targets()
 
-        if self.animate:
-            self.last_frame_time = dt.datetime.now()
-            if not self.moving: #if we are moving, then there is a timeout somewhere already
-                gobject.timeout_add(1000 / self.framerate, self._interpolate)
-        else:
-            self.tweener.update(self.tweener.defaultDuration) # set to end frame
-
-            self.redraw_canvas()
-
-
-    def _interpolate(self):
-        """Internal function to do the math, going from previous set to the
-           new one, and redraw graph"""
-        #this can get called before expose    
-        self.moving = self.tweener.hasTweens()
-
-        if not self.window:
-            self.redraw_canvas()
-            return False
+        if not self.animation:
+            self.tweener.finish()
 
-        time_since_start = (dt.datetime.now() - self.last_frame_time).microseconds / 1000000.0
-        self.tweener.update(time_since_start)
         self.redraw_canvas()
-        self.last_frame_time = dt.datetime.now()
 
-        return self.moving
 
-    def _render(self):
+    def on_expose(self):
         # fill whole area 
         if self.background:
             self.fill_area(0, 0, self.width, self.height, self.background)
@@ -258,9 +237,9 @@ class Chart(graphics.Area):
 
 
 class BarChart(Chart):
-    def _render(self):
+    def on_expose(self):
         context = self.context
-        Chart._render(self)
+        Chart.on_expose(self)
         
         # determine graph dimensions
         if self.show_stack_labels:
@@ -484,9 +463,9 @@ class BarChart(Chart):
 
 
 class HorizontalBarChart(Chart):
-    def _render(self):
+    def on_expose(self):
         context = self.context
-        Chart._render(self)
+        Chart.on_expose(self)
         rowcount, keys = len(self.keys), self.keys
         
         # push graph to the right, so it doesn't overlap
@@ -612,9 +591,9 @@ class HorizontalDayChart(Chart):
         self.show()
         self.redraw_canvas()
     
-    def _render(self):
+    def on_expose(self):
         context = self.context
-        Chart._render(self)
+        Chart.on_expose(self)
         rowcount, keys = len(self.keys), self.keys
         
         start_hour = 0
diff --git a/hamster/graphics.py b/hamster/graphics.py
index 15107ce..b5dff88 100644
--- a/hamster/graphics.py
+++ b/hamster/graphics.py
@@ -3,6 +3,9 @@ import gtk, gobject
 
 import pango, cairo
 
+import pytweener
+from pytweener import Easing
+
 class Colors(object):
     aluminium = [(238, 238, 236), (211, 215, 207), (186, 189, 182),
                  (136, 138, 133), (85, 87, 83), (46, 52, 54)]
@@ -43,25 +46,58 @@ class Area(gtk.DrawingArea):
         self.connect("motion_notify_event", self.__on_mouse_move)
         self.connect("leave_notify_event", self.__on_mouse_out)
 
+        self.font_size = 8
+        self.mouse_regions = [] #regions of drawing that respond to hovering/clicking
 
-        self.context = None
-        self.layout = None
-        self.width = None
-        self.height = None
-        self.value_boundaries = None #x_min, x_max, y_min, y_max
-        
-        self.x_factor, self.y_factor = None, None
+        self.context, self.layout = None, None
+        self.width, self.height = None, None
+        self.__prev_mouse_regions = None
         
-        self.font_size = 8
+        self.tweener = pytweener.Tweener(0.4, pytweener.Easing.Cubic.easeInOut)
+        self.framerate = 60
+        self.last_frame_time = None
+        self.__animating = False
+
+    def on_expose(self):
+        """ on_expose event is where you hook in all your drawing
+            canvas has been initialized for you """
+        raise NotImplementedError
 
-        # use these to mark area where the "real" drawing is going on
-        self.graph_x, self.graph_y = 0, 0
-        self.graph_width, self.graph_height = None, None
+    def redraw_canvas(self):
+        """Redraw canvas. Triggers also to do all animations"""
+        if not self.__animating: #if we are moving, then there is a timeout somewhere already
+            self.__animating = True
+            self.last_frame_time = dt.datetime.now()
+            gobject.timeout_add(1000 / self.framerate, self.__interpolate)
+            
+    """ animation bits """
+    def __interpolate(self):
+        self.__animating = self.tweener.hasTweens()
+
+        if not self.window: #will wait until window comes
+            return self.__animating
         
-        self.mouse_regions = [] #regions of drawing that respond to hovering/clicking
-        self.__prev_mouse_regions = None
+        
+        time_since_start = (dt.datetime.now() - self.last_frame_time).microseconds / 1000000.0
+        self.tweener.update(time_since_start)
 
+        self.queue_draw()
+        self.window.process_updates(True)
+
+        self.last_frame_time = dt.datetime.now()
+
+        return self.__animating
+
+
+    def animate(self, object, params = {}, duration = None, easing = None, callback = None):
+        if duration: params["tweenTime"] = duration  # if none will fallback to tweener's default
+        if easing: params["tweenType"] = easing    # if none will fallback to tweener's default
+        if callback: params["onCompleteFunction"] = callback
+        self.tweener.addTween(object, **params)
+        self.redraw_canvas()
+    
 
+    """ drawing on canvas bits """
     def __rectangle(self, x, y, w, h, color, opacity = 0):
         if color[0] > 1: color = [c / 256.0 for c in color]
 
@@ -107,17 +143,6 @@ class Area(gtk.DrawingArea):
     def register_mouse_region(self, x1, y1, x2, y2, region_name):
         self.mouse_regions.append((x1, y1, x2, y2, region_name))
 
-    def redraw_canvas(self):
-        """Force graph redraw"""
-        if self.window:    #this can get called before expose
-            self.queue_draw()
-            self.window.process_updates(True)
-
-
-    def _render(self):
-        raise NotImplementedError
-
-
     """ exposure events """
     def do_configure_event(self, event):
         (self.__width, self.__height) = self.window.get_size()
@@ -141,7 +166,7 @@ class Area(gtk.DrawingArea):
         self.width, self.height = alloc.width, alloc.height
         
         self.mouse_regions = [] #reset since these can move in each redraw
-        self._render()
+        self.on_expose()
 
 
     """ mouse events """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]