comparison examples/dynamic/main.c @ 217:8d9d717c9300

Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
author wycc@wycc-desktop
date Sat, 13 Dec 2008 17:13:31 +0800
parents 31933f9ee70e
children ad4f8a956505
comparison
equal deleted inserted replaced
216:f61b709bbd0f 217:8d9d717c9300
4 * menu with MadButterfly. 4 * menu with MadButterfly.
5 * 5 *
6 */ 6 */
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <mb.h> 8 #include <mb.h>
9 #include <string.h>
9 #include "menu.h" 10 #include "menu.h"
10 #include "button.h" 11 #include "button.h"
11 12
12 13
13 typedef struct _engine engine_t; 14 typedef struct _engine engine_t;
21 int start_x,start_y; 22 int start_x,start_y;
22 observer_t *obs1,*obs2; 23 observer_t *obs1,*obs2;
23 shape_t *rect; 24 shape_t *rect;
24 co_aix rx,ry; 25 co_aix rx,ry;
25 }; 26 };
27
28
29 typedef struct _mb_button {
30 engine_t *en;
31 coord_t *root;
32 coord_t *active;
33 coord_t *normal;
34 coord_t *click;
35 void (*press)();
36 void *arg;
37 } mb_button_t;
38
39
40 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(en->rdman, group)
41 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(en->rdman, group)
42
43 #define CMOUSE(e) (coord_get_mouse_event(e))
44
45
46
47 static void mb_button_move(event_t *evt, void *arg)
48 {
49 mb_button_t *btn = (mb_button_t *) arg;
50 engine_t *en = btn->en;
51
52
53 printf("Mouse move\n");
54 COORD_SHOW(btn->active);
55 rdman_coord_changed(btn->en->rdman,btn->root);
56 rdman_redraw_changed(btn->en->rdman);
57 }
58 static void mb_button_out(event_t *evt, void *arg)
59 {
60 mb_button_t *btn = (mb_button_t *) arg;
61 engine_t *en = btn->en;
62
63 printf("mouse out\n");
64 COORD_HIDE(btn->active);
65 rdman_coord_changed(btn->en->rdman,btn->root);
66 rdman_redraw_changed(btn->en->rdman);
67 }
68
69 void mb_button_show_active(const mb_timeval_t *tmo, const mb_timeval_t *now, void *arg)
70 {
71 mb_button_t *btn = (mb_button_t *) arg;
72 engine_t *en = btn->en;
73
74 COORD_HIDE(btn->click);
75 rdman_coord_changed(btn->en->rdman,btn->root);
76 rdman_redraw_changed(btn->en->rdman);
77 }
78
79 void mb_button_pressed(event_t *evt, void *arg)
80 {
81 mb_button_t *btn = (mb_button_t *) arg;
82 engine_t *en = btn->en;
83 mb_timeval_t now,to;
84
85 printf("Pressed\n");
86 COORD_SHOW(btn->click);
87 rdman_coord_changed(en->rdman,en->button->root_coord);
88 rdman_redraw_changed(en->rdman);
89 get_now(&now);
90 MB_TIMEVAL_SET(&to, 0, 500);
91 MB_TIMEVAL_ADD(&to,&now);
92
93 mb_tman_timeout(X_MB_tman(btn->en->rt), &to, mb_button_show_active, arg);
94 }
95 mb_button_t *mb_button_new(engine_t *en,mb_sprite_t *sp, char *name)
96 {
97 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t));
98 char *buf = (char *) malloc(strlen(name)+5);
99
100 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name);
101 sprintf(buf, "%s_normal", name);
102 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
103 if (btn->normal == NULL) {
104 printf("Missing normal button, this is not a correct button\n");
105 }
106 sprintf(buf, "%s_active", name);
107 btn->active = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
108 if (btn->active == NULL) {
109 printf("Missing active button, this is not a correct button\n");
110 }
111 sprintf(buf, "%s_click", name);
112 btn->click = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
113 if (btn->click == NULL) {
114 printf("Missing click button, this is not a correct button\n");
115 }
116 btn->press = NULL;
117 // Show only the normal button
118 COORD_HIDE(btn->active);
119 COORD_HIDE(btn->click);
120 // Move to the same position
121 btn->active->matrix[2] = 200;
122 btn->active->matrix[5] = 200;
123 btn->normal->matrix[2] = 200;
124 btn->normal->matrix[5] = 200;
125 btn->click->matrix[2] = 200;
126 btn->click->matrix[5] = 200;
127 btn->en = en;
128 subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn);
129 subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn);
130 subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_RELEASE, mb_button_pressed,btn);
131 return btn;
132 }
133
134
135 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg)
136 {
137 b->press = h;
138 b->arg = arg;
139 }
140
26 engine_t *engine_init() 141 engine_t *engine_init()
27 { 142 {
28 143
29 X_MB_runtime_t *rt; 144 X_MB_runtime_t *rt;
30 rt = X_MB_new(":0.0", 800, 600); 145 rt = X_MB_new(":0.0", 800, 600);
48 */ 163 */
49 menu_free(en->menu); 164 menu_free(en->menu);
50 X_MB_free(en->rt); 165 X_MB_free(en->rt);
51 free(en); 166 free(en);
52 } 167 }
53 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(en->rdman, group) 168
54 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(en->rdman, group)
55
56 #define CMOUSE(e) (coord_get_mouse_event(e))
57
58
59
60 static void button_move(event_t *evt, void *arg)
61 {
62 engine_t *en = (engine_t *) arg;
63
64
65 printf("Mouse move\n");
66 COORD_SHOW(en->button->active);
67 rdman_coord_changed(en->rdman,en->button->root_coord);
68 rdman_redraw_changed(en->rdman);
69 }
70 static void button_out(event_t *evt, void *arg)
71 {
72 engine_t *en = (engine_t *) arg;
73
74 printf("mouse out\n");
75 COORD_HIDE(en->button->active);
76 rdman_coord_changed(en->rdman,en->button->root_coord);
77 rdman_redraw_changed(en->rdman);
78 }
79
80 void button_pressed(event_t *evt, void *arg)
81 {
82 printf("Pressed\n");
83 }
84
85 void engine_add_button(engine_t *en, coord_t *normal, coord_t *active, void (*func)())
86 {
87 active->matrix[2] = 200;
88 active->matrix[5] = 200;
89 normal->matrix[2] = 200;
90 normal->matrix[5] = 200;
91 COORD_HIDE(en->button->active);
92 rdman_coord_changed(en->rdman,en->button->root_coord);
93 rdman_redraw_changed(en->rdman);
94 subject_add_event_observer(CMOUSE(normal), EVT_MOUSE_MOVE, button_move,en);
95 subject_add_event_observer(CMOUSE(active), EVT_MOUSE_OUT, button_out,en);
96 subject_add_event_observer(CMOUSE(active), EVT_MOUSE_BUT_RELEASE, button_pressed,en);
97 }
98 169
99 170
100 void coord_move(coord_t *c, co_aix x, co_aix y) 171 void coord_move(coord_t *c, co_aix x, co_aix y)
101 { 172 {
102 c->matrix[2] = x; 173 c->matrix[2] = x;
169 // Add to the stage 240 // Add to the stage
170 rdman_add_shape(en->rdman, en->rect, en->menu->root_coord); 241 rdman_add_shape(en->rdman, en->rect, en->menu->root_coord);
171 } 242 }
172 243
173 244
174 245 void test(void *a)
246 {
247 printf("Button is pressed.....\n");
248 }
175 249
176 250
177 int main(int argc, char * const argv[]) { 251 int main(int argc, char * const argv[]) {
178 subject_t *subject; 252 subject_t *subject;
179 engine_t *en; 253 engine_t *en;
254 mb_button_t *b;
180 255
181 en = engine_init(); 256 en = engine_init();
182 en->menu = menu_new(en->rdman, en->rdman->root_coord); 257 en->menu = menu_new(en->rdman, en->rdman->root_coord);
183 en->button = button_new(en->rdman, en->rdman->root_coord); 258 en->button = button_new(en->rdman, en->rdman->root_coord);
184 engine_add_button(en, en->button->normal, en->button->active, button_pressed); 259 b = mb_button_new(en, (mb_sprite_t *) en->button, "btn");
260 mb_button_add_onClick(b, test,NULL);
185 261
186 en->rx = 0; 262 en->rx = 0;
187 en->ry = 0; 263 en->ry = 0;
188 264
189 /* 265 /*