Mercurial > MadButterfly
comparison src/animate.c @ 52:59a295651480
Add action mb_chgcolor_t to change color of paints.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 09 Aug 2008 22:16:23 +0800 |
parents | 5f5bd3ac9316 |
children | ab028c9f0930 |
comparison
equal
deleted
inserted
replaced
51:5f5bd3ac9316 | 52:59a295651480 |
---|---|
356 mb_word_add_action(word, (mb_action_t *)shift); | 356 mb_word_add_action(word, (mb_action_t *)shift); |
357 | 357 |
358 return (mb_action_t *)shift; | 358 return (mb_action_t *)shift; |
359 } | 359 } |
360 | 360 |
361 | |
362 #include "paint.h" | |
363 typedef struct _mb_chgcolor mb_chgcolor_t; | |
364 | |
365 struct _mb_chgcolor { | |
366 mb_action_t action; | |
367 | |
368 co_comp_t r, g, b, a; | |
369 paint_t *paint; | |
370 | |
371 mb_timeval_t start_time; | |
372 const mb_timeval_t *playing_time; | |
373 co_comp_t s_r, s_g, s_b, s_a; /*!< saved RGBA values. */ | |
374 }; | |
375 | |
376 static void mb_chgcolor_start(mb_action_t *act, | |
377 const mb_timeval_t *now, | |
378 const mb_timeval_t *playing_time, | |
379 redraw_man_t *rdman) { | |
380 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; | |
381 | |
382 MB_TIMEVAL_CP(&chg->start_time, now); | |
383 chg->playing_time = playing_time; /* playing_time is in word, | |
384 * it live time is as long as | |
385 * actions. */ | |
386 paint_color_get(chg->paint, | |
387 &chg->s_r, &chg->s_g, | |
388 &chg->s_b, &chg->s_a); | |
389 } | |
390 | |
391 static void mb_chgcolor_step(mb_action_t *act, | |
392 const mb_timeval_t *now, | |
393 redraw_man_t *rdman) { | |
394 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; | |
395 mb_timeval_t diff; | |
396 co_comp_t r, g, b, a; | |
397 float ratio, comp; | |
398 | |
399 MB_TIMEVAL_CP(&diff, now); | |
400 MB_TIMEVAL_DIFF(&diff, &chg->start_time); | |
401 ratio = comp_mb_timeval_ratio(&diff, chg->playing_time); | |
402 comp = 1 - ratio; | |
403 | |
404 r = chg->s_r * comp + ratio * chg->r; | |
405 g = chg->s_g * comp + ratio * chg->g; | |
406 b = chg->s_b * comp + ratio * chg->b; | |
407 a = chg->s_a * comp + ratio * chg->a; | |
408 paint_color_set(chg->paint, r, g, b, a); | |
409 | |
410 rdman_paint_changed(rdman, chg->paint); | |
411 } | |
412 | |
413 static void mb_chgcolor_stop(mb_action_t *act, | |
414 const mb_timeval_t *now, | |
415 redraw_man_t *rdman) { | |
416 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; | |
417 | |
418 paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a); | |
419 | |
420 rdman_paint_changed(rdman, chg->paint); | |
421 } | |
422 | |
423 static void mb_chgcolor_free(mb_action_t *act) { | |
424 free(act); | |
425 } | |
426 | |
427 mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g, | |
428 co_comp_t b, co_comp_t a, | |
429 paint_t *paint, mb_word_t *word) { | |
430 mb_chgcolor_t *chg; | |
431 | |
432 chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t)); | |
433 if(chg == NULL) | |
434 return NULL; | |
435 | |
436 chg->r = r; | |
437 chg->g = g; | |
438 chg->b = b; | |
439 chg->a = a; | |
440 | |
441 chg->paint = paint; | |
442 | |
443 chg->action.start = mb_chgcolor_start; | |
444 chg->action.step = mb_chgcolor_step; | |
445 chg->action.stop = mb_chgcolor_stop; | |
446 chg->action.free = mb_chgcolor_free; | |
447 | |
448 mb_word_add_action(word, (mb_action_t *)chg); | |
449 | |
450 return (mb_action_t *)chg; | |
451 } | |
452 | |
361 #ifdef UNITTEST | 453 #ifdef UNITTEST |
362 | 454 |
363 #include <CUnit/Basic.h> | 455 #include <CUnit/Basic.h> |
364 | 456 |
365 typedef struct _mb_dummy mb_dummy_t; | 457 typedef struct _mb_dummy mb_dummy_t; |