annotate examples/drag/main.c @ 874:ec8d7e9c9642 abs_n_rel_center

Decorate mb_rt created by mb_rt_with_win()
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 24 Sep 2010 23:47:01 +0800
parents 29e1b2bffe4c
children 7f49b501ac4d
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);
219
1eb9ee5ae4f2 Star in drag would be moved out the window border when cursor hit it.
Thinker K.F. Li <thinker@branda.to>
parents: 214
diff changeset
30 en->state = 0;
199
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
31 return en;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
32 }
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
33 void engine_close(engine_t *en)
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
34 {
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
35 /*
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
36 * Start handle connections, includes one to X server.
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
37 * User start to interact with the application.
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
38 */
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
39 X_MB_handle_connection(en->rt);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
40
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
41 /*
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
42 * Clean
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
43 */
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
44 menu_free(en->menu);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
45 X_MB_free(en->rt);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
46 free(en);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
47 }
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
48 #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
49 #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
50
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
51
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
52 static void cursor_press_handler(event_t *evt, void *arg) {
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
53 engine_t *en = (engine_t *) arg;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
54 mouse_event_t *mev = (mouse_event_t *) evt;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
55
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
56 en->start_x = mev->x;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
57 en->start_y = mev->y;
214
09ad4382aa41 Modify the drag example to use the sprite API.
wycc
parents: 199
diff changeset
58 en->orx = en->cursor->matrix[2];
09ad4382aa41 Modify the drag example to use the sprite API.
wycc
parents: 199
diff changeset
59 en->ory = en->cursor->matrix[5];
199
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
60 printf("pressed %g %g\n",en->orx,en->ory);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
61 en->state = 1;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
62 }
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
63
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
64 static void cursor_release_handler(event_t *evt, void *arg) {
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
65 engine_t *en = (engine_t *) arg;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
66 printf("up\n");
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
67 en->state = 0;
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_move_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 mouse_event_t *mev = (mouse_event_t *) evt;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
73
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
74 if (en->state) {
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
75 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
76 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
77 rdman_coord_changed(en->rdman, en->cursor);
199
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
78 /* Update changed part to UI. */
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
79 rdman_redraw_changed(en->rdman);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
80 }
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
81 }
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
82
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
83 int main(int argc, char * const argv[]) {
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
84 subject_t *subject;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
85 engine_t *en;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
86
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
87 en = engine_init();
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
88 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
89 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
90 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
91 printf("sprite=%x\n",&en->menu->lsym.sprite);
09ad4382aa41 Modify the drag example to use the sprite API.
wycc
parents: 199
diff changeset
92 printf("en->menu=%x\n",en->menu);
199
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
93
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
94 /*
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
95 * Register observers to subjects of events for objects.
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
96 */
214
09ad4382aa41 Modify the drag example to use the sprite API.
wycc
parents: 199
diff changeset
97 subject = coord_get_mouse_event(en->cursor);
199
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
98 subject_add_event_observer(subject, EVT_MOUSE_BUT_PRESS, cursor_press_handler, en);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
99 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, cursor_release_handler, en);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
100 subject_add_event_observer(subject, EVT_MOUSE_MOVE, cursor_move_handler, en);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
101
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
102
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
103 engine_close(en);
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
104
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
105 return 0;
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
106 }
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
107
55533146efdf Add drag example
wycc@wycc-desktop
parents:
diff changeset
108 /* vim: set ts=4 */