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); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 164 | |
| 165 | if (base->frame_counter <= 1) |
| 166 | animation->spring.timestamp = msecs; |
| 167 | |
| 168 | weston_spring_update(&animation->spring, msecs); |
| 169 | |
| 170 | if (weston_spring_done(&animation->spring)) { |
Kristian Høgsberg | 90dfb11 | 2013-10-30 09:07:04 -0700 | [diff] [blame] | 171 | weston_view_schedule_repaint(animation->view); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 172 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (animation->frame) |
| 177 | animation->frame(animation); |
| 178 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 179 | weston_view_geometry_dirty(animation->view); |
| 180 | weston_view_schedule_repaint(animation->view); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 183 | static struct weston_view_animation * |
| 184 | weston_view_animation_run(struct weston_view *view, |
| 185 | float start, float stop, |
| 186 | weston_view_animation_frame_func_t frame, |
| 187 | weston_view_animation_frame_func_t reset, |
| 188 | weston_view_animation_done_func_t done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 189 | void *data, |
| 190 | void *private) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 191 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 192 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 193 | |
| 194 | animation = malloc(sizeof *animation); |
| 195 | if (!animation) |
| 196 | return NULL; |
| 197 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 198 | animation->view = view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 199 | animation->frame = frame; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 200 | animation->reset = reset; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 201 | animation->done = done; |
| 202 | animation->data = data; |
| 203 | animation->start = start; |
| 204 | animation->stop = stop; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 205 | animation->private = private; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 206 | weston_matrix_init(&animation->transform.matrix); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 207 | wl_list_insert(&view->geometry.transformation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 208 | &animation->transform.link); |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame^] | 209 | weston_spring_init(&animation->spring, 200.0, start, stop); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 210 | animation->spring.friction = 700; |
| 211 | animation->animation.frame_counter = 0; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 212 | animation->animation.frame = weston_view_animation_frame; |
| 213 | weston_view_animation_frame(&animation->animation, NULL, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 214 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 215 | animation->listener.notify = handle_animation_view_destroy; |
| 216 | wl_signal_add(&view->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 217 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 218 | wl_list_insert(&view->output->animation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 219 | &animation->animation.link); |
| 220 | |
| 221 | return animation; |
| 222 | } |
| 223 | |
| 224 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 225 | reset_alpha(struct weston_view_animation *animation) |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 226 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 227 | struct weston_view *view = animation->view; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 228 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 229 | view->alpha = animation->stop; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 233 | zoom_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 234 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 235 | struct weston_view *es = animation->view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 236 | float scale; |
| 237 | |
| 238 | scale = animation->start + |
| 239 | (animation->stop - animation->start) * |
| 240 | animation->spring.current; |
| 241 | weston_matrix_init(&animation->transform.matrix); |
| 242 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 243 | -0.5f * es->surface->width, |
| 244 | -0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 245 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 246 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 247 | 0.5f * es->surface->width, |
| 248 | 0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 249 | |
| 250 | es->alpha = animation->spring.current; |
| 251 | if (es->alpha > 1.0) |
| 252 | es->alpha = 1.0; |
| 253 | } |
| 254 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 255 | WL_EXPORT struct weston_view_animation * |
| 256 | weston_zoom_run(struct weston_view *view, float start, float stop, |
| 257 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 258 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 259 | struct weston_view_animation *zoom; |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 260 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 261 | zoom = weston_view_animation_run(view, start, stop, |
| 262 | zoom_frame, reset_alpha, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 263 | done, data, NULL); |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 264 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 265 | if (zoom == NULL) |
| 266 | return NULL; |
| 267 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 268 | weston_spring_init(&zoom->spring, 300.0, start, stop); |
| 269 | zoom->spring.friction = 1400; |
| 270 | zoom->spring.previous = start - (stop - start) * 0.03; |
| 271 | |
| 272 | return zoom; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 276 | fade_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 277 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 278 | if (animation->spring.current > 0.999) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 279 | animation->view->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 280 | else if (animation->spring.current < 0.001 ) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 281 | animation->view->alpha = 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 282 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 283 | animation->view->alpha = animation->spring.current; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 286 | WL_EXPORT struct weston_view_animation * |
| 287 | weston_fade_run(struct weston_view *view, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 288 | float start, float end, float k, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 289 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 290 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 291 | struct weston_view_animation *fade; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 292 | |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame^] | 293 | fade = weston_view_animation_run(view, start, end, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 294 | fade_frame, reset_alpha, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 295 | done, data, NULL); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 296 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 297 | if (fade == NULL) |
| 298 | return NULL; |
| 299 | |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame^] | 300 | fade->spring.k = 1000.0; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 301 | |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame^] | 302 | fade->spring.friction = 4000; |
| 303 | fade->spring.previous = start - (end - start) * 0.1; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 304 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 305 | view->alpha = start; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 306 | |
| 307 | return fade; |
| 308 | } |
| 309 | |
| 310 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 311 | weston_fade_update(struct weston_view_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 312 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 313 | fade->spring.target = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 317 | stable_fade_frame(struct weston_view_animation *animation) |
| 318 | { |
| 319 | struct weston_view *back_view; |
| 320 | |
| 321 | if (animation->spring.current > 0.999) |
| 322 | animation->view->alpha = 1; |
| 323 | else if (animation->spring.current < 0.001 ) |
| 324 | animation->view->alpha = 0; |
| 325 | else |
| 326 | animation->view->alpha = animation->spring.current; |
| 327 | |
| 328 | back_view = (struct weston_view *) animation->private; |
| 329 | back_view->alpha = |
| 330 | (animation->spring.target - animation->view->alpha) / |
| 331 | (1.0 - animation->view->alpha); |
| 332 | weston_view_geometry_dirty(back_view); |
| 333 | } |
| 334 | |
| 335 | WL_EXPORT struct weston_view_animation * |
| 336 | weston_stable_fade_run(struct weston_view *front_view, float start, |
| 337 | struct weston_view *back_view, float end, |
| 338 | weston_view_animation_done_func_t done, void *data) |
| 339 | { |
| 340 | struct weston_view_animation *fade; |
| 341 | |
| 342 | fade = weston_view_animation_run(front_view, 0, 0, |
| 343 | stable_fade_frame, NULL, |
| 344 | done, data, back_view); |
| 345 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 346 | if (fade == NULL) |
| 347 | return NULL; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 348 | |
| 349 | weston_spring_init(&fade->spring, 400, start, end); |
| 350 | fade->spring.friction = 1150; |
| 351 | |
| 352 | front_view->alpha = start; |
| 353 | back_view->alpha = end; |
| 354 | |
| 355 | return fade; |
| 356 | } |
| 357 | |
| 358 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 359 | slide_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 360 | { |
| 361 | float scale; |
| 362 | |
| 363 | scale = animation->start + |
| 364 | (animation->stop - animation->start) * |
| 365 | animation->spring.current; |
| 366 | weston_matrix_init(&animation->transform.matrix); |
| 367 | weston_matrix_translate(&animation->transform.matrix, 0, scale, 0); |
| 368 | } |
| 369 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 370 | WL_EXPORT struct weston_view_animation * |
| 371 | weston_slide_run(struct weston_view *view, float start, float stop, |
| 372 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 373 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 374 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 375 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 376 | animation = weston_view_animation_run(view, start, stop, |
| 377 | slide_frame, NULL, done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 378 | data, NULL); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 379 | if (!animation) |
| 380 | return NULL; |
| 381 | |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 382 | animation->spring.friction = 600; |
| 383 | animation->spring.k = 400; |
| 384 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 385 | |
| 386 | return animation; |
| 387 | } |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 388 | |
| 389 | struct weston_move_animation { |
| 390 | int dx; |
| 391 | int dy; |
| 392 | int reverse; |
| 393 | weston_view_animation_done_func_t done; |
| 394 | }; |
| 395 | |
| 396 | static void |
| 397 | move_frame(struct weston_view_animation *animation) |
| 398 | { |
| 399 | struct weston_move_animation *move = animation->private; |
| 400 | float scale; |
| 401 | float progress = animation->spring.current; |
| 402 | |
| 403 | if (move->reverse) |
| 404 | progress = 1.0 - progress; |
| 405 | |
| 406 | scale = animation->start + |
| 407 | (animation->stop - animation->start) * |
| 408 | progress; |
| 409 | weston_matrix_init(&animation->transform.matrix); |
| 410 | weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f); |
| 411 | weston_matrix_translate(&animation->transform.matrix, |
| 412 | move->dx * progress, move->dy * progress, |
| 413 | 0); |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | move_done(struct weston_view_animation *animation, void *data) |
| 418 | { |
| 419 | struct weston_move_animation *move = animation->private; |
| 420 | |
| 421 | if (move->done) |
| 422 | move->done(animation, data); |
| 423 | |
| 424 | free(move); |
| 425 | } |
| 426 | |
| 427 | WL_EXPORT struct weston_view_animation * |
| 428 | weston_move_scale_run(struct weston_view *view, int dx, int dy, |
| 429 | float start, float end, int reverse, |
| 430 | weston_view_animation_done_func_t done, void *data) |
| 431 | { |
| 432 | struct weston_move_animation *move; |
| 433 | struct weston_view_animation *animation; |
| 434 | |
| 435 | move = malloc(sizeof(*move)); |
| 436 | if (!move) |
| 437 | return NULL; |
| 438 | move->dx = dx; |
| 439 | move->dy = dy; |
| 440 | move->reverse = reverse; |
| 441 | move->done = done; |
| 442 | |
| 443 | animation = weston_view_animation_run(view, start, end, move_frame, |
| 444 | NULL, move_done, data, move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 445 | |
| 446 | if (animation == NULL) |
| 447 | return NULL; |
| 448 | |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 449 | animation->spring.k = 400; |
| 450 | animation->spring.friction = 1150; |
| 451 | |
| 452 | return animation; |
| 453 | } |