Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 4 | * 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 11 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 12 | * 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 28 | #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 | |
| 38 | WL_EXPORT void |
| 39 | weston_spring_init(struct weston_spring *spring, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 40 | double k, double current, double target) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 41 | { |
| 42 | spring->k = k; |
| 43 | spring->friction = 400.0; |
| 44 | spring->current = current; |
| 45 | spring->previous = current; |
| 46 | spring->target = target; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 47 | spring->clip = WESTON_SPRING_OVERSHOOT; |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 48 | spring->min = 0.0; |
| 49 | spring->max = 1.0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | WL_EXPORT void |
| 53 | weston_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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 80 | switch (spring->clip) { |
| 81 | case WESTON_SPRING_OVERSHOOT: |
| 82 | break; |
| 83 | |
| 84 | case WESTON_SPRING_CLAMP: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 85 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 91 | } |
| 92 | break; |
| 93 | |
| 94 | case WESTON_SPRING_BOUNCE: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 95 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 105 | } |
| 106 | break; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 109 | spring->timestamp += 4; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | WL_EXPORT int |
| 114 | weston_spring_done(struct weston_spring *spring) |
| 115 | { |
Kristian Høgsberg | c24744e | 2013-06-17 08:59:20 -0400 | [diff] [blame] | 116 | return fabs(spring->previous - spring->target) < 0.002 && |
| 117 | fabs(spring->current - spring->target) < 0.002; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 120 | 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] | 121 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 122 | struct weston_view_animation { |
| 123 | struct weston_view *view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 124 | struct weston_animation animation; |
| 125 | struct weston_spring spring; |
| 126 | struct weston_transform transform; |
| 127 | struct wl_listener listener; |
| 128 | float start, stop; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 129 | 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 132 | void *data; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 133 | void *private; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 136 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 137 | weston_view_animation_destroy(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 138 | { |
| 139 | wl_list_remove(&animation->animation.link); |
| 140 | wl_list_remove(&animation->listener.link); |
| 141 | wl_list_remove(&animation->transform.link); |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 142 | if (animation->reset) |
| 143 | animation->reset(animation); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 144 | weston_view_geometry_dirty(animation->view); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 145 | if (animation->done) |
| 146 | animation->done(animation, animation->data); |
| 147 | free(animation); |
| 148 | } |
| 149 | |
| 150 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 151 | handle_animation_view_destroy(struct wl_listener *listener, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 152 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 153 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 154 | container_of(listener, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 155 | struct weston_view_animation, listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 156 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 157 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 161 | weston_view_animation_frame(struct weston_animation *base, |
| 162 | struct weston_output *output, uint32_t msecs) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 163 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 164 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 165 | container_of(base, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 166 | struct weston_view_animation, animation); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 167 | struct weston_compositor *compositor = |
| 168 | animation->view->surface->compositor; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 169 | |
| 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øgsberg | 90dfb11 | 2013-10-30 09:07:04 -0700 | [diff] [blame] | 176 | weston_view_schedule_repaint(animation->view); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 177 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (animation->frame) |
| 182 | animation->frame(animation); |
| 183 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 184 | weston_view_geometry_dirty(animation->view); |
| 185 | weston_view_schedule_repaint(animation->view); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 186 | |
| 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 197 | static struct weston_view_animation * |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 198 | weston_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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 205 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 206 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 207 | |
| 208 | animation = malloc(sizeof *animation); |
| 209 | if (!animation) |
| 210 | return NULL; |
| 211 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 212 | animation->view = view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 213 | animation->frame = frame; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 214 | animation->reset = reset; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 215 | animation->done = done; |
| 216 | animation->data = data; |
| 217 | animation->start = start; |
| 218 | animation->stop = stop; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 219 | animation->private = private; |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 220 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 221 | weston_matrix_init(&animation->transform.matrix); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 222 | wl_list_insert(&view->geometry.transformation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 223 | &animation->transform.link); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 224 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 225 | animation->animation.frame = weston_view_animation_frame; |
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 | animation->listener.notify = handle_animation_view_destroy; |
| 228 | wl_signal_add(&view->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 229 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 230 | wl_list_insert(&view->output->animation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 231 | &animation->animation.link); |
| 232 | |
| 233 | return animation; |
| 234 | } |
| 235 | |
| 236 | static void |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 237 | weston_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 | |
| 243 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 244 | reset_alpha(struct weston_view_animation *animation) |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 245 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 246 | struct weston_view *view = animation->view; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 247 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 248 | view->alpha = animation->stop; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 252 | zoom_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 253 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 254 | struct weston_view *es = animation->view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 255 | 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 Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 262 | -0.5f * es->surface->width, |
| 263 | -0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 264 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 265 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 266 | 0.5f * es->surface->width, |
| 267 | 0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 268 | |
| 269 | es->alpha = animation->spring.current; |
| 270 | if (es->alpha > 1.0) |
| 271 | es->alpha = 1.0; |
| 272 | } |
| 273 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 274 | WL_EXPORT struct weston_view_animation * |
| 275 | weston_zoom_run(struct weston_view *view, float start, float stop, |
| 276 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 277 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 278 | struct weston_view_animation *zoom; |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 279 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 280 | zoom = weston_view_animation_create(view, start, stop, |
| 281 | zoom_frame, reset_alpha, |
| 282 | done, data, NULL); |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 283 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 284 | if (zoom == NULL) |
| 285 | return NULL; |
| 286 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 287 | 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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 291 | weston_view_animation_run(zoom); |
| 292 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 293 | return zoom; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 297 | fade_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 298 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 299 | if (animation->spring.current > 0.999) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 300 | animation->view->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 301 | else if (animation->spring.current < 0.001 ) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 302 | animation->view->alpha = 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 303 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 304 | animation->view->alpha = animation->spring.current; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 307 | WL_EXPORT struct weston_view_animation * |
| 308 | weston_fade_run(struct weston_view *view, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 309 | float start, float end, float k, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 310 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 311 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 312 | struct weston_view_animation *fade; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 313 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 314 | fade = weston_view_animation_create(view, start, end, |
| 315 | fade_frame, reset_alpha, |
| 316 | done, data, NULL); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 317 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 318 | if (fade == NULL) |
| 319 | return NULL; |
| 320 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 321 | weston_spring_init(&fade->spring, 1000.0, start, end); |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame] | 322 | fade->spring.friction = 4000; |
| 323 | fade->spring.previous = start - (end - start) * 0.1; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 324 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 325 | view->alpha = start; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 326 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 327 | weston_view_animation_run(fade); |
| 328 | |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 329 | return fade; |
| 330 | } |
| 331 | |
| 332 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 333 | weston_fade_update(struct weston_view_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 334 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 335 | fade->spring.target = target; |
Jonny Lamb | 70ee3ad | 2014-07-30 00:56:18 +0100 | [diff] [blame] | 336 | fade->stop = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 340 | stable_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 | |
| 358 | WL_EXPORT struct weston_view_animation * |
| 359 | weston_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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 365 | fade = weston_view_animation_create(front_view, 0, 0, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 366 | stable_fade_frame, NULL, |
| 367 | done, data, back_view); |
| 368 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 369 | if (fade == NULL) |
| 370 | return NULL; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 371 | |
| 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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 378 | weston_view_animation_run(fade); |
| 379 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 380 | return fade; |
| 381 | } |
| 382 | |
| 383 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 384 | slide_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 385 | { |
| 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 Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 395 | WL_EXPORT struct weston_view_animation * |
| 396 | weston_slide_run(struct weston_view *view, float start, float stop, |
| 397 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 398 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 399 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 400 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 401 | animation = weston_view_animation_create(view, start, stop, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 402 | slide_frame, NULL, done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 403 | data, NULL); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 404 | if (!animation) |
| 405 | return NULL; |
| 406 | |
Ander Conselvan de Oliveira | a4a6f16 | 2014-04-14 15:48:06 +0300 | [diff] [blame] | 407 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 408 | animation->spring.friction = 600; |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 409 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 410 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 411 | weston_view_animation_run(animation); |
| 412 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 413 | return animation; |
| 414 | } |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 415 | |
| 416 | struct weston_move_animation { |
| 417 | int dx; |
| 418 | int dy; |
| 419 | int reverse; |
| 420 | weston_view_animation_done_func_t done; |
| 421 | }; |
| 422 | |
| 423 | static void |
| 424 | move_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 | |
| 443 | static void |
| 444 | move_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 | |
| 454 | WL_EXPORT struct weston_view_animation * |
| 455 | weston_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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 470 | animation = weston_view_animation_create(view, start, end, move_frame, |
| 471 | NULL, move_done, data, move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 472 | |
| 473 | if (animation == NULL) |
| 474 | return NULL; |
| 475 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 476 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 477 | animation->spring.friction = 1150; |
| 478 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 479 | weston_view_animation_run(animation); |
| 480 | |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 481 | return animation; |
| 482 | } |