blob: 30b3e5de832237c6c092af5ed863b7177761f63a [file] [log] [blame]
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020028#include <stdlib.h>
29#include <string.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020031#include <stdio.h>
32#include <math.h>
33
34#include <unistd.h>
35#include <fcntl.h>
36
37#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070038#include "shared/helpers.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020039
40WL_EXPORT void
41weston_spring_init(struct weston_spring *spring,
Jason Ekstranda7af7042013-10-12 22:38:11 -050042 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020043{
44 spring->k = k;
45 spring->friction = 400.0;
46 spring->current = current;
47 spring->previous = current;
48 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040049 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040050 spring->min = 0.0;
51 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020052}
53
54WL_EXPORT void
55weston_spring_update(struct weston_spring *spring, uint32_t msec)
56{
57 double force, v, current, step;
58
59 /* Limit the number of executions of the loop below by ensuring that
60 * the timestamp for last update of the spring is no more than 1s ago.
61 * This handles the case where time moves backwards or forwards in
62 * large jumps.
63 */
64 if (msec - spring->timestamp > 1000) {
65 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
66 spring->timestamp, msec);
67 spring->timestamp = msec - 1000;
68 }
69
70 step = 0.01;
71 while (4 < msec - spring->timestamp) {
72 current = spring->current;
73 v = current - spring->previous;
74 force = spring->k * (spring->target - current) / 10.0 +
75 (spring->previous - current) - v * spring->friction;
76
77 spring->current =
78 current + (current - spring->previous) +
79 force * step * step;
80 spring->previous = current;
81
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040082 switch (spring->clip) {
83 case WESTON_SPRING_OVERSHOOT:
84 break;
85
86 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040087 if (spring->current > spring->max) {
88 spring->current = spring->max;
89 spring->previous = spring->max;
90 } else if (spring->current < 0.0) {
91 spring->current = spring->min;
92 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040093 }
94 break;
95
96 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040097 if (spring->current > spring->max) {
98 spring->current =
99 2 * spring->max - spring->current;
100 spring->previous =
101 2 * spring->max - spring->previous;
102 } else if (spring->current < spring->min) {
103 spring->current =
104 2 * spring->min - spring->current;
105 spring->previous =
106 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400107 }
108 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200109 }
110
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200111 spring->timestamp += 4;
112 }
113}
114
115WL_EXPORT int
116weston_spring_done(struct weston_spring *spring)
117{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400118 return fabs(spring->previous - spring->target) < 0.002 &&
119 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200120}
121
Jason Ekstranda7af7042013-10-12 22:38:11 -0500122typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200123
Jason Ekstranda7af7042013-10-12 22:38:11 -0500124struct weston_view_animation {
125 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200126 struct weston_animation animation;
127 struct weston_spring spring;
128 struct weston_transform transform;
129 struct wl_listener listener;
130 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500131 weston_view_animation_frame_func_t frame;
132 weston_view_animation_frame_func_t reset;
133 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200134 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100135 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200136};
137
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100138WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500139weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200140{
141 wl_list_remove(&animation->animation.link);
142 wl_list_remove(&animation->listener.link);
143 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200144 if (animation->reset)
145 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500146 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200147 if (animation->done)
148 animation->done(animation, animation->data);
149 free(animation);
150}
151
152static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200154{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200156 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500157 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200158
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200160}
161
162static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500163weston_view_animation_frame(struct weston_animation *base,
164 struct weston_output *output, uint32_t msecs)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200165{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500166 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200167 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500168 struct weston_view_animation, animation);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200169 struct weston_compositor *compositor =
170 animation->view->surface->compositor;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200171
172 if (base->frame_counter <= 1)
173 animation->spring.timestamp = msecs;
174
175 weston_spring_update(&animation->spring, msecs);
176
177 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700178 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500179 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200180 return;
181 }
182
183 if (animation->frame)
184 animation->frame(animation);
185
Jason Ekstranda7af7042013-10-12 22:38:11 -0500186 weston_view_geometry_dirty(animation->view);
187 weston_view_schedule_repaint(animation->view);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200188
189 /* The view's output_mask will be zero if its position is
190 * offscreen. Animations should always run but as they are also
191 * run off the repaint cycle, if there's nothing to repaint
192 * the animation stops running. Therefore if we catch this situation
193 * and schedule a repaint on all outputs it will be avoided.
194 */
195 if (animation->view->output_mask == 0)
196 weston_compositor_schedule_repaint(compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200197}
198
Jason Ekstranda7af7042013-10-12 22:38:11 -0500199static struct weston_view_animation *
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300200weston_view_animation_create(struct weston_view *view,
201 float start, float stop,
202 weston_view_animation_frame_func_t frame,
203 weston_view_animation_frame_func_t reset,
204 weston_view_animation_done_func_t done,
205 void *data,
206 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200207{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500208 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200209
210 animation = malloc(sizeof *animation);
211 if (!animation)
212 return NULL;
213
Jason Ekstranda7af7042013-10-12 22:38:11 -0500214 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200215 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200216 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200217 animation->done = done;
218 animation->data = data;
219 animation->start = start;
220 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100221 animation->private = private;
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300222
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200223 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500224 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200225 &animation->transform.link);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300226
Jason Ekstranda7af7042013-10-12 22:38:11 -0500227 animation->animation.frame = weston_view_animation_frame;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200228
Jason Ekstranda7af7042013-10-12 22:38:11 -0500229 animation->listener.notify = handle_animation_view_destroy;
230 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200231
Jason Ekstranda7af7042013-10-12 22:38:11 -0500232 wl_list_insert(&view->output->animation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200233 &animation->animation.link);
234
235 return animation;
236}
237
238static void
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300239weston_view_animation_run(struct weston_view_animation *animation)
240{
241 animation->animation.frame_counter = 0;
242 weston_view_animation_frame(&animation->animation, NULL, 0);
243}
244
245static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500246reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200247{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200249
Jason Ekstranda7af7042013-10-12 22:38:11 -0500250 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200251}
252
253static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500254zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200255{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500256 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200257 float scale;
258
259 scale = animation->start +
260 (animation->stop - animation->start) *
261 animation->spring.current;
262 weston_matrix_init(&animation->transform.matrix);
263 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600264 -0.5f * es->surface->width,
265 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200266 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
267 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600268 0.5f * es->surface->width,
269 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200270
271 es->alpha = animation->spring.current;
272 if (es->alpha > 1.0)
273 es->alpha = 1.0;
274}
275
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276WL_EXPORT struct weston_view_animation *
277weston_zoom_run(struct weston_view *view, float start, float stop,
278 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200279{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400281
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300282 zoom = weston_view_animation_create(view, start, stop,
283 zoom_frame, reset_alpha,
284 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400285
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800286 if (zoom == NULL)
287 return NULL;
288
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400289 weston_spring_init(&zoom->spring, 300.0, start, stop);
290 zoom->spring.friction = 1400;
291 zoom->spring.previous = start - (stop - start) * 0.03;
292
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300293 weston_view_animation_run(zoom);
294
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400295 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200296}
297
298static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200300{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200301 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500302 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200303 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200305 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500306 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200307}
308
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309WL_EXPORT struct weston_view_animation *
310weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200311 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200313{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200315
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300316 fade = weston_view_animation_create(view, start, end,
317 fade_frame, reset_alpha,
318 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200319
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800320 if (fade == NULL)
321 return NULL;
322
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300323 weston_spring_init(&fade->spring, 1000.0, start, end);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700324 fade->spring.friction = 4000;
325 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400326
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200328
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300329 weston_view_animation_run(fade);
330
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200331 return fade;
332}
333
334WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500335weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200336{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400337 fade->spring.target = target;
Jonny Lamb70ee3ad2014-07-30 00:56:18 +0100338 fade->stop = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200339}
340
341static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100342stable_fade_frame(struct weston_view_animation *animation)
343{
344 struct weston_view *back_view;
345
346 if (animation->spring.current > 0.999)
347 animation->view->alpha = 1;
348 else if (animation->spring.current < 0.001 )
349 animation->view->alpha = 0;
350 else
351 animation->view->alpha = animation->spring.current;
352
353 back_view = (struct weston_view *) animation->private;
354 back_view->alpha =
355 (animation->spring.target - animation->view->alpha) /
356 (1.0 - animation->view->alpha);
357 weston_view_geometry_dirty(back_view);
358}
359
360WL_EXPORT struct weston_view_animation *
361weston_stable_fade_run(struct weston_view *front_view, float start,
362 struct weston_view *back_view, float end,
363 weston_view_animation_done_func_t done, void *data)
364{
365 struct weston_view_animation *fade;
366
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300367 fade = weston_view_animation_create(front_view, 0, 0,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100368 stable_fade_frame, NULL,
369 done, data, back_view);
370
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800371 if (fade == NULL)
372 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100373
374 weston_spring_init(&fade->spring, 400, start, end);
375 fade->spring.friction = 1150;
376
377 front_view->alpha = start;
378 back_view->alpha = end;
379
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300380 weston_view_animation_run(fade);
381
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100382 return fade;
383}
384
385static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500386slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200387{
388 float scale;
389
390 scale = animation->start +
391 (animation->stop - animation->start) *
392 animation->spring.current;
393 weston_matrix_init(&animation->transform.matrix);
394 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
395}
396
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397WL_EXPORT struct weston_view_animation *
398weston_slide_run(struct weston_view *view, float start, float stop,
399 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200400{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500401 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200402
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300403 animation = weston_view_animation_create(view, start, stop,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100405 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200406 if (!animation)
407 return NULL;
408
Ander Conselvan de Oliveiraa4a6f162014-04-14 15:48:06 +0300409 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400410 animation->spring.friction = 600;
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400411 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200412
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300413 weston_view_animation_run(animation);
414
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200415 return animation;
416}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100417
418struct weston_move_animation {
419 int dx;
420 int dy;
421 int reverse;
422 weston_view_animation_done_func_t done;
423};
424
425static void
426move_frame(struct weston_view_animation *animation)
427{
428 struct weston_move_animation *move = animation->private;
429 float scale;
430 float progress = animation->spring.current;
431
432 if (move->reverse)
433 progress = 1.0 - progress;
434
435 scale = animation->start +
436 (animation->stop - animation->start) *
437 progress;
438 weston_matrix_init(&animation->transform.matrix);
439 weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
440 weston_matrix_translate(&animation->transform.matrix,
441 move->dx * progress, move->dy * progress,
442 0);
443}
444
445static void
446move_done(struct weston_view_animation *animation, void *data)
447{
448 struct weston_move_animation *move = animation->private;
449
450 if (move->done)
451 move->done(animation, data);
452
453 free(move);
454}
455
456WL_EXPORT struct weston_view_animation *
457weston_move_scale_run(struct weston_view *view, int dx, int dy,
458 float start, float end, int reverse,
459 weston_view_animation_done_func_t done, void *data)
460{
461 struct weston_move_animation *move;
462 struct weston_view_animation *animation;
463
464 move = malloc(sizeof(*move));
465 if (!move)
466 return NULL;
467 move->dx = dx;
468 move->dy = dy;
469 move->reverse = reverse;
470 move->done = done;
471
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300472 animation = weston_view_animation_create(view, start, end, move_frame,
473 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800474
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300475 if (animation == NULL){
476 free(move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800477 return NULL;
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300478 }
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800479
Jonny Lamb91f80f22014-05-22 22:41:30 +0200480 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100481 animation->spring.friction = 1150;
482
Jonny Lamb91f80f22014-05-22 22:41:30 +0200483 weston_view_animation_run(animation);
484
Daniel Stonea67e6b92013-11-19 11:37:13 +0100485 return animation;
486}