comparison src/chgcolor.c @ 116:1d74eb3861b7

move animation actions from animate.c to files.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 14 Sep 2008 09:42:07 +0800
parents
children c7e5b8779bb5
comparison
equal deleted inserted replaced
115:3895d2224e67 116:1d74eb3861b7
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "animate.h"
4 #include "paint.h"
5
6 static float comp_mb_timeval_ratio(const mb_timeval_t *a,
7 const mb_timeval_t *b) {
8 float ratio;
9
10 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
11 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
12 return ratio;
13 }
14
15 typedef struct _mb_chgcolor mb_chgcolor_t;
16
17 struct _mb_chgcolor {
18 mb_action_t action;
19
20 co_comp_t r, g, b, a;
21 paint_t *paint;
22
23 mb_timeval_t start_time;
24 const mb_timeval_t *playing_time;
25 co_comp_t s_r, s_g, s_b, s_a; /*!< saved RGBA values. */
26 };
27
28 static void mb_chgcolor_start(mb_action_t *act,
29 const mb_timeval_t *now,
30 const mb_timeval_t *playing_time,
31 redraw_man_t *rdman) {
32 mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
33
34 MB_TIMEVAL_CP(&chg->start_time, now);
35 chg->playing_time = playing_time; /* playing_time is in word,
36 * it live time is as long as
37 * actions. */
38 paint_color_get(chg->paint,
39 &chg->s_r, &chg->s_g,
40 &chg->s_b, &chg->s_a);
41 }
42
43 static void mb_chgcolor_step(mb_action_t *act,
44 const mb_timeval_t *now,
45 redraw_man_t *rdman) {
46 mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
47 mb_timeval_t diff;
48 co_comp_t r, g, b, a;
49 float ratio, comp;
50
51 MB_TIMEVAL_CP(&diff, now);
52 MB_TIMEVAL_DIFF(&diff, &chg->start_time);
53 ratio = comp_mb_timeval_ratio(&diff, chg->playing_time);
54 comp = 1 - ratio;
55
56 r = chg->s_r * comp + ratio * chg->r;
57 g = chg->s_g * comp + ratio * chg->g;
58 b = chg->s_b * comp + ratio * chg->b;
59 a = chg->s_a * comp + ratio * chg->a;
60 paint_color_set(chg->paint, r, g, b, a);
61
62 rdman_paint_changed(rdman, chg->paint);
63 }
64
65 static void mb_chgcolor_stop(mb_action_t *act,
66 const mb_timeval_t *now,
67 redraw_man_t *rdman) {
68 mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
69
70 paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a);
71
72 rdman_paint_changed(rdman, chg->paint);
73 }
74
75 static void mb_chgcolor_free(mb_action_t *act) {
76 free(act);
77 }
78
79 mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g,
80 co_comp_t b, co_comp_t a,
81 paint_t *paint, mb_word_t *word) {
82 mb_chgcolor_t *chg;
83
84 chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t));
85 if(chg == NULL)
86 return NULL;
87
88 chg->r = r;
89 chg->g = g;
90 chg->b = b;
91 chg->a = a;
92
93 chg->paint = paint;
94
95 chg->action.start = mb_chgcolor_start;
96 chg->action.step = mb_chgcolor_step;
97 chg->action.stop = mb_chgcolor_stop;
98 chg->action.free = mb_chgcolor_free;
99
100 mb_word_add_action(word, (mb_action_t *)chg);
101
102 return (mb_action_t *)chg;
103 }
104
105