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