Mercurial > MadButterfly
comparison examples/dynamic/main.c @ 201:31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
author | wycc@wycc-desktop |
---|---|
date | Sun, 07 Dec 2008 18:00:06 +0800 |
parents | |
children | 8d9d717c9300 |
comparison
equal
deleted
inserted
replaced
200:1528209c6c92 | 201:31933f9ee70e |
---|---|
1 /*! \file | |
2 * | |
3 * svg2code_ex is an example that show programmers how to create a | |
4 * menu with MadButterfly. | |
5 * | |
6 */ | |
7 #include <stdio.h> | |
8 #include <mb.h> | |
9 #include "menu.h" | |
10 #include "button.h" | |
11 | |
12 | |
13 typedef struct _engine engine_t; | |
14 struct _engine { | |
15 X_MB_runtime_t *rt; | |
16 redraw_man_t *rdman; | |
17 menu_t *menu; | |
18 button_t *button; | |
19 int state; | |
20 co_aix orx,ory; | |
21 int start_x,start_y; | |
22 observer_t *obs1,*obs2; | |
23 shape_t *rect; | |
24 co_aix rx,ry; | |
25 }; | |
26 engine_t *engine_init() | |
27 { | |
28 | |
29 X_MB_runtime_t *rt; | |
30 rt = X_MB_new(":0.0", 800, 600); | |
31 engine_t *en = (engine_t *) malloc(sizeof(engine_t)); | |
32 | |
33 en->rt = rt; | |
34 en->rdman = X_MB_rdman(rt); | |
35 return en; | |
36 } | |
37 | |
38 void engine_close(engine_t *en) | |
39 { | |
40 /* | |
41 * Start handle connections, includes one to X server. | |
42 * User start to interact with the application. | |
43 */ | |
44 X_MB_handle_connection(en->rt); | |
45 | |
46 /* | |
47 * Clean | |
48 */ | |
49 menu_free(en->menu); | |
50 X_MB_free(en->rt); | |
51 free(en); | |
52 } | |
53 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(en->rdman, group) | |
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 | |
99 | |
100 void coord_move(coord_t *c, co_aix x, co_aix y) | |
101 { | |
102 c->matrix[2] = x; | |
103 c->matrix[5] = y; | |
104 } | |
105 | |
106 | |
107 | |
108 static void add_rect_move(event_t *evt, void *arg) | |
109 { | |
110 engine_t *en = (engine_t *) arg; | |
111 mouse_event_t *mev = (mouse_event_t *) evt; | |
112 | |
113 printf("resize rectangle\n"); | |
114 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); | |
115 rdman_shape_changed(en->rdman,en->rect); | |
116 rdman_redraw_changed(en->rdman); | |
117 } | |
118 | |
119 static void add_rect_release(event_t *evt, void *arg) | |
120 { | |
121 engine_t *en = (engine_t *) arg; | |
122 mouse_event_t *mev = (mouse_event_t *) evt; | |
123 | |
124 printf("rectangle done\n"); | |
125 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs1); | |
126 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs2); | |
127 } | |
128 | |
129 static void add_rect_2(event_t *evt, void *arg) | |
130 { | |
131 engine_t *en = (engine_t *) arg; | |
132 mouse_event_t *mev = (mouse_event_t *) evt; | |
133 paint_t *color; | |
134 | |
135 printf("select first point\n"); | |
136 // Add an rect path | |
137 | |
138 en->start_x = mev->x; | |
139 en->start_y = mev->y; | |
140 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs1); | |
141 subject_remove_observer(CMOUSE(en->rdman->root_coord), en->obs2); | |
142 en->obs1 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_MOVE, add_rect_move, en); | |
143 en->obs2 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_BUT_RELEASE, add_rect_release, en); | |
144 } | |
145 | |
146 static void add_rect_2_move(event_t *evt, void *arg) | |
147 { | |
148 engine_t *en = (engine_t *) arg; | |
149 mouse_event_t *mev = (mouse_event_t *) evt; | |
150 | |
151 sh_rect_set(en->rect, mev->x, mev->y, 50,50,en->rx,en->ry); | |
152 rdman_shape_changed(en->rdman,en->rect); | |
153 rdman_redraw_changed(en->rdman); | |
154 } | |
155 | |
156 static void add_rect(event_t *evt, void *arg) | |
157 { | |
158 engine_t *en = (engine_t *) arg; | |
159 mouse_event_t *mev = (mouse_event_t *) evt; | |
160 paint_t *color; | |
161 | |
162 printf("menut selected\n"); | |
163 en->obs1 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_BUT_PRESS, add_rect_2, en); | |
164 en->obs2 = subject_add_event_observer(CMOUSE(en->rdman->root_coord), EVT_MOUSE_MOVE, add_rect_2_move, en); | |
165 en->rect = rdman_shape_rect_new(en->rdman, mev->x, mev->y, 50 , 50, en->rx, en->ry); | |
166 // Paint it with color | |
167 color = rdman_paint_color_new(en->rdman, 0.800000, 0.800000, 0.400000, 1.000000); | |
168 rdman_paint_fill(en->rdman, color, en->rect); | |
169 // Add to the stage | |
170 rdman_add_shape(en->rdman, en->rect, en->menu->root_coord); | |
171 } | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 int main(int argc, char * const argv[]) { | |
178 subject_t *subject; | |
179 engine_t *en; | |
180 | |
181 en = engine_init(); | |
182 en->menu = menu_new(en->rdman, en->rdman->root_coord); | |
183 en->button = button_new(en->rdman, en->rdman->root_coord); | |
184 engine_add_button(en, en->button->normal, en->button->active, button_pressed); | |
185 | |
186 en->rx = 0; | |
187 en->ry = 0; | |
188 | |
189 /* | |
190 * Register observers to subjects of events for objects. | |
191 */ | |
192 subject = coord_get_mouse_event(en->menu->rect); | |
193 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, add_rect, en); | |
194 | |
195 | |
196 engine_close(en); | |
197 | |
198 return 0; | |
199 } | |
200 | |
201 /* vim: set ts=4 */ |