Debian Codegraph @

gtk/gtkkineticscrolling.c

raw symbols SCIP
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2014 Lieven van der Heide
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 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
18 #include "config.h"cxx . . $ `<file>/build/config.h`/
19 #include "gtkkineticscrollingprivate.h"cxx . . $ `<file>/gtk/gtkkineticscrollingprivate.h`/
20
21 #include <math.h>
22 #include <stdio.h>
23
24 /*
25 * All our curves are second degree linear differential equations, and
26 * so they can always be written as linear combinations of 2 base
27 * solutions. c1 and c2 are the coefficients to these two base solutions,
28 * and are computed from the initial position and velocity.
29 *
30 * In the case of simple deceleration, the differential equation is
31 *
32 * y'' = -my'
33 *
34 * With m the resistance factor. For this we use the following 2
35 * base solutions:
36 *
37 * f1(x) = 1
38 * f2(x) = exp(-mx)
39 *
40 * In the case of overshoot, the differential equation is
41 *
42 * y'' = -my' - ky
43 *
44 * With m the resistance, and k the spring stiffness constant. We let
45 * k = m^2 / 4, so that the system is critically damped (ie, returns to its
46 * equilibrium position as quickly as possible, without oscillating), and offset
47 * the whole thing, such that the equilibrium position is at 0. This gives the
48 * base solutions
49 *
50 * f1(x) = exp(-mx / 2)
51 * f2(x) = t exp(-mx / 2)
52 */
53
54 typedef enumcxx . . $ $anonymous_type_246a55d9aeebf3fc_0# {
55 GTK_KINETIC_SCROLLING_PHASE_DECELERATINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_DECELERATING.,
56 GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTING.,
57 GTK_KINETIC_SCROLLING_PHASE_FINISHEDcxx . . $ GTK_KINETIC_SCROLLING_PHASE_FINISHED.,
58 } GtkKineticScrollingPhasecxx . . $ GtkKineticScrollingPhase#;
59
60 struct _GtkKineticScrollingcxx . . $ _GtkKineticScrolling#
61 {
62 GtkKineticScrollingPhasecxx . . $ GtkKineticScrollingPhase# phasecxx . . $ _GtkKineticScrolling#phase.;
63 double lowercxx . . $ _GtkKineticScrolling#lower.;
64 double uppercxx . . $ _GtkKineticScrolling#upper.;
65 double overshoot_widthcxx . . $ _GtkKineticScrolling#overshoot_width.;
66 double decel_frictioncxx . . $ _GtkKineticScrolling#decel_friction.;
67 double overshoot_frictioncxx . . $ _GtkKineticScrolling#overshoot_friction.;
68
69 double c1cxx . . $ _GtkKineticScrolling#c1.;
70 double c2cxx . . $ _GtkKineticScrolling#c2.;
71 double equilibrium_positioncxx . . $ _GtkKineticScrolling#equilibrium_position.;
72
73 gint64cxx . . $ gint64# tcxx . . $ _GtkKineticScrolling#t.;
74 double positioncxx . . $ _GtkKineticScrolling#position.;
75 double velocitycxx . . $ _GtkKineticScrolling#velocity.;
76 };
77
78 static void gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (GtkKineticScrollingcxx . . $ GtkKineticScrolling# *data,
79 gint64cxx . . $ gint64# frame_time,
80 double equilibrium_position,
81 double initial_position,
82 double initial_velocity);
83
84 GtkKineticScrollingcxx . . $ GtkKineticScrolling# *
85 gtk_kinetic_scrolling_newcxx . . $ gtk_kinetic_scrolling_new(2a610f5a42db4f05). (gint64cxx . . $ gint64# frame_time,
86 double lower,
87 double upper,
88 double overshoot_width,
89 double decel_friction,
90 double overshoot_friction,
91 double initial_position,
92 double initial_velocity)
93 {
94 GtkKineticScrollingcxx . . $ GtkKineticScrolling# *data;
95
96 data = g_new0cxx . . $ `<external>/1123956534522462559/gmem.h:335:9`! (GtkKineticScrolling, 1);
97 data->lowercxx . . $ _GtkKineticScrolling#lower. = lower;
98 data->uppercxx . . $ _GtkKineticScrolling#upper. = upper;
99 data->decel_frictioncxx . . $ _GtkKineticScrolling#decel_friction. = decel_friction;
100 data->overshoot_frictioncxx . . $ _GtkKineticScrolling#overshoot_friction. = overshoot_friction;
101 data->overshoot_widthcxx . . $ _GtkKineticScrolling#overshoot_width. = overshoot_width;
102 if (initial_position < lower)
103 {
104 gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (data,
105 frame_time,
106 lower,
107 initial_position,
108 initial_velocity);
109 }
110 else if (initial_position > upper)
111 {
112 gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (data,
113 frame_time,
114 upper,
115 initial_position,
116 initial_velocity);
117 }
118 else
119 {
120 data->phasecxx . . $ _GtkKineticScrolling#phase. = GTK_KINETIC_SCROLLING_PHASE_DECELERATINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_DECELERATING.;
121 data->c1cxx . . $ _GtkKineticScrolling#c1. = initial_velocity / decel_friction + initial_position;
122 data->c2cxx . . $ _GtkKineticScrolling#c2. = -initial_velocity / decel_friction;
123 data->positioncxx . . $ _GtkKineticScrolling#position. = initial_position;
124 data->velocitycxx . . $ _GtkKineticScrolling#velocity. = initial_velocity;
125 data->tcxx . . $ _GtkKineticScrolling#t. = frame_time;
126 }
127
128 return data;
129 }
130
131 GtkKineticScrollingChangecxx . . $ GtkKineticScrollingChange#
132 gtk_kinetic_scrolling_update_sizecxx . . $ gtk_kinetic_scrolling_update_size(4ec1fa978c1e83c). (GtkKineticScrollingcxx . . $ GtkKineticScrolling# *data,
133 double lower,
134 double upper)
135 {
136 GtkKineticScrollingChangecxx . . $ GtkKineticScrollingChange# change = GTK_KINETIC_SCROLLING_CHANGE_NONEcxx . . $ GTK_KINETIC_SCROLLING_CHANGE_NONE.;
137
138 if (lower != data->lowercxx . . $ _GtkKineticScrolling#lower.)
139 {
140 if (data->positioncxx . . $ _GtkKineticScrolling#position. <= lower)
141 change |= GTK_KINETIC_SCROLLING_CHANGE_LOWERcxx . . $ GTK_KINETIC_SCROLLING_CHANGE_LOWER.;
142
143 data->lowercxx . . $ _GtkKineticScrolling#lower. = lower;
144 }
145
146 if (upper != data->uppercxx . . $ _GtkKineticScrolling#upper.)
147 {
148 if (data->positioncxx . . $ _GtkKineticScrolling#position. >= data->uppercxx . . $ _GtkKineticScrolling#upper.)
149 change |= GTK_KINETIC_SCROLLING_CHANGE_UPPERcxx . . $ GTK_KINETIC_SCROLLING_CHANGE_UPPER.;
150
151 data->uppercxx . . $ _GtkKineticScrolling#upper. = upper;
152 }
153
154 if (data->phasecxx . . $ _GtkKineticScrolling#phase. == GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTING.)
155 change |= GTK_KINETIC_SCROLLING_CHANGE_IN_OVERSHOOTcxx . . $ GTK_KINETIC_SCROLLING_CHANGE_IN_OVERSHOOT.;
156
157 return change;
158 }
159
160 void
161 gtk_kinetic_scrolling_freecxx . . $ gtk_kinetic_scrolling_free(c0a091e175acfe88). (GtkKineticScrollingcxx . . $ GtkKineticScrolling# *kinetic)
162 {
163 g_freecxx . . $ g_free(5d22bdacc48458e8). (kinetic);
164 }
165
166 static void
167 gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (GtkKineticScrollingcxx . . $ GtkKineticScrolling# *data,
168 gint64cxx . . $ gint64# frame_time,
169 double equilibrium_position,
170 double initial_position,
171 double initial_velocity)
172 {
173 data->phasecxx . . $ _GtkKineticScrolling#phase. = GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTING.;
174 data->equilibrium_positioncxx . . $ _GtkKineticScrolling#equilibrium_position. = equilibrium_position;
175 data->c1cxx . . $ _GtkKineticScrolling#c1. = initial_position - equilibrium_position;
176 data->c2cxx . . $ _GtkKineticScrolling#c2. = initial_velocity + data->overshoot_frictioncxx . . $ _GtkKineticScrolling#overshoot_friction. / 2 * data->c1cxx . . $ _GtkKineticScrolling#c1.;
177 data->tcxx . . $ _GtkKineticScrolling#t. = frame_time;
178 }
179
180 gbooleancxx . . $ gboolean#
181 gtk_kinetic_scrolling_tickcxx . . $ gtk_kinetic_scrolling_tick(f2badcd499cef3c6). (GtkKineticScrollingcxx . . $ GtkKineticScrolling# *data,
182 gint64cxx . . $ gint64# frame_time,
183 double *position,
184 double *velocity)
185 {
186 double t = (frame_time - data->tcxx . . $ _GtkKineticScrolling#t.) / (double)G_USEC_PER_SECcxx . . $ `<external>/15991247088948160287/gtimer.h:44:9`!;
187
188 switch (data->phasecxx . . $ _GtkKineticScrolling#phase.)
189 {
190 case GTK_KINETIC_SCROLLING_PHASE_DECELERATINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_DECELERATING.:
191 {
192 double exp_part;
193
194 exp_part = expcxx . . $ exp(447c67c6676f75c3). (-data->decel_frictioncxx . . $ _GtkKineticScrolling#decel_friction. * t);
195 data->positioncxx . . $ _GtkKineticScrolling#position. = data->c1cxx . . $ _GtkKineticScrolling#c1. + data->c2cxx . . $ _GtkKineticScrolling#c2. * exp_part;
196 data->velocitycxx . . $ _GtkKineticScrolling#velocity. = -data->decel_frictioncxx . . $ _GtkKineticScrolling#decel_friction. * data->c2cxx . . $ _GtkKineticScrolling#c2. * exp_part;
197
198 if (data->positioncxx . . $ _GtkKineticScrolling#position. < data->lowercxx . . $ _GtkKineticScrolling#lower.)
199 {
200 gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (data, frame_time, data->lowercxx . . $ _GtkKineticScrolling#lower., data->positioncxx . . $ _GtkKineticScrolling#position., data->velocitycxx . . $ _GtkKineticScrolling#velocity.);
201 }
202 else if (data->positioncxx . . $ _GtkKineticScrolling#position. > data->uppercxx . . $ _GtkKineticScrolling#upper.)
203 {
204 gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (data, frame_time, data->uppercxx . . $ _GtkKineticScrolling#upper., data->positioncxx . . $ _GtkKineticScrolling#position., data->velocitycxx . . $ _GtkKineticScrolling#velocity.);
205 }
206 else if (fabscxx . . $ fabs(447c67c6676f75c3). (data->velocitycxx . . $ _GtkKineticScrolling#velocity.) < 0.1)
207 {
208 gtk_kinetic_scrolling_stopcxx . . $ gtk_kinetic_scrolling_stop(c0a091e175acfe88). (data);
209 }
210 break;
211 }
212
213 case GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_OVERSHOOTING.:
214 {
215 double half_overshoot_width = data->overshoot_widthcxx . . $ _GtkKineticScrolling#overshoot_width. / 2.;
216 double exp_part, pos;
217
218 exp_part = expcxx . . $ exp(447c67c6676f75c3). (-data->overshoot_frictioncxx . . $ _GtkKineticScrolling#overshoot_friction. / 2 * t);
219 pos = exp_part * (data->c1cxx . . $ _GtkKineticScrolling#c1. + data->c2cxx . . $ _GtkKineticScrolling#c2. * t);
220
221 if (pos < data->lowercxx . . $ _GtkKineticScrolling#lower. - half_overshoot_width || pos > data->uppercxx . . $ _GtkKineticScrolling#upper. + half_overshoot_width)
222 {
223 pos = CLAMPcxx . . $ `<external>/12741050725406041920/gmacros.h:984:9`! (pos, data->lower - half_overshoot_width, data->upper + half_overshoot_width);
224 gtk_kinetic_scrolling_init_overshootcxx . . $ gtk_kinetic_scrolling_init_overshoot(5ead80f32c27aecc). (data, frame_time, data->equilibrium_positioncxx . . $ _GtkKineticScrolling#equilibrium_position., pos, 0);
225 }
226 else
227 data->velocitycxx . . $ _GtkKineticScrolling#velocity. = data->c2cxx . . $ _GtkKineticScrolling#c2. * exp_part - data->overshoot_frictioncxx . . $ _GtkKineticScrolling#overshoot_friction. / 2 * pos;
228
229 data->positioncxx . . $ _GtkKineticScrolling#position. = pos + data->equilibrium_positioncxx . . $ _GtkKineticScrolling#equilibrium_position.;
230
231 if (fabscxx . . $ fabs(447c67c6676f75c3). (pos) < 0.1)
232 {
233 data->phasecxx . . $ _GtkKineticScrolling#phase. = GTK_KINETIC_SCROLLING_PHASE_FINISHEDcxx . . $ GTK_KINETIC_SCROLLING_PHASE_FINISHED.;
234 data->positioncxx . . $ _GtkKineticScrolling#position. = data->equilibrium_positioncxx . . $ _GtkKineticScrolling#equilibrium_position.;
235 data->velocitycxx . . $ _GtkKineticScrolling#velocity. = 0;
236 }
237 break;
238 }
239
240 case GTK_KINETIC_SCROLLING_PHASE_FINISHEDcxx . . $ GTK_KINETIC_SCROLLING_PHASE_FINISHED.:
241 default:
242 break;
243 }
244
245 if (position)
246 *position = data->positioncxx . . $ _GtkKineticScrolling#position.;
247 if (velocity)
248 *velocity = data->velocitycxx . . $ _GtkKineticScrolling#velocity.;
249
250 return data->phasecxx . . $ _GtkKineticScrolling#phase. != GTK_KINETIC_SCROLLING_PHASE_FINISHEDcxx . . $ GTK_KINETIC_SCROLLING_PHASE_FINISHED.;
251 }
252
253 void
254 gtk_kinetic_scrolling_stopcxx . . $ gtk_kinetic_scrolling_stop(c0a091e175acfe88). (GtkKineticScrollingcxx . . $ GtkKineticScrolling# *data)
255 {
256 if (data->phasecxx . . $ _GtkKineticScrolling#phase. == GTK_KINETIC_SCROLLING_PHASE_DECELERATINGcxx . . $ GTK_KINETIC_SCROLLING_PHASE_DECELERATING.)
257 {
258 data->phasecxx . . $ _GtkKineticScrolling#phase. = GTK_KINETIC_SCROLLING_PHASE_FINISHEDcxx . . $ GTK_KINETIC_SCROLLING_PHASE_FINISHED.;
259 data->positioncxx . . $ _GtkKineticScrolling#position. = roundcxx . . $ round(447c67c6676f75c3). (data->positioncxx . . $ _GtkKineticScrolling#position.);
260 }
261 }