[gtk+/wip/watson/progress-tracker: 52/60] cssanimation: port to progress tracker



commit db745a3cd660bbe68481beb0c706d4d32e4a8500
Author: Matt Watson <mattdangerw gmail com>
Date:   Thu Mar 24 23:43:15 2016 -0700

    cssanimation: port to progress tracker

 gtk/gtkcssanimatedstyle.c    |    5 +--
 gtk/gtkcssanimation.c        |   83 +++++++++++++-----------------------------
 gtk/gtkcssanimationprivate.h |    6 +--
 3 files changed, 29 insertions(+), 65 deletions(-)
---
diff --git a/gtk/gtkcssanimatedstyle.c b/gtk/gtkcssanimatedstyle.c
index f7bcd6e..8005bcd 100644
--- a/gtk/gtkcssanimatedstyle.c
+++ b/gtk/gtkcssanimatedstyle.c
@@ -330,7 +330,6 @@ static GSList *
 gtk_css_animated_style_create_css_animations (GSList                  *animations,
                                               GtkCssStyle             *base_style,
                                               GtkCssStyle             *parent_style,
-                                              gint64                   timestamp,
                                               GtkStyleProviderPrivate *provider,
                                               GtkCssStyle             *source)
 {
@@ -367,7 +366,6 @@ gtk_css_animated_style_create_css_animations (GSList                  *animation
       if (animation)
         {
           animation = _gtk_css_animation_copy (GTK_CSS_ANIMATION (animation),
-                                               timestamp,
                                                _gtk_css_play_state_value_get (_gtk_css_array_value_get_nth 
(play_states, i)));
         }
       else
@@ -380,7 +378,6 @@ gtk_css_animated_style_create_css_animations (GSList                  *animation
 
           animation = _gtk_css_animation_new (name,
                                               keyframes,
-                                              timestamp,
                                               _gtk_css_number_value_get (_gtk_css_array_value_get_nth 
(delays, i), 100) * G_USEC_PER_SEC,
                                               _gtk_css_number_value_get (_gtk_css_array_value_get_nth 
(durations, i), 100) * G_USEC_PER_SEC,
                                               _gtk_css_array_value_get_nth (timing_functions, i),
@@ -436,7 +433,7 @@ gtk_css_animated_style_new (GtkCssStyle             *base_style,
 
   if (previous_style != NULL)
     animations = gtk_css_animated_style_create_css_transitions (animations, base_style, previous_style);
-  animations = gtk_css_animated_style_create_css_animations (animations, base_style, parent_style, 
timestamp, provider, previous_style);
+  animations = gtk_css_animated_style_create_css_animations (animations, base_style, parent_style, provider, 
previous_style);
 
   if (animations == NULL)
     return g_object_ref (base_style);
diff --git a/gtk/gtkcssanimation.c b/gtk/gtkcssanimation.c
index c1b10c7..9a8f9b5 100644
--- a/gtk/gtkcssanimation.c
+++ b/gtk/gtkcssanimation.c
@@ -22,41 +22,25 @@
 #include "gtkcssanimationprivate.h"
 
 #include "gtkcsseasevalueprivate.h"
+#include "gtkprogresstrackerprivate.h"
 
 #include <math.h>
 
 G_DEFINE_TYPE (GtkCssAnimation, _gtk_css_animation, GTK_TYPE_STYLE_ANIMATION)
 
-/* NB: Return value can be negative (if animation hasn't started yet) */
-static gint64
-gtk_css_animation_get_elapsed (GtkCssAnimation *animation,
-                               gint64           for_time_us)
-{
-  if (animation->play_state == GTK_CSS_PLAY_STATE_PAUSED)
-    return animation->timestamp;
-  else
-    return for_time_us - animation->timestamp;
-}
-/* NB: Return value can be negative and +-Inf */
-static double
-gtk_css_animation_get_iteration (GtkCssAnimation *animation,
-                                 gint64           for_time_us)
-{
-  return (double) gtk_css_animation_get_elapsed (animation, for_time_us) / animation->duration;
-}
-
 static gboolean
-gtk_css_animation_is_executing_at_iteration (GtkCssAnimation *animation,
-                                             double           iteration)
+gtk_css_animation_is_executing (GtkCssAnimation *animation)
 {
+  GtkProgressState state = gtk_progress_tracker_get_state (&animation->tracker);
+
   switch (animation->fill_mode)
     {
     case GTK_CSS_FILL_NONE:
-      return iteration >= 0 && iteration <= animation->iteration_count;
+      return state == GTK_PROGRESS_STATE_DURING;
     case GTK_CSS_FILL_FORWARDS:
-      return iteration >= 0;
+      return state != GTK_PROGRESS_STATE_BEFORE;
     case GTK_CSS_FILL_BACKWARDS:
-      return iteration <= animation->iteration_count;
+      return state != GTK_PROGRESS_STATE_AFTER;
     case GTK_CSS_FILL_BOTH:
       return TRUE;
     default:
@@ -65,38 +49,31 @@ gtk_css_animation_is_executing_at_iteration (GtkCssAnimation *animation,
 }
 
 static double
-gtk_css_animation_get_progress_from_iteration (GtkCssAnimation *animation,
-                                               double           iteration)
+gtk_css_animation_get_progress (GtkCssAnimation *animation)
 {
-  gboolean reverse;
-  double completed;
-
-  iteration = CLAMP (iteration, 0.0, animation->iteration_count);
-  completed = floor (iteration);
+  gboolean reverse, odd_iteration;
+  gint cycle = gtk_progress_tracker_get_iteration_cycle (&animation->tracker);
+  odd_iteration = cycle % 2 > 0;
 
   switch (animation->direction)
     {
     case GTK_CSS_DIRECTION_NORMAL:
-      reverse =   completed == iteration && iteration > 0;
+      reverse = FALSE;
       break;
     case GTK_CSS_DIRECTION_REVERSE:
-      reverse = !(completed == iteration && iteration > 0);
+      reverse = TRUE;
       break;
     case GTK_CSS_DIRECTION_ALTERNATE:
-      reverse =   fmod (iteration, 2) >= 1.0;
+      reverse = odd_iteration;
       break;
     case GTK_CSS_DIRECTION_ALTERNATE_REVERSE:
-      reverse = !(fmod (iteration, 2) >= 1.0);
+      reverse = !odd_iteration;
       break;
     default:
       g_return_val_if_reached (0.0);
     }
 
-  iteration -= completed;
-  if (reverse)
-    iteration = 1.0 - iteration;
-
-  return iteration;
+  return gtk_progress_tracker_get_progress (&animation->tracker, reverse);
 }
 
 static void
@@ -105,17 +82,18 @@ gtk_css_animation_set_values (GtkStyleAnimation    *style_animation,
                               GtkCssAnimatedStyle  *style)
 {
   GtkCssAnimation *animation = GTK_CSS_ANIMATION (style_animation);
-  double iteration, progress;
+  double progress;
   guint i;
 
-  iteration = gtk_css_animation_get_iteration (animation, for_time_us);
+  if (animation->play_state != GTK_CSS_PLAY_STATE_PAUSED)
+    gtk_progress_tracker_next_frame (&animation->tracker, for_time_us);
 
-  if (!gtk_css_animation_is_executing_at_iteration (animation, iteration))
+  if (!gtk_css_animation_is_executing (animation))
     return;
 
-  progress = gtk_css_animation_get_progress_from_iteration (animation, iteration);
+  progress = gtk_css_animation_get_progress (animation);
   progress = _gtk_css_ease_value_transform (animation->ease, progress);
-  
+
   for (i = 0; i < _gtk_css_keyframes_get_n_properties (animation->keyframes); i++)
     {
       GtkCssValue *value;
@@ -144,14 +122,11 @@ gtk_css_animation_is_static (GtkStyleAnimation *style_animation,
                              gint64             at_time_us)
 {
   GtkCssAnimation *animation = GTK_CSS_ANIMATION (style_animation);
-  double iteration;
 
   if (animation->play_state == GTK_CSS_PLAY_STATE_PAUSED)
     return TRUE;
 
-  iteration = gtk_css_animation_get_iteration (animation, at_time_us);
-
-  return iteration >= animation->iteration_count;
+  return gtk_progress_tracker_get_state (&animation->tracker) == GTK_PROGRESS_STATE_AFTER;
 }
 
 static void
@@ -187,7 +162,6 @@ _gtk_css_animation_init (GtkCssAnimation *animation)
 GtkStyleAnimation *
 _gtk_css_animation_new (const char      *name,
                         GtkCssKeyframes *keyframes,
-                        gint64           timestamp,
                         gint64           delay_us,
                         gint64           duration_us,
                         GtkCssValue     *ease,
@@ -207,11 +181,6 @@ _gtk_css_animation_new (const char      *name,
 
   animation->name = g_strdup (name);
   animation->keyframes = _gtk_css_keyframes_ref (keyframes);
-  if (play_state == GTK_CSS_PLAY_STATE_PAUSED)
-    animation->timestamp = - delay_us;
-  else
-    animation->timestamp = timestamp + delay_us;
-
   animation->duration = duration_us;
   animation->ease = _gtk_css_value_ref (ease);
   animation->direction = direction;
@@ -219,6 +188,8 @@ _gtk_css_animation_new (const char      *name,
   animation->fill_mode = fill_mode;
   animation->iteration_count = iteration_count;
 
+  gtk_progress_tracker_start (&animation->tracker, duration_us, delay_us, iteration_count);
+
   return GTK_STYLE_ANIMATION (animation);
 }
 
@@ -232,7 +203,6 @@ _gtk_css_animation_get_name (GtkCssAnimation *animation)
 
 GtkStyleAnimation *
 _gtk_css_animation_copy (GtkCssAnimation *animation,
-                         gint64           at_time_us,
                          GtkCssPlayState  play_state)
 {
   g_return_val_if_fail (GTK_IS_CSS_ANIMATION (animation), NULL);
@@ -242,8 +212,7 @@ _gtk_css_animation_copy (GtkCssAnimation *animation,
 
   return _gtk_css_animation_new (animation->name,
                                  animation->keyframes,
-                                 at_time_us,
-                                 - gtk_css_animation_get_elapsed (animation, at_time_us),
+                                 - gtk_progress_tracker_get_iteration (&animation->tracker) * 
animation->duration,
                                  animation->duration,
                                  animation->ease,
                                  animation->direction,
diff --git a/gtk/gtkcssanimationprivate.h b/gtk/gtkcssanimationprivate.h
index 928b614..83b0dfb 100644
--- a/gtk/gtkcssanimationprivate.h
+++ b/gtk/gtkcssanimationprivate.h
@@ -23,6 +23,7 @@
 #include "gtkstyleanimationprivate.h"
 
 #include "gtkcsskeyframesprivate.h"
+#include "gtkprogresstrackerprivate.h"
 
 G_BEGIN_DECLS
 
@@ -43,12 +44,11 @@ struct _GtkCssAnimation
   char            *name;
   GtkCssKeyframes *keyframes;
   GtkCssValue     *ease;
-  gint64           timestamp;           /* elapsed time when paused, start time when playing (can be 
negative) */
   gint64           duration;            /* duration of 1 cycle */
-  double           iteration_count;
   GtkCssDirection  direction;
   GtkCssPlayState  play_state;
   GtkCssFillMode   fill_mode;
+  GtkProgressTracker tracker;
 };
 
 struct _GtkCssAnimationClass
@@ -60,7 +60,6 @@ GType                   _gtk_css_animation_get_type        (void) G_GNUC_CONST;
 
 GtkStyleAnimation *     _gtk_css_animation_new             (const char         *name,
                                                             GtkCssKeyframes    *keyframes,
-                                                            gint64              timestamp,
                                                             gint64              delay_us,
                                                             gint64              duration_us,
                                                             GtkCssValue        *ease,
@@ -70,7 +69,6 @@ GtkStyleAnimation *     _gtk_css_animation_new             (const char         *
                                                             double              iteration_count);
 
 GtkStyleAnimation *     _gtk_css_animation_copy            (GtkCssAnimation   *animation,
-                                                            gint64             at_time_us,
                                                             GtkCssPlayState    play_state);
 
 const char *            _gtk_css_animation_get_name        (GtkCssAnimation   *animation);


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