Mercurial > MadButterfly
annotate examples/drag/main.c @ 1472:7cb7abb5063b
Refactory drawing functions into classes for state and transition
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 24 Apr 2011 15:13:41 +0800 |
parents | 7f49b501ac4d |
children |
rev | line source |
---|---|
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 { | |
1028
7f49b501ac4d
Migrate examples/drag to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents:
224
diff
changeset
|
13 mb_rt_t *rt; |
199 | 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 | |
1028
7f49b501ac4d
Migrate examples/drag to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents:
224
diff
changeset
|
24 mb_rt_t *rt; |
7f49b501ac4d
Migrate examples/drag to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents:
224
diff
changeset
|
25 rt = mb_runtime_new(":0.0", 800, 600); |
199 | 26 engine_t *en = (engine_t *) malloc(sizeof(engine_t)); |
27 | |
28 en->rt = rt; | |
1028
7f49b501ac4d
Migrate examples/drag to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents:
224
diff
changeset
|
29 en->rdman = mb_runtime_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 | 31 return en; |
32 } | |
33 void engine_close(engine_t *en) | |
34 { | |
35 /* | |
36 * Start handle connections, includes one to X server. | |
37 * User start to interact with the application. | |
38 */ | |
1028
7f49b501ac4d
Migrate examples/drag to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents:
224
diff
changeset
|
39 mb_runtime_event_loop(en->rt); |
199 | 40 |
41 /* | |
42 * Clean | |
43 */ | |
44 menu_free(en->menu); | |
1028
7f49b501ac4d
Migrate examples/drag to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents:
224
diff
changeset
|
45 mb_runtime_free(en->rt); |
199 | 46 free(en); |
47 } | |
48 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(X_MB_rdman(ex_rt->rt), group) | |
49 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(X_MB_rdman(ex_rt->rt), group) | |
50 | |
51 | |
52 static void cursor_press_handler(event_t *evt, void *arg) { | |
53 engine_t *en = (engine_t *) arg; | |
54 mouse_event_t *mev = (mouse_event_t *) evt; | |
55 | |
56 en->start_x = mev->x; | |
57 en->start_y = mev->y; | |
214 | 58 en->orx = en->cursor->matrix[2]; |
59 en->ory = en->cursor->matrix[5]; | |
199 | 60 printf("pressed %g %g\n",en->orx,en->ory); |
61 en->state = 1; | |
62 } | |
63 | |
64 static void cursor_release_handler(event_t *evt, void *arg) { | |
65 engine_t *en = (engine_t *) arg; | |
66 printf("up\n"); | |
67 en->state = 0; | |
68 } | |
69 | |
70 static void cursor_move_handler(event_t *evt, void *arg) { | |
71 engine_t *en = (engine_t *) arg; | |
72 mouse_event_t *mev = (mouse_event_t *) evt; | |
73 | |
74 if (en->state) { | |
75 printf("move to (%d %d)\n", mev->x,mev->y); | |
214 | 76 coord_move(en->cursor,en->orx + (mev->x-en->start_x),en->ory + (mev->y-en->start_y)); |
77 rdman_coord_changed(en->rdman, en->cursor); | |
199 | 78 /* Update changed part to UI. */ |
79 rdman_redraw_changed(en->rdman); | |
80 } | |
81 } | |
82 | |
83 int main(int argc, char * const argv[]) { | |
84 subject_t *subject; | |
85 engine_t *en; | |
86 | |
87 en = engine_init(); | |
88 en->menu = menu_new(en->rdman, en->rdman->root_coord); | |
214 | 89 en->cursor = (coord_t *) MB_SPRITE_GET_OBJ(&en->menu->lsym.sprite, "star"); |
90 printf("en->cursor=%x star=%x\n",en->cursor,en->menu->star); | |
91 printf("sprite=%x\n",&en->menu->lsym.sprite); | |
92 printf("en->menu=%x\n",en->menu); | |
199 | 93 |
94 /* | |
95 * Register observers to subjects of events for objects. | |
96 */ | |
214 | 97 subject = coord_get_mouse_event(en->cursor); |
199 | 98 subject_add_event_observer(subject, EVT_MOUSE_BUT_PRESS, cursor_press_handler, en); |
99 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, cursor_release_handler, en); | |
100 subject_add_event_observer(subject, EVT_MOUSE_MOVE, cursor_move_handler, en); | |
101 | |
102 | |
103 engine_close(en); | |
104 | |
105 return 0; | |
106 } | |
107 | |
108 /* vim: set ts=4 */ |