]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsstransition.c
csscomputedvalues: Store animated values here
[~andy/gtk] / gtk / gtkcsstransition.c
1 /*
2  * Copyright © 2012 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcsstransitionprivate.h"
23
24 #include "gtkcsseasevalueprivate.h"
25
26 G_DEFINE_TYPE (GtkCssTransition, _gtk_css_transition, GTK_TYPE_STYLE_ANIMATION)
27
28 static GtkBitmask *
29 gtk_css_transition_set_values (GtkStyleAnimation    *animation,
30                                GtkBitmask           *changed,
31                                gint64                for_time_us,
32                                GtkCssComputedValues *values)
33 {
34   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
35   GtkCssValue *value;
36   double progress;
37
38   if (transition->start_time >= for_time_us)
39     value = _gtk_css_value_ref (transition->start);
40   else if (transition->end_time <= for_time_us)
41     value = _gtk_css_value_ref (transition->end);
42   else
43     {
44       progress = (double) (for_time_us - transition->start_time) / (transition->end_time - transition->start_time);
45       progress = _gtk_css_ease_value_transform (transition->ease, progress);
46
47       value = _gtk_css_value_transition (transition->start,
48                                          transition->end,
49                                          transition->property,
50                                          progress);
51       if (value == NULL)
52         value = _gtk_css_value_ref (transition->end);
53     }
54
55   _gtk_css_computed_values_set_animated_value (values, transition->property, value);
56   _gtk_css_value_unref (value);
57
58   return _gtk_bitmask_set (changed, transition->property, TRUE);
59 }
60
61 static gboolean
62 gtk_css_transition_is_finished (GtkStyleAnimation *animation,
63                                 gint64             at_time_us)
64 {
65   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
66
67   return at_time_us >= transition->end_time;
68 }
69
70 static gboolean
71 gtk_css_transition_is_static (GtkStyleAnimation *animation,
72                               gint64             at_time_us)
73 {
74   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
75
76   return at_time_us >= transition->end_time;
77 }
78
79 static void
80 gtk_css_transition_finalize (GObject *object)
81 {
82   GtkCssTransition *transition = GTK_CSS_TRANSITION (object);
83
84   _gtk_css_value_unref (transition->start);
85   _gtk_css_value_unref (transition->end);
86   _gtk_css_value_unref (transition->ease);
87
88   G_OBJECT_CLASS (_gtk_css_transition_parent_class)->finalize (object);
89 }
90
91 static void
92 _gtk_css_transition_class_init (GtkCssTransitionClass *klass)
93 {
94   GObjectClass *object_class = G_OBJECT_CLASS (klass);
95   GtkStyleAnimationClass *animation_class = GTK_STYLE_ANIMATION_CLASS (klass);
96
97   object_class->finalize = gtk_css_transition_finalize;
98
99   animation_class->set_values = gtk_css_transition_set_values;
100   animation_class->is_finished = gtk_css_transition_is_finished;
101   animation_class->is_static = gtk_css_transition_is_static;
102 }
103
104 static void
105 _gtk_css_transition_init (GtkCssTransition *transition)
106 {
107 }
108
109 GtkStyleAnimation *
110 _gtk_css_transition_new (guint        property,
111                          GtkCssValue *start,
112                          GtkCssValue *end,
113                          GtkCssValue *ease,
114                          gint64       start_time_us,
115                          gint64       end_time_us)
116 {
117   GtkCssTransition *transition;
118
119   g_return_val_if_fail (start != NULL, NULL);
120   g_return_val_if_fail (end != NULL, NULL);
121   g_return_val_if_fail (ease != NULL, NULL);
122   g_return_val_if_fail (start_time_us <= end_time_us, NULL);
123
124   transition = g_object_new (GTK_TYPE_CSS_TRANSITION, NULL);
125
126   transition->property = property;
127   transition->start = _gtk_css_value_ref (start);
128   transition->end = _gtk_css_value_ref (end);
129   transition->ease = _gtk_css_value_ref (ease);
130   transition->start_time = start_time_us;
131   transition->end_time = end_time_us;
132
133   return GTK_STYLE_ANIMATION (transition);
134 }
135
136 guint
137 _gtk_css_transition_get_property (GtkCssTransition *transition)
138 {
139   g_return_val_if_fail (GTK_IS_CSS_TRANSITION (transition), 0);
140
141   return transition->property;
142 }