Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdio.h> |
| 28 | #include <math.h> |
| 29 | |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #include "compositor.h" |
| 34 | |
| 35 | WL_EXPORT void |
| 36 | weston_spring_init(struct weston_spring *spring, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 37 | double k, double current, double target) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 38 | { |
| 39 | spring->k = k; |
| 40 | spring->friction = 400.0; |
| 41 | spring->current = current; |
| 42 | spring->previous = current; |
| 43 | spring->target = target; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 44 | spring->clip = WESTON_SPRING_OVERSHOOT; |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 45 | spring->min = 0.0; |
| 46 | spring->max = 1.0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | WL_EXPORT void |
| 50 | weston_spring_update(struct weston_spring *spring, uint32_t msec) |
| 51 | { |
| 52 | double force, v, current, step; |
| 53 | |
| 54 | /* Limit the number of executions of the loop below by ensuring that |
| 55 | * the timestamp for last update of the spring is no more than 1s ago. |
| 56 | * This handles the case where time moves backwards or forwards in |
| 57 | * large jumps. |
| 58 | */ |
| 59 | if (msec - spring->timestamp > 1000) { |
| 60 | weston_log("unexpectedly large timestamp jump (from %u to %u)\n", |
| 61 | spring->timestamp, msec); |
| 62 | spring->timestamp = msec - 1000; |
| 63 | } |
| 64 | |
| 65 | step = 0.01; |
| 66 | while (4 < msec - spring->timestamp) { |
| 67 | current = spring->current; |
| 68 | v = current - spring->previous; |
| 69 | force = spring->k * (spring->target - current) / 10.0 + |
| 70 | (spring->previous - current) - v * spring->friction; |
| 71 | |
| 72 | spring->current = |
| 73 | current + (current - spring->previous) + |
| 74 | force * step * step; |
| 75 | spring->previous = current; |
| 76 | |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 77 | switch (spring->clip) { |
| 78 | case WESTON_SPRING_OVERSHOOT: |
| 79 | break; |
| 80 | |
| 81 | case WESTON_SPRING_CLAMP: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 82 | if (spring->current > spring->max) { |
| 83 | spring->current = spring->max; |
| 84 | spring->previous = spring->max; |
| 85 | } else if (spring->current < 0.0) { |
| 86 | spring->current = spring->min; |
| 87 | spring->previous = spring->min; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 88 | } |
| 89 | break; |
| 90 | |
| 91 | case WESTON_SPRING_BOUNCE: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 92 | if (spring->current > spring->max) { |
| 93 | spring->current = |
| 94 | 2 * spring->max - spring->current; |
| 95 | spring->previous = |
| 96 | 2 * spring->max - spring->previous; |
| 97 | } else if (spring->current < spring->min) { |
| 98 | spring->current = |
| 99 | 2 * spring->min - spring->current; |
| 100 | spring->previous = |
| 101 | 2 * spring->min - spring->previous; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 102 | } |
| 103 | break; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 106 | spring->timestamp += 4; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | WL_EXPORT int |
| 111 | weston_spring_done(struct weston_spring *spring) |
| 112 | { |
Kristian Høgsberg | c24744e | 2013-06-17 08:59:20 -0400 | [diff] [blame] | 113 | return fabs(spring->previous - spring->target) < 0.002 && |
| 114 | fabs(spring->current - spring->target) < 0.002; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 117 | typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 118 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 119 | struct weston_view_animation { |
| 120 | struct weston_view *view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 121 | struct weston_animation animation; |
| 122 | struct weston_spring spring; |
| 123 | struct weston_transform transform; |
| 124 | struct wl_listener listener; |
| 125 | float start, stop; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 126 | weston_view_animation_frame_func_t frame; |
| 127 | weston_view_animation_frame_func_t reset; |
| 128 | weston_view_animation_done_func_t done; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 129 | void *data; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 130 | void *private; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 133 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 134 | weston_view_animation_destroy(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 135 | { |
| 136 | wl_list_remove(&animation->animation.link); |
| 137 | wl_list_remove(&animation->listener.link); |
| 138 | wl_list_remove(&animation->transform.link); |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 139 | if (animation->reset) |
| 140 | animation->reset(animation); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 141 | weston_view_geometry_dirty(animation->view); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 142 | if (animation->done) |
| 143 | animation->done(animation, animation->data); |
| 144 | free(animation); |
| 145 | } |
| 146 | |
| 147 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 148 | handle_animation_view_destroy(struct wl_listener *listener, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 149 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 150 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 151 | container_of(listener, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 152 | struct weston_view_animation, listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 153 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 154 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 158 | weston_view_animation_frame(struct weston_animation *base, |
| 159 | struct weston_output *output, uint32_t msecs) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 160 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 161 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 162 | container_of(base, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 163 | struct weston_view_animation, animation); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame^] | 164 | struct weston_compositor *compositor = |
| 165 | animation->view->surface->compositor; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 166 | |
| 167 | if (base->frame_counter <= 1) |
| 168 | animation->spring.timestamp = msecs; |
| 169 | |
| 170 | weston_spring_update(&animation->spring, msecs); |
| 171 | |
| 172 | if (weston_spring_done(&animation->spring)) { |
Kristian Høgsberg | 90dfb11 | 2013-10-30 09:07:04 -0700 | [diff] [blame] | 173 | weston_view_schedule_repaint(animation->view); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 174 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | if (animation->frame) |
| 179 | animation->frame(animation); |
| 180 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 181 | weston_view_geometry_dirty(animation->view); |
| 182 | weston_view_schedule_repaint(animation->view); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame^] | 183 | |
| 184 | /* The view's output_mask will be zero if its position is |
| 185 | * offscreen. Animations should always run but as they are also |
| 186 | * run off the repaint cycle, if there's nothing to repaint |
| 187 | * the animation stops running. Therefore if we catch this situation |
| 188 | * and schedule a repaint on all outputs it will be avoided. |
| 189 | */ |
| 190 | if (animation->view->output_mask == 0) |
| 191 | weston_compositor_schedule_repaint(compositor); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 194 | static struct weston_view_animation * |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 195 | weston_view_animation_create(struct weston_view *view, |
| 196 | float start, float stop, |
| 197 | weston_view_animation_frame_func_t frame, |
| 198 | weston_view_animation_frame_func_t reset, |
| 199 | weston_view_animation_done_func_t done, |
| 200 | void *data, |
| 201 | void *private) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 202 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 203 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 204 | |
| 205 | animation = malloc(sizeof *animation); |
| 206 | if (!animation) |
| 207 | return NULL; |
| 208 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 209 | animation->view = view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 210 | animation->frame = frame; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 211 | animation->reset = reset; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 212 | animation->done = done; |
| 213 | animation->data = data; |
| 214 | animation->start = start; |
| 215 | animation->stop = stop; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 216 | animation->private = private; |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 217 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 218 | weston_matrix_init(&animation->transform.matrix); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 219 | wl_list_insert(&view->geometry.transformation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 220 | &animation->transform.link); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 221 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 222 | animation->animation.frame = weston_view_animation_frame; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 223 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 224 | animation->listener.notify = handle_animation_view_destroy; |
| 225 | wl_signal_add(&view->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 226 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 227 | wl_list_insert(&view->output->animation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 228 | &animation->animation.link); |
| 229 | |
| 230 | return animation; |
| 231 | } |
| 232 | |
| 233 | static void |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 234 | weston_view_animation_run(struct weston_view_animation *animation) |
| 235 | { |
| 236 | animation->animation.frame_counter = 0; |
| 237 | weston_view_animation_frame(&animation->animation, NULL, 0); |
| 238 | } |
| 239 | |
| 240 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 241 | reset_alpha(struct weston_view_animation *animation) |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 242 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 243 | struct weston_view *view = animation->view; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 244 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 245 | view->alpha = animation->stop; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 249 | zoom_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 250 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 251 | struct weston_view *es = animation->view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 252 | float scale; |
| 253 | |
| 254 | scale = animation->start + |
| 255 | (animation->stop - animation->start) * |
| 256 | animation->spring.current; |
| 257 | weston_matrix_init(&animation->transform.matrix); |
| 258 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 259 | -0.5f * es->surface->width, |
| 260 | -0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 261 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 262 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 263 | 0.5f * es->surface->width, |
| 264 | 0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 265 | |
| 266 | es->alpha = animation->spring.current; |
| 267 | if (es->alpha > 1.0) |
| 268 | es->alpha = 1.0; |
| 269 | } |
| 270 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 271 | WL_EXPORT struct weston_view_animation * |
| 272 | weston_zoom_run(struct weston_view *view, float start, float stop, |
| 273 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 274 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 275 | struct weston_view_animation *zoom; |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 276 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 277 | zoom = weston_view_animation_create(view, start, stop, |
| 278 | zoom_frame, reset_alpha, |
| 279 | done, data, NULL); |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 280 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 281 | if (zoom == NULL) |
| 282 | return NULL; |
| 283 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 284 | weston_spring_init(&zoom->spring, 300.0, start, stop); |
| 285 | zoom->spring.friction = 1400; |
| 286 | zoom->spring.previous = start - (stop - start) * 0.03; |
| 287 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 288 | weston_view_animation_run(zoom); |
| 289 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 290 | return zoom; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 294 | fade_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 295 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 296 | if (animation->spring.current > 0.999) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 297 | animation->view->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 298 | else if (animation->spring.current < 0.001 ) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 299 | animation->view->alpha = 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 300 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 301 | animation->view->alpha = animation->spring.current; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 304 | WL_EXPORT struct weston_view_animation * |
| 305 | weston_fade_run(struct weston_view *view, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 306 | float start, float end, float k, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 307 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 308 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 309 | struct weston_view_animation *fade; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 310 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 311 | fade = weston_view_animation_create(view, start, end, |
| 312 | fade_frame, reset_alpha, |
| 313 | done, data, NULL); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 314 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 315 | if (fade == NULL) |
| 316 | return NULL; |
| 317 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 318 | weston_spring_init(&fade->spring, 1000.0, start, end); |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame] | 319 | fade->spring.friction = 4000; |
| 320 | fade->spring.previous = start - (end - start) * 0.1; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 321 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 322 | view->alpha = start; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 323 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 324 | weston_view_animation_run(fade); |
| 325 | |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 326 | return fade; |
| 327 | } |
| 328 | |
| 329 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 330 | weston_fade_update(struct weston_view_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 331 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 332 | fade->spring.target = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 336 | stable_fade_frame(struct weston_view_animation *animation) |
| 337 | { |
| 338 | struct weston_view *back_view; |
| 339 | |
| 340 | if (animation->spring.current > 0.999) |
| 341 | animation->view->alpha = 1; |
| 342 | else if (animation->spring.current < 0.001 ) |
| 343 | animation->view->alpha = 0; |
| 344 | else |
| 345 | animation->view->alpha = animation->spring.current; |
| 346 | |
| 347 | back_view = (struct weston_view *) animation->private; |
| 348 | back_view->alpha = |
| 349 | (animation->spring.target - animation->view->alpha) / |
| 350 | (1.0 - animation->view->alpha); |
| 351 | weston_view_geometry_dirty(back_view); |
| 352 | } |
| 353 | |
| 354 | WL_EXPORT struct weston_view_animation * |
| 355 | weston_stable_fade_run(struct weston_view *front_view, float start, |
| 356 | struct weston_view *back_view, float end, |
| 357 | weston_view_animation_done_func_t done, void *data) |
| 358 | { |
| 359 | struct weston_view_animation *fade; |
| 360 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 361 | fade = weston_view_animation_create(front_view, 0, 0, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 362 | stable_fade_frame, NULL, |
| 363 | done, data, back_view); |
| 364 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 365 | if (fade == NULL) |
| 366 | return NULL; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 367 | |
| 368 | weston_spring_init(&fade->spring, 400, start, end); |
| 369 | fade->spring.friction = 1150; |
| 370 | |
| 371 | front_view->alpha = start; |
| 372 | back_view->alpha = end; |
| 373 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 374 | weston_view_animation_run(fade); |
| 375 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 376 | return fade; |
| 377 | } |
| 378 | |
| 379 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 380 | slide_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 381 | { |
| 382 | float scale; |
| 383 | |
| 384 | scale = animation->start + |
| 385 | (animation->stop - animation->start) * |
| 386 | animation->spring.current; |
| 387 | weston_matrix_init(&animation->transform.matrix); |
| 388 | weston_matrix_translate(&animation->transform.matrix, 0, scale, 0); |
| 389 | } |
| 390 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 391 | WL_EXPORT struct weston_view_animation * |
| 392 | weston_slide_run(struct weston_view *view, float start, float stop, |
| 393 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 394 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 395 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 396 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 397 | animation = weston_view_animation_create(view, start, stop, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 398 | slide_frame, NULL, done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 399 | data, NULL); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 400 | if (!animation) |
| 401 | return NULL; |
| 402 | |
Ander Conselvan de Oliveira | a4a6f16 | 2014-04-14 15:48:06 +0300 | [diff] [blame] | 403 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 404 | animation->spring.friction = 600; |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 405 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 406 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 407 | weston_view_animation_run(animation); |
| 408 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 409 | return animation; |
| 410 | } |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 411 | |
| 412 | struct weston_move_animation { |
| 413 | int dx; |
| 414 | int dy; |
| 415 | int reverse; |
| 416 | weston_view_animation_done_func_t done; |
| 417 | }; |
| 418 | |
| 419 | static void |
| 420 | move_frame(struct weston_view_animation *animation) |
| 421 | { |
| 422 | struct weston_move_animation *move = animation->private; |
| 423 | float scale; |
| 424 | float progress = animation->spring.current; |
| 425 | |
| 426 | if (move->reverse) |
| 427 | progress = 1.0 - progress; |
| 428 | |
| 429 | scale = animation->start + |
| 430 | (animation->stop - animation->start) * |
| 431 | progress; |
| 432 | weston_matrix_init(&animation->transform.matrix); |
| 433 | weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f); |
| 434 | weston_matrix_translate(&animation->transform.matrix, |
| 435 | move->dx * progress, move->dy * progress, |
| 436 | 0); |
| 437 | } |
| 438 | |
| 439 | static void |
| 440 | move_done(struct weston_view_animation *animation, void *data) |
| 441 | { |
| 442 | struct weston_move_animation *move = animation->private; |
| 443 | |
| 444 | if (move->done) |
| 445 | move->done(animation, data); |
| 446 | |
| 447 | free(move); |
| 448 | } |
| 449 | |
| 450 | WL_EXPORT struct weston_view_animation * |
| 451 | weston_move_scale_run(struct weston_view *view, int dx, int dy, |
| 452 | float start, float end, int reverse, |
| 453 | weston_view_animation_done_func_t done, void *data) |
| 454 | { |
| 455 | struct weston_move_animation *move; |
| 456 | struct weston_view_animation *animation; |
| 457 | |
| 458 | move = malloc(sizeof(*move)); |
| 459 | if (!move) |
| 460 | return NULL; |
| 461 | move->dx = dx; |
| 462 | move->dy = dy; |
| 463 | move->reverse = reverse; |
| 464 | move->done = done; |
| 465 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 466 | animation = weston_view_animation_create(view, start, end, move_frame, |
| 467 | NULL, move_done, data, move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 468 | |
| 469 | if (animation == NULL) |
| 470 | return NULL; |
| 471 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 472 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 473 | animation->spring.friction = 1150; |
| 474 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 475 | weston_view_animation_run(animation); |
| 476 | |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 477 | return animation; |
| 478 | } |