00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "mb_animate.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_shift mb_shift_t;
00016
00017 struct _mb_shift {
00018 mb_action_t action;
00019
00020 co_aix x, y;
00021 coord_t *coord;
00022
00023 mb_timeval_t start_time;
00024 co_aix saved_matrix[6];
00025 const mb_timeval_t *playing_time;
00026 };
00027
00028 static void mb_shift_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_shift_t *shift = (mb_shift_t *)act;
00033 coord_t *coord;
00034
00035 MB_TIMEVAL_CP(&shift->start_time, now);
00036 coord = shift->coord;
00037 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6]));
00038 shift->playing_time = playing_time;
00039 }
00040
00041 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now,
00042 redraw_man_t *rdman) {
00043 mb_shift_t *shift = (mb_shift_t *)act;
00044 mb_timeval_t diff;
00045 coord_t *coord;
00046 float ratio;
00047
00048
00049 MB_TIMEVAL_CP(&diff, now);
00050 MB_TIMEVAL_DIFF(&diff, &shift->start_time);
00051 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time);
00052
00053 coord = shift->coord;
00054 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio;
00055 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio;
00056
00057 rdman_coord_changed(rdman, coord);
00058 }
00059
00060 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now,
00061 redraw_man_t *rdman) {
00062 mb_shift_t *shift = (mb_shift_t *)act;
00063 coord_t *coord;
00064
00065 coord = shift->coord;
00066 coord->matrix[2] = shift->saved_matrix[2] + shift->x;
00067 coord->matrix[5] = shift->saved_matrix[5] + shift->y;
00068
00069 rdman_coord_changed(rdman, coord);
00070 }
00071
00072
00073 static void mb_shift_free(mb_action_t *act) {
00074 free(act);
00075 }
00076
00077 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord,
00078 mb_word_t *word) {
00079 mb_shift_t *shift;
00080
00081 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t));
00082 if(shift == NULL)
00083 return (mb_action_t *)shift;
00084
00085 shift->x = x;
00086 shift->y = y;
00087 shift->coord = coord;
00088
00089 shift->action.start = mb_shift_start;
00090 shift->action.step = mb_shift_step;
00091 shift->action.stop = mb_shift_stop;
00092 shift->action.free = mb_shift_free;
00093
00094
00095 mb_word_add_action(word, (mb_action_t *)shift);
00096
00097 return (mb_action_t *)shift;
00098 }