comparison examples/dynamic/main.c @ 240:d347a577a232

Rewrite the example by using the MBApp API.
author wycc
date Tue, 30 Dec 2008 09:21:23 +0800
parents 3e6da6f6a226
children d3fe0a0f3a8b
comparison
equal deleted inserted replaced
239:1499b8b86fcc 240:d347a577a232
6 */ 6 */
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <mb.h> 8 #include <mb.h>
9 #include <string.h> 9 #include <string.h>
10 #include "menu.h" 10 #include "menu.h"
11 #include "button.h" 11
12 12
13 13 typedef struct _mbapp MBApp;
14 typedef struct _engine engine_t; 14 struct _mbapp {
15 struct _engine { 15 void *rt;
16 X_MB_runtime_t *rt;
17 redraw_man_t *rdman; 16 redraw_man_t *rdman;
18 menu_t *menu; 17 mb_sprite_t *rootsprite;
19 mb_sprite_t *button; 18 mb_obj_t *root;
20 int state; 19 void *private;
20 };
21
22 typedef struct {
23 shape_t *rect;
21 co_aix orx,ory; 24 co_aix orx,ory;
22 int start_x,start_y; 25 int start_x,start_y;
23 observer_t *obs1,*obs2; 26 observer_t *obs1,*obs2;
24 shape_t *rect; 27 }MyAppData;
25 co_aix rx,ry; 28
26 }; 29 #define MBAPP_DATA(app,type) ((type *) ((app)->private))
30 #define MBAPP_RDMAN(app) (((MBApp *) app)->rdman)
27 31
28 32
29 typedef struct _mb_button { 33 typedef struct _mb_button {
30 mb_obj_t obj; 34 mb_obj_t obj;
31 engine_t *en; 35 MBApp *en;
36 int state;
32 coord_t *root; 37 coord_t *root;
33 coord_t *active; 38 coord_t *active;
34 coord_t *normal; 39 coord_t *normal;
35 coord_t *click; 40 coord_t *click;
36 void (*press)(); 41 void (*press)();
38 observer_t *obs_move,*obs_out,*obs_press; 43 observer_t *obs_move,*obs_out,*obs_press;
39 mb_progm_t *progm; 44 mb_progm_t *progm;
40 } mb_button_t; 45 } mb_button_t;
41 46
42 47
43 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(en->rdman, group)
44 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(en->rdman, group)
45 48
46 #define CMOUSE(e) (coord_get_mouse_event(e)) 49 #define CMOUSE(e) (coord_get_mouse_event(e))
47 50
48 51
49 static void mb_button_pressed(event_t *evt, void *arg); 52 static void mb_button_pressed(event_t *evt, void *arg);
50 static void mb_button_out(event_t *evt, void *arg); 53 static void mb_button_out(event_t *evt, void *arg);
51 54
55 void mb_button_refresh(mb_button_t *btn)
56 {
57 rdman_coord_changed(btn->en->rdman,btn->root);
58 rdman_redraw_changed(btn->en->rdman);
59 }
60
52 static void mb_button_move(event_t *evt, void *arg) 61 static void mb_button_move(event_t *evt, void *arg)
53 { 62 {
54 mb_button_t *btn = (mb_button_t *) arg; 63 mb_button_t *btn = (mb_button_t *) arg;
55 engine_t *en = btn->en; 64 MBApp *en = btn->en;
56 65
57 66
58 printf("Mouse move\n"); 67 printf("Mouse move\n");
59 COORD_SHOW(btn->active); 68 arg = (void *)en;
60 #if 0 69 coord_show(btn->active);
61 rdman_coord_changed(btn->en->rdman,btn->root); 70 mb_button_refresh(btn);
62 #endif
63 rdman_redraw_changed(btn->en->rdman);
64 } 71 }
65 static void mb_button_out(event_t *evt, void *arg) 72 static void mb_button_out(event_t *evt, void *arg)
66 { 73 {
67 mb_button_t *btn = (mb_button_t *) arg; 74 mb_button_t *btn = (mb_button_t *) arg;
68 engine_t *en = btn->en; 75 MBApp *en = btn->en;
76 arg = (void *) en;
69 77
70 if (btn->progm) { 78 if (btn->progm) {
71 mb_progm_abort(btn->progm); 79 mb_progm_abort(btn->progm);
72 btn->progm = NULL; 80 btn->progm = NULL;
73 } 81 }
74 printf("mouse out\n"); 82 printf("mouse out\n");
75 COORD_HIDE(btn->click); 83 coord_hide(btn->click);
76 COORD_HIDE(btn->active); 84 coord_hide(btn->active);
77 COORD_SHOW(btn->normal); 85 coord_show(btn->normal);
78 #if 1 86 mb_button_refresh(btn);
79 rdman_coord_changed(btn->en->rdman,btn->normal);
80 #endif
81 rdman_redraw_changed(btn->en->rdman);
82 } 87 }
83 88
84 void mb_button_show_active(event_t *evt, void *arg) 89 void mb_button_show_active(event_t *evt, void *arg)
85 { 90 {
86 mb_button_t *btn = (mb_button_t *) arg; 91 mb_button_t *btn = (mb_button_t *) arg;
87 engine_t *en = btn->en; 92 MBApp *en = btn->en;
88 93
89 COORD_SHOW(btn->active); 94 coord_show(btn->active);
90 rdman_coord_changed(btn->en->rdman,btn->root); 95 mb_button_refresh(btn);
91 rdman_redraw_changed(btn->en->rdman);
92 } 96 }
93 97
94 void mb_button_pressed(event_t *evt, void *arg) 98 void mb_button_pressed(event_t *evt, void *arg)
95 { 99 {
96 mb_button_t *btn = (mb_button_t *) arg; 100 mb_button_t *btn = (mb_button_t *) arg;
97 engine_t *en = btn->en; 101 MBApp *en = btn->en;
98 mb_timeval_t start, playing, now; 102 mb_timeval_t start, playing, now;
99 mb_progm_t *progm; 103 mb_progm_t *progm;
100 mb_word_t *word; 104 mb_word_t *word;
105 arg = (void *) en;
101 106
102 printf("Pressed\n"); 107 printf("Pressed\n");
103 if (btn->progm) { 108 if (btn->progm) {
104 mb_progm_abort(btn->progm); 109 mb_progm_abort(btn->progm);
105 btn->progm = NULL; 110 btn->progm = NULL;
106 } 111 }
107 COORD_SHOW(btn->click); 112 coord_show(btn->click);
108 COORD_HIDE(btn->active); 113 coord_hide(btn->active);
109 rdman_coord_changed(en->rdman,btn->root); 114 rdman_coord_changed(MBAPP_RDMAN(arg),btn->root);
110 rdman_redraw_changed(en->rdman); 115 rdman_redraw_changed(MBAPP_RDMAN(arg));
111 116
112 btn->progm = progm = mb_progm_new(1, en->rdman); 117 btn->progm = progm = mb_progm_new(1, MBAPP_RDMAN(arg));
113 MB_TIMEVAL_SET(&start, 0, 500000); 118 MB_TIMEVAL_SET(&start, 0, 500000);
114 MB_TIMEVAL_SET(&playing, 0, 0); 119 MB_TIMEVAL_SET(&playing, 0, 0);
115 word = mb_progm_next_word(progm, &start, &playing); 120 word = mb_progm_next_word(progm, &start, &playing);
116 mb_visibility_new(VIS_HIDDEN, btn->click, word); 121 mb_visibility_new(VIS_HIDDEN, btn->click, word);
117 mb_visibility_new(VIS_VISIBLE, btn->active, word); 122 mb_visibility_new(VIS_VISIBLE, btn->active, word);
119 get_now(&now); 124 get_now(&now);
120 mb_progm_start(progm, X_MB_tman(en->rt), &now); 125 mb_progm_start(progm, X_MB_tman(en->rt), &now);
121 if (btn->press) 126 if (btn->press)
122 btn->press(btn->arg); 127 btn->press(btn->arg);
123 } 128 }
124 mb_button_t *mb_button_new(engine_t *en,mb_sprite_t *sp, char *name) 129 mb_button_t *mb_button_new(MBApp *app,mb_sprite_t *sp, char *name)
125 { 130 {
126 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t)); 131 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t));
127 char *buf = (char *) malloc(strlen(name)+5); 132 char *buf = (char *) malloc(strlen(name)+5);
133 MBApp *arg = app;
128 134
129 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name); 135 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name);
130 printf("btn->root=%x\n",btn->root);
131 sprintf(buf, "%s_normal", name); 136 sprintf(buf, "%s_normal", name);
132 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); 137 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
133 if (btn->normal == NULL) { 138 if (btn->normal == NULL) {
134 printf("Missing normal button, this is not a correct button\n"); 139 printf("Missing normal button, this is not a correct button\n");
135 } 140 }
136 sprintf(buf, "%s_active", name); 141 sprintf(buf, "%s_active", name);
137 btn->active = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); 142 btn->active = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
138 if (btn->active == NULL) { 143 if (btn->active == NULL) {
139 printf("Missing active button, this is not a correct button\n"); 144 printf("Missing click button, this is not a correct button\n");
140 } 145 }
141 sprintf(buf, "%s_click", name); 146 sprintf(buf, "%s_click", name);
142 btn->click = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); 147 btn->click = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
143 if (btn->click == NULL) { 148 if (btn->active == NULL) {
144 printf("Missing click button, this is not a correct button\n"); 149 printf("Missing click button, this is not a correct button\n");
145 } 150 }
146 btn->press = NULL; 151 btn->press = NULL;
147 // Show only the normal button 152 // Show only the normal button
148 COORD_HIDE(btn->active); 153 coord_hide(btn->active);
149 COORD_HIDE(btn->click); 154 coord_hide(btn->click);
150 COORD_SHOW(btn->normal); 155 coord_show(btn->normal);
151 // Move to the same position 156 // Move to the same position
152 btn->active->matrix[2] = 200; 157 btn->active->matrix[2] = 200;
153 btn->active->matrix[5] = 200; 158 btn->active->matrix[5] = 200;
154 btn->normal->matrix[2] = 200; 159 btn->normal->matrix[2] = 200;
155 btn->normal->matrix[5] = 200; 160 btn->normal->matrix[5] = 200;
156 btn->click->matrix[2] = 200; 161 btn->click->matrix[2] = 200;
157 btn->click->matrix[5] = 200; 162 btn->click->matrix[5] = 200;
158 btn->en = en; 163 btn->en = app;
159 printf("btn->root=%x\n",CMOUSE(btn->root));
160 btn->obs_move = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn); 164 btn->obs_move = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn);
161 btn->obs_press = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn); 165 btn->obs_press = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn);
162 btn->obs_out = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn); 166 btn->obs_out = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn);
163 btn->progm = NULL; 167 btn->progm = NULL;
164 rdman_redraw_changed(en->rdman); 168 rdman_redraw_changed(MBAPP_RDMAN(arg));
165 return btn; 169 return btn;
166 } 170 }
167 171
168 172
169 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg) 173 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg)
170 { 174 {
171 b->press = h; 175 b->press = h;
172 b->arg = arg; 176 b->arg = arg;
173 } 177 }
174 178
175 engine_t *engine_init() 179 MBApp *MBApp_Init(char *module)
176 { 180 {
177 181 MBApp *app = (MBApp *) malloc(sizeof(MBApp));
178 X_MB_runtime_t *rt; 182 X_MB_runtime_t *rt;
183
179 rt = X_MB_new(":0.0", 800, 600); 184 rt = X_MB_new(":0.0", 800, 600);
180 engine_t *en = (engine_t *) malloc(sizeof(engine_t)); 185
181 186 app->rt = rt;
182 en->rt = rt; 187 app->rdman = X_MB_rdman(rt);
183 en->rdman = X_MB_rdman(rt); 188 app->rootsprite= sprite_load("button",app->rdman, app->rdman->root_coord);
184 return en; 189 return app;
185 } 190 }
186 191
187 void engine_mainloop(engine_t *en) 192 void MBApp_setData(MBApp *app,void *data)
193 {
194 app->private = (void *) data;
195 }
196
197 void MBApp_loop(MBApp *en)
188 { 198 {
189 /* 199 /*
190 * Start handle connections, includes one to X server. 200 * Start handle connections, includes one to X server.
191 * User start to interact with the application. 201 * User start to interact with the application.
192 */ 202 */
193 X_MB_handle_connection(en->rt); 203 X_MB_handle_connection(en->rt);
194 204
195 /* 205 /*
196 * Clean 206 * Clean
197 */ 207 */
198 menu_free(en->menu);
199 X_MB_free(en->rt); 208 X_MB_free(en->rt);
200 free(en); 209 free(en);
201 } 210 }
202 211
203 212
204 static void add_rect_move(event_t *evt, void *arg) 213 static void add_rect_move(event_t *evt, void *arg)
205 { 214 {
206 engine_t *en = (engine_t *) arg; 215 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData );
207 mouse_event_t *mev = (mouse_event_t *) evt; 216 mouse_event_t *mev = (mouse_event_t *) evt;
208 217
209 printf("resize rectangle\n"); 218 printf("resize rectangle\n");
210 sh_rect_set(en->rect, en->start_x, en->start_y, mev->x - en->start_x, mev->y-en->start_y,en->rx,en->ry); 219 sh_rect_set(en->rect, en->start_x, en->start_y, mev->x - en->start_x, mev->y-en->start_y,0,0);
211 rdman_shape_changed(en->rdman,en->rect); 220 rdman_shape_changed(MBAPP_RDMAN(arg),en->rect);
212 rdman_redraw_changed(en->rdman); 221 rdman_redraw_changed(MBAPP_RDMAN(arg));
213 } 222 }
214 223
215 static void add_rect_release(event_t *evt, void *arg) 224 static void add_rect_release(event_t *evt, void *arg)
216 { 225 {
217 engine_t *en = (engine_t *) arg; 226 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData );
218 mouse_event_t *mev = (mouse_event_t *) evt; 227 mouse_event_t *mev = (mouse_event_t *) evt;
219 228
220 printf("rectangle done\n"); 229 printf("rectangle done\n");
221 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs1); 230 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs1);
222 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs2); 231 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs2);
223 } 232 }
224 233
225 static void add_rect_2(event_t *evt, void *arg) 234 static void add_rect_2(event_t *evt, void *arg)
226 { 235 {
227 engine_t *en = (engine_t *) arg; 236 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData );
228 mouse_event_t *mev = (mouse_event_t *) evt; 237 mouse_event_t *mev = (mouse_event_t *) evt;
229 paint_t *color; 238 paint_t *color;
230 239
231 printf("select first point\n"); 240 printf("select first point\n");
232 // Add an rect path 241 // Add an rect path
233 242
234 en->start_x = mev->x; 243 en->start_x = mev->x;
235 en->start_y = mev->y; 244 en->start_y = mev->y;
236 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs1); 245 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs1);
237 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs2); 246 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs2);
238 en->obs1 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_MOVE, add_rect_move, en); 247 en->obs1 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_MOVE, add_rect_move, en);
239 en->obs2 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_BUT_RELEASE, add_rect_release, en); 248 en->obs2 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_BUT_RELEASE, add_rect_release, en);
240 } 249 }
241 250
242 static void add_rect_2_move(event_t *evt, void *arg) 251 static void add_rect_2_move(event_t *evt, void *arg)
243 { 252 {
244 engine_t *en = (engine_t *) arg; 253 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData );
245 mouse_event_t *mev = (mouse_event_t *) evt; 254 mouse_event_t *mev = (mouse_event_t *) evt;
246 255
247 sh_rect_set(en->rect, mev->x, mev->y, 50,50,en->rx,en->ry); 256 sh_rect_set(en->rect, mev->x, mev->y, 50,50,0,0);
248 rdman_shape_changed(en->rdman,en->rect); 257 rdman_shape_changed(MBAPP_RDMAN(arg),en->rect);
249 rdman_redraw_changed(en->rdman); 258 rdman_redraw_changed(MBAPP_RDMAN(arg));
250 } 259 }
251 260
252 static void add_rect(event_t *evt, void *arg) 261 static void add_rect(event_t *evt, void *arg)
253 { 262 {
254 engine_t *en = (engine_t *) arg; 263 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData );
255 mouse_event_t *mev = (mouse_event_t *) evt; 264 mouse_event_t *mev = (mouse_event_t *) evt;
256 paint_t *color; 265 paint_t *color;
257 266
258 printf("menut selected\n"); 267 printf("menut selected\n");
259 en->obs1 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_BUT_PRESS, add_rect_2, en); 268 en->obs1 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_BUT_PRESS, add_rect_2, en);
260 en->obs2 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_MOVE, add_rect_2_move, en); 269 en->obs2 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_MOVE, add_rect_2_move, en);
261 en->rect = rdman_shape_rect_new(en->rdman, mev->x, mev->y, 50 , 50, en->rx, en->ry); 270 en->rect = rdman_shape_rect_new(MBAPP_RDMAN(arg), mev->x, mev->y, 50 , 50, 0,0);
262 // Paint it with color 271 // Paint it with color
263 color = rdman_paint_color_new(en->rdman, 0.800000, 0.800000, 0.400000, 1.000000); 272 en->obs1 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_BUT_PRESS, add_rect_2, en);
264 rdman_paint_fill(en->rdman, color, en->rect); 273 en->obs2 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_MOVE, add_rect_2_move, en);
274 en->rect = rdman_shape_rect_new(MBAPP_RDMAN(arg), mev->x, mev->y, 50 , 50, 0,0);
275 // Paint it with color
276 color = rdman_paint_color_new(MBAPP_RDMAN(arg), 0.800000, 0.800000, 0.400000, 1.000000);
277 rdman_paint_fill(MBAPP_RDMAN(arg), color, en->rect);
265 // Add to the stage 278 // Add to the stage
266 rdman_add_shape(en->rdman, en->rect, en->menu->root_coord); 279 //rdman_add_shape(MBAPP_RDMAN(arg), en->rect, en->menu->root_coord);
267 } 280 }
268 281
269 282
270 void test(void *a) 283 void test(void *a)
271 { 284 {
272 printf("Button is pressed.....\n"); 285 printf("Button is pressed.....\n");
273 } 286 }
274 287
288 MBApp *myApp;
275 289
276 int main(int argc, char * const argv[]) { 290 int main(int argc, char * const argv[]) {
277 subject_t *subject; 291 subject_t *subject;
278 engine_t *en;
279 mb_button_t *b; 292 mb_button_t *b;
280 293 mb_obj_t *button;
281 en = engine_init(); 294 MyAppData data;
282 en->menu = menu_new(en->rdman, en->rdman->root_coord); 295
283 en->button = sprite_load("button",en->rdman, en->rdman->root_coord); 296 myApp = MBApp_Init("button");
284 b = mb_button_new(en, (mb_sprite_t *) en->button, "btn"); 297 MBApp_setData(myApp,&data);
298 b = mb_button_new(myApp, myApp->rootsprite, "btn");
285 mb_button_add_onClick(b, test,NULL); 299 mb_button_add_onClick(b, test,NULL);
286 300
287 en->rx = 0; 301 MBApp_loop(myApp);
288 en->ry = 0;
289
290 /*
291 * Register observers to subjects of events for objects.
292 */
293 subject = coord_get_mouse_event(en->menu->rect);
294 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, add_rect, en);
295
296
297 engine_mainloop(en);
298 302
299 return 0; 303 return 0;
300 } 304 }
301 305
302 /* vim: set ts=4 */ 306 /* vim: set ts=4 */