annotate examples/dynamic/mbbutton.c @ 247:d9a78c859660

Seperate the frameowrk codes from the main.c. Write a simpler MBAF demo hello program.
author wycc
date Thu, 01 Jan 2009 08:32:03 +0800
parents
children ab8284c8dcee
rev   line source
247
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
1
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
2 #include <stdio.h>
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
3 #include <mb.h>
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
4 #include <string.h>
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
5 #include "mbapp.h"
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
6
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
7
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
8
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
9 #define CMOUSE(e) (coord_get_mouse_event(e))
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
10 #define MBAPP_DATA(app,type) ((type *) ((app)->private))
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
11 #define MBAPP_RDMAN(app) (((MBApp *) app)->rdman)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
12
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
13
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
14 static void mb_button_pressed(event_t *evt, void *arg);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
15 static void mb_button_out(event_t *evt, void *arg);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
16
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
17 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
18 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
19 b->press = h;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
20 b->arg = arg;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
21 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
22
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
23 void mb_button_refresh(mb_button_t *btn)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
24 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
25 rdman_coord_changed(btn->en->rdman,btn->root);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
26 rdman_redraw_changed(btn->en->rdman);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
27 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
28
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
29 static void mb_button_move(event_t *evt, void *arg)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
30 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
31 mb_button_t *btn = (mb_button_t *) arg;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
32 MBApp *en = btn->en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
33
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
34
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
35 printf("Mouse move\n");
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
36 arg = (void *)en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
37 coord_show(btn->active);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
38 mb_button_refresh(btn);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
39 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
40 static void mb_button_out(event_t *evt, void *arg)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
41 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
42 mb_button_t *btn = (mb_button_t *) arg;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
43 MBApp *en = btn->en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
44 arg = (void *) en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
45
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
46 if (btn->progm) {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
47 mb_progm_abort(btn->progm);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
48 btn->progm = NULL;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
49 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
50 printf("mouse out\n");
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
51 coord_hide(btn->click);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
52 coord_hide(btn->active);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
53 coord_show(btn->normal);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
54 mb_button_refresh(btn);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
55 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
56
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
57 static void mb_button_show_active(event_t *evt, void *arg)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
58 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
59 mb_button_t *btn = (mb_button_t *) arg;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
60 MBApp *en = btn->en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
61
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
62 coord_show(btn->active);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
63 mb_button_refresh(btn);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
64 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
65
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
66 static void mb_button_pressed(event_t *evt, void *arg)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
67 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
68 mb_button_t *btn = (mb_button_t *) arg;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
69 MBApp *en = btn->en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
70 mb_timeval_t start, playing, now;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
71 mb_progm_t *progm;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
72 mb_word_t *word;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
73 arg = (void *) en;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
74
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
75 printf("Pressed\n");
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
76 if (btn->progm) {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
77 mb_progm_abort(btn->progm);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
78 btn->progm = NULL;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
79 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
80 coord_show(btn->click);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
81 coord_hide(btn->active);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
82 rdman_coord_changed(MBAPP_RDMAN(arg),btn->root);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
83 rdman_redraw_changed(MBAPP_RDMAN(arg));
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
84
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
85 btn->progm = progm = mb_progm_new(1, MBAPP_RDMAN(arg));
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
86 MB_TIMEVAL_SET(&start, 0, 500000);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
87 MB_TIMEVAL_SET(&playing, 0, 0);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
88 word = mb_progm_next_word(progm, &start, &playing);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
89 mb_visibility_new(VIS_HIDDEN, btn->click, word);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
90 mb_visibility_new(VIS_VISIBLE, btn->active, word);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
91 mb_progm_free_completed(progm);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
92 get_now(&now);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
93 mb_progm_start(progm, X_MB_tman(en->rt), &now);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
94 if (btn->press)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
95 btn->press(btn->arg);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
96 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
97 mb_button_t *mb_button_new(MBApp *app,mb_sprite_t *sp, char *name)
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
98 {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
99 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t));
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
100 char *buf = (char *) malloc(strlen(name)+5);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
101 MBApp *arg = app;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
102
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
103 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
104 sprintf(buf, "%s_normal", name);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
105 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
106 if (btn->normal == NULL) {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
107 printf("Missing normal button, this is not a correct button\n");
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
108 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
109 sprintf(buf, "%s_active", name);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
110 btn->active = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
111 if (btn->active == NULL) {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
112 printf("Missing click button, this is not a correct button\n");
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
113 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
114 sprintf(buf, "%s_click", name);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
115 btn->click = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
116 if (btn->active == NULL) {
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
117 printf("Missing click button, this is not a correct button\n");
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
118 }
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
119 btn->press = NULL;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
120 // Show only the normal button
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
121 coord_hide(btn->active);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
122 coord_hide(btn->click);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
123 coord_show(btn->normal);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
124 // Move to the same position
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
125 btn->active->matrix[2] = 200;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
126 btn->active->matrix[5] = 200;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
127 btn->normal->matrix[2] = 200;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
128 btn->normal->matrix[5] = 200;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
129 btn->click->matrix[2] = 200;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
130 btn->click->matrix[5] = 200;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
131 btn->en = app;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
132 btn->obs_move = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
133 btn->obs_press = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
134 btn->obs_out = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn);
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
135 btn->progm = NULL;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
136 rdman_redraw_changed(MBAPP_RDMAN(arg));
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
137 return btn;
d9a78c859660 Seperate the frameowrk codes from the main.c.
wycc
parents:
diff changeset
138 }