Mercurial > MadButterfly
annotate examples/dynamic/main.c @ 247:d9a78c859660
Seperate the frameowrk codes from the main.c.
Write a simpler MBAF demo hello program.
author | wycc |
---|---|
date | Thu, 01 Jan 2009 08:32:03 +0800 |
parents | 81458bb0bf34 |
children | ab8284c8dcee |
rev | line source |
---|---|
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
1 /*! \file |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
2 * |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
3 * svg2code_ex is an example that show programmers how to create a |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
4 * menu with MadButterfly. |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
5 * |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
6 */ |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
7 #include <stdio.h> |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
8 #include <mb.h> |
217
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
9 #include <string.h> |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
10 #include "menu.h" |
247 | 11 #include "mbapp.h" |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
12 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
13 |
240 | 14 |
15 typedef struct { | |
16 shape_t *rect; | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
17 co_aix orx,ory; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
18 int start_x,start_y; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
19 observer_t *obs1,*obs2; |
242
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
20 int currentscene; |
240 | 21 }MyAppData; |
22 | |
217
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
23 |
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
24 #define CMOUSE(e) (coord_get_mouse_event(e)) |
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
25 |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
26 static void add_rect_move(event_t *evt, void *arg) |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
27 { |
240 | 28 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
29 mouse_event_t *mev = (mouse_event_t *) evt; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
30 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
31 printf("resize rectangle\n"); |
240 | 32 sh_rect_set(en->rect, en->start_x, en->start_y, mev->x - en->start_x, mev->y-en->start_y,0,0); |
33 rdman_shape_changed(MBAPP_RDMAN(arg),en->rect); | |
34 rdman_redraw_changed(MBAPP_RDMAN(arg)); | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
35 } |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
36 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
37 static void add_rect_release(event_t *evt, void *arg) |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
38 { |
240 | 39 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
40 mouse_event_t *mev = (mouse_event_t *) evt; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
41 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
42 printf("rectangle done\n"); |
240 | 43 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs1); |
44 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs2); | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
45 } |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
46 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
47 static void add_rect_2(event_t *evt, void *arg) |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
48 { |
240 | 49 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
50 mouse_event_t *mev = (mouse_event_t *) evt; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
51 paint_t *color; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
52 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
53 printf("select first point\n"); |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
54 // Add an rect path |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
55 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
56 en->start_x = mev->x; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
57 en->start_y = mev->y; |
240 | 58 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs1); |
59 subject_remove_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), en->obs2); | |
60 en->obs1 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_MOVE, add_rect_move, en); | |
61 en->obs2 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_BUT_RELEASE, add_rect_release, en); | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
62 } |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
63 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
64 static void add_rect_2_move(event_t *evt, void *arg) |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
65 { |
240 | 66 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
67 mouse_event_t *mev = (mouse_event_t *) evt; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
68 |
240 | 69 sh_rect_set(en->rect, mev->x, mev->y, 50,50,0,0); |
70 rdman_shape_changed(MBAPP_RDMAN(arg),en->rect); | |
71 rdman_redraw_changed(MBAPP_RDMAN(arg)); | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
72 } |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
73 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
74 static void add_rect(event_t *evt, void *arg) |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
75 { |
240 | 76 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
77 mouse_event_t *mev = (mouse_event_t *) evt; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
78 paint_t *color; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
79 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
80 printf("menut selected\n"); |
240 | 81 en->obs1 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_BUT_PRESS, add_rect_2, en); |
82 en->obs2 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_MOVE, add_rect_2_move, en); | |
83 en->rect = rdman_shape_rect_new(MBAPP_RDMAN(arg), mev->x, mev->y, 50 , 50, 0,0); | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
84 // Paint it with color |
240 | 85 en->obs1 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_BUT_PRESS, add_rect_2, en); |
86 en->obs2 = subject_add_event_observer(CMOUSE(MBAPP_RDMAN(arg)->root_coord), EVT_MOUSE_MOVE, add_rect_2_move, en); | |
87 en->rect = rdman_shape_rect_new(MBAPP_RDMAN(arg), mev->x, mev->y, 50 , 50, 0,0); | |
88 // Paint it with color | |
89 color = rdman_paint_color_new(MBAPP_RDMAN(arg), 0.800000, 0.800000, 0.400000, 1.000000); | |
90 rdman_paint_fill(MBAPP_RDMAN(arg), color, en->rect); | |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
91 // Add to the stage |
240 | 92 //rdman_add_shape(MBAPP_RDMAN(arg), en->rect, en->menu->root_coord); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
93 } |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
94 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
95 |
217
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
96 void test(void *a) |
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
97 { |
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
98 printf("Button is pressed.....\n"); |
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
99 } |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
100 |
240 | 101 MBApp *myApp; |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
102 |
242
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
103 void switch_scene(const mb_timeval_t *tmo, const mb_timeval_t *now,void *arg) |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
104 { |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
105 MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
106 mb_timeval_t timer,interval; |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
107 |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
108 |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
109 get_now(&timer); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
110 MB_TIMEVAL_SET(&interval, 1 ,0); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
111 MB_TIMEVAL_ADD(&timer, &interval); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
112 mb_tman_timeout( MBApp_getTimer(myApp), &timer, switch_scene, myApp); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
113 |
244
d36abace2ce4
Fix bug in *_goto_scene()
Thinker K.F. Li <thinker@branda.to>
parents:
242
diff
changeset
|
114 en->currentscene = (en->currentscene + 1) % 2; |
d36abace2ce4
Fix bug in *_goto_scene()
Thinker K.F. Li <thinker@branda.to>
parents:
242
diff
changeset
|
115 printf("switch to scene %d\n", en->currentscene + 1); |
d36abace2ce4
Fix bug in *_goto_scene()
Thinker K.F. Li <thinker@branda.to>
parents:
242
diff
changeset
|
116 MB_SPRITE_GOTO_SCENE(myApp->rootsprite,en->currentscene + 1); |
242
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
117 } |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
118 |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
119 int main(int argc, char * const argv[]) { |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
120 subject_t *subject; |
217
8d9d717c9300
Add sample code fro mb_button_t. If everything is OK, I will move it to the main src tree.
wycc@wycc-desktop
parents:
201
diff
changeset
|
121 mb_button_t *b; |
240 | 122 mb_obj_t *button; |
123 MyAppData data; | |
242
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
124 mb_timeval_t tmo,interval; |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
125 |
242
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
126 if (argc > 1) |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
127 myApp = MBApp_Init(argv[1]); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
128 else |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
129 myApp = MBApp_Init("scene"); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
130 data.currentscene=0; |
240 | 131 MBApp_setData(myApp,&data); |
242
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
132 //b = mb_button_new(myApp, myApp->rootsprite, "btn"); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
133 //mb_button_add_onClick(b, test,NULL); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
134 get_now(&tmo); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
135 MB_TIMEVAL_SET(&interval, 1 ,0); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
136 mb_tman_timeout( MBApp_getTimer(myApp), &tmo, switch_scene, myApp); |
d3fe0a0f3a8b
Implement MBApp API and modify the dynamic example to use this API.
wycc
parents:
240
diff
changeset
|
137 |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
138 |
240 | 139 MBApp_loop(myApp); |
201
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
140 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
141 return 0; |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
142 } |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
143 |
31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
wycc@wycc-desktop
parents:
diff
changeset
|
144 /* vim: set ts=4 */ |