199
|
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
|
|
11 typedef struct _engine engine_t;
|
|
12 struct _engine {
|
|
13 X_MB_runtime_t *rt;
|
|
14 redraw_man_t *rdman;
|
|
15 menu_t *menu;
|
|
16 int state;
|
|
17 co_aix orx,ory;
|
|
18 int start_x,start_y;
|
214
|
19 coord_t *cursor;
|
199
|
20 };
|
|
21 engine_t *engine_init()
|
|
22 {
|
|
23
|
|
24 X_MB_runtime_t *rt;
|
|
25 rt = X_MB_new(":0.0", 800, 600);
|
|
26 engine_t *en = (engine_t *) malloc(sizeof(engine_t));
|
|
27
|
|
28 en->rt = rt;
|
|
29 en->rdman = X_MB_rdman(rt);
|
|
30 return en;
|
|
31 }
|
|
32 void engine_close(engine_t *en)
|
|
33 {
|
|
34 /*
|
|
35 * Start handle connections, includes one to X server.
|
|
36 * User start to interact with the application.
|
|
37 */
|
|
38 X_MB_handle_connection(en->rt);
|
|
39
|
|
40 /*
|
|
41 * Clean
|
|
42 */
|
|
43 menu_free(en->menu);
|
|
44 X_MB_free(en->rt);
|
|
45 free(en);
|
|
46 }
|
|
47 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(X_MB_rdman(ex_rt->rt), group)
|
|
48 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(X_MB_rdman(ex_rt->rt), group)
|
|
49
|
|
50
|
|
51 void coord_move(coord_t *c, co_aix x, co_aix y)
|
|
52 {
|
|
53 c->matrix[2] = x;
|
|
54 c->matrix[5] = y;
|
|
55 }
|
|
56
|
|
57
|
|
58 static void cursor_press_handler(event_t *evt, void *arg) {
|
|
59 engine_t *en = (engine_t *) arg;
|
|
60 mouse_event_t *mev = (mouse_event_t *) evt;
|
|
61
|
|
62 en->start_x = mev->x;
|
|
63 en->start_y = mev->y;
|
214
|
64 en->orx = en->cursor->matrix[2];
|
|
65 en->ory = en->cursor->matrix[5];
|
199
|
66 printf("pressed %g %g\n",en->orx,en->ory);
|
|
67 en->state = 1;
|
|
68 }
|
|
69
|
|
70 static void cursor_release_handler(event_t *evt, void *arg) {
|
|
71 engine_t *en = (engine_t *) arg;
|
|
72 printf("up\n");
|
|
73 en->state = 0;
|
|
74 }
|
|
75
|
|
76 static void cursor_move_handler(event_t *evt, void *arg) {
|
|
77 engine_t *en = (engine_t *) arg;
|
|
78 mouse_event_t *mev = (mouse_event_t *) evt;
|
|
79
|
|
80 if (en->state) {
|
|
81 printf("move to (%d %d)\n", mev->x,mev->y);
|
214
|
82 coord_move(en->cursor,en->orx + (mev->x-en->start_x),en->ory + (mev->y-en->start_y));
|
|
83 rdman_coord_changed(en->rdman, en->cursor);
|
199
|
84 /* Update changed part to UI. */
|
|
85 rdman_redraw_changed(en->rdman);
|
|
86 }
|
|
87 }
|
|
88
|
|
89 int main(int argc, char * const argv[]) {
|
|
90 subject_t *subject;
|
|
91 engine_t *en;
|
|
92
|
|
93 en = engine_init();
|
|
94 en->menu = menu_new(en->rdman, en->rdman->root_coord);
|
214
|
95 en->cursor = (coord_t *) MB_SPRITE_GET_OBJ(&en->menu->lsym.sprite, "star");
|
|
96 printf("en->cursor=%x star=%x\n",en->cursor,en->menu->star);
|
|
97 printf("sprite=%x\n",&en->menu->lsym.sprite);
|
|
98 printf("en->menu=%x\n",en->menu);
|
199
|
99
|
|
100 /*
|
|
101 * Register observers to subjects of events for objects.
|
|
102 */
|
214
|
103 subject = coord_get_mouse_event(en->cursor);
|
199
|
104 subject_add_event_observer(subject, EVT_MOUSE_BUT_PRESS, cursor_press_handler, en);
|
|
105 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, cursor_release_handler, en);
|
|
106 subject_add_event_observer(subject, EVT_MOUSE_MOVE, cursor_move_handler, en);
|
|
107
|
|
108
|
|
109 engine_close(en);
|
|
110
|
|
111 return 0;
|
|
112 }
|
|
113
|
|
114 /* vim: set ts=4 */
|