00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include "mb_animate.h"
00004 #include "mb_paint.h"
00005
00006 static float comp_mb_timeval_ratio(const mb_timeval_t *a,
00007 const mb_timeval_t *b) {
00008 float ratio;
00009
00010 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
00011 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
00012 return ratio;
00013 }
00014
00015 typedef struct _mb_chgcolor mb_chgcolor_t;
00016
00017 struct _mb_chgcolor {
00018 mb_action_t action;
00019
00020 co_comp_t r, g, b, a;
00021 paint_t *paint;
00022
00023 mb_timeval_t start_time;
00024 const mb_timeval_t *playing_time;
00025 co_comp_t s_r, s_g, s_b, s_a;
00026 };
00027
00028 static void mb_chgcolor_start(mb_action_t *act,
00029 const mb_timeval_t *now,
00030 const mb_timeval_t *playing_time,
00031 redraw_man_t *rdman) {
00032 mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
00033
00034 MB_TIMEVAL_CP(&chg->start_time, now);
00035 chg->playing_time = playing_time;
00036
00037
00038 paint_color_get(chg->paint,
00039 &chg->s_r, &chg->s_g,
00040 &chg->s_b, &chg->s_a);
00041 }
00042
00043 static void mb_chgcolor_step(mb_action_t *act,
00044 const mb_timeval_t *now,
00045 redraw_man_t *rdman) {
00046 mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
00047 mb_timeval_t diff;
00048 co_comp_t r, g, b, a;
00049 float ratio, comp;
00050
00051 MB_TIMEVAL_CP(&diff, now);
00052 MB_TIMEVAL_DIFF(&diff, &chg->start_time);
00053 ratio = comp_mb_timeval_ratio(&diff, chg->playing_time);
00054 comp = 1 - ratio;
00055
00056 r = chg->s_r * comp + ratio * chg->r;
00057 g = chg->s_g * comp + ratio * chg->g;
00058 b = chg->s_b * comp + ratio * chg->b;
00059 a = chg->s_a * comp + ratio * chg->a;
00060 paint_color_set(chg->paint, r, g, b, a);
00061
00062 rdman_paint_changed(rdman, chg->paint);
00063 }
00064
00065 static void mb_chgcolor_stop(mb_action_t *act,
00066 const mb_timeval_t *now,
00067 redraw_man_t *rdman) {
00068 mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
00069
00070 paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a);
00071
00072 rdman_paint_changed(rdman, chg->paint);
00073 }
00074
00075 static void mb_chgcolor_free(mb_action_t *act) {
00076 free(act);
00077 }
00078
00079 mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g,
00080 co_comp_t b, co_comp_t a,
00081 paint_t *paint, mb_word_t *word) {
00082 mb_chgcolor_t *chg;
00083
00084 chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t));
00085 if(chg == NULL)
00086 return NULL;
00087
00088 chg->r = r;
00089 chg->g = g;
00090 chg->b = b;
00091 chg->a = a;
00092
00093 chg->paint = paint;
00094
00095 chg->action.start = mb_chgcolor_start;
00096 chg->action.step = mb_chgcolor_step;
00097 chg->action.stop = mb_chgcolor_stop;
00098 chg->action.free = mb_chgcolor_free;
00099
00100 mb_word_add_action(word, (mb_action_t *)chg);
00101
00102 return (mb_action_t *)chg;
00103 }
00104
00105