blob: 4b69bddd6bcfd1b1c5979ee9573ac4853db45f10 [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"
37
38WL_EXPORT void
39weston_spring_init(struct weston_spring *spring,
Jason Ekstranda7af7042013-10-12 22:38:11 -050040 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020041{
42 spring->k = k;
43 spring->friction = 400.0;
44 spring->current = current;
45 spring->previous = current;
46 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040047 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040048 spring->min = 0.0;
49 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020050}
51
52WL_EXPORT void
53weston_spring_update(struct weston_spring *spring, uint32_t msec)
54{
55 double force, v, current, step;
56
57 /* Limit the number of executions of the loop below by ensuring that
58 * the timestamp for last update of the spring is no more than 1s ago.
59 * This handles the case where time moves backwards or forwards in
60 * large jumps.
61 */
62 if (msec - spring->timestamp > 1000) {
63 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
64 spring->timestamp, msec);
65 spring->timestamp = msec - 1000;
66 }
67
68 step = 0.01;
69 while (4 < msec - spring->timestamp) {
70 current = spring->current;
71 v = current - spring->previous;
72 force = spring->k * (spring->target - current) / 10.0 +
73 (spring->previous - current) - v * spring->friction;
74
75 spring->current =
76 current + (current - spring->previous) +
77 force * step * step;
78 spring->previous = current;
79
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040080 switch (spring->clip) {
81 case WESTON_SPRING_OVERSHOOT:
82 break;
83
84 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040085 if (spring->current > spring->max) {
86 spring->current = spring->max;
87 spring->previous = spring->max;
88 } else if (spring->current < 0.0) {
89 spring->current = spring->min;
90 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040091 }
92 break;
93
94 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040095 if (spring->current > spring->max) {
96 spring->current =
97 2 * spring->max - spring->current;
98 spring->previous =
99 2 * spring->max - spring->previous;
100 } else if (spring->current < spring->min) {
101 spring->current =
102 2 * spring->min - spring->current;
103 spring->previous =
104 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400105 }
106 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200107 }
108
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200109 spring->timestamp += 4;
110 }
111}
112
113WL_EXPORT int
114weston_spring_done(struct weston_spring *spring)
115{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400116 return fabs(spring->previous - spring->target) < 0.002 &&
117 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200118}
119
Jason Ekstranda7af7042013-10-12 22:38:11 -0500120typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200121
Jason Ekstranda7af7042013-10-12 22:38:11 -0500122struct weston_view_animation {
123 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200124 struct weston_animation animation;
125 struct weston_spring spring;
126 struct weston_transform transform;
127 struct wl_listener listener;
128 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500129 weston_view_animation_frame_func_t frame;
130 weston_view_animation_frame_func_t reset;
131 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200132 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100133 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200134};
135
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100136WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500137weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200138{
139 wl_list_remove(&animation->animation.link);
140 wl_list_remove(&animation->listener.link);
141 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200142 if (animation->reset)
143 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500144 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200145 if (animation->done)
146 animation->done(animation, animation->data);
147 free(animation);
148}
149
150static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500151handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200152{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200154 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200156
Jason Ekstranda7af7042013-10-12 22:38:11 -0500157 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200158}
159
160static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500161weston_view_animation_frame(struct weston_animation *base,
162 struct weston_output *output, uint32_t msecs)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200163{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500164 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200165 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500166 struct weston_view_animation, animation);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200167 struct weston_compositor *compositor =
168 animation->view->surface->compositor;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200169
170 if (base->frame_counter <= 1)
171 animation->spring.timestamp = msecs;
172
173 weston_spring_update(&animation->spring, msecs);
174
175 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700176 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500177 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200178 return;
179 }
180
181 if (animation->frame)
182 animation->frame(animation);
183
Jason Ekstranda7af7042013-10-12 22:38:11 -0500184 weston_view_geometry_dirty(animation->view);
185 weston_view_schedule_repaint(animation->view);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200186
187 /* The view's output_mask will be zero if its position is
188 * offscreen. Animations should always run but as they are also
189 * run off the repaint cycle, if there's nothing to repaint
190 * the animation stops running. Therefore if we catch this situation
191 * and schedule a repaint on all outputs it will be avoided.
192 */
193 if (animation->view->output_mask == 0)
194 weston_compositor_schedule_repaint(compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200195}
196
Jason Ekstranda7af7042013-10-12 22:38:11 -0500197static struct weston_view_animation *
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300198weston_view_animation_create(struct weston_view *view,
199 float start, float stop,
200 weston_view_animation_frame_func_t frame,
201 weston_view_animation_frame_func_t reset,
202 weston_view_animation_done_func_t done,
203 void *data,
204 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200205{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500206 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200207
208 animation = malloc(sizeof *animation);
209 if (!animation)
210 return NULL;
211
Jason Ekstranda7af7042013-10-12 22:38:11 -0500212 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200213 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200214 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200215 animation->done = done;
216 animation->data = data;
217 animation->start = start;
218 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100219 animation->private = private;
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300220
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200221 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500222 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200223 &animation->transform.link);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300224
Jason Ekstranda7af7042013-10-12 22:38:11 -0500225 animation->animation.frame = weston_view_animation_frame;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200226
Jason Ekstranda7af7042013-10-12 22:38:11 -0500227 animation->listener.notify = handle_animation_view_destroy;
228 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200229
Jason Ekstranda7af7042013-10-12 22:38:11 -0500230 wl_list_insert(&view->output->animation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200231 &animation->animation.link);
232
233 return animation;
234}
235
236static void
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300237weston_view_animation_run(struct weston_view_animation *animation)
238{
239 animation->animation.frame_counter = 0;
240 weston_view_animation_frame(&animation->animation, NULL, 0);
241}
242
243static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500244reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200245{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500246 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200247
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200249}
250
251static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500252zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200253{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500254 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200255 float scale;
256
257 scale = animation->start +
258 (animation->stop - animation->start) *
259 animation->spring.current;
260 weston_matrix_init(&animation->transform.matrix);
261 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600262 -0.5f * es->surface->width,
263 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200264 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
265 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600266 0.5f * es->surface->width,
267 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200268
269 es->alpha = animation->spring.current;
270 if (es->alpha > 1.0)
271 es->alpha = 1.0;
272}
273
Jason Ekstranda7af7042013-10-12 22:38:11 -0500274WL_EXPORT struct weston_view_animation *
275weston_zoom_run(struct weston_view *view, float start, float stop,
276 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200277{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400279
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300280 zoom = weston_view_animation_create(view, start, stop,
281 zoom_frame, reset_alpha,
282 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400283
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800284 if (zoom == NULL)
285 return NULL;
286
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400287 weston_spring_init(&zoom->spring, 300.0, start, stop);
288 zoom->spring.friction = 1400;
289 zoom->spring.previous = start - (stop - start) * 0.03;
290
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300291 weston_view_animation_run(zoom);
292
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400293 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200294}
295
296static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200298{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200299 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200301 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500302 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200303 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200305}
306
Jason Ekstranda7af7042013-10-12 22:38:11 -0500307WL_EXPORT struct weston_view_animation *
308weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200309 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500310 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200311{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200313
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300314 fade = weston_view_animation_create(view, start, end,
315 fade_frame, reset_alpha,
316 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200317
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800318 if (fade == NULL)
319 return NULL;
320
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300321 weston_spring_init(&fade->spring, 1000.0, start, end);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700322 fade->spring.friction = 4000;
323 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400324
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200326
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300327 weston_view_animation_run(fade);
328
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200329 return fade;
330}
331
332WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200334{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400335 fade->spring.target = target;
Jonny Lamb70ee3ad2014-07-30 00:56:18 +0100336 fade->stop = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200337}
338
339static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100340stable_fade_frame(struct weston_view_animation *animation)
341{
342 struct weston_view *back_view;
343
344 if (animation->spring.current > 0.999)
345 animation->view->alpha = 1;
346 else if (animation->spring.current < 0.001 )
347 animation->view->alpha = 0;
348 else
349 animation->view->alpha = animation->spring.current;
350
351 back_view = (struct weston_view *) animation->private;
352 back_view->alpha =
353 (animation->spring.target - animation->view->alpha) /
354 (1.0 - animation->view->alpha);
355 weston_view_geometry_dirty(back_view);
356}
357
358WL_EXPORT struct weston_view_animation *
359weston_stable_fade_run(struct weston_view *front_view, float start,
360 struct weston_view *back_view, float end,
361 weston_view_animation_done_func_t done, void *data)
362{
363 struct weston_view_animation *fade;
364
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300365 fade = weston_view_animation_create(front_view, 0, 0,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100366 stable_fade_frame, NULL,
367 done, data, back_view);
368
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800369 if (fade == NULL)
370 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100371
372 weston_spring_init(&fade->spring, 400, start, end);
373 fade->spring.friction = 1150;
374
375 front_view->alpha = start;
376 back_view->alpha = end;
377
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300378 weston_view_animation_run(fade);
379
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100380 return fade;
381}
382
383static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500384slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200385{
386 float scale;
387
388 scale = animation->start +
389 (animation->stop - animation->start) *
390 animation->spring.current;
391 weston_matrix_init(&animation->transform.matrix);
392 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
393}
394
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395WL_EXPORT struct weston_view_animation *
396weston_slide_run(struct weston_view *view, float start, float stop,
397 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200398{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500399 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200400
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300401 animation = weston_view_animation_create(view, start, stop,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100403 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200404 if (!animation)
405 return NULL;
406
Ander Conselvan de Oliveiraa4a6f162014-04-14 15:48:06 +0300407 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400408 animation->spring.friction = 600;
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400409 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200410
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300411 weston_view_animation_run(animation);
412
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200413 return animation;
414}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100415
416struct weston_move_animation {
417 int dx;
418 int dy;
419 int reverse;
420 weston_view_animation_done_func_t done;
421};
422
423static void
424move_frame(struct weston_view_animation *animation)
425{
426 struct weston_move_animation *move = animation->private;
427 float scale;
428 float progress = animation->spring.current;
429
430 if (move->reverse)
431 progress = 1.0 - progress;
432
433 scale = animation->start +
434 (animation->stop - animation->start) *
435 progress;
436 weston_matrix_init(&animation->transform.matrix);
437 weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
438 weston_matrix_translate(&animation->transform.matrix,
439 move->dx * progress, move->dy * progress,
440 0);
441}
442
443static void
444move_done(struct weston_view_animation *animation, void *data)
445{
446 struct weston_move_animation *move = animation->private;
447
448 if (move->done)
449 move->done(animation, data);
450
451 free(move);
452}
453
454WL_EXPORT struct weston_view_animation *
455weston_move_scale_run(struct weston_view *view, int dx, int dy,
456 float start, float end, int reverse,
457 weston_view_animation_done_func_t done, void *data)
458{
459 struct weston_move_animation *move;
460 struct weston_view_animation *animation;
461
462 move = malloc(sizeof(*move));
463 if (!move)
464 return NULL;
465 move->dx = dx;
466 move->dy = dy;
467 move->reverse = reverse;
468 move->done = done;
469
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300470 animation = weston_view_animation_create(view, start, end, move_frame,
471 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800472
473 if (animation == NULL)
474 return NULL;
475
Jonny Lamb91f80f22014-05-22 22:41:30 +0200476 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100477 animation->spring.friction = 1150;
478
Jonny Lamb91f80f22014-05-22 22:41:30 +0200479 weston_view_animation_run(animation);
480
Daniel Stonea67e6b92013-11-19 11:37:13 +0100481 return animation;
482}