comparison examples/dynamic/mbbutton.c @ 249:ab8284c8dcee

* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman. * Remove reference to the MBApp from the mb_button_t so that it will be easier for us to generate button inside the sprite.
author wycc
date Sun, 04 Jan 2009 09:32:49 +0800
parents d9a78c859660
children 26c302b47de1
comparison
equal deleted inserted replaced
248:1958bb2a87a2 249:ab8284c8dcee
5 #include "mbapp.h" 5 #include "mbapp.h"
6 6
7 7
8 8
9 #define CMOUSE(e) (coord_get_mouse_event(e)) 9 #define CMOUSE(e) (coord_get_mouse_event(e))
10 #define MBAPP_DATA(app,type) ((type *) ((app)->private))
11 #define MBAPP_RDMAN(app) (((MBApp *) app)->rdman)
12 10
13 11
14 static void mb_button_pressed(event_t *evt, void *arg); 12 static void mb_button_pressed(event_t *evt, void *arg);
15 static void mb_button_out(event_t *evt, void *arg); 13 static void mb_button_out(event_t *evt, void *arg);
16 14
20 b->arg = arg; 18 b->arg = arg;
21 } 19 }
22 20
23 void mb_button_refresh(mb_button_t *btn) 21 void mb_button_refresh(mb_button_t *btn)
24 { 22 {
25 rdman_coord_changed(btn->en->rdman,btn->root); 23 rdman_coord_changed(btn->rdman,btn->root);
26 rdman_redraw_changed(btn->en->rdman); 24 rdman_redraw_changed(btn->rdman);
27 } 25 }
28 26
29 static void mb_button_move(event_t *evt, void *arg) 27 static void mb_button_move(event_t *evt, void *arg)
30 { 28 {
31 mb_button_t *btn = (mb_button_t *) arg; 29 mb_button_t *btn = (mb_button_t *) arg;
32 MBApp *en = btn->en;
33 30
34 31
35 printf("Mouse move\n"); 32 printf("Mouse move\n");
36 arg = (void *)en;
37 coord_show(btn->active); 33 coord_show(btn->active);
38 mb_button_refresh(btn); 34 mb_button_refresh(btn);
39 } 35 }
40 static void mb_button_out(event_t *evt, void *arg) 36 static void mb_button_out(event_t *evt, void *arg)
41 { 37 {
42 mb_button_t *btn = (mb_button_t *) arg; 38 mb_button_t *btn = (mb_button_t *) arg;
43 MBApp *en = btn->en;
44 arg = (void *) en;
45 39
46 if (btn->progm) { 40 if (btn->progm) {
47 mb_progm_abort(btn->progm); 41 mb_progm_abort(btn->progm);
48 btn->progm = NULL; 42 btn->progm = NULL;
49 } 43 }
55 } 49 }
56 50
57 static void mb_button_show_active(event_t *evt, void *arg) 51 static void mb_button_show_active(event_t *evt, void *arg)
58 { 52 {
59 mb_button_t *btn = (mb_button_t *) arg; 53 mb_button_t *btn = (mb_button_t *) arg;
60 MBApp *en = btn->en;
61 54
62 coord_show(btn->active); 55 coord_show(btn->active);
63 mb_button_refresh(btn); 56 mb_button_refresh(btn);
64 } 57 }
65 58
66 static void mb_button_pressed(event_t *evt, void *arg) 59 static void mb_button_pressed(event_t *evt, void *arg)
67 { 60 {
68 mb_button_t *btn = (mb_button_t *) arg; 61 mb_button_t *btn = (mb_button_t *) arg;
69 MBApp *en = btn->en;
70 mb_timeval_t start, playing, now; 62 mb_timeval_t start, playing, now;
71 mb_progm_t *progm; 63 mb_progm_t *progm;
72 mb_word_t *word; 64 mb_word_t *word;
73 arg = (void *) en;
74 65
75 printf("Pressed\n"); 66 printf("Pressed\n");
76 if (btn->progm) { 67 if (btn->progm) {
77 mb_progm_abort(btn->progm); 68 mb_progm_abort(btn->progm);
78 btn->progm = NULL; 69 btn->progm = NULL;
79 } 70 }
80 coord_show(btn->click); 71 coord_show(btn->click);
81 coord_hide(btn->active); 72 coord_hide(btn->active);
82 rdman_coord_changed(MBAPP_RDMAN(arg),btn->root); 73 rdman_coord_changed(btn->rdman,btn->root);
83 rdman_redraw_changed(MBAPP_RDMAN(arg)); 74 rdman_redraw_changed(btn->rdman);
84 75
85 btn->progm = progm = mb_progm_new(1, MBAPP_RDMAN(arg)); 76 btn->progm = progm = mb_progm_new(1, btn->rdman);
86 MB_TIMEVAL_SET(&start, 0, 500000); 77 MB_TIMEVAL_SET(&start, 0, 500000);
87 MB_TIMEVAL_SET(&playing, 0, 0); 78 MB_TIMEVAL_SET(&playing, 0, 0);
88 word = mb_progm_next_word(progm, &start, &playing); 79 word = mb_progm_next_word(progm, &start, &playing);
89 mb_visibility_new(VIS_HIDDEN, btn->click, word); 80 mb_visibility_new(VIS_HIDDEN, btn->click, word);
90 mb_visibility_new(VIS_VISIBLE, btn->active, word); 81 mb_visibility_new(VIS_VISIBLE, btn->active, word);
91 mb_progm_free_completed(progm); 82 mb_progm_free_completed(progm);
92 get_now(&now); 83 get_now(&now);
93 mb_progm_start(progm, X_MB_tman(en->rt), &now); 84 printf("rt = %x\n", btn->rdman->rt);
85 mb_progm_start(progm, X_MB_tman(btn->rdman->rt), &now);
94 if (btn->press) 86 if (btn->press)
95 btn->press(btn->arg); 87 btn->press(btn->arg);
96 } 88 }
97 mb_button_t *mb_button_new(MBApp *app,mb_sprite_t *sp, char *name) 89 mb_button_t *mb_button_new(redraw_man_t *rdman,mb_sprite_t *sp, char *name)
98 { 90 {
99 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t)); 91 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t));
100 char *buf = (char *) malloc(strlen(name)+5); 92 char *buf = (char *) malloc(strlen(name)+5);
101 MBApp *arg = app;
102 93
103 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name); 94 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name);
104 sprintf(buf, "%s_normal", name); 95 sprintf(buf, "%s_normal", name);
105 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); 96 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
106 if (btn->normal == NULL) { 97 if (btn->normal == NULL) {
126 btn->active->matrix[5] = 200; 117 btn->active->matrix[5] = 200;
127 btn->normal->matrix[2] = 200; 118 btn->normal->matrix[2] = 200;
128 btn->normal->matrix[5] = 200; 119 btn->normal->matrix[5] = 200;
129 btn->click->matrix[2] = 200; 120 btn->click->matrix[2] = 200;
130 btn->click->matrix[5] = 200; 121 btn->click->matrix[5] = 200;
131 btn->en = app; 122 btn->rdman = rdman;
132 btn->obs_move = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn); 123 btn->obs_move = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn);
133 btn->obs_press = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn); 124 btn->obs_press = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn);
134 btn->obs_out = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn); 125 btn->obs_out = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn);
135 btn->progm = NULL; 126 btn->progm = NULL;
136 rdman_redraw_changed(MBAPP_RDMAN(arg)); 127 rdman_redraw_changed(rdman);
137 return btn; 128 return btn;
138 } 129 }