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