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