blob: 2c7943f65887024708a7c2c1ac73d8d0125dcca0 [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>
30#include <stdio.h>
31#include <math.h>
32
33#include <unistd.h>
34#include <fcntl.h>
35
36#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070037#include "shared/helpers.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020038
39WL_EXPORT void
40weston_spring_init(struct weston_spring *spring,
Jason Ekstranda7af7042013-10-12 22:38:11 -050041 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020042{
43 spring->k = k;
44 spring->friction = 400.0;
45 spring->current = current;
46 spring->previous = current;
47 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040048 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040049 spring->min = 0.0;
50 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020051}
52
53WL_EXPORT void
54weston_spring_update(struct weston_spring *spring, uint32_t msec)
55{
56 double force, v, current, step;
57
58 /* Limit the number of executions of the loop below by ensuring that
59 * the timestamp for last update of the spring is no more than 1s ago.
60 * This handles the case where time moves backwards or forwards in
61 * large jumps.
62 */
63 if (msec - spring->timestamp > 1000) {
64 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
65 spring->timestamp, msec);
66 spring->timestamp = msec - 1000;
67 }
68
69 step = 0.01;
70 while (4 < msec - spring->timestamp) {
71 current = spring->current;
72 v = current - spring->previous;
73 force = spring->k * (spring->target - current) / 10.0 +
74 (spring->previous - current) - v * spring->friction;
75
76 spring->current =
77 current + (current - spring->previous) +
78 force * step * step;
79 spring->previous = current;
80
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040081 switch (spring->clip) {
82 case WESTON_SPRING_OVERSHOOT:
83 break;
84
85 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040086 if (spring->current > spring->max) {
87 spring->current = spring->max;
88 spring->previous = spring->max;
89 } else if (spring->current < 0.0) {
90 spring->current = spring->min;
91 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040092 }
93 break;
94
95 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040096 if (spring->current > spring->max) {
97 spring->current =
98 2 * spring->max - spring->current;
99 spring->previous =
100 2 * spring->max - spring->previous;
101 } else if (spring->current < spring->min) {
102 spring->current =
103 2 * spring->min - spring->current;
104 spring->previous =
105 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400106 }
107 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200108 }
109
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200110 spring->timestamp += 4;
111 }
112}
113
114WL_EXPORT int
115weston_spring_done(struct weston_spring *spring)
116{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400117 return fabs(spring->previous - spring->target) < 0.002 &&
118 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200119}
120
Jason Ekstranda7af7042013-10-12 22:38:11 -0500121typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200122
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123struct weston_view_animation {
124 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200125 struct weston_animation animation;
126 struct weston_spring spring;
127 struct weston_transform transform;
128 struct wl_listener listener;
129 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500130 weston_view_animation_frame_func_t frame;
131 weston_view_animation_frame_func_t reset;
132 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200133 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100134 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200135};
136
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100137WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500138weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200139{
140 wl_list_remove(&animation->animation.link);
141 wl_list_remove(&animation->listener.link);
142 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200143 if (animation->reset)
144 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500145 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200146 if (animation->done)
147 animation->done(animation, animation->data);
148 free(animation);
149}
150
151static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500152handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200153{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500154 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200155 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500156 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200157
Jason Ekstranda7af7042013-10-12 22:38:11 -0500158 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200159}
160
161static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500162weston_view_animation_frame(struct weston_animation *base,
163 struct weston_output *output, uint32_t msecs)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200164{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500165 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200166 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500167 struct weston_view_animation, animation);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200168 struct weston_compositor *compositor =
169 animation->view->surface->compositor;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200170
171 if (base->frame_counter <= 1)
172 animation->spring.timestamp = msecs;
173
174 weston_spring_update(&animation->spring, msecs);
175
176 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700177 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500178 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200179 return;
180 }
181
182 if (animation->frame)
183 animation->frame(animation);
184
Jason Ekstranda7af7042013-10-12 22:38:11 -0500185 weston_view_geometry_dirty(animation->view);
186 weston_view_schedule_repaint(animation->view);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200187
188 /* The view's output_mask will be zero if its position is
189 * offscreen. Animations should always run but as they are also
190 * run off the repaint cycle, if there's nothing to repaint
191 * the animation stops running. Therefore if we catch this situation
192 * and schedule a repaint on all outputs it will be avoided.
193 */
194 if (animation->view->output_mask == 0)
195 weston_compositor_schedule_repaint(compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200196}
197
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198static struct weston_view_animation *
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300199weston_view_animation_create(struct weston_view *view,
200 float start, float stop,
201 weston_view_animation_frame_func_t frame,
202 weston_view_animation_frame_func_t reset,
203 weston_view_animation_done_func_t done,
204 void *data,
205 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200206{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500207 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200208
209 animation = malloc(sizeof *animation);
210 if (!animation)
211 return NULL;
212
Jason Ekstranda7af7042013-10-12 22:38:11 -0500213 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200214 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200215 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200216 animation->done = done;
217 animation->data = data;
218 animation->start = start;
219 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100220 animation->private = private;
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300221
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200222 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500223 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200224 &animation->transform.link);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300225
Jason Ekstranda7af7042013-10-12 22:38:11 -0500226 animation->animation.frame = weston_view_animation_frame;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200227
Jason Ekstranda7af7042013-10-12 22:38:11 -0500228 animation->listener.notify = handle_animation_view_destroy;
229 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200230
Jason Ekstranda7af7042013-10-12 22:38:11 -0500231 wl_list_insert(&view->output->animation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200232 &animation->animation.link);
233
234 return animation;
235}
236
237static void
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300238weston_view_animation_run(struct weston_view_animation *animation)
239{
240 animation->animation.frame_counter = 0;
241 weston_view_animation_frame(&animation->animation, NULL, 0);
242}
243
244static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500245reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200246{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500247 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200248
Jason Ekstranda7af7042013-10-12 22:38:11 -0500249 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200250}
251
252static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500253zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200254{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500255 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200256 float scale;
257
258 scale = animation->start +
259 (animation->stop - animation->start) *
260 animation->spring.current;
261 weston_matrix_init(&animation->transform.matrix);
262 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600263 -0.5f * es->surface->width,
264 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200265 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
266 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600267 0.5f * es->surface->width,
268 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200269
270 es->alpha = animation->spring.current;
271 if (es->alpha > 1.0)
272 es->alpha = 1.0;
273}
274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275WL_EXPORT struct weston_view_animation *
276weston_zoom_run(struct weston_view *view, float start, float stop,
277 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200278{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400280
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300281 zoom = weston_view_animation_create(view, start, stop,
282 zoom_frame, reset_alpha,
283 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400284
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800285 if (zoom == NULL)
286 return NULL;
287
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400288 weston_spring_init(&zoom->spring, 300.0, start, stop);
289 zoom->spring.friction = 1400;
290 zoom->spring.previous = start - (stop - start) * 0.03;
291
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300292 weston_view_animation_run(zoom);
293
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400294 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200295}
296
297static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200299{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200300 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500301 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200302 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200304 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500305 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200306}
307
Jason Ekstranda7af7042013-10-12 22:38:11 -0500308WL_EXPORT struct weston_view_animation *
309weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200310 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200312{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500313 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200314
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300315 fade = weston_view_animation_create(view, start, end,
316 fade_frame, reset_alpha,
317 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200318
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800319 if (fade == NULL)
320 return NULL;
321
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300322 weston_spring_init(&fade->spring, 1000.0, start, end);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700323 fade->spring.friction = 4000;
324 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400325
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200327
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300328 weston_view_animation_run(fade);
329
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200330 return fade;
331}
332
333WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200335{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400336 fade->spring.target = target;
Jonny Lamb70ee3ad2014-07-30 00:56:18 +0100337 fade->stop = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200338}
339
340static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100341stable_fade_frame(struct weston_view_animation *animation)
342{
343 struct weston_view *back_view;
344
345 if (animation->spring.current > 0.999)
346 animation->view->alpha = 1;
347 else if (animation->spring.current < 0.001 )
348 animation->view->alpha = 0;
349 else
350 animation->view->alpha = animation->spring.current;
351
352 back_view = (struct weston_view *) animation->private;
353 back_view->alpha =
354 (animation->spring.target - animation->view->alpha) /
355 (1.0 - animation->view->alpha);
356 weston_view_geometry_dirty(back_view);
357}
358
359WL_EXPORT struct weston_view_animation *
360weston_stable_fade_run(struct weston_view *front_view, float start,
361 struct weston_view *back_view, float end,
362 weston_view_animation_done_func_t done, void *data)
363{
364 struct weston_view_animation *fade;
365
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300366 fade = weston_view_animation_create(front_view, 0, 0,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100367 stable_fade_frame, NULL,
368 done, data, back_view);
369
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800370 if (fade == NULL)
371 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100372
373 weston_spring_init(&fade->spring, 400, start, end);
374 fade->spring.friction = 1150;
375
376 front_view->alpha = start;
377 back_view->alpha = end;
378
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300379 weston_view_animation_run(fade);
380
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100381 return fade;
382}
383
384static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200386{
387 float scale;
388
389 scale = animation->start +
390 (animation->stop - animation->start) *
391 animation->spring.current;
392 weston_matrix_init(&animation->transform.matrix);
393 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
394}
395
Jason Ekstranda7af7042013-10-12 22:38:11 -0500396WL_EXPORT struct weston_view_animation *
397weston_slide_run(struct weston_view *view, float start, float stop,
398 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200399{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200401
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300402 animation = weston_view_animation_create(view, start, stop,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100404 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200405 if (!animation)
406 return NULL;
407
Ander Conselvan de Oliveiraa4a6f162014-04-14 15:48:06 +0300408 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400409 animation->spring.friction = 600;
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400410 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200411
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300412 weston_view_animation_run(animation);
413
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200414 return animation;
415}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100416
417struct weston_move_animation {
418 int dx;
419 int dy;
420 int reverse;
421 weston_view_animation_done_func_t done;
422};
423
424static void
425move_frame(struct weston_view_animation *animation)
426{
427 struct weston_move_animation *move = animation->private;
428 float scale;
429 float progress = animation->spring.current;
430
431 if (move->reverse)
432 progress = 1.0 - progress;
433
434 scale = animation->start +
435 (animation->stop - animation->start) *
436 progress;
437 weston_matrix_init(&animation->transform.matrix);
438 weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
439 weston_matrix_translate(&animation->transform.matrix,
440 move->dx * progress, move->dy * progress,
441 0);
442}
443
444static void
445move_done(struct weston_view_animation *animation, void *data)
446{
447 struct weston_move_animation *move = animation->private;
448
449 if (move->done)
450 move->done(animation, data);
451
452 free(move);
453}
454
455WL_EXPORT struct weston_view_animation *
456weston_move_scale_run(struct weston_view *view, int dx, int dy,
457 float start, float end, int reverse,
458 weston_view_animation_done_func_t done, void *data)
459{
460 struct weston_move_animation *move;
461 struct weston_view_animation *animation;
462
463 move = malloc(sizeof(*move));
464 if (!move)
465 return NULL;
466 move->dx = dx;
467 move->dy = dy;
468 move->reverse = reverse;
469 move->done = done;
470
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300471 animation = weston_view_animation_create(view, start, end, move_frame,
472 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800473
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300474 if (animation == NULL){
475 free(move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800476 return NULL;
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300477 }
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800478
Jonny Lamb91f80f22014-05-22 22:41:30 +0200479 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100480 animation->spring.friction = 1150;
481
Jonny Lamb91f80f22014-05-22 22:41:30 +0200482 weston_view_animation_run(animation);
483
Daniel Stonea67e6b92013-11-19 11:37:13 +0100484 return animation;
485}