summaryrefslogtreecommitdiff
path: root/gtk/gtkcssanimation.c
diff options
context:
space:
mode:
authorMatt Watson <mattdangerw@gmail.com>2016-03-24 23:43:15 -0700
committerMatt Watson <mattdangerw@gmail.com>2016-04-08 16:09:30 -0700
commit2800b00e1d5d46f374d50c279eb16aebd521ca4e (patch)
treec66d0d755a89a6d7b0e9ba576d3bea86be263ac6 /gtk/gtkcssanimation.c
parent50e057e02541151a9de828dc7567042c00f499d1 (diff)
downloadgtk+-2800b00e1d5d46f374d50c279eb16aebd521ca4e.tar.gz
cssanimation: port to progress tracker
Diffstat (limited to 'gtk/gtkcssanimation.c')
-rw-r--r--gtk/gtkcssanimation.c121
1 files changed, 50 insertions, 71 deletions
diff --git a/gtk/gtkcssanimation.c b/gtk/gtkcssanimation.c
index c1b10c7efd..af479345aa 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,20 @@ 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_advance_frame (&animation->tracker, for_time_us);
+ else
+ gtk_progress_tracker_skip_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 +124,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
@@ -207,17 +184,16 @@ _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;
animation->play_state = play_state;
animation->fill_mode = fill_mode;
- animation->iteration_count = iteration_count;
+
+ gtk_progress_tracker_start (&animation->tracker, duration_us, delay_us, iteration_count);
+ if (animation->play_state == GTK_CSS_PLAY_STATE_PAUSED)
+ gtk_progress_tracker_skip_frame (&animation->tracker, timestamp);
+ else
+ gtk_progress_tracker_advance_frame (&animation->tracker, timestamp);
return GTK_STYLE_ANIMATION (animation);
}
@@ -231,24 +207,27 @@ _gtk_css_animation_get_name (GtkCssAnimation *animation)
}
GtkStyleAnimation *
-_gtk_css_animation_copy (GtkCssAnimation *animation,
- gint64 at_time_us,
+_gtk_css_animation_copy (GtkCssAnimation *source,
GtkCssPlayState play_state)
{
- g_return_val_if_fail (GTK_IS_CSS_ANIMATION (animation), NULL);
+ GtkCssAnimation *animation;
+
+ g_return_val_if_fail (GTK_IS_CSS_ANIMATION (source), NULL);
- if (animation->play_state == play_state)
- return g_object_ref (animation);
-
- return _gtk_css_animation_new (animation->name,
- animation->keyframes,
- at_time_us,
- - gtk_css_animation_get_elapsed (animation, at_time_us),
- animation->duration,
- animation->ease,
- animation->direction,
- play_state,
- animation->fill_mode,
- animation->iteration_count);
+ if (source->play_state == play_state)
+ return g_object_ref (source);
+
+ animation = g_object_new (GTK_TYPE_CSS_ANIMATION, NULL);
+
+ animation->name = g_strdup (source->name);
+ animation->keyframes = _gtk_css_keyframes_ref (source->keyframes);
+ animation->ease = _gtk_css_value_ref (source->ease);
+ animation->direction = source->direction;
+ animation->play_state = play_state;
+ animation->fill_mode = source->fill_mode;
+
+ memcpy (&animation->tracker, &source->tracker, sizeof (source->tracker));
+
+ return GTK_STYLE_ANIMATION (animation);
}