changeset 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 ffed18510d55
files src/X_main.c src/animate.c src/animate.h src/paint.c src/paint.h
diffstat 5 files changed, 113 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/X_main.c	Sat Aug 09 21:34:07 2008 +0800
+++ b/src/X_main.c	Sat Aug 09 22:16:23 2008 +0800
@@ -313,8 +313,6 @@
 	word = mb_progm_next_word(progm, &start, &playing);
 	act = mb_shift_new(0, 20, coord1, word);
 	
-	MB_TIMEVAL_SET(&start, 3, 0);
-	MB_TIMEVAL_SET(&playing, 2, 0);
 	act = mb_shift_new(0, -20, coord2, word);
 	
 	MB_TIMEVAL_SET(&start, 3, 0);
@@ -322,9 +320,10 @@
 	word = mb_progm_next_word(progm, &start, &playing);
 	act = mb_shift_new(0, -20, coord1, word);
 	
-	MB_TIMEVAL_SET(&start, 3, 0);
-	MB_TIMEVAL_SET(&playing, 2, 0);
 	act = mb_shift_new(0, 20, coord2, word);
+
+	act = mb_chgcolor_new(0, 0, 1, 0.5, fill1, word);
+	act = mb_chgcolor_new(1, 0, 0, 0.5, fill2, word);
 	
 	gettimeofday(&tv, NULL);
 	MB_TIMEVAL_SET(&mbtv, tv.tv_sec, tv.tv_usec);
--- a/src/animate.c	Sat Aug 09 21:34:07 2008 +0800
+++ b/src/animate.c	Sat Aug 09 22:16:23 2008 +0800
@@ -358,6 +358,98 @@
     return (mb_action_t *)shift;
 }
 
+
+#include "paint.h"
+typedef struct _mb_chgcolor mb_chgcolor_t;
+
+struct _mb_chgcolor {
+    mb_action_t action;
+
+    co_comp_t r, g, b, a;
+    paint_t *paint;
+
+    mb_timeval_t start_time;
+    const mb_timeval_t *playing_time;
+    co_comp_t s_r, s_g, s_b, s_a; /*!< saved RGBA values. */
+};
+
+static void mb_chgcolor_start(mb_action_t *act,
+			      const mb_timeval_t *now,
+			      const mb_timeval_t *playing_time,
+			      redraw_man_t *rdman) {
+    mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
+
+    MB_TIMEVAL_CP(&chg->start_time, now);
+    chg->playing_time = playing_time; /* playing_time is in word,
+				       * it live time is as long as
+				       * actions. */
+    paint_color_get(chg->paint,
+		    &chg->s_r, &chg->s_g,
+		    &chg->s_b, &chg->s_a);
+}
+
+static void mb_chgcolor_step(mb_action_t *act,
+			     const mb_timeval_t *now,
+			     redraw_man_t *rdman) {
+    mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
+    mb_timeval_t diff;
+    co_comp_t r, g, b, a;
+    float ratio, comp;
+
+    MB_TIMEVAL_CP(&diff, now);
+    MB_TIMEVAL_DIFF(&diff, &chg->start_time);
+    ratio = comp_mb_timeval_ratio(&diff, chg->playing_time);
+    comp = 1 - ratio;
+
+    r = chg->s_r * comp + ratio * chg->r;
+    g = chg->s_g * comp + ratio * chg->g;
+    b = chg->s_b * comp + ratio * chg->b;
+    a = chg->s_a * comp + ratio * chg->a;
+    paint_color_set(chg->paint, r, g, b, a);
+
+    rdman_paint_changed(rdman, chg->paint);
+}
+
+static void mb_chgcolor_stop(mb_action_t *act,
+			     const mb_timeval_t *now,
+			     redraw_man_t *rdman) {
+    mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
+
+    paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a);
+
+    rdman_paint_changed(rdman, chg->paint);
+}
+
+static void mb_chgcolor_free(mb_action_t *act) {
+    free(act);
+}
+
+mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g,
+			     co_comp_t b, co_comp_t a,
+			     paint_t *paint, mb_word_t *word) {
+    mb_chgcolor_t *chg;
+
+    chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t));
+    if(chg == NULL)
+	return NULL;
+
+    chg->r = r;
+    chg->g = g;
+    chg->b = b;
+    chg->a = a;
+
+    chg->paint = paint;
+
+    chg->action.start = mb_chgcolor_start;
+    chg->action.step = mb_chgcolor_step;
+    chg->action.stop = mb_chgcolor_stop;
+    chg->action.free = mb_chgcolor_free;
+
+    mb_word_add_action(word, (mb_action_t *)chg);
+
+    return (mb_action_t *)chg;
+}
+
 #ifdef UNITTEST
 
 #include <CUnit/Basic.h>
--- a/src/animate.h	Sat Aug 09 21:34:07 2008 +0800
+++ b/src/animate.h	Sat Aug 09 22:16:23 2008 +0800
@@ -3,6 +3,7 @@
 
 #include "mb_types.h"
 #include "mb_timer.h"
+#include "paint.h"
 
 typedef struct _mb_progm mb_progm_t;
 typedef struct _mb_word mb_word_t;
@@ -19,6 +20,9 @@
 extern void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman);
 extern mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord,
 				 mb_word_t *word);
+extern mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g,
+				    co_comp_t b, co_comp_t a,
+				    paint_t *paint, mb_word_t *word);
 
 
 #endif /* __ANIMATE_H_ */
--- a/src/paint.c	Sat Aug 09 21:34:07 2008 +0800
+++ b/src/paint.c	Sat Aug 09 22:16:23 2008 +0800
@@ -53,6 +53,17 @@
     color->a = a;
 }
 
+void paint_color_get(paint_t *paint,
+		     co_comp_t *r, co_comp_t *g,
+		     co_comp_t *b, co_comp_t *a) {
+    paint_color_t *color = (paint_color_t *)paint;
+
+    *r = color->r;
+    *g = color->g;
+    *b = color->b;
+    *a = color->a;
+}
+
 /*! \brief Linear gradient.
  */
 typedef struct _paint_linear {
--- a/src/paint.h	Sat Aug 09 21:34:07 2008 +0800
+++ b/src/paint.h	Sat Aug 09 22:16:23 2008 +0800
@@ -14,6 +14,9 @@
 extern void paint_color_set(paint_t *paint,
 			    co_comp_t r, co_comp_t g,
 			    co_comp_t b, co_comp_t a);
+extern void paint_color_get(paint_t *paint,
+			    co_comp_t *r, co_comp_t *g,
+			    co_comp_t *b, co_comp_t *a);
 #define paint_init(_paint, _prepare, _free)	\
      do {					\
 	 (_paint)->prepare = _prepare;		\