annotate examples/drag/main.c @ 214:09ad4382aa41

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