#include #include #include #include "animate.h" #include "state.h" #include "log.h" double quadratic_bezier(double x, double a, double b, double c) { g_assert(x >= 0 && x <= 1); g_assert(a >= 0 && a <= 1); g_assert(b >= 0 && b <= 1); g_assert(c >= 0 && c <= 1); const double t = 1 - x; return a * t * t + 2 * b * t * x + c * x * x; } double cubic_bezier(double x, double a, double b, double c, double d) { g_assert(x >= 0 && x <= 1); g_assert(a >= 0 && a <= 1); g_assert(b >= 0 && b <= 1); g_assert(c >= 0 && c <= 1); g_assert(d >= 0 && d <= 1); const double t = 1 - x; return a * (t * t * t) + 3 * b * (t * t * x) + 3 * c * (t * x * x) + d * (x * x * x); } typedef struct { Animation anim; gint64 start; gint64 duration; int width; cairo_pattern_t *gradient; } AnimationShine; // FIXME: Not working for button group bool shine_paint(AnimationShine *shine, cairo_t *cr, const Layout *layout) { gint64 now = g_get_monotonic_time(); if (shine->start == 0) shine->start = now; gint64 end = shine->start + shine->duration; if (now > end) return false; double t = (double)(now - shine->start) / (end - shine->start); double pos = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0); double angle = atan((double)layout->height / layout->width); // Make it double just to be sure int h = 2 * (double)layout->height / cos(angle); int x = layout->x + pos * layout->width - shine->width; int y = layout->y; cairo_matrix_t matrix; cairo_matrix_init_translate(&matrix, -x, -y); cairo_pattern_set_matrix(shine->gradient, &matrix); int dx = layout->x + layout->width / 2; int dy = layout->y + layout->height / 2; cairo_translate(cr, dx, dy); cairo_rotate(cr, -angle); cairo_translate(cr, -dx, -dy); cairo_set_operator(cr, CAIRO_OPERATOR_ATOP); cairo_set_source(cr, shine->gradient); cairo_rectangle(cr, x, y - (h - layout->height) / 2, shine->width, h); cairo_fill(cr); return true; } Animation *animation_shine_create(gint64 duration) { // Note the 0 initialization AnimationShine *shine = g_malloc0(sizeof(AnimationShine)); shine->anim.type = ANIM_SHINE; shine->anim.paint = (DrawFunc)shine_paint; shine->duration = duration; // TODO: Change it depending on container size shine->width = 20; shine->gradient = cairo_pattern_create_linear(0, 0, shine->width, 0); cairo_pattern_add_color_stop_rgba(shine->gradient, 0, 1, 1, 1, 0); cairo_pattern_add_color_stop_rgba(shine->gradient, 0.5, 1, 1, 1, 0.333); cairo_pattern_add_color_stop_rgba(shine->gradient, 1, 1, 1, 1, 0); return (gpointer)shine; } //XXX void animation_destroy(Animation *anim) { if (anim == NULL) return; if (anim->type == ANIM_SHINE) cairo_pattern_destroy(CAST(anim, AnimationShine)->gradient); g_free(anim); } // vim: ts=4 sw=4 et